Make WordPress Core

Changeset 57539


Ignore:
Timestamp:
02/06/2024 08:40:38 AM (10 months ago)
Author:
youknowriad
Message:

Editor: Introduce the Font Library post types and low level APIs.

This is the first step towards adding the font library to WordPress.
This commit includes the font library and font face CPTs.
It also adds the necessary APIs and classes to register and manipulate font collections.

This PR backports the font library post types and low level APIs to Core. This is the first step to include the font library entirely into Core. Once this merged, we'll open a PR with the necessary REST API controllers.

Props youknowriad, get_dave, grantmkin, swissspidy, hellofromtonya, mukesh27, mcsf.
See #59166.

Location:
trunk
Files:
19 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/default-filters.php

    r57306 r57539  
    749749// Font management.
    750750add_action( 'wp_head', 'wp_print_font_faces', 50 );
     751add_action( 'deleted_post', '_wp_after_delete_font_family', 10, 2 );
     752add_action( 'before_delete_post', '_wp_before_delete_font_face', 10, 2 );
    751753
    752754unset( $filter, $action );
  • trunk/src/wp-includes/fonts.php

    r57240 r57539  
    5252    $wp_font_face->generate_and_print( $fonts );
    5353}
     54
     55/**
     56 * Registers a new Font Collection in the Font Library.
     57 *
     58 * @since 6.5.0
     59 *
     60 * @param string       $slug Font collection slug. May only contain alphanumeric characters, dashes,
     61 *                     and underscores. See sanitize_title().
     62 * @param array|string $data_or_file {
     63 *     Font collection data array or a path/URL to a JSON file containing the font collection.
     64 *
     65 *     @link https://schemas.wp.org/trunk/font-collection.json
     66 *
     67 *     @type string $name           Required. Name of the font collection shown in the Font Library.
     68 *     @type string $description    Optional. A short descriptive summary of the font collection. Default empty.
     69 *     @type array  $font_families  Required. Array of font family definitions that are in the collection.
     70 *     @type array  $categories     Optional. Array of categories, each with a name and slug, that are used by the
     71 *                                  fonts in the collection. Default empty.
     72 * }
     73 * @return WP_Font_Collection|WP_Error A font collection if it was registered
     74 *                                     successfully, or WP_Error object on failure.
     75 */
     76function wp_register_font_collection( $slug, $data_or_file ) {
     77    return WP_Font_Library::get_instance()->register_font_collection( $slug, $data_or_file );
     78}
     79
     80/**
     81 * Unregisters a font collection from the Font Library.
     82 *
     83 * @since 6.5.0
     84 *
     85 * @param string $slug Font collection slug.
     86 * @return bool True if the font collection was unregistered successfully, else false.
     87 */
     88function wp_unregister_font_collection( $slug ) {
     89    return WP_Font_Library::get_instance()->unregister_font_collection( $slug );
     90}
     91
     92/**
     93 * Returns an array containing the current fonts upload directory's path and URL.
     94 *
     95 * @since 6.5.0
     96 *
     97 * @param array $defaults {
     98 *     Array of information about the upload directory.
     99 *
     100 *     @type string       $path    Base directory and subdirectory or full path to the fonts upload directory.
     101 *     @type string       $url     Base URL and subdirectory or absolute URL to the fonts upload directory.
     102 *     @type string       $subdir  Subdirectory
     103 *     @type string       $basedir Path without subdir.
     104 *     @type string       $baseurl URL path without subdir.
     105 *     @type string|false $error   False or error message.
     106 * }
     107 * @return array $defaults {
     108 *     Array of information about the upload directory.
     109 *
     110 *     @type string       $path    Base directory and subdirectory or full path to the fonts upload directory.
     111 *     @type string       $url     Base URL and subdirectory or absolute URL to the fonts upload directory.
     112 *     @type string       $subdir  Subdirectory
     113 *     @type string       $basedir Path without subdir.
     114 *     @type string       $baseurl URL path without subdir.
     115 *     @type string|false $error   False or error message.
     116 * }
     117 */
     118function wp_get_font_dir( $defaults = array() ) {
     119    $site_path = '';
     120    if ( is_multisite() && ! ( is_main_network() && is_main_site() ) ) {
     121        $site_path = '/sites/' . get_current_blog_id();
     122    }
     123
     124    // Sets the defaults.
     125    $defaults['path']    = path_join( WP_CONTENT_DIR, 'fonts' ) . $site_path;
     126    $defaults['url']     = untrailingslashit( content_url( 'fonts' ) ) . $site_path;
     127    $defaults['subdir']  = '';
     128    $defaults['basedir'] = path_join( WP_CONTENT_DIR, 'fonts' ) . $site_path;
     129    $defaults['baseurl'] = untrailingslashit( content_url( 'fonts' ) ) . $site_path;
     130    $defaults['error']   = false;
     131
     132    /**
     133     * Filters the fonts directory data.
     134     *
     135     * This filter allows developers to modify the fonts directory data.
     136     *
     137     * @since 6.5.0
     138     *
     139     * @param array $defaults The original fonts directory data.
     140     */
     141    return apply_filters( 'font_dir', $defaults );
     142}
     143
     144/**
     145 * Deletes child font faces when a font family is deleted.
     146 *
     147 * @access private
     148 * @since 6.5.0
     149 *
     150 * @param int     $post_id Post ID.
     151 * @param WP_Post $post    Post object.
     152 */
     153function _wp_after_delete_font_family( $post_id, $post ) {
     154    if ( 'wp_font_family' !== $post->post_type ) {
     155        return;
     156    }
     157
     158    $font_faces = get_children(
     159        array(
     160            'post_parent' => $post_id,
     161            'post_type'   => 'wp_font_face',
     162        )
     163    );
     164
     165    foreach ( $font_faces as $font_face ) {
     166        wp_delete_post( $font_face->ID, true );
     167    }
     168}
     169
     170/**
     171 * Deletes associated font files when a font face is deleted.
     172 *
     173 * @access private
     174 * @since 6.5.0
     175 *
     176 * @param int     $post_id Post ID.
     177 * @param WP_Post $post    Post object.
     178 */
     179function _wp_before_delete_font_face( $post_id, $post ) {
     180    if ( 'wp_font_face' !== $post->post_type ) {
     181        return;
     182    }
     183
     184    $font_files = get_post_meta( $post_id, '_wp_font_face_file', false );
     185    $font_dir   = wp_get_font_dir()['path'];
     186
     187    foreach ( $font_files as $font_file ) {
     188        wp_delete_file( $font_dir . '/' . $font_file );
     189    }
     190}
  • trunk/src/wp-includes/post.php

    r57524 r57539  
    562562                'revisions',
    563563            ),
     564        )
     565    );
     566
     567    register_post_type(
     568        'wp_font_family',
     569        array(
     570            'labels'                         => array(
     571                'name'          => __( 'Font Families' ),
     572                'singular_name' => __( 'Font Family' ),
     573            ),
     574            'public'                         => false,
     575            '_builtin'                       => true, /* internal use only. don't use this when registering your own post type. */
     576            'hierarchical'                   => false,
     577            'capabilities'                   => array(
     578                'read'                   => 'edit_theme_options',
     579                'read_private_posts'     => 'edit_theme_options',
     580                'create_posts'           => 'edit_theme_options',
     581                'publish_posts'          => 'edit_theme_options',
     582                'edit_posts'             => 'edit_theme_options',
     583                'edit_others_posts'      => 'edit_theme_options',
     584                'edit_published_posts'   => 'edit_theme_options',
     585                'delete_posts'           => 'edit_theme_options',
     586                'delete_others_posts'    => 'edit_theme_options',
     587                'delete_published_posts' => 'edit_theme_options',
     588            ),
     589            'map_meta_cap'                   => true,
     590            'query_var'                      => false,
     591            'show_in_rest'                   => false,
     592            'rewrite'                        => false,
     593        )
     594    );
     595
     596    register_post_type(
     597        'wp_font_face',
     598        array(
     599            'labels'                         => array(
     600                'name'          => __( 'Font Faces' ),
     601                'singular_name' => __( 'Font Face' ),
     602            ),
     603            'public'                         => false,
     604            '_builtin'                       => true, /* internal use only. don't use this when registering your own post type. */
     605            'hierarchical'                   => false,
     606            'capabilities'                   => array(
     607                'read'                   => 'edit_theme_options',
     608                'read_private_posts'     => 'edit_theme_options',
     609                'create_posts'           => 'edit_theme_options',
     610                'publish_posts'          => 'edit_theme_options',
     611                'edit_posts'             => 'edit_theme_options',
     612                'edit_others_posts'      => 'edit_theme_options',
     613                'edit_published_posts'   => 'edit_theme_options',
     614                'delete_posts'           => 'edit_theme_options',
     615                'delete_others_posts'    => 'edit_theme_options',
     616                'delete_published_posts' => 'edit_theme_options',
     617            ),
     618            'map_meta_cap'                   => true,
     619            'query_var'                      => false,
     620            'show_in_rest'                   => false,
     621            'rewrite'                        => false,
    564622        )
    565623    );
  • trunk/src/wp-settings.php

    r57526 r57539  
    375375require ABSPATH . WPINC . '/style-engine/class-wp-style-engine-processor.php';
    376376require ABSPATH . WPINC . '/fonts/class-wp-font-face-resolver.php';
     377require ABSPATH . WPINC . '/fonts/class-wp-font-collection.php';
    377378require ABSPATH . WPINC . '/fonts/class-wp-font-face.php';
     379require ABSPATH . WPINC . '/fonts/class-wp-font-library.php';
     380require ABSPATH . WPINC . '/fonts/class-wp-font-utils.php';
    378381require ABSPATH . WPINC . '/fonts.php';
    379382require ABSPATH . WPINC . '/class-wp-script-modules.php';
Note: See TracChangeset for help on using the changeset viewer.