Skip to content

Commit

Permalink
Merge pull request #4 from graphcdn/GCDN-1059/wordpress-plugin-action…
Browse files Browse the repository at this point in the history
…-hook-that-runs-after-invalidation

add hook that runs after successful invalidation
  • Loading branch information
thomasheyenbrock authored Apr 20, 2022
2 parents 2f9e144 + e7ae3ca commit 4db80e4
Showing 1 changed file with 61 additions and 46 deletions.
107 changes: 61 additions & 46 deletions wp-graphcdn.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ function render_caching_page()
*/

add_action('admin_post_graphcdn_purge_all', function () {
$res = purge_all();
$query = $res ? 'failure=' . urlencode($res) : 'success=true';
$err = purge_all();
if (!$err) do_action('graphcdn_purge', ['has_purged_all' => true]);
$query = $err ? 'failure=' . urlencode($err) : 'success=true';
wp_redirect('admin.php?page=graphql-caching&' . $query);
exit;
});
Expand All @@ -141,7 +142,8 @@ function render_caching_page()
*/

$GLOBALS['gcdn_purges'] = [
'purge_all' => [],
'has_purged_all' => false,
'purged_types' => [],
'Post' => [],
'Page' => [],
'Category' => [],
Expand Down Expand Up @@ -169,6 +171,12 @@ function render_caching_page()
'User' => 'user'
];

function add_purge_entity(string $key, $value)
{
if (!in_array($value, $GLOBALS['gcdn_purges'][$key], true))
$GLOBALS['gcdn_purges'][$key][] = $value;
}

add_action('registered_post_type', function (string $post_type, WP_Post_Type $post_type_object) {
/**
* Noting to do if the type is not exposed over GraphQL, or if the type
Expand Down Expand Up @@ -214,21 +222,21 @@ function render_caching_page()
* This runs when creating a new term.
*/
add_action("created_{$taxonomy}", function () use ($args) {
$GLOBALS['gcdn_purges']['purge_all'][] = $args['graphql_single_name'];
add_purge_entity('purged_types', $args['graphql_single_name']);
});

/**
* This runs when updating an existing term.
*/
add_action("edited_{$taxonomy}", function (int $term_id) use ($args) {
$GLOBALS['gcdn_purges'][$args['graphql_single_name']][] = $term_id;
add_purge_entity($args['graphql_single_name'], $term_id);
});

/**
* This runs when deleting a term.
*/
add_action("delete_${taxonomy}", function (int $term_id) use ($args) {
$GLOBALS['gcdn_purges'][$args['graphql_single_name']][] = $term_id;
add_purge_entity($args['graphql_single_name'], $term_id);
});
}, 10, 3);

Expand All @@ -244,13 +252,13 @@ function render_caching_page()
/**
* When a post or page has been updated, purge just this one post
*/
$GLOBALS['gcdn_purges'][$type][] = $post_id;
add_purge_entity($type, $post_id);
} else {
/**
* When a new post or page has been created, purge all things related to
* that entity
*/
$GLOBALS['gcdn_purges']['purge_all'][] = $type;
add_purge_entity('purged_types', $type);
}

/**
Expand All @@ -262,8 +270,8 @@ function render_caching_page()
* TODO: Implement a more fine-grained purging for this case.
*/
if ($type === 'Post') {
$GLOBALS['gcdn_purges']['purge_all'][] = 'Category';
$GLOBALS['gcdn_purges']['purge_all'][] = 'Tag';
add_purge_entity('purged_types', 'Category');
add_purge_entity('purged_types', 'Tag');
}
}, 10, 3);

Expand All @@ -274,57 +282,56 @@ function render_caching_page()
add_action('deleted_post', function (int $post_id, WP_Post $post) {
$type = $GLOBALS['gcdn_typename_map'][$post->post_type];
if (!$type) return;

$GLOBALS['gcdn_purges'][$type][] = $post_id;
add_purge_entity($type, $post_id);
}, 10, 2);

/**
* This runs when creating a new category.
*/
add_action('created_category', function () {
$GLOBALS['gcdn_purges']['purge_all'][] = 'Category';
add_purge_entity('purged_types', 'Category');
});

/**
* This runs when updating an existing category.
*/
add_action('edited_category', function (int $category_id) {
$GLOBALS['gcdn_purges']['Category'][] = $category_id;
add_purge_entity('Category', $category_id);
});

/**
* This runs when deleting a category.
*/
add_action('delete_category', function (int $category_id) {
$GLOBALS['gcdn_purges']['Category'][] = $category_id;
add_purge_entity('Category', $category_id);
});

/**
* This runs when creating a new tag.
*/
add_action('created_post_tag', function () {
$GLOBALS['gcdn_purges']['purge_all'][] = 'Tag';
add_purge_entity('purged_types', 'Tag');
});

/**
* This runs when updating an existing tag.
*/
add_action('edited_post_tag', function (int $tag_id) {
$GLOBALS['gcdn_purges']['Tag'][] = $tag_id;
add_purge_entity('Tag', $tag_id);
});

