public function run() { if (GRAV_TESTS::guess_environment() === 'local') { return array('pass' => null, 'message' => 'Cannot Validate when using Localhost. Try on a valid Hostname.', 'location' => $item->url); } $urls = GRAV_TESTS::get_template_page_urls(); $errors = array(); $loaded_pages = 0; $total_score = 0; foreach ($urls as $url) { if ($response = wp_remote_get('https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=' . $url, array('sslverify' => false, 'timeout' => 30))) { if (!empty($response['body'])) { $json = json_decode($response['body'], true); if (!empty($json) && is_array($json) && !empty($json['ruleGroups']['SPEED']['score'])) { $score = $json['ruleGroups']['SPEED']['score']; if ($score > 0) { $total_score += $score; if ($score < 75) { $errors[] = array('message' => 'Page Speed rating is (' . $score . '). <a target="_blank" href="https://developers.google.com/speed/pagespeed/insights/?url=' . $url . '">Learn More</a>', 'location' => $url); } $loaded_pages++; } } } } } if (!empty($errors)) { return array('pass' => false, 'errors' => $errors, 'message' => 'There were (' . count($errors) . ') pages that did not pass the Speed Test'); } if (!$loaded_pages) { return array('pass' => null, 'message' => 'Unable to connect to Google Page Speed Insights API.'); } return array('pass' => true, 'message' => 'Overall Page Speed rating is (' . round($total_score / $loaded_pages) . ').'); }
public function run() { if (GRAV_TESTS::guess_environment() === 'local') { return array('pass' => null, 'message' => 'Cannot Validate when using Localhost. Try on a valid Hostname.', 'location' => $item->url); } $loaded_pages = 0; $urls = GRAV_TESTS::get_general_page_urls(); $errors = array(); foreach ($urls as $url) { if ($contents = wp_remote_get('https://validator.w3.org/nu/?doc=' . $url, array('sslverify' => false, 'timeout' => 15))) { if (!empty($contents['body'])) { if (strpos($contents['body'], '<div id="results">') === false) { return array('pass' => null, 'message' => 'Error loading W3C Validator', 'location' => ''); } if (strpos($contents['body'], '<li class="error">') !== false) { $errors[] = array('message' => 'Page (' . $url . ') is not HTML Valid. <a target="_blank" href="' . 'https://validator.w3.org/nu/?doc=' . $url . '">Learn More</a>', 'location' => $url); } $loaded_pages++; } } } if (!$loaded_pages) { return array('pass' => null, 'message' => 'Error loading W3C Validator'); } if (!empty($errors)) { return array('pass' => false, 'errors' => $errors, 'message' => 'There are (' . count($errors) . ') Pages with Errors'); } return array('pass' => true, 'message' => 'Successfully Validated (' . $loaded_pages . ') Pages'); }