WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( WP_Theme $theme, bool $create_post = false, array $post_status_filter = array('publish') ): 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.

Description

This can also create and return a new draft custom post type.

Parameters

$themeWP_Themerequired
The theme object. If empty, it defaults to the active theme.
$create_postbooloptional
Whether a new custom post type should be created if none are found.

Default:false

$post_status_filterarrayoptional
Filter custom post type by post status. Default array( 'publish' ), so it only fetches published posts.

Default:array('publish')

Return

array Custom Post Type for the user’s origin config.

Source

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();
	}

	$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 && $recent_posts[0] instanceof WP_Post ) {
		$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 ) ) {
			$post = get_post( $cpt_post_id );
			if ( $post instanceof WP_Post ) {
				$user_cpt = get_object_vars( $post );
			}
		}
	}

	return $user_cpt;
}

Changelog

VersionDescription
5.9.0Introduced.

User Contributed Notes

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