cloudflare / Cloudflare-WordPress

A Cloudflare plugin for WordPress
https://www.cloudflare.com/wordpress/
BSD 3-Clause "New" or "Revised" License
215 stars 84 forks source link

No way to purge specific URL #521

Open ivptr opened 1 year ago

ivptr commented 1 year ago

Confirmation

WordPress version

6.2.2

Cloudflare-WordPress version

4.12.2

PHP version

8.0

Expected result

purgeCacheByRelevantURLs should accept URLs, also.

Actual result

purgeCacheByRelevantURLs function accepts post IDs only.

Steps to reproduce

-

Additional factoids

No response

References

No response

github-actions[bot] commented 8 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

ivptr commented 8 months ago

Still no changes in version 4.12.4.

robwent commented 4 months ago

I'm also looking for this and it's related to #160

We have some content on our site which comes from an API. There is a registered post type, but it's empty and any requests for posts of that type go through the API for the content. I was looking through the Hooks class hoping it was possible to pass a URL to some method to clear a page by URL instead of the ID but nothing currently exists.

Possible new method for Hooks:

public function purgeCacheByUrl($url)
{
    $wpDomainList = $this->integrationAPI->getDomainList();
    if (!count($wpDomainList)) {
        return;
    }
    $wpDomain = $wpDomainList[0];
    $zoneTag = $this->api->getZoneTag($wpDomain);
    if (!isset($zoneTag)) {
        return;
    }

    // Don't attempt to purge anything outside of the provided zone.
    if (!Utils::strEndsWith(parse_url($url, PHP_URL_HOST), $wpDomain)) {
        return;
    }

    $isOK = $this->api->zonePurgeFiles($zoneTag, array($url));

    $isOK = ($isOK) ? 'succeeded' : 'failed';
    $this->logger->debug("purgeCacheByUrl " . $isOK);
}

Would this be accepted as a pull request?

rakesh-newfold commented 2 months ago

Addition to you code we should add this filter also in cloudflare.loader.php after line number 104

$cloudflarePurgeIndividualURLActions = apply_filters('cloudflare_purge_individual_url_actions', $cloudflarePurgeIndividualURLActions);

foreach ($cloudflarePurgeIndividualURLActions as $action) {
    add_action($action, array($cloudflareHooks, 'purgeCacheByUrl'), PHP_INT_MAX);
}
robwent commented 2 months ago

If anyone is looking for a quick fix, I ended up adding the creds as constants in the wp-config.php file. This is the relevant part of the function that clears various caches when passed a URL:

    // Check if the cloudflare constants are defined.
    if ( defined( 'CLOUDFLARE_ZONE_ID' ) && defined( 'CLOUDFLARE_API_KEY' ) && defined( 'CLOUDFLARE_EMAIL' ) ) {
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/zones/' . CLOUDFLARE_ZONE_ID . '/purge_cache' );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'DELETE' );
        $headers   = array();
        $headers[] = 'X-Auth-Email: ' . CLOUDFLARE_EMAIL;
        $headers[] = 'X-Auth-Key: ' . CLOUDFLARE_API_KEY;
        $headers[] = 'Content-Type: application/json';
        curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
        $data = '{"files":["' . $url_to_flush . '"]}';
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );

        $result = curl_exec( $ch );
        if ( curl_errno( $ch ) ) {
            $response .= 'Error clearing Cloudflare. ';
        }
        $responseObj = json_decode( $result );
        if ( isset( $responseObj->success ) && $responseObj->success ) {
            $response .= 'Cloudflare cleared. ';
        }
        curl_close( $ch );
    }