Skip to content

Commit

Permalink
Fixes an issue where the_advanced_excerpt() wasn't working on singula…
Browse files Browse the repository at this point in the history
…r page types
  • Loading branch information
aprea committed May 29, 2014
1 parent 064ae88 commit d0229a7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
32 changes: 20 additions & 12 deletions class/advanced-excerpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ function __construct( $plugin_file_path ) {
}

function hook_content_filters() {
// Excerpt filtering should always occur on the 'get_the_excerpt' hook, regardless of page type
if ( 1 == $this->options['the_excerpt'] ) {
remove_all_filters( 'get_the_excerpt' );
add_filter( 'get_the_excerpt', array( $this, 'filter' ) );
}

/*
* Allow developers to skip running the advanced excerpt filters on certain page types.
* They can do so by using the "Disable On" checkboxes on the options page or
Expand All @@ -79,21 +85,16 @@ function hook_content_filters() {
* The filter, when implemented, takes precedence over the options page selection.
*
* WordPress default themes (and others) do not use the_excerpt() or get_the_excerpt()
* instead they use the_content(). As such, we also need to hook into the_content().
* To ensure we're not changing the content of posts / pages we first check if is_singular().
* and instead use the_content(). As such, we also need to hook into the_content().
* To ensure we're not changing the content of single posts / pages we automatically exclude 'singular' page types.
*/
$page_types = $this->get_current_page_types();
$skip_page_types = array_unique( array_merge( array( 'singular' ), $this->options['exclude_pages'] ) );
$skip_page_types = apply_filters( 'advanced_excerpt_skip_page_types', $skip_page_types );
$page_type_matches = array_intersect( $page_types, $skip_page_types );
if ( !empty( $page_types ) && !empty( $page_type_matches ) ) return;

if( 1 == $this->options['the_excerpt'] ) {
remove_all_filters( 'get_the_excerpt' );
add_filter( 'get_the_excerpt', array( $this, 'filter' ) );
}

if( 1 == $this->options['the_content'] ) {
if ( 1 == $this->options['the_content'] ) {
add_filter( 'the_content', array( $this, 'filter' ) );
}
}
Expand Down Expand Up @@ -218,12 +219,19 @@ function filter( $text ) {
if ( 1 == $no_shortcode ) {
$text = strip_shortcodes( $text );
}
if( 1 == $this->options['the_content'] ) {
remove_filter( 'the_content', array( $this, 'filter' ) ); // prevent recursion

// prevent recursion on 'the_content' hook
$content_has_filter = false;
if ( has_filter( 'the_content', array( $this, 'filter' ) ) ) {
remove_filter( 'the_content', array( $this, 'filter' ) );
$content_has_filter = true;
}

$text = apply_filters( 'the_content', $text );
if( 1 == $this->options['the_content'] ) {
add_filter( 'the_content', array( $this, 'filter' ) ); // add our filter back in

// add our filter back in
if ( $content_has_filter ) {
add_filter( 'the_content', array( $this, 'filter' ) );
}

if ( $the_content_no_break && false !== strpos( $text, '<!--more-->' ) ) {
Expand Down
5 changes: 4 additions & 1 deletion functions/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ function the_advanced_excerpt( $args = '', $get = false ) {
}

// Set temporary options
$advanced_excerpt->options = wp_parse_args( $args, $advanced_excerpt->default_options );
$advanced_excerpt->options = wp_parse_args( $args, $advanced_excerpt->options );

if ( $get ) {
return get_the_excerpt();
} else {
the_excerpt();
}

// Reset the options back to their original state
$advanced_excerpt->load_options();
}

0 comments on commit d0229a7

Please sign in to comment.