From 91870c310ff9b222b4d4a002ad77b40fb8c4c068 Mon Sep 17 00:00:00 2001 From: Vedant Gandhi Date: Wed, 25 Sep 2024 19:42:53 +0530 Subject: [PATCH 1/5] feat: add general options. --- admin/class-nginx-helper-admin.php | 77 +++++++++++++++++++ .../partials/nginx-helper-general-options.php | 7 ++ 2 files changed, 84 insertions(+) diff --git a/admin/class-nginx-helper-admin.php b/admin/class-nginx-helper-admin.php index f91289a..675753d 100644 --- a/admin/class-nginx-helper-admin.php +++ b/admin/class-nginx-helper-admin.php @@ -96,6 +96,12 @@ public function __construct( $plugin_name, $version ) { ); $this->options = $this->nginx_helper_settings(); + + $is_cache_preloaded = get_option( 'is_cache_preloaded' ); + $preload_cache_enabled = get_option( 'preload_cache' ); + if ( $preload_cache_enabled && false === $is_cache_preloaded ) { + $this->preload_cache_from_sitemap(); + } } @@ -278,6 +284,8 @@ public function nginx_helper_default_settings() { 'redis_prefix' => 'nginx-cache:', 'purge_url' => '', 'redis_enabled_by_constant' => 0, + 'preload_cache' => 0, + 'is_cache_preloaded' => 0 ); } @@ -775,5 +783,74 @@ public function purge_all() { public function display_notices() { echo '

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

'; } + + /** + * This function preloads the cache from sitemap url. + * + * @return void + */ + public function preload_cache_from_sitemap() { + global $nginx_purger; + $sitemap_url = get_sitemap_url( 'index' ); + + if ( false === $sitemap_url || empty( $sitemap_url ) ) { + return; + } + + $sitemap_page = get_page_by_path( $sitemap_url ); + + if ( ! $sitemap_page instanceof WP_Post ) { + return; + } + + $sitemap_content = $sitemap_page->post_content; + $urls = $this->extract_sitemap_urls( $sitemap_content ); + + if ( $urls instanceof WP_Error ) { + $nginx_purger->log( ' Error while extracting URL\'s from the sitemap while preloading cache. Error - ', $urls->get_error_message() ); + return; + } + + $args = array( + 'timeout' => 0.01, + 'blocking' => false, + 'sslverify' => false, + ); + + foreach ( $urls as $url ) { + wp_remote_get( esc_url_raw( $url ), $args ); + } + + add_option( 'is_cache_preloaded', true ); + } + + /** + * Parse sitemap content and extract all URLs. + * + * @param string $sitemap_content The XML content of the sitemap. + * @return array|WP_Error An array of URLs or WP_Error on failure. + */ + private function extract_sitemap_urls( $sitemap_content ) { + libxml_use_internal_errors( true ); + $xml = simplexml_load_string( $sitemap_content ); + + if ( false === $xml ) { + return new WP_Error( 'sitemap_parse_error', esc_html__( 'Failed to parse the sitemap XML', 'nginx-helper' ) ); + } + + $urls = array(); + + if ( 'sitemapindex' === $xml->getName() ) { + foreach ( $xml->sitemap as $sitemap ) { + $urls[] = (string) $sitemap->loc; + } + } else { + foreach ( $xml->url as $url ) { + $urls[] = (string) $url->loc; + } + } + + return $urls; + } } diff --git a/admin/partials/nginx-helper-general-options.php b/admin/partials/nginx-helper-general-options.php index a91eb53..705fcb2 100644 --- a/admin/partials/nginx-helper-general-options.php +++ b/admin/partials/nginx-helper-general-options.php @@ -40,6 +40,7 @@ 'purge_page_on_deleted_comment', 'purge_feeds', 'smart_http_expire_form_nonce', + 'preload_cache', ); $all_inputs = array(); @@ -126,6 +127,12 @@ + + + /> + + + From fbd0fe77dd50fd62f4a80a424baa6910146a30bd Mon Sep 17 00:00:00 2001 From: Vedant Gandhi Date: Thu, 26 Sep 2024 13:19:28 +0530 Subject: [PATCH 2/5] feat: add cache preload. --- admin/class-nginx-helper-admin.php | 94 +++++++++++++++++------------- 1 file changed, 55 insertions(+), 39 deletions(-) diff --git a/admin/class-nginx-helper-admin.php b/admin/class-nginx-helper-admin.php index 675753d..cde6ce4 100644 --- a/admin/class-nginx-helper-admin.php +++ b/admin/class-nginx-helper-admin.php @@ -96,12 +96,19 @@ public function __construct( $plugin_name, $version ) { ); $this->options = $this->nginx_helper_settings(); - - $is_cache_preloaded = get_option( 'is_cache_preloaded' ); - $preload_cache_enabled = get_option( 'preload_cache' ); - if ( $preload_cache_enabled && false === $is_cache_preloaded ) { - $this->preload_cache_from_sitemap(); - } + + add_action( 'init', function() { + $is_cache_preloaded = $this->options['is_cache_preloaded']; + $preload_cache_enabled = $this->options['preload_cache']; + + if ( $preload_cache_enabled && false === boolval( $is_cache_preloaded ) ) { + $this->options['is_cache_preloaded'] = true; + + update_site_option( 'rt_wp_nginx_helper_options', $this->options ); + $this->preload_cache_from_sitemap(); + + } + } ); } @@ -790,47 +797,58 @@ public function display_notices() { * @return void */ public function preload_cache_from_sitemap() { - global $nginx_purger; - $sitemap_url = get_sitemap_url( 'index' ); - - if ( false === $sitemap_url || empty( $sitemap_url ) ) { - return; - } - - $sitemap_page = get_page_by_path( $sitemap_url ); - - if ( ! $sitemap_page instanceof WP_Post ) { - return; - } - - $sitemap_content = $sitemap_page->post_content; - $urls = $this->extract_sitemap_urls( $sitemap_content ); - - if ( $urls instanceof WP_Error ) { - $nginx_purger->log( ' Error while extracting URL\'s from the sitemap while preloading cache. Error - ', $urls->get_error_message() ); - return; - } + $sitemap_urls = $this->get_index_sitemap_urls(); + $all_urls = array(); + + foreach ( $sitemap_urls as $sitemap_url ) { + $urls = $this->extract_sitemap_urls( $sitemap_url ); + $all_urls = array_merge( $all_urls, $urls ); + } + $args = array( 'timeout' => 0.01, 'blocking' => false, 'sslverify' => false, ); - foreach ( $urls as $url ) { + foreach ( $all_urls as $url ) { wp_remote_get( esc_url_raw( $url ), $args ); } - - add_option( 'is_cache_preloaded', true ); + + } + + /** + * Fetches all the sitemap urls for the site. + * + * @return array + */ + private function get_index_sitemap_urls() { + $sitemaps = wp_sitemaps_get_server()->index->get_sitemap_list(); + $urls = array(); + foreach ( $sitemaps as $sitemap ) { + $urls[] = $sitemap['loc']; + } + return $urls; } /** * Parse sitemap content and extract all URLs. * - * @param string $sitemap_content The XML content of the sitemap. + * @param string $sitemap_url The URL of the sitemap. * @return array|WP_Error An array of URLs or WP_Error on failure. */ - private function extract_sitemap_urls( $sitemap_content ) { + private function extract_sitemap_urls( $sitemap_url ) { + $response = wp_remote_get( $sitemap_url ); + + $urls = array(); + + if ( is_wp_error( $response ) ) { + return $urls; + } + + $sitemap_content = wp_remote_retrieve_body( $response ); + libxml_use_internal_errors( true ); $xml = simplexml_load_string( $sitemap_content ); @@ -840,14 +858,12 @@ private function extract_sitemap_urls( $sitemap_content ) { $urls = array(); - if ( 'sitemapindex' === $xml->getName() ) { - foreach ( $xml->sitemap as $sitemap ) { - $urls[] = (string) $sitemap->loc; - } - } else { - foreach ( $xml->url as $url ) { - $urls[] = (string) $url->loc; - } + if ( $xml === false ) { + return $urls; + } + + foreach ( $xml->url as $url ) { + $urls[] = (string) $url->loc; } return $urls; From ef85e1d55e3d49d17555a66d637e4d5e99260d16 Mon Sep 17 00:00:00 2001 From: Vedant Gandhi Date: Thu, 26 Sep 2024 13:26:12 +0530 Subject: [PATCH 3/5] feat: tag the preload cache on admin_init action. --- admin/class-nginx-helper-admin.php | 32 +++++++++++++++++------------- includes/class-nginx-helper.php | 3 +++ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/admin/class-nginx-helper-admin.php b/admin/class-nginx-helper-admin.php index cde6ce4..436c9dc 100644 --- a/admin/class-nginx-helper-admin.php +++ b/admin/class-nginx-helper-admin.php @@ -96,19 +96,6 @@ public function __construct( $plugin_name, $version ) { ); $this->options = $this->nginx_helper_settings(); - - add_action( 'init', function() { - $is_cache_preloaded = $this->options['is_cache_preloaded']; - $preload_cache_enabled = $this->options['preload_cache']; - - if ( $preload_cache_enabled && false === boolval( $is_cache_preloaded ) ) { - $this->options['is_cache_preloaded'] = true; - - update_site_option( 'rt_wp_nginx_helper_options', $this->options ); - $this->preload_cache_from_sitemap(); - - } - } ); } @@ -791,12 +778,29 @@ public function display_notices() { echo '

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

'; } + /** + * Preloads the cache for the website. + * + * @return void + */ + public function preload_cache() { + $is_cache_preloaded = $this->options['is_cache_preloaded']; + $preload_cache_enabled = $this->options['preload_cache']; + + if ( $preload_cache_enabled && false === boolval( $is_cache_preloaded ) ) { + $this->options['is_cache_preloaded'] = true; + + update_site_option( 'rt_wp_nginx_helper_options', $this->options ); + $this->preload_cache_from_sitemap(); + } + } + /** * This function preloads the cache from sitemap url. * * @return void */ - public function preload_cache_from_sitemap() { + private function preload_cache_from_sitemap() { $sitemap_urls = $this->get_index_sitemap_urls(); $all_urls = array(); diff --git a/includes/class-nginx-helper.php b/includes/class-nginx-helper.php index f96e6d0..71cb137 100644 --- a/includes/class-nginx-helper.php +++ b/includes/class-nginx-helper.php @@ -227,6 +227,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 action to preload the cache + $this->loader->add_action( 'admin_init', $nginx_helper_admin, 'preload_cache' ); } /** From e0a026ece9e56d37a30a1883a11888a4bedae286 Mon Sep 17 00:00:00 2001 From: Vedant Gandhi Date: Thu, 26 Sep 2024 13:46:42 +0530 Subject: [PATCH 4/5] feat: change the timeout for sitemap fetch. --- admin/class-nginx-helper-admin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/class-nginx-helper-admin.php b/admin/class-nginx-helper-admin.php index 436c9dc..1040495 100644 --- a/admin/class-nginx-helper-admin.php +++ b/admin/class-nginx-helper-admin.php @@ -811,7 +811,7 @@ private function preload_cache_from_sitemap() { } $args = array( - 'timeout' => 0.01, + 'timeout' => 1, 'blocking' => false, 'sslverify' => false, ); From 91c6021fc3d1534b2e22b7d932ead355780a9c2f Mon Sep 17 00:00:00 2001 From: Vedant Gandhi Date: Thu, 8 Aug 2024 14:38:36 +0530 Subject: [PATCH 5/5] feat: add contribution name. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fee3ca7..70de764 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Nginx Helper # [![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active) -**Contributors:** rtcamp, rahul286, saurabhshukla, manishsongirkar36, faishal, desaiuditd, darren-slatten, jk3us, daankortenbach, telofy, pjv, llonchj, jinnko, weskoop, bcole808, gungeekatx, rohanveer, chandrapatel, gagan0123, ravanh, michaelbeil, samedwards, niwreg, entr, nuvoPoint, iam404, rittesh.patel, vishalkakadiya, BhargavBhandari90, vincent-lu, murrayjbrown, bryant1410, 1gor, matt-h, pySilver, johan-chassaing, dotsam, sanketio, petenelson, nathanielks, rigagoogoo, dslatten, jinschoi, kelin1003, vaishuagola27, rahulsprajapati, Joel-James, utkarshpatel, gsayed786, shashwatmittal, sudhiryadav, thrijith, stayallive, jaredwsmith, abhijitrakas, umeshnevase, sid177, souptik, arafatkn, subscriptiongroup, akrocks +**Contributors:** rtcamp, rahul286, saurabhshukla, manishsongirkar36, faishal, desaiuditd, darren-slatten, jk3us, daankortenbach, telofy, pjv, llonchj, jinnko, weskoop, bcole808, gungeekatx, rohanveer, chandrapatel, gagan0123, ravanh, michaelbeil, samedwards, niwreg, entr, nuvoPoint, iam404, rittesh.patel, vishalkakadiya, BhargavBhandari90, vincent-lu, murrayjbrown, bryant1410, 1gor, matt-h, pySilver, johan-chassaing, dotsam, sanketio, petenelson, nathanielks, rigagoogoo, dslatten, jinschoi, kelin1003, vaishuagola27, rahulsprajapati, Joel-James, utkarshpatel, gsayed786, shashwatmittal, sudhiryadav, thrijith, stayallive, jaredwsmith, abhijitrakas, umeshnevase, sid177, souptik, arafatkn, subscriptiongroup, akrocks, Vedant-Gandhi **Tags:** nginx, cache, purge, nginx map, nginx cache, maps, fastcgi, proxy, redis, redis-cache, rewrite, permalinks