Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache post page upon getting published and Preload URLs on shutdown hook #274

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 71 additions & 1 deletion admin/class-nginx-helper-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,77 @@ public function purge_all() {
}

/**
* Dispay plugin notices.
* Cache new post upon getting published.
*
* @since 2.2.3
*
* @param string $new_status New post status.
* @param string $old_status Old post status.
* @param WP_Post $post Post object.
*
* @return void
*/
public function preload_new_published_post( $new_status, $old_status, $post ) {

if ( ! $this->options['enable_purge'] ) {
return;
}

if ( 'publish' === $new_status && 'publish' !== $old_status ) {

$post_url = get_permalink( $post );

add_filter( 'rt_nginx_helper_preload_urls', function ( $urls ) use ( $post_url ) {
$urls[] = $post_url;
return $urls;
} );
}

}

/**
* Preload cache of URLs.
*
* @since 2.2.3
*
* @return void
*/
public function preload_urls() {

global $nginx_purger;

if ( ! $this->options['enable_purge'] ) {
return;
}

/**
* Filters the preload urls.
*
* @since 2.2.3
*
* @param array $urls URLs, which will be preloaded.
*/
$urls = apply_filters( "rt_nginx_helper_preload_urls", array() );

if ( ! is_array( $urls ) || count( $urls ) === 0 ) {
return;
}

$nginx_purger->log( "Preloading cache | BEGIN" );

foreach ( $urls as $url ) {

$nginx_purger->log( "- Preloading {$url}" );

wp_remote_get( $url );
}

$nginx_purger->log( "Preloaded cache | END ^^^" );

}

/**
* Display plugin notices.
*/
public function display_notices() {
echo '<div class="updated"><p>' . esc_html__( 'Purge initiated', 'nginx-helper' ) . '</p></div>';
Expand Down
4 changes: 4 additions & 0 deletions includes/class-nginx-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ private function define_admin_hooks() {
$this->loader->add_action( 'wp_ajax_rt_get_feeds', $nginx_helper_admin, 'nginx_helper_get_feeds' );

$this->loader->add_action( 'shutdown', $nginx_helper_admin, 'add_timestamps', 99999 );
$this->loader->add_action( 'shutdown', $nginx_helper_admin, 'preload_urls', 999999 );
$this->loader->add_action( 'add_init', $nginx_helper_admin, 'update_map' );

// Add actions to purge.
Expand All @@ -227,6 +228,9 @@ private function define_admin_hooks() {

// expose action to allow other plugins to purge the cache.
$this->loader->add_action( 'rt_nginx_helper_purge_all', $nginx_purger, 'purge_all' );

// Add URLs to preload
$this->loader->add_action( 'transition_post_status', $nginx_helper_admin, 'preload_new_published_post', 20, 3 );
}

/**
Expand Down