Skip to content
Merged
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
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
},
"require-dev": {
"wp-cli/entity-command": "^1.3 || ^2",
"wp-cli/wp-cli-tests": "^4"
"wp-cli/wp-cli-tests": "^5"
},
"config": {
"process-timeout": 7200,
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"johnpbloch/wordpress-core-installer": true
"johnpbloch/wordpress-core-installer": true,
"phpstan/extension-installer": true
},
"lock": false
},
Expand Down Expand Up @@ -60,12 +61,14 @@
"behat-rerun": "rerun-behat-tests",
"lint": "run-linter-tests",
"phpcs": "run-phpcs-tests",
"phpstan": "run-phpstan-tests",
"phpcbf": "run-phpcbf-cleanup",
"phpunit": "run-php-unit-tests",
"prepare-tests": "install-package-tests",
"test": [
"@lint",
"@phpcs",
"@phpstan",
"@phpunit",
"@behat"
]
Expand Down
55 changes: 0 additions & 55 deletions features/fetch.feature
Original file line number Diff line number Diff line change
Expand Up @@ -90,32 +90,6 @@ Feature: Manage oEmbed fetch.
<a
"""

# `wp_filter_oembed_result` filter introduced WP 4.4 which sanitizes oEmbed responses that don't include an iframe.
@less-than-wp-4.9 @require-wp-4.4
Scenario: Get HTML embed code for a given URL that requires discovery and is sanitized
When I try `wp embed fetch https://view.ceros.com/ceros/new-experience-3/p/1`
Then the return code should be 1
And STDERR should be:
"""
Error: There was an error fetching the oEmbed data.
"""
And STDOUT should be empty

# No sanitization prior to WP 4.4.
@less-than-wp-4.4 @require-wp-4.0
Scenario: Get HTML embed code for a given URL that requires discovery and is sanitized
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discover so use "try" to cater for these.
When I try `wp embed fetch https://view.ceros.com/ceros/new-experience-3/p/1`
Then the return code should be 0
And STDERR should not contain:
"""
Error:
"""
And STDOUT should contain:
"""
ceros.com/
"""

@require-wp-4.0
Scenario: Get raw oEmbed data for a given URL
When I run `wp embed fetch https://www.youtube.com/watch?v=dQw4w9WgXcQ --raw`
Expand Down Expand Up @@ -192,18 +166,6 @@ Feature: Manage oEmbed fetch.
<a href="https://foo.example.com">https://foo.example.com</a>
"""

# WP prior to 4.9 does not return clickable link.
@less-than-wp-4.9 @require-wp-4.0
Scenario: Doesn't make unknown URLs clickable
When I try `wp embed fetch https://foo.example.com`
Then the return code should be 1
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discover so use "contain" to cater for these.
And STDERR should contain:
"""
Error: There was an error fetching the oEmbed data.
"""
And STDOUT should be empty

