WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles()public staticWP 5.9.0

Returns the custom post type that contains the user's origin config for the active theme or a void array if none are found.

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

{} Это метод класса: WP_Theme_JSON_Resolver{}

Хуков нет.

Возвращает

Массив. Custom Post Type for the user's origin config.

Использование

$result = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, $create_post, $post_status_filter );
$theme(WP_Theme) (обязательный)
The theme object. If empty, it defaults to the active theme.
$create_post(true|false)
Whether a new custom post type should be created if none are found.
По умолчанию: false
$post_status_filter(массив)
Filter custom post type by post status.
По умолчанию: array( 'publish' ), so it only fetches published posts

Список изменений

С версии 5.9.0 Введена.

Код WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles() WP 6.0

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';
	$args             = array(
		'numberposts' => 1,
		'orderby'     => 'date',
		'order'       => 'desc',
		'post_type'   => $post_type_filter,
		'post_status' => $post_status_filter,
		'tax_query'   => array(
			array(
				'taxonomy' => 'wp_theme',
				'field'    => 'name',
				'terms'    => $theme->get_stylesheet(),
			),
		),
	);

	$cache_key = sprintf( 'wp_global_styles_%s', md5( serialize( $args ) ) );
	$post_id   = wp_cache_get( $cache_key );

	if ( (int) $post_id > 0 ) {
		return get_post( $post_id, ARRAY_A );
	}

	// Special case: '-1' is a results not found.
	if ( -1 === $post_id && ! $create_post ) {
		return $user_cpt;
	}

	$recent_posts = wp_get_recent_posts( $args );
	if ( is_array( $recent_posts ) && ( count( $recent_posts ) === 1 ) ) {
		$user_cpt = $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',
				'post_type'    => $post_type_filter,
				'post_name'    => 'wp-global-styles-' . urlencode( wp_get_theme()->get_stylesheet() ),
				'tax_input'    => array(
					'wp_theme' => array( wp_get_theme()->get_stylesheet() ),
				),
			),
			true
		);
		$user_cpt    = get_post( $cpt_post_id, ARRAY_A );
	}
	$cache_expiration = $user_cpt ? DAY_IN_SECONDS : HOUR_IN_SECONDS;
	wp_cache_set( $cache_key, $user_cpt ? $user_cpt['ID'] : -1, '', $cache_expiration );

	return $user_cpt;
}