diff --git a/functions.php b/functions.php index 828e4c59..2d7d6a69 100644 --- a/functions.php +++ b/functions.php @@ -424,3 +424,27 @@ function wpcomsp_wayback_link_fixer_get_admin_post_type_link( string $post_type, // Return the link. return '' . esc_html( $post_type_object->labels->name ) . ''; } + +/** + * Checks if the API is online. + * + * @since 1.3.0 + * + * @param boolean $force If set to true, will force a check of the API status, ignoring the transient. + * + * @return boolean + */ +function wpcomsp_wayback_link_fixer_is_archive_api_online( bool $force = false ): bool { + // Try to get from transient. + $online = get_transient( 'wlf_archive_api_online' ); + if ( false !== (bool) $online && false === $force ) { + return (bool) $online; + } + + // Check if the system client is online. + $online = wpcomsp_wayback_link_fixer_get_system_client()->is_online(); + // Set the transient + $duration = apply_filters( 'wlf_archive_api_status_duration', \HOUR_IN_SECONDS ); + set_transient( 'wlf_archive_api_online', $online, $duration ); + return (bool) $online; +} diff --git a/src/Dashboard/Dashboard_Notifications.php b/src/Dashboard/Dashboard_Notifications.php index 14dbd605..dfd5bd80 100644 --- a/src/Dashboard/Dashboard_Notifications.php +++ b/src/Dashboard/Dashboard_Notifications.php @@ -58,7 +58,7 @@ public function render_widget(): void { if ( ! $api_configured ) { printf( '

%s

', - esc_html__( 'You are using Link Fixer in unauthenticated mode, which restricts you to 200 new snapshots per day. To unlock higher limits, please enter your API credentials to authenticate with Archive.org.', 'wpcomsp_wayback_link_fixer' ) + esc_html__( 'You are using Link Fixer in unauthenticated mode, which restricts you to 4000 new snapshots per day. To unlock higher limits, please enter your API credentials to authenticate with Archive.org.', 'wpcomsp_wayback_link_fixer' ) ); return; @@ -74,26 +74,7 @@ public function render_widget(): void { return; } - // IF the service is offline. - if ( ! $status['is_online'] ) { - printf( - '

%s

', - esc_html__( 'The link fixer service is currently offline, all checks and snapshot creation processes are paused.', 'wpcomsp_wayback_link_fixer' ) - ); - } - - // Print both services with icons. - printf( - '

%s: %s

', - esc_html__( 'Link Checker', 'wpcomsp_wayback_link_fixer' ), - $status['link_checker_online'] ? '' : '' - ); - - printf( - '

%s: %s

', - esc_html__( 'Snapshot Service', 'wpcomsp_wayback_link_fixer' ), - $status['snapshot_online'] ? '' : '' - ); + print 'TODO'; printf( '

%s: %s

', diff --git a/src/Event/Check_Archive_Services_Online_Event.php b/src/Event/Check_Archive_Services_Online_Event.php index 9412904e..236eaa4f 100644 --- a/src/Event/Check_Archive_Services_Online_Event.php +++ b/src/Event/Check_Archive_Services_Online_Event.php @@ -65,10 +65,6 @@ public static function add_to_queue(): void { */ public function __invoke(): void { $this->setup(); - - $statues = $this->wayback_machine->is_online(); - - // Update the settings. - Settings::update_archive_api_status( (bool) $statues['link_checker'], (bool) $statues['snapshot'] ); + wpcomsp_wayback_link_fixer_is_archive_api_online( true ); } } diff --git a/src/Event/Process_Local_Post_Event.php b/src/Event/Process_Local_Post_Event.php index 20362761..6722dcf7 100644 --- a/src/Event/Process_Local_Post_Event.php +++ b/src/Event/Process_Local_Post_Event.php @@ -92,7 +92,6 @@ public static function add_to_queue_with_delay( int $post_id, int $delay = 1 * \ public function __invoke( int $post_id ): void { $this->setup(); - // If Wayback Link Fixer is offline, add the event to the queue delayed. if ( ! $this->wayback_machine->is_online()['snapshot'] ) { self::add_to_queue_with_delay( $post_id ); diff --git a/src/Processor/Content_Scanner.php b/src/Processor/Content_Scanner.php index 85358389..05ce6017 100644 --- a/src/Processor/Content_Scanner.php +++ b/src/Processor/Content_Scanner.php @@ -67,7 +67,7 @@ public function scan(): self { return $this; } - $dom = new WP_HTML_Tag_Processor( $this->content ); + $dom = new \WP_HTML_Tag_Processor( $this->content ); while ( $dom->next_tag( 'a' ) ) { $href = $dom->get_attribute( 'href' ); diff --git a/src/Settings/Settings.php b/src/Settings/Settings.php index a130360c..e9df28a7 100644 --- a/src/Settings/Settings.php +++ b/src/Settings/Settings.php @@ -266,47 +266,7 @@ public static function get_fixer_option(): string { * @return boolean */ public static function is_archive_api_online(): bool { - // Get the transient. - $status = get_transient( self::ARCHIVE_ORG_STATUS_KEY ); - - // If not set, trigger a check. - if ( false === $status ) { - Check_Archive_Services_Online_Event::add_to_queue(); - return false; - } - - // If the status is not an array or doesnt have the status keu, trigger a check. - if ( ! is_array( $status ) || ! isset( $status['status'] ) ) { - Check_Archive_Services_Online_Event::add_to_queue(); - return false; - } - - // Return the status. - return 'online' === $status['status']; - } - - /** - * Update the current online status of the archive.org API. - * - * @param boolean $link_checker_status The status of the link checker. - * @param boolean $snapshot_status The status of the snapshot service. - * - * @since 1.3.0 - * - * @return void - */ - public static function update_archive_api_status( bool $link_checker_status, bool $snapshot_status ): void { - $status = array( - 'link_checker' => $link_checker_status, - 'snapshot' => $snapshot_status, - 'status' => $link_checker_status && $snapshot_status ? 'online' : 'offline', - 'last-checked' => gmdate( 'Y-m-d H:i:s' ), - ); - - $duration = apply_filters( 'wlf_archive_api_status_duration', \HOUR_IN_SECONDS ); - - // Set the transient. - set_transient( self::ARCHIVE_ORG_STATUS_KEY, $status, $duration ); + return wpcomsp_wayback_link_fixer_is_archive_api_online(); } /** diff --git a/src/Wayback_Machine/HTTP_Client/HTTP_Link_Checker_Client.php b/src/Wayback_Machine/HTTP_Client/HTTP_Link_Checker_Client.php index 4cfa4a59..ab9675ae 100644 --- a/src/Wayback_Machine/HTTP_Client/HTTP_Link_Checker_Client.php +++ b/src/Wayback_Machine/HTTP_Client/HTTP_Link_Checker_Client.php @@ -164,35 +164,6 @@ public function check_single( string $url, array $additional_params = array() ): * @return boolean */ public function is_online(): bool { - - // Compile the url for the live web check service. - $url_params = array( - 'url' => 'https://www.wordpress.org', - 'impersonate' => 1, - ); - - $query_url = add_query_arg( - $url_params, - apply_filters( 'wlf_link_checker_url_base', 'https://iabot-api.archive.org/livewebcheck' ) - ); - - // Get the response, with a defined timeout. - try { - $response = wp_remote_get( $query_url, array( 'timeout' => apply_filters( 'wlf_is_online_timeout', 10 ) ) ); - } catch ( Throwable $e ) { - return false; - } - - // If we get a 503 or 404 response, the service is offline. - if ( 503 === wp_remote_retrieve_response_code( $response ) || 404 === wp_remote_retrieve_response_code( $response ) ) { - return false; - } - - // If the response is a WP Error, the service is offline. - if ( is_wp_error( $response ) ) { - return false; - } - - return true; + return wpcomsp_wayback_link_fixer_is_archive_api_online(); } } diff --git a/src/Wayback_Machine/HTTP_Client/HTTP_Snapshot_Client.php b/src/Wayback_Machine/HTTP_Client/HTTP_Snapshot_Client.php index 130bed03..71d3368f 100644 --- a/src/Wayback_Machine/HTTP_Client/HTTP_Snapshot_Client.php +++ b/src/Wayback_Machine/HTTP_Client/HTTP_Snapshot_Client.php @@ -346,29 +346,6 @@ public function get_snapshot_status( string $job_id ): array { * @return boolean */ public function is_online(): bool { - // Call the check status endpoint. - try { - $response = wp_remote_get( - 'https://web.archive.org/save/status/wlf-status-check', - array( - 'timeout' => apply_filters( 'wlf_is_online_timeout', 10 ), - 'sslverify' => false, - ) - ); - } catch ( Throwable $th ) { - return false; - } - - // If we have a 503 or 404 response, the service is offline. - if ( 503 === wp_remote_retrieve_response_code( $response ) || 404 === wp_remote_retrieve_response_code( $response ) ) { - return false; - } - - // If the response is a WP Error, the service is offline. - if ( is_wp_error( $response ) ) { - return false; - } - - return true; + return wpcomsp_wayback_link_fixer_is_archive_api_online(); } } diff --git a/src/Wayback_Machine/HTTP_Client/HTTP_System_Client.php b/src/Wayback_Machine/HTTP_Client/HTTP_System_Client.php index 228f0676..bdbd7178 100644 --- a/src/Wayback_Machine/HTTP_Client/HTTP_System_Client.php +++ b/src/Wayback_Machine/HTTP_Client/HTTP_System_Client.php @@ -34,7 +34,17 @@ class HTTP_System_Client implements System_Client { * @return boolean True if the service is online, false otherwise. */ public function is_online(): bool { - return true; // Placeholder for actual online check logic. + try { + $response = wp_remote_get( 'http://web.archive.org/save/status/system' ); + } catch ( Throwable $e ) { + return false; + } + + // If we dont have an error or a non 200 response, return false. + if ( is_wp_error( $response ) || ! isset( $response['response']['code'] ) || 200 !== (int) $response['response']['code'] ) { + return false; + } + return true; } /** diff --git a/src/Wayback_Machine/Wayback_Machine_Service.php b/src/Wayback_Machine/Wayback_Machine_Service.php index 171b0f03..7d8316a5 100644 --- a/src/Wayback_Machine/Wayback_Machine_Service.php +++ b/src/Wayback_Machine/Wayback_Machine_Service.php @@ -163,6 +163,7 @@ public function get_snapshot_status( string $ref_code ): array { * @return array{snapshot:boolean, link_checker:boolean} */ public function is_online(): array { + // Only return true if both services are online. return array( 'snapshot' => $this->snapshot_client->is_online(), diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 87c5c7e4..f9be95d8 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -33,9 +33,6 @@ function () { // include the function file include_once dirname( __DIR__ ) . '/functions.php'; - // Set the service status to online. - Settings::update_archive_api_status( true, true ); - // Denote if we should skip live API tests. $GLOBALS['wpcomsp_wayback_link_fixer_skip_live_api_tests'] = false;