diff --git a/.github/workflows/performance.yml b/.github/workflows/performance.yml index 78352971755d67..9451c122121bb2 100644 --- a/.github/workflows/performance.yml +++ b/.github/workflows/performance.yml @@ -33,7 +33,7 @@ jobs: name: Run performance tests runs-on: ubuntu-latest permissions: - contents: read + contents: read if: ${{ github.repository == 'WordPress/gutenberg' }} env: WP_ARTIFACTS_PATH: ${{ github.workspace }}/artifacts @@ -76,13 +76,14 @@ jobs: - name: Compare performance with base branch if: github.event_name == 'push' # The base hash used here need to be a commit that is compatible with the current WP version - # The current one is c7722262e65a3f4d0f1a2d1ad29eccb2069509e4 and it needs to be updated every WP major release. + # The current one is 177c166a5eada898ad015f5efaf1c6fefa7042c4 and it needs to be updated every WP major release. # It is used as a base comparison point to avoid fluctuation in the performance metrics. + # See: https://developer.wordpress.org/block-editor/explanations/architecture/performance/#update-the-reference-commit. run: | WP_VERSION="$(awk -F ': ' '/^Tested up to/{print $2}' readme.txt)" IFS=. read -ra WP_VERSION_ARRAY <<< "$WP_VERSION" WP_MAJOR="${WP_VERSION_ARRAY[0]}.${WP_VERSION_ARRAY[1]}" - ./bin/plugin/cli.js perf "$GITHUB_SHA" c7722262e65a3f4d0f1a2d1ad29eccb2069509e4 --tests-branch "$GITHUB_SHA" --wp-version "$WP_MAJOR" + ./bin/plugin/cli.js perf "$GITHUB_SHA" 177c166a5eada898ad015f5efaf1c6fefa7042c4 --tests-branch "$GITHUB_SHA" --wp-version "$WP_MAJOR" - name: Compare performance with custom branches if: github.event_name == 'workflow_dispatch' diff --git a/gutenberg.php b/gutenberg.php index 9874d7073541de..49b26f41958f42 100644 --- a/gutenberg.php +++ b/gutenberg.php @@ -3,7 +3,7 @@ * Plugin Name: Gutenberg * Plugin URI: https://github.com/WordPress/gutenberg * Description: Printing since 1440. This is the development plugin for the block editor, site editor, and other future WordPress core functionality. - * Requires at least: 6.6 + * Requires at least: 6.7 * Requires PHP: 7.2 * Version: 20.7.0 * Author: Gutenberg Team @@ -15,7 +15,7 @@ ### BEGIN AUTO-GENERATED DEFINES defined( 'GUTENBERG_DEVELOPMENT_MODE' ) or define( 'GUTENBERG_DEVELOPMENT_MODE', true ); ### END AUTO-GENERATED DEFINES -defined( 'GUTENBERG_MINIMUM_WP_VERSION' ) or define( 'GUTENBERG_MINIMUM_WP_VERSION', '6.6' ); +defined( 'GUTENBERG_MINIMUM_WP_VERSION' ) or define( 'GUTENBERG_MINIMUM_WP_VERSION', '6.7' ); gutenberg_pre_init(); diff --git a/lib/compat/wordpress-6.7/block-bindings.php b/lib/compat/wordpress-6.7/block-bindings.php deleted file mode 100644 index 08608a8d394e72..00000000000000 --- a/lib/compat/wordpress-6.7/block-bindings.php +++ /dev/null @@ -1,82 +0,0 @@ - $source->name, - 'label' => $source->label, - 'usesContext' => $source->uses_context, - ); - } - $script = sprintf( 'for ( const source of %s ) { ! wp.blocks.getBlockBindingsSource( source.name ) && wp.blocks.registerBlockBindingsSource( source ); }', wp_json_encode( $filtered_sources ) ); - wp_add_inline_script( - 'wp-blocks', - $script - ); - } -} - -add_action( 'enqueue_block_editor_assets', 'gutenberg_bootstrap_server_block_bindings_sources', 5 ); - -/** - * Initialize `canUpdateBlockBindings` editor setting if it doesn't exist. By default, it is `true` only for admin users. - * - * @param array $editor_settings The block editor settings from the `block_editor_settings_all` filter. - * @return array The editor settings including `canUpdateBlockBindings`. - */ -function gutenberg_add_can_update_block_bindings_editor_setting( $editor_settings ) { - if ( empty( $editor_settings['canUpdateBlockBindings'] ) ) { - $editor_settings['canUpdateBlockBindings'] = current_user_can( 'manage_options' ); - } - return $editor_settings; -} - -add_filter( 'block_editor_settings_all', 'gutenberg_add_can_update_block_bindings_editor_setting', 10 ); - -/** - * Add `label` to `register_meta`. - * - * @param array $args Array of arguments for registering meta. - * @return array Modified arguments array including `label`. - */ -function gutenberg_update_meta_args_with_label( $args ) { - // Don't update schema when label isn't provided. - if ( ! isset( $args['label'] ) ) { - return $args; - } - - // Don't update schema if not exposed to REST - if ( ! isset( $args['show_in_rest'] ) ) { - return $args; - } - - $schema = array( 'title' => $args['label'] ); - if ( ! is_array( $args['show_in_rest'] ) ) { - $args['show_in_rest'] = array( - 'schema' => $schema, - ); - return $args; - } - - if ( ! empty( $args['show_in_rest']['schema'] ) ) { - $args['show_in_rest']['schema'] = array_merge( $args['show_in_rest']['schema'], $schema ); - } else { - $args['show_in_rest']['schema'] = $schema; - } - - return $args; -} - -// Priority must be lower than 10 to ensure the label is not removed. -add_filter( 'register_meta_args', 'gutenberg_update_meta_args_with_label', 5, 1 ); diff --git a/lib/compat/wordpress-6.7/block-templates.php b/lib/compat/wordpress-6.7/block-templates.php deleted file mode 100644 index acbd0b4981fe29..00000000000000 --- a/lib/compat/wordpress-6.7/block-templates.php +++ /dev/null @@ -1,84 +0,0 @@ -register( $template_name, $args ); - } -} - -if ( ! function_exists( 'unregister_block_template' ) ) { - /** - * Unregister a template. - * - * @param string $template_name Template name in the form of `plugin_uri//template_name`. - * @return WP_Block_Template|WP_Error The unregistered template object on success, WP_Error object on failure or if - * the template doesn't exist. - */ - function unregister_block_template( $template_name ) { - return WP_Block_Templates_Registry::get_instance()->unregister( $template_name ); - } -} - -if ( ! function_exists( 'wp_register_block_template' ) ) { - /** - * Register a template. - * - * @deprecated 19.4.0 wp_register_block_template is deprecated. Please use register_block_template instead. - * - * @param string $template_name Template name in the form of `plugin_uri//template_name`. - * @param array|string $args { - * Optional. Array or string of arguments for registering a block template. - * - * @type string $title Optional. Title of the template as it will be shown in the Site Editor - * and other UI elements. - * @type string $description Optional. Description of the template as it will be shown in the Site - * Editor. - * @type string $content Optional. Default content of the template that will be used when the - * template is rendered or edited in the editor. - * @type string[] $post_types Optional. Array of post types to which the template should be available. - * @type string $plugin Uri of the plugin that registers the template. - * } - * @return WP_Block_Template|WP_Error The registered template object on success, WP_Error object on failure. - */ - function wp_register_block_template( $template_name, $args = array() ) { - _deprecated_function( __FUNCTION__, 'Gutenberg 19.4.0', 'register_block_template' ); - return register_block_template( $template_name, $args ); - } -} - -if ( ! function_exists( 'wp_unregister_block_template' ) ) { - /** - * Unregister a template. - * - * @deprecated 19.4.0 wp_unregister_block_template is deprecated. Please use unregister_block_template instead. - * - * @param string $template_name Template name in the form of `plugin_uri//template_name`. - * @return WP_Block_Template|WP_Error The unregistered template object on success, WP_Error object on failure or if - * the template doesn't exist. - */ - function wp_unregister_block_template( $template_name ) { - _deprecated_function( __FUNCTION__, 'Gutenberg 19.4.0', 'unregister_block_template' ); - return unregister_block_template( $template_name ); - } -} diff --git a/lib/compat/wordpress-6.7/blocks.php b/lib/compat/wordpress-6.7/blocks.php deleted file mode 100644 index e739808000c5f3..00000000000000 --- a/lib/compat/wordpress-6.7/blocks.php +++ /dev/null @@ -1,139 +0,0 @@ -context['query']['format'] ) || ! is_array( $block->context['query']['format'] ) ) { - return $query; - } - - // Return early if the query already contains a post format. This is to avoid duplication if the - // WordPress core filter is already applied. - if ( ! empty( $query['tax_query'] ) ) { - foreach ( $query['tax_query'] as $taxquery ) { - if ( isset( $taxquery['taxonomy'] ) && 'post_format' === $taxquery['taxonomy'] ) { - return $query; - } - } - } - - $formats = $block->context['query']['format']; - /* - * Validate that the format is either `standard` or a supported post format. - * - First, add `standard` to the array of valid formats. - * - Then, remove any invalid formats. - */ - $valid_formats = array_merge( array( 'standard' ), get_post_format_slugs() ); - $formats = array_intersect( $formats, $valid_formats ); - - /* - * The relation needs to be set to `OR` since the request can contain - * two separate conditions. The user may be querying for items that have - * either the `standard` format or a specific format. - */ - $formats_query = array( 'relation' => 'OR' ); - - /* - * The default post format, `standard`, is not stored in the database. - * If `standard` is part of the request, the query needs to exclude all post items that - * have a format assigned. - */ - if ( in_array( 'standard', $formats, true ) ) { - $formats_query[] = array( - 'taxonomy' => 'post_format', - 'field' => 'slug', - 'operator' => 'NOT EXISTS', - ); - // Remove the `standard` format, since it cannot be queried. - unset( $formats[ array_search( 'standard', $formats, true ) ] ); - } - // Add any remaining formats to the formats query. - if ( ! empty( $formats ) ) { - // Add the `post-format-` prefix. - $terms = array_map( - static function ( $format ) { - return "post-format-$format"; - }, - $formats - ); - $formats_query[] = array( - 'taxonomy' => 'post_format', - 'field' => 'slug', - 'terms' => $terms, - 'operator' => 'IN', - ); - } - - /* - * Add `$formats_query` to `$query`, as long as it contains more than one key: - * If `$formats_query` only contains the initial `relation` key, there are no valid formats to query, - * and the query should not be modified. - */ - if ( count( $formats_query ) > 1 ) { - // Enable filtering by both post formats and other taxonomies by combining them with `AND`. - if ( empty( $query['tax_query'] ) ) { - $query['tax_query'] = $formats_query; - } else { - $query['tax_query'] = array( - 'relation' => 'AND', - $query['tax_query'], - $formats_query, - ); - } - } - - return $query; -} -add_filter( 'query_loop_block_query_vars', 'gutenberg_add_format_query_vars_to_query_loop_block', 10, 2 ); diff --git a/lib/compat/wordpress-6.7/class-gutenberg-rest-posts-controller-6-7.php b/lib/compat/wordpress-6.7/class-gutenberg-rest-posts-controller-6-7.php deleted file mode 100644 index 5d3795cf68db76..00000000000000 --- a/lib/compat/wordpress-6.7/class-gutenberg-rest-posts-controller-6-7.php +++ /dev/null @@ -1,704 +0,0 @@ - 400 ) - ); - } - - // Ensure an include parameter is set in case the orderby is set to 'include'. - if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) { - return new WP_Error( - 'rest_orderby_include_missing_include', - __( 'You need to define an include parameter to order by include.' ), - array( 'status' => 400 ) - ); - } - - // Retrieve the list of registered collection query parameters. - $registered = $this->get_collection_params(); - $args = array(); - - /* - * This array defines mappings between public API query parameters whose - * values are accepted as-passed, and their internal WP_Query parameter - * name equivalents (some are the same). Only values which are also - * present in $registered will be set. - */ - $parameter_mappings = array( - 'author' => 'author__in', - 'author_exclude' => 'author__not_in', - 'exclude' => 'post__not_in', - 'include' => 'post__in', - 'menu_order' => 'menu_order', - 'offset' => 'offset', - 'order' => 'order', - 'orderby' => 'orderby', - 'page' => 'paged', - 'parent' => 'post_parent__in', - 'parent_exclude' => 'post_parent__not_in', - 'search' => 's', - 'search_columns' => 'search_columns', - 'slug' => 'post_name__in', - 'status' => 'post_status', - ); - - /* - * For each known parameter which is both registered and present in the request, - * set the parameter's value on the query $args. - */ - foreach ( $parameter_mappings as $api_param => $wp_param ) { - if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { - $args[ $wp_param ] = $request[ $api_param ]; - } - } - - // Check for & assign any parameters which require special handling or setting. - $args['date_query'] = array(); - - if ( isset( $registered['before'], $request['before'] ) ) { - $args['date_query'][] = array( - 'before' => $request['before'], - 'column' => 'post_date', - ); - } - - if ( isset( $registered['modified_before'], $request['modified_before'] ) ) { - $args['date_query'][] = array( - 'before' => $request['modified_before'], - 'column' => 'post_modified', - ); - } - - if ( isset( $registered['after'], $request['after'] ) ) { - $args['date_query'][] = array( - 'after' => $request['after'], - 'column' => 'post_date', - ); - } - - if ( isset( $registered['modified_after'], $request['modified_after'] ) ) { - $args['date_query'][] = array( - 'after' => $request['modified_after'], - 'column' => 'post_modified', - ); - } - - // Ensure our per_page parameter overrides any provided posts_per_page filter. - if ( isset( $registered['per_page'] ) ) { - $args['posts_per_page'] = $request['per_page']; - } - - if ( isset( $registered['sticky'], $request['sticky'] ) ) { - $sticky_posts = get_option( 'sticky_posts', array() ); - if ( ! is_array( $sticky_posts ) ) { - $sticky_posts = array(); - } - if ( $request['sticky'] ) { - /* - * As post__in will be used to only get sticky posts, - * we have to support the case where post__in was already - * specified. - */ - $args['post__in'] = $args['post__in'] ? array_intersect( $sticky_posts, $args['post__in'] ) : $sticky_posts; - - /* - * If we intersected, but there are no post IDs in common, - * WP_Query won't return "no posts" for post__in = array() - * so we have to fake it a bit. - */ - if ( ! $args['post__in'] ) { - $args['post__in'] = array( 0 ); - } - } elseif ( $sticky_posts ) { - /* - * As post___not_in will be used to only get posts that - * are not sticky, we have to support the case where post__not_in - * was already specified. - */ - $args['post__not_in'] = array_merge( $args['post__not_in'], $sticky_posts ); - } - } - - $args = $this->prepare_tax_query( $args, $request ); - - if ( ! empty( $request['format'] ) ) { - $formats = $request['format']; - /* - * The relation needs to be set to `OR` since the request can contain - * two separate conditions. The user may be querying for items that have - * either the `standard` format or a specific format. - */ - $formats_query = array( 'relation' => 'OR' ); - - /* - * The default post format, `standard`, is not stored in the database. - * If `standard` is part of the request, the query needs to exclude all post items that - * have a format assigned. - */ - if ( in_array( 'standard', $formats, true ) ) { - $formats_query[] = array( - 'taxonomy' => 'post_format', - 'field' => 'slug', - 'operator' => 'NOT EXISTS', - ); - // Remove the `standard` format, since it cannot be queried. - unset( $formats[ array_search( 'standard', $formats, true ) ] ); - } - - // Add any remaining formats to the formats query. - if ( ! empty( $formats ) ) { - // Add the `post-format-` prefix. - $terms = array_map( - static function ( $format ) { - return "post-format-$format"; - }, - $formats - ); - - $formats_query[] = array( - 'taxonomy' => 'post_format', - 'field' => 'slug', - 'terms' => $terms, - 'operator' => 'IN', - ); - } - - // Enable filtering by both post formats and other taxonomies by combining them with `AND`. - if ( isset( $args['tax_query'] ) ) { - $args['tax_query'][] = array( - 'relation' => 'AND', - $formats_query, - ); - } else { - $args['tax_query'] = $formats_query; - } - } - - // Force the post_type argument, since it's not a user input variable. - $args['post_type'] = $this->post_type; - - /** - * Filters WP_Query arguments when querying posts via the REST API. - * - * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug. - * - * Possible hook names include: - * - * - `rest_post_query` - * - `rest_page_query` - * - `rest_attachment_query` - * - * Enables adding extra arguments or setting defaults for a post collection request. - * - * @since 4.7.0 - * @since 5.7.0 Moved after the `tax_query` query arg is generated. - * - * @link https://developer.wordpress.org/reference/classes/wp_query/ - * - * @param array $args Array of arguments for WP_Query. - * @param WP_REST_Request $request The REST API request. - */ - $args = apply_filters( "rest_{$this->post_type}_query", $args, $request ); - $query_args = $this->prepare_items_query( $args, $request ); - - $posts_query = new WP_Query(); - $query_result = $posts_query->query( $query_args ); - - // Allow access to all password protected posts if the context is edit. - if ( 'edit' === $request['context'] ) { - add_filter( 'post_password_required', array( $this, 'check_password_required' ), 10, 2 ); - } - - $posts = array(); - - update_post_author_caches( $query_result ); - update_post_parent_caches( $query_result ); - - if ( post_type_supports( $this->post_type, 'thumbnail' ) ) { - update_post_thumbnail_cache( $posts_query ); - } - - foreach ( $query_result as $post ) { - if ( ! $this->check_read_permission( $post ) ) { - continue; - } - - $data = $this->prepare_item_for_response( $post, $request ); - $posts[] = $this->prepare_response_for_collection( $data ); - } - - // Reset filter. - if ( 'edit' === $request['context'] ) { - remove_filter( 'post_password_required', array( $this, 'check_password_required' ) ); - } - - $page = (int) $query_args['paged']; - $total_posts = $posts_query->found_posts; - - if ( $total_posts < 1 && $page > 1 ) { - // Out-of-bounds, run the query again without LIMIT for total count. - unset( $query_args['paged'] ); - - $count_query = new WP_Query(); - $count_query->query( $query_args ); - $total_posts = $count_query->found_posts; - } - - $max_pages = (int) ceil( $total_posts / (int) $posts_query->query_vars['posts_per_page'] ); - - if ( $page > $max_pages && $total_posts > 0 ) { - return new WP_Error( - 'rest_post_invalid_page_number', - __( 'The page number requested is larger than the number of pages available.' ), - array( 'status' => 400 ) - ); - } - - $response = rest_ensure_response( $posts ); - - $response->header( 'X-WP-Total', (int) $total_posts ); - $response->header( 'X-WP-TotalPages', (int) $max_pages ); - - $request_params = $request->get_query_params(); - $collection_url = rest_url( rest_get_route_for_post_type_items( $this->post_type ) ); - $base = add_query_arg( urlencode_deep( $request_params ), $collection_url ); - - if ( $page > 1 ) { - $prev_page = $page - 1; - - if ( $prev_page > $max_pages ) { - $prev_page = $max_pages; - } - - $prev_link = add_query_arg( 'page', $prev_page, $base ); - $response->link_header( 'prev', $prev_link ); - } - if ( $max_pages > $page ) { - $next_page = $page + 1; - $next_link = add_query_arg( 'page', $next_page, $base ); - - $response->link_header( 'next', $next_link ); - } - - return $response; - } - - /** - * Retrieves the query params for the posts collection. - * - * @since 4.7.0 - * @since 5.4.0 The `tax_relation` query parameter was added. - * @since 5.7.0 The `modified_after` and `modified_before` query parameters were added. - * @since 6.7.0 The `format` query parameter was added. - * - * @return array Collection parameters. - */ - public function get_collection_params() { - $query_params = parent::get_collection_params(); - - $query_params['context']['default'] = 'view'; - - $query_params['after'] = array( - 'description' => __( 'Limit response to posts published after a given ISO8601 compliant date.' ), - 'type' => 'string', - 'format' => 'date-time', - ); - - $query_params['modified_after'] = array( - 'description' => __( 'Limit response to posts modified after a given ISO8601 compliant date.' ), - 'type' => 'string', - 'format' => 'date-time', - ); - - if ( post_type_supports( $this->post_type, 'author' ) ) { - $query_params['author'] = array( - 'description' => __( 'Limit result set to posts assigned to specific authors.' ), - 'type' => 'array', - 'items' => array( - 'type' => 'integer', - ), - 'default' => array(), - ); - $query_params['author_exclude'] = array( - 'description' => __( 'Ensure result set excludes posts assigned to specific authors.' ), - 'type' => 'array', - 'items' => array( - 'type' => 'integer', - ), - 'default' => array(), - ); - } - - $query_params['before'] = array( - 'description' => __( 'Limit response to posts published before a given ISO8601 compliant date.' ), - 'type' => 'string', - 'format' => 'date-time', - ); - - $query_params['modified_before'] = array( - 'description' => __( 'Limit response to posts modified before a given ISO8601 compliant date.' ), - 'type' => 'string', - 'format' => 'date-time', - ); - - $query_params['exclude'] = array( - 'description' => __( 'Ensure result set excludes specific IDs.' ), - 'type' => 'array', - 'items' => array( - 'type' => 'integer', - ), - 'default' => array(), - ); - - $query_params['include'] = array( - 'description' => __( 'Limit result set to specific IDs.' ), - 'type' => 'array', - 'items' => array( - 'type' => 'integer', - ), - 'default' => array(), - ); - - if ( 'page' === $this->post_type || post_type_supports( $this->post_type, 'page-attributes' ) ) { - $query_params['menu_order'] = array( - 'description' => __( 'Limit result set to posts with a specific menu_order value.' ), - 'type' => 'integer', - ); - } - - $query_params['offset'] = array( - 'description' => __( 'Offset the result set by a specific number of items.' ), - 'type' => 'integer', - ); - - $query_params['order'] = array( - 'description' => __( 'Order sort attribute ascending or descending.' ), - 'type' => 'string', - 'default' => 'desc', - 'enum' => array( 'asc', 'desc' ), - ); - - $query_params['orderby'] = array( - 'description' => __( 'Sort collection by post attribute.' ), - 'type' => 'string', - 'default' => 'date', - 'enum' => array( - 'author', - 'date', - 'id', - 'include', - 'modified', - 'parent', - 'relevance', - 'slug', - 'include_slugs', - 'title', - ), - ); - - if ( 'page' === $this->post_type || post_type_supports( $this->post_type, 'page-attributes' ) ) { - $query_params['orderby']['enum'][] = 'menu_order'; - } - - $post_type = get_post_type_object( $this->post_type ); - - if ( $post_type->hierarchical || 'attachment' === $this->post_type ) { - $query_params['parent'] = array( - 'description' => __( 'Limit result set to items with particular parent IDs.' ), - 'type' => 'array', - 'items' => array( - 'type' => 'integer', - ), - 'default' => array(), - ); - $query_params['parent_exclude'] = array( - 'description' => __( 'Limit result set to all items except those of a particular parent ID.' ), - 'type' => 'array', - 'items' => array( - 'type' => 'integer', - ), - 'default' => array(), - ); - } - - $query_params['search_columns'] = array( - 'default' => array(), - 'description' => __( 'Array of column names to be searched.' ), - 'type' => 'array', - 'items' => array( - 'enum' => array( 'post_title', 'post_content', 'post_excerpt' ), - 'type' => 'string', - ), - ); - - $query_params['slug'] = array( - 'description' => __( 'Limit result set to posts with one or more specific slugs.' ), - 'type' => 'array', - 'items' => array( - 'type' => 'string', - ), - ); - - $query_params['status'] = array( - 'default' => 'publish', - 'description' => __( 'Limit result set to posts assigned one or more statuses.' ), - 'type' => 'array', - 'items' => array( - 'enum' => array_merge( array_keys( get_post_stati() ), array( 'any' ) ), - 'type' => 'string', - ), - 'sanitize_callback' => array( $this, 'sanitize_post_statuses' ), - ); - - $query_params = $this->prepare_taxonomy_limit_schema( $query_params ); - - if ( 'post' === $this->post_type ) { - $query_params['sticky'] = array( - 'description' => __( 'Limit result set to items that are sticky.' ), - 'type' => 'boolean', - ); - } - - if ( post_type_supports( $this->post_type, 'post-formats' ) ) { - $query_params['format'] = array( - 'description' => __( 'Limit result set to items assigned one or more given formats.' ), - 'type' => 'array', - 'uniqueItems' => true, - 'items' => array( - 'enum' => array_values( get_post_format_slugs() ), - 'type' => 'string', - ), - ); - } - - /** - * Filters collection parameters for the posts controller. - * - * The dynamic part of the filter `$this->post_type` refers to the post - * type slug for the controller. - * - * This filter registers the collection parameter, but does not map the - * collection parameter to an internal WP_Query parameter. Use the - * `rest_{$this->post_type}_query` filter to set WP_Query parameters. - * - * @since 4.7.0 - * - * @param array $query_params JSON Schema-formatted collection parameters. - * @param WP_Post_Type $post_type Post type object. - */ - return apply_filters( "rest_{$this->post_type}_collection_params", $query_params, $post_type ); - } - - /** - * Prepares the 'tax_query' for a collection of posts. - * - * @since 5.7.0 - * - * @param array $args WP_Query arguments. - * @param WP_REST_Request $request Full details about the request. - * @return array Updated query arguments. - */ - private function prepare_tax_query( array $args, WP_REST_Request $request ) { - $relation = $request['tax_relation']; - - if ( $relation ) { - $args['tax_query'] = array( 'relation' => $relation ); - } - - $taxonomies = wp_list_filter( - get_object_taxonomies( $this->post_type, 'objects' ), - array( 'show_in_rest' => true ) - ); - - foreach ( $taxonomies as $taxonomy ) { - $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; - - $tax_include = $request[ $base ]; - $tax_exclude = $request[ $base . '_exclude' ]; - - if ( $tax_include ) { - $terms = array(); - $include_children = false; - $operator = 'IN'; - - if ( rest_is_array( $tax_include ) ) { - $terms = $tax_include; - } elseif ( rest_is_object( $tax_include ) ) { - $terms = empty( $tax_include['terms'] ) ? array() : $tax_include['terms']; - $include_children = ! empty( $tax_include['include_children'] ); - - if ( isset( $tax_include['operator'] ) && 'AND' === $tax_include['operator'] ) { - $operator = 'AND'; - } - } - - if ( $terms ) { - $args['tax_query'][] = array( - 'taxonomy' => $taxonomy->name, - 'field' => 'term_id', - 'terms' => $terms, - 'include_children' => $include_children, - 'operator' => $operator, - ); - } - } - - if ( $tax_exclude ) { - $terms = array(); - $include_children = false; - - if ( rest_is_array( $tax_exclude ) ) { - $terms = $tax_exclude; - } elseif ( rest_is_object( $tax_exclude ) ) { - $terms = empty( $tax_exclude['terms'] ) ? array() : $tax_exclude['terms']; - $include_children = ! empty( $tax_exclude['include_children'] ); - } - - if ( $terms ) { - $args['tax_query'][] = array( - 'taxonomy' => $taxonomy->name, - 'field' => 'term_id', - 'terms' => $terms, - 'include_children' => $include_children, - 'operator' => 'NOT IN', - ); - } - } - } - - return $args; - } - - /** - * Prepares the collection schema for including and excluding items by terms. - * - * @since 5.7.0 - * - * @param array $query_params Collection schema. - * @return array Updated schema. - */ - private function prepare_taxonomy_limit_schema( array $query_params ) { - $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) ); - - if ( ! $taxonomies ) { - return $query_params; - } - - $query_params['tax_relation'] = array( - 'description' => __( 'Limit result set based on relationship between multiple taxonomies.' ), - 'type' => 'string', - 'enum' => array( 'AND', 'OR' ), - ); - - $limit_schema = array( - 'type' => array( 'object', 'array' ), - 'oneOf' => array( - array( - 'title' => __( 'Term ID List' ), - 'description' => __( 'Match terms with the listed IDs.' ), - 'type' => 'array', - 'items' => array( - 'type' => 'integer', - ), - ), - array( - 'title' => __( 'Term ID Taxonomy Query' ), - 'description' => __( 'Perform an advanced term query.' ), - 'type' => 'object', - 'properties' => array( - 'terms' => array( - 'description' => __( 'Term IDs.' ), - 'type' => 'array', - 'items' => array( - 'type' => 'integer', - ), - 'default' => array(), - ), - 'include_children' => array( - 'description' => __( 'Whether to include child terms in the terms limiting the result set.' ), - 'type' => 'boolean', - 'default' => false, - ), - ), - 'additionalProperties' => false, - ), - ), - ); - - $include_schema = array_merge( - array( - /* translators: %s: Taxonomy name. */ - 'description' => __( 'Limit result set to items with specific terms assigned in the %s taxonomy.' ), - ), - $limit_schema - ); - // 'operator' is supported only for 'include' queries. - $include_schema['oneOf'][1]['properties']['operator'] = array( - 'description' => __( 'Whether items must be assigned all or any of the specified terms.' ), - 'type' => 'string', - 'enum' => array( 'AND', 'OR' ), - 'default' => 'OR', - ); - - $exclude_schema = array_merge( - array( - /* translators: %s: Taxonomy name. */ - 'description' => __( 'Limit result set to items except those with specific terms assigned in the %s taxonomy.' ), - ), - $limit_schema - ); - - foreach ( $taxonomies as $taxonomy ) { - $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; - $base_exclude = $base . '_exclude'; - - $query_params[ $base ] = $include_schema; - $query_params[ $base ]['description'] = sprintf( $query_params[ $base ]['description'], $base ); - - $query_params[ $base_exclude ] = $exclude_schema; - $query_params[ $base_exclude ]['description'] = sprintf( $query_params[ $base_exclude ]['description'], $base ); - - if ( ! $taxonomy->hierarchical ) { - unset( $query_params[ $base ]['oneOf'][1]['properties']['include_children'] ); - unset( $query_params[ $base_exclude ]['oneOf'][1]['properties']['include_children'] ); - } - } - - return $query_params; - } -} diff --git a/lib/compat/wordpress-6.7/class-gutenberg-rest-server.php b/lib/compat/wordpress-6.7/class-gutenberg-rest-server.php deleted file mode 100644 index efdc41289dfac2..00000000000000 --- a/lib/compat/wordpress-6.7/class-gutenberg-rest-server.php +++ /dev/null @@ -1,185 +0,0 @@ -get_data(); - $links = static::get_compact_response_links( $response ); - - if ( ! empty( $links ) ) { - // Convert links to part of the data. - $data['_links'] = $links; - } - - if ( $embed ) { - $this->embed_cache = array(); - // Determine if this is a numeric array. - if ( wp_is_numeric_array( $data ) ) { - foreach ( $data as $key => $item ) { - $data[ $key ] = $this->embed_links( $item, $embed ); - } - } else { - $data = $this->embed_links( $data, $embed ); - } - $this->embed_cache = array(); - } - - return $data; - } - - /** - * Retrieves links from a response. - * - * Extracts the links from a response into a structured hash, suitable for - * direct output. - * - * @since 4.4.0 - * @since 6.7.0 The `targetHints` property to the `self` link object was added. - * - * @param WP_REST_Response $response Response to extract links from. - * @return array Map of link relation to list of link hashes. - */ - public static function get_response_links( $response ) { - $links = $response->get_links(); - - if ( empty( $links ) ) { - return array(); - } - - $server = rest_get_server(); - - // Convert links to part of the data. - $data = array(); - foreach ( $links as $rel => $items ) { - $data[ $rel ] = array(); - - foreach ( $items as $item ) { - $attributes = $item['attributes']; - $attributes['href'] = $item['href']; - - if ( 'self' !== $rel ) { - $data[ $rel ][] = $attributes; - continue; - } - - // Prefer targetHints that were specifically designated by the developer. - if ( isset( $attributes['targetHints']['allow'] ) ) { - $data[ $rel ][] = $attributes; - continue; - } - - $request = WP_REST_Request::from_url( $item['href'] ); - if ( ! $request ) { - $data[ $rel ][] = $attributes; - continue; - } - - $matched = $server->match_request_to_handler( $request ); - - if ( is_wp_error( $matched ) ) { - $data[ $rel ][] = $attributes; - continue; - } - - if ( is_wp_error( $request->has_valid_params() ) ) { - $data[ $rel ][] = $attributes; - continue; - } - - if ( is_wp_error( $request->sanitize_params() ) ) { - $data[ $rel ][] = $attributes; - continue; - } - - list( $route, $handler ) = $matched; - - $response = new WP_REST_Response(); - $response->set_matched_route( $route ); - $response->set_matched_handler( $handler ); - $headers = rest_send_allow_header( $response, $server, $request )->get_headers(); - - foreach ( $headers as $name => $value ) { - $name = WP_REST_Request::canonicalize_header_name( $name ); - $attributes['targetHints'][ $name ] = array_map( 'trim', explode( ',', $value ) ); - } - - $data[ $rel ][] = $attributes; - } - } - - return $data; - } - - /** - * Retrieves the CURIEs (compact URIs) used for relations. - * - * Extracts the links from a response into a structured hash, suitable for - * direct output. - * - * @since 4.5.0 - * - * @param WP_REST_Response $response Response to extract links from. - * @return array Map of link relation to list of link hashes. - */ - // @core-merge: Do not merge. The method is copied here to fix the inheritance issue. - public static function get_compact_response_links( $response ) { - $links = static::get_response_links( $response ); - - if ( empty( $links ) ) { - return array(); - } - - $curies = $response->get_curies(); - $used_curies = array(); - - foreach ( $links as $rel => $items ) { - - // Convert $rel URIs to their compact versions if they exist. - foreach ( $curies as $curie ) { - $href_prefix = substr( $curie['href'], 0, strpos( $curie['href'], '{rel}' ) ); - if ( ! str_starts_with( $rel, $href_prefix ) ) { - continue; - } - - // Relation now changes from '$uri' to '$curie:$relation'. - $rel_regex = str_replace( '\{rel\}', '(.+)', preg_quote( $curie['href'], '!' ) ); - preg_match( '!' . $rel_regex . '!', $rel, $matches ); - if ( $matches ) { - $new_rel = $curie['name'] . ':' . $matches[1]; - $used_curies[ $curie['name'] ] = $curie; - $links[ $new_rel ] = $items; - unset( $links[ $rel ] ); - break; - } - } - } - - // Push the curies onto the start of the links array. - if ( $used_curies ) { - $links['curies'] = array_values( $used_curies ); - } - - return $links; - } -} diff --git a/lib/compat/wordpress-6.7/class-gutenberg-rest-templates-controller-6-7.php b/lib/compat/wordpress-6.7/class-gutenberg-rest-templates-controller-6-7.php deleted file mode 100644 index e5f6eb126f2a6a..00000000000000 --- a/lib/compat/wordpress-6.7/class-gutenberg-rest-templates-controller-6-7.php +++ /dev/null @@ -1,203 +0,0 @@ -post_type ); - } else { - $template = get_block_template( $request['id'], $this->post_type ); - } - - if ( ! $template ) { - return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) ); - } - - return $this->prepare_item_for_response( $template, $request ); - } - - /** - * Prepare a single template output for response - * - * @param WP_Block_Template $item Template instance. - * @param WP_REST_Request $request Request object. - * @return WP_REST_Response Response object. - */ - // @core-merge: Fix wrong author in plugin templates. - public function prepare_item_for_response( $item, $request ) { - $template = $item; - - $fields = $this->get_fields_for_response( $request ); - - if ( 'plugin' !== $item->origin ) { - return parent::prepare_item_for_response( $item, $request ); - } - $cloned_item = clone $item; - // Set the origin as theme when calling the previous `prepare_item_for_response()` to prevent warnings when generating the author text. - $cloned_item->origin = 'theme'; - $response = parent::prepare_item_for_response( $cloned_item, $request ); - $data = $response->data; - - if ( rest_is_field_included( 'origin', $fields ) ) { - $data['origin'] = 'plugin'; - } - - if ( rest_is_field_included( 'plugin', $fields ) ) { - $registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $cloned_item->slug ); - if ( $registered_template ) { - $data['plugin'] = $registered_template->plugin; - } - } - - if ( rest_is_field_included( 'author_text', $fields ) ) { - $data['author_text'] = $this->get_wp_templates_author_text_field( $template ); - } - - if ( rest_is_field_included( 'original_source', $fields ) ) { - $data['original_source'] = $this->get_wp_templates_original_source_field( $template ); - } - - $response = rest_ensure_response( $data ); - - if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { - $links = $this->prepare_links( $template->id ); - $response->add_links( $links ); - if ( ! empty( $links['self']['href'] ) ) { - $actions = $this->get_available_actions(); - $self = $links['self']['href']; - foreach ( $actions as $rel ) { - $response->add_link( $rel, $self ); - } - } - } - - return $response; - } - - /** - * Returns the source from where the template originally comes from. - * - * @param WP_Block_Template $template_object Template instance. - * @return string Original source of the template one of theme, plugin, site, or user. - */ - // @core-merge: Changed the comments format (from inline to multi-line) in the entire function. - private static function get_wp_templates_original_source_field( $template_object ) { - if ( 'wp_template' === $template_object->type || 'wp_template_part' === $template_object->type ) { - /* - * Added by theme. - * Template originally provided by a theme, but customized by a user. - * Templates originally didn't have the 'origin' field so identify - * older customized templates by checking for no origin and a 'theme' - * or 'custom' source. - */ - if ( $template_object->has_theme_file && - ( 'theme' === $template_object->origin || ( - empty( $template_object->origin ) && in_array( - $template_object->source, - array( - 'theme', - 'custom', - ), - true - ) ) - ) - ) { - return 'theme'; - } - - // Added by plugin. - // @core-merge: Removed `$template_object->has_theme_file` check from this if clause. - if ( 'plugin' === $template_object->origin ) { - return 'plugin'; - } - - /* - * Added by site. - * Template was created from scratch, but has no author. Author support - * was only added to templates in WordPress 5.9. Fallback to showing the - * site logo and title. - */ - if ( empty( $template_object->has_theme_file ) && 'custom' === $template_object->source && empty( $template_object->author ) ) { - return 'site'; - } - } - - // Added by user. - return 'user'; - } - - /** - * Returns a human readable text for the author of the template. - * - * @param WP_Block_Template $template_object Template instance. - * @return string Human readable text for the author. - */ - private static function get_wp_templates_author_text_field( $template_object ) { - $original_source = self::get_wp_templates_original_source_field( $template_object ); - switch ( $original_source ) { - case 'theme': - $theme_name = wp_get_theme( $template_object->theme )->get( 'Name' ); - return empty( $theme_name ) ? $template_object->theme : $theme_name; - case 'plugin': - // @core-merge: Prioritize plugin name instead of theme name for plugin-registered templates. - if ( ! function_exists( 'get_plugins' ) || ! function_exists( 'get_plugin_data' ) ) { - require_once ABSPATH . 'wp-admin/includes/plugin.php'; - } - if ( isset( $template_object->plugin ) ) { - $plugins = wp_get_active_and_valid_plugins(); - - foreach ( $plugins as $plugin_file ) { - $plugin_basename = plugin_basename( $plugin_file ); - // Split basename by '/' to get the plugin slug. - list( $plugin_slug, ) = explode( '/', $plugin_basename ); - - if ( $plugin_slug === $template_object->plugin ) { - $plugin_data = get_plugin_data( $plugin_file ); - - if ( ! empty( $plugin_data['Name'] ) ) { - return $plugin_data['Name']; - } - - break; - } - } - } - - /* - * Fall back to the theme name if the plugin is not defined. That's needed to keep backwards - * compatibility with templates that were registered before the plugin attribute was added. - */ - $plugins = get_plugins(); - $plugin_basename = plugin_basename( sanitize_text_field( $template_object->theme . '.php' ) ); - if ( isset( $plugins[ $plugin_basename ] ) && isset( $plugins[ $plugin_basename ]['Name'] ) ) { - return $plugins[ $plugin_basename ]['Name']; - } - return isset( $template_object->plugin ) ? - $template_object->plugin : - $template_object->theme; - // @core-merge: End of changes to merge in core. - case 'site': - return get_bloginfo( 'name' ); - case 'user': - $author = get_user_by( 'id', $template_object->author ); - if ( ! $author ) { - return __( 'Unknown author' ); - } - return $author->get( 'display_name' ); - } - } -} diff --git a/lib/compat/wordpress-6.7/class-gutenberg-token-map-6-7.php b/lib/compat/wordpress-6.7/class-gutenberg-token-map-6-7.php deleted file mode 100644 index a2142171ddc14f..00000000000000 --- a/lib/compat/wordpress-6.7/class-gutenberg-token-map-6-7.php +++ /dev/null @@ -1,820 +0,0 @@ - '😯', - * ':(' => 'πŸ™', - * ':)' => 'πŸ™‚', - * ':?' => 'πŸ˜•', - * ) ); - * - * true === $smilies->contains( ':)' ); - * false === $smilies->contains( 'simile' ); - * - * 'πŸ˜•' === $smilies->read_token( 'Not sure :?.', 9, $length_of_smily_syntax ); - * 2 === $length_of_smily_syntax; - * - * ## Precomputing the Token Map. - * - * Creating the class involves some work sorting and organizing the tokens and their - * replacement values. In order to skip this, it's possible for the class to export - * its state and be used as actual PHP source code. - * - * Example: - * - * // Export with four spaces as the indent, only for the sake of this docblock. - * // The default indent is a tab character. - * $indent = ' '; - * echo $smilies->precomputed_php_source_table( $indent ); - * - * // Output, to be pasted into a PHP source file: - * WP_Token_Map::from_precomputed_table( - * array( - * "storage_version" => "6.6.0", - * "key_length" => 2, - * "groups" => "", - * "long_words" => array(), - * "small_words" => "8O\x00:)\x00:(\x00:?\x00", - * "small_mappings" => array( "😯", "πŸ™‚", "πŸ™", "πŸ˜•" ) - * ) - * ); - * - * ## Large vs. small words. - * - * This class uses a short prefix called the "key" to optimize lookup of its tokens. - * This means that some tokens may be shorter than or equal in length to that key. - * Those words that are longer than the key are called "large" while those shorter - * than or equal to the key length are called "small." - * - * This separation of large and small words is incidental to the way this class - * optimizes lookup, and should be considered an internal implementation detail - * of the class. It may still be important to be aware of it, however. - * - * ## Determining Key Length. - * - * The choice of the size of the key length should be based on the data being stored in - * the token map. It should divide the data as evenly as possible, but should not create - * so many groups that a large fraction of the groups only contain a single token. - * - * For the HTML5 named character references, a key length of 2 was found to provide a - * sufficient spread and should be a good default for relatively large sets of tokens. - * - * However, for some data sets this might be too long. For example, a list of smilies - * may be too small for a key length of 2. Perhaps 1 would be more appropriate. It's - * best to experiment and determine empirically which values are appropriate. - * - * ## Generate Pre-Computed Source Code. - * - * Since the `WP_Token_Map` is designed for relatively static lookups, it can be - * advantageous to precompute the values and instantiate a table that has already - * sorted and grouped the tokens and built the lookup strings. - * - * This can be done with `WP_Token_Map::precomputed_php_source_table()`. - * - * Note that if there is a leading character that all tokens need, such as `&` for - * HTML named character references, it can be beneficial to exclude this from the - * token map. Instead, find occurrences of the leading character and then use the - * token map to see if the following characters complete the token. - * - * Example: - * - * $map = WP_Token_Map::from_array( array( 'simple_smile:' => 'πŸ™‚', 'sob:' => '😭', 'soba:' => '🍜' ) ); - * echo $map->precomputed_php_source_table(); - * // Output - * WP_Token_Map::from_precomputed_table( - * array( - * "storage_version" => "6.6.0", - * "key_length" => 2, - * "groups" => "si\x00so\x00", - * "long_words" => array( - * // simple_smile:[πŸ™‚]. - * "\x0bmple_smile:\x04πŸ™‚", - * // soba:[🍜] sob:[😭]. - * "\x03ba:\x04🍜\x02b:\x04😭", - * ), - * "short_words" => "", - * "short_mappings" => array() - * } - * ); - * - * This precomputed value can be stored directly in source code and will skip the - * startup cost of generating the lookup strings. See `$html5_named_character_entities`. - * - * Note that any updates to the precomputed format should update the storage version - * constant. It would also be best to provide an update function to take older known - * versions and upgrade them in place when loading into `from_precomputed_table()`. - * - * ## Future Direction. - * - * It may be viable to dynamically increase the length limits such that there's no need to impose them. - * The limit appears because of the packing structure, which indicates how many bytes each segment of - * text in the lookup tables spans. If, however, care were taken to track the longest word length, then - * the packing structure could change its representation to allow for that. Each additional byte storing - * length, however, increases the memory overhead and lookup runtime. - * - * An alternative approach could be to borrow the UTF-8 variable-length encoding and store lengths of less - * than 127 as a single byte with the high bit unset, storing longer lengths as the combination of - * continuation bytes. - * - * Since it has not been shown during the development of this class that longer strings are required, this - * update is deferred until such a need is clear. - * - * @since 6.6.0 - */ -class Gutenberg_Token_Map_6_7 { - /** - * Denotes the version of the code which produces pre-computed source tables. - * - * This version will be used not only to verify pre-computed data, but also - * to upgrade pre-computed data from older versions. Choosing a name that - * corresponds to the WordPress release will help people identify where an - * old copy of data came from. - */ - const STORAGE_VERSION = '6.6.0-trunk'; - - /** - * Maximum length for each key and each transformed value in the table (in bytes). - * - * @since 6.6.0 - */ - const MAX_LENGTH = 256; - - /** - * How many bytes of each key are used to form a group key for lookup. - * This also determines whether a word is considered short or long. - * - * @since 6.6.0 - * - * @var int - */ - private $key_length = 2; - - /** - * Stores an optimized form of the word set, where words are grouped - * by a prefix of the `$key_length` and then collapsed into a string. - * - * In each group, the keys and lookups form a packed data structure. - * The keys in the string are stripped of their "group key," which is - * the prefix of length `$this->key_length` shared by all of the items - * in the group. Each word in the string is prefixed by a single byte - * whose raw unsigned integer value represents how many bytes follow. - * - * β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β” - * β”‚ Length of rest β”‚ Rest of key β”‚ Length of value β”‚ Value β”‚ - * β”‚ of key (bytes) β”‚ β”‚ (bytes) β”‚ β”‚ - * β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€ - * β”‚ 0x08 β”‚ nterDot; β”‚ 0x02 β”‚ Β· β”‚ - * β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜ - * - * In this example, the key `CenterDot;` has a group key `Ce`, leaving - * eight bytes for the rest of the key, `nterDot;`, and two bytes for - * the transformed value `Β·` (or U+B7 or "\xC2\xB7"). - * - * Example: - * - * // Stores array( 'CenterDot;' => 'Β·', 'Cedilla;' => 'ΒΈ' ). - * $groups = "Ce\x00"; - * $large_words = array( "\x08nterDot;\x02Β·\x06dilla;\x02ΒΈ" ) - * - * The prefixes appear in the `$groups` string, each followed by a null - * byte. This makes for quick lookup of where in the group string the key - * is found, and then a simple division converts that offset into the index - * in the `$large_words` array where the group string is to be found. - * - * This lookup data structure is designed to optimize cache locality and - * minimize indirect memory reads when matching strings in the set. - * - * @since 6.6.0 - * - * @var array - */ - private $large_words = array(); - - /** - * Stores the group keys for sequential string lookup. - * - * The offset into this string where the group key appears corresponds with the index - * into the group array where the rest of the group string appears. This is an optimization - * to improve cache locality while searching and minimize indirect memory accesses. - * - * @since 6.6.0 - * - * @var string - */ - private $groups = ''; - - /** - * Stores an optimized row of small words, where every entry is - * `$this->key_size + 1` bytes long and zero-extended. - * - * This packing allows for direct lookup of a short word followed - * by the null byte, if extended to `$this->key_size + 1`. - * - * Example: - * - * // Stores array( 'GT', 'LT', 'gt', 'lt' ). - * "GT\x00LT\x00gt\x00lt\x00" - * - * @since 6.6.0 - * - * @var string - */ - private $small_words = ''; - - /** - * Replacements for the small words, in the same order they appear. - * - * With the position of a small word it's possible to index the translation - * directly, as its position in the `$small_words` string corresponds to - * the index of the replacement in the `$small_mapping` array. - * - * Example: - * - * array( '>', '<', '>', '<' ) - * - * @since 6.6.0 - * - * @var string[] - */ - private $small_mappings = array(); - - /** - * Create a token map using an associative array of key/value pairs as the input. - * - * Example: - * - * $smilies = WP_Token_Map::from_array( array( - * '8O' => '😯', - * ':(' => 'πŸ™', - * ':)' => 'πŸ™‚', - * ':?' => 'πŸ˜•', - * ) ); - * - * @since 6.6.0 - * - * @param array $mappings The keys transform into the values, both are strings. - * @param int $key_length Determines the group key length. Leave at the default value - * of 2 unless there's an empirical reason to change it. - * - * @return WP_Token_Map|null Token map, unless unable to create it. - */ - public static function from_array( array $mappings, int $key_length = 2 ): ?WP_Token_Map { - $map = new WP_Token_Map(); - $map->key_length = $key_length; - - // Start by grouping words. - - $groups = array(); - $shorts = array(); - foreach ( $mappings as $word => $mapping ) { - if ( - self::MAX_LENGTH <= strlen( $word ) || - self::MAX_LENGTH <= strlen( $mapping ) - ) { - _doing_it_wrong( - __METHOD__, - sprintf( - /* translators: 1: maximum byte length (a count) */ - __( 'Token Map tokens and substitutions must all be shorter than %1$d bytes.' ), - self::MAX_LENGTH - ), - '6.6.0' - ); - return null; - } - - $length = strlen( $word ); - - if ( $key_length >= $length ) { - $shorts[] = $word; - } else { - $group = substr( $word, 0, $key_length ); - - if ( ! isset( $groups[ $group ] ) ) { - $groups[ $group ] = array(); - } - - $groups[ $group ][] = array( substr( $word, $key_length ), $mapping ); - } - } - - /* - * Sort the words to ensure that no smaller substring of a match masks the full match. - * For example, `Cap` should not match before `CapitalDifferentialD`. - */ - usort( $shorts, 'WP_Token_Map::longest_first_then_alphabetical' ); - foreach ( $groups as $group_key => $group ) { - usort( - $groups[ $group_key ], - static function ( array $a, array $b ): int { - return self::longest_first_then_alphabetical( $a[0], $b[0] ); - } - ); - } - - // Finally construct the optimized lookups. - - foreach ( $shorts as $word ) { - $map->small_words .= str_pad( $word, $key_length + 1, "\x00", STR_PAD_RIGHT ); - $map->small_mappings[] = $mappings[ $word ]; - } - - $group_keys = array_keys( $groups ); - sort( $group_keys ); - - foreach ( $group_keys as $group ) { - $map->groups .= "{$group}\x00"; - - $group_string = ''; - - foreach ( $groups[ $group ] as $group_word ) { - list( $word, $mapping ) = $group_word; - - $word_length = pack( 'C', strlen( $word ) ); - $mapping_length = pack( 'C', strlen( $mapping ) ); - $group_string .= "{$word_length}{$word}{$mapping_length}{$mapping}"; - } - - $map->large_words[] = $group_string; - } - - return $map; - } - - /** - * Creates a token map from a pre-computed table. - * This skips the initialization cost of generating the table. - * - * This function should only be used to load data created with - * WP_Token_Map::precomputed_php_source_tag(). - * - * @since 6.6.0 - * - * @param array $state { - * Stores pre-computed state for directly loading into a Token Map. - * - * @type string $storage_version Which version of the code produced this state. - * @type int $key_length Group key length. - * @type string $groups Group lookup index. - * @type array $large_words Large word groups and packed strings. - * @type string $small_words Small words packed string. - * @type array $small_mappings Small word mappings. - * } - * - * @return WP_Token_Map Map with precomputed data loaded. - */ - public static function from_precomputed_table( $state ): ?WP_Token_Map { - $has_necessary_state = isset( - $state['storage_version'], - $state['key_length'], - $state['groups'], - $state['large_words'], - $state['small_words'], - $state['small_mappings'] - ); - - if ( ! $has_necessary_state ) { - _doing_it_wrong( - __METHOD__, - __( 'Missing required inputs to pre-computed WP_Token_Map.' ), - '6.6.0' - ); - return null; - } - - if ( self::STORAGE_VERSION !== $state['storage_version'] ) { - _doing_it_wrong( - __METHOD__, - /* translators: 1: version string, 2: version string. */ - sprintf( __( 'Loaded version \'%1$s\' incompatible with expected version \'%2$s\'.' ), $state['storage_version'], self::STORAGE_VERSION ), - '6.6.0' - ); - return null; - } - - $map = new WP_Token_Map(); - - $map->key_length = $state['key_length']; - $map->groups = $state['groups']; - $map->large_words = $state['large_words']; - $map->small_words = $state['small_words']; - $map->small_mappings = $state['small_mappings']; - - return $map; - } - - /** - * Indicates if a given word is a lookup key in the map. - * - * Example: - * - * true === $smilies->contains( ':)' ); - * false === $smilies->contains( 'simile' ); - * - * @since 6.6.0 - * - * @param string $word Determine if this word is a lookup key in the map. - * @param string $case_sensitivity Optional. Pass 'ascii-case-insensitive' to ignore ASCII case when matching. Default 'case-sensitive'. - * @return bool Whether there's an entry for the given word in the map. - */ - public function contains( string $word, string $case_sensitivity = 'case-sensitive' ): bool { - $ignore_case = 'ascii-case-insensitive' === $case_sensitivity; - - if ( $this->key_length >= strlen( $word ) ) { - if ( 0 === strlen( $this->small_words ) ) { - return false; - } - - $term = str_pad( $word, $this->key_length + 1, "\x00", STR_PAD_RIGHT ); - $word_at = $ignore_case ? stripos( $this->small_words, $term ) : strpos( $this->small_words, $term ); - if ( false === $word_at ) { - return false; - } - - return true; - } - - $group_key = substr( $word, 0, $this->key_length ); - $group_at = $ignore_case ? stripos( $this->groups, $group_key ) : strpos( $this->groups, $group_key ); - if ( false === $group_at ) { - return false; - } - $group = $this->large_words[ $group_at / ( $this->key_length + 1 ) ]; - $group_length = strlen( $group ); - $slug = substr( $word, $this->key_length ); - $length = strlen( $slug ); - $at = 0; - - while ( $at < $group_length ) { - $token_length = unpack( 'C', $group[ $at++ ] )[1]; - $token_at = $at; - $at += $token_length; - $mapping_length = unpack( 'C', $group[ $at++ ] )[1]; - $mapping_at = $at; - - if ( $token_length === $length && 0 === substr_compare( $group, $slug, $token_at, $token_length, $ignore_case ) ) { - return true; - } - - $at = $mapping_at + $mapping_length; - } - - return false; - } - - /** - * If the text starting at a given offset is a lookup key in the map, - * return the corresponding transformation from the map, else `false`. - * - * This function returns the translated string, but accepts an optional - * parameter `$matched_token_byte_length`, which communicates how many - * bytes long the lookup key was, if it found one. This can be used to - * advance a cursor in calling code if a lookup key was found. - * - * Example: - * - * false === $smilies->read_token( 'Not sure :?.', 0, $token_byte_length ); - * 'πŸ˜•' === $smilies->read_token( 'Not sure :?.', 9, $token_byte_length ); - * 2 === $token_byte_length; - * - * Example: - * - * while ( $at < strlen( $input ) ) { - * $next_at = strpos( $input, ':', $at ); - * if ( false === $next_at ) { - * break; - * } - * - * $smily = $smilies->read_token( $input, $next_at, $token_byte_length ); - * if ( false === $next_at ) { - * ++$at; - * continue; - * } - * - * $prefix = substr( $input, $at, $next_at - $at ); - * $at += $token_byte_length; - * $output .= "{$prefix}{$smily}"; - * } - * - * @since 6.6.0 - * - * @param string $text String in which to search for a lookup key. - * @param int $offset Optional. How many bytes into the string where the lookup key ought to start. Default 0. - * @param int|null &$matched_token_byte_length Optional. Holds byte-length of found token matched, otherwise not set. Default null. - * @param string $case_sensitivity Optional. Pass 'ascii-case-insensitive' to ignore ASCII case when matching. Default 'case-sensitive'. - * - * @return string|null Mapped value of lookup key if found, otherwise `null`. - */ - public function read_token( string $text, int $offset = 0, &$matched_token_byte_length = null, $case_sensitivity = 'case-sensitive' ): ?string { - $ignore_case = 'ascii-case-insensitive' === $case_sensitivity; - $text_length = strlen( $text ); - - // Search for a long word first, if the text is long enough, and if that fails, a short one. - if ( $text_length > $this->key_length ) { - $group_key = substr( $text, $offset, $this->key_length ); - - $group_at = $ignore_case ? stripos( $this->groups, $group_key ) : strpos( $this->groups, $group_key ); - if ( false === $group_at ) { - // Perhaps a short word then. - return strlen( $this->small_words ) > 0 - ? $this->read_small_token( $text, $offset, $matched_token_byte_length, $case_sensitivity ) - : null; - } - - $group = $this->large_words[ $group_at / ( $this->key_length + 1 ) ]; - $group_length = strlen( $group ); - $at = 0; - while ( $at < $group_length ) { - $token_length = unpack( 'C', $group[ $at++ ] )[1]; - $token = substr( $group, $at, $token_length ); - $at += $token_length; - $mapping_length = unpack( 'C', $group[ $at++ ] )[1]; - $mapping_at = $at; - - if ( 0 === substr_compare( $text, $token, $offset + $this->key_length, $token_length, $ignore_case ) ) { - $matched_token_byte_length = $this->key_length + $token_length; - return substr( $group, $mapping_at, $mapping_length ); - } - - $at = $mapping_at + $mapping_length; - } - } - - // Perhaps a short word then. - return strlen( $this->small_words ) > 0 - ? $this->read_small_token( $text, $offset, $matched_token_byte_length, $case_sensitivity ) - : null; - } - - /** - * Finds a match for a short word at the index. - * - * @since 6.6.0 - * - * @param string $text String in which to search for a lookup key. - * @param int $offset Optional. How many bytes into the string where the lookup key ought to start. Default 0. - * @param int|null &$matched_token_byte_length Optional. Holds byte-length of found lookup key if matched, otherwise not set. Default null. - * @param string $case_sensitivity Optional. Pass 'ascii-case-insensitive' to ignore ASCII case when matching. Default 'case-sensitive'. - * - * @return string|null Mapped value of lookup key if found, otherwise `null`. - */ - private function read_small_token( string $text, int $offset = 0, &$matched_token_byte_length = null, $case_sensitivity = 'case-sensitive' ): ?string { - $ignore_case = 'ascii-case-insensitive' === $case_sensitivity; - $small_length = strlen( $this->small_words ); - $search_text = substr( $text, $offset, $this->key_length ); - if ( $ignore_case ) { - $search_text = strtoupper( $search_text ); - } - $starting_char = $search_text[0]; - - $at = 0; - while ( $at < $small_length ) { - if ( - $starting_char !== $this->small_words[ $at ] && - ( ! $ignore_case || strtoupper( $this->small_words[ $at ] ) !== $starting_char ) - ) { - $at += $this->key_length + 1; - continue; - } - - for ( $adjust = 1; $adjust < $this->key_length; $adjust++ ) { - if ( "\x00" === $this->small_words[ $at + $adjust ] ) { - $matched_token_byte_length = $adjust; - return $this->small_mappings[ $at / ( $this->key_length + 1 ) ]; - } - - if ( - $search_text[ $adjust ] !== $this->small_words[ $at + $adjust ] && - ( ! $ignore_case || strtoupper( $this->small_words[ $at + $adjust ] !== $search_text[ $adjust ] ) ) - ) { - $at += $this->key_length + 1; - continue 2; - } - } - - $matched_token_byte_length = $adjust; - return $this->small_mappings[ $at / ( $this->key_length + 1 ) ]; - } - - return null; - } - - /** - * Exports the token map into an associate array of key/value pairs. - * - * Example: - * - * $smilies->to_array() === array( - * '8O' => '😯', - * ':(' => 'πŸ™', - * ':)' => 'πŸ™‚', - * ':?' => 'πŸ˜•', - * ); - * - * @return array The lookup key/substitution values as an associate array. - */ - public function to_array(): array { - $tokens = array(); - - $at = 0; - $small_mapping = 0; - $small_length = strlen( $this->small_words ); - while ( $at < $small_length ) { - $key = rtrim( substr( $this->small_words, $at, $this->key_length + 1 ), "\x00" ); - $value = $this->small_mappings[ $small_mapping++ ]; - $tokens[ $key ] = $value; - - $at += $this->key_length + 1; - } - - foreach ( $this->large_words as $index => $group ) { - $prefix = substr( $this->groups, $index * ( $this->key_length + 1 ), 2 ); - $group_length = strlen( $group ); - $at = 0; - while ( $at < $group_length ) { - $length = unpack( 'C', $group[ $at++ ] )[1]; - $key = $prefix . substr( $group, $at, $length ); - - $at += $length; - $length = unpack( 'C', $group[ $at++ ] )[1]; - $value = substr( $group, $at, $length ); - - $tokens[ $key ] = $value; - $at += $length; - } - } - - return $tokens; - } - - /** - * Export the token map for quick loading in PHP source code. - * - * This function has a specific purpose, to make loading of static token maps fast. - * It's used to ensure that the HTML character reference lookups add a minimal cost - * to initializing the PHP process. - * - * Example: - * - * echo $smilies->precomputed_php_source_table(); - * - * // Output. - * WP_Token_Map::from_precomputed_table( - * array( - * "storage_version" => "6.6.0", - * "key_length" => 2, - * "groups" => "", - * "long_words" => array(), - * "small_words" => "8O\x00:)\x00:(\x00:?\x00", - * "small_mappings" => array( "😯", "πŸ™‚", "πŸ™", "πŸ˜•" ) - * ) - * ); - * - * @since 6.6.0 - * - * @param string $indent Optional. Use this string for indentation, or rely on the default horizontal tab character. Default "\t". - * @return string Value which can be pasted into a PHP source file for quick loading of table. - */ - public function precomputed_php_source_table( string $indent = "\t" ): string { - $i1 = $indent; - $i2 = $i1 . $indent; - $i3 = $i2 . $indent; - - $class_version = self::STORAGE_VERSION; - - $output = self::class . "::from_precomputed_table(\n"; - $output .= "{$i1}array(\n"; - $output .= "{$i2}\"storage_version\" => \"{$class_version}\",\n"; - $output .= "{$i2}\"key_length\" => {$this->key_length},\n"; - - $group_line = str_replace( "\x00", "\\x00", $this->groups ); - $output .= "{$i2}\"groups\" => \"{$group_line}\",\n"; - - $output .= "{$i2}\"large_words\" => array(\n"; - - $prefixes = explode( "\x00", $this->groups ); - foreach ( $prefixes as $index => $prefix ) { - if ( '' === $prefix ) { - break; - } - $group = $this->large_words[ $index ]; - $group_length = strlen( $group ); - $comment_line = "{$i3}//"; - $data_line = "{$i3}\""; - $at = 0; - while ( $at < $group_length ) { - $token_length = unpack( 'C', $group[ $at++ ] )[1]; - $token = substr( $group, $at, $token_length ); - $at += $token_length; - $mapping_length = unpack( 'C', $group[ $at++ ] )[1]; - $mapping = substr( $group, $at, $mapping_length ); - $at += $mapping_length; - - $token_digits = str_pad( dechex( $token_length ), 2, '0', STR_PAD_LEFT ); - $mapping_digits = str_pad( dechex( $mapping_length ), 2, '0', STR_PAD_LEFT ); - - $mapping = preg_replace_callback( - "~[\\x00-\\x1f\\x22\\x5c]~", - static function ( $match_result ) { - switch ( $match_result[0] ) { - case '"': - return '\\"'; - - case '\\': - return '\\\\'; - - default: - $hex = dechex( ord( $match_result[0] ) ); - return "\\x{$hex}"; - } - }, - $mapping - ); - - $comment_line .= " {$prefix}{$token}[{$mapping}]"; - $data_line .= "\\x{$token_digits}{$token}\\x{$mapping_digits}{$mapping}"; - } - $comment_line .= ".\n"; - $data_line .= "\",\n"; - - $output .= $comment_line; - $output .= $data_line; - } - - $output .= "{$i2}),\n"; - - $small_words = array(); - $small_length = strlen( $this->small_words ); - $at = 0; - while ( $at < $small_length ) { - $small_words[] = substr( $this->small_words, $at, $this->key_length + 1 ); - $at += $this->key_length + 1; - } - - $small_text = str_replace( "\x00", '\x00', implode( '', $small_words ) ); - $output .= "{$i2}\"small_words\" => \"{$small_text}\",\n"; - - $output .= "{$i2}\"small_mappings\" => array(\n"; - foreach ( $this->small_mappings as $mapping ) { - $output .= "{$i3}\"{$mapping}\",\n"; - } - $output .= "{$i2})\n"; - $output .= "{$i1})\n"; - $output .= ')'; - - return $output; - } - - /** - * Compares two strings, returning the longest, or whichever - * is first alphabetically if they are the same length. - * - * This is an important sort when building the token map because - * it should not form a match on a substring of a longer potential - * match. For example, it should not detect `Cap` when matching - * against the string `CapitalDifferentialD`. - * - * @since 6.6.0 - * - * @param string $a First string to compare. - * @param string $b Second string to compare. - * @return int -1 or lower if `$a` is less than `$b`; 1 or greater if `$a` is greater than `$b`, and 0 if they are equal. - */ - private static function longest_first_then_alphabetical( string $a, string $b ): int { - if ( $a === $b ) { - return 0; - } - - $length_a = strlen( $a ); - $length_b = strlen( $b ); - - // Longer strings are less-than for comparison's sake. - if ( $length_a !== $length_b ) { - return $length_b - $length_a; - } - - return strcmp( $a, $b ); - } -} diff --git a/lib/compat/wordpress-6.7/class-wp-block-templates-registry.php b/lib/compat/wordpress-6.7/class-wp-block-templates-registry.php deleted file mode 100644 index 92673c0bf50f90..00000000000000 --- a/lib/compat/wordpress-6.7/class-wp-block-templates-registry.php +++ /dev/null @@ -1,256 +0,0 @@ - $instance` pairs. - * - * @since 6.7.0 - * @var WP_Block_Template[] $registered_block_templates Registered templates. - */ - private $registered_templates = array(); - - /** - * Container for the main instance of the class. - * - * @since 6.7.0 - * @var WP_Block_Templates_Registry|null - */ - private static $instance = null; - - /** - * Registers a template. - * - * @since 6.7.0 - * - * @param string $template_name Template name including namespace. - * @param array $args Optional. Array of template arguments. - * @return WP_Block_Template|WP_Error The registered template on success, or WP_Error on failure. - */ - public function register( $template_name, $args = array() ) { - - $template = null; - - $error_message = ''; - $error_code = ''; - if ( ! is_string( $template_name ) ) { - $error_message = __( 'Template names must be strings.', 'gutenberg' ); - $error_code = 'template_name_no_string'; - } elseif ( preg_match( '/[A-Z]+/', $template_name ) ) { - $error_message = __( 'Template names must not contain uppercase characters.', 'gutenberg' ); - $error_code = 'template_name_no_uppercase'; - } elseif ( ! preg_match( '/^[a-z0-9-]+\/\/[a-z0-9-]+$/', $template_name ) ) { - $error_message = __( 'Template names must contain a namespace prefix. Example: my-plugin//my-custom-template', 'gutenberg' ); - $error_code = 'template_no_prefix'; - } elseif ( $this->is_registered( $template_name ) ) { - /* translators: %s: Template name. */ - $error_message = sprintf( __( 'Template "%s" is already registered.', 'gutenberg' ), $template_name ); - $error_code = 'template_already_registered'; - } - - if ( $error_message ) { - _doing_it_wrong( - __METHOD__, - $error_message, - '6.7.0' - ); - return new WP_Error( $error_code, $error_message ); - } - - if ( ! $template ) { - $theme_name = get_stylesheet(); - list( $plugin, $slug ) = explode( '//', $template_name ); - $default_template_types = get_default_block_template_types(); - - $template = new WP_Block_Template(); - $template->id = $theme_name . '//' . $slug; - $template->theme = $theme_name; - $template->plugin = $plugin; - $template->author = null; - $template->content = isset( $args['content'] ) ? $args['content'] : ''; - $template->source = 'plugin'; - $template->slug = $slug; - $template->type = 'wp_template'; - $template->title = isset( $args['title'] ) ? $args['title'] : $template_name; - $template->description = isset( $args['description'] ) ? $args['description'] : ''; - $template->status = 'publish'; - $template->origin = 'plugin'; - $template->is_custom = ! isset( $default_template_types[ $template_name ] ); - $template->post_types = isset( $args['post_types'] ) ? $args['post_types'] : array(); - } - - $this->registered_templates[ $template_name ] = $template; - - return $template; - } - - /** - * Retrieves all registered templates. - * - * @since 6.7.0 - * - * @return WP_Block_Template[] Associative array of `$template_name => $template` pairs. - */ - public function get_all_registered() { - return $this->registered_templates; - } - - /** - * Retrieves a registered template by its name. - * - * @since 6.7.0 - * - * @param string $template_name Template name including namespace. - * @return WP_Block_Template|null The registered template, or null if it is not registered. - */ - public function get_registered( $template_name ) { - if ( ! $this->is_registered( $template_name ) ) { - return null; - } - - return $this->registered_templates[ $template_name ]; - } - - /** - * Retrieves a registered template by its slug. - * - * @since 6.7.0 - * - * @param string $template_slug Slug of the template. - * @return WP_Block_Template|null The registered template, or null if it is not registered. - */ - public function get_by_slug( $template_slug ) { - $all_templates = $this->get_all_registered(); - - if ( ! $all_templates ) { - return null; - } - - foreach ( $all_templates as $template ) { - if ( $template->slug === $template_slug ) { - return $template; - } - } - - return null; - } - - /** - * Retrieves registered templates matching a query. - * - * @since 6.7.0 - * - * @param array $query { - * Arguments to retrieve templates. Optional, empty by default. - * - * @type string[] $slug__in List of slugs to include. - * @type string[] $slug__not_in List of slugs to skip. - * @type string $post_type Post type to get the templates for. - * } - */ - public function get_by_query( $query = array() ) { - $all_templates = $this->get_all_registered(); - - if ( ! $all_templates ) { - return array(); - } - - $query = wp_parse_args( - $query, - array( - 'slug__in' => array(), - 'slug__not_in' => array(), - 'post_type' => '', - ) - ); - $slugs_to_include = $query['slug__in']; - $slugs_to_skip = $query['slug__not_in']; - $post_type = $query['post_type']; - - $matching_templates = array(); - foreach ( $all_templates as $template_name => $template ) { - if ( $slugs_to_include && ! in_array( $template->slug, $slugs_to_include, true ) ) { - continue; - } - - if ( $slugs_to_skip && in_array( $template->slug, $slugs_to_skip, true ) ) { - continue; - } - - if ( $post_type && ! in_array( $post_type, $template->post_types, true ) ) { - continue; - } - - $matching_templates[ $template_name ] = $template; - } - - return $matching_templates; - } - - /** - * Checks if a template is registered. - * - * @since 6.7.0 - * - * @param string $template_name Template name. - * @return bool True if the template is registered, false otherwise. - */ - public function is_registered( $template_name ) { - return isset( $this->registered_templates[ $template_name ] ); - } - - /** - * Unregisters a template. - * - * @since 6.7.0 - * - * @param string $template_name Template name including namespace. - * @return WP_Block_Template|WP_Error The unregistered template on success, or WP_Error on failure. - */ - public function unregister( $template_name ) { - if ( ! $this->is_registered( $template_name ) ) { - _doing_it_wrong( - __METHOD__, - /* translators: %s: Template name. */ - sprintf( __( 'Template "%s" is not registered.', 'gutenberg' ), $template_name ), - '6.7.0' - ); - /* translators: %s: Template name. */ - return new WP_Error( 'template_not_registered', __( 'Template "%s" is not registered.', 'gutenberg' ) ); - } - - $unregistered_template = $this->registered_templates[ $template_name ]; - unset( $this->registered_templates[ $template_name ] ); - - return $unregistered_template; - } - - /** - * Utility method to retrieve the main instance of the class. - * - * The instance will be created if it does not exist yet. - * - * @since 6.7.0 - * - * @return WP_Block_Templates_Registry The main instance. - */ - public static function get_instance() { - if ( null === self::$instance ) { - self::$instance = new self(); - } - - return self::$instance; - } - } -} diff --git a/lib/compat/wordpress-6.7/compat.php b/lib/compat/wordpress-6.7/compat.php deleted file mode 100644 index 98ea34c813ae43..00000000000000 --- a/lib/compat/wordpress-6.7/compat.php +++ /dev/null @@ -1,126 +0,0 @@ - $value ) { - $registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $query_result[ $key ]->slug ); - if ( $registered_template ) { - $query_result[ $key ]->plugin = $registered_template->plugin; - $query_result[ $key ]->origin = - 'theme' !== $query_result[ $key ]->origin && 'theme' !== $query_result[ $key ]->source ? - 'plugin' : - $query_result[ $key ]->origin; - $query_result[ $key ]->title = - empty( $query_result[ $key ]->title ) || $query_result[ $key ]->title === $query_result[ $key ]->slug ? - $registered_template->title : $query_result[ $key ]->title; - $query_result[ $key ]->description = empty( $query_result[ $key ]->description ) ? $registered_template->description : $query_result[ $key ]->description; - } - } - - if ( ! isset( $query['wp_id'] ) ) { - // We need to unset the post_type query param because some templates - // would be excluded otherwise, like `page.html` when looking for - // `page` templates. - // See: https://github.com/WordPress/gutenberg/issues/65584 - $template_files_query = $query; - unset( $template_files_query['post_type'] ); - $template_files = _get_block_templates_files( $template_type, $template_files_query ); - - /* - * Add templates registered in the template registry. Filtering out the ones which have a theme file. - */ - $registered_templates = WP_Block_Templates_Registry::get_instance()->get_by_query( $query ); - $matching_registered_templates = array_filter( - $registered_templates, - function ( $registered_template ) use ( $template_files ) { - foreach ( $template_files as $template_file ) { - if ( $template_file['slug'] === $registered_template->slug ) { - return false; - } - } - return true; - } - ); - $query_result = array_merge( $query_result, $matching_registered_templates ); - } - - return $query_result; -} -add_filter( 'get_block_templates', '_gutenberg_add_block_templates_from_registry', 10, 3 ); - -/** - * Hooks into `get_block_template` to add the `plugin` property when necessary. - * - * @param WP_Block_Template|null $block_template The found block template, or null if there isn’t one. - * @return WP_Block_Template|null The block template that was already found with the plugin property defined if it was registered by a plugin. - */ -function _gutenberg_add_block_template_plugin_attribute( $block_template ) { - if ( $block_template ) { - $registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $block_template->slug ); - if ( $registered_template ) { - $block_template->plugin = $registered_template->plugin; - $block_template->origin = - 'theme' !== $block_template->origin && 'theme' !== $block_template->source ? - 'plugin' : - $block_template->origin; - $block_template->title = empty( $block_template->title ) || $block_template->title === $block_template->slug ? $registered_template->title : $block_template->title; - $block_template->description = empty( $block_template->description ) ? $registered_template->description : $block_template->description; - } - } - - return $block_template; -} -add_filter( 'get_block_template', '_gutenberg_add_block_template_plugin_attribute', 10, 1 ); - -/** - * Hooks into `get_block_file_template` so templates from the registry are also returned. - * - * @param WP_Block_Template|null $block_template The found block template, or null if there is none. - * @param string $id Template unique identifier (example: 'theme_slug//template_slug'). - * @return WP_Block_Template|null The block template that was already found or from the registry. In case the template was already found, add the necessary details from the registry. - */ -function _gutenberg_add_block_file_templates_from_registry( $block_template, $id ) { - if ( $block_template ) { - $registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $block_template->slug ); - if ( $registered_template ) { - $block_template->plugin = $registered_template->plugin; - $block_template->origin = - 'theme' !== $block_template->origin && 'theme' !== $block_template->source ? - 'plugin' : - $block_template->origin; - } - return $block_template; - } - - $parts = explode( '//', $id, 2 ); - - if ( count( $parts ) < 2 ) { - return $block_template; - } - - list( , $slug ) = $parts; - return WP_Block_Templates_Registry::get_instance()->get_by_slug( $slug ); -} -add_filter( 'get_block_file_template', '_gutenberg_add_block_file_templates_from_registry', 10, 2 ); diff --git a/lib/compat/wordpress-6.7/html-api/class-gutenberg-html-active-formatting-elements-6-7.php b/lib/compat/wordpress-6.7/html-api/class-gutenberg-html-active-formatting-elements-6-7.php deleted file mode 100644 index 10f53fe82ce4e0..00000000000000 --- a/lib/compat/wordpress-6.7/html-api/class-gutenberg-html-active-formatting-elements-6-7.php +++ /dev/null @@ -1,187 +0,0 @@ - Initially, the list of active formatting elements is empty. - * > It is used to handle mis-nested formatting element tags. - * > - * > The list contains elements in the formatting category, and markers. - * > The markers are inserted when entering applet, object, marquee, - * > template, td, th, and caption elements, and are used to prevent - * > formatting from "leaking" into applet, object, marquee, template, - * > td, th, and caption elements. - * > - * > In addition, each element in the list of active formatting elements - * > is associated with the token for which it was created, so that - * > further elements can be created for that token if necessary. - * - * @since 6.4.0 - * - * @access private - * - * @see https://html.spec.whatwg.org/#list-of-active-formatting-elements - * @see WP_HTML_Processor - */ -class Gutenberg_HTML_Active_Formatting_Elements_6_7 { - /** - * Holds the stack of active formatting element references. - * - * @since 6.4.0 - * - * @var WP_HTML_Token[] - */ - private $stack = array(); - - /** - * Reports if a specific node is in the stack of active formatting elements. - * - * @since 6.4.0 - * - * @param WP_HTML_Token $token Look for this node in the stack. - * @return bool Whether the referenced node is in the stack of active formatting elements. - */ - public function contains_node( Gutenberg_HTML_Token_6_7 $token ) { - foreach ( $this->walk_up() as $item ) { - if ( $token->bookmark_name === $item->bookmark_name ) { - return true; - } - } - - return false; - } - - /** - * Returns how many nodes are currently in the stack of active formatting elements. - * - * @since 6.4.0 - * - * @return int How many node are in the stack of active formatting elements. - */ - public function count() { - return count( $this->stack ); - } - - /** - * Returns the node at the end of the stack of active formatting elements, - * if one exists. If the stack is empty, returns null. - * - * @since 6.4.0 - * - * @return WP_HTML_Token|null Last node in the stack of active formatting elements, if one exists, otherwise null. - */ - public function current_node() { - $current_node = end( $this->stack ); - - return $current_node ? $current_node : null; - } - - /** - * Pushes a node onto the stack of active formatting elements. - * - * @since 6.4.0 - * - * @see https://html.spec.whatwg.org/#push-onto-the-list-of-active-formatting-elements - * - * @param WP_HTML_Token $token Push this node onto the stack. - */ - public function push( Gutenberg_HTML_Token_6_7 $token ) { - /* - * > If there are already three elements in the list of active formatting elements after the last marker, - * > if any, or anywhere in the list if there are no markers, that have the same tag name, namespace, and - * > attributes as element, then remove the earliest such element from the list of active formatting - * > elements. For these purposes, the attributes must be compared as they were when the elements were - * > created by the parser; two elements have the same attributes if all their parsed attributes can be - * > paired such that the two attributes in each pair have identical names, namespaces, and values - * > (the order of the attributes does not matter). - * - * @todo Implement the "Noah's Ark clause" to only add up to three of any given kind of formatting elements to the stack. - */ - // > Add element to the list of active formatting elements. - $this->stack[] = $token; - } - - /** - * Removes a node from the stack of active formatting elements. - * - * @since 6.4.0 - * - * @param WP_HTML_Token $token Remove this node from the stack, if it's there already. - * @return bool Whether the node was found and removed from the stack of active formatting elements. - */ - public function remove_node( Gutenberg_HTML_Token_6_7 $token ) { - foreach ( $this->walk_up() as $position_from_end => $item ) { - if ( $token->bookmark_name !== $item->bookmark_name ) { - continue; - } - - $position_from_start = $this->count() - $position_from_end - 1; - array_splice( $this->stack, $position_from_start, 1 ); - return true; - } - - return false; - } - - /** - * Steps through the stack of active formatting elements, starting with the - * top element (added first) and walking downwards to the one added last. - * - * This generator function is designed to be used inside a "foreach" loop. - * - * Example: - * - * $html = 'We are here'; - * foreach ( $stack->walk_down() as $node ) { - * echo "{$node->node_name} -> "; - * } - * > EM -> STRONG -> A -> - * - * To start with the most-recently added element and walk towards the top, - * see WP_HTML_Active_Formatting_Elements::walk_up(). - * - * @since 6.4.0 - */ - public function walk_down() { - $count = count( $this->stack ); - - for ( $i = 0; $i < $count; $i++ ) { - yield $this->stack[ $i ]; - } - } - - /** - * Steps through the stack of active formatting elements, starting with the - * bottom element (added last) and walking upwards to the one added first. - * - * This generator function is designed to be used inside a "foreach" loop. - * - * Example: - * - * $html = 'We are here'; - * foreach ( $stack->walk_up() as $node ) { - * echo "{$node->node_name} -> "; - * } - * > A -> STRONG -> EM -> - * - * To start with the first added element and walk towards the bottom, - * see WP_HTML_Active_Formatting_Elements::walk_down(). - * - * @since 6.4.0 - */ - public function walk_up() { - for ( $i = count( $this->stack ) - 1; $i >= 0; $i-- ) { - yield $this->stack[ $i ]; - } - } -} diff --git a/lib/compat/wordpress-6.7/html-api/class-gutenberg-html-attribute-token-6-7.php b/lib/compat/wordpress-6.7/html-api/class-gutenberg-html-attribute-token-6-7.php deleted file mode 100644 index 4ee369b795c84c..00000000000000 --- a/lib/compat/wordpress-6.7/html-api/class-gutenberg-html-attribute-token-6-7.php +++ /dev/null @@ -1,116 +0,0 @@ - - * ------------ length is 12, including quotes - * - * - * ------- length is 6 - * - * - * ------------ length is 11 - * - * @since 6.5.0 Replaced `end` with `length` to more closely match `substr()`. - * - * @var int - */ - public $length; - - /** - * Whether the attribute is a boolean attribute with value `true`. - * - * @since 6.2.0 - * - * @var bool - */ - public $is_true; - - /** - * Constructor. - * - * @since 6.2.0 - * @since 6.5.0 Replaced `end` with `length` to more closely match `substr()`. - * - * @param string $name Attribute name. - * @param int $value_start Attribute value. - * @param int $value_length Number of bytes attribute value spans. - * @param int $start The string offset where the attribute name starts. - * @param int $length Byte length of the entire attribute name or name and value pair expression. - * @param bool $is_true Whether the attribute is a boolean attribute with true value. - */ - public function __construct( $name, $value_start, $value_length, $start, $length, $is_true ) { - $this->name = $name; - $this->value_starts_at = $value_start; - $this->value_length = $value_length; - $this->start = $start; - $this->length = $length; - $this->is_true = $is_true; - } -} diff --git a/lib/compat/wordpress-6.7/html-api/class-gutenberg-html-decoder-6-7.php b/lib/compat/wordpress-6.7/html-api/class-gutenberg-html-decoder-6-7.php deleted file mode 100644 index c745904865b1f8..00000000000000 --- a/lib/compat/wordpress-6.7/html-api/class-gutenberg-html-decoder-6-7.php +++ /dev/null @@ -1,463 +0,0 @@ -= $length ) { - return null; - } - - if ( '&' !== $text[ $at ] ) { - return null; - } - - /* - * Numeric character references. - * - * When truncated, these will encode the code point found by parsing the - * digits that are available. For example, when `🅰` is truncated - * to `DZ` it will encode `Η±`. It does not: - * - know how to parse the original `πŸ…°`. - * - fail to parse and return plaintext `DZ`. - * - fail to parse and return the replacement character `οΏ½` - */ - if ( '#' === $text[ $at + 1 ] ) { - if ( $at + 2 >= $length ) { - return null; - } - - /** Tracks inner parsing within the numeric character reference. */ - $digits_at = $at + 2; - - if ( 'x' === $text[ $digits_at ] || 'X' === $text[ $digits_at ] ) { - $numeric_base = 16; - $numeric_digits = '0123456789abcdefABCDEF'; - $max_digits = 6; // 􏿿 - ++$digits_at; - } else { - $numeric_base = 10; - $numeric_digits = '0123456789'; - $max_digits = 7; // 􏿿 - } - - // Cannot encode invalid Unicode code points. Max is to U+10FFFF. - $zero_count = strspn( $text, '0', $digits_at ); - $digit_count = strspn( $text, $numeric_digits, $digits_at + $zero_count ); - $after_digits = $digits_at + $zero_count + $digit_count; - $has_semicolon = $after_digits < $length && ';' === $text[ $after_digits ]; - $end_of_span = $has_semicolon ? $after_digits + 1 : $after_digits; - - // `&#` or `&#x` without digits returns into plaintext. - if ( 0 === $digit_count && 0 === $zero_count ) { - return null; - } - - // Whereas `&#` and only zeros is invalid. - if ( 0 === $digit_count ) { - $match_byte_length = $end_of_span - $at; - return 'οΏ½'; - } - - // If there are too many digits then it's not worth parsing. It's invalid. - if ( $digit_count > $max_digits ) { - $match_byte_length = $end_of_span - $at; - return 'οΏ½'; - } - - $digits = substr( $text, $digits_at + $zero_count, $digit_count ); - $code_point = intval( $digits, $numeric_base ); - - /* - * Noncharacters, 0x0D, and non-ASCII-whitespace control characters. - * - * > A noncharacter is a code point that is in the range U+FDD0 to U+FDEF, - * > inclusive, or U+FFFE, U+FFFF, U+1FFFE, U+1FFFF, U+2FFFE, U+2FFFF, - * > U+3FFFE, U+3FFFF, U+4FFFE, U+4FFFF, U+5FFFE, U+5FFFF, U+6FFFE, - * > U+6FFFF, U+7FFFE, U+7FFFF, U+8FFFE, U+8FFFF, U+9FFFE, U+9FFFF, - * > U+AFFFE, U+AFFFF, U+BFFFE, U+BFFFF, U+CFFFE, U+CFFFF, U+DFFFE, - * > U+DFFFF, U+EFFFE, U+EFFFF, U+FFFFE, U+FFFFF, U+10FFFE, or U+10FFFF. - * - * A C0 control is a code point that is in the range of U+00 to U+1F, - * but ASCII whitespace includes U+09, U+0A, U+0C, and U+0D. - * - * These characters are invalid but still decode as any valid character. - * This comment is here to note and explain why there's no check to - * remove these characters or replace them. - * - * @see https://infra.spec.whatwg.org/#noncharacter - */ - - /* - * Code points in the C1 controls area need to be remapped as if they - * were stored in Windows-1252. Note! This transformation only happens - * for numeric character references. The raw code points in the byte - * stream are not translated. - * - * > If the number is one of the numbers in the first column of - * > the following table, then find the row with that number in - * > the first column, and set the character reference code to - * > the number in the second column of that row. - */ - if ( $code_point >= 0x80 && $code_point <= 0x9F ) { - $windows_1252_mapping = array( - 0x20AC, // 0x80 -> EURO SIGN (€). - 0x81, // 0x81 -> (no change). - 0x201A, // 0x82 -> SINGLE LOW-9 QUOTATION MARK (β€š). - 0x0192, // 0x83 -> LATIN SMALL LETTER F WITH HOOK (Ζ’). - 0x201E, // 0x84 -> DOUBLE LOW-9 QUOTATION MARK (β€ž). - 0x2026, // 0x85 -> HORIZONTAL ELLIPSIS (…). - 0x2020, // 0x86 -> DAGGER (†). - 0x2021, // 0x87 -> DOUBLE DAGGER (‑). - 0x02C6, // 0x88 -> MODIFIER LETTER CIRCUMFLEX ACCENT (Λ†). - 0x2030, // 0x89 -> PER MILLE SIGN (‰). - 0x0160, // 0x8A -> LATIN CAPITAL LETTER S WITH CARON (Ε ). - 0x2039, // 0x8B -> SINGLE LEFT-POINTING ANGLE QUOTATION MARK (β€Ή). - 0x0152, // 0x8C -> LATIN CAPITAL LIGATURE OE (Ε’). - 0x8D, // 0x8D -> (no change). - 0x017D, // 0x8E -> LATIN CAPITAL LETTER Z WITH CARON (Ε½). - 0x8F, // 0x8F -> (no change). - 0x90, // 0x90 -> (no change). - 0x2018, // 0x91 -> LEFT SINGLE QUOTATION MARK (β€˜). - 0x2019, // 0x92 -> RIGHT SINGLE QUOTATION MARK (’). - 0x201C, // 0x93 -> LEFT DOUBLE QUOTATION MARK (β€œ). - 0x201D, // 0x94 -> RIGHT DOUBLE QUOTATION MARK (”). - 0x2022, // 0x95 -> BULLET (β€’). - 0x2013, // 0x96 -> EN DASH (–). - 0x2014, // 0x97 -> EM DASH (β€”). - 0x02DC, // 0x98 -> SMALL TILDE (˜). - 0x2122, // 0x99 -> TRADE MARK SIGN (β„’). - 0x0161, // 0x9A -> LATIN SMALL LETTER S WITH CARON (Ε‘). - 0x203A, // 0x9B -> SINGLE RIGHT-POINTING ANGLE QUOTATION MARK (β€Ί). - 0x0153, // 0x9C -> LATIN SMALL LIGATURE OE (Ε“). - 0x9D, // 0x9D -> (no change). - 0x017E, // 0x9E -> LATIN SMALL LETTER Z WITH CARON (ΕΎ). - 0x0178, // 0x9F -> LATIN CAPITAL LETTER Y WITH DIAERESIS (ΕΈ). - ); - - $code_point = $windows_1252_mapping[ $code_point - 0x80 ]; - } - - $match_byte_length = $end_of_span - $at; - return self::code_point_to_utf8_bytes( $code_point ); - } - - /** Tracks inner parsing within the named character reference. */ - $name_at = $at + 1; - // Minimum named character reference is two characters. E.g. `GT`. - if ( $name_at + 2 > $length ) { - return null; - } - - $name_length = 0; - $replacement = $html5_named_character_references->read_token( $text, $name_at, $name_length ); - if ( false === $replacement ) { - return null; - } - - $after_name = $name_at + $name_length; - - // If the match ended with a semicolon then it should always be decoded. - if ( ';' === $text[ $name_at + $name_length - 1 ] ) { - $match_byte_length = $after_name - $at; - return $replacement; - } - - /* - * At this point though there's a match for an entry in the named - * character reference table but the match doesn't end in `;`. - * It may be allowed if it's followed by something unambiguous. - */ - $ambiguous_follower = ( - $after_name < $length && - $name_at < $length && - ( - ctype_alnum( $text[ $after_name ] ) || - '=' === $text[ $after_name ] - ) - ); - - // It's non-ambiguous, safe to leave it in. - if ( ! $ambiguous_follower ) { - $match_byte_length = $after_name - $at; - return $replacement; - } - - // It's ambiguous, which isn't allowed inside attributes. - if ( 'attribute' === $context ) { - return null; - } - - $match_byte_length = $after_name - $at; - return $replacement; - } - - /** - * Encode a code point number into the UTF-8 encoding. - * - * This encoder implements the UTF-8 encoding algorithm for converting - * a code point into a byte sequence. If it receives an invalid code - * point it will return the Unicode Replacement Character U+FFFD `οΏ½`. - * - * Example: - * - * 'πŸ…°' === WP_HTML_Decoder::code_point_to_utf8_bytes( 0x1f170 ); - * - * // Half of a surrogate pair is an invalid code point. - * 'οΏ½' === WP_HTML_Decoder::code_point_to_utf8_bytes( 0xd83c ); - * - * @since 6.6.0 - * - * @see https://www.rfc-editor.org/rfc/rfc3629 For the UTF-8 standard. - * - * @param int $code_point Which code point to convert. - * @return string Converted code point, or `οΏ½` if invalid. - */ - public static function code_point_to_utf8_bytes( $code_point ): string { - // Pre-check to ensure a valid code point. - if ( - $code_point <= 0 || - ( $code_point >= 0xD800 && $code_point <= 0xDFFF ) || - $code_point > 0x10FFFF - ) { - return 'οΏ½'; - } - - if ( $code_point <= 0x7F ) { - return chr( $code_point ); - } - - if ( $code_point <= 0x7FF ) { - $byte1 = chr( ( $code_point >> 6 ) | 0xC0 ); - $byte2 = chr( $code_point & 0x3F | 0x80 ); - - return "{$byte1}{$byte2}"; - } - - if ( $code_point <= 0xFFFF ) { - $byte1 = chr( ( $code_point >> 12 ) | 0xE0 ); - $byte2 = chr( ( $code_point >> 6 ) & 0x3F | 0x80 ); - $byte3 = chr( $code_point & 0x3F | 0x80 ); - - return "{$byte1}{$byte2}{$byte3}"; - } - - // Any values above U+10FFFF are eliminated above in the pre-check. - $byte1 = chr( ( $code_point >> 18 ) | 0xF0 ); - $byte2 = chr( ( $code_point >> 12 ) & 0x3F | 0x80 ); - $byte3 = chr( ( $code_point >> 6 ) & 0x3F | 0x80 ); - $byte4 = chr( $code_point & 0x3F | 0x80 ); - - return "{$byte1}{$byte2}{$byte3}{$byte4}"; - } -} diff --git a/lib/compat/wordpress-6.7/html-api/class-gutenberg-html-open-elements-6-7.php b/lib/compat/wordpress-6.7/html-api/class-gutenberg-html-open-elements-6-7.php deleted file mode 100644 index fd2b252432455e..00000000000000 --- a/lib/compat/wordpress-6.7/html-api/class-gutenberg-html-open-elements-6-7.php +++ /dev/null @@ -1,598 +0,0 @@ - Initially, the stack of open elements is empty. The stack grows - * > downwards; the topmost node on the stack is the first one added - * > to the stack, and the bottommost node of the stack is the most - * > recently added node in the stack (notwithstanding when the stack - * > is manipulated in a random access fashion as part of the handling - * > for misnested tags). - * - * @since 6.4.0 - * - * @access private - * - * @see https://html.spec.whatwg.org/#stack-of-open-elements - * @see WP_HTML_Processor - */ -class Gutenberg_HTML_Open_Elements_6_7 { - /** - * Holds the stack of open element references. - * - * @since 6.4.0 - * - * @var WP_HTML_Token[] - */ - public $stack = array(); - - /** - * Whether a P element is in button scope currently. - * - * This class optimizes scope lookup by pre-calculating - * this value when elements are added and removed to the - * stack of open elements which might change its value. - * This avoids frequent iteration over the stack. - * - * @since 6.4.0 - * - * @var bool - */ - private $has_p_in_button_scope = false; - - /** - * A function that will be called when an item is popped off the stack of open elements. - * - * The function will be called with the popped item as its argument. - * - * @since 6.6.0 - * - * @var Closure|null - */ - private $pop_handler = null; - - /** - * A function that will be called when an item is pushed onto the stack of open elements. - * - * The function will be called with the pushed item as its argument. - * - * @since 6.6.0 - * - * @var Closure|null - */ - private $push_handler = null; - - /** - * Sets a pop handler that will be called when an item is popped off the stack of - * open elements. - * - * The function will be called with the pushed item as its argument. - * - * @since 6.6.0 - * - * @param Closure $handler The handler function. - */ - public function set_pop_handler( Closure $handler ): void { - $this->pop_handler = $handler; - } - - /** - * Sets a push handler that will be called when an item is pushed onto the stack of - * open elements. - * - * The function will be called with the pushed item as its argument. - * - * @since 6.6.0 - * - * @param Closure $handler The handler function. - */ - public function set_push_handler( Closure $handler ): void { - $this->push_handler = $handler; - } - - /** - * Reports if a specific node is in the stack of open elements. - * - * @since 6.4.0 - * - * @param WP_HTML_Token $token Look for this node in the stack. - * @return bool Whether the referenced node is in the stack of open elements. - */ - public function contains_node( Gutenberg_HTML_Token_6_7 $token ): bool { - foreach ( $this->walk_up() as $item ) { - if ( $token->bookmark_name === $item->bookmark_name ) { - return true; - } - } - - return false; - } - - /** - * Returns how many nodes are currently in the stack of open elements. - * - * @since 6.4.0 - * - * @return int How many node are in the stack of open elements. - */ - public function count(): int { - return count( $this->stack ); - } - - /** - * Returns the node at the end of the stack of open elements, - * if one exists. If the stack is empty, returns null. - * - * @since 6.4.0 - * - * @return WP_HTML_Token|null Last node in the stack of open elements, if one exists, otherwise null. - */ - public function current_node(): ?Gutenberg_HTML_Token_6_7 { - $current_node = end( $this->stack ); - - return $current_node ? $current_node : null; - } - - /** - * Indicates if the current node is of a given type or name. - * - * It's possible to pass either a node type or a node name to this function. - * In the case there is no current element it will always return `false`. - * - * Example: - * - * // Is the current node a text node? - * $stack->current_node_is( '#text' ); - * - * // Is the current node a DIV element? - * $stack->current_node_is( 'DIV' ); - * - * // Is the current node any element/tag? - * $stack->current_node_is( '#tag' ); - * - * @see WP_HTML_Tag_Processor::get_token_type - * @see WP_HTML_Tag_Processor::get_token_name - * - * @since 6.7.0 - * - * @access private - * - * @param string $identity Check if the current node has this name or type (depending on what is provided). - * @return bool Whether there is a current element that matches the given identity, whether a token name or type. - */ - public function current_node_is( string $identity ): bool { - $current_node = end( $this->stack ); - if ( false === $current_node ) { - return false; - } - - $current_node_name = $current_node->node_name; - - return ( - $current_node_name === $identity || - ( '#doctype' === $identity && 'html' === $current_node_name ) || - ( '#tag' === $identity && ctype_upper( $current_node_name ) ) - ); - } - - /** - * Returns whether an element is in a specific scope. - * - * @since 6.4.0 - * - * @see https://html.spec.whatwg.org/#has-an-element-in-the-specific-scope - * - * @param string $tag_name Name of tag check. - * @param string[] $termination_list List of elements that terminate the search. - * @return bool Whether the element was found in a specific scope. - */ - public function has_element_in_specific_scope( string $tag_name, $termination_list ): bool { - foreach ( $this->walk_up() as $node ) { - if ( $node->node_name === $tag_name ) { - return true; - } - - if ( - '(internal: H1 through H6 - do not use)' === $tag_name && - in_array( $node->node_name, array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ), true ) - ) { - return true; - } - - switch ( $node->node_name ) { - case 'HTML': - return false; - } - - if ( in_array( $node->node_name, $termination_list, true ) ) { - return false; - } - } - - return false; - } - - /** - * Returns whether a particular element is in scope. - * - * @since 6.4.0 - * - * @see https://html.spec.whatwg.org/#has-an-element-in-scope - * - * @param string $tag_name Name of tag to check. - * @return bool Whether given element is in scope. - */ - public function has_element_in_scope( string $tag_name ): bool { - return $this->has_element_in_specific_scope( - $tag_name, - array( - - /* - * Because it's not currently possible to encounter - * one of the termination elements, they don't need - * to be listed here. If they were, they would be - * unreachable and only waste CPU cycles while - * scanning through HTML. - */ - ) - ); - } - - /** - * Returns whether a particular element is in list item scope. - * - * @since 6.4.0 - * @since 6.5.0 Implemented: no longer throws on every invocation. - * - * @see https://html.spec.whatwg.org/#has-an-element-in-list-item-scope - * - * @param string $tag_name Name of tag to check. - * @return bool Whether given element is in scope. - */ - public function has_element_in_list_item_scope( string $tag_name ): bool { - return $this->has_element_in_specific_scope( - $tag_name, - array( - // There are more elements that belong here which aren't currently supported. - 'OL', - 'UL', - ) - ); - } - - /** - * Returns whether a particular element is in button scope. - * - * @since 6.4.0 - * - * @see https://html.spec.whatwg.org/#has-an-element-in-button-scope - * - * @param string $tag_name Name of tag to check. - * @return bool Whether given element is in scope. - */ - public function has_element_in_button_scope( string $tag_name ): bool { - return $this->has_element_in_specific_scope( $tag_name, array( 'BUTTON' ) ); - } - - /** - * Returns whether a particular element is in table scope. - * - * @since 6.4.0 - * - * @see https://html.spec.whatwg.org/#has-an-element-in-table-scope - * - * @throws WP_HTML_Unsupported_Exception Always until this function is implemented. - * - * @param string $tag_name Name of tag to check. - * @return bool Whether given element is in scope. - */ - public function has_element_in_table_scope( string $tag_name ): bool { - throw new Gutenberg_HTML_Unsupported_Exception_6_7( 'Cannot process elements depending on table scope.' ); - - return false; // The linter requires this unreachable code until the function is implemented and can return. - } - - /** - * Returns whether a particular element is in select scope. - * - * This test differs from the others like it, in that its rules are inverted. - * Instead of arriving at a match when one of any tag in a termination group - * is reached, this one terminates if any other tag is reached. - * - * > The stack of open elements is said to have a particular element in select scope when it has - * > that element in the specific scope consisting of all element types except the following: - * > - optgroup in the HTML namespace - * > - option in the HTML namespace - * - * @since 6.4.0 Stub implementation (throws). - * @since 6.7.0 Full implementation. - * - * @see https://html.spec.whatwg.org/#has-an-element-in-select-scope - * - * @param string $tag_name Name of tag to check. - * @return bool Whether the given element is in SELECT scope. - */ - public function has_element_in_select_scope( string $tag_name ): bool { - foreach ( $this->walk_up() as $node ) { - if ( $node->node_name === $tag_name ) { - return true; - } - - if ( - 'OPTION' !== $node->node_name && - 'OPTGROUP' !== $node->node_name - ) { - return false; - } - } - - return false; - } - - /** - * Returns whether a P is in BUTTON scope. - * - * @since 6.4.0 - * - * @see https://html.spec.whatwg.org/#has-an-element-in-button-scope - * - * @return bool Whether a P is in BUTTON scope. - */ - public function has_p_in_button_scope(): bool { - return $this->has_p_in_button_scope; - } - - /** - * Pops a node off of the stack of open elements. - * - * @since 6.4.0 - * - * @see https://html.spec.whatwg.org/#stack-of-open-elements - * - * @return bool Whether a node was popped off of the stack. - */ - public function pop(): bool { - $item = array_pop( $this->stack ); - if ( null === $item ) { - return false; - } - - if ( 'context-node' === $item->bookmark_name ) { - $this->stack[] = $item; - return false; - } - - $this->after_element_pop( $item ); - return true; - } - - /** - * Pops nodes off of the stack of open elements until one with the given tag name has been popped. - * - * @since 6.4.0 - * - * @see WP_HTML_Open_Elements::pop - * - * @param string $tag_name Name of tag that needs to be popped off of the stack of open elements. - * @return bool Whether a tag of the given name was found and popped off of the stack of open elements. - */ - public function pop_until( string $tag_name ): bool { - foreach ( $this->walk_up() as $item ) { - if ( 'context-node' === $item->bookmark_name ) { - return true; - } - - $this->pop(); - - if ( - '(internal: H1 through H6 - do not use)' === $tag_name && - in_array( $item->node_name, array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ), true ) - ) { - return true; - } - - if ( $tag_name === $item->node_name ) { - return true; - } - } - - return false; - } - - /** - * Pushes a node onto the stack of open elements. - * - * @since 6.4.0 - * - * @see https://html.spec.whatwg.org/#stack-of-open-elements - * - * @param WP_HTML_Token $stack_item Item to add onto stack. - */ - public function push( Gutenberg_HTML_Token_6_7 $stack_item ): void { - $this->stack[] = $stack_item; - $this->after_element_push( $stack_item ); - } - - /** - * Removes a specific node from the stack of open elements. - * - * @since 6.4.0 - * - * @param WP_HTML_Token $token The node to remove from the stack of open elements. - * @return bool Whether the node was found and removed from the stack of open elements. - */ - public function remove_node( Gutenberg_HTML_Token_6_7 $token ): bool { - if ( 'context-node' === $token->bookmark_name ) { - return false; - } - - foreach ( $this->walk_up() as $position_from_end => $item ) { - if ( $token->bookmark_name !== $item->bookmark_name ) { - continue; - } - - $position_from_start = $this->count() - $position_from_end - 1; - array_splice( $this->stack, $position_from_start, 1 ); - $this->after_element_pop( $item ); - return true; - } - - return false; - } - - - /** - * Steps through the stack of open elements, starting with the top element - * (added first) and walking downwards to the one added last. - * - * This generator function is designed to be used inside a "foreach" loop. - * - * Example: - * - * $html = 'We are here'; - * foreach ( $stack->walk_down() as $node ) { - * echo "{$node->node_name} -> "; - * } - * > EM -> STRONG -> A -> - * - * To start with the most-recently added element and walk towards the top, - * see WP_HTML_Open_Elements::walk_up(). - * - * @since 6.4.0 - */ - public function walk_down() { - $count = count( $this->stack ); - - for ( $i = 0; $i < $count; $i++ ) { - yield $this->stack[ $i ]; - } - } - - /** - * Steps through the stack of open elements, starting with the bottom element - * (added last) and walking upwards to the one added first. - * - * This generator function is designed to be used inside a "foreach" loop. - * - * Example: - * - * $html = 'We are here'; - * foreach ( $stack->walk_up() as $node ) { - * echo "{$node->node_name} -> "; - * } - * > A -> STRONG -> EM -> - * - * To start with the first added element and walk towards the bottom, - * see WP_HTML_Open_Elements::walk_down(). - * - * @since 6.4.0 - * @since 6.5.0 Accepts $above_this_node to start traversal above a given node, if it exists. - * - * @param WP_HTML_Token|null $above_this_node Optional. Start traversing above this node, - * if provided and if the node exists. - */ - public function walk_up( ?Gutenberg_HTML_Token_6_7 $above_this_node = null ) { - $has_found_node = null === $above_this_node; - - for ( $i = count( $this->stack ) - 1; $i >= 0; $i-- ) { - $node = $this->stack[ $i ]; - - if ( ! $has_found_node ) { - $has_found_node = $node === $above_this_node; - continue; - } - - yield $node; - } - } - - /* - * Internal helpers. - */ - - /** - * Updates internal flags after adding an element. - * - * Certain conditions (such as "has_p_in_button_scope") are maintained here as - * flags that are only modified when adding and removing elements. This allows - * the HTML Processor to quickly check for these conditions instead of iterating - * over the open stack elements upon each new tag it encounters. These flags, - * however, need to be maintained as items are added and removed from the stack. - * - * @since 6.4.0 - * - * @param WP_HTML_Token $item Element that was added to the stack of open elements. - */ - public function after_element_push( Gutenberg_HTML_Token_6_7 $item ): void { - /* - * When adding support for new elements, expand this switch to trap - * cases where the precalculated value needs to change. - */ - switch ( $item->node_name ) { - case 'BUTTON': - $this->has_p_in_button_scope = false; - break; - - case 'P': - $this->has_p_in_button_scope = true; - break; - } - - if ( null !== $this->push_handler ) { - ( $this->push_handler )( $item ); - } - } - - /** - * Updates internal flags after removing an element. - * - * Certain conditions (such as "has_p_in_button_scope") are maintained here as - * flags that are only modified when adding and removing elements. This allows - * the HTML Processor to quickly check for these conditions instead of iterating - * over the open stack elements upon each new tag it encounters. These flags, - * however, need to be maintained as items are added and removed from the stack. - * - * @since 6.4.0 - * - * @param WP_HTML_Token $item Element that was removed from the stack of open elements. - */ - public function after_element_pop( Gutenberg_HTML_Token_6_7 $item ): void { - /* - * When adding support for new elements, expand this switch to trap - * cases where the precalculated value needs to change. - */ - switch ( $item->node_name ) { - case 'BUTTON': - $this->has_p_in_button_scope = $this->has_element_in_button_scope( 'P' ); - break; - - case 'P': - $this->has_p_in_button_scope = $this->has_element_in_button_scope( 'P' ); - break; - } - - if ( null !== $this->pop_handler ) { - ( $this->pop_handler )( $item ); - } - } - - /** - * Wakeup magic method. - * - * @since 6.6.0 - */ - public function __wakeup() { - throw new \LogicException( __CLASS__ . ' should never be unserialized' ); - } -} diff --git a/lib/compat/wordpress-6.7/html-api/class-gutenberg-html-processor-6-7.php b/lib/compat/wordpress-6.7/html-api/class-gutenberg-html-processor-6-7.php deleted file mode 100644 index 15cbbdc6adda8c..00000000000000 --- a/lib/compat/wordpress-6.7/html-api/class-gutenberg-html-processor-6-7.php +++ /dev/null @@ -1,3402 +0,0 @@ -next_tag( array( 'breadcrumbs' => array( 'DIV', 'FIGURE', 'IMG' ) ) ) ) { - * $processor->add_class( 'responsive-image' ); - * } - * - * #### Breadcrumbs - * - * Breadcrumbs represent the stack of open elements from the root - * of the document or fragment down to the currently-matched node, - * if one is currently selected. Call WP_HTML_Processor::get_breadcrumbs() - * to inspect the breadcrumbs for a matched tag. - * - * Breadcrumbs can specify nested HTML structure and are equivalent - * to a CSS selector comprising tag names separated by the child - * combinator, such as "DIV > FIGURE > IMG". - * - * Since all elements find themselves inside a full HTML document - * when parsed, the return value from `get_breadcrumbs()` will always - * contain any implicit outermost elements. For example, when parsing - * with `create_fragment()` in the `BODY` context (the default), any - * tag in the given HTML document will contain `array( 'HTML', 'BODY', … )` - * in its breadcrumbs. - * - * Despite containing the implied outermost elements in their breadcrumbs, - * tags may be found with the shortest-matching breadcrumb query. That is, - * `array( 'IMG' )` matches all IMG elements and `array( 'P', 'IMG' )` - * matches all IMG elements directly inside a P element. To ensure that no - * partial matches erroneously match it's possible to specify in a query - * the full breadcrumb match all the way down from the root HTML element. - * - * Example: - * - * $html = '
A lovely day outside
'; - * // ----- Matches here. - * $processor->next_tag( array( 'breadcrumbs' => array( 'FIGURE', 'IMG' ) ) ); - * - * $html = '
A lovely day outside
'; - * // ---- Matches here. - * $processor->next_tag( array( 'breadcrumbs' => array( 'FIGURE', 'FIGCAPTION', 'EM' ) ) ); - * - * $html = '
'; - * // ----- Matches here, because IMG must be a direct child of the implicit BODY. - * $processor->next_tag( array( 'breadcrumbs' => array( 'BODY', 'IMG' ) ) ); - * - * ## HTML Support - * - * This class implements a small part of the HTML5 specification. - * It's designed to operate within its support and abort early whenever - * encountering circumstances it can't properly handle. This is - * the principle way in which this class remains as simple as possible - * without cutting corners and breaking compliance. - * - * ### Supported elements - * - * If any unsupported element appears in the HTML input the HTML Processor - * will abort early and stop all processing. This draconian measure ensures - * that the HTML Processor won't break any HTML it doesn't fully understand. - * - * The following list specifies the HTML tags that _are_ supported: - * - * - Containers: ADDRESS, BLOCKQUOTE, DETAILS, DIALOG, DIV, FOOTER, HEADER, MAIN, MENU, SPAN, SUMMARY. - * - Custom elements: All custom elements are supported. :) - * - Form elements: BUTTON, DATALIST, FIELDSET, INPUT, LABEL, LEGEND, METER, OPTGROUP, OPTION, PROGRESS, SEARCH, SELECT. - * - Formatting elements: B, BIG, CODE, EM, FONT, I, PRE, SMALL, STRIKE, STRONG, TT, U, WBR. - * - Heading elements: H1, H2, H3, H4, H5, H6, HGROUP. - * - Links: A. - * - Lists: DD, DL, DT, LI, OL, UL. - * - Media elements: AUDIO, CANVAS, EMBED, FIGCAPTION, FIGURE, IMG, MAP, PICTURE, SOURCE, TRACK, VIDEO. - * - Paragraph: BR, P. - * - Phrasing elements: ABBR, AREA, BDI, BDO, CITE, DATA, DEL, DFN, INS, MARK, OUTPUT, Q, SAMP, SUB, SUP, TIME, VAR. - * - Sectioning elements: ARTICLE, ASIDE, HR, NAV, SECTION. - * - Templating elements: SLOT. - * - Text decoration: RUBY. - * - Deprecated elements: ACRONYM, BLINK, CENTER, DIR, ISINDEX, KEYGEN, LISTING, MULTICOL, NEXTID, PARAM, SPACER. - * - * ### Supported markup - * - * Some kinds of non-normative HTML involve reconstruction of formatting elements and - * re-parenting of mis-nested elements. For example, a DIV tag found inside a TABLE - * may in fact belong _before_ the table in the DOM. If the HTML Processor encounters - * such a case it will stop processing. - * - * The following list specifies HTML markup that _is_ supported: - * - * - Markup involving only those tags listed above. - * - Fully-balanced and non-overlapping tags. - * - HTML with unexpected tag closers. - * - Some unbalanced or overlapping tags. - * - P tags after unclosed P tags. - * - BUTTON tags after unclosed BUTTON tags. - * - A tags after unclosed A tags that don't involve any active formatting elements. - * - * @since 6.4.0 - * - * @see WP_HTML_Tag_Processor - * @see https://html.spec.whatwg.org/ - */ -class Gutenberg_HTML_Processor_6_7 extends Gutenberg_HTML_Tag_Processor_6_7 { - /** - * The maximum number of bookmarks allowed to exist at any given time. - * - * HTML processing requires more bookmarks than basic tag processing, - * so this class constant from the Tag Processor is overwritten. - * - * @since 6.4.0 - * - * @var int - */ - const MAX_BOOKMARKS = 100; - - /** - * Holds the working state of the parser, including the stack of - * open elements and the stack of active formatting elements. - * - * Initialized in the constructor. - * - * @since 6.4.0 - * - * @var WP_HTML_Processor_State - */ - private $state; - - /** - * Used to create unique bookmark names. - * - * This class sets a bookmark for every tag in the HTML document that it encounters. - * The bookmark name is auto-generated and increments, starting with `1`. These are - * internal bookmarks and are automatically released when the referring WP_HTML_Token - * goes out of scope and is garbage-collected. - * - * @since 6.4.0 - * - * @see WP_HTML_Processor::$release_internal_bookmark_on_destruct - * - * @var int - */ - private $bookmark_counter = 0; - - /** - * Stores an explanation for why something failed, if it did. - * - * @see self::get_last_error - * - * @since 6.4.0 - * - * @var string|null - */ - private $last_error = null; - - /** - * Stores context for why the parser bailed on unsupported HTML, if it did. - * - * @see self::get_unsupported_exception - * - * @since 6.7.0 - * - * @var WP_HTML_Unsupported_Exception|null - */ - private $unsupported_exception = null; - - /** - * Releases a bookmark when PHP garbage-collects its wrapping WP_HTML_Token instance. - * - * This function is created inside the class constructor so that it can be passed to - * the stack of open elements and the stack of active formatting elements without - * exposing it as a public method on the class. - * - * @since 6.4.0 - * - * @var Closure|null - */ - private $release_internal_bookmark_on_destruct = null; - - /** - * Stores stack events which arise during parsing of the - * HTML document, which will then supply the "match" events. - * - * @since 6.6.0 - * - * @var WP_HTML_Stack_Event[] - */ - private $element_queue = array(); - - /** - * Stores the current breadcrumbs. - * - * @since 6.7.0 - * - * @var string[] - */ - private $breadcrumbs = array(); - - /** - * Current stack event, if set, representing a matched token. - * - * Because the parser may internally point to a place further along in a document - * than the nodes which have already been processed (some "virtual" nodes may have - * appeared while scanning the HTML document), this will point at the "current" node - * being processed. It comes from the front of the element queue. - * - * @since 6.6.0 - * - * @var WP_HTML_Stack_Event|null - */ - private $current_element = null; - - /** - * Context node if created as a fragment parser. - * - * @var WP_HTML_Token|null - */ - private $context_node = null; - - /** - * Whether the parser has yet processed the context node, - * if created as a fragment parser. - * - * The context node will be initially pushed onto the stack of open elements, - * but when created as a fragment parser, this context element (and the implicit - * HTML document node above it) should not be exposed as a matched token or node. - * - * This boolean indicates whether the processor should skip over the current - * node in its initial search for the first node created from the input HTML. - * - * @var bool - */ - private $has_seen_context_node = false; - - /* - * Public Interface Functions - */ - - /** - * Creates an HTML processor in the fragment parsing mode. - * - * Use this for cases where you are processing chunks of HTML that - * will be found within a bigger HTML document, such as rendered - * block output that exists within a post, `the_content` inside a - * rendered site layout. - * - * Fragment parsing occurs within a context, which is an HTML element - * that the document will eventually be placed in. It becomes important - * when special elements have different rules than others, such as inside - * a TEXTAREA or a TITLE tag where things that look like tags are text, - * or inside a SCRIPT tag where things that look like HTML syntax are JS. - * - * The context value should be a representation of the tag into which the - * HTML is found. For most cases this will be the body element. The HTML - * form is provided because a context element may have attributes that - * impact the parse, such as with a SCRIPT tag and its `type` attribute. - * - * ## Current HTML Support - * - * - The only supported context is ``, which is the default value. - * - The only supported document encoding is `UTF-8`, which is the default value. - * - * @since 6.4.0 - * @since 6.6.0 Returns `static` instead of `self` so it can create subclass instances. - * - * @param string $html Input HTML fragment to process. - * @param string $context Context element for the fragment, must be default of ``. - * @param string $encoding Text encoding of the document; must be default of 'UTF-8'. - * @return static|null The created processor if successful, otherwise null. - */ - public static function create_fragment( $html, $context = '', $encoding = 'UTF-8' ) { - if ( '' !== $context || 'UTF-8' !== $encoding ) { - return null; - } - - $processor = new static( $html, self::CONSTRUCTOR_UNLOCK_CODE ); - $processor->state->context_node = array( 'BODY', array() ); - $processor->state->insertion_mode = Gutenberg_HTML_Processor_State_6_7::INSERTION_MODE_IN_BODY; - - // @todo Create "fake" bookmarks for non-existent but implied nodes. - $processor->bookmarks['root-node'] = new Gutenberg_HTML_Span_6_7( 0, 0 ); - $processor->bookmarks['context-node'] = new Gutenberg_HTML_Span_6_7( 0, 0 ); - - $processor->state->stack_of_open_elements->push( - new Gutenberg_HTML_Token_6_7( - 'root-node', - 'HTML', - false - ) - ); - - $context_node = new Gutenberg_HTML_Token_6_7( - 'context-node', - $processor->state->context_node[0], - false - ); - - $processor->context_node = $context_node; - $processor->breadcrumbs = array( 'HTML', $context_node->node_name ); - - return $processor; - } - - /** - * Constructor. - * - * Do not use this method. Use the static creator methods instead. - * - * @access private - * - * @since 6.4.0 - * - * @see WP_HTML_Processor::create_fragment() - * - * @param string $html HTML to process. - * @param string|null $use_the_static_create_methods_instead This constructor should not be called manually. - */ - public function __construct( $html, $use_the_static_create_methods_instead = null ) { - parent::__construct( $html ); - - if ( self::CONSTRUCTOR_UNLOCK_CODE !== $use_the_static_create_methods_instead ) { - _doing_it_wrong( - __METHOD__, - sprintf( - /* translators: %s: WP_HTML_Processor::create_fragment(). */ - __( 'Call %s to create an HTML Processor instead of calling the constructor directly.' ), - 'WP_HTML_Processor::create_fragment()' - ), - '6.4.0' - ); - } - - $this->state = new Gutenberg_HTML_Processor_State_6_7(); - - $this->state->stack_of_open_elements->set_push_handler( - function ( Gutenberg_HTML_Token_6_7 $token ): void { - $is_virtual = ! isset( $this->state->current_token ) || $this->is_tag_closer(); - $same_node = isset( $this->state->current_token ) && $token->node_name === $this->state->current_token->node_name; - $provenance = ( ! $same_node || $is_virtual ) ? 'virtual' : 'real'; - $this->element_queue[] = new Gutenberg_HTML_Stack_Event_6_7( $token, Gutenberg_HTML_Stack_Event_6_7::PUSH, $provenance ); - } - ); - - $this->state->stack_of_open_elements->set_pop_handler( - function ( Gutenberg_HTML_Token_6_7 $token ): void { - $is_virtual = ! isset( $this->state->current_token ) || ! $this->is_tag_closer(); - $same_node = isset( $this->state->current_token ) && $token->node_name === $this->state->current_token->node_name; - $provenance = ( ! $same_node || $is_virtual ) ? 'virtual' : 'real'; - $this->element_queue[] = new Gutenberg_HTML_Stack_Event_6_7( $token, Gutenberg_HTML_Stack_Event_6_7::POP, $provenance ); - } - ); - - /* - * Create this wrapper so that it's possible to pass - * a private method into WP_HTML_Token classes without - * exposing it to any public API. - */ - $this->release_internal_bookmark_on_destruct = function ( string $name ): void { - parent::release_bookmark( $name ); - }; - } - - /** - * Stops the parser and terminates its execution when encountering unsupported markup. - * - * @throws WP_HTML_Unsupported_Exception Halts execution of the parser. - * - * @since 6.7.0 - * - * @param string $message Explains support is missing in order to parse the current node. - */ - private function bail( string $message ) { - $here = $this->bookmarks[ $this->state->current_token->bookmark_name ]; - $token = substr( $this->html, $here->start, $here->length ); - - $open_elements = array(); - foreach ( $this->state->stack_of_open_elements->stack as $item ) { - $open_elements[] = $item->node_name; - } - - $active_formats = array(); - foreach ( $this->state->active_formatting_elements->walk_down() as $item ) { - $active_formats[] = $item->node_name; - } - - $this->last_error = self::ERROR_UNSUPPORTED; - - $this->unsupported_exception = new Gutenberg_HTML_Unsupported_Exception_6_7( - $message, - $this->state->current_token->node_name, - $here->start, - $token, - $open_elements, - $active_formats - ); - - throw $this->unsupported_exception; - } - - /** - * Returns the last error, if any. - * - * Various situations lead to parsing failure but this class will - * return `false` in all those cases. To determine why something - * failed it's possible to request the last error. This can be - * helpful to know to distinguish whether a given tag couldn't - * be found or if content in the document caused the processor - * to give up and abort processing. - * - * Example - * - * $processor = WP_HTML_Processor::create_fragment( '