get_diverted_js_file_path() ) ) ) { $this->store_local_rendering_files_copy(); } // If for some reason we could not create the file, return the CDN version if ( ! ( file_exists( $this->get_diverted_js_file_path() ) ) ) { return \Affilizz\Core::get_rendering_script_location(); } // Divert the loading $affilizz_proxy_uuid = get_option( 'affilizz_proxy_uuid' ) ?? Core::regenerate_uuid(); $cache_folder_url = trailingslashit( \Affilizz\Core::get_cdn_cache_root_url() ) . $affilizz_proxy_uuid; $cache_file_name = sha1( $affilizz_proxy_uuid . '-' . $extension ) . '.js'; // Bump the version to the installed version return trailingslashit( $cache_folder_url ) . $cache_file_name; } /** * Version of the rendering script URL : the date of the local copy, so that the browsers and the * page caches load a refreshed copy instead of the one they hold under the same URL. * @author Affilizz * @since 1.18.0 * @return string The version. */ public function get_diverted_js_file_version() { $local_file = $this->get_diverted_js_file_path(); return file_exists( $local_file ) ? (string) filemtime( $local_file ) : AFFILIZZ_VERSION; } public function get_diverted_css_file_path() { // Divert the loading $affilizz_proxy_uuid = get_option( 'affilizz_proxy_uuid' ) ?? Core::regenerate_uuid(); $cache_folder_path = trailingslashit( \Affilizz\Core::get_cdn_cache_root_path() ) . $affilizz_proxy_uuid; $cache_file_name = sha1( $affilizz_proxy_uuid ) . '.css'; // Bump the version to the installed version return trailingslashit( $cache_folder_path ) . $cache_file_name; } public function get_diverted_css_file_url() { // Try to copy if ( ! ( file_exists( $this->get_diverted_css_file_path() ) ) ) { $this->store_local_rendering_files_copy(); } // If for some reason we could not create the file, return the CDN version if ( ! ( file_exists( $this->get_diverted_css_file_path() ) ) ) { return \Affilizz\Core::get_rendering_style_location(); } // Divert the loading $affilizz_proxy_uuid = get_option( 'affilizz_proxy_uuid' ) ?? Core::regenerate_uuid(); $cache_folder_url = trailingslashit( \Affilizz\Core::get_cdn_cache_root_url() ) . $affilizz_proxy_uuid; $cache_file_name = sha1( $affilizz_proxy_uuid ) . '.css'; // Bump the version to the installed version return trailingslashit( $cache_folder_url ) . $cache_file_name; } public function store_local_rendering_files_copy() { ( Core::get_instance() )->init_cache_directories(); foreach ( array( array( 'location' => \Affilizz\Core::get_rendering_script_location(), 'local_file' => $this->get_diverted_js_file_path(), 'oldest_time' => time() - ( \Affilizz\Core::get_cdn_cache_time() ?? 0 ), ), array( 'location' => \Affilizz\Core::get_rendering_style_location(), 'local_file' => $this->get_diverted_css_file_path(), 'oldest_time' => time() - ( 15 * MINUTE_IN_SECONDS ), ), ) as $file ) { if ( empty( $file['location'] ) ) { continue; } $remote_file_url = add_query_arg( array( 'cache_var' => time() ), $file['location'] ); $local_file = $file['local_file']; // Check if the file exists and create it if not (method for specific hosting providers) $file_did_exist = file_exists( $local_file ); if ( ! $file_did_exist ) { global $wp_filesystem; if ( empty( $wp_filesystem ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; WP_Filesystem(); } $wp_filesystem->put_contents( $local_file, '', FS_CHMOD_FILE ); } // Check if the file is older than the oldest time if ( ! $file_did_exist || filemtime( $local_file ) < ( $file['oldest_time'] ) ) { $response = wp_remote_get( $remote_file_url, array( 'timeout' => \Affilizz\Core::get_api_http_timeout() ) ); $remote_file_body = wp_remote_retrieve_body( $response ); // A failed download keeps the current copy, or leaves none so that the CDN is used : never an empty script if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) || '' === $remote_file_body ) { if ( ! $file_did_exist ) { wp_delete_file( $local_file ); } continue; } global $wp_filesystem; if ( empty( $wp_filesystem ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; WP_Filesystem(); } $wp_filesystem->put_contents( $local_file, '/* Loaded from local cache - ' . time() . ' */' . $remote_file_body, FS_CHMOD_FILE ); } } } }