ger $counter Added to retry 3 times if the request fails.
*
* @return array The analisys result.
*/
public function run_analysis( $url, $device = 'desktop', $counter = 0 ) {
// Try to get the analysis 3 times and then bail.
if ( 3 === $counter ) {
return false;
}
$full_url = home_url( '/' ) . trim( $url, '/' );
// Hit the url, so it can be cached, when Google Api make the request.
if ( 0 === $counter ) {
wp_remote_get( $full_url );
}
// Make the request.
$response = wp_remote_get(
'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=' . $full_url . '&locale=' . get_locale() . '&strategy=' . $device,
array(
'timeout' => 15,
)
);
// Make another request if the previous fail.
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
$counter++;
return $this->run_analysis( $url, $device, $counter );
}
// Decode the response.
$response = json_decode( $response['body'], true );
// Return the analysis.
$items = $this->process_analysis( $response );
$items['device'] = $device;
// Save the test results in the database.
$this->save_test_result( $items );
return $items;
}
/**
* Get the pre-migration speed results.
*
* @since 5.7.13
*
* @return bool/string False if the file is non existing/The array containing the speed-test results.
*/
public function check_for_premigration_test() {
global $wp_filesystem;
// Bail if the file does not exist.
if ( ! file_exists( Helper_Service::get_uploads_dir() . '/pagespeed_results.json' ) ) {
return false;
}
// Return the string containing the pre-migration speed test.
$pre_migration_result = json_decode( $wp_filesystem->get_contents( Helper_Service::get_uploads_dir() . '/pagespeed_results.json' ), true );
// Bail if json cannot be decoded or if the encoded data is deeper than the nesting limit.
if ( false === (bool) $pre_migration_result ) {
// Delete the file so we don't try to decode it upon future activation.
$wp_filesystem->delete( Helper_Service::get_uploads_dir() . '/pagespeed_results.json' );
return false;
}
$data = array_merge( $this->process_analysis( $pre_migration_result ), array( 'device' => 'desktop' ) );
// Save the processed analysis in the database.
add_option( 'sgo_pre_migration_speed_test', $data, '', false );
// Remove the file from the folder.
$wp_filesystem->delete( Helper_Service::get_uploads_dir() . '/pagespeed_results.json' );
}
}
Fatal error: Uncaught Error: Class "SiteGround_Optimizer\Analysis\Analysis" not found in /var/www/vhosts/casainfotech.co.nz/httpdocs/wp-content/plugins/sg-cachepress/core/Rest/Rest_Helper_Options.php:89
Stack trace:
#0 /var/www/vhosts/casainfotech.co.nz/httpdocs/wp-content/plugins/sg-cachepress/core/Helper/Factory_Trait.php(32): SiteGround_Optimizer\Rest\Rest_Helper_Options->__construct()
#1 /var/www/vhosts/casainfotech.co.nz/httpdocs/wp-content/plugins/sg-cachepress/core/Rest/Rest.php(130): SiteGround_Optimizer\Rest\Rest->factory()
#2 /var/www/vhosts/casainfotech.co.nz/httpdocs/wp-content/plugins/sg-cachepress/core/Rest/Rest.php(120): SiteGround_Optimizer\Rest\Rest->load_dependencies()
#3 /var/www/vhosts/casainfotech.co.nz/httpdocs/wp-content/plugins/sg-cachepress/core/Helper/Factory_Trait.php(32): SiteGround_Optimizer\Rest\Rest->__construct()
#4 /var/www/vhosts/casainfotech.co.nz/httpdocs/wp-content/plugins/sg-cachepress/core/Loader/Loader.php(208): SiteGround_Optimizer\Loader\Loader->factory()
#5 /var/www/vhosts/casainfotech.co.nz/httpdocs/wp-content/plugins/sg-cachepress/core/Loader/Loader.php(118): SiteGround_Optimizer\Loader\Loader->load_dependencies()
#6 /var/www/vhosts/casainfotech.co.nz/httpdocs/wp-content/plugins/sg-cachepress/sg-cachepress.php(72): SiteGround_Optimizer\Loader\Loader->__construct()
#7 /var/www/vhosts/casainfotech.co.nz/httpdocs/wp-settings.php(517): include_once('...')
#8 /var/www/vhosts/casainfotech.co.nz/httpdocs/wp-config.php(87): require_once('...')
#9 /var/www/vhosts/casainfotech.co.nz/httpdocs/wp-load.php(50): require_once('...')
#10 /var/www/vhosts/casainfotech.co.nz/httpdocs/wp-blog-header.php(13): require_once('...')
#11 /var/www/vhosts/casainfotech.co.nz/httpdocs/index.php(17): require('...')
#12 {main}
thrown in /var/www/vhosts/casainfotech.co.nz/httpdocs/wp-content/plugins/sg-cachepress/core/Rest/Rest_Helper_Options.php on line 89