1

I am looking for a solution and have no idea how to start.

All my static joomla HTML sites are cached by Cloudflare. After updating an article on joomla, I update the Cloudflare cache manual with a API request.

So I am looking for a solution to send the API request automatic after I press the save button on joomla.

The 'real' URL is already available in a custom field by the article.

  • You can create a custom plugin and use the onContentAfterSave event. – Lodder Jan 8 at 18:48
  • @Lodder Many thanks, that I am looking for. – Lovntola Jan 8 at 18:58
  • @Lovntola if Lodder's hint is enough for you to self-solve, please post your developed snippet (without sensitive api key /data) and include some explanation. This allows future researchers to benefit from your experience. Please progress this page to a system recognized resolution. ...if you cannot self-solve, please update your question with where you are stuck. – mickmackusa Jan 8 at 23:22
  • 1
    @mickmackusa I will post the code if it work, need time it will my first plugin. – Lovntola Jan 9 at 11:22
  • @mickmackusa: DONE – Lovntola Jan 10 at 20:16
3

As far as I can remember since I haven't tested this recently, you don't need to clear CloudFlare's cache in order to display update's of article's with CloudFlare. CloudFlare will only cache static assets like css, js. In any case, there are ready made plugins that do the job. One I can recommend is the RegularLabs Cache Cleaner: https://www.regularlabs.com/extensions/cachecleaner/features

The Pro version of the plugin supports Purging CloudFlare Cache automatically while saving content in Joomla backend.

  • I haved setup Cloudflare to store all my HTML Files too. And exclude dynamtic HTML sites via Page-Rules, or load dyn content via JSON, etc – Lovntola Jan 10 at 20:16
  • Ah okay then @Lovntola. I see you posted also your own plugin solution. Haven't reviewed the code - but in any case well-done for getting into it. – FFrewin Jan 10 at 23:59
2

Here is my first plugin:

<?php
/**
 * Plugin class for update the Cloudflare Cache after save a article
 */

class plgContentBk_onsave extends JPlugin
{

    public function onContentAfterSave ($context, $article, $isNew)
    {
    // I have saved the URL in a custom field by the article to protect against duplicate content. 
    // I use this field for <link rel="canonical" href="..."> too.
    // so I get the URl from the article:
    $context = 'com_content.article';
    $article = JModelLegacy::getInstance('Article', 'ContentModel')->getItem(); 
    JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); // Helper to get the field by the article
    $fields = FieldsHelper::getFields($context, $article);
    $fields = json_decode(json_encode($fields), true); // convert to a array
    $url = $fields[0]["value"]; // Get the URL from the Field $fields[0]["value"] the custom field in the article
    // if you want to see all your custom fields : var_dump($fields)

    // Okay now the part that delete the Cloudflare cache:

    $authKey = "XXX";
    $authEmail = "XXX";
    $zoneId = "XXX";
    $endpoint = "purge_cache";
    $data = '{"files":[
    "'.$url.'"
    ]}';
    $url = "https://api.cloudflare.com/client/v4/zones/{$zoneId}/{$endpoint}";
    $opts = ['http' => [
    'method' => 'DELETE',
    'header' => [
        "Content-Type: application/json",
        "X-Auth-Key: {$authKey}",
        "X-Auth-Email: {$authEmail}",
    ],
    'content' => $data,
    ]];

    $context = stream_context_create($opts);
    $result = file_get_contents($url, false, $context);
    // to sse the Cloudflare result use: var_dump($result);
    // add exit; to see all otputs like var_dumps
    return true;    
    }
}
 ?>

Tested and works fine. After save the article the Cloudflare CDN for the one url is purged.
Any improvements are welcome.
Thanks to all here for your support to solve my problem.

I have added to see the succes after save:

ob_start();
var_dump($result);
$ext = ob_get_clean();
// to see the Cloudflare result use: var_dump($result);
// add exit; to see all otputs like var_dumps
$application = JFactory::getApplication();
$message="Cloudflare Update von: ".$fields[0]["value"]."<br>".$ext; $type="notice";
$application->enqueueMessage(JText::_($message), 'notice');

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.