| @@ -194,49 +194,28 @@ | ||
| 194 | 194 | ); |
| 195 | 195 | |
| 196 | 196 | |
| 197 | 197 | /* Get the excerpt */ |
| 198 | - | |
| 199 | - $post_id = get_the_ID(); | |
| 200 | - | |
| 201 | - $excerpt = apply_filters('the_excerpt', | |
| 202 | - get_post_field( | |
| 203 | - 'post_excerpt', | |
| 204 | - $post_id, | |
| 205 | - 'display' | |
| 206 | - ) | |
| 207 | - ); | |
| 208 | - | |
| 209 | - if(empty($excerpt) && isset($attributes['excerptLength'])) { | |
| 210 | - // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound, PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket -- Running the_excerpt directly, Previous rule doesn't take without the_excerpt being moved up a line | |
| 211 | - $excerpt = apply_filters('the_excerpt', | |
| 212 | - wp_trim_words( | |
| 213 | - preg_replace( | |
| 214 | - array( | |
| 215 | - '/\<figcaption>.*\<\/figcaption>/', | |
| 216 | - '/\[caption.*\[\/caption\]/', | |
| 217 | - ), | |
| 218 | - '', | |
| 219 | - get_the_content() | |
| 220 | - ), | |
| 221 | - $attributes['excerptLength'] | |
| 222 | - ) | |
| 223 | - ); | |
| 198 | + | |
| 199 | + global $post; | |
| 200 | + | |
| 201 | + | |
| 202 | + $excerpt = blockspare_post_excerpt($post_id, $post); | |
| 203 | + | |
| 204 | + if (!$excerpt) { | |
| 205 | + $excerpt = null; | |
| 224 | 206 | } |
| 225 | - | |
| 226 | - // Trim the excerpt if necessary. | |
| 227 | - if ( isset( $attributes['excerptLength'] ) ) { | |
| 228 | - $excerpt = wp_trim_words( | |
| 229 | - $excerpt, | |
| 230 | - $attributes['excerptLength'] | |
| 231 | - ); | |
| 207 | + | |
| 208 | + if (!empty($excerpt)) { | |
| 209 | + $excerpt = explode(' ', $excerpt); | |
| 210 | + $trim_to_length = (int)$attributes['excerptLength'] ? (int)$attributes['excerptLength'] : 55; | |
| 211 | + if (count($excerpt) > $trim_to_length) { | |
| 212 | + $excerpt = implode(' ', array_slice($excerpt, 0, $trim_to_length)) . '...'; | |
| 213 | + } else { | |
| 214 | + $excerpt = implode(' ', $excerpt); | |
| 215 | + } | |
| 216 | + | |
| 232 | 217 | } |
| 233 | - | |
| 234 | - $excerpt = apply_filters( 'the_excerpt', $excerpt ); | |
| 235 | - | |
| 236 | - if(!$excerpt) { | |
| 237 | - $excerpt = null; | |
| 238 | - } | |
| 239 | 218 | |
| 240 | 219 | |
| 241 | 220 | if ((isset($attributes['displayPostExcerpt']) && $excerpt != null) || isset($attributes['displayPostLink'])) { |
| 242 | 221 | |
| @@ -249,9 +228,9 @@ | ||
| 249 | 228 | ); |
| 250 | 229 | |
| 251 | 230 | if (isset($attributes['displayPostExcerpt']) && $attributes['displayPostExcerpt']) { |
| 252 | 231 | $blockspare_content_markup .= sprintf( |
| 253 | - '<div class="blockspare-block-post-grid-excerpt-content">%1$s</div>', | |
| 232 | + '<div class="blockspare-block-post-grid-excerpt-content">%s</div>', | |
| 254 | 233 | wp_kses_post($excerpt) |
| 255 | 234 | ); |
| 256 | 235 | } |
| 257 | 236 | |
| @@ -341,12 +320,8 @@ | ||
| 341 | 320 | $block_content .= ' .' . $blockuniqueclass . ' .blockspare-post-category{ |
| 342 | 321 | color:' . $attributes['linkColor'] . '; |
| 343 | 322 | }'; |
| 344 | 323 | |
| 345 | - $block_content .= ' .' . $blockuniqueclass . ' a.blockspare-text-link.blockspare-block-post-grid-more-link{ | |
| 346 | - color:' . $attributes['linkColor'] . '; | |
| 347 | - }'; | |
| 348 | - | |
| 349 | 324 | $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-author a span{ |
| 350 | 325 | color:' . $attributes['linkColor'] . '; |
| 351 | 326 | }'; |
| 352 | 327 | |
| @@ -439,8 +414,43 @@ | ||
| 439 | 414 | } |
| 440 | 415 | |
| 441 | 416 | add_action('init', 'blockspare_blocks_register_block_core_latest_posts_grid'); |
| 442 | 417 | |
| 418 | + | |
| 419 | +if (!function_exists('blockspare_post_excerpt')) { | |
| 420 | + /** | |
| 421 | + * Get the post excerpt. | |
| 422 | + * | |
| 423 | + * @since 1.7 | |
| 424 | + */ | |
| 425 | + function blockspare_post_excerpt($object) | |
| 426 | + { | |
| 427 | + return blockspare_get_excerpt($object['id']); | |
| 428 | + } | |
| 429 | +} | |
| 430 | + | |
| 431 | +if (!function_exists('blockspare_get_excerpt')) { | |
| 432 | + /** | |
| 433 | + * Get the excerpt. | |
| 434 | + * | |
| 435 | + * @since 1.7 | |
| 436 | + */ | |
| 437 | + function blockspare_get_excerpt($post_id, $post = null) | |
| 438 | + { | |
| 439 | + $excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id, 'display')); | |
| 440 | + if (!empty($excerpt)) { | |
| 441 | + return $excerpt; | |
| 442 | + } | |
| 443 | + | |
| 444 | + $max_excerpt = 100; // WP default is 55. | |
| 445 | + | |
| 446 | + if (!empty($post['post_content'])) { | |
| 447 | + return apply_filters('the_excerpt', wp_trim_words($post['post_content'], $max_excerpt)); | |
| 448 | + } | |
| 449 | + $post_content = apply_filters('the_content', get_post_field('post_content', $post_id)); | |
| 450 | + return apply_filters('the_excerpt', wp_trim_words($post_content, $max_excerpt)); | |
| 451 | + } | |
| 452 | +} | |
| 443 | 453 | |
| 444 | 454 | |
| 445 | 455 | |
| 446 | 456 | |