Changeset 52275 for trunk/src/wp-includes/class-wp-theme-json-resolver.php
- Timestamp:
- 11/30/2021 12:22:30 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-theme-json-resolver.php
r52232 r52275 223 223 * @since 5.9.0 224 224 * 225 * @param bool $should_create_cpt Optional. Whether a new custom post type should be created if none are found. 226 * False by default. 227 * @param array $post_status_filter Filter Optional. custom post type by post status. 228 * ['publish'] by default, so it only fetches published posts. 229 * 225 * @param WP_Theme $theme The theme object. If empty, it 226 * defaults to the current theme. 227 * @param bool $should_create_cpt Optional. Whether a new custom post 228 * type should be created if none are 229 * found. False by default. 230 * @param array $post_status_filter Filter Optional. custom post type by 231 * post status. ['publish'] by default, 232 * so it only fetches published posts. 230 233 * @return array Custom Post Type for the user's origin config. 231 234 */ 232 private static function get_user_data_from_custom_post_type( $should_create_cpt = false, $post_status_filter = array( 'publish' ) ) { 235 public static function get_user_data_from_custom_post_type( $theme, $should_create_cpt = false, $post_status_filter = array( 'publish' ) ) { 236 if ( ! $theme instanceof WP_Theme ) { 237 $theme = wp_get_theme(); 238 } 233 239 $user_cpt = array(); 234 240 $post_type_filter = 'wp_global_styles'; 235 $query = new WP_Query( 236 array( 237 'posts_per_page' => 1, 238 'orderby' => 'date', 239 'order' => 'desc', 240 'post_type' => $post_type_filter, 241 'post_status' => $post_status_filter, 242 'tax_query' => array( 243 array( 244 'taxonomy' => 'wp_theme', 245 'field' => 'name', 246 'terms' => wp_get_theme()->get_stylesheet(), 247 ), 241 $args = array( 242 'numberposts' => 1, 243 'orderby' => 'date', 244 'order' => 'desc', 245 'post_type' => $post_type_filter, 246 'post_status' => $post_status_filter, 247 'tax_query' => array( 248 array( 249 'taxonomy' => 'wp_theme', 250 'field' => 'name', 251 'terms' => $theme->get_stylesheet(), 248 252 ), 249 ) 253 ), 250 254 ); 251 255 252 if ( is_array( $query->posts ) && ( 1 === $query->post_count ) ) { 253 $user_cpt = $query->posts[0]->to_array(); 256 $cache_key = sprintf( 'wp_global_styles_%s', md5( serialize( $args ) ) ); 257 $post_id = wp_cache_get( $cache_key ); 258 259 if ( (int) $post_id > 0 ) { 260 return get_post( $post_id, ARRAY_A ); 261 } 262 263 // Special case: '-1' is a results not found. 264 if ( -1 === $post_id && ! $should_create_cpt ) { 265 return $user_cpt; 266 } 267 268 $recent_posts = wp_get_recent_posts( $args ); 269 if ( is_array( $recent_posts ) && ( count( $recent_posts ) === 1 ) ) { 270 $user_cpt = $recent_posts[0]; 254 271 } elseif ( $should_create_cpt ) { 255 272 $cpt_post_id = wp_insert_post( … … 266 283 true 267 284 ); 268 269 if ( is_wp_error( $cpt_post_id ) ) { 270 $user_cpt = array(); 271 } else { 272 $user_cpt = get_post( $cpt_post_id, ARRAY_A ); 273 } 274 } 285 $user_cpt = get_post( $cpt_post_id, ARRAY_A ); 286 } 287 $cache_expiration = $user_cpt ? DAY_IN_SECONDS : HOUR_IN_SECONDS; 288 wp_cache_set( $cache_key, $user_cpt ? $user_cpt['ID'] : -1, '', $cache_expiration ); 275 289 276 290 return $user_cpt; … … 290 304 291 305 $config = array(); 292 $user_cpt = self::get_user_data_from_custom_post_type( );306 $user_cpt = self::get_user_data_from_custom_post_type( wp_get_theme() ); 293 307 294 308 if ( array_key_exists( 'post_content', $user_cpt ) ) { … … 298 312 if ( JSON_ERROR_NONE !== $json_decoding_error ) { 299 313 trigger_error( 'Error when decoding a theme.json schema for user data. ' . json_last_error_msg() ); 300 return new WP_Theme_JSON( $config, ' user' );314 return new WP_Theme_JSON( $config, 'custom' ); 301 315 } 302 316 … … 312 326 } 313 327 } 314 self::$user = new WP_Theme_JSON( $config, ' user' );328 self::$user = new WP_Theme_JSON( $config, 'custom' ); 315 329 316 330 return self::$user; … … 319 333 /** 320 334 * There are three sources of data (origins) for a site: 321 * default, theme, and user. The user's has higher priority322 * than the theme's, and the theme's higher than core's.335 * default, theme, and custom. The custom's has higher priority 336 * than the theme's, and the theme's higher than default's. 323 337 * 324 338 * Unlike the getters {@link get_core_data}, … … 337 351 * 338 352 * @param string $origin Optional. To what level should we merge data. 339 * Valid values are 'theme' or ' user'.340 * Default is ' user'.353 * Valid values are 'theme' or 'custom'. 354 * Default is 'custom'. 341 355 * @return WP_Theme_JSON 342 356 */ 343 public static function get_merged_data( $origin = ' user' ) {357 public static function get_merged_data( $origin = 'custom' ) { 344 358 if ( is_array( $origin ) ) { 345 359 _deprecated_argument( __FUNCTION__, '5.9' ); … … 350 364 $result->merge( self::get_theme_data() ); 351 365 352 if ( ' user' === $origin ) {366 if ( 'custom' === $origin ) { 353 367 $result->merge( self::get_user_data() ); 354 368 } … … 370 384 } 371 385 372 $user_cpt = self::get_user_data_from_custom_post_type( true );386 $user_cpt = self::get_user_data_from_custom_post_type( wp_get_theme(), true ); 373 387 374 388 if ( array_key_exists( 'ID', $user_cpt ) ) {
Note: See TracChangeset
for help on using the changeset viewer.