| 1 |
<?php |
| 2 |
/** |
| 3 |
* A special compat layer for legacy font files upload directory. |
| 4 |
* |
| 5 |
* @see https://github.com/WordPress/gutenberg/pull/57688#issuecomment-2259037546 |
| 6 |
* |
| 7 |
* @package gutenberg |
| 8 |
*/ |
| 9 |
|
| 10 |
// @core-merge: Do not merge this function, it is for deleting fonts from the wp-content/fonts directory only used in Gutenberg. |
| 11 |
/** |
| 12 |
* Deletes associated font files from wp-content/fonts, when a font face is deleted. |
| 13 |
* |
| 14 |
* @param int $post_id Post ID. |
| 15 |
* @param WP_Post $post Post object. |
| 16 |
*/ |
| 17 |
function gutenberg_before_delete_font_face( $post_id, $post ) { |
| 18 |
if ( 'wp_font_face' !== $post->post_type ) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
$font_files = get_post_meta( $post_id, '_wp_font_face_file', false ); |
| 23 |
|
| 24 |
if ( empty( $font_files ) ) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
$site_path = ''; |
| 29 |
if ( is_multisite() && ! ( is_main_network() && is_main_site() ) ) { |
| 30 |
$site_path = '/sites/' . get_current_blog_id(); |
| 31 |
} |
| 32 |
|
| 33 |
$font_dir = path_join( WP_CONTENT_DIR, 'fonts' ) . $site_path; |
| 34 |
|
| 35 |
foreach ( $font_files as $font_file ) { |
| 36 |
$font_path = $font_dir . '/' . $font_file; |
| 37 |
|
| 38 |
if ( file_exists( $font_path ) ) { |
| 39 |
wp_delete_file( $font_path ); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
add_action( 'before_delete_post', 'gutenberg_before_delete_font_face', 10, 2 ); |
| 44 |
|