sal0max / grav-plugin-shortcode-gallery-plusplus

A Shortcode extension to add sweet galleries with a lightbox to your Grav website.
MIT License
33 stars 8 forks source link

Basic solution to disable thumbnails on RSS, Atom and JSON feeds from the Feed plugin #25

Closed ghost closed 2 years ago

ghost commented 2 years ago

Good evening, Well then, fasten your seatbelts, it will be complicated. In order not to do bad English, I used DeepL. I think it speaks better English than me, and anyway, it's 11pm in France, and I'm too tired.

Anyway... Generating thumbnails was a good idea. However, when you use the Feed plugin to generate RSS and Atom feeds, the images appear as thumbnails in these feeds, because RSS aggregator apps don't execute Javascripts.

So you have to generate two versions of the page: One by having generated a gallery, and another by doing nothing at all (in the Shortcode handler, you return the HTML content as you received it, without transforming it).

We can detect when we are in the case of an RSS feed:

$type = $this->grav['uri']->extension();
$feed_config = $this->grav['config']->get('plugins.feed');
$feed_types = array('rss','atom');
if ($feed_config && $feed_config['enable_json_feed']) $feed_types[] = 'json';

if ( $feed_config && $feed_config['enabled'] &&       // if the Feed plugin is enabled
     isset($this->grav['page']->header()->content) && // and the current page has a collection
     $feed_types && in_array($type, $feed_types) ) {  // and the Feed plugin handles it
    // then we are sure to be in a RSS feed
}

In fact I was inspired by what the Feed plugin does. https://github.com/getgrav/grav-plugin-feed/blob/develop/feed.php

However, there are two problems:

That's why I listen to the same events as the Shortcode Core plugin (onPageContentRaw() and onPageContentProcessed()) in order to know which page we are processing. So I can check that the page has its cache_enable header set to 0.

So if a user wants to disable thumbnails in their feeds, they only have to disable the cache_enable option on each page where they have a gallery. Therefore, the AdvancedPageCache plugin is recommended, otherwise the HTML of the page will be regenerated at each execution. If you accept this PR, you might want to write this paragraph somewhere in your plugin documentation.

That's it, it was very annoying because the Grav API is not very well documented. Have a nice evening! Bisous!


PS: It's 1am, I solved all the problems and edited this post several times. Writing PHP while being tired is a very bad idea.

PPS: Very important: Grav is mono-threaded. If one day they decide to multi-thread it, the method it uses to retrieve the page being processed might not work anymore!

sal0max commented 2 years ago

Exceptional work, again! Thanks a lot for your effort!