Cloudflare-WordPress icon indicating copy to clipboard operation
Cloudflare-WordPress copied to clipboard

No way to purge specific URL

Open ivptr opened this issue 1 year ago • 5 comments

Confirmation

  • [X] My issue isn't already found on the issue tracker.
  • [X] I have replicated my issue using the latest version of the plugin and it is still present.

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

ivptr avatar Aug 10 '23 18:08 ivptr

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.

github-actions[bot] avatar Feb 07 '24 01:02 github-actions[bot]

Still no changes in version 4.12.4.

ivptr avatar Feb 07 '24 07:02 ivptr

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?

robwent avatar May 09 '24 19:05 robwent

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);
}

rakesh-newfold avatar Jul 10 '24 11:07 rakesh-newfold

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 );
	}

robwent avatar Jul 10 '24 13:07 robwent