module-export.php
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('acfe_module_export')): |
| 8 | |
| 9 | class acfe_module_export extends ACF_Admin_Tool{ |
| 10 | |
| 11 | // vars |
| 12 | public $module; |
| 13 | public $action; |
| 14 | public $data = array(); |
| 15 | |
| 16 | /** |
| 17 | * construct |
| 18 | * |
| 19 | * @param $module |
| 20 | */ |
| 21 | function __construct($module){ |
| 22 | |
| 23 | // module |
| 24 | $this->module = $module; |
| 25 | |
| 26 | // vars |
| 27 | $this->name = $this->module->get_export_tool(); |
| 28 | $this->title = $this->module->get_message('export_title'); |
| 29 | |
| 30 | parent::__construct(); |
| 31 | |
| 32 | } |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * html |
| 37 | * |
| 38 | * @return void |
| 39 | */ |
| 40 | function html(){ |
| 41 | |
| 42 | // single |
| 43 | if($this->is_active()){ |
| 44 | |
| 45 | $this->html_single(); |
| 46 | |
| 47 | // archive |
| 48 | }else{ |
| 49 | |
| 50 | $this->html_archive(); |
| 51 | |
| 52 | } |
| 53 | |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * html_archive |
| 59 | */ |
| 60 | function html_archive(){ |
| 61 | |
| 62 | if(!method_exists($this->module, 'get_items')){ |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | // vars |
| 67 | $choices = array(); |
| 68 | $items = $this->module->get_raw_items(); |
| 69 | |
| 70 | if($items){ |
| 71 | foreach($items as $item){ |
| 72 | |
| 73 | $choices[ $item['name'] ] = esc_html($item['label']); |
| 74 | |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | ?> |
| 79 | |
| 80 | <?php if(acfe_is_acf_6()): ?> |
| 81 | |
| 82 | <div class="acf-postbox-header"> |
| 83 | <h2 class="acf-postbox-title"><?php echo $this->module->get_message('export_description'); ?></h2> |
| 84 | </div> |
| 85 | <div class="acf-postbox-inner"> |
| 86 | |
| 87 | <?php else: ?> |
| 88 | |
| 89 | <p><?php echo $this->module->get_message('export_description'); ?></p> |
| 90 | |
| 91 | <?php endif; ?> |
| 92 | |
| 93 | <div class="acf-fields"> |
| 94 | <?php |
| 95 | |
| 96 | if(!empty($choices)){ |
| 97 | |
| 98 | // render |
| 99 | acf_render_field_wrap(array( |
| 100 | 'label' => $this->module->get_message('export_select'), |
| 101 | 'type' => 'checkbox', |
| 102 | 'name' => 'keys', |
| 103 | 'prefix' => false, |
| 104 | 'value' => false, |
| 105 | 'toggle' => true, |
| 106 | 'choices' => $choices, |
| 107 | 'class' => 'acfe-module-export-choices' |
| 108 | )); |
| 109 | |
| 110 | }else{ |
| 111 | |
| 112 | echo '<div style="padding:15px 12px;">'; |
| 113 | echo $this->module->get_message('export_not_found'); |
| 114 | echo '</div>'; |
| 115 | |
| 116 | } |
| 117 | |
| 118 | ?> |
| 119 | </div> |
| 120 | |
| 121 | <?php $disabled = empty($choices) ? 'disabled="disabled"' : ''; ?> |
| 122 | |
| 123 | <p class="acf-submit"> |
| 124 | |
| 125 | <?php if(in_array('json', $this->module->export_actions)){ ?> |
| 126 | <button type="submit" name="action" class="button button-primary" value="json" <?php echo $disabled; ?>><?php _e('Export File'); ?></button> |
| 127 | <?php } ?> |
| 128 | |
| 129 | <?php if(in_array('php', $this->module->export_actions)){ ?> |
| 130 | <button type="submit" name="action" class="button" value="php" <?php echo $disabled; ?>><?php _e('Generate PHP'); ?></button> |
| 131 | <?php } ?> |
| 132 | |
| 133 | </p> |
| 134 | |
| 135 | <?php if(acfe_is_acf_6()): ?> |
| 136 | </div> |
| 137 | <?php endif; ?> |
| 138 | |
| 139 | <?php |
| 140 | |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /** |
| 145 | * html_single |
| 146 | */ |
| 147 | function html_single(){ |
| 148 | |
| 149 | // enqueue |
| 150 | wp_enqueue_script('code-editor'); |
| 151 | wp_enqueue_style('code-editor'); |
| 152 | |
| 153 | ?> |
| 154 | |
| 155 | <?php if(acfe_is_acf_6()): ?> |
| 156 | <div class="acf-postbox-header"> |
| 157 | <h2 class="acf-postbox-title"><?php echo $this->module->get_message('export_description'); ?></h2> |
| 158 | </div> |
| 159 | <?php endif; ?> |
| 160 | |
| 161 | <div class="acf-postbox-columns" style="margin-top: 0;margin-right: 280px;margin-bottom: 0;margin-left: 0;padding: 0;"> |
| 162 | <div class="acf-postbox-main"> |
| 163 | |
| 164 | <?php |
| 165 | $instructions = array( |
| 166 | __("You can copy and paste the following code to your theme's <code>functions.php</code> file or include it within an external file.", 'acfe') |
| 167 | ); |
| 168 | |
| 169 | if($this->module->get_message('export_instructions')){ |
| 170 | $instructions[] = $this->module->get_message('export_instructions'); |
| 171 | } |
| 172 | ?> |
| 173 | |
| 174 | <p><?php echo implode(' ', $instructions); ?></p> |
| 175 | |
| 176 | <div id="acf-admin-tool-export"> |
| 177 | <textarea id="acf-export-textarea" readonly="true"><?php |
| 178 | |
| 179 | foreach($this->data as $item){ |
| 180 | |
| 181 | // translation |
| 182 | $l10n = acf_get_setting('l10n'); |
| 183 | $l10n_textdomain = acf_get_setting('l10n_textdomain'); |
| 184 | |
| 185 | if($l10n && $l10n_textdomain){ |
| 186 | |
| 187 | acf_update_setting('l10n_var_export', true); |
| 188 | |
| 189 | $item = $this->module->translate_item($item); |
| 190 | |
| 191 | acf_update_setting('l10n_var_export', false); |
| 192 | |
| 193 | } |
| 194 | |
| 195 | // cleanup keys |
| 196 | $item = $this->module->prepare_item_for_export($item); |
| 197 | |
| 198 | // var export |
| 199 | $code = acfe_var_export($item); |
| 200 | |
| 201 | // echo |
| 202 | echo $this->module->export_code($code, $item) . "\r\n" . "\r\n"; |
| 203 | |
| 204 | } |
| 205 | |
| 206 | ?></textarea> |
| 207 | </div> |
| 208 | |
| 209 | <p class="acf-submit"> |
| 210 | <a class="button" id="acf-export-copy"><?php _e('Copy to clipboard', 'acf'); ?></a> |
| 211 | </p> |
| 212 | <script type="text/javascript"> |
| 213 | (function($){ |
| 214 | |
| 215 | if(typeof acf === 'undefined'){ |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | // acf 6.0 add display block; |
| 220 | $('#acf-admin-tools #normal-sortables').css('display', 'block'); |
| 221 | |
| 222 | acf.addAction('ready', function(){ |
| 223 | |
| 224 | // elements |
| 225 | var $a = $('#acf-export-copy'); |
| 226 | var $textarea = $('#acf-export-textarea'); |
| 227 | |
| 228 | // initialize code mirror |
| 229 | var edit = wp.codeEditor.initialize($textarea.get(0), { |
| 230 | |
| 231 | codemirror: $.extend(wp.codeEditor.defaultSettings.codemirror, { |
| 232 | lineNumbers: true, |
| 233 | lineWrapping: true, |
| 234 | styleActiveLine: false, |
| 235 | continueComments: true, |
| 236 | indentUnit: 4, |
| 237 | tabSize: 1, |
| 238 | indentWithTabs: false, |
| 239 | mode: 'text/x-php', |
| 240 | extraKeys: { |
| 241 | 'Tab': function(cm){cm.execCommand('indentMore')}, |
| 242 | 'Shift-Tab': function(cm){cm.execCommand('indentLess')}, |
| 243 | }, |
| 244 | }) |
| 245 | |
| 246 | }); |
| 247 | |
| 248 | // set height |
| 249 | edit.codemirror.getScrollerElement().style.minHeight = 15 * 18.5 + 'px'; |
| 250 | |
| 251 | if(!document.queryCommandSupported('copy')){ |
| 252 | return $a.remove(); |
| 253 | } |
| 254 | |
| 255 | $a.on('click', function(e){ |
| 256 | |
| 257 | e.preventDefault(); |
| 258 | var $this = $(this); |
| 259 | |
| 260 | // copy |
| 261 | navigator.clipboard.writeText(edit.codemirror.getValue()).then(function(){ |
| 262 | |
| 263 | // tooltip |
| 264 | acf.newTooltip({ |
| 265 | text: "<?php _e('Copied', 'acf'); ?>", |
| 266 | timeout: 250, |
| 267 | target: $this, |
| 268 | }); |
| 269 | |
| 270 | }); |
| 271 | |
| 272 | }); |
| 273 | |
| 274 | }); |
| 275 | |
| 276 | })(jQuery); |
| 277 | </script> |
| 278 | </div> |
| 279 | |
| 280 | <div class="acf-postbox-side"> |
| 281 | |
| 282 | <div class="acf-panel acf-panel-selection -open"> |
| 283 | <h3 class="acf-panel-title"><?php echo $this->module->get_message('export_select'); ?> <i class="dashicons dashicons-arrow-down"></i></h3> |
| 284 | <div class="acf-panel-inside"> |
| 285 | |
| 286 | <?php |
| 287 | // vars |
| 288 | $choices = array(); |
| 289 | $selected = $this->get_keys(); |
| 290 | $items = $this->module->get_items(); |
| 291 | |
| 292 | if($items){ |
| 293 | foreach($items as $item){ |
| 294 | $choices[ $item['name'] ] = esc_html($item['label']); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // render |
| 299 | acf_render_field_wrap(array( |
| 300 | 'type' => 'checkbox', |
| 301 | 'name' => 'keys', |
| 302 | 'prefix' => false, |
| 303 | 'value' => $selected, |
| 304 | 'toggle' => true, |
| 305 | 'choices' => $choices, |
| 306 | )); |
| 307 | ?> |
| 308 | |
| 309 | </div> |
| 310 | </div> |
| 311 | |
| 312 | <p class="acf-submit"> |
| 313 | |
| 314 | <?php if(in_array('json', $this->module->export_actions)){ ?> |
| 315 | <button type="submit" name="action" class="button button-primary" value="json"><?php _e('Export File'); ?></button> |
| 316 | <?php } ?> |
| 317 | |
| 318 | <?php if(in_array('php', $this->module->export_actions)){ ?> |
| 319 | <button type="submit" name="action" class="button" value="php"><?php _e('Generate PHP'); ?></button> |
| 320 | <?php } ?> |
| 321 | |
| 322 | </p> |
| 323 | </div> |
| 324 | |
| 325 | </div> |
| 326 | <?php |
| 327 | |
| 328 | } |
| 329 | |
| 330 | |
| 331 | /** |
| 332 | * load |
| 333 | * |
| 334 | * @return void |
| 335 | */ |
| 336 | function load(){ |
| 337 | |
| 338 | if(!$this->is_active()){ |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | $this->action = $this->get_action(); |
| 343 | $this->data = $this->get_data(); |
| 344 | |
| 345 | // Json |
| 346 | if($this->action === 'json'){ |
| 347 | |
| 348 | $this->submit(); |
| 349 | |
| 350 | // PHP |
| 351 | }elseif($this->action === 'php'){ |
| 352 | |
| 353 | // add notice |
| 354 | if(!empty($this->data)){ |
| 355 | |
| 356 | $count = count($this->data); |
| 357 | |
| 358 | $text = $this->module->get_message('export_success_single'); |
| 359 | |
| 360 | if($count > 1){ |
| 361 | $text = sprintf($this->module->get_message('export_success_multiple'), $count); |
| 362 | } |
| 363 | |
| 364 | acf_add_admin_notice($text, 'success'); |
| 365 | |
| 366 | } |
| 367 | |
| 368 | } |
| 369 | |
| 370 | } |
| 371 | |
| 372 | |
| 373 | /** |
| 374 | * submit |
| 375 | * |
| 376 | * @return ACF_Admin_Notice|n|void |
| 377 | */ |
| 378 | function submit(){ |
| 379 | |
| 380 | // vars |
| 381 | $this->action = $this->get_action(); |
| 382 | $this->data = $this->get_data(); |
| 383 | $keys = wp_list_pluck($this->data, 'name'); |
| 384 | |
| 385 | // validate |
| 386 | if(!$this->data){ |
| 387 | return acf_add_admin_notice($this->module->get_message('export_not_selected'), 'warning'); |
| 388 | } |
| 389 | |
| 390 | // Json |
| 391 | if($this->action === 'json'){ |
| 392 | |
| 393 | // prefix |
| 394 | $prefix = (count($this->data) > 1) ? $this->module->export_files['multiple'] : $this->module->export_files['single']; |
| 395 | |
| 396 | // slugs |
| 397 | $slugs = implode('-', $keys); |
| 398 | |
| 399 | // date |
| 400 | $date = date('Y-m-d'); |
| 401 | |
| 402 | // file |
| 403 | $file_name = 'acfe-export-' . $prefix . '-' . $slugs . '-' . $date . '.json'; |
| 404 | |
| 405 | // data |
| 406 | $data = array(); |
| 407 | foreach($this->data as $item){ |
| 408 | |
| 409 | // cleanup keys |
| 410 | $item = $this->module->prepare_item_for_export($item); |
| 411 | |
| 412 | // append to data |
| 413 | $data[] = $item; |
| 414 | |
| 415 | } |
| 416 | |
| 417 | // headers |
| 418 | header("Content-Description: File Transfer"); |
| 419 | header("Content-Disposition: attachment; filename={$file_name}"); |
| 420 | header("Content-Type: application/json; charset=utf-8"); |
| 421 | |
| 422 | // return |
| 423 | echo acf_json_encode($data); |
| 424 | |
| 425 | // PHP |
| 426 | }elseif($this->action === 'php'){ |
| 427 | |
| 428 | // url |
| 429 | $url = add_query_arg(array( |
| 430 | 'keys' => implode('+', $keys), |
| 431 | 'action' => 'php' |
| 432 | ), $this->get_url()); |
| 433 | |
| 434 | // redirect |
| 435 | wp_redirect($url); |
| 436 | |
| 437 | } |
| 438 | |
| 439 | exit; |
| 440 | |
| 441 | } |
| 442 | |
| 443 | |
| 444 | /** |
| 445 | * get_data |
| 446 | * |
| 447 | * @return array |
| 448 | */ |
| 449 | function get_data(){ |
| 450 | |
| 451 | // vars |
| 452 | $keys = $this->get_keys(); |
| 453 | $data = array(); |
| 454 | |
| 455 | foreach($keys as $name){ |
| 456 | |
| 457 | // get item |
| 458 | $item = $this->module->get_raw_item($name); |
| 459 | |
| 460 | if($item){ |
| 461 | $data[] = $item; |
| 462 | } |
| 463 | |
| 464 | } |
| 465 | |
| 466 | return $data; |
| 467 | |
| 468 | } |
| 469 | |
| 470 | |
| 471 | /** |
| 472 | * get_keys |
| 473 | * |
| 474 | * @return array|false|string[] |
| 475 | */ |
| 476 | function get_keys(){ |
| 477 | |
| 478 | // vars |
| 479 | $keys_post = acf_maybe_get_POST('keys'); |
| 480 | $keys_get = acf_maybe_get_GET('keys'); |
| 481 | $keys = array(); |
| 482 | |
| 483 | // $_POST |
| 484 | if($keys_post){ |
| 485 | |
| 486 | $keys = (array) $keys_post; |
| 487 | |
| 488 | // $_GET |
| 489 | }elseif($keys_get){ |
| 490 | |
| 491 | $keys_get = str_replace(' ', '+', $keys_get); |
| 492 | $keys = explode('+', $keys_get); |
| 493 | |
| 494 | } |
| 495 | |
| 496 | return $keys; |
| 497 | |
| 498 | } |
| 499 | |
| 500 | |
| 501 | /** |
| 502 | * get_action |
| 503 | * |
| 504 | * @return false|mixed|null |
| 505 | */ |
| 506 | function get_action(){ |
| 507 | |
| 508 | // vars |
| 509 | $action = acfe_maybe_get_REQUEST('action'); |
| 510 | |
| 511 | // check allowed |
| 512 | if(!in_array($action, $this->module->export_actions)){ |
| 513 | return current($this->module->export_actions); |
| 514 | } |
| 515 | |
| 516 | // return |
| 517 | return $action; |
| 518 | |
| 519 | } |
| 520 | |
| 521 | } |
| 522 | |
| 523 | endif; |