|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +// Migrate existing "edited" templates. By existing, it means that the template |
| 4 | +// is active. |
| 5 | +function gutenberg_get_migrated_active_templates() { |
| 6 | + // Query all templates in the database. See `get_block_templates`. |
| 7 | + $wp_query_args = array( |
| 8 | + 'post_status' => 'publish', |
| 9 | + 'post_type' => 'wp_template', |
| 10 | + 'posts_per_page' => -1, |
| 11 | + 'no_found_rows' => true, |
| 12 | + 'lazy_load_term_meta' => false, |
| 13 | + 'tax_query' => array( |
| 14 | + array( |
| 15 | + 'taxonomy' => 'wp_theme', |
| 16 | + 'field' => 'name', |
| 17 | + 'terms' => get_stylesheet(), |
| 18 | + ), |
| 19 | + ), |
| 20 | + // Only get templates that are not inactive by default. |
| 21 | + 'meta_query' => array( |
| 22 | + 'relation' => 'OR', |
| 23 | + array( |
| 24 | + 'key' => 'is_inactive_by_default', |
| 25 | + 'compare' => 'NOT EXISTS', |
| 26 | + ), |
| 27 | + array( |
| 28 | + 'key' => 'is_inactive_by_default', |
| 29 | + 'value' => false, |
| 30 | + 'compare' => '=', |
| 31 | + ), |
| 32 | + ), |
| 33 | + ); |
| 34 | + |
| 35 | + $template_query = new WP_Query( $wp_query_args ); |
| 36 | + $active_templates = array(); |
| 37 | + |
| 38 | + foreach ( $template_query->posts as $post ) { |
| 39 | + $active_templates[ $post->post_name ] = $post->ID; |
| 40 | + } |
| 41 | + |
| 42 | + return $active_templates; |
| 43 | +} |
| 44 | + |
| 45 | +// Only load template activation system if the experiment is enabled. |
| 46 | +if ( ! gutenberg_is_experiment_enabled( 'active_templates' ) ) { |
| 47 | + return; |
| 48 | +} |
| 49 | + |
3 | 50 | require_once __DIR__ . '/class-gutenberg-rest-old-templates-controller.php'; |
4 | 51 |
|
5 | 52 | // How does this work? |
@@ -500,55 +547,6 @@ function gutenberg_set_active_template_theme( $changes, $request ) { |
500 | 547 | return $changes; |
501 | 548 | } |
502 | 549 |
|
503 | | -// Migrate existing "edited" templates. By existing, it means that the template |
504 | | -// is active. |
505 | | -add_action( 'init', 'gutenberg_migrate_existing_templates' ); |
506 | | -function gutenberg_migrate_existing_templates() { |
507 | | - $active_templates = get_option( 'active_templates', false ); |
508 | | - |
509 | | - if ( false !== $active_templates ) { |
510 | | - return; |
511 | | - } |
512 | | - |
513 | | - // Query all templates in the database. See `get_block_templates`. |
514 | | - $wp_query_args = array( |
515 | | - 'post_status' => 'publish', |
516 | | - 'post_type' => 'wp_template', |
517 | | - 'posts_per_page' => -1, |
518 | | - 'no_found_rows' => true, |
519 | | - 'lazy_load_term_meta' => false, |
520 | | - 'tax_query' => array( |
521 | | - array( |
522 | | - 'taxonomy' => 'wp_theme', |
523 | | - 'field' => 'name', |
524 | | - 'terms' => get_stylesheet(), |
525 | | - ), |
526 | | - ), |
527 | | - // Only get templates that are not inactive by default. |
528 | | - 'meta_query' => array( |
529 | | - 'relation' => 'OR', |
530 | | - array( |
531 | | - 'key' => 'is_inactive_by_default', |
532 | | - 'compare' => 'NOT EXISTS', |
533 | | - ), |
534 | | - array( |
535 | | - 'key' => 'is_inactive_by_default', |
536 | | - 'value' => false, |
537 | | - 'compare' => '=', |
538 | | - ), |
539 | | - ), |
540 | | - ); |
541 | | - |
542 | | - $template_query = new WP_Query( $wp_query_args ); |
543 | | - $active_templates = array(); |
544 | | - |
545 | | - foreach ( $template_query->posts as $post ) { |
546 | | - $active_templates[ $post->post_name ] = $post->ID; |
547 | | - } |
548 | | - |
549 | | - update_option( 'active_templates', $active_templates ); |
550 | | -} |
551 | | - |
552 | 550 | add_action( 'save_post_wp_template', 'gutenberg_maybe_update_active_templates' ); |
553 | 551 | function gutenberg_maybe_update_active_templates( $post_id ) { |
554 | 552 | $post = get_post( $post_id ); |
|
0 commit comments