Skip to content

Commit d9c9f5a

Browse files
author
Rafael Grigorian
committed
Fixed #49
1 parent 8c0a163 commit d9c9f5a

File tree

9 files changed

+419
-117
lines changed

9 files changed

+419
-117
lines changed
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,24 @@
55
use JetRails\Cloudflare\Controller\Adminhtml\Action;
66

77
/**
8-
* This controller has two endpoints. One is used to send an API call to
9-
* Cloudflare and it purges all the cache for the current domain. The other
10-
* endpoint asks the Cloudflare API to purge certain files from a zone.
8+
* This action is used to send an API call to Cloudflare and request to
9+
* purge cache for the current domain.
1110
* @version 1.2.6
1211
* @package JetRails® Cloudflare
1312
* @author Rafael Grigorian <[email protected]>
1413
* @copyright © 2018 JETRAILS, All rights reserved
1514
* @license MIT https://opensource.org/licenses/MIT
1615
*/
17-
class Individual extends Action {
16+
class Hostname extends Action {
1817

1918
/**
20-
* This action takes in a list of files from the 'files' parameter and
21-
* it asks the Cloudflare API to purge the cache related to said files.
22-
* @return void
19+
* This action simply triggers the Cloudflare API to purge cache related
20+
* to the list of items that are passed.
21+
* @return void
2322
*/
2423
public function execute () {
25-
$files = $this->_request->getParam ("files");
26-
$response = $this->_api->purgeIndividual ( $files );
24+
$items = $this->_request->getParam ("items");
25+
$response = $this->_api->purgeHosts ( $items );
2726
if ( isset ( $response->success ) && $response->success ) {
2827
$response->messages = array_merge (
2928
array (
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace JetRails\Cloudflare\Controller\Adminhtml\Api\Caching\PurgeCache;
4+
5+
use JetRails\Cloudflare\Controller\Adminhtml\Action;
6+
7+
/**
8+
* This action is used to send an API call to Cloudflare and request to
9+
* purge cache for the current domain.
10+
* @version 1.2.6
11+
* @package JetRails® Cloudflare
12+
* @author Rafael Grigorian <[email protected]>
13+
* @copyright © 2018 JETRAILS, All rights reserved
14+
* @license MIT https://opensource.org/licenses/MIT
15+
*/
16+
class Prefix extends Action {
17+
18+
/**
19+
* This action simply triggers the Cloudflare API to purge cache related
20+
* to the list of items that are passed.
21+
* @return void
22+
*/
23+
public function execute () {
24+
$items = $this->_request->getParam ("items");
25+
$response = $this->_api->purgePrefixes ( $items );
26+
if ( isset ( $response->success ) && $response->success ) {
27+
$response->messages = array_merge (
28+
array (
29+
"Successfully purged assets. Please allow up to 30 " .
30+
"seconds for changes to take effect."
31+
),
32+
$response->messages
33+
);
34+
}
35+
return $this->_sendResponse ( $response );
36+
}
37+
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace JetRails\Cloudflare\Controller\Adminhtml\Api\Caching\PurgeCache;
4+
5+
use JetRails\Cloudflare\Controller\Adminhtml\Action;
6+
7+
/**
8+
* This action is used to send an API call to Cloudflare and request to
9+
* purge cache for the current domain.
10+
* @version 1.2.6
11+
* @package JetRails® Cloudflare
12+
* @author Rafael Grigorian <[email protected]>
13+
* @copyright © 2018 JETRAILS, All rights reserved
14+
* @license MIT https://opensource.org/licenses/MIT
15+
*/
16+
class Tag extends Action {
17+
18+
/**
19+
* This action simply triggers the Cloudflare API to purge cache related
20+
* to the list of items that are passed.
21+
* @return void
22+
*/
23+
public function execute () {
24+
$items = $this->_request->getParam ("items");
25+
$response = $this->_api->purgeTags ( $items );
26+
if ( isset ( $response->success ) && $response->success ) {
27+
$response->messages = array_merge (
28+
array (
29+
"Successfully purged assets. Please allow up to 30 " .
30+
"seconds for changes to take effect."
31+
),
32+
$response->messages
33+
);
34+
}
35+
return $this->_sendResponse ( $response );
36+
}
37+
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace JetRails\Cloudflare\Controller\Adminhtml\Api\Caching\PurgeCache;
4+
5+
use JetRails\Cloudflare\Controller\Adminhtml\Action;
6+
7+
/**
8+
* This action is used to send an API call to Cloudflare and request to
9+
* purge cache for the current domain.
10+
* @version 1.2.6
11+
* @package JetRails® Cloudflare
12+
* @author Rafael Grigorian <[email protected]>
13+
* @copyright © 2018 JETRAILS, All rights reserved
14+
* @license MIT https://opensource.org/licenses/MIT
15+
*/
16+
class Url extends Action {
17+
18+
/**
19+
* This action simply triggers the Cloudflare API to purge cache related
20+
* to the list of items that are passed.
21+
* @return void
22+
*/
23+
public function execute () {
24+
$items = $this->_request->getParam ("items");
25+
$response = $this->_api->purgeUrls ( $items );
26+
if ( isset ( $response->success ) && $response->success ) {
27+
$response->messages = array_merge (
28+
array (
29+
"Successfully purged assets. Please allow up to 30 " .
30+
"seconds for changes to take effect."
31+
),
32+
$response->messages
33+
);
34+
}
35+
return $this->_sendResponse ( $response );
36+
}
37+
38+
}

src/app/code/JetRails/Cloudflare/Model/Adminhtml/Api/Caching/PurgeCache.php

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,58 @@ public function purgeEverything () {
4343
}
4444

4545
/**
46-
* This method asks the Cloudflare API to purge all the cache in the
47-
* currently selected zone and that match any of the URLs found in the
48-
* passed array.
49-
* @param array urls URLs to purge in zone
46+
* Given a list of items, ask Cloudflare to purge cache that matches the
47+
* list of urls.
48+
* @param array items Items to purge
5049
* @return stdClass Cloudflare response
5150
*/
52-
public function purgeIndividual ( $urls ) {
51+
public function purgeUrls ( $items ) {
5352
$zoneId = $this->_configurationModel->getZoneId ();
5453
$endpoint = sprintf ( "zones/%s/purge_cache", $zoneId );
5554
$this->_requestModel->setType ( Request::REQUEST_DELETE );
56-
$this->_requestModel->setData ( array ( "files" => $urls ) );
55+
$this->_requestModel->setData ( array ( "files" => $items ) );
56+
return $this->_requestModel->resolve ( $endpoint );
57+
}
58+
59+
/**
60+
* Given a list of items, ask Cloudflare to purge cache that matches the
61+
* list of hostnames.
62+
* @param array items Items to purge
63+
* @return stdClass Cloudflare response
64+
*/
65+
public function purgeHosts ( $items ) {
66+
$zoneId = $this->_configurationModel->getZoneId ();
67+
$endpoint = sprintf ( "zones/%s/purge_cache", $zoneId );
68+
$this->_requestModel->setType ( Request::REQUEST_DELETE );
69+
$this->_requestModel->setData ( array ( "hosts" => $items ) );
70+
return $this->_requestModel->resolve ( $endpoint );
71+
}
72+
73+
/**
74+
* Given a list of items, ask Cloudflare to purge cache that matches the
75+
* list of tags.
76+
* @param array items Items to purge
77+
* @return stdClass Cloudflare response
78+
*/
79+
public function purgeTags ( $items ) {
80+
$zoneId = $this->_configurationModel->getZoneId ();
81+
$endpoint = sprintf ( "zones/%s/purge_cache", $zoneId );
82+
$this->_requestModel->setType ( Request::REQUEST_DELETE );
83+
$this->_requestModel->setData ( array ( "tags" => $items ) );
84+
return $this->_requestModel->resolve ( $endpoint );
85+
}
86+
87+
/**
88+
* Given a list of items, ask Cloudflare to purge cache that matches the
89+
* list of prefixes.
90+
* @param array items Items to purge
91+
* @return stdClass Cloudflare response
92+
*/
93+
public function purgePrefixes ( $items ) {
94+
$zoneId = $this->_configurationModel->getZoneId ();
95+
$endpoint = sprintf ( "zones/%s/purge_cache", $zoneId );
96+
$this->_requestModel->setType ( Request::REQUEST_DELETE );
97+
$this->_requestModel->setData ( array ( "prefixes" => $items ) );
5798
return $this->_requestModel->resolve ( $endpoint );
5899
}
59100

src/app/code/JetRails/Cloudflare/view/adminhtml/templates/caching/purge_cache.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</div>
1313
<div class="wrapper_right" >
1414
<div>
15-
<input class="full trigger" data-target="individual" type="button" value="Purge Individual Files" />
15+
<input class="full trigger" data-target="custom" type="button" value="Custom Purge" />
1616
<input class="full trigger" data-target="everything" type="button" value="Purge Everything" />
1717
</div>
1818
<div class="lightbox" style="background-image: url(<?php echo $block->getViewFileUrl ('JetRails_Cloudflare/images/loading.svg') ?>)" > </div>

0 commit comments

Comments
 (0)