@require-wp-4.0
Scenario: Caches oEmbed response data for a given post
# Note need post author for 'unfiltered_html' check to work for WP < 4.4.
Expand Down Expand Up @@ -410,23 +372,6 @@ Feature: Manage oEmbed fetch.
<video
"""

# `wp_embed_handler_googlevideo` handler deprecated WP 4.6.
@less-than-wp-4.6 @require-wp-4.0
Scenario: Invoke built-in Google Video handler
When I run `wp post create --post_title="Foo Bar" --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID}

When I run `wp embed fetch http://video.google.com/videoplay?docid=123456789 --post-id={POST_ID}`
Then STDOUT should contain:
"""
video.google.com
"""
And STDOUT should contain:
"""
<embed
"""

@require-wp-4.0
Scenario: Incompatible options
When I try `wp embed fetch https://www.example.com/watch?v=dQw4w9WgXcQ --no-discover --limit-response-size=50000`
Expand Down
15 changes: 15 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
parameters:
level: 9
paths:
- src
- embed-command.php
scanDirectories:
- vendor/wp-cli/wp-cli/php
scanFiles:
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
treatPhpDocTypesAsCertain: false
ignoreErrors:
- identifier: missingType.iterableValue
- identifier: missingType.property
- identifier: missingType.parameter
- identifier: missingType.return
6 changes: 1 addition & 5 deletions src/Cache_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ function ( $v ) {
* 123
*/
public function find( $args, $assoc_args ) {
if ( Utils\wp_version_compare( '4.9', '<' ) ) {
WP_CLI::error( 'Requires WordPress 4.9 or greater.' );
}

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

Expand Down Expand Up @@ -139,7 +135,7 @@ public function find( $args, $assoc_args ) {
$cached_post_id = $wp_embed->find_oembed_post_id( $key_suffix );

if ( $cached_post_id ) {
WP_CLI::line( $cached_post_id );
WP_CLI::line( (string) $cached_post_id );

return;
}
Expand Down
63 changes: 26 additions & 37 deletions src/Fetch_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,35 @@
/** @var \WP_Embed $wp_embed */
global $wp_embed;

$url = $args[0];
$raw = Utils\get_flag_value( $assoc_args, 'raw' );
$raw_format = Utils\get_flag_value( $assoc_args, 'raw-format' );
$url = $args[0];
$raw = Utils\get_flag_value( $assoc_args, 'raw' );
$raw_format = Utils\get_flag_value( $assoc_args, 'raw-format' );

/**
* @var string|null $post_id
*/
$post_id = Utils\get_flag_value( $assoc_args, 'post-id' );
$discover = Utils\get_flag_value( $assoc_args, 'discover' );
$response_size_limit = Utils\get_flag_value( $assoc_args, 'limit-response-size' );
$width = Utils\get_flag_value( $assoc_args, 'width' );
$height = Utils\get_flag_value( $assoc_args, 'height' );

/**
* @var string $width
*/
$width = Utils\get_flag_value( $assoc_args, 'width' );

/**
* @var string $height
*/
$height = Utils\get_flag_value( $assoc_args, 'height' );

// The `$key_suffix` used for caching is part based on serializing the attributes array without normalizing it first so need to try to replicate that.
$oembed_args = array();

if ( null !== $width ) {
$oembed_args['width'] = $width; // Keep as string as if from a shortcode attribute.
$oembed_args['width'] = (int) $width;
}
if ( null !== $height ) {
$oembed_args['height'] = $height; // Keep as string as if from a shortcode attribute.
$oembed_args['height'] = (int) $height;

Check warning on line 100 in src/Fetch_Command.php

View check run for this annotation

Codecov / codecov/patch

src/Fetch_Command.php#L100

Added line #L100 was not covered by tests
}
if ( null !== $discover ) {
$oembed_args['discover'] = $discover ? '1' : '0'; // Make it a string as if from a shortcode attribute.
Expand All @@ -102,10 +114,6 @@
}

if ( $response_size_limit ) {
if ( Utils\wp_version_compare( '4.0', '<' ) ) {
WP_CLI::warning( "The 'limit-response-size' option only works for WordPress 4.0 onwards." );
// Fall through anyway...
}
add_filter(
'oembed_remote_get_args',
function ( $args ) use ( $response_size_limit ) {
Expand All @@ -119,7 +127,7 @@

// If raw, query providers directly, by-passing cache.
if ( $raw ) {
$oembed = new oEmbed(); // Needs to be here to make sure `_wp_oembed_get_object()` defined (in "wp-includes/class-oembed.php" for WP < 4.7).
$oembed = new \WP_oEmbed();

$oembed_args['discover'] = $discover;

Expand Down Expand Up @@ -160,27 +168,27 @@
if ( ! class_exists( 'SimpleXMLElement' ) ) {
WP_CLI::error( "The PHP extension 'SimpleXMLElement' is not available but is required for XML-formatted output." );
}
WP_CLI::log( $this->oembed_create_xml( (array) $data ) );
WP_CLI::log( (string) $this->oembed_create_xml( (array) $data ) );
} else {
WP_CLI::log( json_encode( $data ) );
WP_CLI::log( (string) json_encode( $data ) );
}

return;
}

if ( $post_id ) {
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- the request is asking for a post id directly so need to override the global.
$GLOBALS['post'] = get_post( $post_id );
$GLOBALS['post'] = get_post( (int) $post_id );
if ( null === $GLOBALS['post'] ) {
WP_CLI::warning( sprintf( "Post id '%s' not found.", $post_id ) );
}
}

if ( Utils\get_flag_value( $assoc_args, 'skip-sanitization' ) ) {
remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10, 3 );
if ( Utils\get_flag_value( $assoc_args, 'skip-sanitization', false ) ) {
remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );
}

if ( Utils\get_flag_value( $assoc_args, 'skip-cache' ) ) {
if ( Utils\get_flag_value( $assoc_args, 'skip-cache', false ) ) {
$wp_embed->usecache = false;
// In order to skip caching, also need `$cached_recently` to be false in `WP_Embed::shortcode()`, so set TTL to zero.
add_filter( 'oembed_ttl', '__return_zero', PHP_INT_MAX );
Expand All @@ -191,27 +199,8 @@
// `WP_Embed::shortcode()` sets the 'discover' attribute based on 'embed_oembed_discover' filter, no matter what's passed to it.
add_filter( 'embed_oembed_discover', $discover ? '__return_true' : '__return_false', PHP_INT_MAX );

// For WP < 4.9, `WP_Embed::shortcode()` won't check providers if no post_id supplied, so set `maybe_make_link()` to return false so can check and do it ourselves.
// Also set if WP < 4.4 and don't have 'unfiltered_html' privileges on post.
$check_providers = Utils\wp_version_compare( '4.9', '<' ) && ( ! $post_id || ( Utils\wp_version_compare( '4.4', '<' ) && ! author_can( $post_id, 'unfiltered_html' ) ) );
if ( $check_providers ) {
add_filter( 'embed_maybe_make_link', '__return_false', PHP_INT_MAX );
}

$html = $wp_embed->shortcode( $oembed_args, $url );

if ( false === $html && $check_providers ) {

// Check providers.
$oembed_args['discover'] = $discover;
$html = wp_oembed_get( $url, $oembed_args );

// `wp_oembed_get()` returns zero-length string instead of false on failure due to `_strip_newlines()` 'oembed_dataparse' filter so make sure false.
if ( '' === $html ) {
$html = false;
}
}

if ( false !== $html && '[' === substr( $html, 0, 1 ) && Utils\get_flag_value( $assoc_args, 'do-shortcode' ) ) {
$html = do_shortcode( $html, true );
}
Expand Down
22 changes: 12 additions & 10 deletions src/Provider_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,13 @@ class Provider_Command extends WP_CLI_Command {
* | #https?://wordpress\.tv/.*#i | https://wordpress.tv/oembed/ |
*
* @subcommand list
*
* @param string[] $args Positional arguments. Unused.
* @param array{field?: string, fields?: string, format: 'table'|'csv'|'json', 'force-regex'?: bool} $assoc_args Associative arguments.
*/
public function list_providers( $args, $assoc_args ) {

$oembed = new oEmbed();
$oembed = new \WP_oEmbed();

$force_regex = Utils\get_flag_value( $assoc_args, 'force-regex' );

Expand Down Expand Up @@ -138,14 +141,17 @@ public function list_providers( $args, $assoc_args ) {
* https://www.youtube.com/oembed
*
* @subcommand match
*
* @param array{0: string} $args Positional arguments.
* @param array{discover?: bool, 'limit-response-size'?: string, 'link-type'?: 'json'|'xml', } $assoc_args Associative arguments.
*/
public function match_provider( $args, $assoc_args ) {
$oembed = new oEmbed();
$oembed = new \WP_oEmbed();

$url = $args[0];
$discover = \WP_CLI\Utils\get_flag_value( $assoc_args, 'discover', true );
$response_size_limit = \WP_CLI\Utils\get_flag_value( $assoc_args, 'limit-response-size' );
$link_type = \WP_CLI\Utils\get_flag_value( $assoc_args, 'link-type' );
$discover = Utils\get_flag_value( $assoc_args, 'discover', true );
$response_size_limit = Utils\get_flag_value( $assoc_args, 'limit-response-size' );
$link_type = Utils\get_flag_value( $assoc_args, 'link-type' );

if ( ! $discover && ( null !== $response_size_limit || null !== $link_type ) ) {
if ( null !== $response_size_limit && null !== $link_type ) {
Expand All @@ -159,14 +165,10 @@ public function match_provider( $args, $assoc_args ) {
}

if ( $response_size_limit ) {
if ( Utils\wp_version_compare( '4.0', '<' ) ) {
WP_CLI::warning( "The 'limit-response-size' option only works for WordPress 4.0 onwards." );
// Fall through anyway...
}
add_filter(
'oembed_remote_get_args',
function ( $args ) use ( $response_size_limit ) {
$args['limit_response_size'] = $response_size_limit;
$args['limit_response_size'] = (int) $response_size_limit;
return $args;
}
);
Expand Down
59 changes: 0 additions & 59 deletions src/oEmbed.php

This file was deleted.