Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRI - the_excerpt within the_content #153

Closed
wants to merge 2 commits into from

Conversation

willstocks
Copy link

the_content filter is potentially applied to related posts as well. The related posts section uses the_excerpt() and the_content filter gets applied when there is no custom excerpt is set for the post.

Let's say that I show 3 related posts, 2 x has Enlighter block and 1 x doesn't doesn't have Enlighter block, then variable $T->_hasContent of the plugin is set to false and the plugin doesn't add the necessary resources, because that one related post doesn't have enlighter within it.

This code checks if the current content being processed is excerpt. If it's excerpt (related posts), then just return the content. If not, then do the actual fun stuff.

The reason why it was working on twentynineteen is that there wasn't any other post using the_excerpt() function on the theme. E.g. related posts

`the_content` filter is potentially applied to related posts as well. The related posts section uses `the_excerpt()` and `the_content` filter gets applied when there is no custom excerpt is set for the post.

Let's say that I show 3 related posts, 2 x has Enlighter block and 1 x doesn't doesn't have Enlighter block, then variable `$T->_hasContent` of the plugin is set to `false` and the plugin doesn't add the necessary resources, because that one related post doesn't have enlighter within it.

This code checks if the current content being processed is excerpt. If it's excerpt (related posts), then just return the content. If not, then do the actual fun stuff.

The reason why it was working on twentynineteen is that there wasn't any other post using `the_excerpt()` function on the theme. E.g. related posts
Added `{}` and removed whitespacing and add a comment to explain what is being done
@AndiDittrich
Copy link
Member

AndiDittrich commented Feb 27, 2019

the problem is the use of $GLOBALS ..

i'll check it soon but i'm not sure what the code should do....it seems that the_content/the_excerpt are not used in the intended way...

@willstocks
Copy link
Author

Full excerpt from the guys that support my theme:

Did some tests and found out that the_content filter is getting applied to the related posts section as well. The related posts section uses the_excerpt() and the_content filter gets applied when there is no custom excerpt is set for the post.

Let's say that "Golden Gate Bridge" post has Enlighter block, but the_content filter also gets applied on those 2 related posts as well and if the last one "Getting My Van Ready" doesn't have Enlighter block, then variable $T->_hasContent of the plugin is set to false and the plugin doesn't add the necessary resources.

So the solution part is here. To make the_content filter to get applied only for the current post, I added this code to the plugin's "ContentProcessor.php" file (line 148)

if (in_array('get_the_excerpt', $GLOBALS['wp_current_filter'])) return $content;

This code checks if the current content being processed is excerpt. If it's excerpt (related posts), then just return the content. If not, then do the actual fun stuff.

The reason why it was working on twentynineteen is that there wasn't any other post using the_excerpt() function on the theme. E.g. related posts.

@AndiDittrich
Copy link
Member

ah ok, now i'm understanding whats happening: the the_content filter is called multiple times by the theme and the hasContent flag got overwritten by each call.

in this case we need a different solution: a singleton flag which can only set 1-times to true

the disadvantage of the solution would be that EnlighterJS resources may added to pages where no Enlighter codeblocks appear (because they exists in related post on the page).

@willstocks
Copy link
Author

Ahhhh, yes I see what you're saying too! Yes, a single flag would be most ideal 😄

@AndiDittrich
Copy link
Member

that would be my prefered version:

// EnlighterJS Code detection
add_filter('the_content', function($content) use ($T){
    // block overrides caused by multiple calls to the_content filter
    // is the filter called regular to display the content ? 
    if (!$T->_hasContent && in_the_loop() && is_main_query()){
        // contains enlighterjs codeblocks ?                   
        $T->_hasContent =  (strpos($content, 'EnlighterJSRAW') !== false);
    }

    return $content;
}, 9999, 1);

@AndiDittrich
Copy link
Member

would you like to try it ? :)

@willstocks
Copy link
Author

Doing it now 😉

@willstocks
Copy link
Author

That seems to be working very well!!! :)

One quick side question - why is mootools from jsdelivr a minor version behind all others? I'm currently enqueue-ing it manually (https://cdn.jsdelivr.net/gh/mootools/[email protected]/dist/mootools-core.min.js) and it's working OK. I'll switch back to using the plugin enqueued version so that it's only enqueued when necessary, but just wondered why it's v1.5.0 not v1.6.0 like all the other CDN's?

self::$cdnLocations['mootools-google'] = '//ajax.googleapis.com/ajax/libs/mootools/1.6.0/mootools.min.js';
self::$cdnLocations['mootools-cdnjs'] = '//cdnjs.cloudflare.com/ajax/libs/mootools/1.6.0/mootools-core.min.js';
self::$cdnLocations['mootools-jsdelivr'] = '//cdn.jsdelivr.net/mootools/1.5.0/mootools-core-nocompat.min.js';

AndiDittrich added a commit that referenced this pull request Feb 27, 2019
@AndiDittrich
Copy link
Member

because jsdelivr is not always up to date....as i updated to 1.6.0 this version was not available yet!
i'll change this - thx for that hint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants