dop-export.php
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | // Check setting |
| 7 | if(!acf_get_setting('acfe/modules/dynamic_options_pages')) |
| 8 | return; |
| 9 | |
| 10 | if(!class_exists('ACFE_Admin_Tool_Export_DOP')): |
| 11 | |
| 12 | class ACFE_Admin_Tool_Export_DOP extends ACF_Admin_Tool{ |
| 13 | |
| 14 | public $action = false; |
| 15 | public $data = array(); |
| 16 | |
| 17 | function initialize(){ |
| 18 | |
| 19 | // vars |
| 20 | $this->name = 'acfe_tool_dop_export'; |
| 21 | $this->title = __('Export Options Pages'); |
| 22 | $this->icon = 'dashicons-upload'; |
| 23 | |
| 24 | } |
| 25 | |
| 26 | function html(){ |
| 27 | |
| 28 | // Single |
| 29 | if($this->is_active()){ |
| 30 | |
| 31 | $this->html_single(); |
| 32 | |
| 33 | |
| 34 | // Archive |
| 35 | }else{ |
| 36 | |
| 37 | $this->html_archive(); |
| 38 | |
| 39 | } |
| 40 | |
| 41 | } |
| 42 | |
| 43 | function html_archive(){ |
| 44 | |
| 45 | // vars |
| 46 | $choices = array(); |
| 47 | |
| 48 | $dynamic_options_pages = acfe_settings('modules.dynamic_option.data'); |
| 49 | |
| 50 | if($dynamic_options_pages){ |
| 51 | foreach($dynamic_options_pages as $options_page_name => $args){ |
| 52 | |
| 53 | $choices[$options_page_name] = esc_html($args['page_title']); |
| 54 | |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | ?> |
| 59 | <p><?php _e('Export Options Pages', 'acf'); ?></p> |
| 60 | |
| 61 | <div class="acf-fields"> |
| 62 | <?php |
| 63 | |
| 64 | if(!empty($choices)){ |
| 65 | |
| 66 | // render |
| 67 | acf_render_field_wrap(array( |
| 68 | 'label' => __('Select Options Pages', 'acf'), |
| 69 | 'type' => 'checkbox', |
| 70 | 'name' => 'keys', |
| 71 | 'prefix' => false, |
| 72 | 'value' => false, |
| 73 | 'toggle' => true, |
| 74 | 'choices' => $choices, |
| 75 | )); |
| 76 | |
| 77 | } |
| 78 | |
| 79 | else{ |
| 80 | |
| 81 | echo '<div style="padding:15px 12px;">'; |
| 82 | _e('No options page available.'); |
| 83 | echo '</div>'; |
| 84 | |
| 85 | } |
| 86 | |
| 87 | ?> |
| 88 | </div> |
| 89 | |
| 90 | <?php |
| 91 | |
| 92 | $disabled = ''; |
| 93 | if(empty($choices)) |
| 94 | $disabled = 'disabled="disabled"'; |
| 95 | |
| 96 | ?> |
| 97 | |
| 98 | <p class="acf-submit"> |
| 99 | <button type="submit" name="action" class="button button-primary" value="json" <?php echo $disabled; ?>><?php _e('Export File'); ?></button> |
| 100 | <button type="submit" name="action" class="button" value="php" <?php echo $disabled; ?>><?php _e('Generate PHP'); ?></button> |
| 101 | </p> |
| 102 | <?php |
| 103 | |
| 104 | } |
| 105 | |
| 106 | function html_single(){ |
| 107 | |
| 108 | ?> |
| 109 | <div class="acf-postbox-columns"> |
| 110 | <div class="acf-postbox-main"> |
| 111 | |
| 112 | <?php |
| 113 | // prevent default translation and fake __() within string |
| 114 | acf_update_setting('l10n_var_export', true); |
| 115 | |
| 116 | $str_replace = array( |
| 117 | " " => "\t", |
| 118 | "'!!__(!!\'" => "__('", |
| 119 | "!!\', !!\'" => "', '", |
| 120 | "!!\')!!'" => "')", |
| 121 | "array (" => "array(" |
| 122 | ); |
| 123 | |
| 124 | $preg_replace = array( |
| 125 | '/([\t\r\n]+?)array/' => 'array', |
| 126 | '/[0-9]+ => array/' => 'array' |
| 127 | ); |
| 128 | |
| 129 | // Get settings. |
| 130 | $l10n = acf_get_setting('l10n'); |
| 131 | $l10n_textdomain = acf_get_setting('l10n_textdomain'); |
| 132 | |
| 133 | ?> |
| 134 | <p><?php _e("The following code can be used to register an options page. Simply copy and paste the following code to your theme's functions.php file or include it within an external file.", 'acf'); ?></p> |
| 135 | |
| 136 | <div id="acf-admin-tool-export"> |
| 137 | |
| 138 | <textarea id="acf-export-textarea" readonly="true"><?php |
| 139 | |
| 140 | echo "if( function_exists('acf_add_options_page') ):" . "\r\n" . "\r\n"; |
| 141 | |
| 142 | foreach($this->data as $args){ |
| 143 | |
| 144 | // Translate settings if textdomain is set. |
| 145 | if($l10n && $l10n_textdomain){ |
| 146 | |
| 147 | $args['page_title'] = acf_translate($args['page_title']); |
| 148 | $args['menu_title'] = acf_translate($args['menu_title']); |
| 149 | $args['update_button'] = acf_translate($args['update_button']); |
| 150 | $args['updated_message'] = acf_translate($args['updated_message']); |
| 151 | |
| 152 | } |
| 153 | |
| 154 | // code |
| 155 | $code = var_export($args, true); |
| 156 | |
| 157 | |
| 158 | // change double spaces to tabs |
| 159 | $code = str_replace( array_keys($str_replace), array_values($str_replace), $code ); |
| 160 | |
| 161 | |
| 162 | // correctly formats "=> array(" |
| 163 | $code = preg_replace( array_keys($preg_replace), array_values($preg_replace), $code ); |
| 164 | |
| 165 | |
| 166 | // esc_textarea |
| 167 | $code = esc_textarea( $code ); |
| 168 | |
| 169 | |
| 170 | // echo |
| 171 | echo "acf_add_options_page({$code});" . "\r\n" . "\r\n"; |
| 172 | |
| 173 | } |
| 174 | |
| 175 | echo "endif;"; |
| 176 | |
| 177 | ?></textarea> |
| 178 | |
| 179 | </div> |
| 180 | |
| 181 | <p class="acf-submit"> |
| 182 | <a class="button" id="acf-export-copy"><?php _e( 'Copy to clipboard', 'acf' ); ?></a> |
| 183 | </p> |
| 184 | <script type="text/javascript"> |
| 185 | (function($){ |
| 186 | |
| 187 | // vars |
| 188 | var $a = $('#acf-export-copy'); |
| 189 | var $textarea = $('#acf-export-textarea'); |
| 190 | |
| 191 | |
| 192 | // remove $a if 'copy' is not supported |
| 193 | if( !document.queryCommandSupported('copy') ) { |
| 194 | return $a.remove(); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | // event |
| 199 | $a.on('click', function( e ){ |
| 200 | |
| 201 | // prevent default |
| 202 | e.preventDefault(); |
| 203 | |
| 204 | |
| 205 | // select |
| 206 | $textarea.get(0).select(); |
| 207 | |
| 208 | |
| 209 | // try |
| 210 | try { |
| 211 | |
| 212 | // copy |
| 213 | var copy = document.execCommand('copy'); |
| 214 | if( !copy ) return; |
| 215 | |
| 216 | |
| 217 | // tooltip |
| 218 | acf.newTooltip({ |
| 219 | text: "<?php _e('Copied', 'acf' ); ?>", |
| 220 | timeout: 250, |
| 221 | target: $(this), |
| 222 | }); |
| 223 | |
| 224 | } catch (err) { |
| 225 | |
| 226 | // do nothing |
| 227 | |
| 228 | } |
| 229 | |
| 230 | }); |
| 231 | |
| 232 | })(jQuery); |
| 233 | </script> |
| 234 | </div> |
| 235 | </div> |
| 236 | <?php |
| 237 | |
| 238 | } |
| 239 | |
| 240 | function load(){ |
| 241 | |
| 242 | if($this->is_active()){ |
| 243 | |
| 244 | $this->action = $this->get_action(); |
| 245 | $this->data = $this->get_selected(); |
| 246 | |
| 247 | // Json submit |
| 248 | if($this->action === 'json') |
| 249 | $this->submit(); |
| 250 | |
| 251 | // add notice |
| 252 | if(!empty($this->data)){ |
| 253 | |
| 254 | $count = count($this->data); |
| 255 | $text = sprintf(_n( 'Exported 1 option page.', 'Exported %s option pages.', $count, 'acf' ), $count); |
| 256 | |
| 257 | acf_add_admin_notice($text, 'success'); |
| 258 | |
| 259 | } |
| 260 | |
| 261 | } |
| 262 | |
| 263 | } |
| 264 | |
| 265 | function submit(){ |
| 266 | |
| 267 | $this->action = $this->get_action(); |
| 268 | $this->data = $this->get_selected(); |
| 269 | |
| 270 | // validate |
| 271 | if($this->data === false) |
| 272 | return acf_add_admin_notice(__('No options page selected'), 'warning'); |
| 273 | |
| 274 | $keys = array(); |
| 275 | foreach($this->data as $key => $args){ |
| 276 | |
| 277 | $keys[] = $key; |
| 278 | |
| 279 | } |
| 280 | |
| 281 | if($this->action === 'json'){ |
| 282 | |
| 283 | // Prefix |
| 284 | $prefix = (count($keys) > 1) ? 'options-pages' : 'options-page'; |
| 285 | |
| 286 | // Slugs |
| 287 | $slugs = implode('-', $keys); |
| 288 | |
| 289 | // Date |
| 290 | $date = date('Y-m-d'); |
| 291 | |
| 292 | // file |
| 293 | $file_name = 'acfe-export-' . $prefix . '-' . $slugs . '-' . $date . '.json'; |
| 294 | |
| 295 | // headers |
| 296 | header("Content-Description: File Transfer"); |
| 297 | header("Content-Disposition: attachment; filename={$file_name}"); |
| 298 | header("Content-Type: application/json; charset=utf-8"); |
| 299 | |
| 300 | // return |
| 301 | echo acf_json_encode($this->data); |
| 302 | die; |
| 303 | |
| 304 | } |
| 305 | |
| 306 | elseif($this->action === 'php'){ |
| 307 | |
| 308 | // url |
| 309 | $url = add_query_arg(array( |
| 310 | 'keys' => implode('+', $keys), |
| 311 | 'action' => 'php' |
| 312 | ), $this->get_url()); |
| 313 | |
| 314 | // redirect |
| 315 | wp_redirect($url); |
| 316 | exit; |
| 317 | |
| 318 | } |
| 319 | |
| 320 | } |
| 321 | |
| 322 | function get_selected(){ |
| 323 | |
| 324 | // vars |
| 325 | $selected = $this->get_selected_keys(); |
| 326 | $data = array(); |
| 327 | |
| 328 | if(!$selected) |
| 329 | return false; |
| 330 | $dynamic_options_pages = acfe_settings('modules.dynamic_option.data'); |
| 331 | |
| 332 | if(empty($dynamic_options_pages)) |
| 333 | return false; |
| 334 | |
| 335 | // construct data |
| 336 | foreach($selected as $key){ |
| 337 | |
| 338 | if(!isset($dynamic_options_pages[$key])) |
| 339 | continue; |
| 340 | |
| 341 | // add to data array |
| 342 | $data[$key] = $dynamic_options_pages[$key]; |
| 343 | |
| 344 | } |
| 345 | |
| 346 | // return |
| 347 | return $data; |
| 348 | |
| 349 | } |
| 350 | |
| 351 | function get_selected_keys(){ |
| 352 | |
| 353 | // check $_POST |
| 354 | if($keys = acf_maybe_get_POST('keys')){ |
| 355 | |
| 356 | return (array) $keys; |
| 357 | |
| 358 | } |
| 359 | |
| 360 | // check $_GET |
| 361 | if($keys = acf_maybe_get_GET('keys')){ |
| 362 | |
| 363 | $keys = str_replace(' ', '+', $keys); |
| 364 | return explode('+', $keys); |
| 365 | |
| 366 | } |
| 367 | |
| 368 | // return |
| 369 | return false; |
| 370 | |
| 371 | } |
| 372 | |
| 373 | function get_action(){ |
| 374 | |
| 375 | // init |
| 376 | $type = 'json'; |
| 377 | |
| 378 | // check GET / POST |
| 379 | if(($action = acf_maybe_get_GET('action')) || ($action = acf_maybe_get_POST('action'))){ |
| 380 | |
| 381 | if(in_array($action, array('json', 'php'))) |
| 382 | $type = $action; |
| 383 | |
| 384 | } |
| 385 | |
| 386 | // return |
| 387 | return $type; |
| 388 | |
| 389 | } |
| 390 | |
| 391 | } |
| 392 | |
| 393 | acf_register_admin_tool('ACFE_Admin_Tool_Export_DOP'); |
| 394 | |
| 395 | endif; |
| 396 |