WP_Theme_JSON example

$parent               = $this->get_parent( $request['parent'] );
        $global_styles_config = $this->get_decoded_global_styles_json( $post->post_content );

        if ( is_wp_error( $global_styles_config ) ) {
            return $global_styles_config;
        }

        $fields = $this->get_fields_for_response( $request );
        $data   = array();

        if ( ! empty( $global_styles_config['styles'] ) || ! empty( $global_styles_config['settings'] ) ) {
            $global_styles_config = ( new WP_Theme_JSON( $global_styles_config, 'custom' ) )->get_raw_data();
            if ( rest_is_field_included( 'settings', $fields ) ) {
                $data['settings'] = ! empty( $global_styles_config['settings'] ) ? $global_styles_config['settings'] : new stdClass();
            }
            if ( rest_is_field_included( 'styles', $fields ) ) {
                $data['styles'] = ! empty( $global_styles_config['styles'] ) ? $global_styles_config['styles'] : new stdClass();
            }
        }

        if ( rest_is_field_included( 'author', $fields ) ) {
            $data['author'] = (int) $post->post_author;
        }

        
$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. */

    }
    $variables_root_selector = WP_Theme_JSON::scope_selector( $class_name$variables_root_selector );

    // Remove any potentially unsafe styles.     $theme_json_shape  = WP_Theme_JSON::remove_insecure_properties(
        array(
            'version'  => WP_Theme_JSON::LATEST_SCHEMA,
            'settings' => $block_settings,
        )
    );
    $theme_json_object = new WP_Theme_JSON( $theme_json_shape );

    $styles = '';

    // include preset css variables declaration on the stylesheet.     $styles .= $theme_json_object->get_stylesheet(
        array( 'variables' ),
        null,
        array(
            'root_selector' => $variables_root_selector,
            'scope'         => $class_name,
        )
    );

    public function prepare_item_for_response( $post$request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable         $raw_config                       = json_decode( $post->post_content, true );
        $is_global_styles_user_theme_json = isset( $raw_config['isGlobalStylesUserThemeJSON'] ) && true === $raw_config['isGlobalStylesUserThemeJSON'];
        $config                           = array();
        if ( $is_global_styles_user_theme_json ) {
            $config = ( new WP_Theme_JSON( $raw_config, 'custom' ) )->get_raw_data();
        }

        // Base fields for every post.         $data   = array();
        $fields = $this->get_fields_for_response( $request );

        if ( rest_is_field_included( 'id', $fields ) ) {
            $data['id'] = $post->ID;
        }

        if ( rest_is_field_included( 'title', $fields ) ) {
            

    public function __construct( $data = array()$origin = 'theme' ) {
        $this->origin     = $origin;
        $this->theme_json = new WP_Theme_JSON( $data$this->origin );
    }

    /** * Updates the theme.json with the the given data. * * @since 6.1.0 * * @param array $new_data Array following the theme.json specification. * * @return WP_Theme_JSON_Data The own instance with access to the modified data. */
    
Home | Imprint | This part of the site doesn't use cookies.