Closed GaryJones closed 1 year ago
After thinking about it a bit, it seems like that method could be used by anyone that might want to negatively impact the server, so it'd probably have to be restricted to users who are logged in. Adding a shortcut to the toolbar on single post pages might also be beneficial.
Just dropping this here for future reference:
<?php
add_action( 'admin_post_refresh-gists', function() {
$post_id = absint( $_GET['post_id'] );
$is_valid_nonce = isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'delete-gistpress-transients_' . $post_id );
if ( $is_valid_nonce ) {
// @todo Need a public API method to delete the transients.
}
$redirect = empty( $post_id ) ? home_url( '/' ) : get_permalink( $post_id );
wp_safe_redirect( $redirect );
exit;
} );
add_action( 'admin_bar_menu', function( $wp_admin_bar ) {
if ( ! is_single() ) {
return;
}
// @todo Which works better if a secondary loop wasn't properly reset?
//wp_reset_postdata();
$post = get_queried_object();
// @todo Add a capability check.
$url = add_query_arg(
array(
'action' => 'refresh-gists',
'post_id' => $post->ID,
),
admin_url( 'admin-post.php' )
);
$wp_admin_bar->add_menu( array(
'id' => 'gistpress',
'title' => __( 'Refresh Gists', 'gistpress' ),
'href' => wp_nonce_url( $url, 'delete-gistpress-transients_' . $post->ID ),
) );
}, 100 );
An alternative, would be a button on the Edit Post screen (rather than the admin bar), that could clear the GistPress transients associated with the post being edited. Content is edited on the back-end - it wouldn't make sense for some of it (the gists) to be directly editable (refreshable) from the admin bar on the front-end.
This was mentioned in https://github.com/bradyvercher/wp-blazer-six-gist-oembed/issues/1#issuecomment-11771728 - allow a client-side option on single post / pages to force refresh the cache for that post / page.