This repository was archived by the owner on Apr 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Convert [img] shortcodes back to tags/captions #31
Open
goldenapples
wants to merge
31
commits into
master
Choose a base branch
from
20-convert-shortcodes-to-inline-images
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
517ed1d
Convert [img] shortcodes back to tags/captions
goldenapples 5a0e9c4
Merge remote-tracking branch 'origin/master' into 20-convert-shortcod…
goldenapples 930fa79
Rename 'downdate' function to the more sensical 'revert'
goldenapples ad1e6ff
Start adding tests for reverse conversion
25f117f
Merge branch 'master' into 20-convert-shortcodes-to-inline-images
3a3de08
Fix caption reversal test
da0b515
PHPCS cleanup for CLI command
ef7b443
Remove commented test code until it's real
a44876c
Add some more shortcake conversion tests
b1a0773
Add first cadillac test
81ad0ad
Add second cadillac test
c2ee82c
Remove EOL whitespace
be4c6e8
Add another test for caption width
87de98c
Modify the shortcode callback to oupt img w/o src if no attachment
c678a16
Updates to behavior of the migration script
e95e9d1
Adds test coverage for missing/invalid attachment ID's
bcd1c4b
PHPCS tweaks
c320ffe
Fix WP CLI phpdoc
619f530
If invalid attachment, store in data attr for posterity
6d16c9d
Use yoda conditional
1f8ae43
Scaffold some tests of the regex
4cd9062
Update callback to model core behavior w/r/t caption + alignment
882a0c5
Update tests to have alignments in the right spot
dcba78b
Updates to regex
6facd5b
Capture caption in the regex
76b74e5
Revert the logic change to alignment, broke stuff
cacbb2d
Tweaks to the regex
171dff5
PHPCS issues
d835a40
Tweak to regex for readability
24793a7
Fix another regex naggle
96df81d
Fix caption regex to get tests passing
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,7 @@ public function __construct() { | |
| * ## EXAMPLES | ||
| * | ||
| * ## Migrate all Posts to the Image Shortcake syntax | ||
| * wp image-shortcake-shortcode migrate `wp post list --post_type=post` --ids` | ||
| * wp image-shortcake migrate `wp post list --post_type=post` --ids` | ||
| * | ||
| * ## Converts images to shortcodes on one post, preserving a log to rollback in case of errors. | ||
| * wp image-shortcake migrate 123 > potential-oops.txt | ||
|
|
@@ -102,6 +102,86 @@ public function migrate( $args, $assoc_args ) { | |
|
|
||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Revert post content from image shortcodes back to <img> tags and [caption] shortcodes | ||
| * | ||
| * ## OPTIONS | ||
| * | ||
| * <id>... | ||
| * : One or more IDs of posts to update. | ||
| * | ||
| * [--dry-run] | ||
| * : Only show the content which is to be changed, don't update posts. | ||
| * | ||
| * ## EXAMPLES | ||
| * | ||
| * ## Revert all Posts from the Image Shortcake syntax | ||
| * wp image-shortcake migrate `wp post list --post_type=post` --ids` | ||
| * | ||
| * ## Converts shortcodes back to images on one post, preserving a log to rollback in case of errors. | ||
| * wp image-shortcake migrate 123 > potential-oops.txt | ||
|
Contributor
Author
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. same here |
||
| * | ||
| * @synopsis <id>... [--dry-run] | ||
| */ | ||
| public function revert( $args, $assoc_args ) { | ||
|
|
||
| foreach ( array_filter( $args ) as $post_ID ) { | ||
|
|
||
| $post = $this->fetcher->get_check( $post_ID ); | ||
|
|
||
| $_content = $post->post_content; | ||
|
|
||
| $replacements = Img_Shortcode_Data_Migration::find_img_shortcodes_for_replacement( $_content ); | ||
|
|
||
| $_content = str_replace( | ||
| array_keys( $replacements ), | ||
| array_values( $replacements ), | ||
| $_content | ||
| ); | ||
|
|
||
| WP_CLI::log( '' ); | ||
|
|
||
| if ( 0 === count( $replacements ) ) { | ||
| WP_CLI::log( 'Nothing to replace on post ' . $post->ID . '. Skipping.' ); | ||
| WP_CLI::log( '' ); | ||
| continue; | ||
| } | ||
|
|
||
| $header = 'Image shortcode replacements for post ' . $post->ID; | ||
|
|
||
| WP_CLI::log( $header ); | ||
| WP_CLI::log( str_repeat( '=', strlen( $header ) ) ); | ||
| WP_CLI::log( '' ); | ||
|
|
||
| foreach ( $replacements as $del => $ins ) { | ||
| \WP_CLI::log( \cli\Colors::colorize( '%C-%n' ) . $del, true ); | ||
| \WP_CLI::log( \cli\Colors::colorize( '%G+%n' ) . $ins, true ); | ||
| } | ||
|
|
||
| WP_CLI::log( '' ); | ||
|
|
||
| if ( isset( $assoc_args['dry-run'] ) ) { | ||
| WP_CLI::log( 'Post not updated: --dry-run specifed.' ); | ||
| WP_CLI::log( '' ); | ||
| continue; | ||
| } | ||
|
|
||
| global $wpdb; | ||
|
|
||
| // @codingStandardsIgnoreStart | ||
| $updated = $wpdb->update( $wpdb->posts, array( 'post_content' => $_content ), array( 'ID' => $post_ID ) ); | ||
| // @codingStandardsIgnoreEnd | ||
|
|
||
| if ( 1 === $updated ) { | ||
| clean_post_cache( $post ); | ||
| WP_CLI::success( 'Updated post ' . $post->ID . '.' ); | ||
| } else { | ||
| WP_CLI::warning( 'There was an unexpected error updating post ' . $post->ID . '.' ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
s/migrate/revert