re && static::has_same_registered_blocks( 'core' ) ) { return static::$core; } $config = static::read_json_file( __DIR__ . '/theme.json' ); $config = static::translate( $config ); /** * Filters the default data provided by WordPress for global styles & settings. * * @since 6.1.0 * * @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data. */ $theme_json = apply_filters( 'wp_theme_json_data_default', new WP_Theme_JSON_Data( $config, 'default' ) ); $config = $theme_json->get_data(); static::$core = new WP_Theme_JSON( $config, 'default' ); return static::$core; } /** * Checks whether the registered blocks were already processed for this origin. * * @since 6.1.0 * * @param string $origin Data source for which to cache the blocks. * Valid values are 'core', 'blocks', 'theme', and 'user'. * @return bool True on success, false otherwise. */ protected static function has_same_registered_blocks( $origin ) { // Bail out if the origin is invalid. if ( ! isset( static::$blocks_cache[ $origin ] ) ) { return false; } $registry = WP_Block_Type_Registry::get_instance(); $blocks = $registry->get_all_registered(); // Is there metadata for all currently registered blocks? $block_diff = array_diff_key( $blocks, static::$blocks_cache[ $origin ] ); if ( empty( $block_diff ) ) { return true; } foreach ( $blocks as $block_name => $block_type ) { static::$blocks_cache[ $origin ][ $block_name ] = true; } return false; } /** * Returns the theme's data. * * Data from theme.json will be backfilled from existing * theme supports, if any. Note that if the same data * is present in theme.json and in theme supports, * the theme.json takes precedence. * * @since 5.8.0 * @since 5.9.0 Theme supports have been inlined and the `$theme_support_data` argument removed. * @since 6.0.0 Added an `$options` parameter to allow the theme data to be returned without theme supports. * * @param array $deprecated Deprecated. Not used. * @param array $options { * Options arguments. * * @type bool $with_supports Whether to include theme supports in the data. Default true. * } * @return WP_Theme_JSON Entity that holds theme data. */ public static function get_theme_data( $deprecated = array(), $options = array() ) { if ( ! empty( $deprecated ) ) { _deprecated_argument( __METHOD__, '5.9.0' ); } $options = wp_parse_args( $options, array( 'with_supports' => true ) ); if ( null === static::$theme || ! static::has_same_registered_blocks( 'theme' ) ) { $wp_theme = wp_get_theme(); $theme_json_file = $wp_theme->get_file_path( 'theme.json' ); if ( is_readable( $theme_json_file ) ) { $theme_json_data = static::read_json_file( $theme_json_file ); $theme_json_data = static::translate( $theme_json_data, $wp_theme->get( 'TextDomain' ) ); } else { $theme_json_data = array(); } /** * Filters the data provided by the theme for global styles and settings. * * @since 6.1.0 * * @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data. */ $theme_json = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) ); $theme_json_data = $theme_json->get_data(); static::$theme = new WP_Theme_JSON( $theme_json_data ); if ( $wp_theme->parent() ) { // Get parent theme.json. $parent_theme_json_file = $wp_theme->parent()->get_file_path( 'theme.json' ); if ( $theme_json_file !== $parent_theme_json_file && is_readable( $parent_theme_json_file ) ) { $parent_theme_json_data = static::read_json_file( $parent_theme_json_file ); $parent_theme_json_data = static::translate( $parent_theme_json_data, $wp_theme->parent()->get( 'TextDomain' ) ); $parent_theme = new WP_Theme_JSON( $parent_theme_json_data ); /* * Merge the child theme.json into the parent theme.json. * The child theme takes precedence over the parent. */ $parent_theme->merge( static::$theme ); static::$theme = $parent_theme; } } } if ( ! $options['with_supports'] ) { return static::$theme; } /* * We want the presets and settings declared in theme.json * to override the ones declared via theme supports. * So we take theme supports, transform it to theme.json shape * and merge the static::$theme upon that. */ $theme_support_data = WP_Theme_JSON::get_from_editor_settings( get_classic_theme_supports_block_editor_settings() ); if ( ! wp_theme_has_theme_json() ) { if ( ! isset( $theme_support_data['settings']['color'] ) ) { $theme_support_data['settings']['color'] = array(); } $default_palette = false; if ( current_theme_supports( 'default-color-palette' ) ) { $default_palette = true; } if ( ! isset( $theme_support_data['settings']['color']['palette'] ) ) { // If the theme does not have any palette, we still want to show the core one. $default_palette = true; } $theme_support_data['settings']['color']['defaultPalette'] = $default_palette; $default_gradients = false; if ( current_theme_supports( 'default-gradient-presets' ) ) { $default_gradients = true; } if ( ! isset( $theme_support_data['settings']['color']['gradients'] ) ) { // If the theme does not have any gradients, we still want to show the core ones. $default_gradients = true; } $theme_support_data['settings']['color']['defaultGradients'] = $default_gradients; if ( ! isset( $theme_support_data['settings']['shadow'] ) ) { $theme_support_data['settings']['shadow'] = array(); } /* * Shadow presets are explicitly disabled for classic themes until a * decision is made for whether the default presets should match the * other presets or if they should be disabled by default in classic * themes. See https://github.com/WordPress/gutenberg/issues/59989. */ $theme_support_data['settings']['shadow']['defaultPresets'] = false; // Allow themes to enable link color setting via theme_support. if ( current_theme_supports( 'link-color' ) ) { $theme_support_data['settings']['color']['link'] = true; } // Allow themes to enable all border settings via theme_support. if ( current_theme_supports( 'border' ) ) { $theme_support_data['settings']['border']['color'] = true; $theme_support_data['settings']['border']['radius'] = true; $theme_support_data['settings']['border']['style'] = true; $theme_support_data['settings']['border']['width'] = true; } // Allow themes to enable appearance tools via theme_support. if ( current_theme_supports( 'appearance-tools' ) ) { $theme_support_data['settings']['appearanceTools'] = true; } } $with_theme_supports = new WP_Theme_JSON( $theme_support_data ); $with_theme_supports->merge( static::$theme ); return $with_theme_supports; } /** * Gets the styles for blocks from the block.json file. * * @since 6.1.0 * * @return WP_Theme_JSON */ public static function get_block_data() { $registry = WP_Block_Type_Registry::get_instance(); $blocks = $registry->get_all_registered(); if ( null !== static::$blocks && static::has_same_registered_blocks( 'blocks' ) ) { return static::$blocks; } $config = array( 'version' => 2 ); foreach ( $blocks as $block_name => $block_type ) { if ( isset( $block_type->supports['__experimentalStyle'] ) ) { $config['styles']['blocks'][ $block_name ] = static::remove_json_comments( $block_type->supports['__experimentalStyle'] ); } if ( isset( $block_type->supports['spacing']['blockGap']['__experimentalDefault'] ) && ! isset( $config['styles']['blocks'][ $block_name ]['spacing']['blockGap'] ) ) { /* * Ensure an empty placeholder value exists for the block, if it provides a default blockGap value. * The real blockGap value to be used will be determined when the styles are rendered for output. */ $config['styles']['blocks'][ $block_name ]['spacing']['blockGap'] = null; } } /** * Filters the data provided by the blocks for global styles & settings. * * @since 6.1.0 * * @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data. */ $theme_json = apply_filters( 'wp_theme_json_data_blocks', new WP_Theme_JSON_Data( $config, 'blocks' ) ); $config = $theme_json->get_data(); static::$blocks = new WP_Theme_JSON( $config, 'blocks' ); return static::$blocks; } /** * When given an array, this will remove any keys with the name `//`. * * @since 6.1.0 * * @param array $input_array The array to filter. * @return array The filtered array. */ private static function remove_json_comments( $input_array ) { unset( $input_array['//'] ); foreach ( $input_array as $k => $v ) { if ( is_array( $v ) ) { $input_array[ $k ] = static::remove_json_comments( $v ); } } return $input_array; } /** * Returns the custom post type that contains the user's origin config * for the active theme or an empty array if none are found. * * This can also create and return a new draft custom post type. * * @since 5.9.0 * * @param WP_Theme $theme The theme object. If empty, it * defaults to the active theme. * @param bool $create_post Optional. Whether a new custom post * type should be created if none are * found. Default false. * @param array $post_status_filter Optional. Filter custom post type by * post status. Default `array( 'publish' )`, * so it only fetches published posts. * @return array Custom Post Type for the user's origin config. */ public static function get_user_data_from_wp_global_styles( $theme, $create_post = false, $post_status_filter = array( 'publish' ) ) { if ( ! $theme instanceof WP_Theme ) { $theme = wp_get_theme(); } /* * Bail early if the theme does not support a theme.json. * * Since wp_theme_has_theme_json() only supports the active * theme, the extra condition for whether $theme is the active theme is * present here. */ if ( $theme->get_stylesheet() === get_stylesheet() && ! wp_theme_has_theme_json() ) { return array(); } $user_cpt = array(); $post_type_filter = 'wp_global_styles'; $stylesheet = $theme->get_stylesheet(); $args = array( 'posts_per_page' => 1, 'orderby' => 'date', 'order' => 'desc', 'post_type' => $post_type_filter, 'post_status' => $post_status_filter, 'ignore_sticky_posts' => true, 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'tax_query' => array( array( 'taxonomy' => 'wp_theme', 'field' => 'name', 'terms' => $stylesheet, ), ), ); $global_style_query = new WP_Query(); $recent_posts = $global_style_query->query( $args ); if ( count( $recent_posts ) === 1 ) { $user_cpt = get_object_vars( $recent_posts[0] ); } elseif ( $create_post ) { $cpt_post_id = wp_insert_post( array( 'post_content' => '{"version": ' . WP_Theme_JSON::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }', 'post_status' => 'publish', 'post_title' => 'Custom Styles', // Do not make string translatable, see https://core.trac.wordpress.org/ticket/54518. 'post_type' => $post_type_filter, 'post_name' => sprintf( 'wp-global-styles-%s', urlencode( $stylesheet ) ), 'tax_input' => array( 'wp_theme' => array( $stylesheet ), ), ), true ); if ( ! is_wp_error( $cpt_post_id ) ) { $user_cpt = get_object_vars( get_post( $cpt_post_id ) ); } } return $user_cpt; } /** * Returns the user's origin config. * * @since 5.9.0 * * @return WP_Theme_JSON Entity that holds styles for user data. */ public static function get_user_data() { if ( null !== static::$user && static::has_same_registered_blocks( 'user' ) ) { return static::$user; } $config = array(); $user_cpt = static::get_user_data_from_wp_global_styles( wp_get_theme() ); if ( array_key_exists( 'post_content', $user_cpt ) ) { $decoded_data = json_decode( $user_cpt['post_content'], true ); $json_decoding_error = json_last_error(); if ( JSON_ERROR_NONE !== $json_decoding_error ) { trigger_error( 'Error when decoding a theme.json schema for user data. ' . json_last_error_msg() ); /** * Filters the data provided by the user for global styles & settings. * * @since 6.1.0 * * @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data. */ $theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) ); $config = $theme_json->get_data(); return new WP_Theme_JSON( $config, 'custom' ); } /* * Very important to verify that the flag isGlobalStylesUserThemeJSON is true. * If it's not true then the content was not escaped and is not safe. */ if ( is_array( $decoded_data ) && isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) && $decoded_data['isGlobalStylesUserThemeJSON'] ) { unset( $decoded_data['isGlobalStylesUserThemeJSON'] ); $config = $decoded_data; } } /** This filter is documented in wp-includes/class-wp-theme-json-resolver.php */ $theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) ); $config = $theme_json->get_data(); static::$user = new WP_Theme_JSON( $config, 'custom' ); return static::$user; } /** * Returns the data merged from multiple origins. * * There are four sources of data (origins) for a site: * * - default => WordPress * - blocks => each one of the blocks provides data for itself * - theme => the active theme * - custom => data provided by the user * * The custom's has higher priority than the theme's, the theme's higher than blocks', * and block's higher than default's. * * Unlike the getters * {@link https://developer.wordpress.org/reference/classes/wp_theme_json_resolver/get_core_data/ get_core_data}, * {@link https://developer.wordpress.org/reference/classes/wp_theme_json_resolver/get_theme_data/ get_theme_data}, * and {@link https://developer.wordpress.org/reference/classes/wp_theme_json_resolver/get_user_data/ get_user_data}, * this method returns data after it has been merged with the previous origins. * This means that if the same piece of data is declared in different origins * (default, blocks, theme, custom), the last origin overrides the previous. * * For example, if the user has set a background color * for the paragraph block, and the theme has done it as well, * the user preference wins. * * @since 5.8.0 * @since 5.9.0 Added user data, removed the `$settings` parameter, * added the `$origin` parameter. * @since 6.1.0 Added block data and generation of spacingSizes array. * @since 6.2.0 Changed ' $origin' parameter values to 'default', 'blocks', 'theme' or 'custom'. * * @param string $origin Optional. To what level should we merge data: 'default', 'blocks', 'theme' or 'custom'. * 'custom' is used as default value as well as fallback value if the origin is unknown. * @return WP_Theme_JSON */ public static function get_merged_data( $origin = 'custom' ) { if ( is_array( $origin ) ) { _deprecated_argument( __FUNCTION__, '5.9.0' ); } $result = new WP_Theme_JSON(); $result->merge( static::get_core_data() ); if ( 'default' === $origin ) { $result->set_spacing_sizes(); return $result; } $result->merge( static::get_block_data() ); if ( 'blocks' === $origin ) { return $result; } $result->merge( static::get_theme_data() ); if ( 'theme' === $origin ) { $result->set_spacing_sizes(); return $result; } $result->merge( static::get_user_data() ); $result->set_spacing_sizes(); return $result; } /** * Returns the ID of the custom post type * that stores user data. * * @since 5.9.0 * * @return integer|null */ public static function get_user_global_styles_post_id() { if ( null !== static::$user_custom_post_type_id ) { return static::$user_custom_post_type_id; } $user_cpt = static::get_user_data_from_wp_global_styles( wp_get_theme(), true ); if ( array_key_exists( 'ID', $user_cpt ) ) { static::$user_custom_post_type_id = $user_cpt['ID']; } return static::$user_custom_post_type_id; } /** * Determines whether the active theme has a theme.json file. * * @since 5.8.0 * @since 5.9.0 Added a check in the parent theme. * @deprecated 6.2.0 Use wp_theme_has_theme_json() instead. * * @return bool */ public static function theme_has_support() { _deprecated_function( __METHOD__, '6.2.0', 'wp_theme_has_theme_json()' ); return wp_theme_has_theme_json(); } /** * Builds the path to the given file and checks that it is readable. * * If it isn't, returns an empty string, otherwise returns the whole file path. * * @since 5.8.0 * @since 5.9.0 Adapted to work with child themes, added the `$template` argument. * * @param string $file_name Name of the file. * @param bool $template Optional. Use template theme directory. Default false. * @return string The whole file path or empty if the file doesn't exist. */ protected static function get_file_path_from_theme( $file_name, $template = false ) { $path = $template ? get_template_directory() : get_stylesheet_directory(); $candidate = $path . '/' . $file_name; return is_readable( $candidate ) ? $candidate : ''; } /** * Cleans the cached data so it can be recalculated. * * @since 5.8.0 * @since 5.9.0 Added the `$user`, `$user_custom_post_type_id`, * and `$i18n_schema` variables to reset. * @since 6.1.0 Added the `$blocks` and `$blocks_cache` variables * to reset. */ public static function clean_cached_data() { static::$core = null; static::$blocks = null; static::$blocks_cache = array( 'core' => array(), 'blocks' => array(), 'theme' => array(), 'user' => array(), ); static::$theme = null; static::$user = null; static::$user_custom_post_type_id = null; static::$i18n_schema = null; } /** * Returns an array of all nested JSON files within a given directory. * * @since 6.2.0 * * @param string $dir The directory to recursively iterate and list files of. * @return array The merged array. */ private static function recursively_iterate_json( $dir ) { $nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ) ); $nested_json_files = iterator_to_array( new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) ); return $nested_json_files; } /** * Returns the style variations defined by the theme. * * @since 6.0.0 * @since 6.2.0 Returns parent theme variations if theme is a child. * * @return array */ public static function get_style_variations() { $variation_files = array(); $variations = array(); $base_directory = get_stylesheet_directory() . '/styles'; $template_directory = get_template_directory() . '/styles'; if ( is_dir( $base_directory ) ) { $variation_files = static::recursively_iterate_json( $base_directory ); } if ( is_dir( $template_directory ) && $template_directory !== $base_directory ) { $variation_files_parent = static::recursively_iterate_json( $template_directory ); // If the child and parent variation file basename are the same, only include the child theme's. foreach ( $variation_files_parent as $parent_path => $parent ) { foreach ( $variation_files as $child_path => $child ) { if ( basename( $parent_path ) === basename( $child_path ) ) { unset( $variation_files_parent[ $parent_path ] ); } } } $variation_files = array_merge( $variation_files, $variation_files_parent ); } ksort( $variation_files ); foreach ( $variation_files as $path => $file ) { $decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) ); if ( is_array( $decoded_file ) ) { $translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) ); $variation = ( new WP_Theme_JSON( $translated ) )->get_raw_data(); if ( empty( $variation['title'] ) ) { $variation['title'] = basename( $path, '.json' ); } $variations[] = $variation; } } return $variations; } } array() ): array { if ( ! isset( $this->config_data[ $store_namespace ] ) ) { $this->config_data[ $store_namespace ] = array(); } if ( is_array( $config ) ) { $this->config_data[ $store_namespace ] = array_replace_recursive( $this->config_data[ $store_namespace ], $config ); } return $this->config_data[ $store_namespace ]; } /** * Prints the serialized client-side interactivity data. * * Encodes the config and initial state into JSON and prints them inside a * script tag of type "application/json". Once in the browser, the state will * be parsed and used to hydrate the client-side interactivity stores and the * configuration will be available using a `getConfig` utility. * * @since 6.5.0 */ public function print_client_interactivity_data() { if ( empty( $this->state_data ) && empty( $this->config_data ) ) { return; } $interactivity_data = array(); $config = array(); foreach ( $this->config_data as $key => $value ) { if ( ! empty( $value ) ) { $config[ $key ] = $value; } } if ( ! empty( $config ) ) { $interactivity_data['config'] = $config; } $state = array(); foreach ( $this->state_data as $key => $value ) { if ( ! empty( $value ) ) { $state[ $key ] = $value; } } if ( ! empty( $state ) ) { $interactivity_data['state'] = $state; } if ( ! empty( $interactivity_data ) ) { wp_print_inline_script_tag( wp_json_encode( $interactivity_data, JSON_HEX_TAG | JSON_HEX_AMP ), array( 'type' => 'application/json', 'id' => 'wp-interactivity-data', ) ); } } /** * Registers the `@wordpress/interactivity` script modules. * * @since 6.5.0 */ public function register_script_modules() { $suffix = wp_scripts_get_suffix(); wp_register_script_module( '@wordpress/interactivity', includes_url( "js/dist/interactivity$suffix.js" ) ); wp_register_script_module( '@wordpress/interactivity-router', includes_url( "js/dist/interactivity-router$suffix.js" ), array( '@wordpress/interactivity' ) ); } /** * Adds the necessary hooks for the Interactivity API. * * @since 6.5.0 */ public function add_hooks() { add_action( 'wp_enqueue_scripts', array( $this, 'register_script_modules' ) ); add_action( 'wp_footer', array( $this, 'print_client_interactivity_data' ) ); } /** * Processes the interactivity directives contained within the HTML content * and updates the markup accordingly. * * @since 6.5.0 * * @param string $html The HTML content to process. * @return string The processed HTML content. It returns the original content when the HTML contains unbalanced tags. */ public function process_directives( string $html ): string { if ( ! str_contains( $html, 'data-wp-' ) ) { return $html; } $context_stack = array(); $namespace_stack = array(); $result = $this->process_directives_args( $html, $context_stack, $namespace_stack ); return null === $result ? $html : $result; } /** * Processes the interactivity directives contained within the HTML content * and updates the markup accordingly. * * It needs the context and namespace stacks to be passed by reference, and * it returns null if the HTML contains unbalanced tags. * * @since 6.5.0 * * @param string $html The HTML content to process. * @param array $context_stack The reference to the array used to keep track of contexts during processing. * @param array $namespace_stack The reference to the array used to manage namespaces during processing. * @return string|null The processed HTML content. It returns null when the HTML contains unbalanced tags. */ private function process_directives_args( string $html, array &$context_stack, array &$namespace_stack ) { $p = new WP_Interactivity_API_Directives_Processor( $html ); $tag_stack = array(); $unbalanced = false; $directive_processor_prefixes = array_keys( self::$directive_processors ); $directive_processor_prefixes_reversed = array_reverse( $directive_processor_prefixes ); while ( $p->next_tag( array( 'tag_closers' => 'visit' ) ) ) { $tag_name = $p->get_tag(); /* * Directives inside SVG and MATH tags are not processed, * as they are not compatible with the Tag Processor yet. * We still process the rest of the HTML. */ if ( 'SVG' === $tag_name || 'MATH' === $tag_name ) { $p->skip_to_tag_closer(); continue; } if ( $p->is_tag_closer() ) { list( $opening_tag_name, $directives_prefixes ) = end( $tag_stack ); if ( 0 === count( $tag_stack ) || $opening_tag_name !== $tag_name ) { /* * If the tag stack is empty or the matching opening tag is not the * same than the closing tag, it means the HTML is unbalanced and it * stops processing it. */ $unbalanced = true; break; } else { // Remove the last tag from the stack. array_pop( $tag_stack ); } } else { if ( 0 !== count( $p->get_attribute_names_with_prefix( 'data-wp-each-child' ) ) ) { /* * If the tag has a `data-wp-each-child` directive, jump to its closer * tag because those tags have already been processed. */ $p->next_balanced_tag_closer_tag(); continue; } else { $directives_prefixes = array(); // Checks if there is a server directive processor registered for each directive. foreach ( $p->get_attribute_names_with_prefix( 'data-wp-' ) as $attribute_name ) { list( $directive_prefix ) = $this->extract_prefix_and_suffix( $attribute_name ); if ( array_key_exists( $directive_prefix, self::$directive_processors ) ) { $directives_prefixes[] = $directive_prefix; } } /* * If this tag will visit its closer tag, it adds it to the tag stack * so it can process its closing tag and check for unbalanced tags. */ if ( $p->has_and_visits_its_closer_tag() ) { $tag_stack[] = array( $tag_name, $directives_prefixes ); } } } /* * If the matching opener tag didn't have any directives, it can skip the * processing. */ if ( 0 === count( $directives_prefixes ) ) { continue; } // Directive processing might be different depending on if it is entering the tag or exiting it. $modes = array( 'enter' => ! $p->is_tag_closer(), 'exit' => $p->is_tag_closer() || ! $p->has_and_visits_its_closer_tag(), ); foreach ( $modes as $mode => $should_run ) { if ( ! $should_run ) { continue; } /* * Sorts the attributes by the order of the `directives_processor` array * and checks what directives are present in this element. */ $existing_directives_prefixes = array_intersect( 'enter' === $mode ? $directive_processor_prefixes : $directive_processor_prefixes_reversed, $directives_prefixes ); foreach ( $existing_directives_prefixes as $directive_prefix ) { $func = is_array( self::$directive_processors[ $directive_prefix ] ) ? self::$directive_processors[ $directive_prefix ] : array( $this, self::$directive_processors[ $directive_prefix ] ); call_user_func_array( $func, array( $p, $mode, &$context_stack, &$namespace_stack, &$tag_stack ) ); } } } /* * It returns null if the HTML is unbalanced because unbalanced HTML is * not safe to process. In that case, the Interactivity API runtime will * update the HTML on the client side during the hydration. */ return $unbalanced || 0 < count( $tag_stack ) ? null : $p->get_updated_html(); } /** * Evaluates the reference path passed to a directive based on the current * store namespace, state and context. * * @since 6.5.0 * * @param string|true $directive_value The directive attribute value string or `true` when it's a boolean attribute. * @param string $default_namespace The default namespace to use if none is explicitly defined in the directive * value. * @param array|false $context The current context for evaluating the directive or false if there is no * context. * @return mixed|null The result of the evaluation. Null if the reference path doesn't exist. */ private function evaluate( $directive_value, string $default_namespace, $context = false ) { list( $ns, $path ) = $this->extract_directive_value( $directive_value, $default_namespace ); if ( empty( $path ) ) { return null; } $store = array( 'state' => $this->state_data[ $ns ] ?? array(), 'context' => $context[ $ns ] ?? array(), ); // Checks if the reference path is preceded by a negation operator (!). $should_negate_value = '!' === $path[0]; $path = $should_negate_value ? substr( $path, 1 ) : $path; // Extracts the value from the store using the reference path. $path_segments = explode( '.', $path ); $current = $store; foreach ( $path_segments as $path_segment ) { if ( isset( $current[ $path_segment ] ) ) { $current = $current[ $path_segment ]; } else { return null; } } // Returns the opposite if it contains a negation operator (!). return $should_negate_value ? ! $current : $current; } /** * Extracts the directive attribute name to separate and return the directive * prefix and an optional suffix. * * The suffix is the string after the first double hyphen and the prefix is * everything that comes before the suffix. * * Example: * * extract_prefix_and_suffix( 'data-wp-interactive' ) => array( 'data-wp-interactive', null ) * extract_prefix_and_suffix( 'data-wp-bind--src' ) => array( 'data-wp-bind', 'src' ) * extract_prefix_and_suffix( 'data-wp-foo--and--bar' ) => array( 'data-wp-foo', 'and--bar' ) * * @since 6.5.0 * * @param string $directive_name The directive attribute name. * @return array An array containing the directive prefix and optional suffix. */ private function extract_prefix_and_suffix( string $directive_name ): array { return explode( '--', $directive_name, 2 ); } /** * Parses and extracts the namespace and reference path from the given * directive attribute value. * * If the value doesn't contain an explicit namespace, it returns the * default one. If the value contains a JSON object instead of a reference * path, the function tries to parse it and return the resulting array. If * the value contains strings that represent booleans ("true" and "false"), * numbers ("1" and "1.2") or "null", the function also transform them to * regular booleans, numbers and `null`. * * Example: * * extract_directive_value( 'actions.foo', 'myPlugin' ) => array( 'myPlugin', 'actions.foo' ) * extract_directive_value( 'otherPlugin::actions.foo', 'myPlugin' ) => array( 'otherPlugin', 'actions.foo' ) * extract_directive_value( '{ "isOpen": false }', 'myPlugin' ) => array( 'myPlugin', array( 'isOpen' => false ) ) * extract_directive_value( 'otherPlugin::{ "isOpen": false }', 'myPlugin' ) => array( 'otherPlugin', array( 'isOpen' => false ) ) * * @since 6.5.0 * * @param string|true $directive_value The directive attribute value. It can be `true` when it's a boolean * attribute. * @param string|null $default_namespace Optional. The default namespace if none is explicitly defined. * @return array An array containing the namespace in the first item and the JSON, the reference path, or null on the * second item. */ private function extract_directive_value( $directive_value, $default_namespace = null ): array { if ( empty( $directive_value ) || is_bool( $directive_value ) ) { return array( $default_namespace, null ); } // Replaces the value and namespace if there is a namespace in the value. if ( 1 === preg_match( '/^([\w\-_\/]+)::./', $directive_value ) ) { list($default_namespace, $directive_value) = explode( '::', $directive_value, 2 ); } /* * Tries to decode the value as a JSON object. If it fails and the value * isn't `null`, it returns the value as it is. Otherwise, it returns the * decoded JSON or null for the string `null`. */ $decoded_json = json_decode( $directive_value, true ); if ( null !== $decoded_json || 'null' === $directive_value ) { $directive_value = $decoded_json; } return array( $default_namespace, $directive_value ); } /** * Transforms a kebab-case string to camelCase. * * @param string $str The kebab-case string to transform to camelCase. * @return string The transformed camelCase string. */ private function kebab_to_camel_case( string $str ): string { return lcfirst( preg_replace_callback( '/(-)([a-z])/', function ( $matches ) { return strtoupper( $matches[2] ); }, strtolower( rtrim( $str, '-' ) ) ) ); } /** * Processes the `data-wp-interactive` directive. * * It adds the default store namespace defined in the directive value to the * stack so that it's available for the nested interactivity elements. * * @since 6.5.0 * * @param WP_Interactivity_API_Directives_Processor $p The directives processor instance. * @param string $mode Whether the processing is entering or exiting the tag. * @param array $context_stack The reference to the context stack. * @param array $namespace_stack The reference to the store namespace stack. */ private function data_wp_interactive_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$context_stack, array &$namespace_stack ) { // When exiting tags, it removes the last namespace from the stack. if ( 'exit' === $mode ) { array_pop( $namespace_stack ); return; } // Tries to decode the `data-wp-interactive` attribute value. $attribute_value = $p->get_attribute( 'data-wp-interactive' ); /* * Pushes the newly defined namespace or the current one if the * `data-wp-interactive` definition was invalid or does not contain a * namespace. It does so because the function pops out the current namespace * from the stack whenever it finds a `data-wp-interactive`'s closing tag, * independently of whether the previous `data-wp-interactive` definition * contained a valid namespace. */ $new_namespace = null; if ( is_string( $attribute_value ) && ! empty( $attribute_value ) ) { $decoded_json = json_decode( $attribute_value, true ); if ( is_array( $decoded_json ) ) { $new_namespace = $decoded_json['namespace'] ?? null; } else { $new_namespace = $attribute_value; } } $namespace_stack[] = ( $new_namespace && 1 === preg_match( '/^([\w\-_\/]+)/', $new_namespace ) ) ? $new_namespace : end( $namespace_stack ); } /** * Processes the `data-wp-context` directive. * * It adds the context defined in the directive value to the stack so that * it's available for the nested interactivity elements. * * @since 6.5.0 * * @param WP_Interactivity_API_Directives_Processor $p The directives processor instance. * @param string $mode Whether the processing is entering or exiting the tag. * @param array $context_stack The reference to the context stack. * @param array $namespace_stack The reference to the store namespace stack. */ private function data_wp_context_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$context_stack, array &$namespace_stack ) { // When exiting tags, it removes the last context from the stack. if ( 'exit' === $mode ) { array_pop( $context_stack ); return; } $attribute_value = $p->get_attribute( 'data-wp-context' ); $namespace_value = end( $namespace_stack ); // Separates the namespace from the context JSON object. list( $namespace_value, $decoded_json ) = is_string( $attribute_value ) && ! empty( $attribute_value ) ? $this->extract_directive_value( $attribute_value, $namespace_value ) : array( $namespace_value, null ); /* * If there is a namespace, it adds a new context to the stack merging the * previous context with the new one. */ if ( is_string( $namespace_value ) ) { $context_stack[] = array_replace_recursive( end( $context_stack ) !== false ? end( $context_stack ) : array(), array( $namespace_value => is_array( $decoded_json ) ? $decoded_json : array() ) ); } else { /* * If there is no namespace, it pushes the current context to the stack. * It needs to do so because the function pops out the current context * from the stack whenever it finds a `data-wp-context`'s closing tag. */ $context_stack[] = end( $context_stack ); } } /** * Processes the `data-wp-bind` directive. * * It updates or removes the bound attributes based on the evaluation of its * associated reference. * * @since 6.5.0 * * @param WP_Interactivity_API_Directives_Processor $p The directives processor instance. * @param string $mode Whether the processing is entering or exiting the tag. * @param array $context_stack The reference to the context stack. * @param array $namespace_stack The reference to the store namespace stack. */ private function data_wp_bind_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$context_stack, array &$namespace_stack ) { if ( 'enter' === $mode ) { $all_bind_directives = $p->get_attribute_names_with_prefix( 'data-wp-bind--' ); foreach ( $all_bind_directives as $attribute_name ) { list( , $bound_attribute ) = $this->extract_prefix_and_suffix( $attribute_name ); if ( empty( $bound_attribute ) ) { return; } $attribute_value = $p->get_attribute( $attribute_name ); $result = $this->evaluate( $attribute_value, end( $namespace_stack ), end( $context_stack ) ); if ( null !== $result && ( false !== $result || ( strlen( $bound_attribute ) > 5 && '-' === $bound_attribute[4] ) ) ) { /* * If the result of the evaluation is a boolean and the attribute is * `aria-` or `data-, convert it to a string "true" or "false". It * follows the exact same logic as Preact because it needs to * replicate what Preact will later do in the client: * https://github.com/preactjs/preact/blob/ea49f7a0f9d1ff2c98c0bdd66aa0cbc583055246/src/diff/props.js#L131C24-L136 */ if ( is_bool( $result ) && ( strlen( $bound_attribute ) > 5 && '-' === $bound_attribute[4] ) ) { $result = $result ? 'true' : 'false'; } $p->set_attribute( $bound_attribute, $result ); } else { $p->remove_attribute( $bound_attribute ); } } } } /** * Processes the `data-wp-class` directive. * * It adds or removes CSS classes in the current HTML element based on the * evaluation of its associated references. * * @since 6.5.0 * * @param WP_Interactivity_API_Directives_Processor $p The directives processor instance. * @param string $mode Whether the processing is entering or exiting the tag. * @param array $context_stack The reference to the context stack. * @param array $namespace_stack The reference to the store namespace stack. */ private function data_wp_class_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$context_stack, array &$namespace_stack ) { if ( 'enter' === $mode ) { $all_class_directives = $p->get_attribute_names_with_prefix( 'data-wp-class--' ); foreach ( $all_class_directives as $attribute_name ) { list( , $class_name ) = $this->extract_prefix_and_suffix( $attribute_name ); if ( empty( $class_name ) ) { return; } $attribute_value = $p->get_attribute( $attribute_name ); $result = $this->evaluate( $attribute_value, end( $namespace_stack ), end( $context_stack ) ); if ( $result ) { $p->add_class( $class_name ); } else { $p->remove_class( $class_name ); } } } } /** * Processes the `data-wp-style` directive. * * It updates the style attribute value of the current HTML element based on * the evaluation of its associated references. * * @since 6.5.0 * * @param WP_Interactivity_API_Directives_Processor $p The directives processor instance. * @param string $mode Whether the processing is entering or exiting the tag. * @param array $context_stack The reference to the context stack. * @param array $namespace_stack The reference to the store namespace stack. */ private function data_wp_style_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$context_stack, array &$namespace_stack ) { if ( 'enter' === $mode ) { $all_style_attributes = $p->get_attribute_names_with_prefix( 'data-wp-style--' ); foreach ( $all_style_attributes as $attribute_name ) { list( , $style_property ) = $this->extract_prefix_and_suffix( $attribute_name ); if ( empty( $style_property ) ) { continue; } $directive_attribute_value = $p->get_attribute( $attribute_name ); $style_property_value = $this->evaluate( $directive_attribute_value, end( $namespace_stack ), end( $context_stack ) ); $style_attribute_value = $p->get_attribute( 'style' ); $style_attribute_value = ( $style_attribute_value && ! is_bool( $style_attribute_value ) ) ? $style_attribute_value : ''; /* * Checks first if the style property is not falsy and the style * attribute value is not empty because if it is, it doesn't need to * update the attribute value. */ if ( $style_property_value || $style_attribute_value ) { $style_attribute_value = $this->merge_style_property( $style_attribute_value, $style_property, $style_property_value ); /* * If the style attribute value is not empty, it sets it. Otherwise, * it removes it. */ if ( ! empty( $style_attribute_value ) ) { $p->set_attribute( 'style', $style_attribute_value ); } else { $p->remove_attribute( 'style' ); } } } } } /** * Merges an individual style property in the `style` attribute of an HTML * element, updating or removing the property when necessary. * * If a property is modified, the old one is removed and the new one is added * at the end of the list. * * @since 6.5.0 * * Example: * * merge_style_property( 'color:green;', 'color', 'red' ) => 'color:red;' * merge_style_property( 'background:green;', 'color', 'red' ) => 'background:green;color:red;' * merge_style_property( 'color:green;', 'color', null ) => '' * * @param string $style_attribute_value The current style attribute value. * @param string $style_property_name The style property name to set. * @param string|false|null $style_property_value The value to set for the style property. With false, null or an * empty string, it removes the style property. * @return string The new style attribute value after the specified property has been added, updated or removed. */ private function merge_style_property( string $style_attribute_value, string $style_property_name, $style_property_value ): string { $style_assignments = explode( ';', $style_attribute_value ); $result = array(); $style_property_value = ! empty( $style_property_value ) ? rtrim( trim( $style_property_value ), ';' ) : null; $new_style_property = $style_property_value ? $style_property_name . ':' . $style_property_value . ';' : ''; // Generates an array with all the properties but the modified one. foreach ( $style_assignments as $style_assignment ) { if ( empty( trim( $style_assignment ) ) ) { continue; } list( $name, $value ) = explode( ':', $style_assignment ); if ( trim( $name ) !== $style_property_name ) { $result[] = trim( $name ) . ':' . trim( $value ) . ';'; } } // Adds the new/modified property at the end of the list. $result[] = $new_style_property; return implode( '', $result ); } /** * Processes the `data-wp-text` directive. * * It updates the inner content of the current HTML element based on the * evaluation of its associated reference. * * @since 6.5.0 * * @param WP_Interactivity_API_Directives_Processor $p The directives processor instance. * @param string $mode Whether the processing is entering or exiting the tag. * @param array $context_stack The reference to the context stack. * @param array $namespace_stack The reference to the store namespace stack. */ private function data_wp_text_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$context_stack, array &$namespace_stack ) { if ( 'enter' === $mode ) { $attribute_value = $p->get_attribute( 'data-wp-text' ); $result = $this->evaluate( $attribute_value, end( $namespace_stack ), end( $context_stack ) ); /* * Follows the same logic as Preact in the client and only changes the * content if the value is a string or a number. Otherwise, it removes the * content. */ if ( is_string( $result ) || is_numeric( $result ) ) { $p->set_content_between_balanced_tags( esc_html( $result ) ); } else { $p->set_content_between_balanced_tags( '' ); } } } /** * Returns the CSS styles for animating the top loading bar in the router. * * @since 6.5.0 * * @return string The CSS styles for the router's top loading bar animation. */ private function get_router_animation_styles(): string { return <<
HTML; } /** * Processes the `data-wp-router-region` directive. * * It renders in the footer a set of HTML elements to notify users about * client-side navigations. More concretely, the elements added are 1) a * top loading bar to visually inform that a navigation is in progress * and 2) an `aria-live` region for accessible navigation announcements. * * @since 6.5.0 * * @param WP_Interactivity_API_Directives_Processor $p The directives processor instance. * @param string $mode Whether the processing is entering or exiting the tag. */ private function data_wp_router_region_processor( WP_Interactivity_API_Directives_Processor $p, string $mode ) { if ( 'enter' === $mode && ! $this->has_processed_router_region ) { $this->has_processed_router_region = true; // Initialize the `core/router` store. $this->state( 'core/router', array( 'navigation' => array( 'texts' => array( 'loading' => __( 'Loading page, please wait.' ), 'loaded' => __( 'Page Loaded.' ), ), ), ) ); // Enqueues as an inline style. wp_register_style( 'wp-interactivity-router-animations', false ); wp_add_inline_style( 'wp-interactivity-router-animations', $this->get_router_animation_styles() ); wp_enqueue_style( 'wp-interactivity-router-animations' ); // Adds the necessary markup to the footer. add_action( 'wp_footer', array( $this, 'print_router_loading_and_screen_reader_markup' ) ); } } /** * Processes the `data-wp-each` directive. * * This directive gets an array passed as reference and iterates over it * generating new content for each item based on the inner markup of the * `template` tag. * * @since 6.5.0 * * @param WP_Interactivity_API_Directives_Processor $p The directives processor instance. * @param string $mode Whether the processing is entering or exiting the tag. * @param array $context_stack The reference to the context stack. * @param array $namespace_stack The reference to the store namespace stack. * @param array $tag_stack The reference to the tag stack. */ private function data_wp_each_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$context_stack, array &$namespace_stack, array &$tag_stack ) { if ( 'enter' === $mode && 'TEMPLATE' === $p->get_tag() ) { $attribute_name = $p->get_attribute_names_with_prefix( 'data-wp-each' )[0]; $extracted_suffix = $this->extract_prefix_and_suffix( $attribute_name ); $item_name = isset( $extracted_suffix[1] ) ? $this->kebab_to_camel_case( $extracted_suffix[1] ) : 'item'; $attribute_value = $p->get_attribute( $attribute_name ); $result = $this->evaluate( $attribute_value, end( $namespace_stack ), end( $context_stack ) ); // Gets the content between the template tags and leaves the cursor in the closer tag. $inner_content = $p->get_content_between_balanced_template_tags(); // Checks if there is a manual server-side directive processing. $template_end = 'data-wp-each: template end'; $p->set_bookmark( $template_end ); $p->next_tag(); $manual_sdp = $p->get_attribute( 'data-wp-each-child' ); $p->seek( $template_end ); // Rewinds to the template closer tag. $p->release_bookmark( $template_end ); /* * It doesn't process in these situations: * - Manual server-side directive processing. * - Empty or non-array values. * - Associative arrays because those are deserialized as objects in JS. * - Templates that contain top-level texts because those texts can't be * identified and removed in the client. */ if ( $manual_sdp || empty( $result ) || ! is_array( $result ) || ! array_is_list( $result ) || ! str_starts_with( trim( $inner_content ), '<' ) || ! str_ends_with( trim( $inner_content ), '>' ) ) { array_pop( $tag_stack ); return; } // Extracts the namespace from the directive attribute value. $namespace_value = end( $namespace_stack ); list( $namespace_value ) = is_string( $attribute_value ) && ! empty( $attribute_value ) ? $this->extract_directive_value( $attribute_value, $namespace_value ) : array( $namespace_value, null ); // Processes the inner content for each item of the array. $processed_content = ''; foreach ( $result as $item ) { // Creates a new context that includes the current item of the array. $context_stack[] = array_replace_recursive( end( $context_stack ) !== false ? end( $context_stack ) : array(), array( $namespace_value => array( $item_name => $item ) ) ); // Processes the inner content with the new context. $processed_item = $this->process_directives_args( $inner_content, $context_stack, $namespace_stack ); if ( null === $processed_item ) { // If the HTML is unbalanced, stop processing it. array_pop( $context_stack ); return; } // Adds the `data-wp-each-child` to each top-level tag. $i = new WP_Interactivity_API_Directives_Processor( $processed_item ); while ( $i->next_tag() ) { $i->set_attribute( 'data-wp-each-child', true ); $i->next_balanced_tag_closer_tag(); } $processed_content .= $i->get_updated_html(); // Removes the current context from the stack. array_pop( $context_stack ); } // Appends the processed content after the tag closer of the template. $p->append_content_after_template_tag_closer( $processed_content ); // Pops the last tag because it skipped the closing tag of the template tag. array_pop( $tag_stack ); } } }
Fatal error: Uncaught Error: Class 'WP_Interactivity_API' not found in /var/www/html/helitower.com.br/web/wp-includes/interactivity-api/interactivity-api.php:90 Stack trace: #0 /var/www/html/helitower.com.br/web/wp-settings.php(400): wp_interactivity() #1 /var/www/html/helitower.com.br/web/wp-config.php(99): require_once('/var/www/html/h...') #2 /var/www/html/helitower.com.br/web/wp-load.php(50): require_once('/var/www/html/h...') #3 /var/www/html/helitower.com.br/web/wp-blog-header.php(13): require_once('/var/www/html/h...') #4 /var/www/html/helitower.com.br/web/index.php(18): require('/var/www/html/h...') #5 {main} thrown in /var/www/html/helitower.com.br/web/wp-includes/interactivity-api/interactivity-api.php on line 90

Fatal error: Uncaught Error: Call to a member function set() on null in /var/www/html/helitower.com.br/web/wp-includes/l10n.php:854 Stack trace: #0 /var/www/html/helitower.com.br/web/wp-includes/l10n.php(957): load_textdomain('default', '/var/www/html/h...', 'pt_BR') #1 /var/www/html/helitower.com.br/web/wp-includes/class-wp-fatal-error-handler.php(49): load_default_textdomain() #2 [internal function]: WP_Fatal_Error_Handler->handle() #3 {main} thrown in /var/www/html/helitower.com.br/web/wp-includes/l10n.php on line 854