APC
3 days ago
Events
3 days ago
Purger
3 days ago
assets
3 days ago
AHSC_Apc.php
3 days ago
AHSC_Check.php
3 days ago
AHSC_Config.php
3 days ago
AHSC_Dboptimization.php
3 days ago
AHSC_Functions.php
3 days ago
AHSC_HtmlOptimizer.php
5 months ago
AHSC_Lazyload.php
3 days ago
AHSC_Preconnect.php
5 months ago
AHSC_Static.php
4 months ago
AHSC_Version.php
5 months ago
AHSC_Warmer.php
3 days ago
AHSC_XmlRPC.php
5 months ago
index.php
5 months ago
AHSC_Warmer.php
211 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | if(isset(AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_cache_warmer']) && AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_cache_warmer']!=="false"){ |
| 6 | \add_action( 'wp_ajax_ahcs_cache_warmer', 'ahsc_cache_warmer_ajax_action' , 100 ); |
| 7 | \add_action( 'wp_ajax_nopriv_ahcs_cache_warmer', 'ahsc_cache_warmer_ajax_action' , 100 ); |
| 8 | |
| 9 | $ahsc_do_purge=get_option('ahsc_do_cache_warmer',false); |
| 10 | //$do_purge = ahsc_has_transient( 'ahsc_do_cache_warmer' ); |
| 11 | //var_dump($do_purge); |
| 12 | if($ahsc_do_purge){ |
| 13 | \add_action( 'init', 'ahsc_do_cache_warmer'); |
| 14 | //add_action( 'wp', 'ahsc_cache_warmer_ajax_action'); |
| 15 | } |
| 16 | } |
| 17 | function ahsc_do_cache_warmer(){ |
| 18 | $ahsc_do_purge=get_option('ahsc_do_cache_warmer',false); |
| 19 | if(AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_cache_warmer'] && $ahsc_do_purge){ |
| 20 | \add_action('admin_footer','ahsc_cache_warmer_runner' ); |
| 21 | \add_action('wp_footer', 'ahsc_cache_warmer_runner'); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | function ahsc_cache_warmer_runner() { |
| 26 | $ahsc_do_purge=get_option('ahsc_do_cache_warmer',false); |
| 27 | $ajax_uri = \admin_url( 'admin-ajax.php' ); |
| 28 | $action = 'ahcs_cache_warmer'; |
| 29 | $nonce = \wp_create_nonce( 'ahsc-cache-warmer' ); |
| 30 | |
| 31 | if ( $ahsc_do_purge ) { |
| 32 | /* |
| 33 | * The runner used to be assembled as a <script> string and printed with an |
| 34 | * escaping exemption. wp_print_inline_script_tag() (WP 5.7+) emits the tag for |
| 35 | * us, so nothing has to be escaped by hand, and it honours the |
| 36 | * wp_inline_script_attributes filter — which is what lets a CSP nonce reach the |
| 37 | * tag. The configuration travels as JSON instead of being interpolated into the |
| 38 | * JavaScript source. |
| 39 | */ |
| 40 | $ahsc_runner_config = \wp_json_encode( |
| 41 | array( |
| 42 | 'ajaxUrl' => $ajax_uri, |
| 43 | 'action' => $action, |
| 44 | 'nonce' => $nonce, |
| 45 | ) |
| 46 | ); |
| 47 | |
| 48 | $js_runner = '( function() { |
| 49 | const cfg = ' . $ahsc_runner_config . '; |
| 50 | const data = new FormData(); |
| 51 | data.append( "action", cfg.action ); |
| 52 | data.append( "ahsc_cw_nonce", cfg.nonce ); |
| 53 | |
| 54 | fetch( cfg.ajaxUrl, { |
| 55 | method: "POST", |
| 56 | credentials: "same-origin", |
| 57 | body: data |
| 58 | } ).then( r => r.json() ).then( rr => console.log( "Cache Rigenerata" ) ); |
| 59 | }() );'; |
| 60 | |
| 61 | \wp_print_inline_script_tag( $js_runner ); |
| 62 | } |
| 63 | // ahsc_delete_transient('ahsc_do_cache_warmer'); |
| 64 | //update_option('ahsc_do_cache_warmer',false); |
| 65 | } |
| 66 | /** |
| 67 | * Medoto connected to WP's ajax handler to handle calls to cleaning APIs. |
| 68 | * |
| 69 | * @return void |
| 70 | * |
| 71 | * @SuppressWarnings(PHPMD.ElseExpression) |
| 72 | */ |
| 73 | function ahsc_cache_warmer_ajax_action() { |
| 74 | |
| 75 | $ahsc_do_purge=get_option('ahsc_do_cache_warmer',false); |
| 76 | |
| 77 | if($ahsc_do_purge) { |
| 78 | $do_warmer = array(); |
| 79 | |
| 80 | if ( isset( $_POST['ahsc_cw_nonce'] ) && ! \wp_verify_nonce( \sanitize_text_field( \wp_unslash( $_POST['ahsc_cw_nonce'] ) ), 'ahsc-cache-warmer' ) ) { |
| 81 | |
| 82 | wp_die( wp_json_encode( AHSC_AJAX['security_error'] ) ); |
| 83 | } |
| 84 | |
| 85 | // If a static page has not been set as the site's home. |
| 86 | if ( 'posts' === \get_option( 'show_on_front' ) ) { |
| 87 | $do_warmer[] = \get_home_url( null, '/' ); |
| 88 | } |
| 89 | |
| 90 | // If a static page has been set as the site's home. |
| 91 | if ( 'page' === get_option( 'show_on_front' ) ) { |
| 92 | $do_warmer[] = \get_permalink( \get_option( 'page_on_front' ) ); |
| 93 | $blog_list = \get_option( 'page_for_posts' ); |
| 94 | |
| 95 | // I check whether the two urls are different. If no page is set as 'article page', the same url is returned. |
| 96 | if ( '0' != $blog_list ) { |
| 97 | $do_warmer[] = \get_post_type_archive_link( 'post' ); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if ( class_exists( 'woocommerce' ) ) { |
| 102 | $do_warmer[] = get_permalink( wc_get_page_id( 'shop' ) ); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | $recent_posts = wp_get_recent_posts( array( |
| 107 | 'numberposts' => 10, // Number of recent posts |
| 108 | 'post_status' => 'publish' // Get only the published posts |
| 109 | ) ); |
| 110 | |
| 111 | foreach ( $recent_posts as $recent_post ) { |
| 112 | $do_warmer[] = get_permalink( $recent_post['ID'] ); |
| 113 | } |
| 114 | |
| 115 | //prodotti ultimi 10 prodotti modificati |
| 116 | if ( class_exists( 'woocommerce' ) ) { |
| 117 | $args = array( |
| 118 | 'limit' => 10, |
| 119 | 'orderby' => 'modified', |
| 120 | 'order' => 'DESC', |
| 121 | 'return' => 'ids', |
| 122 | ); |
| 123 | $products = wc_get_products( $args ); |
| 124 | foreach ( $products as $pos => $pid ) { |
| 125 | $do_warmer[] = get_permalink( $pid ); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | //pagine linkate nella homepage |
| 130 | /* |
| 131 | $url = get_home_url(); |
| 132 | //$html = file_get_contents( $url ); |
| 133 | $ch = curl_init(); |
| 134 | curl_setopt($ch, CURLOPT_URL, $url); |
| 135 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 136 | $html = curl_exec($ch); |
| 137 | curl_close($ch); |
| 138 | $doc = new DOMDocument(); |
| 139 | $doc->loadHTML( $html ); |
| 140 | $xpath = new DOMXpath( $doc ); |
| 141 | $nodes = $xpath->query( '//a' ); |
| 142 | $_domain = preg_replace( '/^www\./', '', $_SERVER['HTTP_HOST'] ); |
| 143 | foreach ( $nodes as $node ) { |
| 144 | $domain = implode( '.', array_slice( explode( '.', parse_url( $node->getAttribute( 'href' ), PHP_URL_HOST ) ), - 2 ) ); |
| 145 | if ( $domain == $_domain && array_search( trailingslashit( $node->getAttribute( 'href' ) ), $do_warmer ) === false ) { |
| 146 | if ( $node->getAttribute( 'href' ) !== $url ) { |
| 147 | $do_warmer[] = $node->getAttribute( 'href' ); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | $do_warmer = array_unique( $do_warmer ); |
| 152 | */ |
| 153 | |
| 154 | /* |
| 155 | * Warming used raw cURL here. The WordPress HTTP API covers the same options |
| 156 | * one to one and, unlike a bare curl_exec(), also honours the |
| 157 | * pre_http_request / http_request_args filters, the WP_PROXY_* constants and |
| 158 | * WP_ACCESSIBLE_HOSTS, which some hosting setups rely on for outbound traffic. |
| 159 | */ |
| 160 | foreach ( $do_warmer as $warmer_item ) { |
| 161 | $ahsc_response = \wp_remote_get( |
| 162 | $warmer_item, |
| 163 | array( |
| 164 | /** |
| 165 | * Filters the per-URL timeout of the cache warmer, in seconds. |
| 166 | * |
| 167 | * The previous cURL implementation set no timeout at all, so a single |
| 168 | * slow page could hang the whole warming request. |
| 169 | * |
| 170 | * @param int $timeout Timeout in seconds. |
| 171 | * @param string $warmer_item URL being warmed. |
| 172 | */ |
| 173 | 'timeout' => \apply_filters( 'ahsc_cache_warmer_timeout', 10, $warmer_item ), |
| 174 | // Was CURLOPT_FOLLOWLOCATION false. |
| 175 | 'redirection' => 0, |
| 176 | /* |
| 177 | * Kept from the cURL implementation (CURLOPT_SSL_VERIFYPEER / |
| 178 | * CURLOPT_SSL_VERIFYHOST were both false): the warmer calls the site |
| 179 | * itself, which may answer on an internal name or a self-signed |
| 180 | * certificate. |
| 181 | */ |
| 182 | 'sslverify' => false, |
| 183 | 'user-agent' => 'arubacache', |
| 184 | 'headers' => array( |
| 185 | 'accept-encoding' => 'gzip, deflate, br, zstd', |
| 186 | ), |
| 187 | ) |
| 188 | ); |
| 189 | |
| 190 | /* |
| 191 | * curl_exec() returns false on failure, it never throws, so the try/catch |
| 192 | * that used to wrap it could not fire and every network error was lost. |
| 193 | */ |
| 194 | if ( \is_wp_error( $ahsc_response ) ) { |
| 195 | AHSC_log( |
| 196 | sprintf( 'Cache warming failed for %1$s: %2$s', $warmer_item, $ahsc_response->get_error_message() ), |
| 197 | 'cache-warmer', |
| 198 | 'warning' |
| 199 | ); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | update_option( 'ahsc_do_cache_warmer', false ); |
| 204 | wp_die( wp_json_encode( array( 'esit' => true, 'items' => $do_warmer ) ) ); |
| 205 | }else{ |
| 206 | wp_die( wp_json_encode( array( 'esit' => true, 'items' => 'no cache to warming' ) ) ); |
| 207 | } |
| 208 | |
| 209 | //update_option( 'ahsc_do_cache_warmer', false ); |
| 210 | |
| 211 | } |