Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Block theme updates if they have been modified by PM #104

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

johnstonphilip
Copy link
Contributor

This PR makes it so that if a theme has been modified by Pattern Manager, it will not be available for auto-update within the WordPress dashboard. This is to prevent people accidentally wiping out their patterns.

How to test

  1. Get your WordPress into a state where a theme needs an update, and check the "Dashboard" > "Updates" page to see that an update is available. Do not update the theme though.
  2. Now, add a pattern to the theme, or delete one from the theme.
  3. Re-check your updates page. Notice the update is no longer available.

Before adding a pattern with PM

Screen Shot 2023-03-10 at 2 31 23 PM

After adding a pattern with PM

Screen Shot 2023-03-10 at 2 33 07 PM

Documentation

Suggested changelog entry

Notes

…into try/block-theme-updates-if-pattern-added

# Conflicts:
#	wp-modules/pattern-data-handlers/pattern-data-handlers.php
Copy link
Contributor

@kienstra kienstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @johnstonphilip,
What do you think about waiting to see how many people this is a problem for?

You know I'm a code snob, so maybe I'm overreacting 😆

Now, update_pattern() and delete_pattern() do 3 things:

  1. Saves the pm_mod_ to the DB
  2. Saves a pattern to disk
  3. Tree shakes

It's getting really complicated, and we haven't even released to wp.org.

Thanks for your patience with all of my nitpicking 😄

You want the best for users, and you're working on a lot of approaches.

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 ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about removing this if, and simply blocking theme auto-updates if PM is active?

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, OK.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Choose a reason for hiding this comment

The 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.

@@ -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 );
Copy link
Contributor

@kienstra kienstra Mar 10, 2023

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() and delete_pattern()

$expected = $value;
$expected->response = array();

$result = \PatternManager\PreventThemeUpdates\block_theme_updates_if_modified_by_pm( $value, 'update_themes' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are great unit tests. Very thorough, and very realistic data.

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 ) {
Copy link
Contributor

@kienstra kienstra Mar 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @johnstonphilip,
What do you think about only blocking updates for the active theme when a pm_pattern post exists?

$theme_has_been_modified_by_pm = basename( get_stylesheet_directory() ) === $theme_slug && get_pm_post_ids();

As you know, editing a pattern creates a pm_pattern post.

Still, this will miss this case:

  1. Edit a pattern
  2. Activate another theme
  3. Go back to the theme from step 1
  4. Try to update the active theme via /wp-admin
  5. Expected: this code blocks the theme update
  6. Actual: it doesn't, as the pm_pattern posts were deleted on changing themes

This approach won't require saving any new data.

But your approach is much more complete.

Thanks for being so patient about this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants