Created
September 12, 2016 22:31
-
-
Save BinaryMoon/2477db9c67c1f50b23557704f4fbc117 to your computer and use it in GitHub Desktop.
Fixes to excerpt preview in blog-display.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The functions to display Content or Excerpt in a theme. | |
*/ | |
/** | |
* If the theme doesn't support 'jetpack-content-options', don't continue. | |
*/ | |
if ( ! current_theme_supports( 'jetpack-content-options' ) ) { | |
return; | |
} | |
/** | |
* Get the Blog Display setting. | |
* If theme is using both 'Content' and 'Excerpt' then this setting will be called 'Mixed'. | |
*/ | |
$options = get_theme_support( 'jetpack-content-options' ); | |
$blog_display = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null; | |
$blog_display = preg_grep( '/^(content|excerpt)$/', (array) $blog_display ); | |
sort( $blog_display ); | |
$blog_display = implode( ', ', $blog_display ); | |
$blog_display = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display; | |
/** | |
* If the theme doesn't support 'jetpack-content-options[ 'blog-display' ]', don't continue. | |
*/ | |
if ( ! in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ) ) ) { | |
return; | |
} | |
/** | |
* Apply Content filters. | |
*/ | |
function jetpack_blog_display_custom_excerpt() { | |
$post = get_post(); | |
if ( empty( $post->post_excerpt ) ) { | |
$text = strip_shortcodes( $post->post_content ); | |
$text = str_replace( ']]>', ']]>', $text ); | |
$text = strip_tags( $text ); | |
$excerpt_length = apply_filters( 'excerpt_length', 55 ); | |
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[...]' ); | |
$words = preg_split( "/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY ); | |
if ( count( $words ) > $excerpt_length ) { | |
array_pop( $words ); | |
$text = implode( ' ', $words ); | |
$text = $text . $excerpt_more; | |
} else { | |
$text = implode( ' ', $words ); | |
} | |
} else { | |
$text = wp_kses_post( $post->post_excerpt ); | |
} | |
return sprintf( '<p>%s</p>', $text ); | |
} | |
/** | |
* Display Excerpt instead of Content. | |
*/ | |
function jetpack_the_content_to_the_excerpt( $content ) { | |
if ( is_home() || is_archive() ) { | |
if ( post_password_required() ) { | |
$content = sprintf( '<p>%s</p>', esc_html__( 'There is no excerpt because this is a protected post.' ) ); | |
} else if ( is_home() || is_archive() ) { | |
$content = jetpack_blog_display_custom_excerpt(); | |
} | |
} | |
return $content; | |
} | |
/** | |
* Display Content instead of Excerpt. | |
*/ | |
function jetpack_the_excerpt_to_the_content( $content ) { | |
if ( is_home() || is_archive() ) { | |
ob_start(); | |
the_content( sprintf( | |
/* translators: %s: Name of current post. */ | |
wp_kses( __( 'Continue reading %s <span class="meta-nav">→</span>', 'jetpack' ), array( 'span' => array( 'class' => array() ) ) ), | |
the_title( '<span class="screen-reader-text">"', '"</span>', false ) | |
) ); | |
$content = ob_get_clean(); | |
} | |
return $content; | |
} | |
/** | |
* Display both Content and Excerpt instead of Content in the Customizer so live preview can switch between them. | |
*/ | |
function jetpack_the_content_customizer( $content ) { | |
$class = jetpack_the_content_customizer_class(); | |
if ( post_password_required() ) { | |
$text = sprintf( '<p>%s</p>', esc_html__( 'There is no excerpt because this is a protected post.' ) ); | |
} else if ( is_home() || is_archive() ) { | |
$text = jetpack_blog_display_custom_excerpt(); | |
} | |
if ( empty( $text ) ) { | |
return $content; | |
} else { | |
return sprintf( '<div class="jetpack-blog-display %s the-content">%s</div><div class="jetpack-blog-display %s the-excerpt">%s</div>', $class, $content, $class, $text ); | |
} | |
} | |
/** | |
* Display both Content and Excerpt instead of Excerpt in the Customizer so live preview can switch between them. | |
*/ | |
function jetpack_the_excerpt_customizer( $excerpt ) { | |
if ( post_password_required() ) { | |
$content = sprintf( '<p>%s</p>', esc_html__( 'There is no excerpt because this is a protected post.' ) ); | |
} else if ( is_home() || is_archive() ) { | |
$content = jetpack_the_excerpt_to_the_content( $excerpt ); | |
} | |
if ( empty( $content ) ) { | |
return $excerpt; | |
} else { | |
return sprintf( '<div class="jetpack-blog-display the-excerpt">%s</div><div class="jetpack-blog-display the-content">%s</div>', $excerpt, $content ); | |
} | |
} | |
/** | |
* Display Content instead of Excerpt in the Customizer when theme uses a 'Mixed' display. | |
*/ | |
function jetpack_the_excerpt_mixed_customizer( $content ) { | |
if ( is_home() || is_archive() ) { | |
jetpack_the_content_customizer_class( 'output-the-excerpt' ); | |
ob_start(); | |
the_content(); | |
$content = ob_get_clean(); | |
} | |
return $content; | |
} | |
/** | |
* Returns a class value, `output-the-content` by default. | |
* Used for themes with a 'Mixed' Blog Display so we can tell which output is by default. | |
*/ | |
function jetpack_the_content_customizer_class( $new_class = null ) { | |
static $class; | |
if ( isset( $new_class ) ) { | |
// Assign a new class and return. | |
$class = $new_class; | |
} else if ( isset( $class ) ) { | |
// Reset the class after getting value. | |
$prev_class = $class; | |
$class = null; | |
return $prev_class; | |
} else { | |
// Return default class value. | |
return 'output-the-content'; | |
} | |
} | |
if ( is_customize_preview() ) { | |
/* | |
* Display Content and Excerpt if the default Blog Display is 'Content' | |
* and we are in the Customizer. | |
*/ | |
if ( 'content' === $blog_display ) { | |
add_filter( 'the_content', 'jetpack_the_content_customizer' ); | |
} | |
/* | |
* Display Content and Excerpt if the default Blog Display is 'Excerpt' | |
* and we are in the Customizer. | |
*/ | |
if ( 'excerpt' === $blog_display ) { | |
add_filter( 'the_excerpt', 'jetpack_the_excerpt_customizer' ); | |
} | |
/* | |
* Display Content and Excerpt if the default Blog Display is 'Mixed' | |
* and we are in the Customizer. | |
*/ | |
if ( 'mixed' === $blog_display ) { | |
add_filter( 'the_content', 'jetpack_the_content_customizer' ); | |
add_filter( 'the_excerpt', 'jetpack_the_excerpt_mixed_customizer' ); | |
} | |
} else { | |
$display_option = get_option( 'jetpack_content_blog_display', $blog_display ); | |
/* | |
* Display Excerpt if the default Blog Display is 'Content' | |
* or default Blog Display is 'Mixed' | |
* and the Option picked is 'Post Excerpt' | |
* and we aren't in the Customizer. | |
*/ | |
if ( ( 'content' === $blog_display || 'mixed' === $blog_display ) && 'excerpt' === $display_option ) { | |
add_filter( 'the_content', 'jetpack_the_content_to_the_excerpt' ); | |
} | |
/* | |
* Display Content if the default Blog Display is 'Excerpt' | |
* or default Blog Display is 'Mixed' | |
* and the Option picked is 'Full Post' | |
* and we aren't in the Customizer. | |
*/ | |
if ( ( 'excerpt' === $blog_display || 'mixed' === $blog_display ) && 'content' === $display_option ) { | |
add_filter( 'the_excerpt', 'jetpack_the_excerpt_to_the_content' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment