Skip to content

Commit

Permalink
Merge pull request #247 from markkelnar/fix/remove-wpengine-explicit-…
Browse files Browse the repository at this point in the history
…purge

Remove wpengine specific code
  • Loading branch information
markkelnar authored Aug 29, 2023
2 parents 39a56a6 + 88672b7 commit fe9acaa
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 57 deletions.
2 changes: 0 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ parameters:
- phpstan/class-wp-dependency.stub
- phpstan/class-wp-error.stub
- phpstan/appsero.stub
scanFiles:
- phpstan/wpengine.php
bootstrapFiles:
- phpstan/constants.php
- vendor/wp-graphql/wp-graphql/wp-graphql.php
Expand Down
13 changes: 0 additions & 13 deletions phpstan/wpengine.php

This file was deleted.

24 changes: 20 additions & 4 deletions src/Cache/Invalidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ public function init() {
// @phpcs:ignore
do_action( 'graphql_cache_invalidation_init', $this );

// Listen for purge all, purge now request
add_action( 'wpgraphql_cache_purge_all', [ $this, 'on_purge_all_cb' ], 10, 0 );

## Log Purge Events
add_action( 'graphql_purge', [ $this, 'log_purge_events' ], 10, 2 );
add_action( 'graphql_purge', [ $this, 'log_purge_events' ], 10, 3 );

## POST ACTIONS

Expand Down Expand Up @@ -207,10 +210,12 @@ public function should_track_meta( $meta_key, $meta_value, $object ) {
*/
public function purge( $key, $event = 'undefined event' ) {

$graphql_endpoint = preg_replace( '#^.*?://#', '', graphql_get_endpoint_url() );

// This action is emitted with the key to purge.
// Plugins can respond to this action to evict caches for that key
// phpcs:ignore
do_action( 'graphql_purge', $key, $event );
do_action( 'graphql_purge', $key, $event, $graphql_endpoint );

$nodes = $this->collection->get( $key );
if ( is_array( $nodes ) && ! empty( $nodes ) ) {
Expand Down Expand Up @@ -252,10 +257,11 @@ public function purge_nodes( $id_prefix, $id, $event = 'unknown event' ) {
*
* @param string $key The key to purge from teh cache
* @param string $event The Event that triggered the purge
* @param string $hostname The url endpoint associated with the cache key. These match the Url and Key headers provided when the results were cached.
*
* @return void
*/
public function log_purge_events( $key, $event ) {
public function log_purge_events( $key, $event, $hostname ) {

// If purge logging is not enabled, bail
if ( ! Settings::purge_logging_enabled() ) {
Expand All @@ -267,7 +273,7 @@ public function log_purge_events( $key, $event ) {
// @phpcs:ignore
$uri = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '';
$current_page = ! empty( $uri ) ? wp_parse_url( $uri, PHP_URL_PATH ) : 'unknown page';
$message = sprintf( '(graphql_purge) key: %1$s, event: %2$s, user: %3$d, page: %4$s', $key, $event, $current_user_id, $current_page );
$message = sprintf( '(graphql_purge) key: %1$s, event: %2$s, user: %3$d, page: %4$s url: %5$s', $key, $event, $current_user_id, $current_page, $hostname );

// @phpcs:ignore
error_log( $message, 0 );
Expand Down Expand Up @@ -1010,4 +1016,14 @@ public function on_insert_comment_cb( $comment_id, $comment ) {
$this->purge( 'list:comment', 'comment_approved' );
}
}

/**
* When admin user clicks 'Purge Cache Now'.
* Trigger cache invalidation hooks/actions listening for 'graphql_purge'.
*
* @return void
*/
public function on_purge_all_cb() {
$this->purge( 'graphql:Query', 'purge all' );
}
}
38 changes: 0 additions & 38 deletions wp-graphql-smart-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,44 +214,6 @@ function () {
}
);

add_action(
'graphql_purge',
function ( $purge_keys ) {
if ( ! function_exists( 'graphql_get_endpoint_url' ) || ! class_exists( 'WpeCommon', false ) || ! method_exists( \WpeCommon::class, 'http_to_varnish' ) ) {
return;
}
\WpeCommon::http_to_varnish(
'PURGE_GRAPHQL',
null,
[
'GraphQL-Purge-Keys' => $purge_keys,
'GraphQL-URL' => graphql_get_endpoint_url(),
]
);
},
0,
1
);

add_action(
'wpgraphql_cache_purge_all',
function ( $purge_keys ) {
if ( ! function_exists( 'graphql_get_endpoint_url' ) || ! class_exists( 'WpeCommon', false ) || ! method_exists( \WpeCommon::class, 'http_to_varnish' ) ) {
return;
}
\WpeCommon::http_to_varnish(
'PURGE_GRAPHQL',
null,
[
'GraphQL-Purge-Keys' => 'graphql:Query',
'GraphQL-URL' => graphql_get_endpoint_url(),
]
);
},
0,
1
);

/**
* Initialize the plugin tracker
*
Expand Down

0 comments on commit fe9acaa

Please sign in to comment.