<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[58353] trunk: Editor (Font Library): Store font subdirectory in post meta.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/58353">58353</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/58353","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>peterwilsoncc</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-06-05 23:17:18 +0000 (Wed, 05 Jun 2024)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Editor (Font Library): Store font subdirectory in post meta.

Stores the font file sub-directory in the `wp_font_face` post meta. Similar to attachments, only the portion of the path relative to the base directory is stored.

This ensures the files can be deleted alongside their post on sites using a plugin to store font files in sub-directories. Previously running such a plugin would result in the files remaining on the file system post delete.

Props costdev, grantmkin, peterwilsoncc.
Fixes <a href="https://core.trac.wordpress.org/ticket/61297">#61297</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesfontsphp">trunk/src/wp-includes/fonts.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestfontfacescontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php</a></li>
<li><a href="#trunktestsphpunittestsfontsfontlibrarywpRestFontFacesControllerphp">trunk/tests/phpunit/tests/fonts/font-library/wpRestFontFacesController.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesfontsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/fonts.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/fonts.php   2024-06-05 19:55:45 UTC (rev 58352)
+++ trunk/src/wp-includes/fonts.php     2024-06-05 23:17:18 UTC (rev 58353)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -230,7 +230,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $font_files = get_post_meta( $post_id, '_wp_font_face_file', false );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $font_dir   = wp_get_font_dir()['path'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $font_dir   = untrailingslashit( wp_get_font_dir()['basedir'] );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        foreach ( $font_files as $font_file ) {
</span><span class="cx" style="display: block; padding: 0 10px">                wp_delete_file( $font_dir . '/' . $font_file );
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestfontfacescontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php  2024-06-05 19:55:45 UTC (rev 58352)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php    2024-06-05 23:17:18 UTC (rev 58353)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -916,8 +916,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $new_path = $path;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $fonts_dir = wp_get_font_dir();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( str_starts_with( $new_path, $fonts_dir['path'] ) ) {
-                       $new_path = str_replace( $fonts_dir, '', $new_path );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( str_starts_with( $new_path, $fonts_dir['basedir'] ) ) {
+                       $new_path = str_replace( $fonts_dir['basedir'], '', $new_path );
</ins><span class="cx" style="display: block; padding: 0 10px">                         $new_path = ltrim( $new_path, '/' );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunktestsphpunittestsfontsfontlibrarywpRestFontFacesControllerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/fonts/font-library/wpRestFontFacesController.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/fonts/font-library/wpRestFontFacesController.php        2024-06-05 19:55:45 UTC (rev 58352)
+++ trunk/tests/phpunit/tests/fonts/font-library/wpRestFontFacesController.php  2024-06-05 23:17:18 UTC (rev 58353)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -397,8 +397,75 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Ensure that setting a subdirectory on font uploads stores and deletes files as expected.
+        *
+        * @ticket 61297
+        *
</ins><span class="cx" style="display: block; padding: 0 10px">          * @covers WP_REST_Font_Faces_Controller::create_item
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        public function test_create_item_sub_dir() {
+               wp_set_current_user( self::$admin_id );
+               add_filter(
+                       'font_dir',
+                       function ( $font_dir ) {
+                               $subdir             = '/subdir';
+                               $font_dir['subdir'] = $subdir;
+                               $font_dir['path']  .= $subdir;
+                               $font_dir['url']   .= $subdir;
+                               return $font_dir;
+                       }
+               );
+
+               $files = $this->setup_font_file_upload( array( 'woff2' ) );
+
+               $request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . self::$font_family_id . '/font-faces' );
+               $request->set_param( 'theme_json_version', WP_REST_Font_Faces_Controller::LATEST_THEME_JSON_VERSION_SUPPORTED );
+               $request->set_param(
+                       'font_face_settings',
+                       wp_json_encode(
+                               array(
+                                       'fontFamily' => '"Open Sans"',
+                                       'fontWeight' => '200',
+                                       'fontStyle'  => 'normal',
+                                       'src'        => array_keys( $files )[0],
+                               )
+                       )
+               );
+               $request->set_file_params( $files );
+
+               $response = rest_get_server()->dispatch( $request );
+               $data     = $response->get_data();
+
+               $this->assertSame( 201, $response->get_status(), 'The response status should be 201.' );
+               $this->check_font_face_data( $data, $data['id'], $response->get_links() );
+               $this->check_file_meta( $data['id'], array( $data['font_face_settings']['src'] ) );
+
+               $settings = $data['font_face_settings'];
+               unset( $settings['src'] );
+               $this->assertSame(
+                       array(
+                               'fontFamily' => '"Open Sans"',
+                               'fontWeight' => '200',
+                               'fontStyle'  => 'normal',
+                       ),
+                       $settings,
+                       'The font_face_settings data should match the expected data.'
+               );
+
+               $expected_file_path = WP_CONTENT_DIR . '/uploads/fonts/subdir/' . reset( $files )['name'];
+               $expected_post_meta = 'subdir/' . reset( $files )['name'];
+               $this->assertFileExists( $expected_file_path, 'The font file should exist in the expected subdirectory.' );
+               $this->assertSame( $expected_post_meta, get_post_meta( $data['id'], '_wp_font_face_file', true ), 'The post meta should match the expected subdirectory.' );
+               $this->assertSame( self::$font_family_id, $data['parent'], 'The returned parent id should match the font family id.' );
+
+               // Delete the post.
+               wp_delete_post( $data['id'], true );
+               $this->assertFileDoesNotExist( $expected_file_path, 'The font file should have been deleted when the post was deleted.' );
+       }
+
+       /**
+        * @covers WP_REST_Font_Faces_Controller::create_item
+        */
</ins><span class="cx" style="display: block; padding: 0 10px">         public function test_create_item_with_multiple_font_files() {
</span><span class="cx" style="display: block; padding: 0 10px">                wp_set_current_user( self::$admin_id );
</span><span class="cx" style="display: block; padding: 0 10px">                $files = $this->setup_font_file_upload( array( 'ttf', 'otf', 'woff', 'woff2' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1048,9 +1115,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">        protected function check_file_meta( $font_face_id, $src_attributes ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $file_meta = get_post_meta( $font_face_id, '_wp_font_face_file' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                foreach ( $src_attributes as $src_attribute ) {
-                       $file_name = basename( $src_attribute );
-                       $this->assertContains( $file_name, $file_meta, 'The uploaded font file path should be saved in the post meta.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         foreach ( $file_meta as $file ) {
+                       $base_directory = wp_get_font_dir()['basedir'];
+                       $this->assertStringStartsNotWith( $base_directory, $file, 'The base directory should not be stored in the post meta.' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre>
</div>
</div>

</body>
</html>