process_404_resources_chunk();
}
$this->fetch_and_check_homepage_resources();
}
// Static method to get the single instance of the class
public static function get_instance() {
if ( self::$instance === null ) {
self::$instance = new self();
}
return self::$instance;
}
// Process resources in chunks
public function process_404_resources_chunk() {
$resources = get_option( 'rsssl_404_resources_to_check' );
if ( empty( $resources ) ) {
update_option( 'rsssl_homepage_contains_404_resources', 'false' );
return false;
}
// Process a chunk of the resources (e.g., 2 at a time)
$chunk_size = 2;
$resources_chunk = array_splice( $resources, 0, $chunk_size );
$result = $this->process_404_resources( $resources_chunk );
// Update the remaining resources back to the option
if ( ! empty( $resources ) ) {
update_option( 'rsssl_404_resources_to_check', $resources );
return 'processing';
} else {
// All resources have been processed
return $result;
}
}
// Function to check homepage and handle 404s
public static function homepage_contains_404_resources() {
$found_404_option_value = get_option( 'rsssl_homepage_contains_404_resources', false );
if ( $found_404_option_value === true || $found_404_option_value === "true" ) {
return true;
}
$resources = get_option( 'rsssl_404_resources_to_check' );
if ( ! empty( $resources ) ) {
// If resources are available to check, process them immediately
return self::get_instance()->process_404_resources_chunk();
}
}
// Function to fetch homepage resources and check for 404 errors
public function fetch_and_check_homepage_resources() {
if ( get_option('rsssl_homepage_contains_404_resources') ) {
return;
}
$site_url = trailingslashit( home_url() );
$response = wp_remote_get( $site_url );
if ( is_wp_error( $response ) ) {
update_option( 'rsssl_homepage_contains_404_resources', false );
return false;
}
$status_code = wp_remote_retrieve_response_code( $response );
if ( $status_code == 404 ) {
update_option( 'rsssl_homepage_contains_404_resources', true );
return true;
}
// Patterns to match img, script, link tags
$body = wp_remote_retrieve_body( $response );
$patterns = array(
'/
]+src=([\'"])?((.*?)\1)/i',
'/