/**
* This runs when deleting a tag.
*/
add_action('delete_post_tag', function (int $tag_id) {
$GLOBALS['gcdn_purges']['Tag'][] = $tag_id;
add_purge_entity('Tag', $tag_id);
});

/**
* This runs when a new comment is created.
*/
add_action('wp_insert_comment', function () {
$GLOBALS['gcdn_purges']['purge_all'][] = 'Comment';
add_purge_entity('purged_types', 'Comment');
});

/**
Expand All @@ -336,56 +343,56 @@ function render_caching_page()
* - Deleting a comment irreversibly
*/
add_action('wp_set_comment_status', function (int $comment_id) {
$GLOBALS['gcdn_purges']['Comment'][] = $comment_id;
add_purge_entity('Comment', $comment_id);
});

/**
* This runs when the content of a comment is updated.
*/
add_action('edit_comment', function (int $comment_id) {
$GLOBALS['gcdn_purges']['Comment'][] = $comment_id;
add_purge_entity('Comment', $comment_id);
});

/**
* This runs when the count of comments for a post is updated.
*/
add_action('wp_update_comment_count', function (int $post_id) {
$GLOBALS['gcdn_purges']['Post'][] = $post_id;
add_purge_entity('Post', $post_id);
});

/**
* This runs when a new menu is created.
*/
add_action('wp_create_nav_menu', function () {
$GLOBALS['gcdn_purges']['purge_all'][] = 'Menu';
add_purge_entity('purged_types', 'Menu');
});

/**
* This runs when a menu is deleted.
*/
add_action('wp_delete_nav_menu', function (int $menu_id) {
$GLOBALS['gcdn_purges']['Menu'][] = $menu_id;
add_purge_entity('Menu', $menu_id);
});

/**
* This runs when a new user is created.
*/
add_action('user_register', function () {
$GLOBALS['gcdn_purges']['purge_all'][] = 'User';
add_purge_entity('purged_types', 'User');
});

/**
* This runs when an existing user is updated.
*/
add_action('profile_update', function (int $user_id) {
$GLOBALS['gcdn_purges']['User'][] = $user_id;
add_purge_entity('User', $user_id);
});

/**
* This runs when a user is deleted.
*/
add_action('delete_user', function (int $user_id) {
$GLOBALS['gcdn_purges']['User'][] = $user_id;
add_purge_entity('User', $user_id);
});


Expand All @@ -403,41 +410,49 @@ function encode_ids(array $ids, string $type_prefix)
};

add_action('shutdown', function () {
/**
* Note that we don't deduplicate at all in the following. The admin api
* will take care of that.
*/

$variable_definitions = '$soft: Boolean';
$selection_set = '';
$variable_values = [];
foreach ($GLOBALS['gcdn_purges'] as $key => $value) {
if ($key === 'purge_all') {
/** Handle types where all entities should by purged. */
foreach ($value as $type) {
$selection_set .= "purge{$type}(soft: \$soft)\n";
}
} else if (count($value) > 0) {
/** Handle purging individual entities by their id. */
$variable_name = "\${$key}Ids";
$variable_definitions .= " {$variable_name}: [ID!]";
$selection_set .= "purge{$key}ById: purge{$key}(soft: \$soft, id: {$variable_name})\n";
$variable_values[$variable_name] = encode_ids($value, $GLOBALS['gcdn_id_prefix_map'][$key]);
switch ($key) {
case 'has_purged_all':
if ($value) {
$selection_set .= '_purgeAll(soft: $soft)';
}
break;
case 'purged_types':
/** Handle types where all entities should by purged. */
foreach ($value as $type) {
$selection_set .= "purge{$type}(soft: \$soft)\n";
}
break;
default:
if (count($value) > 0) {
/** Handle purging individual entities by their id. */
$variable_name = "\${$key}Ids";
$variable_definitions .= " {$variable_name}: [ID!]";
$selection_set .= "purge{$key}ById: purge{$key}(soft: \$soft, id: {$variable_name})\n";
$variable_values[$variable_name] = encode_ids($value, $GLOBALS['gcdn_id_prefix_map'][$key]);
}
break;
}
}

/** Skip sending any request if there is nothing to purge. */
if ($selection_set === '') return;

$query = "mutation WPGraphCDNIntegration(\$soft: Boolean {$variable_definitions}) {
$query = "mutation WPGraphCDNIntegration({$variable_definitions}) {
{$selection_set}
}";
$res = call_admin_api($query, $variable_values);
$err = call_admin_api($query, $variable_values);

if ($res) {
if ($err) {
// Something went wrong, fall back to purging everything
$GLOBALS['gcdn_purges']['has_purged_all'] = true;
purge_all();
}

do_action('graphcdn_purge', $GLOBALS['gcdn_purges']);
});


Expand Down

0 comments on commit 4db80e4

Please sign in to comment.