class-acf-admin-tool-export.php
1 year ago
class-acf-admin-tool-import.php
1 year ago
class-acf-admin-tool.php
1 year ago
index.php
1 year ago
class-acf-admin-tool-export.php
530 lines
| 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 | function initialize() { |
| 31 | |
| 32 | // vars |
| 33 | $this->name = 'export'; |
| 34 | $this->title = __( 'Export Field Groups', 'acf' ); |
| 35 | |
| 36 | // active |
| 37 | if ( $this->is_active() ) { |
| 38 | $this->title .= ' - ' . __( 'Generate PHP', 'acf' ); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * submit |
| 45 | * |
| 46 | * This function will run when the tool's form has been submit |
| 47 | * |
| 48 | * @date 10/10/17 |
| 49 | * @since 5.6.3 |
| 50 | * |
| 51 | * @param n/a |
| 52 | * @return n/a |
| 53 | */ |
| 54 | function submit() { |
| 55 | |
| 56 | // vars |
| 57 | $action = acf_maybe_get_POST( 'action' ); |
| 58 | |
| 59 | // download |
| 60 | if ( $action === 'download' ) { |
| 61 | $this->submit_download(); |
| 62 | |
| 63 | // generate |
| 64 | } elseif ( $action === 'generate' ) { |
| 65 | $this->submit_generate(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | |
| 70 | /** |
| 71 | * submit_download |
| 72 | * |
| 73 | * description |
| 74 | * |
| 75 | * @date 17/10/17 |
| 76 | * @since 5.6.3 |
| 77 | * |
| 78 | * @param n/a |
| 79 | * @return n/a |
| 80 | */ |
| 81 | function submit_download() { |
| 82 | |
| 83 | // vars |
| 84 | $json = $this->get_selected(); |
| 85 | |
| 86 | // validate |
| 87 | if ( $json === false ) { |
| 88 | return acf_add_admin_notice( __( 'No field groups selected', 'acf' ), 'warning' ); |
| 89 | } |
| 90 | |
| 91 | // headers |
| 92 | $file_name = 'scf-export-' . date( 'Y-m-d' ) . '.json'; |
| 93 | header( 'Content-Description: File Transfer' ); |
| 94 | header( "Content-Disposition: attachment; filename={$file_name}" ); |
| 95 | header( 'Content-Type: application/json; charset=utf-8' ); |
| 96 | |
| 97 | // return |
| 98 | echo acf_json_encode( $json ) . "\r\n"; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped as JSON export. |
| 99 | die; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /** |
| 104 | * submit_generate |
| 105 | * |
| 106 | * description |
| 107 | * |
| 108 | * @date 17/10/17 |
| 109 | * @since 5.6.3 |
| 110 | * |
| 111 | * @param n/a |
| 112 | * @return n/a |
| 113 | */ |
| 114 | function submit_generate() { |
| 115 | |
| 116 | // vars |
| 117 | $keys = $this->get_selected_keys(); |
| 118 | |
| 119 | // validate |
| 120 | if ( ! $keys ) { |
| 121 | return acf_add_admin_notice( __( 'No field groups selected', 'acf' ), 'warning' ); |
| 122 | } |
| 123 | |
| 124 | // url |
| 125 | $url = add_query_arg( 'keys', implode( '+', $keys ), $this->get_url() ); |
| 126 | |
| 127 | // redirect |
| 128 | wp_safe_redirect( $url ); |
| 129 | exit; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /** |
| 134 | * load |
| 135 | * |
| 136 | * description |
| 137 | * |
| 138 | * @date 21/10/17 |
| 139 | * @since 5.6.3 |
| 140 | * |
| 141 | * @param n/a |
| 142 | * @return n/a |
| 143 | */ |
| 144 | function load() { |
| 145 | |
| 146 | // active |
| 147 | if ( $this->is_active() ) { |
| 148 | |
| 149 | // get selected keys |
| 150 | $selected = $this->get_selected_keys(); |
| 151 | |
| 152 | // add notice |
| 153 | if ( $selected ) { |
| 154 | $count = count( $selected ); |
| 155 | $text = sprintf( _n( 'Exported 1 item.', 'Exported %s items.', $count, 'acf' ), $count ); |
| 156 | acf_add_admin_notice( $text, 'success' ); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /** |
| 163 | * html |
| 164 | * |
| 165 | * This function will output the metabox HTML |
| 166 | * |
| 167 | * @date 10/10/17 |
| 168 | * @since 5.6.3 |
| 169 | * |
| 170 | * @param n/a |
| 171 | * @return n/a |
| 172 | */ |
| 173 | function html() { |
| 174 | |
| 175 | // single (generate PHP) |
| 176 | if ( $this->is_active() ) { |
| 177 | $this->html_single(); |
| 178 | |
| 179 | // archive |
| 180 | } else { |
| 181 | $this->html_archive(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /** |
| 187 | * Renders the checkboxes to select items to export. |
| 188 | * |
| 189 | * @since 5.6.3 |
| 190 | */ |
| 191 | public function html_field_selection() { |
| 192 | // Ensure `l10n_var_export` is always false at the point we're outputting the options. |
| 193 | acf_update_setting( 'l10n_var_export', false ); |
| 194 | // Reset the field-groups store which may have been corrupted by export. |
| 195 | $store = acf_get_store( 'field-groups' ); |
| 196 | if ( $store ) { |
| 197 | $store->reset(); |
| 198 | } |
| 199 | |
| 200 | $choices = array(); |
| 201 | $selected = $this->get_selected_keys(); |
| 202 | $field_groups = array_filter( |
| 203 | acf_get_internal_post_type_posts( 'acf-field-group' ), |
| 204 | 'acf_internal_post_object_contains_valid_key' |
| 205 | ); |
| 206 | |
| 207 | if ( $field_groups ) { |
| 208 | foreach ( $field_groups as $field_group ) { |
| 209 | $choices[ $field_group['key'] ] = esc_html( $field_group['title'] ); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | acf_render_field_wrap( |
| 214 | array( |
| 215 | 'label' => __( 'Select Field Groups', 'acf' ), |
| 216 | 'type' => 'checkbox', |
| 217 | 'name' => 'keys', |
| 218 | 'prefix' => false, |
| 219 | 'value' => $selected, |
| 220 | 'toggle' => true, |
| 221 | 'choices' => $choices, |
| 222 | ) |
| 223 | ); |
| 224 | |
| 225 | $choices = array(); |
| 226 | $selected = $this->get_selected_keys(); |
| 227 | $post_types = array_filter( |
| 228 | acf_get_internal_post_type_posts( 'acf-post-type' ), |
| 229 | 'acf_internal_post_object_contains_valid_key' |
| 230 | ); |
| 231 | |
| 232 | if ( $post_types ) { |
| 233 | foreach ( $post_types as $post_type ) { |
| 234 | $choices[ $post_type['key'] ] = esc_html( $post_type['title'] ); |
| 235 | } |
| 236 | |
| 237 | acf_render_field_wrap( |
| 238 | array( |
| 239 | 'label' => __( 'Select Post Types', 'acf' ), |
| 240 | 'type' => 'checkbox', |
| 241 | 'name' => 'post_type_keys', |
| 242 | 'prefix' => false, |
| 243 | 'value' => $selected, |
| 244 | 'toggle' => true, |
| 245 | 'choices' => $choices, |
| 246 | ) |
| 247 | ); |
| 248 | } |
| 249 | |
| 250 | $choices = array(); |
| 251 | $selected = $this->get_selected_keys(); |
| 252 | $taxonomies = array_filter( |
| 253 | acf_get_internal_post_type_posts( 'acf-taxonomy' ), |
| 254 | 'acf_internal_post_object_contains_valid_key' |
| 255 | ); |
| 256 | |
| 257 | if ( $taxonomies ) { |
| 258 | foreach ( $taxonomies as $taxonomy ) { |
| 259 | $choices[ $taxonomy['key'] ] = esc_html( $taxonomy['title'] ); |
| 260 | } |
| 261 | |
| 262 | acf_render_field_wrap( |
| 263 | array( |
| 264 | 'label' => __( 'Select Taxonomies', 'acf' ), |
| 265 | 'type' => 'checkbox', |
| 266 | 'name' => 'taxonomy_keys', |
| 267 | 'prefix' => false, |
| 268 | 'value' => $selected, |
| 269 | 'toggle' => true, |
| 270 | 'choices' => $choices, |
| 271 | ) |
| 272 | ); |
| 273 | } |
| 274 | |
| 275 | $choices = array(); |
| 276 | $selected = $this->get_selected_keys(); |
| 277 | $options_pages = array_filter( |
| 278 | acf_get_internal_post_type_posts( 'acf-ui-options-page' ), |
| 279 | 'acf_internal_post_object_contains_valid_key' |
| 280 | ); |
| 281 | |
| 282 | if ( $options_pages ) { |
| 283 | foreach ( $options_pages as $options_page ) { |
| 284 | $choices[ $options_page['key'] ] = esc_html( $options_page['title'] ); |
| 285 | } |
| 286 | |
| 287 | acf_render_field_wrap( |
| 288 | array( |
| 289 | 'label' => __( 'Select Options Pages', 'acf' ), |
| 290 | 'type' => 'checkbox', |
| 291 | 'name' => 'ui_options_page_keys', |
| 292 | 'prefix' => false, |
| 293 | 'value' => $selected, |
| 294 | 'toggle' => true, |
| 295 | 'choices' => $choices, |
| 296 | ) |
| 297 | ); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Renders the side panel for selecting ACF items to export via PHP. |
| 303 | * |
| 304 | * @since 5.6.3 |
| 305 | */ |
| 306 | public function html_panel_selection() { |
| 307 | ?> |
| 308 | <div class="acf-panel acf-panel-selection"> |
| 309 | <?php $this->html_field_selection(); ?> |
| 310 | </div> |
| 311 | <?php |
| 312 | } |
| 313 | |
| 314 | |
| 315 | /** |
| 316 | * html_archive |
| 317 | * |
| 318 | * description |
| 319 | * |
| 320 | * @date 20/10/17 |
| 321 | * @since 5.6.3 |
| 322 | * |
| 323 | * @param n/a |
| 324 | * @return n/a |
| 325 | */ |
| 326 | function html_archive() { |
| 327 | |
| 328 | ?> |
| 329 | <div class="acf-postbox-header"> |
| 330 | <h2 class="acf-postbox-title"><?php esc_html_e( 'Export', 'acf' ); ?></h2> |
| 331 | <div class="acf-tip"><i tabindex="0" class="acf-icon acf-icon-help acf-js-tooltip" title="<?php esc_attr_e( 'Select the items 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 SCF installation. Generate PHP to export to PHP code which you can place in your theme.', 'acf' ); ?>">?</i></div> |
| 332 | </div> |
| 333 | <div class="acf-postbox-inner"> |
| 334 | <div class="acf-fields"> |
| 335 | <?php $this->html_field_selection(); ?> |
| 336 | </div> |
| 337 | <p class="acf-submit acf-actions-strip"> |
| 338 | <button type="submit" name="action" class="acf-btn acf-button-primary" value="download"><?php esc_html_e( 'Export As JSON', 'acf' ); ?></button> |
| 339 | <button type="submit" name="action" class="acf-btn acf-btn-secondary" value="generate"><?php esc_html_e( 'Generate PHP', 'acf' ); ?></button> |
| 340 | </p> |
| 341 | </div> |
| 342 | <?php |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Renders the PHP export screen. |
| 347 | * |
| 348 | * @since 5.6.3 |
| 349 | */ |
| 350 | public function html_single() { |
| 351 | ?> |
| 352 | <div class="acf-postbox-header"> |
| 353 | <h2 class="acf-postbox-title"><?php esc_html_e( 'Export - Generate PHP', 'acf' ); ?></h2> |
| 354 | <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 items. Storing field groups, post types, or taxonomies locally 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, then deactivate or delete the items from the ACF admin.", 'acf' ); ?>">?</i> |
| 355 | </div> |
| 356 | <div class="acf-postbox-columns"> |
| 357 | <div class="acf-postbox-main"> |
| 358 | <?php $this->html_generate(); ?> |
| 359 | </div> |
| 360 | <div class="acf-postbox-side"> |
| 361 | <?php $this->html_panel_selection(); ?> |
| 362 | <p class="acf-submit"> |
| 363 | <button type="submit" name="action" class="acf-btn" value="generate"><?php esc_html_e( 'Generate PHP', 'acf' ); ?></button> |
| 364 | </p> |
| 365 | </div> |
| 366 | </div> |
| 367 | <?php |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Generates the HTML for the PHP export functionality. |
| 372 | * |
| 373 | * @since 5.6.3 |
| 374 | */ |
| 375 | public function html_generate() { |
| 376 | // Prevent default translation and fake __() within string. |
| 377 | acf_update_setting( 'l10n_var_export', true ); |
| 378 | |
| 379 | $json = $this->get_selected(); |
| 380 | $to_export = array(); |
| 381 | |
| 382 | // Sort by ACF post type first so we can wrap them in related functions. |
| 383 | foreach ( $json as $post ) { |
| 384 | $post_type = acf_determine_internal_post_type( $post['key'] ); |
| 385 | |
| 386 | if ( $post_type ) { |
| 387 | $to_export[ $post_type ][] = $post; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | echo '<textarea id="acf-export-textarea" readonly="readonly">'; |
| 392 | |
| 393 | foreach ( $to_export as $post_type => $posts ) { |
| 394 | if ( 'acf-field-group' === $post_type ) { |
| 395 | echo "add_action( 'acf/include_fields', function() {\r\n"; |
| 396 | echo "\tif ( ! function_exists( 'acf_add_local_field_group' ) ) {\r\n\t\treturn;\r\n\t}\r\n\r\n"; |
| 397 | } elseif ( 'acf-post-type' === $post_type || 'acf-taxonomy' === $post_type ) { |
| 398 | echo "add_action( 'init', function() {\r\n"; |
| 399 | } elseif ( 'acf-ui-options-page' === $post_type ) { |
| 400 | echo "add_action( 'acf/init', function() {\r\n"; |
| 401 | } |
| 402 | |
| 403 | $count = 0; |
| 404 | foreach ( $posts as $post ) { |
| 405 | if ( $count !== 0 ) { |
| 406 | echo "\r\n"; |
| 407 | } |
| 408 | |
| 409 | echo "\t" . acf_export_internal_post_type_as_php( $post, $post_type ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- esc_textarea() used earlier. |
| 410 | ++$count; |
| 411 | } |
| 412 | |
| 413 | if ( in_array( $post_type, array( 'acf-post-type', 'acf-taxonomy', 'acf-field-group', 'acf-ui-options-page' ), true ) ) { |
| 414 | echo "} );\r\n\r\n"; |
| 415 | } |
| 416 | |
| 417 | if ( 'acf-post-type' === $post_type ) { |
| 418 | echo acf_export_enter_title_here( $posts ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- esc_textarea() used earlier. |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | echo '</textarea>'; |
| 423 | ?> |
| 424 | <p class="acf-submit"> |
| 425 | <a class="button" id="acf-export-copy"><?php esc_html_e( 'Copy to clipboard', 'acf' ); ?></a> |
| 426 | </p> |
| 427 | <script type="text/javascript"> |
| 428 | (function($){ |
| 429 | const $a = $('#acf-export-copy'); |
| 430 | const $textarea = $('#acf-export-textarea'); |
| 431 | |
| 432 | // Remove $a if 'copy' is not supported. |
| 433 | if( !document.queryCommandSupported('copy') ) { |
| 434 | return $a.remove(); |
| 435 | } |
| 436 | |
| 437 | $a.on('click', function( e ){ |
| 438 | e.preventDefault(); |
| 439 | |
| 440 | $textarea.get(0).select(); |
| 441 | |
| 442 | try { |
| 443 | var copy = document.execCommand('copy'); |
| 444 | if ( ! copy ) { |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | acf.newTooltip({ |
| 449 | text: "<?php esc_html_e( 'Copied', 'acf' ); ?>", |
| 450 | timeout: 250, |
| 451 | target: $(this), |
| 452 | }); |
| 453 | } catch (err) { |
| 454 | // Do nothing. |
| 455 | } |
| 456 | }); |
| 457 | })(jQuery); |
| 458 | </script> |
| 459 | <?php |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Return an array of keys that have been selected in the export tool. |
| 464 | * |
| 465 | * @since 5.6.3 |
| 466 | * |
| 467 | * @return array|boolean |
| 468 | */ |
| 469 | public function get_selected_keys() { |
| 470 | $key_names = array( 'keys', 'taxonomy_keys', 'post_type_keys', 'ui_options_page_keys' ); |
| 471 | $all_keys = array(); |
| 472 | |
| 473 | foreach ( $key_names as $key_name ) { |
| 474 | if ( $keys = acf_maybe_get_POST( $key_name ) ) { |
| 475 | $all_keys = array_merge( $all_keys, (array) $keys ); |
| 476 | } elseif ( $keys = acf_maybe_get_GET( $key_name ) ) { |
| 477 | $keys = str_replace( ' ', '+', $keys ); |
| 478 | $keys = explode( '+', $keys ); |
| 479 | $all_keys = array_merge( $all_keys, (array) $keys ); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | if ( ! empty( $all_keys ) ) { |
| 484 | return $all_keys; |
| 485 | } |
| 486 | |
| 487 | return false; |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Returns the JSON data for given $_POST args. |
| 492 | * |
| 493 | * @since 5.6.3 |
| 494 | * |
| 495 | * @return array|boolean |
| 496 | */ |
| 497 | public function get_selected() { |
| 498 | $selected = $this->get_selected_keys(); |
| 499 | $json = array(); |
| 500 | |
| 501 | if ( ! $selected ) { |
| 502 | return false; |
| 503 | } |
| 504 | |
| 505 | foreach ( $selected as $key ) { |
| 506 | $post_type = acf_determine_internal_post_type( $key ); |
| 507 | $post = acf_get_internal_post_type( $key, $post_type ); |
| 508 | |
| 509 | if ( empty( $post ) ) { |
| 510 | continue; |
| 511 | } |
| 512 | |
| 513 | if ( 'acf-field-group' === $post_type ) { |
| 514 | $post['fields'] = acf_get_fields( $post ); |
| 515 | } |
| 516 | |
| 517 | $post = acf_prepare_internal_post_type_for_export( $post, $post_type ); |
| 518 | $json[] = $post; |
| 519 | } |
| 520 | |
| 521 | return $json; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | // initialize |
| 526 | acf_register_admin_tool( 'ACF_Admin_Tool_Export' ); |
| 527 | endif; // class_exists check |
| 528 | |
| 529 | ?> |
| 530 |