diff --git a/wp-modules/pattern-data-handlers/pattern-data-handlers.php b/wp-modules/pattern-data-handlers/pattern-data-handlers.php index d32f876e2..72cc1facd 100644 --- a/wp-modules/pattern-data-handlers/pattern-data-handlers.php +++ b/wp-modules/pattern-data-handlers/pattern-data-handlers.php @@ -314,6 +314,9 @@ function update_pattern( $pattern ) { FS_CHMOD_FILE ); + // Put a flag in the database to indicate this theme has been modified by PM. + update_option( 'pm_mod_' . get_stylesheet(), true ); + // TO DO: Fix issue with needing to "Save twice" on the frontend, because the pattern files are cached on the first save, making images on disk incorrect. return $pattern_file_created; @@ -331,6 +334,9 @@ function delete_pattern( string $pattern_name ): bool { $result = $wp_filesystem && $wp_filesystem->exists( $pattern_path ) && $wp_filesystem->delete( $pattern_path ); tree_shake_theme_images( $wp_filesystem, 'copy_dir' ); + // Put a flag in the database to indicate this theme has been modified by PM. + update_option( 'pm_mod_' . get_stylesheet(), true ); + return $result; } diff --git a/wp-modules/prevent-theme-updates/prevent-theme-updates.php b/wp-modules/prevent-theme-updates/prevent-theme-updates.php new file mode 100644 index 000000000..7ccf03085 --- /dev/null +++ b/wp-modules/prevent-theme-updates/prevent-theme-updates.php @@ -0,0 +1,44 @@ +response ) ) { + return $update_themes_transient_data; + } + + // Loop through each theme that has an update available. + foreach ( $update_themes_transient_data->response as $theme_slug => $theme_with_update_available ) { + $theme_has_been_modified_by_pm = get_option( 'pm_mod_' . $theme_slug ); + + if ( $theme_has_been_modified_by_pm ) { + unset( $update_themes_transient_data->response[ $theme_slug ] ); + } + } + + return $update_themes_transient_data; +} +add_filter( 'site_transient_update_themes', __NAMESPACE__ . '\block_theme_updates_if_modified_by_pm', 10 ); diff --git a/wp-modules/prevent-theme-updates/tests/PreventThemeUpdateTest.php b/wp-modules/prevent-theme-updates/tests/PreventThemeUpdateTest.php new file mode 100644 index 000000000..0d3bc6a8f --- /dev/null +++ b/wp-modules/prevent-theme-updates/tests/PreventThemeUpdateTest.php @@ -0,0 +1,95 @@ +last_checked = 1678397389; + $value->checked = array( + 'twentytwentythree' => 1.1, + 'twentytwentytwo' => 1.4, + ); + $value->response = array( + 'twentytwentythree' => array( + 'theme' => 'twentytwentythree', + 'new_version' => 1.0, + 'url' => 'https://wordpress.org/themes/twentytwentythree/', + 'package' => 'https://downloads.wordpress.org/theme/twentytwentythree.1.0.zip', + 'requires' => 6.1, + 'requires_php' => 5.6, + ), + ); + $value->no_update = array( + 'twentytwentytwo' => array( + 'theme' => 'twentytwentytwo', + 'new_version' => 1.3, + 'url' => 'https://wordpress.org/themes/twentytwentytwo/', + 'package' => 'https://downloads.wordpress.org/theme/twentytwentytwo.1.3.zip', + 'requires' => 5.9, + 'requires_php' => 5.6, + ), + ); + $value->translations = array(); + + $expected = $value; + + $result = \PatternManager\PreventThemeUpdates\block_theme_updates_if_modified_by_pm( $value, 'update_themes' ); + + $this->assertEquals( $expected, $result ); + } + + + /** + * Test that when themes do have PM mods, their updates are prevented. + */ + public function testBlockThemesWithPmMods() { + update_option( 'pm_mod_twentytwentythree', true ); + + $value = new stdClass(); + $value->last_checked = 1678397389; + $value->checked = array( + 'twentytwentythree' => 1.1, + 'twentytwentytwo' => 1.4, + ); + $value->response = array( + 'twentytwentythree' => array( + 'theme' => 'twentytwentythree', + 'new_version' => 1.0, + 'url' => 'https://wordpress.org/themes/twentytwentythree/', + 'package' => 'https://downloads.wordpress.org/theme/twentytwentythree.1.0.zip', + 'requires' => 6.1, + 'requires_php' => 5.6, + ), + ); + $value->no_update = array( + 'twentytwentytwo' => array( + 'theme' => 'twentytwentytwo', + 'new_version' => 1.3, + 'url' => 'https://wordpress.org/themes/twentytwentytwo/', + 'package' => 'https://downloads.wordpress.org/theme/twentytwentytwo.1.3.zip', + 'requires' => 5.9, + 'requires_php' => 5.6, + ), + ); + $value->translations = array(); + + $expected = $value; + $expected->response = array(); + + $result = \PatternManager\PreventThemeUpdates\block_theme_updates_if_modified_by_pm( $value, 'update_themes' ); + + $this->assertEquals( $expected, $result ); + } + +}