@@ -61,11 +61,13 @@ |
||
61 | 61 | * @return array Default embed parameters. |
62 | 62 | */ |
63 | 63 | function wp_embed_defaults( $url = '' ) { |
64 | - if ( ! empty( $GLOBALS['content_width'] ) ) |
|
65 | - $width = (int) $GLOBALS['content_width']; |
|
64 | + if ( ! empty( $GLOBALS['content_width'] ) ) { |
|
65 | + $width = (int) $GLOBALS['content_width']; |
|
66 | + } |
|
66 | 67 | |
67 | - if ( empty( $width ) ) |
|
68 | - $width = 500; |
|
68 | + if ( empty( $width ) ) { |
|
69 | + $width = 500; |
|
70 | + } |
|
69 | 71 | |
70 | 72 | $height = min( ceil( $width * 1.5 ), 1000 ); |
71 | 73 |
@@ -481,7 +481,7 @@ |
||
481 | 481 | * |
482 | 482 | * @param WP_Post|int $post Post object or ID. |
483 | 483 | * @param int $width The requested width. |
484 | - * @return array|false Response data on success, false if post doesn't exist. |
|
484 | + * @return string Response data on success, false if post doesn't exist. |
|
485 | 485 | */ |
486 | 486 | function get_oembed_response_data( $post, $width ) { |
487 | 487 | $post = get_post( $post ); |
@@ -22,9 +22,9 @@ discard block |
||
22 | 22 | * @param int $priority Optional. Used to specify the order in which the registered handlers will |
23 | 23 | * be tested. Default 10. |
24 | 24 | */ |
25 | -function wp_embed_register_handler( $id, $regex, $callback, $priority = 10 ) { |
|
25 | +function wp_embed_register_handler($id, $regex, $callback, $priority = 10) { |
|
26 | 26 | global $wp_embed; |
27 | - $wp_embed->register_handler( $id, $regex, $callback, $priority ); |
|
27 | + $wp_embed->register_handler($id, $regex, $callback, $priority); |
|
28 | 28 | } |
29 | 29 | |
30 | 30 | /** |
@@ -37,9 +37,9 @@ discard block |
||
37 | 37 | * @param string $id The handler ID that should be removed. |
38 | 38 | * @param int $priority Optional. The priority of the handler to be removed. Default 10. |
39 | 39 | */ |
40 | -function wp_embed_unregister_handler( $id, $priority = 10 ) { |
|
40 | +function wp_embed_unregister_handler($id, $priority = 10) { |
|
41 | 41 | global $wp_embed; |
42 | - $wp_embed->unregister_handler( $id, $priority ); |
|
42 | + $wp_embed->unregister_handler($id, $priority); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | /** |
@@ -60,14 +60,14 @@ discard block |
||
60 | 60 | * |
61 | 61 | * @return array Default embed parameters. |
62 | 62 | */ |
63 | -function wp_embed_defaults( $url = '' ) { |
|
64 | - if ( ! empty( $GLOBALS['content_width'] ) ) |
|
63 | +function wp_embed_defaults($url = '') { |
|
64 | + if ( ! empty($GLOBALS['content_width'])) |
|
65 | 65 | $width = (int) $GLOBALS['content_width']; |
66 | 66 | |
67 | - if ( empty( $width ) ) |
|
67 | + if (empty($width)) |
|
68 | 68 | $width = 500; |
69 | 69 | |
70 | - $height = min( ceil( $width * 1.5 ), 1000 ); |
|
70 | + $height = min(ceil($width * 1.5), 1000); |
|
71 | 71 | |
72 | 72 | /** |
73 | 73 | * Filters the default array of embed dimensions. |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | * in pixels (in that order). |
79 | 79 | * @param string $url The URL that should be embedded. |
80 | 80 | */ |
81 | - return apply_filters( 'embed_defaults', compact( 'width', 'height' ), $url ); |
|
81 | + return apply_filters('embed_defaults', compact('width', 'height'), $url); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | /** |
@@ -93,9 +93,9 @@ discard block |
||
93 | 93 | * Default empty. |
94 | 94 | * @return false|string False on failure or the embed HTML on success. |
95 | 95 | */ |
96 | -function wp_oembed_get( $url, $args = '' ) { |
|
96 | +function wp_oembed_get($url, $args = '') { |
|
97 | 97 | $oembed = _wp_oembed_get_object(); |
98 | - return $oembed->get_html( $url, $args ); |
|
98 | + return $oembed->get_html($url, $args); |
|
99 | 99 | } |
100 | 100 | |
101 | 101 | /** |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | function _wp_oembed_get_object() { |
112 | 112 | static $wp_oembed = null; |
113 | 113 | |
114 | - if ( is_null( $wp_oembed ) ) { |
|
114 | + if (is_null($wp_oembed)) { |
|
115 | 115 | $wp_oembed = new WP_oEmbed(); |
116 | 116 | } |
117 | 117 | return $wp_oembed; |
@@ -129,12 +129,12 @@ discard block |
||
129 | 129 | * @param string $provider The URL to the oEmbed provider. |
130 | 130 | * @param boolean $regex Optional. Whether the `$format` parameter is in a RegEx format. Default false. |
131 | 131 | */ |
132 | -function wp_oembed_add_provider( $format, $provider, $regex = false ) { |
|
133 | - if ( did_action( 'plugins_loaded' ) ) { |
|
132 | +function wp_oembed_add_provider($format, $provider, $regex = false) { |
|
133 | + if (did_action('plugins_loaded')) { |
|
134 | 134 | $oembed = _wp_oembed_get_object(); |
135 | - $oembed->providers[$format] = array( $provider, $regex ); |
|
135 | + $oembed->providers[$format] = array($provider, $regex); |
|
136 | 136 | } else { |
137 | - WP_oEmbed::_add_provider_early( $format, $provider, $regex ); |
|
137 | + WP_oEmbed::_add_provider_early($format, $provider, $regex); |
|
138 | 138 | } |
139 | 139 | } |
140 | 140 | |
@@ -148,16 +148,16 @@ discard block |
||
148 | 148 | * @param string $format The URL format for the oEmbed provider to remove. |
149 | 149 | * @return bool Was the provider removed successfully? |
150 | 150 | */ |
151 | -function wp_oembed_remove_provider( $format ) { |
|
152 | - if ( did_action( 'plugins_loaded' ) ) { |
|
151 | +function wp_oembed_remove_provider($format) { |
|
152 | + if (did_action('plugins_loaded')) { |
|
153 | 153 | $oembed = _wp_oembed_get_object(); |
154 | 154 | |
155 | - if ( isset( $oembed->providers[ $format ] ) ) { |
|
156 | - unset( $oembed->providers[ $format ] ); |
|
155 | + if (isset($oembed->providers[$format])) { |
|
156 | + unset($oembed->providers[$format]); |
|
157 | 157 | return true; |
158 | 158 | } |
159 | 159 | } else { |
160 | - WP_oEmbed::_remove_provider_early( $format ); |
|
160 | + WP_oEmbed::_remove_provider_early($format); |
|
161 | 161 | } |
162 | 162 | |
163 | 163 | return false; |
@@ -183,11 +183,11 @@ discard block |
||
183 | 183 | * |
184 | 184 | * @param bool $maybe_load_embeds Whether to load the embeds library. Default true. |
185 | 185 | */ |
186 | - if ( ! apply_filters( 'load_default_embeds', true ) ) { |
|
186 | + if ( ! apply_filters('load_default_embeds', true)) { |
|
187 | 187 | return; |
188 | 188 | } |
189 | 189 | |
190 | - wp_embed_register_handler( 'youtube_embed_url', '#https?://(www.)?youtube\.com/(?:v|embed)/([^/]+)#i', 'wp_embed_handler_youtube' ); |
|
190 | + wp_embed_register_handler('youtube_embed_url', '#https?://(www.)?youtube\.com/(?:v|embed)/([^/]+)#i', 'wp_embed_handler_youtube'); |
|
191 | 191 | |
192 | 192 | /** |
193 | 193 | * Filters the audio embed handler callback. |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | * |
197 | 197 | * @param callable $handler Audio embed handler callback function. |
198 | 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 ); |
|
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 | 200 | |
201 | 201 | /** |
202 | 202 | * Filters the video embed handler callback. |
@@ -205,7 +205,7 @@ discard block |
||
205 | 205 | * |
206 | 206 | * @param callable $handler Video embed handler callback function. |
207 | 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 ); |
|
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 | 209 | } |
210 | 210 | |
211 | 211 | /** |
@@ -224,9 +224,9 @@ discard block |
||
224 | 224 | * @param array $rawattr The original unmodified attributes. |
225 | 225 | * @return string The embed HTML. |
226 | 226 | */ |
227 | -function wp_embed_handler_youtube( $matches, $attr, $url, $rawattr ) { |
|
227 | +function wp_embed_handler_youtube($matches, $attr, $url, $rawattr) { |
|
228 | 228 | global $wp_embed; |
229 | - $embed = $wp_embed->autoembed( sprintf( "https://youtube.com/watch?v=%s", urlencode( $matches[2] ) ) ); |
|
229 | + $embed = $wp_embed->autoembed(sprintf("https://youtube.com/watch?v=%s", urlencode($matches[2]))); |
|
230 | 230 | |
231 | 231 | /** |
232 | 232 | * Filters the YoutTube embed output. |
@@ -240,7 +240,7 @@ discard block |
||
240 | 240 | * @param string $url The original URL that was matched by the regex. |
241 | 241 | * @param array $rawattr The original unmodified attributes. |
242 | 242 | */ |
243 | - return apply_filters( 'wp_embed_handler_youtube', $embed, $attr, $url, $rawattr ); |
|
243 | + return apply_filters('wp_embed_handler_youtube', $embed, $attr, $url, $rawattr); |
|
244 | 244 | } |
245 | 245 | |
246 | 246 | /** |
@@ -254,8 +254,8 @@ discard block |
||
254 | 254 | * @param array $rawattr The original unmodified attributes. |
255 | 255 | * @return string The embed HTML. |
256 | 256 | */ |
257 | -function wp_embed_handler_audio( $matches, $attr, $url, $rawattr ) { |
|
258 | - $audio = sprintf( '[audio src="%s" /]', esc_url( $url ) ); |
|
257 | +function wp_embed_handler_audio($matches, $attr, $url, $rawattr) { |
|
258 | + $audio = sprintf('[audio src="%s" /]', esc_url($url)); |
|
259 | 259 | |
260 | 260 | /** |
261 | 261 | * Filters the audio embed output. |
@@ -267,7 +267,7 @@ discard block |
||
267 | 267 | * @param string $url The original URL that was matched by the regex. |
268 | 268 | * @param array $rawattr The original unmodified attributes. |
269 | 269 | */ |
270 | - return apply_filters( 'wp_embed_handler_audio', $audio, $attr, $url, $rawattr ); |
|
270 | + return apply_filters('wp_embed_handler_audio', $audio, $attr, $url, $rawattr); |
|
271 | 271 | } |
272 | 272 | |
273 | 273 | /** |
@@ -281,13 +281,13 @@ discard block |
||
281 | 281 | * @param array $rawattr The original unmodified attributes. |
282 | 282 | * @return string The embed HTML. |
283 | 283 | */ |
284 | -function wp_embed_handler_video( $matches, $attr, $url, $rawattr ) { |
|
284 | +function wp_embed_handler_video($matches, $attr, $url, $rawattr) { |
|
285 | 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'] ); |
|
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 | 289 | } |
290 | - $video = sprintf( '[video %s src="%s" /]', $dimensions, esc_url( $url ) ); |
|
290 | + $video = sprintf('[video %s src="%s" /]', $dimensions, esc_url($url)); |
|
291 | 291 | |
292 | 292 | /** |
293 | 293 | * Filters the video embed output. |
@@ -299,7 +299,7 @@ discard block |
||
299 | 299 | * @param string $url The original URL that was matched by the regex. |
300 | 300 | * @param array $rawattr The original unmodified attributes. |
301 | 301 | */ |
302 | - return apply_filters( 'wp_embed_handler_video', $video, $attr, $url, $rawattr ); |
|
302 | + return apply_filters('wp_embed_handler_video', $video, $attr, $url, $rawattr); |
|
303 | 303 | } |
304 | 304 | |
305 | 305 | /** |
@@ -320,11 +320,11 @@ discard block |
||
320 | 320 | function wp_oembed_add_discovery_links() { |
321 | 321 | $output = ''; |
322 | 322 | |
323 | - if ( is_singular() ) { |
|
324 | - $output .= '<link rel="alternate" type="application/json+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink() ) ) . '" />' . "\n"; |
|
323 | + if (is_singular()) { |
|
324 | + $output .= '<link rel="alternate" type="application/json+oembed" href="'.esc_url(get_oembed_endpoint_url(get_permalink())).'" />'."\n"; |
|
325 | 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"; |
|
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"; |
|
328 | 328 | } |
329 | 329 | } |
330 | 330 | |
@@ -335,7 +335,7 @@ discard block |
||
335 | 335 | * |
336 | 336 | * @param string $output HTML of the discovery links. |
337 | 337 | */ |
338 | - echo apply_filters( 'oembed_discovery_links', $output ); |
|
338 | + echo apply_filters('oembed_discovery_links', $output); |
|
339 | 339 | } |
340 | 340 | |
341 | 341 | /** |
@@ -344,7 +344,7 @@ discard block |
||
344 | 344 | * @since 4.4.0 |
345 | 345 | */ |
346 | 346 | function wp_oembed_add_host_js() { |
347 | - wp_enqueue_script( 'wp-embed' ); |
|
347 | + wp_enqueue_script('wp-embed'); |
|
348 | 348 | } |
349 | 349 | |
350 | 350 | /** |
@@ -355,18 +355,18 @@ discard block |
||
355 | 355 | * @param int|WP_Post $post Optional. Post ID or object. Defaults to the current post. |
356 | 356 | * @return string|false The post embed URL on success, false if the post doesn't exist. |
357 | 357 | */ |
358 | -function get_post_embed_url( $post = null ) { |
|
359 | - $post = get_post( $post ); |
|
358 | +function get_post_embed_url($post = null) { |
|
359 | + $post = get_post($post); |
|
360 | 360 | |
361 | - if ( ! $post ) { |
|
361 | + if ( ! $post) { |
|
362 | 362 | return false; |
363 | 363 | } |
364 | 364 | |
365 | - $embed_url = trailingslashit( get_permalink( $post ) ) . user_trailingslashit( 'embed' ); |
|
366 | - $path_conflict = get_page_by_path( str_replace( home_url(), '', $embed_url ), OBJECT, get_post_types( array( 'public' => true ) ) ); |
|
365 | + $embed_url = trailingslashit(get_permalink($post)).user_trailingslashit('embed'); |
|
366 | + $path_conflict = get_page_by_path(str_replace(home_url(), '', $embed_url), OBJECT, get_post_types(array('public' => true))); |
|
367 | 367 | |
368 | - if ( ! get_option( 'permalink_structure' ) || $path_conflict ) { |
|
369 | - $embed_url = add_query_arg( array( 'embed' => 'true' ), get_permalink( $post ) ); |
|
368 | + if ( ! get_option('permalink_structure') || $path_conflict) { |
|
369 | + $embed_url = add_query_arg(array('embed' => 'true'), get_permalink($post)); |
|
370 | 370 | } |
371 | 371 | |
372 | 372 | /** |
@@ -377,7 +377,7 @@ discard block |
||
377 | 377 | * @param string $embed_url The post embed URL. |
378 | 378 | * @param WP_Post $post The corresponding post object. |
379 | 379 | */ |
380 | - return esc_url_raw( apply_filters( 'post_embed_url', $embed_url, $post ) ); |
|
380 | + return esc_url_raw(apply_filters('post_embed_url', $embed_url, $post)); |
|
381 | 381 | } |
382 | 382 | |
383 | 383 | /** |
@@ -391,14 +391,14 @@ discard block |
||
391 | 391 | * @param string $format Optional. The requested response format. Default 'json'. |
392 | 392 | * @return string The oEmbed endpoint URL. |
393 | 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 ); |
|
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 | 402 | } |
403 | 403 | |
404 | 404 | /** |
@@ -410,7 +410,7 @@ discard block |
||
410 | 410 | * @param string $permalink The permalink used for the `url` query arg. |
411 | 411 | * @param string $format The requested response format. |
412 | 412 | */ |
413 | - return apply_filters( 'oembed_endpoint_url', $url, $permalink, $format ); |
|
413 | + return apply_filters('oembed_endpoint_url', $url, $permalink, $format); |
|
414 | 414 | } |
415 | 415 | |
416 | 416 | /** |
@@ -423,21 +423,21 @@ discard block |
||
423 | 423 | * @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`. |
424 | 424 | * @return string|false Embed code on success, false if post doesn't exist. |
425 | 425 | */ |
426 | -function get_post_embed_html( $width, $height, $post = null ) { |
|
427 | - $post = get_post( $post ); |
|
426 | +function get_post_embed_html($width, $height, $post = null) { |
|
427 | + $post = get_post($post); |
|
428 | 428 | |
429 | - if ( ! $post ) { |
|
429 | + if ( ! $post) { |
|
430 | 430 | return false; |
431 | 431 | } |
432 | 432 | |
433 | - $embed_url = get_post_embed_url( $post ); |
|
433 | + $embed_url = get_post_embed_url($post); |
|
434 | 434 | |
435 | - $output = '<blockquote class="wp-embedded-content"><a href="' . esc_url( get_permalink( $post ) ) . '">' . get_the_title( $post ) . "</a></blockquote>\n"; |
|
435 | + $output = '<blockquote class="wp-embedded-content"><a href="'.esc_url(get_permalink($post)).'">'.get_the_title($post)."</a></blockquote>\n"; |
|
436 | 436 | |
437 | 437 | $output .= "<script type='text/javascript'>\n"; |
438 | 438 | $output .= "<!--//--><![CDATA[//><!--\n"; |
439 | - if ( SCRIPT_DEBUG ) { |
|
440 | - $output .= file_get_contents( ABSPATH . WPINC . '/js/wp-embed.js' ); |
|
439 | + if (SCRIPT_DEBUG) { |
|
440 | + $output .= file_get_contents(ABSPATH.WPINC.'/js/wp-embed.js'); |
|
441 | 441 | } else { |
442 | 442 | /* |
443 | 443 | * If you're looking at a src version of this file, you'll see an "include" |
@@ -449,7 +449,7 @@ discard block |
||
449 | 449 | * minified JavaScript. If you need to debug it, please turn on SCRIPT_DEBUG |
450 | 450 | * and edit wp-embed.js directly. |
451 | 451 | */ |
452 | - $output .=<<<JS |
|
452 | + $output .= <<<JS |
|
453 | 453 | include "js/wp-embed.min.js" |
454 | 454 | JS; |
455 | 455 | } |
@@ -458,15 +458,15 @@ discard block |
||
458 | 458 | |
459 | 459 | $output .= sprintf( |
460 | 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 ), |
|
462 | - absint( $width ), |
|
463 | - absint( $height ), |
|
461 | + esc_url($embed_url), |
|
462 | + absint($width), |
|
463 | + absint($height), |
|
464 | 464 | esc_attr( |
465 | 465 | sprintf( |
466 | 466 | /* translators: 1: post title, 2: site name */ |
467 | - __( '“%1$s” — %2$s' ), |
|
468 | - get_the_title( $post ), |
|
469 | - get_bloginfo( 'name' ) |
|
467 | + __('“%1$s” — %2$s'), |
|
468 | + get_the_title($post), |
|
469 | + get_bloginfo('name') |
|
470 | 470 | ) |
471 | 471 | ) |
472 | 472 | ); |
@@ -481,7 +481,7 @@ discard block |
||
481 | 481 | * @param int $width Width of the response. |
482 | 482 | * @param int $height Height of the response. |
483 | 483 | */ |
484 | - return apply_filters( 'embed_html', $output, $post, $width, $height ); |
|
484 | + return apply_filters('embed_html', $output, $post, $width, $height); |
|
485 | 485 | } |
486 | 486 | |
487 | 487 | /** |
@@ -493,15 +493,15 @@ discard block |
||
493 | 493 | * @param int $width The requested width. |
494 | 494 | * @return array|false Response data on success, false if post doesn't exist. |
495 | 495 | */ |
496 | -function get_oembed_response_data( $post, $width ) { |
|
497 | - $post = get_post( $post ); |
|
498 | - $width = absint( $width ); |
|
496 | +function get_oembed_response_data($post, $width) { |
|
497 | + $post = get_post($post); |
|
498 | + $width = absint($width); |
|
499 | 499 | |
500 | - if ( ! $post ) { |
|
500 | + if ( ! $post) { |
|
501 | 501 | return false; |
502 | 502 | } |
503 | 503 | |
504 | - if ( 'publish' !== get_post_status( $post ) ) { |
|
504 | + if ('publish' !== get_post_status($post)) { |
|
505 | 505 | return false; |
506 | 506 | } |
507 | 507 | |
@@ -517,29 +517,29 @@ discard block |
||
517 | 517 | * @type int $max Maximum width. Default 600. |
518 | 518 | * } |
519 | 519 | */ |
520 | - $min_max_width = apply_filters( 'oembed_min_max_width', array( |
|
520 | + $min_max_width = apply_filters('oembed_min_max_width', array( |
|
521 | 521 | 'min' => 200, |
522 | 522 | 'max' => 600 |
523 | - ) ); |
|
523 | + )); |
|
524 | 524 | |
525 | - $width = min( max( $min_max_width['min'], $width ), $min_max_width['max'] ); |
|
526 | - $height = max( ceil( $width / 16 * 9 ), 200 ); |
|
525 | + $width = min(max($min_max_width['min'], $width), $min_max_width['max']); |
|
526 | + $height = max(ceil($width / 16 * 9), 200); |
|
527 | 527 | |
528 | 528 | $data = array( |
529 | 529 | 'version' => '1.0', |
530 | - 'provider_name' => get_bloginfo( 'name' ), |
|
530 | + 'provider_name' => get_bloginfo('name'), |
|
531 | 531 | 'provider_url' => get_home_url(), |
532 | - 'author_name' => get_bloginfo( 'name' ), |
|
532 | + 'author_name' => get_bloginfo('name'), |
|
533 | 533 | 'author_url' => get_home_url(), |
534 | 534 | 'title' => $post->post_title, |
535 | 535 | 'type' => 'link', |
536 | 536 | ); |
537 | 537 | |
538 | - $author = get_userdata( $post->post_author ); |
|
538 | + $author = get_userdata($post->post_author); |
|
539 | 539 | |
540 | - if ( $author ) { |
|
540 | + if ($author) { |
|
541 | 541 | $data['author_name'] = $author->display_name; |
542 | - $data['author_url'] = get_author_posts_url( $author->ID ); |
|
542 | + $data['author_url'] = get_author_posts_url($author->ID); |
|
543 | 543 | } |
544 | 544 | |
545 | 545 | /** |
@@ -552,7 +552,7 @@ discard block |
||
552 | 552 | * @param int $width The requested width. |
553 | 553 | * @param int $height The calculated height. |
554 | 554 | */ |
555 | - return apply_filters( 'oembed_response_data', $data, $post, $width, $height ); |
|
555 | + return apply_filters('oembed_response_data', $data, $post, $width, $height); |
|
556 | 556 | } |
557 | 557 | |
558 | 558 | /** |
@@ -566,30 +566,30 @@ discard block |
||
566 | 566 | * @param int $height The calculated height. |
567 | 567 | * @return array The modified response data. |
568 | 568 | */ |
569 | -function get_oembed_response_data_rich( $data, $post, $width, $height ) { |
|
570 | - $data['width'] = absint( $width ); |
|
571 | - $data['height'] = absint( $height ); |
|
569 | +function get_oembed_response_data_rich($data, $post, $width, $height) { |
|
570 | + $data['width'] = absint($width); |
|
571 | + $data['height'] = absint($height); |
|
572 | 572 | $data['type'] = 'rich'; |
573 | - $data['html'] = get_post_embed_html( $width, $height, $post ); |
|
573 | + $data['html'] = get_post_embed_html($width, $height, $post); |
|
574 | 574 | |
575 | 575 | // Add post thumbnail to response if available. |
576 | 576 | $thumbnail_id = false; |
577 | 577 | |
578 | - if ( has_post_thumbnail( $post->ID ) ) { |
|
579 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
578 | + if (has_post_thumbnail($post->ID)) { |
|
579 | + $thumbnail_id = get_post_thumbnail_id($post->ID); |
|
580 | 580 | } |
581 | 581 | |
582 | - if ( 'attachment' === get_post_type( $post ) ) { |
|
583 | - if ( wp_attachment_is_image( $post ) ) { |
|
582 | + if ('attachment' === get_post_type($post)) { |
|
583 | + if (wp_attachment_is_image($post)) { |
|
584 | 584 | $thumbnail_id = $post->ID; |
585 | - } else if ( wp_attachment_is( 'video', $post ) ) { |
|
586 | - $thumbnail_id = get_post_thumbnail_id( $post ); |
|
585 | + } else if (wp_attachment_is('video', $post)) { |
|
586 | + $thumbnail_id = get_post_thumbnail_id($post); |
|
587 | 587 | $data['type'] = 'video'; |
588 | 588 | } |
589 | 589 | } |
590 | 590 | |
591 | - if ( $thumbnail_id ) { |
|
592 | - list( $thumbnail_url, $thumbnail_width, $thumbnail_height ) = wp_get_attachment_image_src( $thumbnail_id, array( $width, 99999 ) ); |
|
591 | + if ($thumbnail_id) { |
|
592 | + list($thumbnail_url, $thumbnail_width, $thumbnail_height) = wp_get_attachment_image_src($thumbnail_id, array($width, 99999)); |
|
593 | 593 | $data['thumbnail_url'] = $thumbnail_url; |
594 | 594 | $data['thumbnail_width'] = $thumbnail_width; |
595 | 595 | $data['thumbnail_height'] = $thumbnail_height; |
@@ -606,8 +606,8 @@ discard block |
||
606 | 606 | * @param string $format The oEmbed response format. Accepts 'json' or 'xml'. |
607 | 607 | * @return string The format, either 'xml' or 'json'. Default 'json'. |
608 | 608 | */ |
609 | -function wp_oembed_ensure_format( $format ) { |
|
610 | - if ( ! in_array( $format, array( 'json', 'xml' ), true ) ) { |
|
609 | +function wp_oembed_ensure_format($format) { |
|
610 | + if ( ! in_array($format, array('json', 'xml'), true)) { |
|
611 | 611 | return 'json'; |
612 | 612 | } |
613 | 613 | |
@@ -629,35 +629,35 @@ discard block |
||
629 | 629 | * @param WP_REST_Server $server Server instance. |
630 | 630 | * @return true |
631 | 631 | */ |
632 | -function _oembed_rest_pre_serve_request( $served, $result, $request, $server ) { |
|
632 | +function _oembed_rest_pre_serve_request($served, $result, $request, $server) { |
|
633 | 633 | $params = $request->get_params(); |
634 | 634 | |
635 | - if ( '/oembed/1.0/embed' !== $request->get_route() || 'GET' !== $request->get_method() ) { |
|
635 | + if ('/oembed/1.0/embed' !== $request->get_route() || 'GET' !== $request->get_method()) { |
|
636 | 636 | return $served; |
637 | 637 | } |
638 | 638 | |
639 | - if ( ! isset( $params['format'] ) || 'xml' !== $params['format'] ) { |
|
639 | + if ( ! isset($params['format']) || 'xml' !== $params['format']) { |
|
640 | 640 | return $served; |
641 | 641 | } |
642 | 642 | |
643 | 643 | // Embed links inside the request. |
644 | - $data = $server->response_to_data( $result, false ); |
|
644 | + $data = $server->response_to_data($result, false); |
|
645 | 645 | |
646 | - if ( ! class_exists( 'SimpleXMLElement' ) ) { |
|
647 | - status_header( 501 ); |
|
648 | - die( get_status_header_desc( 501 ) ); |
|
646 | + if ( ! class_exists('SimpleXMLElement')) { |
|
647 | + status_header(501); |
|
648 | + die(get_status_header_desc(501)); |
|
649 | 649 | } |
650 | 650 | |
651 | - $result = _oembed_create_xml( $data ); |
|
651 | + $result = _oembed_create_xml($data); |
|
652 | 652 | |
653 | 653 | // Bail if there's no XML. |
654 | - if ( ! $result ) { |
|
655 | - status_header( 501 ); |
|
656 | - return get_status_header_desc( 501 ); |
|
654 | + if ( ! $result) { |
|
655 | + status_header(501); |
|
656 | + return get_status_header_desc(501); |
|
657 | 657 | } |
658 | 658 | |
659 | - if ( ! headers_sent() ) { |
|
660 | - $server->send_header( 'Content-Type', 'text/xml; charset=' . get_option( 'blog_charset' ) ); |
|
659 | + if ( ! headers_sent()) { |
|
660 | + $server->send_header('Content-Type', 'text/xml; charset='.get_option('blog_charset')); |
|
661 | 661 | } |
662 | 662 | |
663 | 663 | echo $result; |
@@ -675,25 +675,25 @@ discard block |
||
675 | 675 | * @param SimpleXMLElement $node Optional. XML node to append the result to recursively. |
676 | 676 | * @return string|false XML string on success, false on error. |
677 | 677 | */ |
678 | -function _oembed_create_xml( $data, $node = null ) { |
|
679 | - if ( ! is_array( $data ) || empty( $data ) ) { |
|
678 | +function _oembed_create_xml($data, $node = null) { |
|
679 | + if ( ! is_array($data) || empty($data)) { |
|
680 | 680 | return false; |
681 | 681 | } |
682 | 682 | |
683 | - if ( null === $node ) { |
|
684 | - $node = new SimpleXMLElement( '<oembed></oembed>' ); |
|
683 | + if (null === $node) { |
|
684 | + $node = new SimpleXMLElement('<oembed></oembed>'); |
|
685 | 685 | } |
686 | 686 | |
687 | - foreach ( $data as $key => $value ) { |
|
688 | - if ( is_numeric( $key ) ) { |
|
687 | + foreach ($data as $key => $value) { |
|
688 | + if (is_numeric($key)) { |
|
689 | 689 | $key = 'oembed'; |
690 | 690 | } |
691 | 691 | |
692 | - if ( is_array( $value ) ) { |
|
693 | - $item = $node->addChild( $key ); |
|
694 | - _oembed_create_xml( $value, $item ); |
|
692 | + if (is_array($value)) { |
|
693 | + $item = $node->addChild($key); |
|
694 | + _oembed_create_xml($value, $item); |
|
695 | 695 | } else { |
696 | - $node->addChild( $key, esc_html( $value ) ); |
|
696 | + $node->addChild($key, esc_html($value)); |
|
697 | 697 | } |
698 | 698 | } |
699 | 699 | |
@@ -715,15 +715,15 @@ discard block |
||
715 | 715 | * @param string $url The URL of the content to be embedded. |
716 | 716 | * @return string The filtered and sanitized oEmbed result. |
717 | 717 | */ |
718 | -function wp_filter_oembed_result( $result, $data, $url ) { |
|
719 | - if ( false === $result || ! in_array( $data->type, array( 'rich', 'video' ) ) ) { |
|
718 | +function wp_filter_oembed_result($result, $data, $url) { |
|
719 | + if (false === $result || ! in_array($data->type, array('rich', 'video'))) { |
|
720 | 720 | return $result; |
721 | 721 | } |
722 | 722 | |
723 | 723 | $wp_oembed = _wp_oembed_get_object(); |
724 | 724 | |
725 | 725 | // Don't modify the HTML for trusted providers. |
726 | - if ( false !== $wp_oembed->get_provider( $url, array( 'discover' => false ) ) ) { |
|
726 | + if (false !== $wp_oembed->get_provider($url, array('discover' => false))) { |
|
727 | 727 | return $result; |
728 | 728 | } |
729 | 729 | |
@@ -744,32 +744,32 @@ discard block |
||
744 | 744 | ), |
745 | 745 | ); |
746 | 746 | |
747 | - $html = wp_kses( $result, $allowed_html ); |
|
747 | + $html = wp_kses($result, $allowed_html); |
|
748 | 748 | |
749 | - preg_match( '|(<blockquote>.*?</blockquote>)?.*(<iframe.*?></iframe>)|ms', $html, $content ); |
|
749 | + preg_match('|(<blockquote>.*?</blockquote>)?.*(<iframe.*?></iframe>)|ms', $html, $content); |
|
750 | 750 | // We require at least the iframe to exist. |
751 | - if ( empty( $content[2] ) ) { |
|
751 | + if (empty($content[2])) { |
|
752 | 752 | return false; |
753 | 753 | } |
754 | - $html = $content[1] . $content[2]; |
|
754 | + $html = $content[1].$content[2]; |
|
755 | 755 | |
756 | - if ( ! empty( $content[1] ) ) { |
|
756 | + if ( ! empty($content[1])) { |
|
757 | 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 ); |
|
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 | 760 | } |
761 | 761 | |
762 | - $html = str_replace( '<iframe', '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"', $html ); |
|
762 | + $html = str_replace('<iframe', '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"', $html); |
|
763 | 763 | |
764 | - preg_match( '/ src=[\'"]([^\'"]*)[\'"]/', $html, $results ); |
|
764 | + preg_match('/ src=[\'"]([^\'"]*)[\'"]/', $html, $results); |
|
765 | 765 | |
766 | - if ( ! empty( $results ) ) { |
|
767 | - $secret = wp_generate_password( 10, false ); |
|
766 | + if ( ! empty($results)) { |
|
767 | + $secret = wp_generate_password(10, false); |
|
768 | 768 | |
769 | - $url = esc_url( "{$results[1]}#?secret=$secret" ); |
|
769 | + $url = esc_url("{$results[1]}#?secret=$secret"); |
|
770 | 770 | |
771 | - $html = str_replace( $results[0], " src=\"$url\" data-secret=\"$secret\"", $html ); |
|
772 | - $html = str_replace( '<blockquote', "<blockquote data-secret=\"$secret\"", $html ); |
|
771 | + $html = str_replace($results[0], " src=\"$url\" data-secret=\"$secret\"", $html); |
|
772 | + $html = str_replace('<blockquote', "<blockquote data-secret=\"$secret\"", $html); |
|
773 | 773 | } |
774 | 774 | |
775 | 775 | return $html; |
@@ -786,17 +786,17 @@ discard block |
||
786 | 786 | * @param string $more_string Default 'more' string. |
787 | 787 | * @return string 'Continue reading' link prepended with an ellipsis. |
788 | 788 | */ |
789 | -function wp_embed_excerpt_more( $more_string ) { |
|
790 | - if ( ! is_embed() ) { |
|
789 | +function wp_embed_excerpt_more($more_string) { |
|
790 | + if ( ! is_embed()) { |
|
791 | 791 | return $more_string; |
792 | 792 | } |
793 | 793 | |
794 | - $link = sprintf( '<a href="%1$s" class="wp-embed-more" target="_top">%2$s</a>', |
|
795 | - esc_url( get_permalink() ), |
|
794 | + $link = sprintf('<a href="%1$s" class="wp-embed-more" target="_top">%2$s</a>', |
|
795 | + esc_url(get_permalink()), |
|
796 | 796 | /* translators: %s: Name of current post */ |
797 | - sprintf( __( 'Continue reading %s' ), '<span class="screen-reader-text">' . get_the_title() . '</span>' ) |
|
797 | + sprintf(__('Continue reading %s'), '<span class="screen-reader-text">'.get_the_title().'</span>') |
|
798 | 798 | ); |
799 | - return ' … ' . $link; |
|
799 | + return ' … '.$link; |
|
800 | 800 | } |
801 | 801 | |
802 | 802 | /** |
@@ -816,7 +816,7 @@ discard block |
||
816 | 816 | * |
817 | 817 | * @param string $output The current post excerpt. |
818 | 818 | */ |
819 | - echo apply_filters( 'the_excerpt_embed', $output ); |
|
819 | + echo apply_filters('the_excerpt_embed', $output); |
|
820 | 820 | } |
821 | 821 | |
822 | 822 | /** |
@@ -829,9 +829,9 @@ discard block |
||
829 | 829 | * @param string $content The current post excerpt. |
830 | 830 | * @return string The modified post excerpt. |
831 | 831 | */ |
832 | -function wp_embed_excerpt_attachment( $content ) { |
|
833 | - if ( is_attachment() ) { |
|
834 | - return prepend_attachment( '' ); |
|
832 | +function wp_embed_excerpt_attachment($content) { |
|
833 | + if (is_attachment()) { |
|
834 | + return prepend_attachment(''); |
|
835 | 835 | } |
836 | 836 | |
837 | 837 | return $content; |
@@ -848,14 +848,14 @@ discard block |
||
848 | 848 | * @since 4.4.0 |
849 | 849 | */ |
850 | 850 | function enqueue_embed_scripts() { |
851 | - wp_enqueue_style( 'wp-embed-template-ie' ); |
|
851 | + wp_enqueue_style('wp-embed-template-ie'); |
|
852 | 852 | |
853 | 853 | /** |
854 | 854 | * Fires when scripts and styles are enqueued for the embed iframe. |
855 | 855 | * |
856 | 856 | * @since 4.4.0 |
857 | 857 | */ |
858 | - do_action( 'enqueue_embed_scripts' ); |
|
858 | + do_action('enqueue_embed_scripts'); |
|
859 | 859 | } |
860 | 860 | |
861 | 861 | /** |
@@ -867,8 +867,8 @@ discard block |
||
867 | 867 | ?> |
868 | 868 | <style type="text/css"> |
869 | 869 | <?php |
870 | - if ( SCRIPT_DEBUG ) { |
|
871 | - readfile( ABSPATH . WPINC . "/css/wp-embed-template.css" ); |
|
870 | + if (SCRIPT_DEBUG) { |
|
871 | + readfile(ABSPATH.WPINC."/css/wp-embed-template.css"); |
|
872 | 872 | } else { |
873 | 873 | /* |
874 | 874 | * If you're looking at a src version of this file, you'll see an "include" |
@@ -898,8 +898,8 @@ discard block |
||
898 | 898 | ?> |
899 | 899 | <script type="text/javascript"> |
900 | 900 | <?php |
901 | - if ( SCRIPT_DEBUG ) { |
|
902 | - readfile( ABSPATH . WPINC . "/js/wp-embed-template.js" ); |
|
901 | + if (SCRIPT_DEBUG) { |
|
902 | + readfile(ABSPATH.WPINC."/js/wp-embed-template.js"); |
|
903 | 903 | } else { |
904 | 904 | /* |
905 | 905 | * If you're looking at a src version of this file, you'll see an "include" |
@@ -929,8 +929,8 @@ discard block |
||
929 | 929 | * @param string $content The content to filter. |
930 | 930 | * @return string The filtered content. |
931 | 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 ); |
|
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 | 934 | } |
935 | 935 | |
936 | 936 | /** |
@@ -939,7 +939,7 @@ discard block |
||
939 | 939 | * @since 4.4.0 |
940 | 940 | */ |
941 | 941 | function print_embed_comments_button() { |
942 | - if ( is_404() || ! ( get_comments_number() || comments_open() ) ) { |
|
942 | + if (is_404() || ! (get_comments_number() || comments_open())) { |
|
943 | 943 | return; |
944 | 944 | } |
945 | 945 | ?> |
@@ -953,7 +953,7 @@ discard block |
||
953 | 953 | '%s <span class="screen-reader-text">Comments</span>', |
954 | 954 | get_comments_number() |
955 | 955 | ), |
956 | - number_format_i18n( get_comments_number() ) |
|
956 | + number_format_i18n(get_comments_number()) |
|
957 | 957 | ); |
958 | 958 | ?> |
959 | 959 | </a> |
@@ -967,12 +967,12 @@ discard block |
||
967 | 967 | * @since 4.4.0 |
968 | 968 | */ |
969 | 969 | function print_embed_sharing_button() { |
970 | - if ( is_404() ) { |
|
970 | + if (is_404()) { |
|
971 | 971 | return; |
972 | 972 | } |
973 | 973 | ?> |
974 | 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' ); ?>"> |
|
975 | + <button type="button" class="wp-embed-share-dialog-open" aria-label="<?php esc_attr_e('Open sharing dialog'); ?>"> |
|
976 | 976 | <span class="dashicons dashicons-share"></span> |
977 | 977 | </button> |
978 | 978 | </div> |
@@ -985,38 +985,38 @@ discard block |
||
985 | 985 | * @since 4.4.0 |
986 | 986 | */ |
987 | 987 | function print_embed_sharing_dialog() { |
988 | - if ( is_404() ) { |
|
988 | + if (is_404()) { |
|
989 | 989 | return; |
990 | 990 | } |
991 | 991 | ?> |
992 | - <div class="wp-embed-share-dialog hidden" role="dialog" aria-label="<?php esc_attr_e( 'Sharing options' ); ?>"> |
|
992 | + <div class="wp-embed-share-dialog hidden" role="dialog" aria-label="<?php esc_attr_e('Sharing options'); ?>"> |
|
993 | 993 | <div class="wp-embed-share-dialog-content"> |
994 | 994 | <div class="wp-embed-share-dialog-text"> |
995 | 995 | <ul class="wp-embed-share-tabs" role="tablist"> |
996 | 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> |
|
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 | 998 | </li> |
999 | 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> |
|
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 | 1001 | </li> |
1002 | 1002 | </ul> |
1003 | 1003 | <div id="wp-embed-share-tab-wordpress" class="wp-embed-share-tab" role="tabpanel" aria-hidden="false"> |
1004 | 1004 | <input type="text" value="<?php the_permalink(); ?>" class="wp-embed-share-input" aria-describedby="wp-embed-share-description-wordpress" tabindex="0" readonly/> |
1005 | 1005 | |
1006 | 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' ); ?> |
|
1007 | + <?php _e('Copy and paste this URL into your WordPress site to embed'); ?> |
|
1008 | 1008 | </p> |
1009 | 1009 | </div> |
1010 | 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> |
|
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> |
|
1012 | 1012 | |
1013 | 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' ); ?> |
|
1014 | + <?php _e('Copy and paste this code into your site to embed'); ?> |
|
1015 | 1015 | </p> |
1016 | 1016 | </div> |
1017 | 1017 | </div> |
1018 | 1018 | |
1019 | - <button type="button" class="wp-embed-share-dialog-close" aria-label="<?php esc_attr_e( 'Close sharing dialog' ); ?>"> |
|
1019 | + <button type="button" class="wp-embed-share-dialog-close" aria-label="<?php esc_attr_e('Close sharing dialog'); ?>"> |
|
1020 | 1020 | <span class="dashicons dashicons-no"></span> |
1021 | 1021 | </button> |
1022 | 1022 | </div> |
@@ -1032,13 +1032,13 @@ discard block |
||
1032 | 1032 | function the_embed_site_title() { |
1033 | 1033 | $site_title = sprintf( |
1034 | 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' ) ) |
|
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 | 1039 | ); |
1040 | 1040 | |
1041 | - $site_title = '<div class="wp-embed-site-title">' . $site_title . '</div>'; |
|
1041 | + $site_title = '<div class="wp-embed-site-title">'.$site_title.'</div>'; |
|
1042 | 1042 | |
1043 | 1043 | /** |
1044 | 1044 | * Filters the site title HTML in the embed footer. |
@@ -1047,7 +1047,7 @@ discard block |
||
1047 | 1047 | * |
1048 | 1048 | * @param string $site_title The site title HTML. |
1049 | 1049 | */ |
1050 | - echo apply_filters( 'embed_site_title_html', $site_title ); |
|
1050 | + echo apply_filters('embed_site_title_html', $site_title); |
|
1051 | 1051 | } |
1052 | 1052 | |
1053 | 1053 | /** |
@@ -1064,22 +1064,22 @@ discard block |
||
1064 | 1064 | * @return null|string The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. |
1065 | 1065 | * Null if the URL does not belong to the current site. |
1066 | 1066 | */ |
1067 | -function wp_filter_pre_oembed_result( $result, $url, $args ) { |
|
1068 | - $post_id = url_to_postid( $url ); |
|
1067 | +function wp_filter_pre_oembed_result($result, $url, $args) { |
|
1068 | + $post_id = url_to_postid($url); |
|
1069 | 1069 | |
1070 | 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 ); |
|
1071 | + $post_id = apply_filters('oembed_request_post_id', $post_id, $url); |
|
1072 | 1072 | |
1073 | - if ( ! $post_id ) { |
|
1073 | + if ( ! $post_id) { |
|
1074 | 1074 | return $result; |
1075 | 1075 | } |
1076 | 1076 | |
1077 | - $width = isset( $args['width'] ) ? $args['width'] : 0; |
|
1077 | + $width = isset($args['width']) ? $args['width'] : 0; |
|
1078 | 1078 | |
1079 | - $data = get_oembed_response_data( $post_id, $width ); |
|
1080 | - $data = _wp_oembed_get_object()->data2html( (object) $data, $url ); |
|
1079 | + $data = get_oembed_response_data($post_id, $width); |
|
1080 | + $data = _wp_oembed_get_object()->data2html((object) $data, $url); |
|
1081 | 1081 | |
1082 | - if ( ! $data ) { |
|
1082 | + if ( ! $data) { |
|
1083 | 1083 | return $result; |
1084 | 1084 | } |
1085 | 1085 |
@@ -20,21 +20,21 @@ discard block |
||
20 | 20 | * @param int $timeout |
21 | 21 | */ |
22 | 22 | public function __construct($server, $path = false, $port = false, $timeout = 15) { |
23 | - if ( ! $path ) { |
|
23 | + if ( ! $path) { |
|
24 | 24 | // Assume we have been given a URL instead |
25 | 25 | $bits = parse_url($server); |
26 | 26 | $this->scheme = $bits['scheme']; |
27 | 27 | $this->server = $bits['host']; |
28 | 28 | $this->port = isset($bits['port']) ? $bits['port'] : $port; |
29 | - $this->path = !empty($bits['path']) ? $bits['path'] : '/'; |
|
29 | + $this->path = ! empty($bits['path']) ? $bits['path'] : '/'; |
|
30 | 30 | |
31 | 31 | // Make absolutely sure we have a path |
32 | - if ( ! $this->path ) { |
|
32 | + if ( ! $this->path) { |
|
33 | 33 | $this->path = '/'; |
34 | 34 | } |
35 | 35 | |
36 | - if ( ! empty( $bits['query'] ) ) { |
|
37 | - $this->path .= '?' . $bits['query']; |
|
36 | + if ( ! empty($bits['query'])) { |
|
37 | + $this->path .= '?'.$bits['query']; |
|
38 | 38 | } |
39 | 39 | } else { |
40 | 40 | $this->scheme = 'http'; |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | $xml = $request->getXml(); |
57 | 57 | |
58 | 58 | $port = $this->port ? ":$this->port" : ''; |
59 | - $url = $this->scheme . '://' . $this->server . $port . $this->path; |
|
59 | + $url = $this->scheme.'://'.$this->server.$port.$this->path; |
|
60 | 60 | $args = array( |
61 | 61 | 'headers' => array('Content-Type' => 'text/xml'), |
62 | 62 | 'user-agent' => $this->useragent, |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | ); |
65 | 65 | |
66 | 66 | // Merge Custom headers ala #8145 |
67 | - foreach ( $this->headers as $header => $value ) { |
|
67 | + foreach ($this->headers as $header => $value) { |
|
68 | 68 | $args['headers'][$header] = $value; |
69 | 69 | } |
70 | 70 | |
@@ -75,45 +75,45 @@ discard block |
||
75 | 75 | * |
76 | 76 | * @param array $headers Array of headers to be sent. |
77 | 77 | */ |
78 | - $args['headers'] = apply_filters( 'wp_http_ixr_client_headers', $args['headers'] ); |
|
78 | + $args['headers'] = apply_filters('wp_http_ixr_client_headers', $args['headers']); |
|
79 | 79 | |
80 | - if ( $this->timeout !== false ) { |
|
80 | + if ($this->timeout !== false) { |
|
81 | 81 | $args['timeout'] = $this->timeout; |
82 | 82 | } |
83 | 83 | |
84 | 84 | // Now send the request |
85 | - if ( $this->debug ) { |
|
86 | - echo '<pre class="ixr_request">' . htmlspecialchars($xml) . "\n</pre>\n\n"; |
|
85 | + if ($this->debug) { |
|
86 | + echo '<pre class="ixr_request">'.htmlspecialchars($xml)."\n</pre>\n\n"; |
|
87 | 87 | } |
88 | 88 | |
89 | 89 | $response = wp_remote_post($url, $args); |
90 | 90 | |
91 | - if ( is_wp_error($response) ) { |
|
91 | + if (is_wp_error($response)) { |
|
92 | 92 | $errno = $response->get_error_code(); |
93 | 93 | $errorstr = $response->get_error_message(); |
94 | 94 | $this->error = new IXR_Error(-32300, "transport error: $errno $errorstr"); |
95 | 95 | return false; |
96 | 96 | } |
97 | 97 | |
98 | - if ( 200 != wp_remote_retrieve_response_code( $response ) ) { |
|
99 | - $this->error = new IXR_Error(-32301, 'transport error - HTTP status code was not 200 (' . wp_remote_retrieve_response_code( $response ) . ')'); |
|
98 | + if (200 != wp_remote_retrieve_response_code($response)) { |
|
99 | + $this->error = new IXR_Error(-32301, 'transport error - HTTP status code was not 200 ('.wp_remote_retrieve_response_code($response).')'); |
|
100 | 100 | return false; |
101 | 101 | } |
102 | 102 | |
103 | - if ( $this->debug ) { |
|
104 | - echo '<pre class="ixr_response">' . htmlspecialchars( wp_remote_retrieve_body( $response ) ) . "\n</pre>\n\n"; |
|
103 | + if ($this->debug) { |
|
104 | + echo '<pre class="ixr_response">'.htmlspecialchars(wp_remote_retrieve_body($response))."\n</pre>\n\n"; |
|
105 | 105 | } |
106 | 106 | |
107 | 107 | // Now parse what we've got back |
108 | - $this->message = new IXR_Message( wp_remote_retrieve_body( $response ) ); |
|
109 | - if ( ! $this->message->parse() ) { |
|
108 | + $this->message = new IXR_Message(wp_remote_retrieve_body($response)); |
|
109 | + if ( ! $this->message->parse()) { |
|
110 | 110 | // XML error |
111 | 111 | $this->error = new IXR_Error(-32700, 'parse error. not well formed'); |
112 | 112 | return false; |
113 | 113 | } |
114 | 114 | |
115 | 115 | // Is the message a fault? |
116 | - if ( $this->message->messageType == 'fault' ) { |
|
116 | + if ($this->message->messageType == 'fault') { |
|
117 | 117 | $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString); |
118 | 118 | return false; |
119 | 119 | } |
@@ -22,227 +22,227 @@ |
||
22 | 22 | */ |
23 | 23 | class Text_Diff_Engine_string { |
24 | 24 | |
25 | - /** |
|
26 | - * Parses a unified or context diff. |
|
27 | - * |
|
28 | - * First param contains the whole diff and the second can be used to force |
|
29 | - * a specific diff type. If the second parameter is 'autodetect', the |
|
30 | - * diff will be examined to find out which type of diff this is. |
|
31 | - * |
|
32 | - * @param string $diff The diff content. |
|
33 | - * @param string $mode The diff mode of the content in $diff. One of |
|
34 | - * 'context', 'unified', or 'autodetect'. |
|
35 | - * |
|
36 | - * @return array List of all diff operations. |
|
37 | - */ |
|
38 | - function diff($diff, $mode = 'autodetect') |
|
39 | - { |
|
40 | - // Detect line breaks. |
|
41 | - $lnbr = "\n"; |
|
42 | - if (strpos($diff, "\r\n") !== false) { |
|
43 | - $lnbr = "\r\n"; |
|
44 | - } elseif (strpos($diff, "\r") !== false) { |
|
45 | - $lnbr = "\r"; |
|
46 | - } |
|
47 | - |
|
48 | - // Make sure we have a line break at the EOF. |
|
49 | - if (substr($diff, -strlen($lnbr)) != $lnbr) { |
|
50 | - $diff .= $lnbr; |
|
51 | - } |
|
52 | - |
|
53 | - if ($mode != 'autodetect' && $mode != 'context' && $mode != 'unified') { |
|
54 | - return PEAR::raiseError('Type of diff is unsupported'); |
|
55 | - } |
|
56 | - |
|
57 | - if ($mode == 'autodetect') { |
|
58 | - $context = strpos($diff, '***'); |
|
59 | - $unified = strpos($diff, '---'); |
|
60 | - if ($context === $unified) { |
|
61 | - return PEAR::raiseError('Type of diff could not be detected'); |
|
62 | - } elseif ($context === false || $unified === false) { |
|
63 | - $mode = $context !== false ? 'context' : 'unified'; |
|
64 | - } else { |
|
65 | - $mode = $context < $unified ? 'context' : 'unified'; |
|
66 | - } |
|
67 | - } |
|
68 | - |
|
69 | - // Split by new line and remove the diff header, if there is one. |
|
70 | - $diff = explode($lnbr, $diff); |
|
71 | - if (($mode == 'context' && strpos($diff[0], '***') === 0) || |
|
72 | - ($mode == 'unified' && strpos($diff[0], '---') === 0)) { |
|
73 | - array_shift($diff); |
|
74 | - array_shift($diff); |
|
75 | - } |
|
76 | - |
|
77 | - if ($mode == 'context') { |
|
78 | - return $this->parseContextDiff($diff); |
|
79 | - } else { |
|
80 | - return $this->parseUnifiedDiff($diff); |
|
81 | - } |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Parses an array containing the unified diff. |
|
86 | - * |
|
87 | - * @param array $diff Array of lines. |
|
88 | - * |
|
89 | - * @return array List of all diff operations. |
|
90 | - */ |
|
91 | - function parseUnifiedDiff($diff) |
|
92 | - { |
|
93 | - $edits = array(); |
|
94 | - $end = count($diff) - 1; |
|
95 | - for ($i = 0; $i < $end;) { |
|
96 | - $diff1 = array(); |
|
97 | - switch (substr($diff[$i], 0, 1)) { |
|
98 | - case ' ': |
|
99 | - do { |
|
100 | - $diff1[] = substr($diff[$i], 1); |
|
101 | - } while (++$i < $end && substr($diff[$i], 0, 1) == ' '); |
|
102 | - $edits[] = new Text_Diff_Op_copy($diff1); |
|
103 | - break; |
|
104 | - |
|
105 | - case '+': |
|
106 | - // get all new lines |
|
107 | - do { |
|
108 | - $diff1[] = substr($diff[$i], 1); |
|
109 | - } while (++$i < $end && substr($diff[$i], 0, 1) == '+'); |
|
110 | - $edits[] = new Text_Diff_Op_add($diff1); |
|
111 | - break; |
|
112 | - |
|
113 | - case '-': |
|
114 | - // get changed or removed lines |
|
115 | - $diff2 = array(); |
|
116 | - do { |
|
117 | - $diff1[] = substr($diff[$i], 1); |
|
118 | - } while (++$i < $end && substr($diff[$i], 0, 1) == '-'); |
|
119 | - |
|
120 | - while ($i < $end && substr($diff[$i], 0, 1) == '+') { |
|
121 | - $diff2[] = substr($diff[$i++], 1); |
|
122 | - } |
|
123 | - if (count($diff2) == 0) { |
|
124 | - $edits[] = new Text_Diff_Op_delete($diff1); |
|
125 | - } else { |
|
126 | - $edits[] = new Text_Diff_Op_change($diff1, $diff2); |
|
127 | - } |
|
128 | - break; |
|
129 | - |
|
130 | - default: |
|
131 | - $i++; |
|
132 | - break; |
|
133 | - } |
|
134 | - } |
|
135 | - |
|
136 | - return $edits; |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Parses an array containing the context diff. |
|
141 | - * |
|
142 | - * @param array $diff Array of lines. |
|
143 | - * |
|
144 | - * @return array List of all diff operations. |
|
145 | - */ |
|
146 | - function parseContextDiff(&$diff) |
|
147 | - { |
|
148 | - $edits = array(); |
|
149 | - $i = $max_i = $j = $max_j = 0; |
|
150 | - $end = count($diff) - 1; |
|
151 | - while ($i < $end && $j < $end) { |
|
152 | - while ($i >= $max_i && $j >= $max_j) { |
|
153 | - // Find the boundaries of the diff output of the two files |
|
154 | - for ($i = $j; |
|
155 | - $i < $end && substr($diff[$i], 0, 3) == '***'; |
|
156 | - $i++); |
|
157 | - for ($max_i = $i; |
|
158 | - $max_i < $end && substr($diff[$max_i], 0, 3) != '---'; |
|
159 | - $max_i++); |
|
160 | - for ($j = $max_i; |
|
161 | - $j < $end && substr($diff[$j], 0, 3) == '---'; |
|
162 | - $j++); |
|
163 | - for ($max_j = $j; |
|
164 | - $max_j < $end && substr($diff[$max_j], 0, 3) != '***'; |
|
165 | - $max_j++); |
|
166 | - } |
|
167 | - |
|
168 | - // find what hasn't been changed |
|
169 | - $array = array(); |
|
170 | - while ($i < $max_i && |
|
171 | - $j < $max_j && |
|
172 | - strcmp($diff[$i], $diff[$j]) == 0) { |
|
173 | - $array[] = substr($diff[$i], 2); |
|
174 | - $i++; |
|
175 | - $j++; |
|
176 | - } |
|
177 | - |
|
178 | - while ($i < $max_i && ($max_j-$j) <= 1) { |
|
179 | - if ($diff[$i] != '' && substr($diff[$i], 0, 1) != ' ') { |
|
180 | - break; |
|
181 | - } |
|
182 | - $array[] = substr($diff[$i++], 2); |
|
183 | - } |
|
184 | - |
|
185 | - while ($j < $max_j && ($max_i-$i) <= 1) { |
|
186 | - if ($diff[$j] != '' && substr($diff[$j], 0, 1) != ' ') { |
|
187 | - break; |
|
188 | - } |
|
189 | - $array[] = substr($diff[$j++], 2); |
|
190 | - } |
|
191 | - if (count($array) > 0) { |
|
192 | - $edits[] = new Text_Diff_Op_copy($array); |
|
193 | - } |
|
194 | - |
|
195 | - if ($i < $max_i) { |
|
196 | - $diff1 = array(); |
|
197 | - switch (substr($diff[$i], 0, 1)) { |
|
198 | - case '!': |
|
199 | - $diff2 = array(); |
|
200 | - do { |
|
201 | - $diff1[] = substr($diff[$i], 2); |
|
202 | - if ($j < $max_j && substr($diff[$j], 0, 1) == '!') { |
|
203 | - $diff2[] = substr($diff[$j++], 2); |
|
204 | - } |
|
205 | - } while (++$i < $max_i && substr($diff[$i], 0, 1) == '!'); |
|
206 | - $edits[] = new Text_Diff_Op_change($diff1, $diff2); |
|
207 | - break; |
|
208 | - |
|
209 | - case '+': |
|
210 | - do { |
|
211 | - $diff1[] = substr($diff[$i], 2); |
|
212 | - } while (++$i < $max_i && substr($diff[$i], 0, 1) == '+'); |
|
213 | - $edits[] = new Text_Diff_Op_add($diff1); |
|
214 | - break; |
|
215 | - |
|
216 | - case '-': |
|
217 | - do { |
|
218 | - $diff1[] = substr($diff[$i], 2); |
|
219 | - } while (++$i < $max_i && substr($diff[$i], 0, 1) == '-'); |
|
220 | - $edits[] = new Text_Diff_Op_delete($diff1); |
|
221 | - break; |
|
222 | - } |
|
223 | - } |
|
224 | - |
|
225 | - if ($j < $max_j) { |
|
226 | - $diff2 = array(); |
|
227 | - switch (substr($diff[$j], 0, 1)) { |
|
228 | - case '+': |
|
229 | - do { |
|
230 | - $diff2[] = substr($diff[$j++], 2); |
|
231 | - } while ($j < $max_j && substr($diff[$j], 0, 1) == '+'); |
|
232 | - $edits[] = new Text_Diff_Op_add($diff2); |
|
233 | - break; |
|
234 | - |
|
235 | - case '-': |
|
236 | - do { |
|
237 | - $diff2[] = substr($diff[$j++], 2); |
|
238 | - } while ($j < $max_j && substr($diff[$j], 0, 1) == '-'); |
|
239 | - $edits[] = new Text_Diff_Op_delete($diff2); |
|
240 | - break; |
|
241 | - } |
|
242 | - } |
|
243 | - } |
|
244 | - |
|
245 | - return $edits; |
|
246 | - } |
|
25 | + /** |
|
26 | + * Parses a unified or context diff. |
|
27 | + * |
|
28 | + * First param contains the whole diff and the second can be used to force |
|
29 | + * a specific diff type. If the second parameter is 'autodetect', the |
|
30 | + * diff will be examined to find out which type of diff this is. |
|
31 | + * |
|
32 | + * @param string $diff The diff content. |
|
33 | + * @param string $mode The diff mode of the content in $diff. One of |
|
34 | + * 'context', 'unified', or 'autodetect'. |
|
35 | + * |
|
36 | + * @return array List of all diff operations. |
|
37 | + */ |
|
38 | + function diff($diff, $mode = 'autodetect') |
|
39 | + { |
|
40 | + // Detect line breaks. |
|
41 | + $lnbr = "\n"; |
|
42 | + if (strpos($diff, "\r\n") !== false) { |
|
43 | + $lnbr = "\r\n"; |
|
44 | + } elseif (strpos($diff, "\r") !== false) { |
|
45 | + $lnbr = "\r"; |
|
46 | + } |
|
47 | + |
|
48 | + // Make sure we have a line break at the EOF. |
|
49 | + if (substr($diff, -strlen($lnbr)) != $lnbr) { |
|
50 | + $diff .= $lnbr; |
|
51 | + } |
|
52 | + |
|
53 | + if ($mode != 'autodetect' && $mode != 'context' && $mode != 'unified') { |
|
54 | + return PEAR::raiseError('Type of diff is unsupported'); |
|
55 | + } |
|
56 | + |
|
57 | + if ($mode == 'autodetect') { |
|
58 | + $context = strpos($diff, '***'); |
|
59 | + $unified = strpos($diff, '---'); |
|
60 | + if ($context === $unified) { |
|
61 | + return PEAR::raiseError('Type of diff could not be detected'); |
|
62 | + } elseif ($context === false || $unified === false) { |
|
63 | + $mode = $context !== false ? 'context' : 'unified'; |
|
64 | + } else { |
|
65 | + $mode = $context < $unified ? 'context' : 'unified'; |
|
66 | + } |
|
67 | + } |
|
68 | + |
|
69 | + // Split by new line and remove the diff header, if there is one. |
|
70 | + $diff = explode($lnbr, $diff); |
|
71 | + if (($mode == 'context' && strpos($diff[0], '***') === 0) || |
|
72 | + ($mode == 'unified' && strpos($diff[0], '---') === 0)) { |
|
73 | + array_shift($diff); |
|
74 | + array_shift($diff); |
|
75 | + } |
|
76 | + |
|
77 | + if ($mode == 'context') { |
|
78 | + return $this->parseContextDiff($diff); |
|
79 | + } else { |
|
80 | + return $this->parseUnifiedDiff($diff); |
|
81 | + } |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Parses an array containing the unified diff. |
|
86 | + * |
|
87 | + * @param array $diff Array of lines. |
|
88 | + * |
|
89 | + * @return array List of all diff operations. |
|
90 | + */ |
|
91 | + function parseUnifiedDiff($diff) |
|
92 | + { |
|
93 | + $edits = array(); |
|
94 | + $end = count($diff) - 1; |
|
95 | + for ($i = 0; $i < $end;) { |
|
96 | + $diff1 = array(); |
|
97 | + switch (substr($diff[$i], 0, 1)) { |
|
98 | + case ' ': |
|
99 | + do { |
|
100 | + $diff1[] = substr($diff[$i], 1); |
|
101 | + } while (++$i < $end && substr($diff[$i], 0, 1) == ' '); |
|
102 | + $edits[] = new Text_Diff_Op_copy($diff1); |
|
103 | + break; |
|
104 | + |
|
105 | + case '+': |
|
106 | + // get all new lines |
|
107 | + do { |
|
108 | + $diff1[] = substr($diff[$i], 1); |
|
109 | + } while (++$i < $end && substr($diff[$i], 0, 1) == '+'); |
|
110 | + $edits[] = new Text_Diff_Op_add($diff1); |
|
111 | + break; |
|
112 | + |
|
113 | + case '-': |
|
114 | + // get changed or removed lines |
|
115 | + $diff2 = array(); |
|
116 | + do { |
|
117 | + $diff1[] = substr($diff[$i], 1); |
|
118 | + } while (++$i < $end && substr($diff[$i], 0, 1) == '-'); |
|
119 | + |
|
120 | + while ($i < $end && substr($diff[$i], 0, 1) == '+') { |
|
121 | + $diff2[] = substr($diff[$i++], 1); |
|
122 | + } |
|
123 | + if (count($diff2) == 0) { |
|
124 | + $edits[] = new Text_Diff_Op_delete($diff1); |
|
125 | + } else { |
|
126 | + $edits[] = new Text_Diff_Op_change($diff1, $diff2); |
|
127 | + } |
|
128 | + break; |
|
129 | + |
|
130 | + default: |
|
131 | + $i++; |
|
132 | + break; |
|
133 | + } |
|
134 | + } |
|
135 | + |
|
136 | + return $edits; |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Parses an array containing the context diff. |
|
141 | + * |
|
142 | + * @param array $diff Array of lines. |
|
143 | + * |
|
144 | + * @return array List of all diff operations. |
|
145 | + */ |
|
146 | + function parseContextDiff(&$diff) |
|
147 | + { |
|
148 | + $edits = array(); |
|
149 | + $i = $max_i = $j = $max_j = 0; |
|
150 | + $end = count($diff) - 1; |
|
151 | + while ($i < $end && $j < $end) { |
|
152 | + while ($i >= $max_i && $j >= $max_j) { |
|
153 | + // Find the boundaries of the diff output of the two files |
|
154 | + for ($i = $j; |
|
155 | + $i < $end && substr($diff[$i], 0, 3) == '***'; |
|
156 | + $i++); |
|
157 | + for ($max_i = $i; |
|
158 | + $max_i < $end && substr($diff[$max_i], 0, 3) != '---'; |
|
159 | + $max_i++); |
|
160 | + for ($j = $max_i; |
|
161 | + $j < $end && substr($diff[$j], 0, 3) == '---'; |
|
162 | + $j++); |
|
163 | + for ($max_j = $j; |
|
164 | + $max_j < $end && substr($diff[$max_j], 0, 3) != '***'; |
|
165 | + $max_j++); |
|
166 | + } |
|
167 | + |
|
168 | + // find what hasn't been changed |
|
169 | + $array = array(); |
|
170 | + while ($i < $max_i && |
|
171 | + $j < $max_j && |
|
172 | + strcmp($diff[$i], $diff[$j]) == 0) { |
|
173 | + $array[] = substr($diff[$i], 2); |
|
174 | + $i++; |
|
175 | + $j++; |
|
176 | + } |
|
177 | + |
|
178 | + while ($i < $max_i && ($max_j-$j) <= 1) { |
|
179 | + if ($diff[$i] != '' && substr($diff[$i], 0, 1) != ' ') { |
|
180 | + break; |
|
181 | + } |
|
182 | + $array[] = substr($diff[$i++], 2); |
|
183 | + } |
|
184 | + |
|
185 | + while ($j < $max_j && ($max_i-$i) <= 1) { |
|
186 | + if ($diff[$j] != '' && substr($diff[$j], 0, 1) != ' ') { |
|
187 | + break; |
|
188 | + } |
|
189 | + $array[] = substr($diff[$j++], 2); |
|
190 | + } |
|
191 | + if (count($array) > 0) { |
|
192 | + $edits[] = new Text_Diff_Op_copy($array); |
|
193 | + } |
|
194 | + |
|
195 | + if ($i < $max_i) { |
|
196 | + $diff1 = array(); |
|
197 | + switch (substr($diff[$i], 0, 1)) { |
|
198 | + case '!': |
|
199 | + $diff2 = array(); |
|
200 | + do { |
|
201 | + $diff1[] = substr($diff[$i], 2); |
|
202 | + if ($j < $max_j && substr($diff[$j], 0, 1) == '!') { |
|
203 | + $diff2[] = substr($diff[$j++], 2); |
|
204 | + } |
|
205 | + } while (++$i < $max_i && substr($diff[$i], 0, 1) == '!'); |
|
206 | + $edits[] = new Text_Diff_Op_change($diff1, $diff2); |
|
207 | + break; |
|
208 | + |
|
209 | + case '+': |
|
210 | + do { |
|
211 | + $diff1[] = substr($diff[$i], 2); |
|
212 | + } while (++$i < $max_i && substr($diff[$i], 0, 1) == '+'); |
|
213 | + $edits[] = new Text_Diff_Op_add($diff1); |
|
214 | + break; |
|
215 | + |
|
216 | + case '-': |
|
217 | + do { |
|
218 | + $diff1[] = substr($diff[$i], 2); |
|
219 | + } while (++$i < $max_i && substr($diff[$i], 0, 1) == '-'); |
|
220 | + $edits[] = new Text_Diff_Op_delete($diff1); |
|
221 | + break; |
|
222 | + } |
|
223 | + } |
|
224 | + |
|
225 | + if ($j < $max_j) { |
|
226 | + $diff2 = array(); |
|
227 | + switch (substr($diff[$j], 0, 1)) { |
|
228 | + case '+': |
|
229 | + do { |
|
230 | + $diff2[] = substr($diff[$j++], 2); |
|
231 | + } while ($j < $max_j && substr($diff[$j], 0, 1) == '+'); |
|
232 | + $edits[] = new Text_Diff_Op_add($diff2); |
|
233 | + break; |
|
234 | + |
|
235 | + case '-': |
|
236 | + do { |
|
237 | + $diff2[] = substr($diff[$j++], 2); |
|
238 | + } while ($j < $max_j && substr($diff[$j], 0, 1) == '-'); |
|
239 | + $edits[] = new Text_Diff_Op_delete($diff2); |
|
240 | + break; |
|
241 | + } |
|
242 | + } |
|
243 | + } |
|
244 | + |
|
245 | + return $edits; |
|
246 | + } |
|
247 | 247 | |
248 | 248 | } |
@@ -95,41 +95,41 @@ discard block |
||
95 | 95 | for ($i = 0; $i < $end;) { |
96 | 96 | $diff1 = array(); |
97 | 97 | switch (substr($diff[$i], 0, 1)) { |
98 | - case ' ': |
|
99 | - do { |
|
100 | - $diff1[] = substr($diff[$i], 1); |
|
101 | - } while (++$i < $end && substr($diff[$i], 0, 1) == ' '); |
|
102 | - $edits[] = new Text_Diff_Op_copy($diff1); |
|
103 | - break; |
|
104 | - |
|
105 | - case '+': |
|
106 | - // get all new lines |
|
107 | - do { |
|
108 | - $diff1[] = substr($diff[$i], 1); |
|
109 | - } while (++$i < $end && substr($diff[$i], 0, 1) == '+'); |
|
110 | - $edits[] = new Text_Diff_Op_add($diff1); |
|
111 | - break; |
|
112 | - |
|
113 | - case '-': |
|
114 | - // get changed or removed lines |
|
115 | - $diff2 = array(); |
|
116 | - do { |
|
117 | - $diff1[] = substr($diff[$i], 1); |
|
118 | - } while (++$i < $end && substr($diff[$i], 0, 1) == '-'); |
|
119 | - |
|
120 | - while ($i < $end && substr($diff[$i], 0, 1) == '+') { |
|
121 | - $diff2[] = substr($diff[$i++], 1); |
|
122 | - } |
|
123 | - if (count($diff2) == 0) { |
|
124 | - $edits[] = new Text_Diff_Op_delete($diff1); |
|
125 | - } else { |
|
126 | - $edits[] = new Text_Diff_Op_change($diff1, $diff2); |
|
127 | - } |
|
128 | - break; |
|
129 | - |
|
130 | - default: |
|
131 | - $i++; |
|
132 | - break; |
|
98 | + case ' ': |
|
99 | + do { |
|
100 | + $diff1[] = substr($diff[$i], 1); |
|
101 | + } while (++$i < $end && substr($diff[$i], 0, 1) == ' '); |
|
102 | + $edits[] = new Text_Diff_Op_copy($diff1); |
|
103 | + break; |
|
104 | + |
|
105 | + case '+': |
|
106 | + // get all new lines |
|
107 | + do { |
|
108 | + $diff1[] = substr($diff[$i], 1); |
|
109 | + } while (++$i < $end && substr($diff[$i], 0, 1) == '+'); |
|
110 | + $edits[] = new Text_Diff_Op_add($diff1); |
|
111 | + break; |
|
112 | + |
|
113 | + case '-': |
|
114 | + // get changed or removed lines |
|
115 | + $diff2 = array(); |
|
116 | + do { |
|
117 | + $diff1[] = substr($diff[$i], 1); |
|
118 | + } while (++$i < $end && substr($diff[$i], 0, 1) == '-'); |
|
119 | + |
|
120 | + while ($i < $end && substr($diff[$i], 0, 1) == '+') { |
|
121 | + $diff2[] = substr($diff[$i++], 1); |
|
122 | + } |
|
123 | + if (count($diff2) == 0) { |
|
124 | + $edits[] = new Text_Diff_Op_delete($diff1); |
|
125 | + } else { |
|
126 | + $edits[] = new Text_Diff_Op_change($diff1, $diff2); |
|
127 | + } |
|
128 | + break; |
|
129 | + |
|
130 | + default: |
|
131 | + $i++; |
|
132 | + break; |
|
133 | 133 | } |
134 | 134 | } |
135 | 135 | |
@@ -195,49 +195,49 @@ discard block |
||
195 | 195 | if ($i < $max_i) { |
196 | 196 | $diff1 = array(); |
197 | 197 | switch (substr($diff[$i], 0, 1)) { |
198 | - case '!': |
|
199 | - $diff2 = array(); |
|
200 | - do { |
|
201 | - $diff1[] = substr($diff[$i], 2); |
|
202 | - if ($j < $max_j && substr($diff[$j], 0, 1) == '!') { |
|
203 | - $diff2[] = substr($diff[$j++], 2); |
|
204 | - } |
|
205 | - } while (++$i < $max_i && substr($diff[$i], 0, 1) == '!'); |
|
206 | - $edits[] = new Text_Diff_Op_change($diff1, $diff2); |
|
207 | - break; |
|
208 | - |
|
209 | - case '+': |
|
210 | - do { |
|
211 | - $diff1[] = substr($diff[$i], 2); |
|
212 | - } while (++$i < $max_i && substr($diff[$i], 0, 1) == '+'); |
|
213 | - $edits[] = new Text_Diff_Op_add($diff1); |
|
214 | - break; |
|
215 | - |
|
216 | - case '-': |
|
217 | - do { |
|
218 | - $diff1[] = substr($diff[$i], 2); |
|
219 | - } while (++$i < $max_i && substr($diff[$i], 0, 1) == '-'); |
|
220 | - $edits[] = new Text_Diff_Op_delete($diff1); |
|
221 | - break; |
|
198 | + case '!': |
|
199 | + $diff2 = array(); |
|
200 | + do { |
|
201 | + $diff1[] = substr($diff[$i], 2); |
|
202 | + if ($j < $max_j && substr($diff[$j], 0, 1) == '!') { |
|
203 | + $diff2[] = substr($diff[$j++], 2); |
|
204 | + } |
|
205 | + } while (++$i < $max_i && substr($diff[$i], 0, 1) == '!'); |
|
206 | + $edits[] = new Text_Diff_Op_change($diff1, $diff2); |
|
207 | + break; |
|
208 | + |
|
209 | + case '+': |
|
210 | + do { |
|
211 | + $diff1[] = substr($diff[$i], 2); |
|
212 | + } while (++$i < $max_i && substr($diff[$i], 0, 1) == '+'); |
|
213 | + $edits[] = new Text_Diff_Op_add($diff1); |
|
214 | + break; |
|
215 | + |
|
216 | + case '-': |
|
217 | + do { |
|
218 | + $diff1[] = substr($diff[$i], 2); |
|
219 | + } while (++$i < $max_i && substr($diff[$i], 0, 1) == '-'); |
|
220 | + $edits[] = new Text_Diff_Op_delete($diff1); |
|
221 | + break; |
|
222 | 222 | } |
223 | 223 | } |
224 | 224 | |
225 | 225 | if ($j < $max_j) { |
226 | 226 | $diff2 = array(); |
227 | 227 | switch (substr($diff[$j], 0, 1)) { |
228 | - case '+': |
|
229 | - do { |
|
230 | - $diff2[] = substr($diff[$j++], 2); |
|
231 | - } while ($j < $max_j && substr($diff[$j], 0, 1) == '+'); |
|
232 | - $edits[] = new Text_Diff_Op_add($diff2); |
|
233 | - break; |
|
234 | - |
|
235 | - case '-': |
|
236 | - do { |
|
237 | - $diff2[] = substr($diff[$j++], 2); |
|
238 | - } while ($j < $max_j && substr($diff[$j], 0, 1) == '-'); |
|
239 | - $edits[] = new Text_Diff_Op_delete($diff2); |
|
240 | - break; |
|
228 | + case '+': |
|
229 | + do { |
|
230 | + $diff2[] = substr($diff[$j++], 2); |
|
231 | + } while ($j < $max_j && substr($diff[$j], 0, 1) == '+'); |
|
232 | + $edits[] = new Text_Diff_Op_add($diff2); |
|
233 | + break; |
|
234 | + |
|
235 | + case '-': |
|
236 | + do { |
|
237 | + $diff2[] = substr($diff[$j++], 2); |
|
238 | + } while ($j < $max_j && substr($diff[$j], 0, 1) == '-'); |
|
239 | + $edits[] = new Text_Diff_Op_delete($diff2); |
|
240 | + break; |
|
241 | 241 | } |
242 | 242 | } |
243 | 243 | } |
@@ -175,14 +175,14 @@ |
||
175 | 175 | $j++; |
176 | 176 | } |
177 | 177 | |
178 | - while ($i < $max_i && ($max_j-$j) <= 1) { |
|
178 | + while ($i < $max_i && ($max_j - $j) <= 1) { |
|
179 | 179 | if ($diff[$i] != '' && substr($diff[$i], 0, 1) != ' ') { |
180 | 180 | break; |
181 | 181 | } |
182 | 182 | $array[] = substr($diff[$i++], 2); |
183 | 183 | } |
184 | 184 | |
185 | - while ($j < $max_j && ($max_i-$i) <= 1) { |
|
185 | + while ($j < $max_j && ($max_i - $i) <= 1) { |
|
186 | 186 | if ($diff[$j] != '' && substr($diff[$j], 0, 1) != ' ') { |
187 | 187 | break; |
188 | 188 | } |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | * SOFTWARE. |
27 | 27 | */ |
28 | 28 | |
29 | -if (!function_exists('RandomCompat_strlen')) { |
|
29 | +if ( ! function_exists('RandomCompat_strlen')) { |
|
30 | 30 | if ( |
31 | 31 | defined('MB_OVERLOAD_STRING') && |
32 | 32 | ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | */ |
46 | 46 | function RandomCompat_strlen($binary_string) |
47 | 47 | { |
48 | - if (!is_string($binary_string)) { |
|
48 | + if ( ! is_string($binary_string)) { |
|
49 | 49 | throw new TypeError( |
50 | 50 | 'RandomCompat_strlen() expects a string' |
51 | 51 | ); |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | */ |
69 | 69 | function RandomCompat_strlen($binary_string) |
70 | 70 | { |
71 | - if (!is_string($binary_string)) { |
|
71 | + if ( ! is_string($binary_string)) { |
|
72 | 72 | throw new TypeError( |
73 | 73 | 'RandomCompat_strlen() expects a string' |
74 | 74 | ); |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | } |
79 | 79 | } |
80 | 80 | |
81 | -if (!function_exists('RandomCompat_substr')) { |
|
81 | +if ( ! function_exists('RandomCompat_substr')) { |
|
82 | 82 | |
83 | 83 | if ( |
84 | 84 | defined('MB_OVERLOAD_STRING') |
@@ -101,13 +101,13 @@ discard block |
||
101 | 101 | */ |
102 | 102 | function RandomCompat_substr($binary_string, $start, $length = null) |
103 | 103 | { |
104 | - if (!is_string($binary_string)) { |
|
104 | + if ( ! is_string($binary_string)) { |
|
105 | 105 | throw new TypeError( |
106 | 106 | 'RandomCompat_substr(): First argument should be a string' |
107 | 107 | ); |
108 | 108 | } |
109 | 109 | |
110 | - if (!is_int($start)) { |
|
110 | + if ( ! is_int($start)) { |
|
111 | 111 | throw new TypeError( |
112 | 112 | 'RandomCompat_substr(): Second argument should be an integer' |
113 | 113 | ); |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | * PHP 5.3, so we have to find the length ourselves. |
120 | 120 | */ |
121 | 121 | $length = RandomCompat_strlen($length) - $start; |
122 | - } elseif (!is_int($length)) { |
|
122 | + } elseif ( ! is_int($length)) { |
|
123 | 123 | throw new TypeError( |
124 | 124 | 'RandomCompat_substr(): Third argument should be an integer, or omitted' |
125 | 125 | ); |
@@ -145,20 +145,20 @@ discard block |
||
145 | 145 | */ |
146 | 146 | function RandomCompat_substr($binary_string, $start, $length = null) |
147 | 147 | { |
148 | - if (!is_string($binary_string)) { |
|
148 | + if ( ! is_string($binary_string)) { |
|
149 | 149 | throw new TypeError( |
150 | 150 | 'RandomCompat_substr(): First argument should be a string' |
151 | 151 | ); |
152 | 152 | } |
153 | 153 | |
154 | - if (!is_int($start)) { |
|
154 | + if ( ! is_int($start)) { |
|
155 | 155 | throw new TypeError( |
156 | 156 | 'RandomCompat_substr(): Second argument should be an integer' |
157 | 157 | ); |
158 | 158 | } |
159 | 159 | |
160 | 160 | if ($length !== null) { |
161 | - if (!is_int($length)) { |
|
161 | + if ( ! is_int($length)) { |
|
162 | 162 | throw new TypeError( |
163 | 163 | 'RandomCompat_substr(): Third argument should be an integer, or omitted' |
164 | 164 | ); |
@@ -27,147 +27,147 @@ |
||
27 | 27 | */ |
28 | 28 | |
29 | 29 | if (!function_exists('RandomCompat_strlen')) { |
30 | - if ( |
|
31 | - defined('MB_OVERLOAD_STRING') && |
|
32 | - ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING |
|
33 | - ) { |
|
34 | - /** |
|
35 | - * strlen() implementation that isn't brittle to mbstring.func_overload |
|
36 | - * |
|
37 | - * This version uses mb_strlen() in '8bit' mode to treat strings as raw |
|
38 | - * binary rather than UTF-8, ISO-8859-1, etc |
|
39 | - * |
|
40 | - * @param string $binary_string |
|
41 | - * |
|
42 | - * @throws TypeError |
|
43 | - * |
|
44 | - * @return int |
|
45 | - */ |
|
46 | - function RandomCompat_strlen($binary_string) |
|
47 | - { |
|
48 | - if (!is_string($binary_string)) { |
|
49 | - throw new TypeError( |
|
50 | - 'RandomCompat_strlen() expects a string' |
|
51 | - ); |
|
52 | - } |
|
30 | + if ( |
|
31 | + defined('MB_OVERLOAD_STRING') && |
|
32 | + ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING |
|
33 | + ) { |
|
34 | + /** |
|
35 | + * strlen() implementation that isn't brittle to mbstring.func_overload |
|
36 | + * |
|
37 | + * This version uses mb_strlen() in '8bit' mode to treat strings as raw |
|
38 | + * binary rather than UTF-8, ISO-8859-1, etc |
|
39 | + * |
|
40 | + * @param string $binary_string |
|
41 | + * |
|
42 | + * @throws TypeError |
|
43 | + * |
|
44 | + * @return int |
|
45 | + */ |
|
46 | + function RandomCompat_strlen($binary_string) |
|
47 | + { |
|
48 | + if (!is_string($binary_string)) { |
|
49 | + throw new TypeError( |
|
50 | + 'RandomCompat_strlen() expects a string' |
|
51 | + ); |
|
52 | + } |
|
53 | 53 | |
54 | - return mb_strlen($binary_string, '8bit'); |
|
55 | - } |
|
54 | + return mb_strlen($binary_string, '8bit'); |
|
55 | + } |
|
56 | 56 | |
57 | - } else { |
|
58 | - /** |
|
59 | - * strlen() implementation that isn't brittle to mbstring.func_overload |
|
60 | - * |
|
61 | - * This version just used the default strlen() |
|
62 | - * |
|
63 | - * @param string $binary_string |
|
64 | - * |
|
65 | - * @throws TypeError |
|
66 | - * |
|
67 | - * @return int |
|
68 | - */ |
|
69 | - function RandomCompat_strlen($binary_string) |
|
70 | - { |
|
71 | - if (!is_string($binary_string)) { |
|
72 | - throw new TypeError( |
|
73 | - 'RandomCompat_strlen() expects a string' |
|
74 | - ); |
|
75 | - } |
|
76 | - return strlen($binary_string); |
|
77 | - } |
|
78 | - } |
|
57 | + } else { |
|
58 | + /** |
|
59 | + * strlen() implementation that isn't brittle to mbstring.func_overload |
|
60 | + * |
|
61 | + * This version just used the default strlen() |
|
62 | + * |
|
63 | + * @param string $binary_string |
|
64 | + * |
|
65 | + * @throws TypeError |
|
66 | + * |
|
67 | + * @return int |
|
68 | + */ |
|
69 | + function RandomCompat_strlen($binary_string) |
|
70 | + { |
|
71 | + if (!is_string($binary_string)) { |
|
72 | + throw new TypeError( |
|
73 | + 'RandomCompat_strlen() expects a string' |
|
74 | + ); |
|
75 | + } |
|
76 | + return strlen($binary_string); |
|
77 | + } |
|
78 | + } |
|
79 | 79 | } |
80 | 80 | |
81 | 81 | if (!function_exists('RandomCompat_substr')) { |
82 | 82 | |
83 | - if ( |
|
84 | - defined('MB_OVERLOAD_STRING') |
|
85 | - && |
|
86 | - ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING |
|
87 | - ) { |
|
88 | - /** |
|
89 | - * substr() implementation that isn't brittle to mbstring.func_overload |
|
90 | - * |
|
91 | - * This version uses mb_substr() in '8bit' mode to treat strings as raw |
|
92 | - * binary rather than UTF-8, ISO-8859-1, etc |
|
93 | - * |
|
94 | - * @param string $binary_string |
|
95 | - * @param int $start |
|
96 | - * @param int $length (optional) |
|
97 | - * |
|
98 | - * @throws TypeError |
|
99 | - * |
|
100 | - * @return string |
|
101 | - */ |
|
102 | - function RandomCompat_substr($binary_string, $start, $length = null) |
|
103 | - { |
|
104 | - if (!is_string($binary_string)) { |
|
105 | - throw new TypeError( |
|
106 | - 'RandomCompat_substr(): First argument should be a string' |
|
107 | - ); |
|
108 | - } |
|
83 | + if ( |
|
84 | + defined('MB_OVERLOAD_STRING') |
|
85 | + && |
|
86 | + ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING |
|
87 | + ) { |
|
88 | + /** |
|
89 | + * substr() implementation that isn't brittle to mbstring.func_overload |
|
90 | + * |
|
91 | + * This version uses mb_substr() in '8bit' mode to treat strings as raw |
|
92 | + * binary rather than UTF-8, ISO-8859-1, etc |
|
93 | + * |
|
94 | + * @param string $binary_string |
|
95 | + * @param int $start |
|
96 | + * @param int $length (optional) |
|
97 | + * |
|
98 | + * @throws TypeError |
|
99 | + * |
|
100 | + * @return string |
|
101 | + */ |
|
102 | + function RandomCompat_substr($binary_string, $start, $length = null) |
|
103 | + { |
|
104 | + if (!is_string($binary_string)) { |
|
105 | + throw new TypeError( |
|
106 | + 'RandomCompat_substr(): First argument should be a string' |
|
107 | + ); |
|
108 | + } |
|
109 | 109 | |
110 | - if (!is_int($start)) { |
|
111 | - throw new TypeError( |
|
112 | - 'RandomCompat_substr(): Second argument should be an integer' |
|
113 | - ); |
|
114 | - } |
|
110 | + if (!is_int($start)) { |
|
111 | + throw new TypeError( |
|
112 | + 'RandomCompat_substr(): Second argument should be an integer' |
|
113 | + ); |
|
114 | + } |
|
115 | 115 | |
116 | - if ($length === null) { |
|
117 | - /** |
|
118 | - * mb_substr($str, 0, NULL, '8bit') returns an empty string on |
|
119 | - * PHP 5.3, so we have to find the length ourselves. |
|
120 | - */ |
|
121 | - $length = RandomCompat_strlen($length) - $start; |
|
122 | - } elseif (!is_int($length)) { |
|
123 | - throw new TypeError( |
|
124 | - 'RandomCompat_substr(): Third argument should be an integer, or omitted' |
|
125 | - ); |
|
126 | - } |
|
116 | + if ($length === null) { |
|
117 | + /** |
|
118 | + * mb_substr($str, 0, NULL, '8bit') returns an empty string on |
|
119 | + * PHP 5.3, so we have to find the length ourselves. |
|
120 | + */ |
|
121 | + $length = RandomCompat_strlen($length) - $start; |
|
122 | + } elseif (!is_int($length)) { |
|
123 | + throw new TypeError( |
|
124 | + 'RandomCompat_substr(): Third argument should be an integer, or omitted' |
|
125 | + ); |
|
126 | + } |
|
127 | 127 | |
128 | - return mb_substr($binary_string, $start, $length, '8bit'); |
|
129 | - } |
|
128 | + return mb_substr($binary_string, $start, $length, '8bit'); |
|
129 | + } |
|
130 | 130 | |
131 | - } else { |
|
131 | + } else { |
|
132 | 132 | |
133 | - /** |
|
134 | - * substr() implementation that isn't brittle to mbstring.func_overload |
|
135 | - * |
|
136 | - * This version just uses the default substr() |
|
137 | - * |
|
138 | - * @param string $binary_string |
|
139 | - * @param int $start |
|
140 | - * @param int $length (optional) |
|
141 | - * |
|
142 | - * @throws TypeError |
|
143 | - * |
|
144 | - * @return string |
|
145 | - */ |
|
146 | - function RandomCompat_substr($binary_string, $start, $length = null) |
|
147 | - { |
|
148 | - if (!is_string($binary_string)) { |
|
149 | - throw new TypeError( |
|
150 | - 'RandomCompat_substr(): First argument should be a string' |
|
151 | - ); |
|
152 | - } |
|
133 | + /** |
|
134 | + * substr() implementation that isn't brittle to mbstring.func_overload |
|
135 | + * |
|
136 | + * This version just uses the default substr() |
|
137 | + * |
|
138 | + * @param string $binary_string |
|
139 | + * @param int $start |
|
140 | + * @param int $length (optional) |
|
141 | + * |
|
142 | + * @throws TypeError |
|
143 | + * |
|
144 | + * @return string |
|
145 | + */ |
|
146 | + function RandomCompat_substr($binary_string, $start, $length = null) |
|
147 | + { |
|
148 | + if (!is_string($binary_string)) { |
|
149 | + throw new TypeError( |
|
150 | + 'RandomCompat_substr(): First argument should be a string' |
|
151 | + ); |
|
152 | + } |
|
153 | 153 | |
154 | - if (!is_int($start)) { |
|
155 | - throw new TypeError( |
|
156 | - 'RandomCompat_substr(): Second argument should be an integer' |
|
157 | - ); |
|
158 | - } |
|
154 | + if (!is_int($start)) { |
|
155 | + throw new TypeError( |
|
156 | + 'RandomCompat_substr(): Second argument should be an integer' |
|
157 | + ); |
|
158 | + } |
|
159 | 159 | |
160 | - if ($length !== null) { |
|
161 | - if (!is_int($length)) { |
|
162 | - throw new TypeError( |
|
163 | - 'RandomCompat_substr(): Third argument should be an integer, or omitted' |
|
164 | - ); |
|
165 | - } |
|
160 | + if ($length !== null) { |
|
161 | + if (!is_int($length)) { |
|
162 | + throw new TypeError( |
|
163 | + 'RandomCompat_substr(): Third argument should be an integer, or omitted' |
|
164 | + ); |
|
165 | + } |
|
166 | 166 | |
167 | - return substr($binary_string, $start, $length); |
|
168 | - } |
|
167 | + return substr($binary_string, $start, $length); |
|
168 | + } |
|
169 | 169 | |
170 | - return substr($binary_string, $start); |
|
171 | - } |
|
172 | - } |
|
170 | + return substr($binary_string, $start); |
|
171 | + } |
|
172 | + } |
|
173 | 173 | } |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | * SOFTWARE. |
27 | 27 | */ |
28 | 28 | |
29 | -if (!defined('RANDOM_COMPAT_READ_BUFFER')) { |
|
29 | +if ( ! defined('RANDOM_COMPAT_READ_BUFFER')) { |
|
30 | 30 | define('RANDOM_COMPAT_READ_BUFFER', 8); |
31 | 31 | } |
32 | 32 | |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | * We never fall back to /dev/random |
56 | 56 | */ |
57 | 57 | $fp = fopen('/dev/urandom', 'rb'); |
58 | - if (!empty($fp)) { |
|
58 | + if ( ! empty($fp)) { |
|
59 | 59 | $st = fstat($fp); |
60 | 60 | if (($st['mode'] & 0170000) !== 020000) { |
61 | 61 | fclose($fp); |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | } |
64 | 64 | } |
65 | 65 | |
66 | - if (!empty($fp)) { |
|
66 | + if ( ! empty($fp)) { |
|
67 | 67 | /** |
68 | 68 | * stream_set_read_buffer() does not exist in HHVM |
69 | 69 | * |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | * if (empty($fp)) line is logic that should only be run once per |
103 | 103 | * page load. |
104 | 104 | */ |
105 | - if (!empty($fp)) { |
|
105 | + if ( ! empty($fp)) { |
|
106 | 106 | $remaining = $bytes; |
107 | 107 | $buf = ''; |
108 | 108 |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | */ |
28 | 28 | |
29 | 29 | if (!defined('RANDOM_COMPAT_READ_BUFFER')) { |
30 | - define('RANDOM_COMPAT_READ_BUFFER', 8); |
|
30 | + define('RANDOM_COMPAT_READ_BUFFER', 8); |
|
31 | 31 | } |
32 | 32 | |
33 | 33 | /** |
@@ -45,104 +45,104 @@ discard block |
||
45 | 45 | */ |
46 | 46 | function random_bytes($bytes) |
47 | 47 | { |
48 | - static $fp = null; |
|
49 | - /** |
|
50 | - * This block should only be run once |
|
51 | - */ |
|
52 | - if (empty($fp)) { |
|
53 | - /** |
|
54 | - * We use /dev/urandom if it is a char device. |
|
55 | - * We never fall back to /dev/random |
|
56 | - */ |
|
57 | - $fp = fopen('/dev/urandom', 'rb'); |
|
58 | - if (!empty($fp)) { |
|
59 | - $st = fstat($fp); |
|
60 | - if (($st['mode'] & 0170000) !== 020000) { |
|
61 | - fclose($fp); |
|
62 | - $fp = false; |
|
63 | - } |
|
64 | - } |
|
48 | + static $fp = null; |
|
49 | + /** |
|
50 | + * This block should only be run once |
|
51 | + */ |
|
52 | + if (empty($fp)) { |
|
53 | + /** |
|
54 | + * We use /dev/urandom if it is a char device. |
|
55 | + * We never fall back to /dev/random |
|
56 | + */ |
|
57 | + $fp = fopen('/dev/urandom', 'rb'); |
|
58 | + if (!empty($fp)) { |
|
59 | + $st = fstat($fp); |
|
60 | + if (($st['mode'] & 0170000) !== 020000) { |
|
61 | + fclose($fp); |
|
62 | + $fp = false; |
|
63 | + } |
|
64 | + } |
|
65 | 65 | |
66 | - if (!empty($fp)) { |
|
67 | - /** |
|
68 | - * stream_set_read_buffer() does not exist in HHVM |
|
69 | - * |
|
70 | - * If we don't set the stream's read buffer to 0, PHP will |
|
71 | - * internally buffer 8192 bytes, which can waste entropy |
|
72 | - * |
|
73 | - * stream_set_read_buffer returns 0 on success |
|
74 | - */ |
|
75 | - if (function_exists('stream_set_read_buffer')) { |
|
76 | - stream_set_read_buffer($fp, RANDOM_COMPAT_READ_BUFFER); |
|
77 | - } |
|
78 | - if (function_exists('stream_set_chunk_size')) { |
|
79 | - stream_set_chunk_size($fp, RANDOM_COMPAT_READ_BUFFER); |
|
80 | - } |
|
81 | - } |
|
82 | - } |
|
66 | + if (!empty($fp)) { |
|
67 | + /** |
|
68 | + * stream_set_read_buffer() does not exist in HHVM |
|
69 | + * |
|
70 | + * If we don't set the stream's read buffer to 0, PHP will |
|
71 | + * internally buffer 8192 bytes, which can waste entropy |
|
72 | + * |
|
73 | + * stream_set_read_buffer returns 0 on success |
|
74 | + */ |
|
75 | + if (function_exists('stream_set_read_buffer')) { |
|
76 | + stream_set_read_buffer($fp, RANDOM_COMPAT_READ_BUFFER); |
|
77 | + } |
|
78 | + if (function_exists('stream_set_chunk_size')) { |
|
79 | + stream_set_chunk_size($fp, RANDOM_COMPAT_READ_BUFFER); |
|
80 | + } |
|
81 | + } |
|
82 | + } |
|
83 | 83 | |
84 | - try { |
|
85 | - $bytes = RandomCompat_intval($bytes); |
|
86 | - } catch (TypeError $ex) { |
|
87 | - throw new TypeError( |
|
88 | - 'random_bytes(): $bytes must be an integer' |
|
89 | - ); |
|
90 | - } |
|
84 | + try { |
|
85 | + $bytes = RandomCompat_intval($bytes); |
|
86 | + } catch (TypeError $ex) { |
|
87 | + throw new TypeError( |
|
88 | + 'random_bytes(): $bytes must be an integer' |
|
89 | + ); |
|
90 | + } |
|
91 | 91 | |
92 | - if ($bytes < 1) { |
|
93 | - throw new Error( |
|
94 | - 'Length must be greater than 0' |
|
95 | - ); |
|
96 | - } |
|
92 | + if ($bytes < 1) { |
|
93 | + throw new Error( |
|
94 | + 'Length must be greater than 0' |
|
95 | + ); |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * This if() block only runs if we managed to open a file handle |
|
100 | - * |
|
101 | - * It does not belong in an else {} block, because the above |
|
102 | - * if (empty($fp)) line is logic that should only be run once per |
|
103 | - * page load. |
|
104 | - */ |
|
105 | - if (!empty($fp)) { |
|
106 | - $remaining = $bytes; |
|
107 | - $buf = ''; |
|
98 | + /** |
|
99 | + * This if() block only runs if we managed to open a file handle |
|
100 | + * |
|
101 | + * It does not belong in an else {} block, because the above |
|
102 | + * if (empty($fp)) line is logic that should only be run once per |
|
103 | + * page load. |
|
104 | + */ |
|
105 | + if (!empty($fp)) { |
|
106 | + $remaining = $bytes; |
|
107 | + $buf = ''; |
|
108 | 108 | |
109 | - /** |
|
110 | - * We use fread() in a loop to protect against partial reads |
|
111 | - */ |
|
112 | - do { |
|
113 | - $read = fread($fp, $remaining); |
|
114 | - if ($read === false) { |
|
115 | - /** |
|
116 | - * We cannot safely read from the file. Exit the |
|
117 | - * do-while loop and trigger the exception condition |
|
118 | - */ |
|
119 | - $buf = false; |
|
120 | - break; |
|
121 | - } |
|
122 | - /** |
|
123 | - * Decrease the number of bytes returned from remaining |
|
124 | - */ |
|
125 | - $remaining -= RandomCompat_strlen($read); |
|
126 | - $buf .= $read; |
|
127 | - } while ($remaining > 0); |
|
109 | + /** |
|
110 | + * We use fread() in a loop to protect against partial reads |
|
111 | + */ |
|
112 | + do { |
|
113 | + $read = fread($fp, $remaining); |
|
114 | + if ($read === false) { |
|
115 | + /** |
|
116 | + * We cannot safely read from the file. Exit the |
|
117 | + * do-while loop and trigger the exception condition |
|
118 | + */ |
|
119 | + $buf = false; |
|
120 | + break; |
|
121 | + } |
|
122 | + /** |
|
123 | + * Decrease the number of bytes returned from remaining |
|
124 | + */ |
|
125 | + $remaining -= RandomCompat_strlen($read); |
|
126 | + $buf .= $read; |
|
127 | + } while ($remaining > 0); |
|
128 | 128 | |
129 | - /** |
|
130 | - * Is our result valid? |
|
131 | - */ |
|
132 | - if ($buf !== false) { |
|
133 | - if (RandomCompat_strlen($buf) === $bytes) { |
|
134 | - /** |
|
135 | - * Return our random entropy buffer here: |
|
136 | - */ |
|
137 | - return $buf; |
|
138 | - } |
|
139 | - } |
|
140 | - } |
|
129 | + /** |
|
130 | + * Is our result valid? |
|
131 | + */ |
|
132 | + if ($buf !== false) { |
|
133 | + if (RandomCompat_strlen($buf) === $bytes) { |
|
134 | + /** |
|
135 | + * Return our random entropy buffer here: |
|
136 | + */ |
|
137 | + return $buf; |
|
138 | + } |
|
139 | + } |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * If we reach here, PHP has failed us. |
|
144 | - */ |
|
145 | - throw new Exception( |
|
146 | - 'Error reading from source device' |
|
147 | - ); |
|
142 | + /** |
|
143 | + * If we reach here, PHP has failed us. |
|
144 | + */ |
|
145 | + throw new Exception( |
|
146 | + 'Error reading from source device' |
|
147 | + ); |
|
148 | 148 | } |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | /** |
100 | 100 | * Test for integer overflow: |
101 | 101 | */ |
102 | - if (!is_int($range)) { |
|
102 | + if ( ! is_int($range)) { |
|
103 | 103 | |
104 | 104 | /** |
105 | 105 | * Still safely calculate wider ranges. |
@@ -185,7 +185,7 @@ discard block |
||
185 | 185 | * ... or smaller than $min, |
186 | 186 | * then try again. |
187 | 187 | */ |
188 | - } while (!is_int($val) || $val > $max || $val < $min); |
|
188 | + } while ( ! is_int($val) || $val > $max || $val < $min); |
|
189 | 189 | |
190 | 190 | return (int) $val; |
191 | 191 | } |
@@ -38,154 +38,154 @@ |
||
38 | 38 | */ |
39 | 39 | function random_int($min, $max) |
40 | 40 | { |
41 | - /** |
|
42 | - * Type and input logic checks |
|
43 | - * |
|
44 | - * If you pass it a float in the range (~PHP_INT_MAX, PHP_INT_MAX) |
|
45 | - * (non-inclusive), it will sanely cast it to an int. If you it's equal to |
|
46 | - * ~PHP_INT_MAX or PHP_INT_MAX, we let it fail as not an integer. Floats |
|
47 | - * lose precision, so the <= and => operators might accidentally let a float |
|
48 | - * through. |
|
49 | - */ |
|
41 | + /** |
|
42 | + * Type and input logic checks |
|
43 | + * |
|
44 | + * If you pass it a float in the range (~PHP_INT_MAX, PHP_INT_MAX) |
|
45 | + * (non-inclusive), it will sanely cast it to an int. If you it's equal to |
|
46 | + * ~PHP_INT_MAX or PHP_INT_MAX, we let it fail as not an integer. Floats |
|
47 | + * lose precision, so the <= and => operators might accidentally let a float |
|
48 | + * through. |
|
49 | + */ |
|
50 | 50 | |
51 | - try { |
|
52 | - $min = RandomCompat_intval($min); |
|
53 | - } catch (TypeError $ex) { |
|
54 | - throw new TypeError( |
|
55 | - 'random_int(): $min must be an integer' |
|
56 | - ); |
|
57 | - } |
|
51 | + try { |
|
52 | + $min = RandomCompat_intval($min); |
|
53 | + } catch (TypeError $ex) { |
|
54 | + throw new TypeError( |
|
55 | + 'random_int(): $min must be an integer' |
|
56 | + ); |
|
57 | + } |
|
58 | 58 | |
59 | - try { |
|
60 | - $max = RandomCompat_intval($max); |
|
61 | - } catch (TypeError $ex) { |
|
62 | - throw new TypeError( |
|
63 | - 'random_int(): $max must be an integer' |
|
64 | - ); |
|
65 | - } |
|
59 | + try { |
|
60 | + $max = RandomCompat_intval($max); |
|
61 | + } catch (TypeError $ex) { |
|
62 | + throw new TypeError( |
|
63 | + 'random_int(): $max must be an integer' |
|
64 | + ); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Now that we've verified our weak typing system has given us an integer, |
|
69 | - * let's validate the logic then we can move forward with generating random |
|
70 | - * integers along a given range. |
|
71 | - */ |
|
72 | - if ($min > $max) { |
|
73 | - throw new Error( |
|
74 | - 'Minimum value must be less than or equal to the maximum value' |
|
75 | - ); |
|
76 | - } |
|
67 | + /** |
|
68 | + * Now that we've verified our weak typing system has given us an integer, |
|
69 | + * let's validate the logic then we can move forward with generating random |
|
70 | + * integers along a given range. |
|
71 | + */ |
|
72 | + if ($min > $max) { |
|
73 | + throw new Error( |
|
74 | + 'Minimum value must be less than or equal to the maximum value' |
|
75 | + ); |
|
76 | + } |
|
77 | 77 | |
78 | - if ($max === $min) { |
|
79 | - return $min; |
|
80 | - } |
|
78 | + if ($max === $min) { |
|
79 | + return $min; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Initialize variables to 0 |
|
84 | - * |
|
85 | - * We want to store: |
|
86 | - * $bytes => the number of random bytes we need |
|
87 | - * $mask => an integer bitmask (for use with the &) operator |
|
88 | - * so we can minimize the number of discards |
|
89 | - */ |
|
90 | - $attempts = $bits = $bytes = $mask = $valueShift = 0; |
|
82 | + /** |
|
83 | + * Initialize variables to 0 |
|
84 | + * |
|
85 | + * We want to store: |
|
86 | + * $bytes => the number of random bytes we need |
|
87 | + * $mask => an integer bitmask (for use with the &) operator |
|
88 | + * so we can minimize the number of discards |
|
89 | + */ |
|
90 | + $attempts = $bits = $bytes = $mask = $valueShift = 0; |
|
91 | 91 | |
92 | - /** |
|
93 | - * At this point, $range is a positive number greater than 0. It might |
|
94 | - * overflow, however, if $max - $min > PHP_INT_MAX. PHP will cast it to |
|
95 | - * a float and we will lose some precision. |
|
96 | - */ |
|
97 | - $range = $max - $min; |
|
92 | + /** |
|
93 | + * At this point, $range is a positive number greater than 0. It might |
|
94 | + * overflow, however, if $max - $min > PHP_INT_MAX. PHP will cast it to |
|
95 | + * a float and we will lose some precision. |
|
96 | + */ |
|
97 | + $range = $max - $min; |
|
98 | 98 | |
99 | - /** |
|
100 | - * Test for integer overflow: |
|
101 | - */ |
|
102 | - if (!is_int($range)) { |
|
99 | + /** |
|
100 | + * Test for integer overflow: |
|
101 | + */ |
|
102 | + if (!is_int($range)) { |
|
103 | 103 | |
104 | - /** |
|
105 | - * Still safely calculate wider ranges. |
|
106 | - * Provided by @CodesInChaos, @oittaa |
|
107 | - * |
|
108 | - * @ref https://gist.github.com/CodesInChaos/03f9ea0b58e8b2b8d435 |
|
109 | - * |
|
110 | - * We use ~0 as a mask in this case because it generates all 1s |
|
111 | - * |
|
112 | - * @ref https://eval.in/400356 (32-bit) |
|
113 | - * @ref http://3v4l.org/XX9r5 (64-bit) |
|
114 | - */ |
|
115 | - $bytes = PHP_INT_SIZE; |
|
116 | - $mask = ~0; |
|
104 | + /** |
|
105 | + * Still safely calculate wider ranges. |
|
106 | + * Provided by @CodesInChaos, @oittaa |
|
107 | + * |
|
108 | + * @ref https://gist.github.com/CodesInChaos/03f9ea0b58e8b2b8d435 |
|
109 | + * |
|
110 | + * We use ~0 as a mask in this case because it generates all 1s |
|
111 | + * |
|
112 | + * @ref https://eval.in/400356 (32-bit) |
|
113 | + * @ref http://3v4l.org/XX9r5 (64-bit) |
|
114 | + */ |
|
115 | + $bytes = PHP_INT_SIZE; |
|
116 | + $mask = ~0; |
|
117 | 117 | |
118 | - } else { |
|
118 | + } else { |
|
119 | 119 | |
120 | - /** |
|
121 | - * $bits is effectively ceil(log($range, 2)) without dealing with |
|
122 | - * type juggling |
|
123 | - */ |
|
124 | - while ($range > 0) { |
|
125 | - if ($bits % 8 === 0) { |
|
126 | - ++$bytes; |
|
127 | - } |
|
128 | - ++$bits; |
|
129 | - $range >>= 1; |
|
130 | - $mask = $mask << 1 | 1; |
|
131 | - } |
|
132 | - $valueShift = $min; |
|
133 | - } |
|
120 | + /** |
|
121 | + * $bits is effectively ceil(log($range, 2)) without dealing with |
|
122 | + * type juggling |
|
123 | + */ |
|
124 | + while ($range > 0) { |
|
125 | + if ($bits % 8 === 0) { |
|
126 | + ++$bytes; |
|
127 | + } |
|
128 | + ++$bits; |
|
129 | + $range >>= 1; |
|
130 | + $mask = $mask << 1 | 1; |
|
131 | + } |
|
132 | + $valueShift = $min; |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * Now that we have our parameters set up, let's begin generating |
|
137 | - * random integers until one falls between $min and $max |
|
138 | - */ |
|
139 | - do { |
|
140 | - /** |
|
141 | - * The rejection probability is at most 0.5, so this corresponds |
|
142 | - * to a failure probability of 2^-128 for a working RNG |
|
143 | - */ |
|
144 | - if ($attempts > 128) { |
|
145 | - throw new Exception( |
|
146 | - 'random_int: RNG is broken - too many rejections' |
|
147 | - ); |
|
148 | - } |
|
135 | + /** |
|
136 | + * Now that we have our parameters set up, let's begin generating |
|
137 | + * random integers until one falls between $min and $max |
|
138 | + */ |
|
139 | + do { |
|
140 | + /** |
|
141 | + * The rejection probability is at most 0.5, so this corresponds |
|
142 | + * to a failure probability of 2^-128 for a working RNG |
|
143 | + */ |
|
144 | + if ($attempts > 128) { |
|
145 | + throw new Exception( |
|
146 | + 'random_int: RNG is broken - too many rejections' |
|
147 | + ); |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * Let's grab the necessary number of random bytes |
|
152 | - */ |
|
153 | - $randomByteString = random_bytes($bytes); |
|
154 | - if ($randomByteString === false) { |
|
155 | - throw new Exception( |
|
156 | - 'Random number generator failure' |
|
157 | - ); |
|
158 | - } |
|
150 | + /** |
|
151 | + * Let's grab the necessary number of random bytes |
|
152 | + */ |
|
153 | + $randomByteString = random_bytes($bytes); |
|
154 | + if ($randomByteString === false) { |
|
155 | + throw new Exception( |
|
156 | + 'Random number generator failure' |
|
157 | + ); |
|
158 | + } |
|
159 | 159 | |
160 | - /** |
|
161 | - * Let's turn $randomByteString into an integer |
|
162 | - * |
|
163 | - * This uses bitwise operators (<< and |) to build an integer |
|
164 | - * out of the values extracted from ord() |
|
165 | - * |
|
166 | - * Example: [9F] | [6D] | [32] | [0C] => |
|
167 | - * 159 + 27904 + 3276800 + 201326592 => |
|
168 | - * 204631455 |
|
169 | - */ |
|
170 | - $val = 0; |
|
171 | - for ($i = 0; $i < $bytes; ++$i) { |
|
172 | - $val |= ord($randomByteString[$i]) << ($i * 8); |
|
173 | - } |
|
160 | + /** |
|
161 | + * Let's turn $randomByteString into an integer |
|
162 | + * |
|
163 | + * This uses bitwise operators (<< and |) to build an integer |
|
164 | + * out of the values extracted from ord() |
|
165 | + * |
|
166 | + * Example: [9F] | [6D] | [32] | [0C] => |
|
167 | + * 159 + 27904 + 3276800 + 201326592 => |
|
168 | + * 204631455 |
|
169 | + */ |
|
170 | + $val = 0; |
|
171 | + for ($i = 0; $i < $bytes; ++$i) { |
|
172 | + $val |= ord($randomByteString[$i]) << ($i * 8); |
|
173 | + } |
|
174 | 174 | |
175 | - /** |
|
176 | - * Apply mask |
|
177 | - */ |
|
178 | - $val &= $mask; |
|
179 | - $val += $valueShift; |
|
175 | + /** |
|
176 | + * Apply mask |
|
177 | + */ |
|
178 | + $val &= $mask; |
|
179 | + $val += $valueShift; |
|
180 | 180 | |
181 | - ++$attempts; |
|
182 | - /** |
|
183 | - * If $val overflows to a floating point number, |
|
184 | - * ... or is larger than $max, |
|
185 | - * ... or smaller than $min, |
|
186 | - * then try again. |
|
187 | - */ |
|
188 | - } while (!is_int($val) || $val > $max || $val < $min); |
|
181 | + ++$attempts; |
|
182 | + /** |
|
183 | + * If $val overflows to a floating point number, |
|
184 | + * ... or is larger than $max, |
|
185 | + * ... or smaller than $min, |
|
186 | + * then try again. |
|
187 | + */ |
|
188 | + } while (!is_int($val) || $val > $max || $val < $min); |
|
189 | 189 | |
190 | - return (int) $val; |
|
190 | + return (int) $val; |
|
191 | 191 | } |
@@ -26,7 +26,7 @@ |
||
26 | 26 | * SOFTWARE. |
27 | 27 | */ |
28 | 28 | |
29 | -if (!function_exists('RandomCompat_intval')) { |
|
29 | +if ( ! function_exists('RandomCompat_intval')) { |
|
30 | 30 | |
31 | 31 | /** |
32 | 32 | * Cast to an integer if we can, safely. |
@@ -28,44 +28,44 @@ |
||
28 | 28 | |
29 | 29 | if (!function_exists('RandomCompat_intval')) { |
30 | 30 | |
31 | - /** |
|
32 | - * Cast to an integer if we can, safely. |
|
33 | - * |
|
34 | - * If you pass it a float in the range (~PHP_INT_MAX, PHP_INT_MAX) |
|
35 | - * (non-inclusive), it will sanely cast it to an int. If you it's equal to |
|
36 | - * ~PHP_INT_MAX or PHP_INT_MAX, we let it fail as not an integer. Floats |
|
37 | - * lose precision, so the <= and => operators might accidentally let a float |
|
38 | - * through. |
|
39 | - * |
|
40 | - * @param int|float $number The number we want to convert to an int |
|
41 | - * @param boolean $fail_open Set to true to not throw an exception |
|
42 | - * |
|
43 | - * @return int (or float if $fail_open) |
|
44 | - * |
|
45 | - * @throws TypeError |
|
46 | - */ |
|
47 | - function RandomCompat_intval($number, $fail_open = false) |
|
48 | - { |
|
49 | - if (is_numeric($number)) { |
|
50 | - $number += 0; |
|
51 | - } |
|
31 | + /** |
|
32 | + * Cast to an integer if we can, safely. |
|
33 | + * |
|
34 | + * If you pass it a float in the range (~PHP_INT_MAX, PHP_INT_MAX) |
|
35 | + * (non-inclusive), it will sanely cast it to an int. If you it's equal to |
|
36 | + * ~PHP_INT_MAX or PHP_INT_MAX, we let it fail as not an integer. Floats |
|
37 | + * lose precision, so the <= and => operators might accidentally let a float |
|
38 | + * through. |
|
39 | + * |
|
40 | + * @param int|float $number The number we want to convert to an int |
|
41 | + * @param boolean $fail_open Set to true to not throw an exception |
|
42 | + * |
|
43 | + * @return int (or float if $fail_open) |
|
44 | + * |
|
45 | + * @throws TypeError |
|
46 | + */ |
|
47 | + function RandomCompat_intval($number, $fail_open = false) |
|
48 | + { |
|
49 | + if (is_numeric($number)) { |
|
50 | + $number += 0; |
|
51 | + } |
|
52 | 52 | |
53 | - if ( |
|
54 | - is_float($number) |
|
55 | - && |
|
56 | - $number > ~PHP_INT_MAX |
|
57 | - && |
|
58 | - $number < PHP_INT_MAX |
|
59 | - ) { |
|
60 | - $number = (int) $number; |
|
61 | - } |
|
53 | + if ( |
|
54 | + is_float($number) |
|
55 | + && |
|
56 | + $number > ~PHP_INT_MAX |
|
57 | + && |
|
58 | + $number < PHP_INT_MAX |
|
59 | + ) { |
|
60 | + $number = (int) $number; |
|
61 | + } |
|
62 | 62 | |
63 | - if (is_int($number) || $fail_open) { |
|
64 | - return $number; |
|
65 | - } |
|
63 | + if (is_int($number) || $fail_open) { |
|
64 | + return $number; |
|
65 | + } |
|
66 | 66 | |
67 | - throw new TypeError( |
|
68 | - 'Expected an integer.' |
|
69 | - ); |
|
70 | - } |
|
67 | + throw new TypeError( |
|
68 | + 'Expected an integer.' |
|
69 | + ); |
|
70 | + } |
|
71 | 71 | } |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | * SOFTWARE. |
27 | 27 | */ |
28 | 28 | |
29 | -if (!class_exists('Error', false)) { |
|
29 | +if ( ! class_exists('Error', false)) { |
|
30 | 30 | // We can't really avoid making this extend Exception in PHP 5. |
31 | 31 | class Error extends Exception |
32 | 32 | { |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | } |
35 | 35 | } |
36 | 36 | |
37 | -if (!class_exists('TypeError', false)) { |
|
37 | +if ( ! class_exists('TypeError', false)) { |
|
38 | 38 | class TypeError extends Error |
39 | 39 | { |
40 | 40 |
@@ -27,16 +27,16 @@ |
||
27 | 27 | */ |
28 | 28 | |
29 | 29 | if (!class_exists('Error', false)) { |
30 | - // We can't really avoid making this extend Exception in PHP 5. |
|
31 | - class Error extends Exception |
|
32 | - { |
|
30 | + // We can't really avoid making this extend Exception in PHP 5. |
|
31 | + class Error extends Exception |
|
32 | + { |
|
33 | 33 | |
34 | - } |
|
34 | + } |
|
35 | 35 | } |
36 | 36 | |
37 | 37 | if (!class_exists('TypeError', false)) { |
38 | - class TypeError extends Error |
|
39 | - { |
|
38 | + class TypeError extends Error |
|
39 | + { |
|
40 | 40 | |
41 | - } |
|
41 | + } |
|
42 | 42 | } |
@@ -90,8 +90,9 @@ discard block |
||
90 | 90 | function get_category( $category, $output = OBJECT, $filter = 'raw' ) { |
91 | 91 | $category = get_term( $category, 'category', $output, $filter ); |
92 | 92 | |
93 | - if ( is_wp_error( $category ) ) |
|
94 | - return $category; |
|
93 | + if ( is_wp_error( $category ) ) { |
|
94 | + return $category; |
|
95 | + } |
|
95 | 96 | |
96 | 97 | _make_cat_compat( $category ); |
97 | 98 | |
@@ -170,8 +171,9 @@ discard block |
||
170 | 171 | */ |
171 | 172 | function get_category_by_slug( $slug ) { |
172 | 173 | $category = get_term_by( 'slug', $slug, 'category' ); |
173 | - if ( $category ) |
|
174 | - _make_cat_compat( $category ); |
|
174 | + if ( $category ) { |
|
175 | + _make_cat_compat( $category ); |
|
176 | + } |
|
175 | 177 | |
176 | 178 | return $category; |
177 | 179 | } |
@@ -186,8 +188,9 @@ discard block |
||
186 | 188 | */ |
187 | 189 | function get_cat_ID( $cat_name ) { |
188 | 190 | $cat = get_term_by( 'name', $cat_name, 'category' ); |
189 | - if ( $cat ) |
|
190 | - return $cat->term_id; |
|
191 | + if ( $cat ) { |
|
192 | + return $cat->term_id; |
|
193 | + } |
|
191 | 194 | return 0; |
192 | 195 | } |
193 | 196 | |
@@ -202,8 +205,9 @@ discard block |
||
202 | 205 | function get_cat_name( $cat_id ) { |
203 | 206 | $cat_id = (int) $cat_id; |
204 | 207 | $category = get_term( $cat_id, 'category' ); |
205 | - if ( ! $category || is_wp_error( $category ) ) |
|
206 | - return ''; |
|
208 | + if ( ! $category || is_wp_error( $category ) ) { |
|
209 | + return ''; |
|
210 | + } |
|
207 | 211 | return $category->name; |
208 | 212 | } |
209 | 213 |
@@ -23,9 +23,9 @@ discard block |
||
23 | 23 | * } |
24 | 24 | * @return array List of categories. |
25 | 25 | */ |
26 | -function get_categories( $args = '' ) { |
|
27 | - $defaults = array( 'taxonomy' => 'category' ); |
|
28 | - $args = wp_parse_args( $args, $defaults ); |
|
26 | +function get_categories($args = '') { |
|
27 | + $defaults = array('taxonomy' => 'category'); |
|
28 | + $args = wp_parse_args($args, $defaults); |
|
29 | 29 | |
30 | 30 | $taxonomy = $args['taxonomy']; |
31 | 31 | |
@@ -37,13 +37,13 @@ discard block |
||
37 | 37 | * @param string $taxonomy Taxonomy to retrieve terms from. |
38 | 38 | * @param array $args An array of arguments. See get_terms(). |
39 | 39 | */ |
40 | - $taxonomy = apply_filters( 'get_categories_taxonomy', $taxonomy, $args ); |
|
40 | + $taxonomy = apply_filters('get_categories_taxonomy', $taxonomy, $args); |
|
41 | 41 | |
42 | 42 | // Back compat |
43 | - if ( isset($args['type']) && 'link' == $args['type'] ) { |
|
44 | - _deprecated_argument( __FUNCTION__, '3.0.0', |
|
43 | + if (isset($args['type']) && 'link' == $args['type']) { |
|
44 | + _deprecated_argument(__FUNCTION__, '3.0.0', |
|
45 | 45 | /* translators: 1: "type => link", 2: "taxonomy => link_category" */ |
46 | - sprintf( __( '%1$s is deprecated. Use %2$s instead.' ), |
|
46 | + sprintf(__('%1$s is deprecated. Use %2$s instead.'), |
|
47 | 47 | '<code>type => link</code>', |
48 | 48 | '<code>taxonomy => link_category</code>' |
49 | 49 | ) |
@@ -51,14 +51,14 @@ discard block |
||
51 | 51 | $taxonomy = $args['taxonomy'] = 'link_category'; |
52 | 52 | } |
53 | 53 | |
54 | - $categories = get_terms( $taxonomy, $args ); |
|
54 | + $categories = get_terms($taxonomy, $args); |
|
55 | 55 | |
56 | - if ( is_wp_error( $categories ) ) { |
|
56 | + if (is_wp_error($categories)) { |
|
57 | 57 | $categories = array(); |
58 | 58 | } else { |
59 | 59 | $categories = (array) $categories; |
60 | - foreach ( array_keys( $categories ) as $k ) { |
|
61 | - _make_cat_compat( $categories[ $k ] ); |
|
60 | + foreach (array_keys($categories) as $k) { |
|
61 | + _make_cat_compat($categories[$k]); |
|
62 | 62 | } |
63 | 63 | } |
64 | 64 | |
@@ -88,13 +88,13 @@ discard block |
||
88 | 88 | * @return object|array|WP_Error|null Category data in type defined by $output parameter. |
89 | 89 | * WP_Error if $category is empty, null if it does not exist. |
90 | 90 | */ |
91 | -function get_category( $category, $output = OBJECT, $filter = 'raw' ) { |
|
92 | - $category = get_term( $category, 'category', $output, $filter ); |
|
91 | +function get_category($category, $output = OBJECT, $filter = 'raw') { |
|
92 | + $category = get_term($category, 'category', $output, $filter); |
|
93 | 93 | |
94 | - if ( is_wp_error( $category ) ) |
|
94 | + if (is_wp_error($category)) |
|
95 | 95 | return $category; |
96 | 96 | |
97 | - _make_cat_compat( $category ); |
|
97 | + _make_cat_compat($category); |
|
98 | 98 | |
99 | 99 | return $category; |
100 | 100 | } |
@@ -119,45 +119,45 @@ discard block |
||
119 | 119 | * a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT. |
120 | 120 | * @return WP_Term|array|WP_Error|null Type is based on $output value. |
121 | 121 | */ |
122 | -function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) { |
|
123 | - $category_path = rawurlencode( urldecode( $category_path ) ); |
|
124 | - $category_path = str_replace( '%2F', '/', $category_path ); |
|
125 | - $category_path = str_replace( '%20', ' ', $category_path ); |
|
126 | - $category_paths = '/' . trim( $category_path, '/' ); |
|
127 | - $leaf_path = sanitize_title( basename( $category_paths ) ); |
|
128 | - $category_paths = explode( '/', $category_paths ); |
|
122 | +function get_category_by_path($category_path, $full_match = true, $output = OBJECT) { |
|
123 | + $category_path = rawurlencode(urldecode($category_path)); |
|
124 | + $category_path = str_replace('%2F', '/', $category_path); |
|
125 | + $category_path = str_replace('%20', ' ', $category_path); |
|
126 | + $category_paths = '/'.trim($category_path, '/'); |
|
127 | + $leaf_path = sanitize_title(basename($category_paths)); |
|
128 | + $category_paths = explode('/', $category_paths); |
|
129 | 129 | $full_path = ''; |
130 | - foreach ( (array) $category_paths as $pathdir ) { |
|
131 | - $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title( $pathdir ); |
|
130 | + foreach ((array) $category_paths as $pathdir) { |
|
131 | + $full_path .= ($pathdir != '' ? '/' : '').sanitize_title($pathdir); |
|
132 | 132 | } |
133 | - $categories = get_terms( 'category', array('get' => 'all', 'slug' => $leaf_path) ); |
|
133 | + $categories = get_terms('category', array('get' => 'all', 'slug' => $leaf_path)); |
|
134 | 134 | |
135 | - if ( empty( $categories ) ) { |
|
135 | + if (empty($categories)) { |
|
136 | 136 | return; |
137 | 137 | } |
138 | 138 | |
139 | - foreach ( $categories as $category ) { |
|
140 | - $path = '/' . $leaf_path; |
|
139 | + foreach ($categories as $category) { |
|
140 | + $path = '/'.$leaf_path; |
|
141 | 141 | $curcategory = $category; |
142 | - while ( ( $curcategory->parent != 0 ) && ( $curcategory->parent != $curcategory->term_id ) ) { |
|
143 | - $curcategory = get_term( $curcategory->parent, 'category' ); |
|
144 | - if ( is_wp_error( $curcategory ) ) { |
|
142 | + while (($curcategory->parent != 0) && ($curcategory->parent != $curcategory->term_id)) { |
|
143 | + $curcategory = get_term($curcategory->parent, 'category'); |
|
144 | + if (is_wp_error($curcategory)) { |
|
145 | 145 | return $curcategory; |
146 | 146 | } |
147 | - $path = '/' . $curcategory->slug . $path; |
|
147 | + $path = '/'.$curcategory->slug.$path; |
|
148 | 148 | } |
149 | 149 | |
150 | - if ( $path == $full_path ) { |
|
151 | - $category = get_term( $category->term_id, 'category', $output ); |
|
152 | - _make_cat_compat( $category ); |
|
150 | + if ($path == $full_path) { |
|
151 | + $category = get_term($category->term_id, 'category', $output); |
|
152 | + _make_cat_compat($category); |
|
153 | 153 | return $category; |
154 | 154 | } |
155 | 155 | } |
156 | 156 | |
157 | 157 | // If full matching is not required, return the first cat that matches the leaf. |
158 | - if ( ! $full_match ) { |
|
159 | - $category = get_term( reset( $categories )->term_id, 'category', $output ); |
|
160 | - _make_cat_compat( $category ); |
|
158 | + if ( ! $full_match) { |
|
159 | + $category = get_term(reset($categories)->term_id, 'category', $output); |
|
160 | + _make_cat_compat($category); |
|
161 | 161 | return $category; |
162 | 162 | } |
163 | 163 | } |
@@ -170,10 +170,10 @@ discard block |
||
170 | 170 | * @param string $slug The category slug. |
171 | 171 | * @return object Category data object |
172 | 172 | */ |
173 | -function get_category_by_slug( $slug ) { |
|
174 | - $category = get_term_by( 'slug', $slug, 'category' ); |
|
175 | - if ( $category ) |
|
176 | - _make_cat_compat( $category ); |
|
173 | +function get_category_by_slug($slug) { |
|
174 | + $category = get_term_by('slug', $slug, 'category'); |
|
175 | + if ($category) |
|
176 | + _make_cat_compat($category); |
|
177 | 177 | |
178 | 178 | return $category; |
179 | 179 | } |
@@ -186,9 +186,9 @@ discard block |
||
186 | 186 | * @param string $cat_name Category name. |
187 | 187 | * @return int 0, if failure and ID of category on success. |
188 | 188 | */ |
189 | -function get_cat_ID( $cat_name ) { |
|
190 | - $cat = get_term_by( 'name', $cat_name, 'category' ); |
|
191 | - if ( $cat ) |
|
189 | +function get_cat_ID($cat_name) { |
|
190 | + $cat = get_term_by('name', $cat_name, 'category'); |
|
191 | + if ($cat) |
|
192 | 192 | return $cat->term_id; |
193 | 193 | return 0; |
194 | 194 | } |
@@ -201,10 +201,10 @@ discard block |
||
201 | 201 | * @param int $cat_id Category ID |
202 | 202 | * @return string Category name, or an empty string if category doesn't exist. |
203 | 203 | */ |
204 | -function get_cat_name( $cat_id ) { |
|
204 | +function get_cat_name($cat_id) { |
|
205 | 205 | $cat_id = (int) $cat_id; |
206 | - $category = get_term( $cat_id, 'category' ); |
|
207 | - if ( ! $category || is_wp_error( $category ) ) |
|
206 | + $category = get_term($cat_id, 'category'); |
|
207 | + if ( ! $category || is_wp_error($category)) |
|
208 | 208 | return ''; |
209 | 209 | return $category->name; |
210 | 210 | } |
@@ -221,8 +221,8 @@ discard block |
||
221 | 221 | * @param int|object $cat2 The child category. |
222 | 222 | * @return bool Whether $cat2 is child of $cat1 |
223 | 223 | */ |
224 | -function cat_is_ancestor_of( $cat1, $cat2 ) { |
|
225 | - return term_is_ancestor_of( $cat1, $cat2, 'category' ); |
|
224 | +function cat_is_ancestor_of($cat1, $cat2) { |
|
225 | + return term_is_ancestor_of($cat1, $cat2, 'category'); |
|
226 | 226 | } |
227 | 227 | |
228 | 228 | /** |
@@ -234,8 +234,8 @@ discard block |
||
234 | 234 | * @param string $context Optional. Default is 'display'. |
235 | 235 | * @return object|array Same type as $category with sanitized data for safe use. |
236 | 236 | */ |
237 | -function sanitize_category( $category, $context = 'display' ) { |
|
238 | - return sanitize_term( $category, 'category', $context ); |
|
237 | +function sanitize_category($category, $context = 'display') { |
|
238 | + return sanitize_term($category, 'category', $context); |
|
239 | 239 | } |
240 | 240 | |
241 | 241 | /** |
@@ -249,8 +249,8 @@ discard block |
||
249 | 249 | * @param string $context What filter to use, 'raw', 'display', etc. |
250 | 250 | * @return mixed Same type as $value after $value has been sanitized. |
251 | 251 | */ |
252 | -function sanitize_category_field( $field, $value, $cat_id, $context ) { |
|
253 | - return sanitize_term_field( $field, $value, $cat_id, 'category', $context ); |
|
252 | +function sanitize_category_field($field, $value, $cat_id, $context) { |
|
253 | + return sanitize_term_field($field, $value, $cat_id, 'category', $context); |
|
254 | 254 | } |
255 | 255 | |
256 | 256 | /* Tags */ |
@@ -264,10 +264,10 @@ discard block |
||
264 | 264 | * @param string|array $args Tag arguments to use when retrieving tags. |
265 | 265 | * @return array List of tags. |
266 | 266 | */ |
267 | -function get_tags( $args = '' ) { |
|
268 | - $tags = get_terms( 'post_tag', $args ); |
|
267 | +function get_tags($args = '') { |
|
268 | + $tags = get_terms('post_tag', $args); |
|
269 | 269 | |
270 | - if ( empty( $tags ) ) { |
|
270 | + if (empty($tags)) { |
|
271 | 271 | $return = array(); |
272 | 272 | return $return; |
273 | 273 | } |
@@ -280,7 +280,7 @@ discard block |
||
280 | 280 | * @param array $tags Array of 'post_tag' term objects. |
281 | 281 | * @param array $args An array of arguments. @see get_terms() |
282 | 282 | */ |
283 | - $tags = apply_filters( 'get_tags', $tags, $args ); |
|
283 | + $tags = apply_filters('get_tags', $tags, $args); |
|
284 | 284 | return $tags; |
285 | 285 | } |
286 | 286 | |
@@ -304,8 +304,8 @@ discard block |
||
304 | 304 | * @param string $filter Optional. Default is raw or no WordPress defined filter will applied. |
305 | 305 | * @return WP_Term|array|WP_Error|null Tag data in type defined by $output parameter. WP_Error if $tag is empty, null if it does not exist. |
306 | 306 | */ |
307 | -function get_tag( $tag, $output = OBJECT, $filter = 'raw' ) { |
|
308 | - return get_term( $tag, 'post_tag', $output, $filter ); |
|
307 | +function get_tag($tag, $output = OBJECT, $filter = 'raw') { |
|
308 | + return get_term($tag, 'post_tag', $output, $filter); |
|
309 | 309 | } |
310 | 310 | |
311 | 311 | /* Cache */ |
@@ -317,8 +317,8 @@ discard block |
||
317 | 317 | * |
318 | 318 | * @param int $id Category ID |
319 | 319 | */ |
320 | -function clean_category_cache( $id ) { |
|
321 | - clean_term_cache( $id, 'category' ); |
|
320 | +function clean_category_cache($id) { |
|
321 | + clean_term_cache($id, 'category'); |
|
322 | 322 | } |
323 | 323 | |
324 | 324 | /** |
@@ -341,15 +341,15 @@ discard block |
||
341 | 341 | * |
342 | 342 | * @param array|object|WP_Term $category Category Row object or array |
343 | 343 | */ |
344 | -function _make_cat_compat( &$category ) { |
|
345 | - if ( is_object( $category ) && ! is_wp_error( $category ) ) { |
|
344 | +function _make_cat_compat(&$category) { |
|
345 | + if (is_object($category) && ! is_wp_error($category)) { |
|
346 | 346 | $category->cat_ID = $category->term_id; |
347 | 347 | $category->category_count = $category->count; |
348 | 348 | $category->category_description = $category->description; |
349 | 349 | $category->cat_name = $category->name; |
350 | 350 | $category->category_nicename = $category->slug; |
351 | 351 | $category->category_parent = $category->parent; |
352 | - } elseif ( is_array( $category ) && isset( $category['term_id'] ) ) { |
|
352 | + } elseif (is_array($category) && isset($category['term_id'])) { |
|
353 | 353 | $category['cat_ID'] = &$category['term_id']; |
354 | 354 | $category['category_count'] = &$category['count']; |
355 | 355 | $category['category_description'] = &$category['description']; |