From 0fed5f70a175dce1606ddd74bd7ae0ee604b5247 Mon Sep 17 00:00:00 2001 From: Jason Crist Date: Thu, 14 Mar 2024 11:54:37 -0400 Subject: [PATCH] Added tests and logic to uninstall deactivated theme fonts --- admin/create-theme/theme-fonts.php | 41 +++++++++++ admin/resolver_additions.php | 10 ++- tests/test-theme-fonts.php | 108 +++++++++++++++++++++++++++-- 3 files changed, 149 insertions(+), 10 deletions(-) diff --git a/admin/create-theme/theme-fonts.php b/admin/create-theme/theme-fonts.php index ebd40556..0878e1f2 100644 --- a/admin/create-theme/theme-fonts.php +++ b/admin/create-theme/theme-fonts.php @@ -79,6 +79,47 @@ public static function copy_activated_fonts_to_theme() { public static function remove_deactivated_fonts_from_theme() { + $user_settings = MY_Theme_JSON_Resolver::get_user_data()->get_settings(); + $theme_json = MY_Theme_JSON_Resolver::get_theme_file_contents(); + + // If there are no deactivated theme fonts, bounce out + if ( ! isset( $user_settings['typography']['fontFamilies']['theme'] ) ) { + return; + } + + $font_families_to_not_remove = $user_settings['typography']['fontFamilies']['theme']; + + // Remove font assets from theme + $theme_font_asset_location = get_stylesheet_directory() . '/assets/fonts/'; + $font_families_to_remove = array_values( + array_filter( + $theme_json['settings']['typography']['fontFamilies'], + function( $theme_font_family ) use ( $font_families_to_not_remove ) { + return ! in_array( $theme_font_family['slug'], array_column( $font_families_to_not_remove, 'slug' ), true ); + } + ) + ); + foreach ( $font_families_to_remove as $font_family ) { + if ( isset( $font_family['fontFace'] ) ) { + foreach ( $font_family['fontFace'] as $font_face ) { + $font_filename = basename( $font_face['src'] ); + if ( file_exists( $theme_font_asset_location . $font_filename ) ) { + $response = unlink( $theme_font_asset_location . $font_filename ); + } + } + } + } + + // Remove user fonts from theme + $theme_json['settings']['typography']['fontFamilies'] = array_values( + array_filter( + $theme_json['settings']['typography']['fontFamilies'], + function( $theme_font_family ) use ( $font_families_to_not_remove ) { + return in_array( $theme_font_family['slug'], array_column( $font_families_to_not_remove, 'slug' ), true ); + } + ) + ); + MY_Theme_JSON_Resolver::write_theme_file_contents( $theme_json ); } } diff --git a/admin/resolver_additions.php b/admin/resolver_additions.php index c1a26fcc..81beea1c 100644 --- a/admin/resolver_additions.php +++ b/admin/resolver_additions.php @@ -105,9 +105,7 @@ public static function get_theme_file_contents() { public static function write_theme_file_contents( $theme_json_data ) { $theme_json = wp_json_encode( $theme_json_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); file_put_contents( static::get_file_path_from_theme( 'theme.json' ), $theme_json ); - - //TODO: Clearing the cache should clear this too. - static::$theme_json_file_cache = array(); + static::clean_cached_data(); } public static function write_user_settings( $user_settings ) { @@ -118,6 +116,12 @@ public static function write_user_settings( $user_settings ) { static::clean_cached_data(); } + public static function clean_cached_data() { + parent::clean_cached_data(); + //TODO: Clearing the cache should clear this too. + static::$theme_json_file_cache = array(); + } + } } diff --git a/tests/test-theme-fonts.php b/tests/test-theme-fonts.php index b2a7350a..538d6706 100644 --- a/tests/test-theme-fonts.php +++ b/tests/test-theme-fonts.php @@ -31,7 +31,7 @@ public function test_copy_activated_fonts_to_theme() { $test_theme_slug = $this->create_blank_theme(); - $this->activate_font(); + $this->activate_user_font(); $user_data_before = MY_Theme_JSON_Resolver::get_user_data()->get_settings(); $theme_data_before = MY_Theme_JSON_Resolver::get_theme_data()->get_settings(); @@ -41,10 +41,10 @@ public function test_copy_activated_fonts_to_theme() { $user_data_after = MY_Theme_JSON_Resolver::get_user_data()->get_settings(); $theme_data_after = MY_Theme_JSON_Resolver::get_theme_data()->get_settings(); - // Ensure that the font was added and then removed from user space - $this->assertArrayNotHasKey( 'typography', $user_data_begin ); - $this->assertEquals( 'open-sans', $user_data_before['typography']['fontFamilies']['custom'][0]['slug'] ); - $this->assertArrayNotHasKey( 'typography', $user_data_after ); + // ensure that the font was added and then removed from user space + $this->assertarraynothaskey( 'typography', $user_data_begin ); + $this->assertequals( 'open-sans', $user_data_before['typography']['fontFamilies']['custom'][0]['slug'] ); + $this->assertarraynothaskey( 'typography', $user_data_after ); // Ensure that the font was added to the theme $this->assertCount( 1, $theme_data_before['typography']['fontFamilies']['theme'] ); @@ -55,7 +55,55 @@ public function test_copy_activated_fonts_to_theme() { $this->assertEquals( 'file:./assets/fonts/open-sans-normal-400.ttf', $theme_data_after['typography']['fontFamilies']['theme'][1]['fontFace'][0]['src'] ); $this->assertTrue( file_exists( get_stylesheet_directory() . '/assets/fonts/open-sans-normal-400.ttf' ) ); - delete_theme( $test_theme_slug ); + $this->uninstall_theme( $test_theme_slug ); + + } + + private function uninstall_theme( $theme_slug ) { + MY_Theme_JSON_Resolver::write_user_settings( array() ); + delete_theme( $theme_slug ); + } + + public function test_remove_deactivated_fonts_from_theme() { + wp_set_current_user( self::$admin_id ); + + $test_theme_slug = $this->create_blank_theme(); + + $this->activate_font_in_theme_and_override_in_user(); + + $user_data_before = MY_Theme_JSON_Resolver::get_user_data()->get_settings(); + $theme_data_before = MY_Theme_JSON_Resolver::get_theme_data()->get_settings(); + $merged_data_before = MY_Theme_JSON_Resolver::get_merged_data()->get_settings(); + $theme_file_exists_before = file_exists( get_stylesheet_directory() . '/assets/fonts/open-sans-normal-400.ttf' ); + + Theme_Fonts::remove_deactivated_fonts_from_theme(); + + $user_data_after = MY_Theme_JSON_Resolver::get_user_data()->get_settings(); + $theme_data_after = MY_Theme_JSON_Resolver::get_theme_data()->get_settings(); + $merged_data_after = MY_Theme_JSON_Resolver::get_merged_data()->get_settings(); + $theme_file_exists_after = file_exists( get_stylesheet_directory() . '/assets/fonts/open-sans-normal-400.ttf' ); + + // ensure that the font was added to the theme settings and removed in user settings and therefore missing in merged settings + $this->assertCount( 2, $theme_data_before['typography']['fontFamilies']['theme'] ); + $this->assertequals( 'open-sans', $theme_data_before['typography']['fontFamilies']['theme'][1]['slug'] ); + $this->assertCount( 1, $user_data_before['typography']['fontFamilies']['theme'] ); + $this->assertnotequals( 'open-sans', $user_data_before['typography']['fontFamilies']['theme'][0]['slug'] ); + $this->assertCount( 1, $merged_data_before['typography']['fontFamilies']['theme'] ); + $this->assertnotequals( 'open-sans', $merged_data_before['typography']['fontFamilies']['theme'][0]['slug'] ); + + // ensure that the font was removed from the user settings and removed from the theme settings and therefore missing in merged settings + $this->assertCount( 1, $theme_data_after['typography']['fontFamilies']['theme'] ); + $this->assertnotequals( 'open-sans', $theme_data_after['typography']['fontFamilies']['theme'][0]['slug'] ); + $this->assertCount( 1, $user_data_after['typography']['fontFamilies']['theme'] ); + $this->assertnotequals( 'open-sans', $theme_data_after['typography']['fontFamilies']['theme'][0]['slug'] ); + $this->assertCount( 1, $merged_data_after['typography']['fontFamilies']['theme'] ); + $this->assertnotequals( 'open-sans', $merged_data_after['typography']['fontFamilies']['theme'][0]['slug'] ); + + // ensure that the file resource was removed + $this->assertTrue( $theme_file_exists_before ); + $this->assertFalse( $theme_file_exists_after ); + + $this->uninstall_theme( $test_theme_slug ); } private function create_blank_theme() { @@ -75,10 +123,12 @@ private function create_blank_theme() { rest_do_request( $request ); + MY_Theme_JSON_Resolver::clean_cached_data(); + return $test_theme_slug; } - private function activate_font() { + private function activate_user_font() { $font_dir = wp_get_font_dir(); $font_test_url = $font_dir['url'] . '/open-sans-normal-400.ttf'; @@ -110,4 +160,48 @@ private function activate_font() { MY_Theme_JSON_Resolver::write_user_settings( $settings ); } + private function activate_font_in_theme_and_override_in_user() { + + // Copy the font asset + $font_dir = get_stylesheet_directory() . '/assets/fonts/'; + $font_test_source = __DIR__ . '/data/fonts/OpenSans-Regular.ttf'; + $font_test_destination = $font_dir . '/open-sans-normal-400.ttf'; + + if ( ! file_exists( get_stylesheet_directory() . '/assets/' ) ) { + mkdir( get_stylesheet_directory() . '/assets/' ); + } + if ( ! file_exists( get_stylesheet_directory() . '/assets/fonts/' ) ) { + mkdir( get_stylesheet_directory() . '/assets/fonts/' ); + } + + copy( $font_test_source, $font_test_destination ); + + // Add the font to the theme + $theme_json = MY_Theme_JSON_Resolver::get_theme_file_contents(); + $theme_original_font_families = $theme_json['settings']['typography']['fontFamilies']; + $theme_json['settings']['typography']['fontFamilies'][] = array( + 'slug' => 'open-sans', + 'name' => 'Open Sans', + 'fontFamily' => 'Open Sans', + 'fontFace' => array( + array( + 'fontFamily' => 'Open Sans', + 'fontStyle' => 'normal', + 'fontWeight' => '400', + 'src' => 'file:./assets/fonts/open-sans-normal-400.ttf', + ), + ), + ); + MY_Theme_JSON_Resolver::write_theme_file_contents( $theme_json ); + + // Deactivate the test font in the theme. To do this the 'theme' collection + // is overwritten to declare the intention of having it gone. + // Here we're writing the font family settings as they existed BEFORE we added + // the test font family to the theme. + $settings = array(); + $settings['typography']['fontFamilies']['theme'] = $theme_original_font_families; + MY_Theme_JSON_Resolver::write_user_settings( $settings ); + } + + }