Changeset 57868
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/fonts.php
r57740 r57868 93 93 94 94 /** 95 * Retrieves font uploads directory information. 96 * 97 * Same as wp_font_dir() but "light weight" as it doesn't attempt to create the font uploads directory. 98 * Intended for use in themes, when only 'basedir' and 'baseurl' are needed, generally in all cases 99 * when not uploading files. 100 * 101 * @since 6.5.0 102 * 103 * @see wp_font_dir() 104 * 105 * @return array See wp_font_dir() for description. 106 */ 107 function wp_get_font_dir() { 108 return wp_font_dir( false ); 109 } 110 111 /** 95 112 * Returns an array containing the current fonts upload directory's path and URL. 96 113 * 97 114 * @since 6.5.0 98 115 * 99 * @return array $defaults { 100 * Array of information about the upload directory. 116 * @param bool $create_dir Optional. Whether to check and create the font uploads directory. Default true. 117 * @return array { 118 * Array of information about the font upload directory. 101 119 * 102 120 * @type string $path Base directory and subdirectory or full path to the fonts upload directory. … … 108 126 * } 109 127 */ 110 function wp_get_font_dir() { 128 function wp_font_dir( $create_dir = true ) { 129 /* 130 * Allow extenders to manipulate the font directory consistently. 131 * 132 * Ensures the upload_dir filter is fired both when calling this function 133 * directly and when the upload directory is filtered in the Font Face 134 * REST API endpoint. 135 */ 136 add_filter( 'upload_dir', '_wp_filter_font_directory' ); 137 $font_dir = wp_upload_dir( null, $create_dir, false ); 138 remove_filter( 'upload_dir', '_wp_filter_font_directory' ); 139 return $font_dir; 140 } 141 142 /** 143 * Returns the font directory for use by the font library. 144 * 145 * This function is a callback for the {@see 'upload_dir'} filter. It is not 146 * intended to be called directly. Use wp_get_font_dir() instead. 147 * 148 * The function can be used when extending the font library to modify the upload 149 * destination for font files via the upload_dir filter. The recommended way to 150 * do this is: 151 * 152 * ```php 153 * add_filter( 'upload_dir', '_wp_filter_font_directory' ); 154 * // Your code to upload or sideload a font file. 155 * remove_filter( 'upload_dir', '_wp_filter_font_directory' ); 156 * ``` 157 * 158 * @since 6.5.0 159 * @access private 160 * 161 * @param string $font_dir The font directory. 162 * @return string The modified font directory. 163 */ 164 function _wp_filter_font_directory( $font_dir ) { 165 if ( doing_filter( 'font_dir' ) ) { 166 // Avoid an infinite loop. 167 return $font_dir; 168 } 169 111 170 $site_path = ''; 112 171 if ( is_multisite() && ! ( is_main_network() && is_main_site() ) ) { … … 114 173 } 115 174 116 $ defaults= array(175 $font_dir = array( 117 176 'path' => path_join( WP_CONTENT_DIR, 'fonts' ) . $site_path, 118 177 'url' => untrailingslashit( content_url( 'fonts' ) ) . $site_path, … … 130 189 * @since 6.5.0 131 190 * 132 * @param array $defaults The original fonts directory data. 191 * @param array $font_dir { 192 * Array of information about the font upload directory. 193 * 194 * @type string $path Base directory and subdirectory or full path to the fonts upload directory. 195 * @type string $url Base URL and subdirectory or absolute URL to the fonts upload directory. 196 * @type string $subdir Subdirectory 197 * @type string $basedir Path without subdir. 198 * @type string $baseurl URL path without subdir. 199 * @type string|false $error False or error message. 200 * } 133 201 */ 134 return apply_filters( 'font_dir', $ defaults);202 return apply_filters( 'font_dir', $font_dir ); 135 203 } 136 204 -
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php
r57804 r57868 857 857 protected function handle_font_file_upload( $file ) { 858 858 add_filter( 'upload_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) ); 859 860 /* 861 * Set the upload directory to the fonts directory. 862 * 863 * wp_get_font_dir() contains the 'font_dir' hook, whose callbacks are 864 * likely to call wp_get_upload_dir(). 865 * 866 * To avoid an infinite loop, don't hook wp_get_font_dir() to 'upload_dir'. 867 * Instead, just pass its return value to the 'upload_dir' callback. 868 */ 869 $font_dir = wp_get_font_dir(); 870 $set_upload_dir = function () use ( $font_dir ) { 871 return $font_dir; 872 }; 873 add_filter( 'upload_dir', $set_upload_dir ); 859 // Filter the upload directory to return the fonts directory. 860 add_filter( 'upload_dir', '_wp_filter_font_directory' ); 874 861 875 862 $overrides = array( … … 888 875 $uploaded_file = wp_handle_upload( $file, $overrides ); 889 876 890 remove_filter( 'upload_dir', $set_upload_dir);877 remove_filter( 'upload_dir', '_wp_filter_font_directory' ); 891 878 remove_filter( 'upload_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) ); 892 879 -
trunk/tests/phpunit/tests/fonts/font-library/fontLibraryHooks.php
r57539 r57868 74 74 75 75 add_filter( 'upload_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) ); 76 add_filter( 'upload_dir', ' wp_get_font_dir' );76 add_filter( 'upload_dir', '_wp_filter_font_directory' ); 77 77 $font_file = wp_upload_bits( 78 78 $font_filename, … … 80 80 file_get_contents( $font_file_path ) 81 81 ); 82 remove_filter( 'upload_dir', ' wp_get_font_dir' );82 remove_filter( 'upload_dir', '_wp_filter_font_directory' ); 83 83 remove_filter( 'upload_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) ); 84 84 -
trunk/tests/phpunit/tests/fonts/font-library/wpFontsDir.php
r57539 r57868 70 70 $this->assertSame( static::$dir_defaults, $font_dir, 'The wp_get_font_dir() method should return the default values.' ); 71 71 } 72 73 /** 74 * @ticket 60652 75 */ 76 public function test_fonts_dir_filters_do_not_trigger_infinite_loop() { 77 /* 78 * Naive filtering of uploads directory to return font directory. 79 * 80 * This emulates the approach a plugin developer may take to 81 * add the filter when extending the font library functionality. 82 */ 83 add_filter( 'upload_dir', '_wp_filter_font_directory' ); 84 85 add_filter( 86 'upload_dir', 87 function ( $upload_dir ) { 88 static $count = 0; 89 ++$count; 90 // The filter may be applied a couple of times, at five iterations assume an infinite loop. 91 if ( $count >= 5 ) { 92 $this->fail( 'Filtering the uploads directory triggered an infinite loop.' ); 93 } 94 return $upload_dir; 95 }, 96 5 97 ); 98 99 /* 100 * Filter the font directory to return the uploads directory. 101 * 102 * This emulates moving font files back to the uploads directory due 103 * to file system structure. 104 */ 105 add_filter( 'font_dir', 'wp_get_upload_dir' ); 106 107 wp_get_upload_dir(); 108 109 // This will never be hit if an infinite loop is triggered. 110 $this->assertTrue( true ); 111 } 72 112 }
Note: See TracChangeset
for help on using the changeset viewer.