diff --git a/.gitignore b/.gitignore index 15876fa47fee8..cb7975f5e0e62 100644 --- a/.gitignore +++ b/.gitignore @@ -33,6 +33,7 @@ wp-tests-config.php /src/wp-admin/css/colors/*/*.css /src/wp-admin/js /src/wp-includes/assets/* +!/src/wp-includes/assets/icon-library-inlined-icons.php !/src/wp-includes/assets/icon-library-manifest.php !/src/wp-includes/assets/script-loader-packages.php !/src/wp-includes/assets/script-modules-packages.php diff --git a/Gruntfile.js b/Gruntfile.js index b720379a23101..5cffd66e4056a 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -74,6 +74,7 @@ module.exports = function(grunt) { // Routes and pages. SOURCE_DIR + 'wp-includes/build', // PHP manifests generated by gutenberg:copy. + SOURCE_DIR + 'wp-includes/assets/icon-library-inlined-icons.php', SOURCE_DIR + 'wp-includes/assets/icon-library-manifest.php', SOURCE_DIR + 'wp-includes/assets/script-loader-packages.php', SOURCE_DIR + 'wp-includes/assets/script-modules-packages.php', @@ -821,6 +822,12 @@ module.exports = function(grunt) { dest: SOURCE_DIR + 'wp-includes/assets/icon-library-manifest.php', } ], }, + 'icon-library-inlined-icons': { + files: [ { + src: 'gutenberg/packages/icons/src/inlined-icons.php', + dest: SOURCE_DIR + 'wp-includes/assets/icon-library-inlined-icons.php', + } ], + }, }, sass: { colors: { @@ -2314,6 +2321,7 @@ module.exports = function(grunt) { 'copy:gutenberg-theme-json', 'copy:icon-library-images', 'copy:icon-library-manifest', + 'copy:icon-library-inlined-icons', ] ); grunt.registerTask( 'build', 'Builds WordPress. Use --dev to build into src/ for development.', function() { diff --git a/src/wp-includes/assets/icon-library-inlined-icons.php b/src/wp-includes/assets/icon-library-inlined-icons.php new file mode 100644 index 0000000000000..1ced4cd65dfef --- /dev/null +++ b/src/wp-includes/assets/icon-library-inlined-icons.php @@ -0,0 +1,28 @@ + '', + 'core-admin/chevron-left' => '', + 'core-admin/comment' => '', + 'core-admin/dashboard' => '', + 'core-admin/home' => '', + 'core-admin/link' => '', + 'core-admin/media' => '', + 'core-admin/menu' => '', + 'core-admin/page' => '', + 'core-admin/pencil' => '', + 'core-admin/people' => '', + 'core-admin/pin' => '', + 'core-admin/plugins' => '', + 'core-admin/plus' => '', + 'core-admin/search' => '', + 'core-admin/settings' => '', + 'core-admin/sites' => '', + 'core-admin/tool' => '', + 'core-admin/update' => '', + 'core-admin/wordpress' => '', +); diff --git a/src/wp-includes/class-wp-icons-registry.php b/src/wp-includes/class-wp-icons-registry.php index 6186a0b48907a..bd6764f872406 100644 --- a/src/wp-includes/class-wp-icons-registry.php +++ b/src/wp-includes/class-wp-icons-registry.php @@ -357,13 +357,31 @@ protected function sanitize_icon_content( $icon_content ) { * Retrieves the content of a registered icon. * * @since 7.0.0 + * @since 7.2.0 The content of some of the bundled icons comes from a generated file. * * @param string $icon_name Icon name including namespace. * @return string|null The content of the icon, if found. */ protected function get_content( $icon_name ) { + static $inlined_icons = null; + if ( ! isset( $this->registered_icons[ $icon_name ]['content'] ) ) { - $file_path = $this->registered_icons[ $icon_name ]['file_path'] ?? ''; + $file_path = $this->registered_icons[ $icon_name ]['file_path'] ?? ''; + + // Some bundled icons have their markup inlined in a generated file, to skip reading and sanitizing their files. + $icons_directory = ABSPATH . WPINC . '/images/icon-library/'; + if ( is_string( $file_path ) && str_starts_with( $file_path, $icons_directory ) ) { + if ( null === $inlined_icons ) { + $inlined_icons_path = ABSPATH . WPINC . '/assets/icon-library-inlined-icons.php'; + $inlined_icons = file_exists( $inlined_icons_path ) ? include $inlined_icons_path : array(); + } + + if ( isset( $inlined_icons[ $icon_name ] ) ) { + $this->registered_icons[ $icon_name ]['content'] = $inlined_icons[ $icon_name ]; + return $inlined_icons[ $icon_name ]; + } + } + $is_stringy = is_string( $file_path ) || ( is_object( $file_path ) && method_exists( $file_path, '__toString' ) ); $icon_path = $is_stringy ? realpath( (string) $file_path ) : false; diff --git a/tests/phpunit/tests/icons/wpIconsRegistry.php b/tests/phpunit/tests/icons/wpIconsRegistry.php index b647a5543b5ec..5afa0f90f6f96 100644 --- a/tests/phpunit/tests/icons/wpIconsRegistry.php +++ b/tests/phpunit/tests/icons/wpIconsRegistry.php @@ -540,6 +540,108 @@ public function test_get_content_returns_null_for_invalid_file( $contents, $exte $this->assertNull( $icon['content'] ); } + /** + * Provides the icons whose markup is inlined in the generated file. + * + * @return array Data sets of [ $icon_name, $content, $file_path ], keyed by icon name. + */ + public function data_inlined_icons(): array { + $manifest = include ABSPATH . WPINC . '/assets/icon-library-manifest.php'; + $inlined = include ABSPATH . WPINC . '/assets/icon-library-inlined-icons.php'; + $data = array(); + + foreach ( $inlined as $icon_name => $content ) { + $slug = explode( '/', $icon_name, 2 )[1]; + $file_path = ABSPATH . WPINC . '/images/icon-library/' . ( $manifest[ $slug ]['filePath'] ?? '' ); + $data[ $icon_name ] = array( $icon_name, $content, $file_path ); + } + + return $data; + } + + /** + * Should only inline icons which exist in the manifest. + * + * The file is generated in Gutenberg and copied over by `npm run grunt build:gutenberg`. + */ + public function test_inlined_icons_exist_in_manifest() { + $manifest = include ABSPATH . WPINC . '/assets/icon-library-manifest.php'; + $inlined = include ABSPATH . WPINC . '/assets/icon-library-inlined-icons.php'; + + $this->assertNotEmpty( $inlined ); + + foreach ( array_keys( $inlined ) as $icon_name ) { + list( $collection, $slug ) = explode( '/', $icon_name, 2 ); + + $this->assertArrayHasKey( $slug, $manifest, "The inlined icon \"$icon_name\" should exist in the manifest." ); + $this->assertContains( $collection, $manifest[ $slug ]['collections'], "The inlined icon \"$icon_name\" should belong to its collection." ); + } + } + + /** + * The inlined markup is not sanitized at runtime, so it should be equivalent + * to the sanitized content of the icon file it was generated from. + * + * @dataProvider data_inlined_icons + * + * @param string $icon_name The namespaced icon name. + * @param string $content The inlined SVG markup of the icon. + * @param string $file_path Absolute path to the icon file. + */ + public function test_inlined_icon_content_matches_sanitized_icon_file( $icon_name, $content, $file_path ) { + $expected = $this->sanitize_icon_content( file_get_contents( $file_path ) ); + + // The generated file has the whitespace between tags removed. + $expected = preg_replace( '/>\s+<', trim( $expected ) ); + + $this->assertEqualHTML( $expected, $content ); + } + + /** + * Should use the inlined markup for a bundled icon, without reading its file. + * + * @covers ::get_content + * + * @dataProvider data_inlined_icons + * + * @param string $icon_name The namespaced icon name. + * @param string $content The inlined SVG markup of the icon. + * @param string $file_path Absolute path to the icon file. + */ + public function test_get_content_uses_inlined_content_for_bundled_icon( $icon_name, $content, $file_path ) { + $this->registry->register( + $icon_name, + array( + 'label' => 'Inlined', + 'file_path' => $file_path, + ) + ); + + $icon = $this->registry->get_registered_icon( $icon_name ); + $this->assertSame( $content, $icon['content'] ); + } + + /** + * Should read the file of an icon registered under an inlined name from another location. + * + * @covers ::get_content + * + */ + public function test_get_content_reads_file_for_core_admin_name_outside_bundled_directory() { + $path = $this->create_temp_icon_file( '' ); + + $this->registry->register( + 'core-admin/dashboard', + array( + 'label' => 'Replacement', + 'file_path' => $path, + ) + ); + + $icon = $this->registry->get_registered_icon( 'core-admin/dashboard' ); + $this->assertEqualHTML( '', $icon['content'] ); + } + /** * Should register an icon that provides a valid `keywords` array. *