| @@ -23,26 +23,28 @@ | ||
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | 25 | * Get media summary for a post. |
| 26 | 26 | * |
| 27 | - * @param int $post_id Post ID. | |
| 27 | + * @param ?int $post_id Post ID. | |
| 28 | 28 | * @param int $blog_id Blog ID, if applicable. |
| 29 | 29 | * @param array $args { |
| 30 | 30 | * Optional. An array of arguments. |
| 31 | 31 | * @type int $max_words Maximum number of words. |
| 32 | 32 | * @type int $max_chars Maximum number of characters. |
| 33 | + * @type bool $include_excerpt Whether to compute the excerpt and return it. Default true. | |
| 34 | + * @type bool $include_counts Whether to compute word/link counts. Default true. | |
| 33 | 35 | * } |
| 34 | 36 | * |
| 35 | 37 | * @return array|mixed|void |
| 36 | 38 | */ |
| 37 | - public static function get( $post_id, $blog_id = 0, $args = array() ) { | |
| 38 | - // @todo: Use type hinting in the line above when at PHP 7.0+. | |
| 39 | + public static function get( ?int $post_id, int $blog_id = 0, array $args = array() ) { | |
| 39 | 40 | $post_id = (int) $post_id; |
| 40 | - $blog_id = (int) $blog_id; | |
| 41 | 41 | |
| 42 | 42 | $defaults = array( |
| 43 | - 'max_words' => 16, | |
| 44 | - 'max_chars' => 256, | |
| 43 | + 'max_words' => 16, | |
| 44 | + 'max_chars' => 256, | |
| 45 | + 'include_excerpt' => true, | |
| 46 | + 'include_counts' => true, | |
| 45 | 47 | ); |
| 46 | 48 | $args = wp_parse_args( $args, $defaults ); |
| 47 | 49 | |
| 48 | 50 | $switched = false; |
| @@ -52,9 +54,10 @@ | ||
| 52 | 54 | } else { |
| 53 | 55 | $blog_id = get_current_blog_id(); |
| 54 | 56 | } |
| 55 | 57 | |
| 56 | - $cache_key = "{$blog_id}_{$post_id}_{$args['max_words']}_{$args['max_chars']}"; | |
| 58 | + $cache_key = "{$blog_id}_{$post_id}_{$args['max_words']}_{$args['max_chars']}_" | |
| 59 | + . (int) $args['include_excerpt'] . '_' . (int) $args['include_counts']; | |
| 57 | 60 | if ( isset( self::$cache[ $cache_key ] ) ) { |
| 58 | 61 | if ( $switched ) { |
| 59 | 62 | restore_current_blog(); |
| 60 | 63 | } |
| @@ -77,20 +80,28 @@ | ||
| 77 | 80 | 'secure' => array( |
| 78 | 81 | 'image' => '', |
| 79 | 82 | ), |
| 80 | 83 | 'count' => array( |
| 81 | - 'image' => 0, | |
| 82 | - 'video' => 0, | |
| 83 | - 'word' => 0, | |
| 84 | - 'link' => 0, | |
| 84 | + 'image' => 0, | |
| 85 | + 'video' => 0, | |
| 86 | + 'word' => 0, | |
| 87 | + 'word_remaining' => 0, | |
| 88 | + 'link' => 0, | |
| 85 | 89 | ), |
| 86 | 90 | ); |
| 87 | 91 | |
| 88 | 92 | if ( $post instanceof WP_Post && empty( $post->post_password ) ) { |
| 89 | - $return['excerpt'] = self::get_excerpt( $post->post_content, $post->post_excerpt, $args['max_words'], $args['max_chars'], $post ); | |
| 90 | - $return['count']['word'] = self::get_word_count( $post->post_content ); | |
| 91 | - $return['count']['word_remaining'] = self::get_word_remaining_count( $post->post_content, $return['excerpt'] ); | |
| 92 | - $return['count']['link'] = self::get_link_count( $post->post_content ); | |
| 93 | + if ( $args['include_excerpt'] ) { | |
| 94 | + $return['excerpt'] = self::get_excerpt( $post->post_content, $post->post_excerpt, $args['max_words'], $args['max_chars'], $post ); | |
| 95 | + } | |
| 96 | + if ( $args['include_counts'] ) { | |
| 97 | + $return['count']['word'] = self::get_word_count( $post->post_content ); | |
| 98 | + $return['count']['link'] = self::get_link_count( $post->post_content ); | |
| 99 | + // Only compute word_remaining if we have an excerpt. If not, leave the default of 0. | |
| 100 | + if ( $args['include_excerpt'] && '' !== $return['excerpt'] ) { | |
| 101 | + $return['count']['word_remaining'] = self::get_word_remaining_count( $post->post_content, $return['excerpt'] ); | |
| 102 | + } | |
| 103 | + } | |
| 93 | 104 | } |
| 94 | 105 | |
| 95 | 106 | $extract = Jetpack_Media_Meta_Extractor::extract( $blog_id, $post_id, Jetpack_Media_Meta_Extractor::ALL ); |
| 96 | 107 | |
| @@ -145,8 +156,11 @@ | ||
| 145 | 156 | ++$return['count']['video']; |
| 146 | 157 | break; |
| 147 | 158 | case 'youtube': |
| 148 | 159 | if ( 0 === $return['count']['video'] ) { |
| 160 | + if ( ! isset( $extract['shortcode']['youtube']['id'][0] ) ) { | |
| 161 | + break; | |
| 162 | + } | |
| 149 | 163 | $return['type'] = 'video'; |
| 150 | 164 | $return['video'] = esc_url_raw( 'http://www.youtube.com/watch?feature=player_embedded&v=' . $extract['shortcode']['youtube']['id'][0] ); |
| 151 | 165 | $return['image'] = self::get_video_poster( 'youtube', $extract['shortcode']['youtube']['id'][0] ); |
| 152 | 166 | $return['secure']['video'] = self::https( $return['video'] ); |
| @@ -155,8 +169,11 @@ | ||
| 155 | 169 | ++$return['count']['video']; |
| 156 | 170 | break; |
| 157 | 171 | case 'vimeo': |
| 158 | 172 | if ( 0 === $return['count']['video'] ) { |
| 173 | + if ( ! isset( $extract['shortcode']['vimeo']['id'][0] ) ) { | |
| 174 | + break; | |
| 175 | + } | |
| 159 | 176 | $return['type'] = 'video'; |
| 160 | 177 | $return['video'] = esc_url_raw( 'http://vimeo.com/' . $extract['shortcode']['vimeo']['id'][0] ); |
| 161 | 178 | $return['secure']['video'] = self::https( $return['video'] ); |
| 162 | 179 | |
| @@ -179,18 +196,18 @@ | ||
| 179 | 196 | if ( 0 === $return['count']['video'] ) { |
| 180 | 197 | $return['type'] = 'video'; |
| 181 | 198 | $return['video'] = 'http://' . $embed; |
| 182 | 199 | $return['secure']['video'] = self::https( $return['video'] ); |
| 183 | - if ( false !== strpos( $embed, 'youtube' ) ) { | |
| 200 | + if ( str_contains( $embed, 'youtube' ) ) { | |
| 184 | 201 | $return['image'] = self::get_video_poster( 'youtube', jetpack_get_youtube_id( $return['video'] ) ); |
| 185 | 202 | $return['secure']['image'] = self::https( $return['image'] ); |
| 186 | - } elseif ( false !== strpos( $embed, 'youtu.be' ) ) { | |
| 203 | + } elseif ( str_contains( $embed, 'youtu.be' ) ) { | |
| 187 | 204 | $youtube_id = jetpack_get_youtube_id( $return['video'] ); |
| 188 | 205 | $return['video'] = 'http://youtube.com/watch?v=' . $youtube_id . '&feature=youtu.be'; |
| 189 | 206 | $return['secure']['video'] = self::https( $return['video'] ); |
| 190 | 207 | $return['image'] = self::get_video_poster( 'youtube', jetpack_get_youtube_id( $return['video'] ) ); |
| 191 | 208 | $return['secure']['image'] = self::https( $return['image'] ); |
| 192 | - } elseif ( false !== strpos( $embed, 'vimeo' ) ) { | |
| 209 | + } elseif ( str_contains( $embed, 'vimeo' ) ) { | |
| 193 | 210 | $poster_image = get_post_meta( $post_id, 'vimeo_poster_image', true ); |
| 194 | 211 | if ( ! empty( $poster_image ) ) { |
| 195 | 212 | $return['image'] = $poster_image; |
| 196 | 213 | $poster_url_parts = wp_parse_url( $poster_image ); |
| @@ -195,9 +212,9 @@ | ||
| 195 | 212 | $return['image'] = $poster_image; |
| 196 | 213 | $poster_url_parts = wp_parse_url( $poster_image ); |
| 197 | 214 | $return['secure']['image'] = 'https://secure-a.vimeocdn.com' . $poster_url_parts['path']; |
| 198 | 215 | } |
| 199 | - } elseif ( false !== strpos( $embed, 'dailymotion' ) ) { | |
| 216 | + } elseif ( str_contains( $embed, 'dailymotion' ) ) { | |
| 200 | 217 | $return['image'] = str_replace( 'dailymotion.com/video/', 'dailymotion.com/thumbnail/video/', $embed ); |
| 201 | 218 | $return['image'] = wp_parse_url( $return['image'], PHP_URL_SCHEME ) === null ? 'http://' . $return['image'] : $return['image']; |
| 202 | 219 | $return['secure']['image'] = self::https( $return['image'] ); |
| 203 | 220 | } |
| @@ -221,9 +238,9 @@ | ||
| 221 | 238 | } |
| 222 | 239 | ++$number_of_paragraphs; |
| 223 | 240 | } |
| 224 | 241 | |
| 225 | - $number_of_paragraphs = $number_of_paragraphs - $return['count']['video']; // subtract amount for videos. | |
| 242 | + $number_of_paragraphs -= $return['count']['video']; // subtract amount for videos. | |
| 226 | 243 | |
| 227 | 244 | // More than 2 paragraph? The video is not the primary focus so we can do some more analysis. |
| 228 | 245 | if ( $number_of_paragraphs > 2 ) { |
| 229 | 246 | $return['type'] = 'standard'; |
| @@ -247,9 +264,9 @@ | ||
| 247 | 264 | $number_of_paragraphs = 0; |
| 248 | 265 | |
| 249 | 266 | foreach ( $paragraphs as $i => $paragraph ) { |
| 250 | 267 | // Don't include 'actual' captions as a paragraph. |
| 251 | - if ( false !== strpos( $paragraph, '[caption' ) ) { | |
| 268 | + if ( str_contains( $paragraph, '[caption' ) ) { | |
| 252 | 269 | unset( $paragraphs[ $i ] ); |
| 253 | 270 | continue; |
| 254 | 271 | } |
| 255 | 272 | // Don't include blank lines as a paragraph. |
| @@ -259,11 +276,14 @@ | ||
| 259 | 276 | } |
| 260 | 277 | ++$number_of_paragraphs; |
| 261 | 278 | } |
| 262 | 279 | |
| 263 | - $return['image'] = $extract['image'][0]['url']; | |
| 264 | - $return['secure']['image'] = self::ssl_img( $return['image'] ); | |
| 265 | - ++$return['count']['image']; | |
| 280 | + // @phan-suppress-next-line PhanTypeMismatchDimFetch -- Phan is understandably confused, as $extract has many forms, including this one. | |
| 281 | + if ( ! empty( $extract['image'][0]['url'] ) ) { | |
| 282 | + $return['image'] = $extract['image'][0]['url']; | |
| 283 | + $return['secure']['image'] = self::ssl_img( $return['image'] ); | |
| 284 | + ++$return['count']['image']; | |
| 285 | + } | |
| 266 | 286 | |
| 267 | 287 | if ( $number_of_paragraphs <= 2 && is_countable( $extract['image'] ) && 1 === count( $extract['image'] ) ) { |
| 268 | 288 | // If we have lots of text or images, let's not treat it as an image post, but return its first image. |
| 269 | 289 | $return['type'] = 'image'; |
| @@ -308,9 +328,9 @@ | ||
| 308 | 328 | * |
| 309 | 329 | * @return string URL. |
| 310 | 330 | */ |
| 311 | 331 | public static function ssl_img( $url ) { |
| 312 | - if ( false !== strpos( $url, 'files.wordpress.com' ) ) { | |
| 332 | + if ( str_contains( $url, 'files.wordpress.com' ) ) { | |
| 313 | 333 | return self::https( $url ); |
| 314 | 334 | } else { |
| 315 | 335 | return self::https( Image_CDN_Core::cdn_url( $url ) ); |
| 316 | 336 | } |
| @@ -340,27 +360,20 @@ | ||
| 340 | 360 | /** |
| 341 | 361 | * Clean text of shortcodes and tags. |
| 342 | 362 | * |
| 343 | 363 | * @param string $text Dirty text. |
| 364 | + * @param bool $preserve_urls When true, keep http(s) URLs in the text instead of stripping them. Default false. | |
| 344 | 365 | * |
| 345 | 366 | * @return string Clean text. |
| 346 | 367 | */ |
| 347 | - public static function clean_text( $text ) { | |
| 348 | - return trim( | |
| 349 | - preg_replace( | |
| 350 | - '/[\s]+/', | |
| 351 | - ' ', | |
| 352 | - preg_replace( | |
| 353 | - '@https?://[\S]+@', | |
| 354 | - '', | |
| 355 | - strip_shortcodes( | |
| 356 | - wp_strip_all_tags( | |
| 357 | - $text | |
| 358 | - ) | |
| 359 | - ) | |
| 360 | - ) | |
| 361 | - ) | |
| 362 | - ); | |
| 368 | + public static function clean_text( $text, $preserve_urls = false ) { | |
| 369 | + $text = strip_shortcodes( wp_strip_all_tags( $text ) ); | |
| 370 | + | |
| 371 | + if ( ! $preserve_urls ) { | |
| 372 | + $text = preg_replace( '@https?://[\S]+@', '', $text ); | |
| 373 | + } | |
| 374 | + | |
| 375 | + return trim( preg_replace( '/[\s]+/', ' ', $text ) ); | |
| 363 | 376 | } |
| 364 | 377 | |
| 365 | 378 | /** |
| 366 | 379 | * Retrieve an excerpt for the post summary. |
| @@ -374,11 +387,12 @@ | ||
| 374 | 387 | * @param string $post_excerpt The post's excerpt. Empty if none was explicitly set. |
| 375 | 388 | * @param int $max_words Maximum number of words for the excerpt. Used on wp.com. Default 16. |
| 376 | 389 | * @param int $max_chars Maximum characters in the excerpt. Used on wp.com. Default 256. |
| 377 | 390 | * @param WP_Post $requested_post The post object. |
| 391 | + * @param bool $preserve_urls When true, keep http(s) URLs in the excerpt instead of stripping them. Default false. | |
| 378 | 392 | * @return string Post excerpt. |
| 379 | 393 | **/ |
| 380 | - public static function get_excerpt( $post_content, $post_excerpt, $max_words = 16, $max_chars = 256, $requested_post = null ) { | |
| 394 | + public static function get_excerpt( $post_content, $post_excerpt, $max_words = 16, $max_chars = 256, $requested_post = null, $preserve_urls = false ) { | |
| 381 | 395 | global $post; |
| 382 | 396 | $original_post = $post; // Saving the global for later use. |
| 383 | 397 | if ( empty( $post_excerpt ) && function_exists( 'wpcom_enhanced_excerpt_extract_excerpt' ) ) { |
| 384 | 398 | return self::clean_text( |
| @@ -390,9 +404,10 @@ | ||
| 390 | 404 | 'max_words' => $max_words, |
| 391 | 405 | 'max_chars' => $max_chars, |
| 392 | 406 | 'read_more_threshold' => 25, |
| 393 | 407 | ) |
| 394 | - ) | |
| 408 | + ), | |
| 409 | + $preserve_urls | |
| 395 | 410 | ); |
| 396 | 411 | } elseif ( $requested_post instanceof WP_Post ) { |
| 397 | 412 | // @todo Refactor to not need to override the global. |
| 398 | 413 | // phpcs:ignore: WordPress.WP.GlobalVariablesOverride.Prohibited |
| @@ -402,9 +417,9 @@ | ||
| 402 | 417 | $post_excerpt = apply_filters( 'get_the_excerpt', $post_excerpt, $post ); |
| 403 | 418 | // phpcs:ignore: WordPress.WP.GlobalVariablesOverride.Prohibited |
| 404 | 419 | $post = $original_post; // wp_reset_postdata uses the $post global. |
| 405 | 420 | wp_reset_postdata(); |
| 406 | - return self::clean_text( $post_excerpt ); | |
| 421 | + return self::clean_text( $post_excerpt, $preserve_urls ); | |
| 407 | 422 | } |
| 408 | 423 | return ''; |
| 409 | 424 | } |
| 410 | 425 | |
| @@ -429,9 +444,9 @@ | ||
| 429 | 444 | * |
| 430 | 445 | * @return int Word count. |
| 431 | 446 | */ |
| 432 | 447 | public static function get_word_count( $post_content ) { |
| 433 | - return (int) count( self::split_content_in_words( self::clean_text( $post_content ) ) ); | |
| 448 | + return count( self::split_content_in_words( self::clean_text( $post_content ) ) ); | |
| 434 | 449 | } |
| 435 | 450 | |
| 436 | 451 | /** |
| 437 | 452 | * Get remainder word count (after the excerpt). |
| @@ -444,9 +459,9 @@ | ||
| 444 | 459 | public static function get_word_remaining_count( $post_content, $excerpt_content ) { |
| 445 | 460 | $content_word_count = count( self::split_content_in_words( self::clean_text( $post_content ) ) ); |
| 446 | 461 | $excerpt_word_count = count( self::split_content_in_words( self::clean_text( $excerpt_content ) ) ); |
| 447 | 462 | |
| 448 | - return (int) $content_word_count - $excerpt_word_count; | |
| 463 | + return $content_word_count - $excerpt_word_count; | |
| 449 | 464 | } |
| 450 | 465 | |
| 451 | 466 | /** |
| 452 | 467 | * Counts the number of links in a post. |