Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions features/cache.feature
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,101 @@ Feature: Manage oEmbed cache.
Success: Cleared oEmbed cache.
"""

Scenario: Clear oEmbed cache with missing arguments
When I try `wp embed cache clear`
Then STDERR should be:
"""
Error: Please specify one or more post IDs, or use --all.
"""
And the return code should be 1

Scenario: Clear oEmbed cache for multiple posts
When I run `wp post create --post_title="Post 1" --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID_1}

When I run `wp post create --post_title="Post 2" --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID_2}

When I run `wp post meta add {POST_ID_1} _oembed_foo 'bar'`
Then STDOUT should not be empty

When I run `wp post meta add {POST_ID_2} _oembed_baz 'qux'`
Then STDOUT should not be empty

When I run `wp embed cache clear {POST_ID_1} {POST_ID_2}`
Then STDOUT should be:
"""
Success: Cleared oEmbed cache for post {POST_ID_1}.
Success: Cleared oEmbed cache for post {POST_ID_2}.
"""
And the return code should be 0

When I try `wp post meta get {POST_ID_1} _oembed_foo`
Then the return code should be 1

When I try `wp post meta get {POST_ID_2} _oembed_baz`
Then the return code should be 1

Scenario: Clear oEmbed cache for multiple posts with some empty
When I run `wp post create --post_title="Post 1" --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID_1}

When I run `wp post create --post_title="Post 2" --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID_2}

When I run `wp post meta add {POST_ID_1} _oembed_foo 'bar'`
Then STDOUT should not be empty

When I try `wp embed cache clear {POST_ID_1} {POST_ID_2}`
Then STDOUT should be:
"""
Success: Cleared oEmbed cache for post {POST_ID_1}.
"""
And STDERR should be:
"""
Warning: No cache to clear for post {POST_ID_2}!
"""
And the return code should be 1

Scenario: Clear all oEmbed caches
When I run `wp post create --post_title="Post 1" --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID_1}

When I run `wp post create --post_title="Post 2" --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID_2}

When I run `wp post meta add {POST_ID_1} _oembed_foo 'bar'`
Then STDOUT should not be empty

When I run `wp post meta add {POST_ID_2} _oembed_baz 'qux'`
Then STDOUT should not be empty

When I run `wp post create --post_title=EmbedCache --post_type=oembed_cache --post_status=publish --porcelain`
Then STDOUT should be a number
And save STDOUT as {OEMBED_POST_ID}

When I run `wp embed cache clear --all`
Then STDOUT should be:
"""
Success: Cleared all oEmbed caches.
"""
And the return code should be 0

When I try `wp post meta get {POST_ID_1} _oembed_foo`
Then the return code should be 1

When I try `wp post meta get {POST_ID_2} _oembed_baz`
Then the return code should be 1

When I try `wp post get {OEMBED_POST_ID}`
Then the return code should be 1

Scenario: Trigger oEmbed cache for a non-existent post
When I try `wp embed cache trigger 123456`
Then STDERR should contain:
Expand Down
92 changes: 73 additions & 19 deletions src/Cache_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,99 @@
class Cache_Command extends WP_CLI_Command {

/**
* Deletes all oEmbed caches for a given post.
* Deletes all oEmbed caches for a given post or all posts.
*
* oEmbed caches for a post are stored in the post's metadata.
* Starting with WordPress 4.9, embeds that aren't associated with a specific post will be cached in
* a new oembed_cache post type.
*
* ## OPTIONS
*
* <post_id>
* : ID of the post to clear the cache for.
* [<post_id>...]
* : One or more IDs of the posts to clear the cache for.
*
* [--all]
* : Clear all oEmbed caches.
*
* ## EXAMPLES
*
* # Clear cache for a post
* # Clear cache for a post.
* $ wp embed cache clear 123
* Success: Cleared oEmbed cache.
*
* # Clear cache for multiple posts.
* $ wp embed cache clear 123 456 789
* Success: Cleared oEmbed cache for post 123.
* Success: Cleared oEmbed cache for post 456.
* Success: Cleared oEmbed cache for post 789.
*
* # Clear all oEmbed caches.
* $ wp embed cache clear --all
* Success: Cleared all oEmbed caches.
*/
public function clear( $args, $assoc_args ) {
$all = Utils\get_flag_value( $assoc_args, 'all', false );

if ( empty( $args ) && ! $all ) {
WP_CLI::error( 'Please specify one or more post IDs, or use --all.' );
}

if ( $all ) {
Comment on lines +62 to +68
/** @var \wpdb $wpdb */
global $wpdb;

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE '_oembed_%'" );
Comment on lines +72 to +73

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query( "DELETE FROM {$wpdb->posts} WHERE post_type = 'oembed_cache'" );

Comment on lines +73 to +77
Comment on lines +76 to +77
if ( function_exists( 'wp_cache_flush' ) ) {
wp_cache_flush();
}

WP_CLI::success( 'Cleared all oEmbed caches.' );
return;
}

/** @var \WP_Embed $wp_embed */
global $wp_embed;

$post_id = $args[0];

$post_metas = get_post_custom_keys( $post_id );
$errors = 0;
foreach ( $args as $post_id ) {
$post_metas = get_post_custom_keys( $post_id );

if ( $post_metas ) {
$post_metas = array_filter(
$post_metas,
function ( $v ) {
return '_oembed_' === substr( $v, 0, 8 );
}
);
}

if ( $post_metas ) {
$post_metas = array_filter(
$post_metas,
function ( $v ) {
return '_oembed_' === substr( $v, 0, 8 );
if ( empty( $post_metas ) ) {
if ( 1 === count( $args ) ) {
WP_CLI::error( 'No cache to clear!' );
} else {
WP_CLI::warning( "No cache to clear for post {$post_id}!" );
++$errors;
continue;
}
);
}
}

if ( empty( $post_metas ) ) {
WP_CLI::error( 'No cache to clear!' );
}
$wp_embed->delete_oembed_caches( $post_id );

$wp_embed->delete_oembed_caches( $post_id );
if ( count( $args ) > 1 ) {
WP_CLI::success( "Cleared oEmbed cache for post {$post_id}." );
}
}

WP_CLI::success( 'Cleared oEmbed cache.' );
if ( 1 === count( $args ) ) {
WP_CLI::success( 'Cleared oEmbed cache.' );
} elseif ( $errors > 0 ) {
WP_CLI::halt( 1 );
}
}

/**
Expand Down
Loading