Issues (4967)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/wp-includes/embed.php (18 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * oEmbed API: Top-level oEmbed functionality
4
 *
5
 * @package WordPress
6
 * @subpackage oEmbed
7
 * @since 4.4.0
8
 */
9
10
/**
11
 * Registers an embed handler.
12
 *
13
 * Should probably only be used for sites that do not support oEmbed.
14
 *
15
 * @since 2.9.0
16
 *
17
 * @global WP_Embed $wp_embed
18
 *
19
 * @param string   $id       An internal ID/name for the handler. Needs to be unique.
20
 * @param string   $regex    The regex that will be used to see if this handler should be used for a URL.
21
 * @param callable $callback The callback function that will be called if the regex is matched.
22
 * @param int      $priority Optional. Used to specify the order in which the registered handlers will
23
 *                           be tested. Default 10.
24
 */
25
function wp_embed_register_handler( $id, $regex, $callback, $priority = 10 ) {
26
	global $wp_embed;
27
	$wp_embed->register_handler( $id, $regex, $callback, $priority );
28
}
29
30
/**
31
 * Unregisters a previously-registered embed handler.
32
 *
33
 * @since 2.9.0
34
 *
35
 * @global WP_Embed $wp_embed
36
 *
37
 * @param string $id       The handler ID that should be removed.
38
 * @param int    $priority Optional. The priority of the handler to be removed. Default 10.
39
 */
40
function wp_embed_unregister_handler( $id, $priority = 10 ) {
41
	global $wp_embed;
42
	$wp_embed->unregister_handler( $id, $priority );
43
}
44
45
/**
46
 * Creates default array of embed parameters.
47
 *
48
 * The width defaults to the content width as specified by the theme. If the
49
 * theme does not specify a content width, then 500px is used.
50
 *
51
 * The default height is 1.5 times the width, or 1000px, whichever is smaller.
52
 *
53
 * The {@see 'embed_defaults'} filter can be used to adjust either of these values.
54
 *
55
 * @since 2.9.0
56
 *
57
 * @global int $content_width
58
 *
59
 * @param string $url Optional. The URL that should be embedded. Default empty.
60
 *
61
 * @return array Default embed parameters.
62
 */
63
function wp_embed_defaults( $url = '' ) {
64
	if ( ! empty( $GLOBALS['content_width'] ) )
65
		$width = (int) $GLOBALS['content_width'];
66
67
	if ( empty( $width ) )
68
		$width = 500;
69
70
	$height = min( ceil( $width * 1.5 ), 1000 );
71
72
	/**
73
	 * Filters the default array of embed dimensions.
74
	 *
75
	 * @since 2.9.0
76
	 *
77
	 * @param array  $size An array of embed width and height values
78
	 *                     in pixels (in that order).
79
	 * @param string $url  The URL that should be embedded.
80
	 */
81
	return apply_filters( 'embed_defaults', compact( 'width', 'height' ), $url );
82
}
83
84
/**
85
 * Attempts to fetch the embed HTML for a provided URL using oEmbed.
86
 *
87
 * @since 2.9.0
88
 *
89
 * @see WP_oEmbed
90
 *
91
 * @param string $url  The URL that should be embedded.
92
 * @param array  $args Optional. Additional arguments and parameters for retrieving embed HTML.
0 ignored issues
show
Should the type for parameter $args not be string|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
93
 *                     Default empty.
94
 * @return false|string False on failure or the embed HTML on success.
95
 */
96
function wp_oembed_get( $url, $args = '' ) {
97
	$oembed = _wp_oembed_get_object();
98
	return $oembed->get_html( $url, $args );
99
}
100
101
/**
102
 * Returns the initialized WP_oEmbed object.
103
 *
104
 * @since 2.9.0
105
 * @access private
106
 *
107
 * @staticvar WP_oEmbed $wp_oembed
108
 *
109
 * @return WP_oEmbed object.
110
 */
111
function _wp_oembed_get_object() {
112
	static $wp_oembed = null;
113
114
	if ( is_null( $wp_oembed ) ) {
115
		$wp_oembed = new WP_oEmbed();
116
	}
117
	return $wp_oembed;
118
}
119
120
/**
121
 * Adds a URL format and oEmbed provider URL pair.
122
 *
123
 * @since 2.9.0
124
 *
125
 * @see WP_oEmbed
126
 *
127
 * @param string  $format   The format of URL that this provider can handle. You can use asterisks
128
 *                          as wildcards.
129
 * @param string  $provider The URL to the oEmbed provider.
130
 * @param boolean $regex    Optional. Whether the `$format` parameter is in a RegEx format. Default false.
131
 */
132
function wp_oembed_add_provider( $format, $provider, $regex = false ) {
133
	if ( did_action( 'plugins_loaded' ) ) {
134
		$oembed = _wp_oembed_get_object();
135
		$oembed->providers[$format] = array( $provider, $regex );
136
	} else {
137
		WP_oEmbed::_add_provider_early( $format, $provider, $regex );
138
	}
139
}
140
141
/**
142
 * Removes an oEmbed provider.
143
 *
144
 * @since 3.5.0
145
 *
146
 * @see WP_oEmbed
147
 *
148
 * @param string $format The URL format for the oEmbed provider to remove.
149
 * @return bool Was the provider removed successfully?
150
 */
151
function wp_oembed_remove_provider( $format ) {
152
	if ( did_action( 'plugins_loaded' ) ) {
153
		$oembed = _wp_oembed_get_object();
154
155
		if ( isset( $oembed->providers[ $format ] ) ) {
156
			unset( $oembed->providers[ $format ] );
157
			return true;
158
		}
159
	} else {
160
		WP_oEmbed::_remove_provider_early( $format );
161
	}
162
163
	return false;
164
}
165
166
/**
167
 * Determines if default embed handlers should be loaded.
168
 *
169
 * Checks to make sure that the embeds library hasn't already been loaded. If
170
 * it hasn't, then it will load the embeds library.
171
 *
172
 * @since 2.9.0
173
 *
174
 * @see wp_embed_register_handler()
175
 */
176
function wp_maybe_load_embeds() {
177
	/**
178
	 * Filters whether to load the default embed handlers.
179
	 *
180
	 * Returning a falsey value will prevent loading the default embed handlers.
181
	 *
182
	 * @since 2.9.0
183
	 *
184
	 * @param bool $maybe_load_embeds Whether to load the embeds library. Default true.
185
	 */
186
	if ( ! apply_filters( 'load_default_embeds', true ) ) {
187
		return;
188
	}
189
190
	wp_embed_register_handler( 'youtube_embed_url', '#https?://(www.)?youtube\.com/(?:v|embed)/([^/]+)#i', 'wp_embed_handler_youtube' );
191
192
	/**
193
	 * Filters the audio embed handler callback.
194
	 *
195
	 * @since 3.6.0
196
	 *
197
	 * @param callable $handler Audio embed handler callback function.
198
	 */
199
	wp_embed_register_handler( 'audio', '#^https?://.+?\.(' . join( '|', wp_get_audio_extensions() ) . ')$#i', apply_filters( 'wp_audio_embed_handler', 'wp_embed_handler_audio' ), 9999 );
200
201
	/**
202
	 * Filters the video embed handler callback.
203
	 *
204
	 * @since 3.6.0
205
	 *
206
	 * @param callable $handler Video embed handler callback function.
207
	 */
208
	wp_embed_register_handler( 'video', '#^https?://.+?\.(' . join( '|', wp_get_video_extensions() ) . ')$#i', apply_filters( 'wp_video_embed_handler', 'wp_embed_handler_video' ), 9999 );
209
}
210
211
/**
212
 * YouTube iframe embed handler callback.
213
 *
214
 * Catches YouTube iframe embed URLs that are not parsable by oEmbed but can be translated into a URL that is.
215
 *
216
 * @since 4.0.0
217
 *
218
 * @global WP_Embed $wp_embed
219
 *
220
 * @param array  $matches The RegEx matches from the provided regex when calling
221
 *                        wp_embed_register_handler().
222
 * @param array  $attr    Embed attributes.
223
 * @param string $url     The original URL that was matched by the regex.
224
 * @param array  $rawattr The original unmodified attributes.
225
 * @return string The embed HTML.
226
 */
227
function wp_embed_handler_youtube( $matches, $attr, $url, $rawattr ) {
228
	global $wp_embed;
229
	$embed = $wp_embed->autoembed( sprintf( "https://youtube.com/watch?v=%s", urlencode( $matches[2] ) ) );
230
231
	/**
232
	 * Filters the YoutTube embed output.
233
	 *
234
	 * @since 4.0.0
235
	 *
236
	 * @see wp_embed_handler_youtube()
237
	 *
238
	 * @param string $embed   YouTube embed output.
239
	 * @param array  $attr    An array of embed attributes.
240
	 * @param string $url     The original URL that was matched by the regex.
241
	 * @param array  $rawattr The original unmodified attributes.
242
	 */
243
	return apply_filters( 'wp_embed_handler_youtube', $embed, $attr, $url, $rawattr );
244
}
245
246
/**
247
 * Audio embed handler callback.
248
 *
249
 * @since 3.6.0
250
 *
251
 * @param array  $matches The RegEx matches from the provided regex when calling wp_embed_register_handler().
252
 * @param array  $attr Embed attributes.
253
 * @param string $url The original URL that was matched by the regex.
254
 * @param array  $rawattr The original unmodified attributes.
255
 * @return string The embed HTML.
256
 */
257
function wp_embed_handler_audio( $matches, $attr, $url, $rawattr ) {
0 ignored issues
show
The parameter $matches is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
258
	$audio = sprintf( '[audio src="%s" /]', esc_url( $url ) );
259
260
	/**
261
	 * Filters the audio embed output.
262
	 *
263
	 * @since 3.6.0
264
	 *
265
	 * @param string $audio   Audio embed output.
266
	 * @param array  $attr    An array of embed attributes.
267
	 * @param string $url     The original URL that was matched by the regex.
268
	 * @param array  $rawattr The original unmodified attributes.
269
	 */
270
	return apply_filters( 'wp_embed_handler_audio', $audio, $attr, $url, $rawattr );
271
}
272
273
/**
274
 * Video embed handler callback.
275
 *
276
 * @since 3.6.0
277
 *
278
 * @param array  $matches The RegEx matches from the provided regex when calling wp_embed_register_handler().
279
 * @param array  $attr    Embed attributes.
280
 * @param string $url     The original URL that was matched by the regex.
281
 * @param array  $rawattr The original unmodified attributes.
282
 * @return string The embed HTML.
283
 */
284
function wp_embed_handler_video( $matches, $attr, $url, $rawattr ) {
0 ignored issues
show
The parameter $matches is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
285
	$dimensions = '';
286
	if ( ! empty( $rawattr['width'] ) && ! empty( $rawattr['height'] ) ) {
287
		$dimensions .= sprintf( 'width="%d" ', (int) $rawattr['width'] );
288
		$dimensions .= sprintf( 'height="%d" ', (int) $rawattr['height'] );
289
	}
290
	$video = sprintf( '[video %s src="%s" /]', $dimensions, esc_url( $url ) );
291
292
	/**
293
	 * Filters the video embed output.
294
	 *
295
	 * @since 3.6.0
296
	 *
297
	 * @param string $video   Video embed output.
298
	 * @param array  $attr    An array of embed attributes.
299
	 * @param string $url     The original URL that was matched by the regex.
300
	 * @param array  $rawattr The original unmodified attributes.
301
	 */
302
	return apply_filters( 'wp_embed_handler_video', $video, $attr, $url, $rawattr );
303
}
304
305
/**
306
 * Registers the oEmbed REST API route.
307
 *
308
 * @since 4.4.0
309
 */
310
function wp_oembed_register_route() {
311
	$controller = new WP_oEmbed_Controller();
312
	$controller->register_routes();
313
}
314
315
/**
316
 * Adds oEmbed discovery links in the website <head>.
317
 *
318
 * @since 4.4.0
319
 */
320
function wp_oembed_add_discovery_links() {
321
	$output = '';
322
323
	if ( is_singular() ) {
324
		$output .= '<link rel="alternate" type="application/json+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink() ) ) . '" />' . "\n";
0 ignored issues
show
It seems like get_permalink() can also be of type false; however, get_oembed_endpoint_url() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
325
326
		if ( class_exists( 'SimpleXMLElement' ) ) {
327
			$output .= '<link rel="alternate" type="text/xml+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink(), 'xml' ) ) . '" />' . "\n";
0 ignored issues
show
It seems like get_permalink() can also be of type false; however, get_oembed_endpoint_url() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
328
		}
329
	}
330
331
	/**
332
	 * Filters the oEmbed discovery links HTML.
333
	 *
334
	 * @since 4.4.0
335
	 *
336
	 * @param string $output HTML of the discovery links.
337
	 */
338
	echo apply_filters( 'oembed_discovery_links', $output );
339
}
340
341
/**
342
 * Adds the necessary JavaScript to communicate with the embedded iframes.
343
 *
344
 * @since 4.4.0
345
 */
346
function wp_oembed_add_host_js() {
347
	wp_enqueue_script( 'wp-embed' );
348
}
349
350
/**
351
 * Retrieves the URL to embed a specific post in an iframe.
352
 *
353
 * @since 4.4.0
354
 *
355
 * @param int|WP_Post $post Optional. Post ID or object. Defaults to the current post.
356
 * @return string|false The post embed URL on success, false if the post doesn't exist.
357
 */
358
function get_post_embed_url( $post = null ) {
359
	$post = get_post( $post );
360
361
	if ( ! $post ) {
362
		return false;
363
	}
364
365
	$embed_url     = trailingslashit( get_permalink( $post ) ) . user_trailingslashit( 'embed' );
0 ignored issues
show
It seems like get_permalink($post) targeting get_permalink() can also be of type false; however, trailingslashit() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
366
	$path_conflict = get_page_by_path( str_replace( home_url(), '', $embed_url ), OBJECT, get_post_types( array( 'public' => true ) ) );
367
368
	if ( ! get_option( 'permalink_structure' ) || $path_conflict ) {
369
		$embed_url = add_query_arg( array( 'embed' => 'true' ), get_permalink( $post ) );
370
	}
371
372
	/**
373
	 * Filters the URL to embed a specific post.
374
	 *
375
	 * @since 4.4.0
376
	 *
377
	 * @param string  $embed_url The post embed URL.
378
	 * @param WP_Post $post      The corresponding post object.
379
	 */
380
	return esc_url_raw( apply_filters( 'post_embed_url', $embed_url, $post ) );
381
}
382
383
/**
384
 * Retrieves the oEmbed endpoint URL for a given permalink.
385
 *
386
 * Pass an empty string as the first argument to get the endpoint base URL.
387
 *
388
 * @since 4.4.0
389
 *
390
 * @param string $permalink Optional. The permalink used for the `url` query arg. Default empty.
391
 * @param string $format    Optional. The requested response format. Default 'json'.
392
 * @return string The oEmbed endpoint URL.
393
 */
394
function get_oembed_endpoint_url( $permalink = '', $format = 'json' ) {
395
	$url = rest_url( 'oembed/1.0/embed' );
396
397
	if ( '' !== $permalink ) {
398
		$url = add_query_arg( array(
399
			'url'    => urlencode( $permalink ),
400
			'format' => ( 'json' !== $format ) ? $format : false,
401
		), $url );
402
	}
403
404
	/**
405
	 * Filters the oEmbed endpoint URL.
406
	 *
407
	 * @since 4.4.0
408
	 *
409
	 * @param string $url       The URL to the oEmbed endpoint.
410
	 * @param string $permalink The permalink used for the `url` query arg.
411
	 * @param string $format    The requested response format.
412
	 */
413
	return apply_filters( 'oembed_endpoint_url', $url, $permalink, $format );
414
}
415
416
/**
417
 * Retrieves the embed code for a specific post.
418
 *
419
 * @since 4.4.0
420
 *
421
 * @param int         $width  The width for the response.
422
 * @param int         $height The height for the response.
423
 * @param int|WP_Post $post   Optional. Post ID or object. Default is global `$post`.
424
 * @return string|false Embed code on success, false if post doesn't exist.
425
 */
426
function get_post_embed_html( $width, $height, $post = null ) {
427
	$post = get_post( $post );
428
429
	if ( ! $post ) {
430
		return false;
431
	}
432
433
	$embed_url = get_post_embed_url( $post );
434
435
	$output = '<blockquote class="wp-embedded-content"><a href="' . esc_url( get_permalink( $post ) ) . '">' . get_the_title( $post ) . "</a></blockquote>\n";
0 ignored issues
show
It seems like get_permalink($post) targeting get_permalink() can also be of type false; however, esc_url() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
436
437
	$output .= "<script type='text/javascript'>\n";
438
	$output .= "<!--//--><![CDATA[//><!--\n";
439
	if ( SCRIPT_DEBUG ) {
440
		$output .= file_get_contents( ABSPATH . WPINC . '/js/wp-embed.js' );
441
	} else {
442
		/*
443
		 * If you're looking at a src version of this file, you'll see an "include"
444
		 * statement below. This is used by the `grunt build` process to directly
445
		 * include a minified version of wp-embed.js, instead of using the
446
		 * file_get_contents() method from above.
447
		 *
448
		 * If you're looking at a build version of this file, you'll see a string of
449
		 * minified JavaScript. If you need to debug it, please turn on SCRIPT_DEBUG
450
		 * and edit wp-embed.js directly.
451
		 */
452
		$output .=<<<JS
453
		include "js/wp-embed.min.js"
454
JS;
455
	}
456
	$output .= "\n//--><!]]>";
457
	$output .= "\n</script>";
458
459
	$output .= sprintf(
460
		'<iframe sandbox="allow-scripts" security="restricted" src="%1$s" width="%2$d" height="%3$d" title="%4$s" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" class="wp-embedded-content"></iframe>',
461
		esc_url( $embed_url ),
0 ignored issues
show
It seems like $embed_url defined by get_post_embed_url($post) on line 433 can also be of type false; however, esc_url() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
462
		absint( $width ),
463
		absint( $height ),
464
		esc_attr(
465
			sprintf(
466
				/* translators: 1: post title, 2: site name */
467
				__( '&#8220;%1$s&#8221; &#8212; %2$s' ),
468
				get_the_title( $post ),
469
				get_bloginfo( 'name' )
470
			)
471
		)
472
	);
473
474
	/**
475
	 * Filters the embed HTML output for a given post.
476
	 *
477
	 * @since 4.4.0
478
	 *
479
	 * @param string  $output The default HTML.
480
	 * @param WP_Post $post   Current post object.
481
	 * @param int     $width  Width of the response.
482
	 * @param int     $height Height of the response.
483
	 */
484
	return apply_filters( 'embed_html', $output, $post, $width, $height );
485
}
486
487
/**
488
 * Retrieves the oEmbed response data for a given post.
489
 *
490
 * @since 4.4.0
491
 *
492
 * @param WP_Post|int $post  Post object or ID.
493
 * @param int         $width The requested width.
494
 * @return array|false Response data on success, false if post doesn't exist.
495
 */
496
function get_oembed_response_data( $post, $width ) {
497
	$post  = get_post( $post );
498
	$width = absint( $width );
499
500
	if ( ! $post ) {
501
		return false;
502
	}
503
504
	if ( 'publish' !== get_post_status( $post ) ) {
505
		return false;
506
	}
507
508
	/**
509
	 * Filters the allowed minimum and maximum widths for the oEmbed response.
510
	 *
511
	 * @since 4.4.0
512
	 *
513
	 * @param array $min_max_width {
514
	 *     Minimum and maximum widths for the oEmbed response.
515
	 *
516
	 *     @type int $min Minimum width. Default 200.
517
	 *     @type int $max Maximum width. Default 600.
518
	 * }
519
	 */
520
	$min_max_width = apply_filters( 'oembed_min_max_width', array(
521
		'min' => 200,
522
		'max' => 600
523
	) );
524
525
	$width  = min( max( $min_max_width['min'], $width ), $min_max_width['max'] );
526
	$height = max( ceil( $width / 16 * 9 ), 200 );
527
528
	$data = array(
529
		'version'       => '1.0',
530
		'provider_name' => get_bloginfo( 'name' ),
531
		'provider_url'  => get_home_url(),
532
		'author_name'   => get_bloginfo( 'name' ),
533
		'author_url'    => get_home_url(),
534
		'title'         => $post->post_title,
535
		'type'          => 'link',
536
	);
537
538
	$author = get_userdata( $post->post_author );
539
540
	if ( $author ) {
541
		$data['author_name'] = $author->display_name;
542
		$data['author_url']  = get_author_posts_url( $author->ID );
543
	}
544
545
	/**
546
	 * Filters the oEmbed response data.
547
	 *
548
	 * @since 4.4.0
549
	 *
550
	 * @param array   $data   The response data.
551
	 * @param WP_Post $post   The post object.
552
	 * @param int     $width  The requested width.
553
	 * @param int     $height The calculated height.
554
	 */
555
	return apply_filters( 'oembed_response_data', $data, $post, $width, $height );
556
}
557
558
/**
559
 * Filters the oEmbed response data to return an iframe embed code.
560
 *
561
 * @since 4.4.0
562
 *
563
 * @param array   $data   The response data.
564
 * @param WP_Post $post   The post object.
565
 * @param int     $width  The requested width.
566
 * @param int     $height The calculated height.
567
 * @return array The modified response data.
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<string,integer|double|string|false>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
568
 */
569
function get_oembed_response_data_rich( $data, $post, $width, $height ) {
570
	$data['width']  = absint( $width );
571
	$data['height'] = absint( $height );
572
	$data['type']   = 'rich';
573
	$data['html']   = get_post_embed_html( $width, $height, $post );
574
575
	// Add post thumbnail to response if available.
576
	$thumbnail_id = false;
577
578
	if ( has_post_thumbnail( $post->ID ) ) {
579
		$thumbnail_id = get_post_thumbnail_id( $post->ID );
580
	}
581
582
	if ( 'attachment' === get_post_type( $post ) ) {
583
		if ( wp_attachment_is_image( $post ) ) {
584
			$thumbnail_id = $post->ID;
585
		} else if ( wp_attachment_is( 'video', $post ) ) {
586
			$thumbnail_id = get_post_thumbnail_id( $post );
587
			$data['type'] = 'video';
588
		}
589
	}
590
591
	if ( $thumbnail_id ) {
592
		list( $thumbnail_url, $thumbnail_width, $thumbnail_height ) = wp_get_attachment_image_src( $thumbnail_id, array( $width, 99999 ) );
593
		$data['thumbnail_url']    = $thumbnail_url;
594
		$data['thumbnail_width']  = $thumbnail_width;
595
		$data['thumbnail_height'] = $thumbnail_height;
596
	}
597
598
	return $data;
599
}
600
601
/**
602
 * Ensures that the specified format is either 'json' or 'xml'.
603
 *
604
 * @since 4.4.0
605
 *
606
 * @param string $format The oEmbed response format. Accepts 'json' or 'xml'.
607
 * @return string The format, either 'xml' or 'json'. Default 'json'.
608
 */
609
function wp_oembed_ensure_format( $format ) {
610
	if ( ! in_array( $format, array( 'json', 'xml' ), true ) ) {
611
		return 'json';
612
	}
613
614
	return $format;
615
}
616
617
/**
618
 * Hooks into the REST API output to print XML instead of JSON.
619
 *
620
 * This is only done for the oEmbed API endpoint,
621
 * which supports both formats.
622
 *
623
 * @access private
624
 * @since 4.4.0
625
 *
626
 * @param bool                      $served  Whether the request has already been served.
627
 * @param WP_HTTP_ResponseInterface $result  Result to send to the client. Usually a WP_REST_Response.
628
 * @param WP_REST_Request           $request Request used to generate the response.
629
 * @param WP_REST_Server            $server  Server instance.
630
 * @return true
0 ignored issues
show
Should the return type not be boolean|null|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
631
 */
632
function _oembed_rest_pre_serve_request( $served, $result, $request, $server ) {
633
	$params = $request->get_params();
634
635
	if ( '/oembed/1.0/embed' !== $request->get_route() || 'GET' !== $request->get_method() ) {
636
		return $served;
637
	}
638
639
	if ( ! isset( $params['format'] ) || 'xml' !== $params['format'] ) {
640
		return $served;
641
	}
642
643
	// Embed links inside the request.
644
	$data = $server->response_to_data( $result, false );
645
646
	if ( ! class_exists( 'SimpleXMLElement' ) ) {
647
		status_header( 501 );
648
		die( get_status_header_desc( 501 ) );
0 ignored issues
show
Coding Style Compatibility introduced by
The function _oembed_rest_pre_serve_request() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
649
	}
650
651
	$result = _oembed_create_xml( $data );
652
653
	// Bail if there's no XML.
654
	if ( ! $result ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
655
		status_header( 501 );
656
		return get_status_header_desc( 501 );
657
	}
658
659
	if ( ! headers_sent() ) {
660
		$server->send_header( 'Content-Type', 'text/xml; charset=' . get_option( 'blog_charset' ) );
661
	}
662
663
	echo $result;
664
665
	return true;
666
}
667
668
/**
669
 * Creates an XML string from a given array.
670
 *
671
 * @since 4.4.0
672
 * @access private
673
 *
674
 * @param array            $data The original oEmbed response data.
675
 * @param SimpleXMLElement $node Optional. XML node to append the result to recursively.
676
 * @return string|false XML string on success, false on error.
677
 */
678
function _oembed_create_xml( $data, $node = null ) {
679
	if ( ! is_array( $data ) || empty( $data ) ) {
680
		return false;
681
	}
682
683
	if ( null === $node ) {
684
		$node = new SimpleXMLElement( '<oembed></oembed>' );
685
	}
686
687
	foreach ( $data as $key => $value ) {
688
		if ( is_numeric( $key ) ) {
689
			$key = 'oembed';
690
		}
691
692
		if ( is_array( $value ) ) {
693
			$item = $node->addChild( $key );
694
			_oembed_create_xml( $value, $item );
695
		} else {
696
			$node->addChild( $key, esc_html( $value ) );
697
		}
698
	}
699
700
	return $node->asXML();
701
}
702
703
/**
704
 * Filters the given oEmbed HTML.
705
 *
706
 * If the `$url` isn't on the trusted providers list,
707
 * we need to filter the HTML heavily for security.
708
 *
709
 * Only filters 'rich' and 'html' response types.
710
 *
711
 * @since 4.4.0
712
 *
713
 * @param string $result The oEmbed HTML result.
714
 * @param object $data   A data object result from an oEmbed provider.
715
 * @param string $url    The URL of the content to be embedded.
716
 * @return string The filtered and sanitized oEmbed result.
0 ignored issues
show
Should the return type not be string|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
717
 */
718
function wp_filter_oembed_result( $result, $data, $url ) {
719
	if ( false === $result || ! in_array( $data->type, array( 'rich', 'video' ) ) ) {
720
		return $result;
721
	}
722
723
	$wp_oembed = _wp_oembed_get_object();
724
725
	// Don't modify the HTML for trusted providers.
726
	if ( false !== $wp_oembed->get_provider( $url, array( 'discover' => false ) ) ) {
727
		return $result;
728
	}
729
730
	$allowed_html = array(
731
		'a'          => array(
732
			'href'         => true,
733
		),
734
		'blockquote' => array(),
735
		'iframe'     => array(
736
			'src'          => true,
737
			'width'        => true,
738
			'height'       => true,
739
			'frameborder'  => true,
740
			'marginwidth'  => true,
741
			'marginheight' => true,
742
			'scrolling'    => true,
743
			'title'        => true,
744
		),
745
	);
746
747
	$html = wp_kses( $result, $allowed_html );
748
749
	preg_match( '|(<blockquote>.*?</blockquote>)?.*(<iframe.*?></iframe>)|ms', $html, $content );
750
	// We require at least the iframe to exist.
751
	if ( empty( $content[2] ) ) {
752
		return false;
753
	}
754
	$html = $content[1] . $content[2];
755
756
	if ( ! empty( $content[1] ) ) {
757
		// We have a blockquote to fall back on. Hide the iframe by default.
758
		$html = str_replace( '<iframe', '<iframe style="position: absolute; clip: rect(1px, 1px, 1px, 1px);"', $html );
759
		$html = str_replace( '<blockquote', '<blockquote class="wp-embedded-content"', $html );
760
	}
761
762
	$html = str_replace( '<iframe', '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"', $html );
763
764
	preg_match( '/ src=[\'"]([^\'"]*)[\'"]/', $html, $results );
765
766
	if ( ! empty( $results ) ) {
767
		$secret = wp_generate_password( 10, false );
768
769
		$url = esc_url( "{$results[1]}#?secret=$secret" );
770
771
		$html = str_replace( $results[0], " src=\"$url\" data-secret=\"$secret\"", $html );
772
		$html = str_replace( '<blockquote', "<blockquote data-secret=\"$secret\"", $html );
773
	}
774
775
	return $html;
776
}
777
778
/**
779
 * Filters the string in the 'more' link displayed after a trimmed excerpt.
780
 *
781
 * Replaces '[...]' (appended to automatically generated excerpts) with an
782
 * ellipsis and a "Continue reading" link in the embed template.
783
 *
784
 * @since 4.4.0
785
 *
786
 * @param string $more_string Default 'more' string.
787
 * @return string 'Continue reading' link prepended with an ellipsis.
788
 */
789
function wp_embed_excerpt_more( $more_string ) {
790
	if ( ! is_embed() ) {
791
		return $more_string;
792
	}
793
794
	$link = sprintf( '<a href="%1$s" class="wp-embed-more" target="_top">%2$s</a>',
795
		esc_url( get_permalink() ),
0 ignored issues
show
It seems like get_permalink() can also be of type false; however, esc_url() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
796
		/* translators: %s: Name of current post */
797
		sprintf( __( 'Continue reading %s' ), '<span class="screen-reader-text">' . get_the_title() . '</span>' )
798
	);
799
	return ' &hellip; ' . $link;
800
}
801
802
/**
803
 * Displays the post excerpt for the embed template.
804
 *
805
 * Intended to be used in 'The Loop'.
806
 *
807
 * @since 4.4.0
808
 */
809
function the_excerpt_embed() {
810
	$output = get_the_excerpt();
811
812
	/**
813
	 * Filters the post excerpt for the embed template.
814
	 *
815
	 * @since 4.4.0
816
	 *
817
	 * @param string $output The current post excerpt.
818
	 */
819
	echo apply_filters( 'the_excerpt_embed', $output );
820
}
821
822
/**
823
 * Filters the post excerpt for the embed template.
824
 *
825
 * Shows players for video and audio attachments.
826
 *
827
 * @since 4.4.0
828
 *
829
 * @param string $content The current post excerpt.
830
 * @return string The modified post excerpt.
831
 */
832
function wp_embed_excerpt_attachment( $content ) {
833
	if ( is_attachment() ) {
834
		return prepend_attachment( '' );
835
	}
836
837
	return $content;
838
}
839
840
/**
841
 * Enqueue embed iframe default CSS and JS & fire do_action('enqueue_embed_scripts')
842
 *
843
 * Enqueue PNG fallback CSS for embed iframe for legacy versions of IE.
844
 *
845
 * Allows plugins to queue scripts for the embed iframe end using wp_enqueue_script().
846
 * Runs first in oembed_head().
847
 *
848
 * @since 4.4.0
849
 */
850
function enqueue_embed_scripts() {
851
	wp_enqueue_style( 'wp-embed-template-ie' );
852
853
	/**
854
	 * Fires when scripts and styles are enqueued for the embed iframe.
855
	 *
856
	 * @since 4.4.0
857
	 */
858
	do_action( 'enqueue_embed_scripts' );
859
}
860
861
/**
862
 * Prints the CSS in the embed iframe header.
863
 *
864
 * @since 4.4.0
865
 */
866 View Code Duplication
function print_embed_styles() {
0 ignored issues
show
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
867
	?>
868
	<style type="text/css">
869
	<?php
870
		if ( SCRIPT_DEBUG ) {
871
			readfile( ABSPATH . WPINC . "/css/wp-embed-template.css" );
872
		} else {
873
			/*
874
			 * If you're looking at a src version of this file, you'll see an "include"
875
			 * statement below. This is used by the `grunt build` process to directly
876
			 * include a minified version of wp-oembed-embed.css, instead of using the
877
			 * readfile() method from above.
878
			 *
879
			 * If you're looking at a build version of this file, you'll see a string of
880
			 * minified CSS. If you need to debug it, please turn on SCRIPT_DEBUG
881
			 * and edit wp-embed-template.css directly.
882
			 */
883
			?>
884
			include "css/wp-embed-template.min.css"
885
			<?php
886
		}
887
	?>
888
	</style>
889
	<?php
890
}
891
892
/**
893
 * Prints the JavaScript in the embed iframe header.
894
 *
895
 * @since 4.4.0
896
 */
897 View Code Duplication
function print_embed_scripts() {
0 ignored issues
show
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
898
	?>
899
	<script type="text/javascript">
900
	<?php
901
		if ( SCRIPT_DEBUG ) {
902
			readfile( ABSPATH . WPINC . "/js/wp-embed-template.js" );
903
		} else {
904
			/*
905
			 * If you're looking at a src version of this file, you'll see an "include"
906
			 * statement below. This is used by the `grunt build` process to directly
907
			 * include a minified version of wp-embed-template.js, instead of using the
908
			 * readfile() method from above.
909
			 *
910
			 * If you're looking at a build version of this file, you'll see a string of
911
			 * minified JavaScript. If you need to debug it, please turn on SCRIPT_DEBUG
912
			 * and edit wp-embed-template.js directly.
913
			 */
914
			?>
915
			include "js/wp-embed-template.min.js"
916
			<?php
917
		}
918
	?>
919
	</script>
920
	<?php
921
}
922
923
/**
924
 * Prepare the oembed HTML to be displayed in an RSS feed.
925
 *
926
 * @since 4.4.0
927
 * @access private
928
 *
929
 * @param string $content The content to filter.
930
 * @return string The filtered content.
931
 */
932
function _oembed_filter_feed_content( $content ) {
933
	return str_replace( '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);"', '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"', $content );
934
}
935
936
/**
937
 * Prints the necessary markup for the embed comments button.
938
 *
939
 * @since 4.4.0
940
 */
941
function print_embed_comments_button() {
942
	if ( is_404() || ! ( get_comments_number() || comments_open() ) ) {
943
		return;
944
	}
945
	?>
946
	<div class="wp-embed-comments">
947
		<a href="<?php comments_link(); ?>" target="_top">
948
			<span class="dashicons dashicons-admin-comments"></span>
949
			<?php
950
			printf(
951
				_n(
952
					'%s <span class="screen-reader-text">Comment</span>',
953
					'%s <span class="screen-reader-text">Comments</span>',
954
					get_comments_number()
955
				),
956
				number_format_i18n( get_comments_number() )
957
			);
958
			?>
959
		</a>
960
	</div>
961
	<?php
962
}
963
964
/**
965
 * Prints the necessary markup for the embed sharing button.
966
 *
967
 * @since 4.4.0
968
 */
969
function print_embed_sharing_button() {
970
	if ( is_404() ) {
971
		return;
972
	}
973
	?>
974
	<div class="wp-embed-share">
975
		<button type="button" class="wp-embed-share-dialog-open" aria-label="<?php esc_attr_e( 'Open sharing dialog' ); ?>">
976
			<span class="dashicons dashicons-share"></span>
977
		</button>
978
	</div>
979
	<?php
980
}
981
982
/**
983
 * Prints the necessary markup for the embed sharing dialog.
984
 *
985
 * @since 4.4.0
986
 */
987
function print_embed_sharing_dialog() {
988
	if ( is_404() ) {
989
		return;
990
	}
991
	?>
992
	<div class="wp-embed-share-dialog hidden" role="dialog" aria-label="<?php esc_attr_e( 'Sharing options' ); ?>">
993
		<div class="wp-embed-share-dialog-content">
994
			<div class="wp-embed-share-dialog-text">
995
				<ul class="wp-embed-share-tabs" role="tablist">
996
					<li class="wp-embed-share-tab-button wp-embed-share-tab-button-wordpress" role="presentation">
997
						<button type="button" role="tab" aria-controls="wp-embed-share-tab-wordpress" aria-selected="true" tabindex="0"><?php esc_html_e( 'WordPress Embed' ); ?></button>
998
					</li>
999
					<li class="wp-embed-share-tab-button wp-embed-share-tab-button-html" role="presentation">
1000
						<button type="button" role="tab" aria-controls="wp-embed-share-tab-html" aria-selected="false" tabindex="-1"><?php esc_html_e( 'HTML Embed' ); ?></button>
1001
					</li>
1002
				</ul>
1003
				<div id="wp-embed-share-tab-wordpress" class="wp-embed-share-tab" role="tabpanel" aria-hidden="false">
1004
					<input type="text" value="<?php the_permalink(); ?>" class="wp-embed-share-input" aria-describedby="wp-embed-share-description-wordpress" tabindex="0" readonly/>
1005
1006
					<p class="wp-embed-share-description" id="wp-embed-share-description-wordpress">
1007
						<?php _e( 'Copy and paste this URL into your WordPress site to embed' ); ?>
1008
					</p>
1009
				</div>
1010
				<div id="wp-embed-share-tab-html" class="wp-embed-share-tab" role="tabpanel" aria-hidden="true">
1011
					<textarea class="wp-embed-share-input" aria-describedby="wp-embed-share-description-html" tabindex="0" readonly><?php echo esc_textarea( get_post_embed_html( 600, 400 ) ); ?></textarea>
0 ignored issues
show
It seems like get_post_embed_html(600, 400) targeting get_post_embed_html() can also be of type false; however, esc_textarea() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
1012
1013
					<p class="wp-embed-share-description" id="wp-embed-share-description-html">
1014
						<?php _e( 'Copy and paste this code into your site to embed' ); ?>
1015
					</p>
1016
				</div>
1017
			</div>
1018
1019
			<button type="button" class="wp-embed-share-dialog-close" aria-label="<?php esc_attr_e( 'Close sharing dialog' ); ?>">
1020
				<span class="dashicons dashicons-no"></span>
1021
			</button>
1022
		</div>
1023
	</div>
1024
	<?php
1025
}
1026
1027
/**
1028
 * Prints the necessary markup for the site title in an embed template.
1029
 *
1030
 * @since 4.5.0
1031
 */
1032
function the_embed_site_title() {
1033
	$site_title = sprintf(
1034
		'<a href="%s" target="_top"><img src="%s" srcset="%s 2x" width="32" height="32" alt="" class="wp-embed-site-icon"/><span>%s</span></a>',
1035
		esc_url( home_url() ),
1036
		esc_url( get_site_icon_url( 32, admin_url( 'images/w-logo-blue.png' ) ) ),
1037
		esc_url( get_site_icon_url( 64, admin_url( 'images/w-logo-blue.png' ) ) ),
1038
		esc_html( get_bloginfo( 'name' ) )
1039
	);
1040
1041
	$site_title = '<div class="wp-embed-site-title">' . $site_title . '</div>';
1042
1043
	/**
1044
	 * Filters the site title HTML in the embed footer.
1045
	 *
1046
	 * @since 4.4.0
1047
	 *
1048
	 * @param string $site_title The site title HTML.
1049
	 */
1050
	echo apply_filters( 'embed_site_title_html', $site_title );
1051
}
1052
1053
/**
1054
 * Filters the oEmbed result before any HTTP requests are made.
1055
 *
1056
 * If the URL belongs to the current site, the result is fetched directly instead of
1057
 * going through the oEmbed discovery process.
1058
 *
1059
 * @since 4.5.3
1060
 *
1061
 * @param null|string $result The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. Default null.
1062
 * @param string      $url    The URL that should be inspected for discovery `<link>` tags.
1063
 * @param array       $args   oEmbed remote get arguments.
1064
 * @return null|string The UNSANITIZED (and potentially unsafe) HTML that should be used to embed.
1065
 *                     Null if the URL does not belong to the current site.
1066
 */
1067
function wp_filter_pre_oembed_result( $result, $url, $args ) {
1068
	$post_id = url_to_postid( $url );
1069
1070
	/** This filter is documented in wp-includes/class-wp-oembed-controller.php */
1071
	$post_id = apply_filters( 'oembed_request_post_id', $post_id, $url );
1072
1073
	if ( ! $post_id ) {
1074
		return $result;
1075
	}
1076
1077
	$width = isset( $args['width'] ) ? $args['width'] : 0;
1078
1079
	$data = get_oembed_response_data( $post_id, $width );
1080
	$data = _wp_oembed_get_object()->data2html( (object) $data, $url );
1081
1082
	if ( ! $data ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
1083
		return $result;
1084
	}
1085
1086
	return $data;
1087
}
1088