-
Notifications
You must be signed in to change notification settings - Fork 6
Block theme updates if they have been modified by PM #104
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* Module Name: Prevent Theme Updates | ||
* Description: This module will prevent a theme from being updated if Pattern Manager has modified it. | ||
* Namespace: PreventThemeUpdates | ||
* | ||
* @package pattern-manager | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PatternManager\PreventThemeUpdates; | ||
|
||
// Exit if accessed directly. | ||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; | ||
} | ||
|
||
/** | ||
* Prevent a theme update if it's been modified by Pattern Manager. | ||
* | ||
* @param mixed $update_themes_transient_data Value of site transient. | ||
* @param string $transient Transient name. | ||
* @return string | ||
*/ | ||
function block_theme_updates_if_modified_by_pm( $update_themes_transient_data ) { | ||
|
||
// No update object exists. Return early. | ||
if ( empty( $update_themes_transient_data ) || ! isset( $update_themes_transient_data->response ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about removing this That'll probably anger some people. But they're not going to benefit from PM anyway, and we could notify them on plugin activation that we're blocking theme auto-updates. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can see the argument. Maybe a reason not to (and keep it how it is) is that someone might have another theme where they are testing update functionality with some custom server, and this would have them waste a lot of time trying to figure out why "their update code isn't working". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, OK. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Side, note: how many people are auto-updating in their local? PM is for local development. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would offer that updates likely start in Local. So devs can test and make sure things are not breaking. |
||
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 ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @johnstonphilip, $theme_has_been_modified_by_pm = basename( get_stylesheet_directory() ) === $theme_slug && get_pm_post_ids(); As you know, editing a pattern creates a Still, this will miss this case:
This approach won't require saving any new data. But your approach is much more complete. Thanks for being so patient about this. |
||
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 ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
/** | ||
* Class PatternManagerPreventThemeUpdateTest | ||
* | ||
* @package pattern-manager | ||
*/ | ||
|
||
/** | ||
* Test this module's functions. | ||
*/ | ||
class PatternManagerPreventThemeUpdateTest extends WP_UnitTestCase { | ||
|
||
/** | ||
* Test that when themes do not have PM mods, their updates are not prevented. | ||
*/ | ||
public function testDoNotBlockThemesWithNoPmMods() { | ||
$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; | ||
|
||
$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' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are great unit tests. Very thorough, and very realistic data. |
||
|
||
$this->assertEquals( $expected, $result ); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're going to do this PR, would it be possible to move this into a helper function? So ideally it's only adding 1 line to
update_pattern()
anddelete_pattern()