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

No way to purge specific URL #521

Open
2 tasks done
ivptr opened this issue Aug 10, 2023 · 5 comments
Open
2 tasks done

No way to purge specific URL #521

ivptr opened this issue Aug 10, 2023 · 5 comments

Comments

@ivptr
Copy link

ivptr commented Aug 10, 2023

Confirmation

  • My issue isn't already found on the issue tracker.
  • 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

Copy link

github-actions bot commented Feb 7, 2024

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 github-actions bot added the Stale label Feb 7, 2024
@ivptr
Copy link
Author

ivptr commented Feb 7, 2024

Still no changes in version 4.12.4.

@github-actions github-actions bot removed the Stale label Feb 8, 2024
@robwent
Copy link

robwent commented May 9, 2024

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
Copy link

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
Copy link

robwent commented Jul 10, 2024

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

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

No branches or pull requests

3 participants