Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,27 @@ function wpcomsp_wayback_link_fixer_get_admin_post_type_link( string $post_type,
// Return the link.
return '<a href="' . esc_url( $url ) . '" target="' . esc_attr( $target ) . '">' . esc_html( $post_type_object->labels->name ) . '</a>';
}

/**
* 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;
}
23 changes: 2 additions & 21 deletions src/Dashboard/Dashboard_Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function render_widget(): void {
if ( ! $api_configured ) {
printf(
'<p><strong>%s</strong></p>',
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;
Expand All @@ -74,26 +74,7 @@ public function render_widget(): void {
return;
}

// IF the service is offline.
if ( ! $status['is_online'] ) {
printf(
'<p style="color: #a00;"><strong>%s</strong></p>',
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(
'<p>%s: %s</p>',
esc_html__( 'Link Checker', 'wpcomsp_wayback_link_fixer' ),
$status['link_checker_online'] ? '<span style="color: #0a0;">&#10004;</span>' : '<span style="color: #a00;">&#10006;</span>'
);

printf(
'<p>%s: %s</p>',
esc_html__( 'Snapshot Service', 'wpcomsp_wayback_link_fixer' ),
$status['snapshot_online'] ? '<span style="color: #0a0;">&#10004;</span>' : '<span style="color: #a00;">&#10006;</span>'
);
print 'TODO';

printf(
'<p>%s: %s</p>',
Expand Down
6 changes: 1 addition & 5 deletions src/Event/Check_Archive_Services_Online_Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
1 change: 0 additions & 1 deletion src/Event/Process_Local_Post_Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion src/Processor/Content_Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
42 changes: 1 addition & 41 deletions src/Settings/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
31 changes: 1 addition & 30 deletions src/Wayback_Machine/HTTP_Client/HTTP_Link_Checker_Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
25 changes: 1 addition & 24 deletions src/Wayback_Machine/HTTP_Client/HTTP_Snapshot_Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
12 changes: 11 additions & 1 deletion src/Wayback_Machine/HTTP_Client/HTTP_System_Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Wayback_Machine/Wayback_Machine_Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 0 additions & 3 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down