ster_font_collection( $slug ); } /** * Retrieves font uploads directory information. * * Same as wp_font_dir() but "light weight" as it doesn't attempt to create the font uploads directory. * Intended for use in themes, when only 'basedir' and 'baseurl' are needed, generally in all cases * when not uploading files. * * @since 6.5.0 * * @see wp_font_dir() * * @return array See wp_font_dir() for description. */ function wp_get_font_dir() { return wp_font_dir( false ); } /** * Returns an array containing the current fonts upload directory's path and URL. * * @since 6.5.0 * * @param bool $create_dir Optional. Whether to check and create the font uploads directory. Default true. * @return array { * Array of information about the font upload directory. * * @type string $path Base directory and subdirectory or full path to the fonts upload directory. * @type string $url Base URL and subdirectory or absolute URL to the fonts upload directory. * @type string $subdir Subdirectory * @type string $basedir Path without subdir. * @type string $baseurl URL path without subdir. * @type string|false $error False or error message. * } */ function wp_font_dir( $create_dir = true ) { /* * Allow extenders to manipulate the font directory consistently. * * Ensures the upload_dir filter is fired both when calling this function * directly and when the upload directory is filtered in the Font Face * REST API endpoint. */ add_filter( 'upload_dir', '_wp_filter_font_directory' ); $font_dir = wp_upload_dir( null, $create_dir, false ); remove_filter( 'upload_dir', '_wp_filter_font_directory' ); return $font_dir; } /** * A callback function for use in the {@see 'upload_dir'} filter. * * This function is intended for internal use only and should not be used by plugins and themes. * Use wp_get_font_dir() instead. * * @since 6.5.0 * @access private * * @param string $font_dir The font directory. * @return string The modified font directory. */ function _wp_filter_font_directory( $font_dir ) { if ( doing_filter( 'font_dir' ) ) { // Avoid an infinite loop. return $font_dir; } $font_dir = array( 'path' => untrailingslashit( $font_dir['basedir'] ) . '/fonts', 'url' => untrailingslashit( $font_dir['baseurl'] ) . '/fonts', 'subdir' => '', 'basedir' => untrailingslashit( $font_dir['basedir'] ) . '/fonts', 'baseurl' => untrailingslashit( $font_dir['baseurl'] ) . '/fonts', 'error' => false, ); /** * Filters the fonts directory data. * * This filter allows developers to modify the fonts directory data. * * @since 6.5.0 * * @param array $font_dir { * Array of information about the font upload directory. * * @type string $path Base directory and subdirectory or full path to the fonts upload directory. * @type string $url Base URL and subdirectory or absolute URL to the fonts upload directory. * @type string $subdir Subdirectory * @type string $basedir Path without subdir. * @type string $baseurl URL path without subdir. * @type string|false $error False or error message. * } */ return apply_filters( 'font_dir', $font_dir ); } /** * Deletes child font faces when a font family is deleted. * * @access private * @since 6.5.0 * * @param int $post_id Post ID. * @param WP_Post $post Post object. */ function _wp_after_delete_font_family( $post_id, $post ) { if ( 'wp_font_family' !== $post->post_type ) { return; } $font_faces = get_children( array( 'post_parent' => $post_id, 'post_type' => 'wp_font_face', ) ); foreach ( $font_faces as $font_face ) { wp_delete_post( $font_face->ID, true ); } } /** * Deletes associated font files when a font face is deleted. * * @access private * @since 6.5.0 * * @param int $post_id Post ID. * @param WP_Post $post Post object. */ function _wp_before_delete_font_face( $post_id, $post ) { if ( 'wp_font_face' !== $post->post_type ) { return; } $font_files = get_post_meta( $post_id, '_wp_font_face_file', false ); $font_dir = untrailingslashit( wp_get_font_dir()['basedir'] ); foreach ( $font_files as $font_file ) { wp_delete_file( $font_dir . '/' . $font_file ); } } /** * Register the default font collections. * * @access private * @since 6.5.0 */ function _wp_register_default_font_collections() { wp_register_font_collection( 'google-fonts', array( 'name' => _x( 'Google Fonts', 'font collection name' ), 'description' => __( 'Install from Google Fonts. Fonts are copied to and served from your site.' ), 'font_families' => 'https://s.w.org/images/fonts/wp-6.5/collections/google-fonts-with-preview.json', 'categories' => array( array( 'name' => _x( 'Sans Serif', 'font category' ), 'slug' => 'sans-serif', ), array( 'name' => _x( 'Display', 'font category' ), 'slug' => 'display', ), array( 'name' => _x( 'Serif', 'font category' ), 'slug' => 'serif', ), array( 'name' => _x( 'Handwriting', 'font category' ), 'slug' => 'handwriting', ), array( 'name' => _x( 'Monospace', 'font category' ), 'slug' => 'monospace', ), ), ) ); } * If a src is provided and the script module has not been registered yet, it * will be registered. * * @since 6.5.0 * * @param string $id The identifier of the script module. Should be unique. It will be used in the * final import map. * @param string $src Optional. Full URL of the script module, or path of the script module relative * to the WordPress root directory. If it is provided and the script module has * not been registered yet, it will be registered. * @param array $deps { * Optional. List of dependencies. * * @type string|array ...$0 { * An array of script module identifiers of the dependencies of this script * module. The dependencies can be strings or arrays. If they are arrays, * they need an `id` key with the script module identifier, and can contain * an `import` key with either `static` or `dynamic`. By default, * dependencies that don't contain an `import` key are considered static. * * @type string $id The script module identifier. * @type string $import Optional. Import type. May be either `static` or * `dynamic`. Defaults to `static`. * } * } * @param string|false|null $version Optional. String specifying the script module version number. Defaults to false. * It is added to the URL as a query string for cache busting purposes. If $version * is set to false, the version number is the currently installed WordPress version. * If $version is set to null, no version is added. */ public function enqueue( string $id, string $src = '', array $deps = array(), $version = false ) { if ( isset( $this->registered[ $id ] ) ) { $this->registered[ $id ]['enqueue'] = true; } elseif ( $src ) { $this->register( $id, $src, $deps, $version ); $this->registered[ $id ]['enqueue'] = true; } else { $this->enqueued_before_registered[ $id ] = true; } } /** * Unmarks the script module so it will no longer be enqueued in the page. * * @since 6.5.0 * * @param string $id The identifier of the script module. */ public function dequeue( string $id ) { if ( isset( $this->registered[ $id ] ) ) { $this->registered[ $id ]['enqueue'] = false; } unset( $this->enqueued_before_registered[ $id ] ); } /** * Removes a registered script module. * * @since 6.5.0 * * @param string $id The identifier of the script module. */ public function deregister( string $id ) { unset( $this->registered[ $id ] ); unset( $this->enqueued_before_registered[ $id ] ); } /** * Adds the hooks to print the import map, enqueued script modules and script * module preloads. * * In classic themes, the script modules used by the blocks are not yet known * when the `wp_head` actions is fired, so it needs to print everything in the * footer. * * @since 6.5.0 */ public function add_hooks() { $position = wp_is_block_theme() ? 'wp_head' : 'wp_footer'; add_action( $position, array( $this, 'print_import_map' ) ); add_action( $position, array( $this, 'print_enqueued_script_modules' ) ); add_action( $position, array( $this, 'print_script_module_preloads' ) ); add_action( 'admin_print_footer_scripts', array( $this, 'print_import_map' ) ); add_action( 'admin_print_footer_scripts', array( $this, 'print_enqueued_script_modules' ) ); add_action( 'admin_print_footer_scripts', array( $this, 'print_script_module_preloads' ) ); } /** * Prints the enqueued script modules using script tags with type="module" * attributes. * * @since 6.5.0 */ public function print_enqueued_script_modules() { foreach ( $this->get_marked_for_enqueue() as $id => $script_module ) { wp_print_script_tag( array( 'type' => 'module', 'src' => $this->get_src( $id ), 'id' => $id . '-js-module', ) ); } } /** * Prints the the static dependencies of the enqueued script modules using * link tags with rel="modulepreload" attributes. * * If a script module is marked for enqueue, it will not be preloaded. * * @since 6.5.0 */ public function print_script_module_preloads() { foreach ( $this->get_dependencies( array_keys( $this->get_marked_for_enqueue() ), array( 'static' ) ) as $id => $script_module ) { // Don't preload if it's marked for enqueue. if ( true !== $script_module['enqueue'] ) { echo sprintf( '', esc_url( $this->get_src( $id ) ), esc_attr( $id . '-js-modulepreload' ) ); } } } /** * Prints the import map using a script tag with a type="importmap" attribute. * * @since 6.5.0 * * @global WP_Scripts $wp_scripts The WP_Scripts object for printing the polyfill. */ public function print_import_map() { $import_map = $this->get_import_map(); if ( ! empty( $import_map['imports'] ) ) { global $wp_scripts; if ( isset( $wp_scripts ) ) { wp_print_inline_script_tag( wp_get_script_polyfill( $wp_scripts, array( 'HTMLScriptElement.supports && HTMLScriptElement.supports("importmap")' => 'wp-polyfill-importmap', ) ), array( 'id' => 'wp-load-polyfill-importmap', ) ); } wp_print_inline_script_tag( wp_json_encode( $import_map, JSON_HEX_TAG | JSON_HEX_AMP ), array( 'type' => 'importmap', 'id' => 'wp-importmap', ) ); } } /** * Returns the import map array. * * @since 6.5.0 * * @return array Array with an `imports` key mapping to an array of script module identifiers and their respective * URLs, including the version query. */ private function get_import_map(): array { $imports = array(); foreach ( $this->get_dependencies( array_keys( $this->get_marked_for_enqueue() ) ) as $id => $script_module ) { $imports[ $id ] = $this->get_src( $id ); } return array( 'imports' => $imports ); } /** * Retrieves the list of script modules marked for enqueue. * * @since 6.5.0 * * @return array[] Script modules marked for enqueue, keyed by script module identifier. */ private function get_marked_for_enqueue(): array { $enqueued = array(); foreach ( $this->registered as $id => $script_module ) { if ( true === $script_module['enqueue'] ) { $enqueued[ $id ] = $script_module; } } return $enqueued; } /** * Retrieves all the dependencies for the given script module identifiers, * filtered by import types. * * It will consolidate an array containing a set of unique dependencies based * on the requested import types: 'static', 'dynamic', or both. This method is * recursive and also retrieves dependencies of the dependencies. * * @since 6.5.0 * * @param string[] $ids The identifiers of the script modules for which to gather dependencies. * @param string[] $import_types Optional. Import types of dependencies to retrieve: 'static', 'dynamic', or both. * Default is both. * @return array[] List of dependencies, keyed by script module identifier. */ private function get_dependencies( array $ids, array $import_types = array( 'static', 'dynamic' ) ) { return array_reduce( $ids, function ( $dependency_script_modules, $id ) use ( $import_types ) { $dependencies = array(); foreach ( $this->registered[ $id ]['dependencies'] as $dependency ) { if ( in_array( $dependency['import'], $import_types, true ) && isset( $this->registered[ $dependency['id'] ] ) && ! isset( $dependency_script_modules[ $dependency['id'] ] ) ) { $dependencies[ $dependency['id'] ] = $this->registered[ $dependency['id'] ]; } } return array_merge( $dependency_script_modules, $dependencies, $this->get_dependencies( array_keys( $dependencies ), $import_types ) ); }, array() ); } /** * Gets the versioned URL for a script module src. * * If $version is set to false, the version number is the currently installed * WordPress version. If $version is set to null, no version is added. * Otherwise, the string passed in $version is used. * * @since 6.5.0 * * @param string $id The script module identifier. * @return string The script module src with a version if relevant. */ private function get_src( string $id ): string { if ( ! isset( $this->registered[ $id ] ) ) { return ''; } $script_module = $this->registered[ $id ]; $src = $script_module['src']; if ( false === $script_module['version'] ) { $src = add_query_arg( 'ver', get_bloginfo( 'version' ), $src ); } elseif ( null !== $script_module['version'] ) { $src = add_query_arg( 'ver', $script_module['version'], $src ); } /** * Filters the script module source. * * @since 6.5.0 * * @param string $src Module source URL. * @param string $id Module identifier. */ $src = apply_filters( 'script_module_loader_src', $src, $id ); return $src; } }