@@ -69,23 +69,35 @@ public function __invoke( $args, $assoc_args ) {
6969 /** @var \WP_Embed $wp_embed */
7070 global $ wp_embed ;
7171
72- $ url = $ args [0 ];
73- $ raw = Utils \get_flag_value ( $ assoc_args , 'raw ' );
74- $ raw_format = Utils \get_flag_value ( $ assoc_args , 'raw-format ' );
72+ $ url = $ args [0 ];
73+ $ raw = Utils \get_flag_value ( $ assoc_args , 'raw ' );
74+ $ raw_format = Utils \get_flag_value ( $ assoc_args , 'raw-format ' );
75+
76+ /**
77+ * @var string|null $post_id
78+ */
7579 $ post_id = Utils \get_flag_value ( $ assoc_args , 'post-id ' );
7680 $ discover = Utils \get_flag_value ( $ assoc_args , 'discover ' );
7781 $ 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 ' );
82+
83+ /**
84+ * @var string $width
85+ */
86+ $ width = Utils \get_flag_value ( $ assoc_args , 'width ' );
87+
88+ /**
89+ * @var string $height
90+ */
91+ $ height = Utils \get_flag_value ( $ assoc_args , 'height ' );
8092
8193 // 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.
8294 $ oembed_args = array ();
8395
8496 if ( null !== $ width ) {
85- $ oembed_args ['width ' ] = $ width ; // Keep as string as if from a shortcode attribute.
97+ $ oembed_args ['width ' ] = ( int ) $ width ;
8698 }
8799 if ( null !== $ height ) {
88- $ oembed_args ['height ' ] = $ height ; // Keep as string as if from a shortcode attribute.
100+ $ oembed_args ['height ' ] = ( int ) $ height ;
89101 }
90102 if ( null !== $ discover ) {
91103 $ oembed_args ['discover ' ] = $ discover ? '1 ' : '0 ' ; // Make it a string as if from a shortcode attribute.
@@ -102,10 +114,6 @@ public function __invoke( $args, $assoc_args ) {
102114 }
103115
104116 if ( $ response_size_limit ) {
105- if ( Utils \wp_version_compare ( '4.0 ' , '< ' ) ) {
106- WP_CLI ::warning ( "The 'limit-response-size' option only works for WordPress 4.0 onwards. " );
107- // Fall through anyway...
108- }
109117 add_filter (
110118 'oembed_remote_get_args ' ,
111119 function ( $ args ) use ( $ response_size_limit ) {
@@ -119,7 +127,7 @@ function ( $args ) use ( $response_size_limit ) {
119127
120128 // If raw, query providers directly, by-passing cache.
121129 if ( $ raw ) {
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).
130+ $ oembed = new \ WP_oEmbed ();
123131
124132 $ oembed_args ['discover ' ] = $ discover ;
125133
@@ -160,27 +168,27 @@ function ( $ret, $data, $url ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunc
160168 if ( ! class_exists ( 'SimpleXMLElement ' ) ) {
161169 WP_CLI ::error ( "The PHP extension 'SimpleXMLElement' is not available but is required for XML-formatted output. " );
162170 }
163- WP_CLI ::log ( $ this ->oembed_create_xml ( (array ) $ data ) );
171+ WP_CLI ::log ( ( string ) $ this ->oembed_create_xml ( (array ) $ data ) );
164172 } else {
165- WP_CLI ::log ( json_encode ( $ data ) );
173+ WP_CLI ::log ( ( string ) json_encode ( $ data ) );
166174 }
167175
168176 return ;
169177 }
170178
171179 if ( $ post_id ) {
172180 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- the request is asking for a post id directly so need to override the global.
173- $ GLOBALS ['post ' ] = get_post ( $ post_id );
181+ $ GLOBALS ['post ' ] = get_post ( ( int ) $ post_id );
174182 if ( null === $ GLOBALS ['post ' ] ) {
175183 WP_CLI ::warning ( sprintf ( "Post id '%s' not found. " , $ post_id ) );
176184 }
177185 }
178186
179- if ( Utils \get_flag_value ( $ assoc_args , 'skip-sanitization ' ) ) {
180- remove_filter ( 'oembed_dataparse ' , 'wp_filter_oembed_result ' , 10 , 3 );
187+ if ( Utils \get_flag_value ( $ assoc_args , 'skip-sanitization ' , false ) ) {
188+ remove_filter ( 'oembed_dataparse ' , 'wp_filter_oembed_result ' , 10 );
181189 }
182190
183- if ( Utils \get_flag_value ( $ assoc_args , 'skip-cache ' ) ) {
191+ if ( Utils \get_flag_value ( $ assoc_args , 'skip-cache ' , false ) ) {
184192 $ wp_embed ->usecache = false ;
185193 // In order to skip caching, also need `$cached_recently` to be false in `WP_Embed::shortcode()`, so set TTL to zero.
186194 add_filter ( 'oembed_ttl ' , '__return_zero ' , PHP_INT_MAX );
@@ -191,27 +199,8 @@ function ( $ret, $data, $url ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunc
191199 // `WP_Embed::shortcode()` sets the 'discover' attribute based on 'embed_oembed_discover' filter, no matter what's passed to it.
192200 add_filter ( 'embed_oembed_discover ' , $ discover ? '__return_true ' : '__return_false ' , PHP_INT_MAX );
193201
194- // 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.
195- // Also set if WP < 4.4 and don't have 'unfiltered_html' privileges on post.
196- $ check_providers = Utils \wp_version_compare ( '4.9 ' , '< ' ) && ( ! $ post_id || ( Utils \wp_version_compare ( '4.4 ' , '< ' ) && ! author_can ( $ post_id , 'unfiltered_html ' ) ) );
197- if ( $ check_providers ) {
198- add_filter ( 'embed_maybe_make_link ' , '__return_false ' , PHP_INT_MAX );
199- }
200-
201202 $ html = $ wp_embed ->shortcode ( $ oembed_args , $ url );
202203
203- if ( false === $ html && $ check_providers ) {
204-
205- // Check providers.
206- $ oembed_args ['discover ' ] = $ discover ;
207- $ html = wp_oembed_get ( $ url , $ oembed_args );
208-
209- // `wp_oembed_get()` returns zero-length string instead of false on failure due to `_strip_newlines()` 'oembed_dataparse' filter so make sure false.
210- if ( '' === $ html ) {
211- $ html = false ;
212- }
213- }
214-
215204 if ( false !== $ html && '[ ' === substr ( $ html , 0 , 1 ) && Utils \get_flag_value ( $ assoc_args , 'do-shortcode ' ) ) {
216205 $ html = do_shortcode ( $ html , true );
217206 }
0 commit comments