Make WordPress Core

Changeset 61078


Ignore:
Timestamp:
10/28/2025 01:34:00 PM (7 weeks ago)
Author:
ellatrix
Message:

Template activation: merge changes for Beta 2.

Developed in https://github.com/WordPress/wordpress-develop/pull/10425.
See https://core.trac.wordpress.org/ticket/62755.

Fixes #62755.
Props ellatrix, priethor.

Location:
trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/site-editor.php

    r61029 r61078  
    183183    array( rest_get_route_for_post_type_items( 'page' ), 'OPTIONS' ),
    184184    '/wp/v2/types?context=view',
    185     '/wp/v2/wp_registered_template?context=edit',
    186185    '/wp/v2/types/wp_template?context=edit',
    187186    '/wp/v2/types/wp_template_part?context=edit',
  • trunk/src/wp-includes/block-template-utils.php

    r61029 r61078  
    13591359                }
    13601360            }
    1361         } elseif ( false === $active_templates[ $slug ] ) {
    1362             return null;
    13631361        }
    13641362    }
     
    18831881    update_option( 'active_templates', $active_templates );
    18841882}
     1883
     1884function _wp_migrate_active_templates() {
     1885    // Do not run during installation when the database is not yet available.
     1886    if ( wp_installing() ) {
     1887        return;
     1888    }
     1889
     1890    $active_templates = get_option( 'active_templates', false );
     1891
     1892    if ( false !== $active_templates ) {
     1893        return;
     1894    }
     1895
     1896    // Query all templates in the database. See `get_block_templates`.
     1897    $wp_query_args = array(
     1898        'post_status'         => 'publish',
     1899        'post_type'           => 'wp_template',
     1900        'posts_per_page'      => -1,
     1901        'no_found_rows'       => true,
     1902        'lazy_load_term_meta' => false,
     1903        'tax_query'           => array(
     1904            array(
     1905                'taxonomy' => 'wp_theme',
     1906                'field'    => 'name',
     1907                'terms'    => get_stylesheet(),
     1908            ),
     1909        ),
     1910        // Only get templates that are not inactive by default. We check these
     1911        // meta to make sure we don't fill the option with inactive templates
     1912        // created after the 6.9 release when for some reason the option is
     1913        // deleted.
     1914        'meta_query'          => array(
     1915            'relation' => 'OR',
     1916            array(
     1917                'key'     => 'is_inactive_by_default',
     1918                'compare' => 'NOT EXISTS',
     1919            ),
     1920            array(
     1921                'key'     => 'is_inactive_by_default',
     1922                'value'   => false,
     1923                'compare' => '=',
     1924            ),
     1925        ),
     1926    );
     1927
     1928    $template_query   = new WP_Query( $wp_query_args );
     1929    $active_templates = array();
     1930
     1931    foreach ( $template_query->posts as $post ) {
     1932        $active_templates[ $post->post_name ] = $post->ID;
     1933    }
     1934
     1935    update_option( 'active_templates', $active_templates );
     1936}
  • trunk/src/wp-includes/block-template.php

    r61029 r61078  
    165165    );
    166166
    167     $object            = get_queried_object();
    168     $specific_template = $object ? get_page_template_slug( $object ) : null;
     167    $object_id         = get_queried_object_id();
     168    $specific_template = $object_id && get_post( $object_id ) ? get_page_template_slug( $object_id ) : null;
    169169    $active_templates  = (array) get_option( 'active_templates', array() );
    170 
    171     // Remove templates slugs that are deactivated, except if it's the specific
    172     // template or index.
    173     $slugs = array_filter(
    174         $slugs,
    175         function ( $slug ) use ( $specific_template, $active_templates ) {
    176             $should_ignore = $slug === $specific_template || 'index' === $slug;
    177             return $should_ignore || ( ! isset( $active_templates[ $slug ] ) || false !== $active_templates[ $slug ] );
    178         }
    179     );
    180170
    181171    // We expect one template for each slug. Use the active template if it is
     
    233223    $templates = array_merge( $templates, get_registered_block_templates( $query ) );
    234224
    235     if ( $specific_template ) {
     225    if ( $specific_template && in_array( $specific_template, $remaining_slugs, true ) ) {
    236226        $templates = array_merge( $templates, get_block_templates( array( 'slug__in' => array( $specific_template ) ) ) );
    237227    }
  • trunk/src/wp-includes/default-filters.php

    r61063 r61078  
    571571// Post.
    572572add_action( 'init', 'create_initial_post_types', 0 ); // Highest priority.
     573add_action( 'init', '_wp_migrate_active_templates', 0 ); // Highest priority.
    573574add_action( 'admin_menu', '_add_post_type_submenus' );
    574575add_action( 'before_delete_post', '_reset_front_page_settings_for_post' );
  • trunk/src/wp-includes/post.php

    r61036 r61078  
    399399            'show_in_rest'            => true,
    400400            'rewrite'                 => false,
    401             'rest_base'               => 'wp_template',
     401            'rest_base'               => 'created-templates',
    402402            'rest_controller_class'   => 'WP_REST_Posts_Controller',
    403403            'late_route_registration' => true,
  • trunk/src/wp-includes/rest-api.php

    r61045 r61078  
    264264 */
    265265function create_initial_rest_routes() {
    266     global $wp_post_types;
    267 
    268     // Register the registered templates endpoint. For that we need to copy the
    269     // wp_template post type so that it's available as an entity in core-data.
    270     $wp_post_types['wp_registered_template']                        = clone $wp_post_types['wp_template'];
    271     $wp_post_types['wp_registered_template']->name                  = 'wp_registered_template';
    272     $wp_post_types['wp_registered_template']->rest_base             = 'wp_registered_template';
    273     $wp_post_types['wp_registered_template']->rest_controller_class = 'WP_REST_Registered_Templates_Controller';
    274 
    275266    foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
    276267        $controller = $post_type->get_rest_controller();
     
    298289        }
    299290    }
     291
     292    global $wp_post_types;
    300293
    301294    // Register the old templates endpoints. The WP_REST_Templates_Controller
     
    304297    // To maintain backward and changes to these controller classes, we make use
    305298    // that the wp_template post type has the right information it needs.
     299    $current_wp_template_rest_base           = $wp_post_types['wp_template']->rest_base;
    306300    $wp_post_types['wp_template']->rest_base = 'templates';
    307301    // Store the classes so they can be restored.
     
    329323    $wp_post_types['wp_template']->revisions_rest_controller_class = $original_revisions_rest_controller_class;
    330324    // Restore the original base.
    331     $wp_post_types['wp_template']->rest_base = 'wp_template';
     325    $wp_post_types['wp_template']->rest_base = $current_wp_template_rest_base;
    332326
    333327    // Register the old routes.
     
    356350        )
    357351    );
     352
     353    // Registered templates.
     354    $controller = new WP_REST_Registered_Templates_Controller();
     355    $controller->register_routes();
    358356
    359357    // Post types.
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-registered-templates-controller.php

    r61033 r61078  
    22
    33class WP_REST_Registered_Templates_Controller extends WP_REST_Templates_Controller {
     4    public function __construct() {
     5        parent::__construct( 'wp_template' );
     6        $this->rest_base = 'registered-templates';
     7        $this->namespace = 'wp/v2';
     8    }
     9
    410    public function register_routes() {
    511        // Lists all templates.
     
    8288        $templates    = array();
    8389        foreach ( $query_result as $template ) {
    84             $item               = $this->prepare_item_for_response( $template, $request );
    85             $item->data['type'] = 'wp_registered_template';
    86             $templates[]        = $this->prepare_response_for_collection( $item );
     90            $item        = $this->prepare_item_for_response( $template, $request );
     91            $templates[] = $this->prepare_response_for_collection( $item );
    8792        }
    8893
     
    98103
    99104        $item = $this->prepare_item_for_response( $template, $request );
    100         // adjust the template type here instead
    101         $item->data['type'] = 'wp_registered_template';
    102105        return rest_ensure_response( $item );
    103106    }
  • trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php

    r61045 r61078  
    158158            '/wp/v2/templates/(?P<parent>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/revisions',
    159159            '/wp/v2/templates/(?P<parent>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/revisions/(?P<id>[\d]+)',
    160             '/wp/v2/wp_registered_template',
    161             '/wp/v2/wp_registered_template/(?P<id>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)',
    162             '/wp/v2/wp_template',
    163             '/wp/v2/wp_template/(?P<id>[\d]+)',
    164             '/wp/v2/wp_template/(?P<id>[\d]+)/autosaves',
    165             '/wp/v2/wp_template/(?P<parent>[\d]+)/autosaves/(?P<id>[\d]+)',
    166             '/wp/v2/wp_template/(?P<parent>[\d]+)/revisions',
    167             '/wp/v2/wp_template/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)',
     160            '/wp/v2/registered-templates',
     161            '/wp/v2/registered-templates/(?P<id>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)',
     162            '/wp/v2/created-templates',
     163            '/wp/v2/created-templates/(?P<id>[\d]+)',
     164            '/wp/v2/created-templates/(?P<id>[\d]+)/autosaves',
     165            '/wp/v2/created-templates/(?P<parent>[\d]+)/autosaves/(?P<id>[\d]+)',
     166            '/wp/v2/created-templates/(?P<parent>[\d]+)/revisions',
     167            '/wp/v2/created-templates/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)',
    168168            '/wp/v2/templates/lookup',
    169169            '/wp/v2/themes',
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r61045 r61078  
    55095509            ]
    55105510        },
    5511         "/wp/v2/wp_template/(?P<parent>[\\d]+)/revisions": {
     5511        "/wp/v2/created-templates/(?P<parent>[\\d]+)/revisions": {
    55125512            "namespace": "wp/v2",
    55135513            "methods": [
     
    56075607            ]
    56085608        },
    5609         "/wp/v2/wp_template/(?P<parent>[\\d]+)/revisions/(?P<id>[\\d]+)": {
     5609        "/wp/v2/created-templates/(?P<parent>[\\d]+)/revisions/(?P<id>[\\d]+)": {
    56105610            "namespace": "wp/v2",
    56115611            "methods": [
     
    56675667            ]
    56685668        },
    5669         "/wp/v2/wp_template/(?P<id>[\\d]+)/autosaves": {
     5669        "/wp/v2/created-templates/(?P<id>[\\d]+)/autosaves": {
    56705670            "namespace": "wp/v2",
    56715671            "methods": [
     
    58665866            ]
    58675867        },
    5868         "/wp/v2/wp_template/(?P<parent>[\\d]+)/autosaves/(?P<id>[\\d]+)": {
     5868        "/wp/v2/created-templates/(?P<parent>[\\d]+)/autosaves/(?P<id>[\\d]+)": {
    58695869            "namespace": "wp/v2",
    58705870            "methods": [
     
    59025902            ]
    59035903        },
    5904         "/wp/v2/wp_template": {
     5904        "/wp/v2/created-templates": {
    59055905            "namespace": "wp/v2",
    59065906            "methods": [
     
    62666266                "self": [
    62676267                    {
    6268                         "href": "http://example.org/index.php?rest_route=/wp/v2/wp_template"
     6268                        "href": "http://example.org/index.php?rest_route=/wp/v2/created-templates"
    62696269                    }
    62706270                ]
    62716271            }
    62726272        },
    6273         "/wp/v2/wp_template/(?P<id>[\\d]+)": {
     6273        "/wp/v2/created-templates/(?P<id>[\\d]+)": {
    62746274            "namespace": "wp/v2",
    62756275            "methods": [
     
    86728672            ]
    86738673        },
    8674         "/wp/v2/wp_registered_template": {
    8675             "namespace": "wp/v2",
    8676             "methods": [
    8677                 "GET"
    8678             ],
    8679             "endpoints": [
    8680                 {
    8681                     "methods": [
    8682                         "GET"
    8683                     ],
    8684                     "args": {
    8685                         "context": {
    8686                             "description": "Scope under which the request is made; determines fields present in response.",
    8687                             "type": "string",
    8688                             "enum": [
    8689                                 "view",
    8690                                 "embed",
    8691                                 "edit"
    8692                             ],
    8693                             "default": "view",
    8694                             "required": false
    8695                         },
    8696                         "wp_id": {
    8697                             "description": "Limit to the specified post id.",
    8698                             "type": "integer",
    8699                             "required": false
    8700                         },
    8701                         "area": {
    8702                             "description": "Limit to the specified template part area.",
    8703                             "type": "string",
    8704                             "required": false
    8705                         },
    8706                         "post_type": {
    8707                             "description": "Post type to get the templates for.",
    8708                             "type": "string",
    8709                             "required": false
    8710                         }
    8711                     }
    8712                 }
    8713             ],
    8714             "_links": {
    8715                 "self": [
    8716                     {
    8717                         "href": "http://example.org/index.php?rest_route=/wp/v2/wp_registered_template"
    8718                     }
    8719                 ]
    8720             }
    8721         },
    8722         "/wp/v2/wp_registered_template/(?P<id>([^\\/:<>\\*\\?\"\\|]+(?:\\/[^\\/:<>\\*\\?\"\\|]+)?)[\\/\\w%-]+)": {
    8723             "namespace": "wp/v2",
    8724             "methods": [
    8725                 "GET"
    8726             ],
    8727             "endpoints": [
    8728                 {
    8729                     "methods": [
    8730                         "GET"
    8731                     ],
    8732                     "args": {
    8733                         "id": {
    8734                             "description": "The id of a template",
    8735                             "type": "string",
    8736                             "required": false
    8737                         },
    8738                         "context": {
    8739                             "description": "Scope under which the request is made; determines fields present in response.",
    8740                             "type": "string",
    8741                             "enum": [
    8742                                 "view",
    8743                                 "embed",
    8744                                 "edit"
    8745                             ],
    8746                             "default": "view",
    8747                             "required": false
    8748                         }
    8749                     }
    8750                 }
    8751             ]
    8752         },
    87538674        "/wp/v2/templates/(?P<id>([^\\/:<>\\*\\?\"\\|]+(?:\\/[^\\/:<>\\*\\?\"\\|]+)?)[\\/\\w%-]+)/autosaves": {
    87548675            "namespace": "wp/v2",
     
    94299350                            "default": false,
    94309351                            "description": "Whether to bypass Trash and force deletion.",
     9352                            "required": false
     9353                        }
     9354                    }
     9355                }
     9356            ]
     9357        },
     9358        "/wp/v2/registered-templates": {
     9359            "namespace": "wp/v2",
     9360            "methods": [
     9361                "GET"
     9362            ],
     9363            "endpoints": [
     9364                {
     9365                    "methods": [
     9366                        "GET"
     9367                    ],
     9368                    "args": {
     9369                        "context": {
     9370                            "description": "Scope under which the request is made; determines fields present in response.",
     9371                            "type": "string",
     9372                            "enum": [
     9373                                "view",
     9374                                "embed",
     9375                                "edit"
     9376                            ],
     9377                            "default": "view",
     9378                            "required": false
     9379                        },
     9380                        "wp_id": {
     9381                            "description": "Limit to the specified post id.",
     9382                            "type": "integer",
     9383                            "required": false
     9384                        },
     9385                        "area": {
     9386                            "description": "Limit to the specified template part area.",
     9387                            "type": "string",
     9388                            "required": false
     9389                        },
     9390                        "post_type": {
     9391                            "description": "Post type to get the templates for.",
     9392                            "type": "string",
     9393                            "required": false
     9394                        }
     9395                    }
     9396                }
     9397            ],
     9398            "_links": {
     9399                "self": [
     9400                    {
     9401                        "href": "http://example.org/index.php?rest_route=/wp/v2/registered-templates"
     9402                    }
     9403                ]
     9404            }
     9405        },
     9406        "/wp/v2/registered-templates/(?P<id>([^\\/:<>\\*\\?\"\\|]+(?:\\/[^\\/:<>\\*\\?\"\\|]+)?)[\\/\\w%-]+)": {
     9407            "namespace": "wp/v2",
     9408            "methods": [
     9409                "GET"
     9410            ],
     9411            "endpoints": [
     9412                {
     9413                    "methods": [
     9414                        "GET"
     9415                    ],
     9416                    "args": {
     9417                        "id": {
     9418                            "description": "The id of a template",
     9419                            "type": "string",
     9420                            "required": false
     9421                        },
     9422                        "context": {
     9423                            "description": "Scope under which the request is made; determines fields present in response.",
     9424                            "type": "string",
     9425                            "enum": [
     9426                                "view",
     9427                                "embed",
     9428                                "edit"
     9429                            ],
     9430                            "default": "view",
    94319431                            "required": false
    94329432                        }
     
    1081010810                                    "wp_navigation": "wp_navigation",
    1081110811                                    "wp_font_family": "wp_font_family",
    10812                                     "wp_font_face": "wp_font_face",
    10813                                     "wp_registered_template": "wp_registered_template"
     10812                                    "wp_font_face": "wp_font_face"
    1081410813                                }
    1081510814                            },
     
    1476914768        "icon": null,
    1477014769        "taxonomies": [],
    14771         "rest_base": "wp_template",
     14770        "rest_base": "created-templates",
    1477214771        "rest_namespace": "wp/v2",
    1477314772        "template": [],
     
    1478114780            "wp:items": [
    1478214781                {
    14783                     "href": "http://example.org/index.php?rest_route=/wp/v2/wp_template"
     14782                    "href": "http://example.org/index.php?rest_route=/wp/v2/created-templates"
    1478414783                }
    1478514784            ],
     
    1494214941                {
    1494314942                    "href": "http://example.org/index.php?rest_route=/wp/v2/font-families/(?P<font_family_id>[\\d]+)/font-faces"
    14944                 }
    14945             ],
    14946             "curies": [
    14947                 {
    14948                     "name": "wp",
    14949                     "href": "https://api.w.org/{rel}",
    14950                     "templated": true
    14951                 }
    14952             ]
    14953         }
    14954     },
    14955     "wp_registered_template": {
    14956         "description": "Templates to include in your theme.",
    14957         "hierarchical": false,
    14958         "has_archive": false,
    14959         "name": "Templates",
    14960         "slug": "wp_registered_template",
    14961         "icon": null,
    14962         "taxonomies": [],
    14963         "rest_base": "wp_registered_template",
    14964         "rest_namespace": "wp/v2",
    14965         "template": [],
    14966         "template_lock": false,
    14967         "_links": {
    14968             "collection": [
    14969                 {
    14970                     "href": "http://example.org/index.php?rest_route=/wp/v2/types"
    14971                 }
    14972             ],
    14973             "wp:items": [
    14974                 {
    14975                     "href": "http://example.org/index.php?rest_route=/wp/v2/wp_registered_template"
    1497614943                }
    1497714944            ],
     
    1559115558    "default_ping_status": "open",
    1559215559    "default_comment_status": "open",
    15593     "active_templates": null,
     15560    "active_templates": [],
    1559415561    "site_logo": null,
    1559515562    "site_icon": 0
Note: See TracChangeset for help on using the changeset viewer.