license['license'])){ self::log('speedycache_ccss_logs', 'License Not found, please link your License'); return false; } $path = speedycache_cache_path('critical-css/'); $error = ''; // To hold errors when single Critical CSS is generated $attempted_url = []; // Keeping track of URL that have been proccessed to generate CriticalCSS to handle in case of Timeout if(!is_dir($path)){ mkdir($path); touch($path . 'index.html'); } foreach($urls as $url){ // Handeling php timeout here if($time < time()){ $urls = array_diff($urls, $attempted_url); self::schedule($urls); return; } $url = trim($url, '/'); $license = strpos($speedycache->license['license'], 'SPDFY') !== 0 ? '' : $speedycache->license['license']; $attempted_url[] = $url; $basename = md5($url); $file_name = $path . $basename . '.css'; $response = wp_remote_post($api, array( 'timeout' => 30, 'body' => array( 'url' => $url, 'license' => $license, ), 'sslverify' => false, )); if(is_wp_error($response)){ $error = $response->get_error_message(); self::log('speedycache_ccss_logs', $response->get_error_message(), $url); continue; } $body = json_decode(wp_remote_retrieve_body($response), true); if(empty($body)){ $error = __('The response recieved is empty.', 'speedycache'); self::log('speedycache_ccss_logs', __('The response recieved is empty.', 'speedycache'), $url); continue; } if(empty($body['success'])){ $error = !empty($body['message']) ? wp_strip_all_tags($body['message']) : __('Unable to extract CriticalCss', 'speedycache'); self::log('speedycache_ccss_logs', !empty($body['message']) ? wp_strip_all_tags($body['message']) : __('Unable to extract CriticalCss', 'speedycache'), $url); continue; } if(empty($body['css']) || strlen($body['css']) < 20){ $error = __('Was unable to generate Critical CSS', 'speedycache'); self::log('speedycache_ccss_logs', __('Was unable to generate Critical CSS', 'speedycache'), $url); continue; } if(!is_dir($path)){ mkdir($path); } file_put_contents($file_name, $body['css']); self::update_css($url, $body['css']); self::log('speedycache_ccss_logs', 'success', $url); //Updates the log on success if(!empty($error)){ return $error; } return true; } } // Builds up the list to schedule URLs static function get_url_list(){ global $blog_id; $pages = get_pages(array('child_of' => 0, 'number' => 9)); if(empty($pages)){ return false; } $page_to_crawl = []; $url = get_home_url(!empty($blog_id) ? $blog_id : null); if(!empty($url)){ $page_to_crawl['home'] = $url; } foreach($pages as $p){ $page_to_crawl[$p->ID] = get_page_link($p->ID); } return $page_to_crawl; } // Adds the generated css and asynchronyses the css includes static function update_css($url, $css){ global $speedycache; if(empty($url)){ return false; } if(empty($css) && file_exists(speedycache_cache_path('critical-css/') . md5($url) . '.css')){ $css = file_get_contents(speedycache_cache_path('critical-css/') . md5($url) . '.css'); } if(empty($css)){ return false; } $css = ''; $url = parse_url($url); $uri = !empty($url['path']) ? $url['path'] : ''; $cache_loc = $uri . '/index.html'; if(empty($cache_loc)){ return; } if(!empty($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT'] === 'SpeedyCacheTest'){ $cache_path = speedycache_cache_path('test' . $cache_loc); } else { $cache_path = speedycache_cache_path('all' . $cache_loc); } $cache_path = rtrim($cache_path, '/'); // For Desktop \SpeedyCache\CriticalCss::update_cached($cache_path, $css); if(!empty($speedycache->options['mobile_theme']) && $_SERVER['HTTP_USER_AGENT'] !== 'SpeedyCacheTest'){ $cache_mobile = speedycache_cache_path('mobile-cache' . $cache_loc); // For Mobile Cache if(file_exists($cache_mobile)){ \SpeedyCache\CriticalCss::update_cached($cache_mobile, $css); } } } // Updates the content of the cached file static function update_content($content, $css){ if(strpos($content, 'speedycache-generated-criticalcss') !== FALSE){ $content = preg_replace('/