diff --git a/admin/class-nginx-helper-admin.php b/admin/class-nginx-helper-admin.php index d94d86b9..bcd65a50 100644 --- a/admin/class-nginx-helper-admin.php +++ b/admin/class-nginx-helper-admin.php @@ -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 '

' . esc_html__( 'Purge initiated', 'nginx-helper' ) . '

'; diff --git a/includes/class-nginx-helper.php b/includes/class-nginx-helper.php index 8637d795..e109a0c5 100644 --- a/includes/class-nginx-helper.php +++ b/includes/class-nginx-helper.php @@ -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. @@ -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 ); } /**