wp/wp-includes/class-wp-theme-json-resolver.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
   846 	 * Resolves relative paths in theme.json styles to theme absolute paths
   846 	 * Resolves relative paths in theme.json styles to theme absolute paths
   847 	 * and returns them in an array that can be embedded
   847 	 * and returns them in an array that can be embedded
   848 	 * as the value of `_link` object in REST API responses.
   848 	 * as the value of `_link` object in REST API responses.
   849 	 *
   849 	 *
   850 	 * @since 6.6.0
   850 	 * @since 6.6.0
       
   851 	 * @since 6.7.0 Resolve relative paths in block styles.
   851 	 *
   852 	 *
   852 	 * @param WP_Theme_JSON $theme_json A theme json instance.
   853 	 * @param WP_Theme_JSON $theme_json A theme json instance.
   853 	 * @return array An array of resolved paths.
   854 	 * @return array An array of resolved paths.
   854 	 */
   855 	 */
   855 	public static function get_resolved_theme_uris( $theme_json ) {
   856 	public static function get_resolved_theme_uris( $theme_json ) {
   858 		if ( ! $theme_json instanceof WP_Theme_JSON ) {
   859 		if ( ! $theme_json instanceof WP_Theme_JSON ) {
   859 			return $resolved_theme_uris;
   860 			return $resolved_theme_uris;
   860 		}
   861 		}
   861 
   862 
   862 		$theme_json_data = $theme_json->get_raw_data();
   863 		$theme_json_data = $theme_json->get_raw_data();
   863 
       
   864 		// Top level styles.
       
   865 		$background_image_url = isset( $theme_json_data['styles']['background']['backgroundImage']['url'] ) ? $theme_json_data['styles']['background']['backgroundImage']['url'] : null;
       
   866 
       
   867 		/*
   864 		/*
   868 		 * The same file convention when registering web fonts.
   865 		 * The same file convention when registering web fonts.
   869 		 * See: WP_Font_Face_Resolver::to_theme_file_uri.
   866 		 * See: WP_Font_Face_Resolver::to_theme_file_uri.
   870 		 */
   867 		 */
   871 		$placeholder = 'file:./';
   868 		$placeholder = 'file:./';
       
   869 
       
   870 		// Top level styles.
       
   871 		$background_image_url = $theme_json_data['styles']['background']['backgroundImage']['url'] ?? null;
   872 		if (
   872 		if (
   873 			isset( $background_image_url ) &&
   873 			isset( $background_image_url ) &&
   874 			is_string( $background_image_url ) &&
   874 			is_string( $background_image_url ) &&
   875 			// Skip if the src doesn't start with the placeholder, as there's nothing to replace.
   875 			// Skip if the src doesn't start with the placeholder, as there's nothing to replace.
   876 			str_starts_with( $background_image_url, $placeholder )
   876 			str_starts_with( $background_image_url, $placeholder )
   886 				$resolved_theme_uri['type'] = $file_type['type'];
   886 				$resolved_theme_uri['type'] = $file_type['type'];
   887 			}
   887 			}
   888 			$resolved_theme_uris[] = $resolved_theme_uri;
   888 			$resolved_theme_uris[] = $resolved_theme_uri;
   889 		}
   889 		}
   890 
   890 
       
   891 		// Block styles.
       
   892 		if ( ! empty( $theme_json_data['styles']['blocks'] ) ) {
       
   893 			foreach ( $theme_json_data['styles']['blocks'] as $block_name => $block_styles ) {
       
   894 				if ( ! isset( $block_styles['background']['backgroundImage']['url'] ) ) {
       
   895 					continue;
       
   896 				}
       
   897 				$background_image_url = $block_styles['background']['backgroundImage']['url'];
       
   898 				if (
       
   899 					is_string( $background_image_url ) &&
       
   900 					// Skip if the src doesn't start with the placeholder, as there's nothing to replace.
       
   901 					str_starts_with( $background_image_url, $placeholder )
       
   902 				) {
       
   903 					$file_type          = wp_check_filetype( $background_image_url );
       
   904 					$src_url            = str_replace( $placeholder, '', $background_image_url );
       
   905 					$resolved_theme_uri = array(
       
   906 						'name'   => $background_image_url,
       
   907 						'href'   => sanitize_url( get_theme_file_uri( $src_url ) ),
       
   908 						'target' => "styles.blocks.{$block_name}.background.backgroundImage.url",
       
   909 					);
       
   910 					if ( isset( $file_type['type'] ) ) {
       
   911 						$resolved_theme_uri['type'] = $file_type['type'];
       
   912 					}
       
   913 					$resolved_theme_uris[] = $resolved_theme_uri;
       
   914 				}
       
   915 			}
       
   916 		}
       
   917 
   891 		return $resolved_theme_uris;
   918 		return $resolved_theme_uris;
   892 	}
   919 	}
   893 
   920 
   894 	/**
   921 	/**
   895 	 * Resolves relative paths in theme.json styles to theme absolute paths
   922 	 * Resolves relative paths in theme.json styles to theme absolute paths
   904 		$resolved_urls = static::get_resolved_theme_uris( $theme_json );
   931 		$resolved_urls = static::get_resolved_theme_uris( $theme_json );
   905 		if ( empty( $resolved_urls ) ) {
   932 		if ( empty( $resolved_urls ) ) {
   906 			return $theme_json;
   933 			return $theme_json;
   907 		}
   934 		}
   908 
   935 
   909 		$resolved_theme_json_data = array(
   936 		$resolved_theme_json_data = $theme_json->get_raw_data();
   910 			'version' => WP_Theme_JSON::LATEST_SCHEMA,
       
   911 		);
       
   912 
   937 
   913 		foreach ( $resolved_urls as $resolved_url ) {
   938 		foreach ( $resolved_urls as $resolved_url ) {
   914 			$path = explode( '.', $resolved_url['target'] );
   939 			$path = explode( '.', $resolved_url['target'] );
   915 			_wp_array_set( $resolved_theme_json_data, $path, $resolved_url['href'] );
   940 			_wp_array_set( $resolved_theme_json_data, $path, $resolved_url['href'] );
   916 		}
   941 		}
   917 
   942 
   918 		$theme_json->merge( new WP_Theme_JSON( $resolved_theme_json_data ) );
   943 		return new WP_Theme_JSON( $resolved_theme_json_data );
   919 
       
   920 		return $theme_json;
       
   921 	}
   944 	}
   922 
   945 
   923 	/**
   946 	/**
   924 	 * Adds variations sourced from block style variations files to the supplied theme.json data.
   947 	 * Adds variations sourced from block style variations files to the supplied theme.json data.
   925 	 *
   948 	 *