| @@ -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 | |
| @@ -158,8 +169,11 @@ | ||
| 158 | 169 | ++$return['count']['video']; |
| 159 | 170 | break; |
| 160 | 171 | case 'vimeo': |
| 161 | 172 | if ( 0 === $return['count']['video'] ) { |
| 173 | + if ( ! isset( $extract['shortcode']['vimeo']['id'][0] ) ) { | |
| 174 | + break; | |
| 175 | + } | |
| 162 | 176 | $return['type'] = 'video'; |
| 163 | 177 | $return['video'] = esc_url_raw( 'http://vimeo.com/' . $extract['shortcode']['vimeo']['id'][0] ); |
| 164 | 178 | $return['secure']['video'] = self::https( $return['video'] ); |
| 165 | 179 | |
| @@ -224,9 +238,9 @@ | ||
| 224 | 238 | } |
| 225 | 239 | ++$number_of_paragraphs; |
| 226 | 240 | } |
| 227 | 241 | |
| 228 | - $number_of_paragraphs = $number_of_paragraphs - $return['count']['video']; // subtract amount for videos. | |
| 242 | + $number_of_paragraphs -= $return['count']['video']; // subtract amount for videos. | |
| 229 | 243 | |
| 230 | 244 | // More than 2 paragraph? The video is not the primary focus so we can do some more analysis. |
| 231 | 245 | if ( $number_of_paragraphs > 2 ) { |
| 232 | 246 | $return['type'] = 'standard'; |
| @@ -262,11 +276,14 @@ | ||
| 262 | 276 | } |
| 263 | 277 | ++$number_of_paragraphs; |
| 264 | 278 | } |
| 265 | 279 | |
| 266 | - $return['image'] = $extract['image'][0]['url']; | |
| 267 | - $return['secure']['image'] = self::ssl_img( $return['image'] ); | |
| 268 | - ++$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 | + } | |
| 269 | 286 | |
| 270 | 287 | if ( $number_of_paragraphs <= 2 && is_countable( $extract['image'] ) && 1 === count( $extract['image'] ) ) { |
| 271 | 288 | // If we have lots of text or images, let's not treat it as an image post, but return its first image. |
| 272 | 289 | $return['type'] = 'image'; |
| @@ -343,27 +360,20 @@ | ||
| 343 | 360 | /** |
| 344 | 361 | * Clean text of shortcodes and tags. |
| 345 | 362 | * |
| 346 | 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. | |
| 347 | 365 | * |
| 348 | 366 | * @return string Clean text. |
| 349 | 367 | */ |
| 350 | - public static function clean_text( $text ) { | |
| 351 | - return trim( | |
| 352 | - preg_replace( | |
| 353 | - '/[\s]+/', | |
| 354 | - ' ', | |
| 355 | - preg_replace( | |
| 356 | - '@https?://[\S]+@', | |
| 357 | - '', | |
| 358 | - strip_shortcodes( | |
| 359 | - wp_strip_all_tags( | |
| 360 | - $text | |
| 361 | - ) | |
| 362 | - ) | |
| 363 | - ) | |
| 364 | - ) | |
| 365 | - ); | |
| 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 ) ); | |
| 366 | 376 | } |
| 367 | 377 | |
| 368 | 378 | /** |
| 369 | 379 | * Retrieve an excerpt for the post summary. |
| @@ -377,11 +387,12 @@ | ||
| 377 | 387 | * @param string $post_excerpt The post's excerpt. Empty if none was explicitly set. |
| 378 | 388 | * @param int $max_words Maximum number of words for the excerpt. Used on wp.com. Default 16. |
| 379 | 389 | * @param int $max_chars Maximum characters in the excerpt. Used on wp.com. Default 256. |
| 380 | 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. | |
| 381 | 392 | * @return string Post excerpt. |
| 382 | 393 | **/ |
| 383 | - 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 ) { | |
| 384 | 395 | global $post; |
| 385 | 396 | $original_post = $post; // Saving the global for later use. |
| 386 | 397 | if ( empty( $post_excerpt ) && function_exists( 'wpcom_enhanced_excerpt_extract_excerpt' ) ) { |
| 387 | 398 | return self::clean_text( |
| @@ -393,9 +404,10 @@ | ||
| 393 | 404 | 'max_words' => $max_words, |
| 394 | 405 | 'max_chars' => $max_chars, |
| 395 | 406 | 'read_more_threshold' => 25, |
| 396 | 407 | ) |
| 397 | - ) | |
| 408 | + ), | |
| 409 | + $preserve_urls | |
| 398 | 410 | ); |
| 399 | 411 | } elseif ( $requested_post instanceof WP_Post ) { |
| 400 | 412 | // @todo Refactor to not need to override the global. |
| 401 | 413 | // phpcs:ignore: WordPress.WP.GlobalVariablesOverride.Prohibited |
| @@ -405,9 +417,9 @@ | ||
| 405 | 417 | $post_excerpt = apply_filters( 'get_the_excerpt', $post_excerpt, $post ); |
| 406 | 418 | // phpcs:ignore: WordPress.WP.GlobalVariablesOverride.Prohibited |
| 407 | 419 | $post = $original_post; // wp_reset_postdata uses the $post global. |
| 408 | 420 | wp_reset_postdata(); |
| 409 | - return self::clean_text( $post_excerpt ); | |
| 421 | + return self::clean_text( $post_excerpt, $preserve_urls ); | |
| 410 | 422 | } |
| 411 | 423 | return ''; |
| 412 | 424 | } |
| 413 | 425 | |
| @@ -432,9 +444,9 @@ | ||
| 432 | 444 | * |
| 433 | 445 | * @return int Word count. |
| 434 | 446 | */ |
| 435 | 447 | public static function get_word_count( $post_content ) { |
| 436 | - 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 ) ) ); | |
| 437 | 449 | } |
| 438 | 450 | |
| 439 | 451 | /** |
| 440 | 452 | * Get remainder word count (after the excerpt). |
| @@ -447,9 +459,9 @@ | ||
| 447 | 459 | public static function get_word_remaining_count( $post_content, $excerpt_content ) { |
| 448 | 460 | $content_word_count = count( self::split_content_in_words( self::clean_text( $post_content ) ) ); |
| 449 | 461 | $excerpt_word_count = count( self::split_content_in_words( self::clean_text( $excerpt_content ) ) ); |
| 450 | 462 | |
| 451 | - return (int) $content_word_count - $excerpt_word_count; | |
| 463 | + return $content_word_count - $excerpt_word_count; | |
| 452 | 464 | } |
| 453 | 465 | |
| 454 | 466 | /** |
| 455 | 467 | * Counts the number of links in a post. |