class-acf-admin-tool-export.php
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; // Exit if accessed directly |
| 5 | } |
| 6 | |
| 7 | if ( ! class_exists( 'ACF_Admin_Tool_Export' ) ) : |
| 8 | |
| 9 | class ACF_Admin_Tool_Export extends ACF_Admin_Tool { |
| 10 | |
| 11 | /** @var string View context */ |
| 12 | var $view = ''; |
| 13 | |
| 14 | |
| 15 | /** @var array Export data */ |
| 16 | var $json = ''; |
| 17 | |
| 18 | |
| 19 | /** |
| 20 | * initialize |
| 21 | * |
| 22 | * This function will initialize the admin tool |
| 23 | * |
| 24 | * @date 10/10/17 |
| 25 | * @since 5.6.3 |
| 26 | * |
| 27 | * @param n/a |
| 28 | * @return n/a |
| 29 | */ |
| 30 | |
| 31 | function initialize() { |
| 32 | |
| 33 | // vars |
| 34 | $this->name = 'export'; |
| 35 | $this->title = __( 'Export Field Groups', 'acf' ); |
| 36 | |
| 37 | // active |
| 38 | if ( $this->is_active() ) { |
| 39 | $this->title .= ' - ' . __( 'Generate PHP', 'acf' ); |
| 40 | } |
| 41 | |
| 42 | } |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * submit |
| 47 | * |
| 48 | * This function will run when the tool's form has been submit |
| 49 | * |
| 50 | * @date 10/10/17 |
| 51 | * @since 5.6.3 |
| 52 | * |
| 53 | * @param n/a |
| 54 | * @return n/a |
| 55 | */ |
| 56 | |
| 57 | function submit() { |
| 58 | |
| 59 | // vars |
| 60 | $action = acf_maybe_get_POST( 'action' ); |
| 61 | |
| 62 | // download |
| 63 | if ( $action === 'download' ) { |
| 64 | |
| 65 | $this->submit_download(); |
| 66 | |
| 67 | // generate |
| 68 | } elseif ( $action === 'generate' ) { |
| 69 | |
| 70 | $this->submit_generate(); |
| 71 | |
| 72 | } |
| 73 | |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /** |
| 78 | * submit_download |
| 79 | * |
| 80 | * description |
| 81 | * |
| 82 | * @date 17/10/17 |
| 83 | * @since 5.6.3 |
| 84 | * |
| 85 | * @param n/a |
| 86 | * @return n/a |
| 87 | */ |
| 88 | |
| 89 | function submit_download() { |
| 90 | |
| 91 | // vars |
| 92 | $json = $this->get_selected(); |
| 93 | |
| 94 | // validate |
| 95 | if ( $json === false ) { |
| 96 | return acf_add_admin_notice( __( 'No field groups selected', 'acf' ), 'warning' ); |
| 97 | } |
| 98 | |
| 99 | // headers |
| 100 | $file_name = 'acf-export-' . date( 'Y-m-d' ) . '.json'; |
| 101 | header( 'Content-Description: File Transfer' ); |
| 102 | header( "Content-Disposition: attachment; filename={$file_name}" ); |
| 103 | header( 'Content-Type: application/json; charset=utf-8' ); |
| 104 | |
| 105 | // return |
| 106 | echo acf_json_encode( $json ); |
| 107 | die; |
| 108 | |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * submit_generate |
| 114 | * |
| 115 | * description |
| 116 | * |
| 117 | * @date 17/10/17 |
| 118 | * @since 5.6.3 |
| 119 | * |
| 120 | * @param n/a |
| 121 | * @return n/a |
| 122 | */ |
| 123 | |
| 124 | function submit_generate() { |
| 125 | |
| 126 | // vars |
| 127 | $keys = $this->get_selected_keys(); |
| 128 | |
| 129 | // validate |
| 130 | if ( ! $keys ) { |
| 131 | return acf_add_admin_notice( __( 'No field groups selected', 'acf' ), 'warning' ); |
| 132 | } |
| 133 | |
| 134 | // url |
| 135 | $url = add_query_arg( 'keys', implode( '+', $keys ), $this->get_url() ); |
| 136 | |
| 137 | // redirect |
| 138 | wp_redirect( $url ); |
| 139 | exit; |
| 140 | |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /** |
| 145 | * load |
| 146 | * |
| 147 | * description |
| 148 | * |
| 149 | * @date 21/10/17 |
| 150 | * @since 5.6.3 |
| 151 | * |
| 152 | * @param n/a |
| 153 | * @return n/a |
| 154 | */ |
| 155 | |
| 156 | function load() { |
| 157 | |
| 158 | // active |
| 159 | if ( $this->is_active() ) { |
| 160 | |
| 161 | // get selected keys |
| 162 | $selected = $this->get_selected_keys(); |
| 163 | |
| 164 | // add notice |
| 165 | if ( $selected ) { |
| 166 | $count = count( $selected ); |
| 167 | $text = sprintf( _n( 'Exported 1 field group.', 'Exported %s field groups.', $count, 'acf' ), $count ); |
| 168 | acf_add_admin_notice( $text, 'success' ); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | } |
| 173 | |
| 174 | |
| 175 | /** |
| 176 | * html |
| 177 | * |
| 178 | * This function will output the metabox HTML |
| 179 | * |
| 180 | * @date 10/10/17 |
| 181 | * @since 5.6.3 |
| 182 | * |
| 183 | * @param n/a |
| 184 | * @return n/a |
| 185 | */ |
| 186 | |
| 187 | function html() { |
| 188 | |
| 189 | // single (generate PHP) |
| 190 | if ( $this->is_active() ) { |
| 191 | |
| 192 | $this->html_single(); |
| 193 | |
| 194 | // archive |
| 195 | } else { |
| 196 | |
| 197 | $this->html_archive(); |
| 198 | |
| 199 | } |
| 200 | |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /** |
| 205 | * html_field_selection |
| 206 | * |
| 207 | * description |
| 208 | * |
| 209 | * @date 24/10/17 |
| 210 | * @since 5.6.3 |
| 211 | * |
| 212 | * @param n/a |
| 213 | * @return n/a |
| 214 | */ |
| 215 | |
| 216 | function html_field_selection() { |
| 217 | |
| 218 | // vars |
| 219 | $choices = array(); |
| 220 | $selected = $this->get_selected_keys(); |
| 221 | $field_groups = acf_get_field_groups(); |
| 222 | |
| 223 | // loop |
| 224 | if ( $field_groups ) { |
| 225 | foreach ( $field_groups as $field_group ) { |
| 226 | $choices[ $field_group['key'] ] = esc_html( $field_group['title'] ); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // render |
| 231 | acf_render_field_wrap( |
| 232 | array( |
| 233 | 'label' => __( 'Select Field Groups', 'acf' ), |
| 234 | 'type' => 'checkbox', |
| 235 | 'name' => 'keys', |
| 236 | 'prefix' => false, |
| 237 | 'value' => $selected, |
| 238 | 'toggle' => true, |
| 239 | 'choices' => $choices, |
| 240 | ) |
| 241 | ); |
| 242 | |
| 243 | } |
| 244 | |
| 245 | |
| 246 | /** |
| 247 | * html_panel_selection |
| 248 | * |
| 249 | * description |
| 250 | * |
| 251 | * @date 21/10/17 |
| 252 | * @since 5.6.3 |
| 253 | * |
| 254 | * @param n/a |
| 255 | * @return n/a |
| 256 | */ |
| 257 | |
| 258 | function html_panel_selection() { |
| 259 | |
| 260 | ?> |
| 261 | <div class="acf-panel acf-panel-selection"> |
| 262 | <h3 class="acf-panel-title"><?php _e( 'Select Field Groups', 'acf' ); ?></h3> |
| 263 | <?php $this->html_field_selection(); ?> |
| 264 | </div> |
| 265 | <?php |
| 266 | |
| 267 | } |
| 268 | |
| 269 | |
| 270 | /** |
| 271 | * html_panel_settings |
| 272 | * |
| 273 | * description |
| 274 | * |
| 275 | * @date 21/10/17 |
| 276 | * @since 5.6.3 |
| 277 | * |
| 278 | * @param n/a |
| 279 | * @return n/a |
| 280 | */ |
| 281 | |
| 282 | function html_panel_settings() { |
| 283 | |
| 284 | ?> |
| 285 | <div class="acf-panel acf-panel-settings"> |
| 286 | <h3 class="acf-panel-title"><?php _e( 'Settings', 'acf' ); ?> <i class="dashicons dashicons-arrow-right"></i></h3> |
| 287 | <div class="acf-panel-inside"> |
| 288 | <?php |
| 289 | |
| 290 | /* |
| 291 | acf_render_field_wrap(array( |
| 292 | 'label' => __('Empty settings', 'acf'), |
| 293 | 'type' => 'select', |
| 294 | 'name' => 'minimal', |
| 295 | 'prefix' => false, |
| 296 | 'value' => '', |
| 297 | 'choices' => array( |
| 298 | 'all' => __('Include all settings', 'acf'), |
| 299 | 'minimal' => __('Ignore empty settings', 'acf'), |
| 300 | ) |
| 301 | )); |
| 302 | */ |
| 303 | |
| 304 | ?> |
| 305 | </div> |
| 306 | </div> |
| 307 | <?php |
| 308 | |
| 309 | } |
| 310 | |
| 311 | |
| 312 | /** |
| 313 | * html_archive |
| 314 | * |
| 315 | * description |
| 316 | * |
| 317 | * @date 20/10/17 |
| 318 | * @since 5.6.3 |
| 319 | * |
| 320 | * @param n/a |
| 321 | * @return n/a |
| 322 | */ |
| 323 | |
| 324 | function html_archive() { |
| 325 | |
| 326 | ?> |
| 327 | <div class="acf-postbox-header"> |
| 328 | <h2 class="acf-postbox-title">Export Field Groups</h2> |
| 329 | <div class="acf-tip"><i tabindex="0" class="acf-icon acf-icon-help acf-js-tooltip" title="<?php esc_attr_e( 'Select the field groups you would like to export and then select your export method. Export As JSON to export to a .json file which you can then import to another ACF installation. Generate PHP to export to PHP code which you can place in your theme.', 'acf' ); ?>">?</i></div> |
| 330 | </div> |
| 331 | <div class="acf-postbox-inner"> |
| 332 | <div class="acf-fields"> |
| 333 | <?php $this->html_field_selection(); ?> |
| 334 | </div> |
| 335 | <p class="acf-submit acf-actions-strip"> |
| 336 | <button type="submit" name="action" class="acf-btn acf-button-primary" value="download"><?php _e( 'Export As JSON', 'acf' ); ?></button> |
| 337 | <button type="submit" name="action" class="acf-btn acf-btn-secondary" value="generate"><?php _e( 'Generate PHP', 'acf' ); ?></button> |
| 338 | </p> |
| 339 | </div> |
| 340 | <?php |
| 341 | |
| 342 | } |
| 343 | |
| 344 | |
| 345 | /** |
| 346 | * html_single |
| 347 | * |
| 348 | * description |
| 349 | * |
| 350 | * @date 20/10/17 |
| 351 | * @since 5.6.3 |
| 352 | * |
| 353 | * @param n/a |
| 354 | * @return n/a |
| 355 | */ |
| 356 | |
| 357 | function html_single() { |
| 358 | |
| 359 | ?> |
| 360 | <div class="acf-postbox-header"> |
| 361 | <h2 class="acf-postbox-title"><?php _e( 'Export Field Groups - Generate PHP', 'acf' ); ?></h2> |
| 362 | <i tabindex="0" class="acf-icon acf-icon-help acf-js-tooltip" title="<?php esc_attr_e( "The following code can be used to register a local version of the selected field group(s). A local field group can provide many benefits such as faster load times, version control & dynamic fields/settings. Simply copy and paste the following code to your theme's functions.php file or include it within an external file.", 'acf' ); ?>">?</i> |
| 363 | </div> |
| 364 | <div class="acf-postbox-columns"> |
| 365 | <div class="acf-postbox-main"> |
| 366 | <?php $this->html_generate(); ?> |
| 367 | </div> |
| 368 | <div class="acf-postbox-side"> |
| 369 | <?php $this->html_panel_selection(); ?> |
| 370 | <p class="acf-submit"> |
| 371 | <button type="submit" name="action" class="acf-btn" value="generate"><?php _e( 'Generate PHP', 'acf' ); ?></button> |
| 372 | </p> |
| 373 | </div> |
| 374 | </div> |
| 375 | <?php |
| 376 | |
| 377 | } |
| 378 | |
| 379 | |
| 380 | /** |
| 381 | * html_generate |
| 382 | * |
| 383 | * description |
| 384 | * |
| 385 | * @date 17/10/17 |
| 386 | * @since 5.6.3 |
| 387 | * |
| 388 | * @param n/a |
| 389 | * @return n/a |
| 390 | */ |
| 391 | |
| 392 | function html_generate() { |
| 393 | |
| 394 | // prevent default translation and fake __() within string |
| 395 | acf_update_setting( 'l10n_var_export', true ); |
| 396 | |
| 397 | // vars |
| 398 | $json = $this->get_selected(); |
| 399 | $str_replace = array( |
| 400 | ' ' => "\t", |
| 401 | "'!!__(!!\'" => "__('", |
| 402 | "!!\', !!\'" => "', '", |
| 403 | "!!\')!!'" => "')", |
| 404 | 'array (' => 'array(', |
| 405 | ); |
| 406 | $preg_replace = array( |
| 407 | '/([\t\r\n]+?)array/' => 'array', |
| 408 | '/[0-9]+ => array/' => 'array', |
| 409 | ); |
| 410 | |
| 411 | ?> |
| 412 | <textarea id="acf-export-textarea" readonly="true"> |
| 413 | <?php |
| 414 | |
| 415 | echo "if( function_exists('acf_add_local_field_group') ):" . "\r\n" . "\r\n"; |
| 416 | |
| 417 | foreach ( $json as $field_group ) { |
| 418 | |
| 419 | // code |
| 420 | $code = var_export( $field_group, true ); |
| 421 | |
| 422 | // change double spaces to tabs |
| 423 | $code = str_replace( array_keys( $str_replace ), array_values( $str_replace ), $code ); |
| 424 | |
| 425 | // correctly formats "=> array(" |
| 426 | $code = preg_replace( array_keys( $preg_replace ), array_values( $preg_replace ), $code ); |
| 427 | |
| 428 | // esc_textarea |
| 429 | $code = esc_textarea( $code ); |
| 430 | |
| 431 | // echo |
| 432 | echo "acf_add_local_field_group({$code});" . "\r\n" . "\r\n"; |
| 433 | |
| 434 | } |
| 435 | |
| 436 | echo 'endif;'; |
| 437 | |
| 438 | ?> |
| 439 | </textarea> |
| 440 | <p class="acf-submit"> |
| 441 | <a class="button" id="acf-export-copy"><?php _e( 'Copy to clipboard', 'acf' ); ?></a> |
| 442 | </p> |
| 443 | <script type="text/javascript"> |
| 444 | (function($){ |
| 445 | |
| 446 | // vars |
| 447 | var $a = $('#acf-export-copy'); |
| 448 | var $textarea = $('#acf-export-textarea'); |
| 449 | |
| 450 | |
| 451 | // remove $a if 'copy' is not supported |
| 452 | if( !document.queryCommandSupported('copy') ) { |
| 453 | return $a.remove(); |
| 454 | } |
| 455 | |
| 456 | |
| 457 | // event |
| 458 | $a.on('click', function( e ){ |
| 459 | |
| 460 | // prevent default |
| 461 | e.preventDefault(); |
| 462 | |
| 463 | |
| 464 | // select |
| 465 | $textarea.get(0).select(); |
| 466 | |
| 467 | |
| 468 | // try |
| 469 | try { |
| 470 | |
| 471 | // copy |
| 472 | var copy = document.execCommand('copy'); |
| 473 | if( !copy ) return; |
| 474 | |
| 475 | |
| 476 | // tooltip |
| 477 | acf.newTooltip({ |
| 478 | text: "<?php _e( 'Copied', 'acf' ); ?>", |
| 479 | timeout: 250, |
| 480 | target: $(this), |
| 481 | }); |
| 482 | |
| 483 | } catch (err) { |
| 484 | |
| 485 | // do nothing |
| 486 | |
| 487 | } |
| 488 | |
| 489 | }); |
| 490 | |
| 491 | })(jQuery); |
| 492 | </script> |
| 493 | <?php |
| 494 | |
| 495 | } |
| 496 | |
| 497 | |
| 498 | |
| 499 | /** |
| 500 | * get_selected_keys |
| 501 | * |
| 502 | * This function will return an array of field group keys that have been selected |
| 503 | * |
| 504 | * @date 20/10/17 |
| 505 | * @since 5.6.3 |
| 506 | * |
| 507 | * @param n/a |
| 508 | * @return n/a |
| 509 | */ |
| 510 | |
| 511 | function get_selected_keys() { |
| 512 | |
| 513 | // check $_POST |
| 514 | if ( $keys = acf_maybe_get_POST( 'keys' ) ) { |
| 515 | return (array) $keys; |
| 516 | } |
| 517 | |
| 518 | // check $_GET |
| 519 | if ( $keys = acf_maybe_get_GET( 'keys' ) ) { |
| 520 | $keys = str_replace( ' ', '+', $keys ); |
| 521 | return explode( '+', $keys ); |
| 522 | } |
| 523 | |
| 524 | // return |
| 525 | return false; |
| 526 | |
| 527 | } |
| 528 | |
| 529 | |
| 530 | /** |
| 531 | * get_selected |
| 532 | * |
| 533 | * This function will return the JSON data for given $_POST args |
| 534 | * |
| 535 | * @date 17/10/17 |
| 536 | * @since 5.6.3 |
| 537 | * |
| 538 | * @param n/a |
| 539 | * @return array |
| 540 | */ |
| 541 | |
| 542 | function get_selected() { |
| 543 | |
| 544 | // vars |
| 545 | $selected = $this->get_selected_keys(); |
| 546 | $json = array(); |
| 547 | |
| 548 | // bail early if no keys |
| 549 | if ( ! $selected ) { |
| 550 | return false; |
| 551 | } |
| 552 | |
| 553 | // construct JSON |
| 554 | foreach ( $selected as $key ) { |
| 555 | |
| 556 | // load field group |
| 557 | $field_group = acf_get_field_group( $key ); |
| 558 | |
| 559 | // validate field group |
| 560 | if ( empty( $field_group ) ) { |
| 561 | continue; |
| 562 | } |
| 563 | |
| 564 | // load fields |
| 565 | $field_group['fields'] = acf_get_fields( $field_group ); |
| 566 | |
| 567 | // prepare for export |
| 568 | $field_group = acf_prepare_field_group_for_export( $field_group ); |
| 569 | |
| 570 | // add to json array |
| 571 | $json[] = $field_group; |
| 572 | |
| 573 | } |
| 574 | |
| 575 | // return |
| 576 | return $json; |
| 577 | |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | // initialize |
| 582 | acf_register_admin_tool( 'ACF_Admin_Tool_Export' ); |
| 583 | |
| 584 | endif; // class_exists check |
| 585 | |
| 586 | ?> |
| 587 |