-
Notifications
You must be signed in to change notification settings - Fork 5
Conversation
This reverts commit d393f4b.
// Spin up the filesystem api. | ||
$wp_filesystem = \PatternManager\GetWpFilesystem\get_wp_filesystem_api(); | ||
|
||
function tree_shake_theme_images( $wp_filesystem, $copy_dir ) { |
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.
Adding 2 parameters isn't good.
But it allows testing this function.
Mocking the $wp_filesystem
global was really hard, I couldn't figure it out.
@@ -401,7 +402,7 @@ function tree_shake_theme_images() { | |||
} | |||
|
|||
// Before we take any action, back up the current images directory. | |||
copy_dir( $images_dir, $backedup_images_dir ); | |||
call_user_func( $copy_dir, $images_dir, $backedup_images_dir ); |
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.
In production, this line should work the same as before.
The copy_dir
function is passed as $copy_dir
.
Before, this line blocked testing tree_shake_theme_images()
.
I couldn't figure out a way around it.
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.
Seems like a minor tradeoff to enable testing.
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.
Thanks!
[ | ||
$this->get_fixtures_directory() . '/patterns/images/used.jpg', | ||
], | ||
$wp_filesystem->get_copied() |
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.
This tests that it tree shakes the unused image (not-used.jpg
) out of fixtures/patterns/images/
.
But it keeps the used image (used.jpg
).
f93f30f
to
bf3bb5f
Compare
@@ -79,6 +81,8 @@ function save_pattern_to_file( WP_Post $post ) { | |||
); | |||
|
|||
add_action( 'rest_after_insert_' . get_pattern_post_type(), __NAMESPACE__ . '\save_pattern_to_file' ); | |||
|
|||
tree_shake_theme_images( get_wp_filesystem_api(), 'copy_dir' ); |
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.
Before, tree_shake_theme_images()
ran in update_pattern()
.
And that ran on post and meta updates.
Now, this only runs on post updates.
post_content
should only change on post updates, and that's the only thing tree shaking applies to.
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.
Maybe tree shaking should also run on delete_pattern()
, but I like the simplicity 😄
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.
It could be a nice touch to also run on delete_pattern
imo — not a huge thing by any means, but I think it would make sense to destroy the asset along with the pattern file.
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.
That's a good point.
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.
Applied in 8534c8e
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.
Awesome, this is working well! Images are deleted along with the pattern file as expected.
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.
Thanks for testing this
@@ -116,6 +116,8 @@ function format_pattern_data( $pattern_data, $file ) { | |||
$pattern_data['description'] = translate_with_gettext_context( $pattern_data['description'], 'Pattern description', $text_domain ); | |||
} | |||
|
|||
opcache_invalidate( $file ); |
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.
Thanks to Phil's suggestion, this was getting an outdated cached version of this file.
This fixed it in my local, let me know if this fixes it for you.
Hi @johnstonphilip, does tree shaking work in your local with this? |
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.
This seems to be working well, @kienstra!
Upon removing an image from a given pattern and saving the post, the image is removed from the images
subfolder.
I am also not seeing any caching issues as I have in the past, though they previously would appear sporadically.
Really nice work rounding this one out! You are @johnstonphilip are both monsters!
Hi @mike-day, |
Amazing! This is actually working really well now. Excellent work @kienstra |
Thanks so much, @johnstonphilip! You did the work, this just brings it back 😄 |
Brings back Phil's image tree shaking. Phil did almost all of the work already.
Fixes part of #57 (Ensure assets save appropriately)
How to test
https://via.placeholder.com/640x350
https://via.placeholder.com/640x351
<active theme>/patterns/images/
640x351
image, but not640x350
: