Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #210 from alleyinteractive/bugfix/IRV-384/cache-an…
Browse files Browse the repository at this point in the history
…chor

IRV-384: fix cache clearing behavior
  • Loading branch information
tylerreckart authored Jul 27, 2020
2 parents 73647d4 + be68a7e commit 9baf4c9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 13 additions & 0 deletions inc/endpoints/class-cache-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,15 @@ public function get_route_response( $request ) {

// Ensure the action exists prior to firing.
if ( ! empty( $action ) ) {
$response = [
'success' => true,
'message' => __( 'Cache purged successfully.', 'wp-irving' ),
];

if ( 'irving_site_cache_purge' === $action ) {
do_action( 'wp_irving_site_cache_purge' );

return new \WP_REST_Response( $response );
}

if ( 'irving_page_cache_purge' === $action ) {
Expand All @@ -62,9 +69,15 @@ public function get_route_response( $request ) {
// to firing the action.
if ( ! empty( $route ) ) {
do_action( 'wp_irving_page_cache_purge', $route );

return new \WP_REST_Response( $response );
}
}
}

$response = new \WP_Error( 500, __( 'There was an error clearing the cache. Please check the network tab in the developer console for more information.', 'wp-irving' ) );

return $response;
}

/**
Expand Down
14 changes: 12 additions & 2 deletions inc/templates/admin-bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ function get_cache_items(): array {
'id' => 'irving-cache-one',
'parent' => 'irving-cache',
'title' => __( 'Clear Site Cache', 'wp-irving' ),
'href' => '#',
],
];

Expand All @@ -378,6 +379,7 @@ function get_cache_items(): array {
'id' => 'irving-cache-two',
'parent' => 'irving-cache',
'title' => __( 'Clear Page Cache', 'wp-irving' ),
'href' => '#',
];
}

Expand All @@ -394,14 +396,22 @@ function cache_purge_click_listener() {
<script type="text/javascript">
jQuery('#wp-admin-bar-irving-cache-one').on('click', function() {
const data = { 'action': 'irving_site_cache_purge' };
jQuery.post( '<?php echo esc_url( $rest_endpoint ); ?>', data );
jQuery.post(
'<?php echo esc_url( $rest_endpoint ); ?>',
data,
function(response) { alert(response.message); }
).fail(function(error) { alert(error.responseJSON.message); });;
});
jQuery('#wp-admin-bar-irving-cache-two').on('click', function() {
const data = {
'action': 'irving_page_cache_purge',
'route': window.location.href,
};
jQuery.post( '<?php echo esc_url( $rest_endpoint ); ?>', data );
jQuery.post(
'<?php echo esc_url( $rest_endpoint ); ?>',
data,
function(response) { alert(response.message); }
).fail(function(error) { alert(error.responseJSON.message); });
});
</script>
<?php
Expand Down

0 comments on commit 9baf4c9

Please sign in to comment.