WP_Theme_JSON_Resolver::get_user_data(): WP_Theme_JSON

In this article

Returns the user’s origin config.

Return

WP_Theme_JSON Entity that holds styles for user data.

Source

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 ) {
			wp_trigger_error( __METHOD__, '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' ) );

			/*
			 * Backward compatibility for extenders returning a WP_Theme_JSON_Data
			 * compatible class that is not a WP_Theme_JSON_Data object.
			 */
			if ( $theme_json instanceof WP_Theme_JSON_Data ) {
				return $theme_json->get_theme_json();
			} else {
				$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' ) );

	/*
	 * Backward compatibility for extenders returning a WP_Theme_JSON_Data
	 * compatible class that is not a WP_Theme_JSON_Data object.
	 */
	if ( $theme_json instanceof WP_Theme_JSON_Data ) {
		static::$user = $theme_json->get_theme_json();
	} else {
		$config       = $theme_json->get_data();
		static::$user = new WP_Theme_JSON( $config, 'custom' );
	}

	return static::$user;
}

Hooks

apply_filters( ‘wp_theme_json_data_user’, WP_Theme_JSON_Data $theme_json )

Filters the data provided by the user for global styles & settings.

Changelog

VersionDescription
6.6.0The 'isGlobalStylesUserThemeJSON' flag is left on the user data.
Register the block style variations coming from the user data.
5.9.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.