Skip to content

Commit 90ec2cb

Browse files
committed
Merge branch 'master' into feature/php73
2 parents ce152f1 + 6b0e992 commit 90ec2cb

File tree

10 files changed

+167
-48
lines changed

10 files changed

+167
-48
lines changed

.distignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
.travis.yml
77
behat.yml
88
circle.yml
9+
phpcs.xml.dist
10+
phpunit.xml.dist
911
bin/
1012
features/
1113
utils/

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ vendor/
88
*.txt
99
*.log
1010
composer.lock
11+
phpunit.xml
12+
phpcs.xml
13+
.phpcs.xml

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"require-dev": {
1717
"wp-cli/entity-command": "^1.3 || ^2",
18-
"wp-cli/wp-cli-tests": "^2.0.7"
18+
"wp-cli/wp-cli-tests": "^2.1"
1919
},
2020
"config": {
2121
"process-timeout": 7200,

embed-command.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
return;
55
}
66

7-
$autoload = __DIR__ . '/vendor/autoload.php';
7+
$wpcli_embed_autoloader = __DIR__ . '/vendor/autoload.php';
88

9-
if ( file_exists( $autoload ) ) {
10-
require_once $autoload;
9+
if ( file_exists( $wpcli_embed_autoloader ) ) {
10+
require_once $wpcli_embed_autoloader;
1111
}
1212

1313
if ( class_exists( 'WP_CLI\Dispatcher\CommandNamespace' ) ) {

features/provider.feature

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ Feature: Manage oEmbed providers.
22

33
Background:
44
Given a WP install
5+
And a filter-providers.php file:
6+
"""
7+
<?php
8+
WP_CLI::add_wp_hook( 'oembed_providers', function ( $providers ) {
9+
$providers['http://example.com/*'] = [ 'http://example.com/api/oembed.{format}', false ];
10+
return $providers;
11+
});
12+
"""
513

614
Scenario: List oEmbed providers
7-
When I run `wp embed provider list`
15+
When I run `wp embed provider list --require=filter-providers.php`
816
And save STDOUT as {DEFAULT_STDOUT}
917
Then STDOUT should contain:
1018
"""
@@ -18,6 +26,14 @@ Feature: Manage oEmbed providers.
1826
"""
1927
regex
2028
"""
29+
And STDOUT should contain:
30+
"""
31+
http://example.com/*
32+
"""
33+
And STDOUT should contain:
34+
"""
35+
http://example.com/api/oembed.{format}
36+
"""
2137
And STDOUT should contain:
2238
"""
2339
youtube\.com/watch.*
@@ -51,16 +67,24 @@ Feature: Manage oEmbed providers.
5167
spotify.com
5268
"""
5369

54-
When I run `wp embed provider list --fields=format,endpoint`
70+
When I run `wp embed provider list --fields=format,endpoint --require=filter-providers.php`
5571
Then STDOUT should be:
5672
"""
5773
{DEFAULT_STDOUT}
5874
"""
5975

60-
When I run `wp embed provider list --force-regex`
76+
When I run `wp embed provider list --force-regex --require=filter-providers.php`
77+
Then STDOUT should contain:
78+
"""
79+
#https?\://example\.com/(.+)#i
80+
"""
81+
And STDOUT should contain:
82+
"""
83+
http://example.com/api/oembed.{format}
84+
"""
6185
Then STDOUT should not contain:
6286
"""
63-
{DEFAULT_STDOUT}
87+
http://example.com/*
6488
"""
6589
And STDOUT should match /^#http/m
6690

phpcs.xml.dist

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="WP-CLI-embed">
3+
<description>Custom ruleset for WP-CLI embed-command</description>
4+
5+
<!--
6+
#############################################################################
7+
COMMAND LINE ARGUMENTS
8+
For help understanding this file: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
9+
For help using PHPCS: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage
10+
#############################################################################
11+
-->
12+
13+
<!-- What to scan. -->
14+
<file>.</file>
15+
16+
<!-- Show progress. -->
17+
<arg value="p"/>
18+
19+
<!-- Strip the filepaths down to the relevant bit. -->
20+
<arg name="basepath" value="./"/>
21+
22+
<!-- Check up to 8 files simultaneously. -->
23+
<arg name="parallel" value="8"/>
24+
25+
<!--
26+
#############################################################################
27+
USE THE WP_CLI_CS RULESET
28+
#############################################################################
29+
-->
30+
31+
<rule ref="WP_CLI_CS"/>-
32+
33+
<!--
34+
#############################################################################
35+
PROJECT SPECIFIC CONFIGURATION FOR SNIFFS
36+
#############################################################################
37+
-->
38+
39+
<!-- For help understanding the `testVersion` configuration setting:
40+
https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
41+
<config name="testVersion" value="5.4-"/>
42+
43+
<!-- Verify that everything in the global namespace is either namespaced or prefixed.
44+
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace -->
45+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
46+
<properties>
47+
<property name="prefixes" type="array">
48+
<element value="WP_CLI\Embed"/><!-- Namespaces. -->
49+
<element value="wpcli_embed"/><!-- Global variables and such. -->
50+
</property>
51+
</properties>
52+
</rule>
53+
54+
<!-- Exclude existing classes from the prefix rule as it would break BC to prefix them now. -->
55+
<rule ref="PEAR.NamingConventions.ValidClassName.StartWithCapital">
56+
<exclude-pattern>*/src/oEmbed.php</exclude-pattern>
57+
</rule>
58+
59+
</ruleset>

src/Cache_Command.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ public function clear( $args, $assoc_args ) {
3838
$post_metas = get_post_custom_keys( $post_id );
3939

4040
if ( $post_metas ) {
41-
$post_metas = array_filter( $post_metas, function ( $v ) {
42-
return '_oembed_' === substr( $v, 0, 8 );
43-
} );
41+
$post_metas = array_filter(
42+
$post_metas,
43+
function ( $v ) {
44+
return '_oembed_' === substr( $v, 0, 8 );
45+
}
46+
);
4447
}
4548

4649
if ( empty( $post_metas ) ) {
@@ -88,24 +91,33 @@ public function find( $args, $assoc_args ) {
8891
/** @var \WP_Embed $wp_embed */
8992
global $wp_embed;
9093

91-
$url = $args[0];
94+
$url = $args[0];
95+
$width = Utils\get_flag_value( $assoc_args, 'width' );
96+
$height = Utils\get_flag_value( $assoc_args, 'height' );
97+
$discover = Utils\get_flag_value( $assoc_args, 'discover' );
9298

9399
// 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.
94100
$oembed_args = array();
95-
if ( null !== ( $width = Utils\get_flag_value( $assoc_args, 'width' ) ) ) {
101+
102+
if ( null !== $width ) {
96103
$oembed_args['width'] = $width; // Keep as string as if from a shortcode attribute.
97104
}
98-
if ( null !== ( $height = Utils\get_flag_value( $assoc_args, 'height' ) ) ) {
105+
if ( null !== $height ) {
99106
$oembed_args['height'] = $height; // Keep as string as if from a shortcode attribute.
100107
}
101-
$discovers = null !== ( $discover = Utils\get_flag_value( $assoc_args, 'discover' ) ) ? array( $discover ? '1' : '0' ) : array( null, '1', '0' );
108+
if ( null !== $discover ) {
109+
$discovers = array( ( $discover ) ? '1' : '0' );
110+
} else {
111+
$discovers = array( null, '1', '0' );
112+
}
102113

103114
$attr = wp_parse_args( $oembed_args, wp_embed_defaults( $url ) );
104115

105116
foreach ( $discovers as $discover ) {
106117
if ( null !== $discover ) {
107118
$attr['discover'] = $discover;
108119
}
120+
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- needed to mimic WP Core behavior. See: \WP_Embed::shortcode.
109121
$key_suffix = md5( $url . serialize( $attr ) );
110122

111123
$cached_post_id = $wp_embed->find_oembed_post_id( $key_suffix );
@@ -151,6 +163,7 @@ public function trigger( $args, $assoc_args ) {
151163
}
152164

153165
/** This filter is documented in wp-includes/class-wp-embed.php */
166+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Using WP Core hook.
154167
if ( ! in_array( $post->post_type, apply_filters( 'embed_cache_oembed_types', $post_types ), true ) ) {
155168
WP_CLI::warning( sprintf( "Cannot cache oEmbed results for '%s' post type", $post->post_type ) );
156169

src/Fetch_Command.php

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,16 @@ public function __invoke( $args, $assoc_args ) {
7575
$post_id = Utils\get_flag_value( $assoc_args, 'post-id' );
7676
$discover = Utils\get_flag_value( $assoc_args, 'discover' );
7777
$response_size_limit = Utils\get_flag_value( $assoc_args, 'limit-response-size' );
78+
$width = Utils\get_flag_value( $assoc_args, 'width' );
79+
$height = Utils\get_flag_value( $assoc_args, 'height' );
7880

7981
// 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.
8082
$oembed_args = array();
81-
if ( null !== ( $width = Utils\get_flag_value( $assoc_args, 'width' ) ) ) {
83+
84+
if ( null !== $width ) {
8285
$oembed_args['width'] = $width; // Keep as string as if from a shortcode attribute.
8386
}
84-
if ( null !== ( $height = Utils\get_flag_value( $assoc_args, 'height' ) ) ) {
87+
if ( null !== $height ) {
8588
$oembed_args['height'] = $height; // Keep as string as if from a shortcode attribute.
8689
}
8790
if ( null !== $discover ) {
@@ -103,26 +106,36 @@ public function __invoke( $args, $assoc_args ) {
103106
WP_CLI::warning( "The 'limit-response-size' option only works for WordPress 4.0 onwards." );
104107
// Fall through anyway...
105108
}
106-
add_filter( 'oembed_remote_get_args', function ( $args ) use ( $response_size_limit ) {
107-
$args['limit_response_size'] = $response_size_limit;
108-
109-
return $args;
110-
}, PHP_INT_MAX );
109+
add_filter(
110+
'oembed_remote_get_args',
111+
function ( $args ) use ( $response_size_limit ) {
112+
$args['limit_response_size'] = $response_size_limit;
113+
114+
return $args;
115+
},
116+
PHP_INT_MAX
117+
);
111118
}
112119

113120
// If raw, query providers directly, by-passing cache.
114121
if ( $raw ) {
115-
$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).
122+
$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).
116123

117124
$oembed_args['discover'] = $discover;
118125

119126
// Make 'oembed_dataparse' filter a no-op so get raw unsanitized data.
120127
remove_all_filters( 'oembed_dataparse' ); // Save a few cycles.
121-
add_filter( 'oembed_dataparse', function ( $return, $data, $url ) {
122-
return $data;
123-
}, PHP_INT_MAX, 3 );
128+
add_filter(
129+
'oembed_dataparse',
130+
function ( $return, $data, $url ) {
131+
return $data;
132+
},
133+
PHP_INT_MAX,
134+
3
135+
);
124136

125137
// Allow `wp_filter_pre_oembed_result()` to provide local URLs (WP >= 4.5.3).
138+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Using WP Core hook.
126139
$data = apply_filters( 'pre_oembed_result', null, $url, $oembed_args );
127140

128141
if ( null === $data ) {
@@ -147,7 +160,7 @@ public function __invoke( $args, $assoc_args ) {
147160
if ( ! class_exists( 'SimpleXMLElement' ) ) {
148161
WP_CLI::error( "The PHP extension 'SimpleXMLElement' is not available but is required for XML-formatted output." );
149162
}
150-
WP_CLI::log( $this->_oembed_create_xml( (array) $data ) );
163+
WP_CLI::log( $this->oembed_create_xml( (array) $data ) );
151164
} else {
152165
WP_CLI::log( json_encode( $data ) );
153166
}
@@ -156,6 +169,7 @@ public function __invoke( $args, $assoc_args ) {
156169
}
157170

158171
if ( $post_id ) {
172+
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- the request is asking for a post id directly so need to override the global.
159173
$GLOBALS['post'] = get_post( $post_id );
160174
if ( null === $GLOBALS['post'] ) {
161175
WP_CLI::warning( sprintf( "Post id '%s' not found.", $post_id ) );
@@ -190,7 +204,7 @@ public function __invoke( $args, $assoc_args ) {
190204

191205
// Check providers.
192206
$oembed_args['discover'] = $discover;
193-
$html = wp_oembed_get( $url, $oembed_args );
207+
$html = wp_oembed_get( $url, $oembed_args );
194208

195209
// `wp_oembed_get()` returns zero-length string instead of false on failure due to `_strip_newlines()` 'oembed_dataparse' filter so make sure false.
196210
if ( '' === $html ) {
@@ -220,7 +234,7 @@ public function __invoke( $args, $assoc_args ) {
220234
* @param \SimpleXMLElement $node Optional. XML node to append the result to recursively.
221235
* @return string|false XML string on success, false on error.
222236
*/
223-
protected function _oembed_create_xml( $data, $node = null ) {
237+
protected function oembed_create_xml( $data, $node = null ) {
224238
if ( ! is_array( $data ) || empty( $data ) ) {
225239
return false;
226240
}
@@ -236,7 +250,7 @@ protected function _oembed_create_xml( $data, $node = null ) {
236250

237251
if ( is_array( $value ) ) {
238252
$item = $node->addChild( $key );
239-
$this->_oembed_create_xml( $value, $item );
253+
$this->oembed_create_xml( $value, $item );
240254
} else {
241255
$node->addChild( $key, esc_html( $value ) );
242256
}

src/Handler_Command.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public function list_handlers( $args, $assoc_args ) {
7171
foreach ( $wp_embed->handlers as $priority => $handlers ) {
7272
foreach ( $handlers as $id => $handler ) {
7373
$all_handlers[] = array(
74-
'id' => $id,
75-
'regex' => $handler['regex'],
74+
'id' => $id,
75+
'regex' => $handler['regex'],
7676
'callback' => $handler['callback'],
7777
'priority' => $priority,
7878
);

0 commit comments

Comments
 (0)