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
51 changes: 51 additions & 0 deletions src/Dashboard/Settings_Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,22 @@ private function register_settings_fields(): void {
)
);

register_setting(
self::PAGE_SLUG,
Settings::CAST_ARCHIVED_TO_HTTPS,
array(
'type' => 'boolean',
'sanitize_callback' => 'wp_validate_boolean',
'default' => false,
'show_in_rest' => array(
'name' => Settings::CAST_ARCHIVED_TO_HTTPS,
'schema' => array(
'type' => 'boolean',
),
),
)
);

register_setting(
self::PAGE_SLUG,
Settings::ALLOW_OWN_CONTENT_SUBMISSIONS,
Expand Down Expand Up @@ -563,6 +579,15 @@ function () {
array( 'class' => Settings::is_link_processing_enabled() ? 'iawmlf_toggle_setting__fixer' : 'iawmlf_toggle_setting__fixer hidden' )
);

add_settings_field(
Settings::CAST_ARCHIVED_TO_HTTPS,
__( 'Cast Archived Links to HTTPS', 'internet-archive-wayback-machine-link-fixer' ),
array( $this, 'render_cast_archived_to_https_field' ),
self::PAGE_SLUG,
self::GROUP_LINK_FIXER,
array( 'class' => Settings::is_link_processing_enabled() ? 'iawmlf_toggle_setting__fixer' : 'iawmlf_toggle_setting__fixer hidden' )
);

$empty_api_creds = '' === Settings::get_archive_access_key() && '' === Settings::get_archive_secret_key();

add_settings_field(
Expand Down Expand Up @@ -916,6 +941,32 @@ public function render_minimum_checks_before_broken_field(): void {
<?php
}

/**
* Render the setting to cast archived links to HTTPS.
*
* @since 1.3.5
*
* @return void
*/
public function render_cast_archived_to_https_field(): void {
?>
<label for="<?php echo esc_attr( Settings::CAST_ARCHIVED_TO_HTTPS ); ?>">
<input
type="checkbox"
id="<?php echo esc_attr( Settings::CAST_ARCHIVED_TO_HTTPS ); ?>"
name="<?php echo esc_attr( Settings::CAST_ARCHIVED_TO_HTTPS ); ?>"
value="1"
data-group="link_fixer"
<?php checked( Settings::should_cast_archived_to_https() ); ?>
/>
<?php esc_html_e( 'Force HTTPS', 'internet-archive-wayback-machine-link-fixer' ); ?>
</label>
<p class="description">
<?php esc_html_e( 'Enabling this will convert all archived urls from http to https.', 'internet-archive-wayback-machine-link-fixer' ); ?>
</p>
<?php
}

/**
* Renders a row for excluded urls.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Dashboard/Setup_Wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ private function handle_step_2(): void {
if ( Settings::ONBOARDING_PENDING_OPTION === Settings::get_onboarding_status() ) {
// Force the scan own content to trigger early.
Scan_Posts_Event::force_add_to_action_scheduler();

// Set to true on first run to ensure the archived urls are cast to https by default.
update_option( Settings::CAST_ARCHIVED_TO_HTTPS, true );
}

// Mark as onboarding completed.
Expand Down
8 changes: 7 additions & 1 deletion src/Link/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ public function get_archived_href(): ?string {
$archived_href = str_replace( 'http://web.archive.org/web/', 'http://web-wp.archive.org/web/', $archived_href );
}

// If the setting to cast to https is enabled, cast the start of the url to https.
if ( Settings::should_cast_archived_to_https() ) {
// Replace http with https at the start of the url.
$archived_href = preg_replace( '#^http://web-wp\.archive\.org/#i', 'https://web-wp.archive.org/', $archived_href );
}

return $archived_href;
}

Expand Down Expand Up @@ -486,7 +492,7 @@ public function jsonSerialize(): array {
return array(
'id' => $this->id,
'href' => $this->href,
'archived_href' => $this->archived_href,
'archived_href' => $this->get_archived_href(),
'redirect_href' => $this->redirect_href,
'checks' => $this->checks,
'broken' => $this->is_broken,
Expand Down
14 changes: 14 additions & 0 deletions src/Settings/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Settings {
public const SETUP_WIZARD_STEP_KEY = self::SETTINGS_PREFIX . 'setup_wizard';
public const SETUP_WIZARD_COMPLETED_KEY = self::SETTINGS_PREFIX . 'setup_wizard_completed';
public const ONBOARDING_DATE_KEY = self::SETTINGS_PREFIX . 'onboarding_date';
public const CAST_ARCHIVED_TO_HTTPS = self::SETTINGS_PREFIX . 'cast_to_https';

// Table names.
public const LINK_TABLE = 'iawmlf_link_archive';
Expand Down Expand Up @@ -559,6 +560,17 @@ public static function get_onboarding_status( string $default_value = self::ONBO
: self::ONBOARDING_PENDING_OPTION;
}

/**
* Checks if archived links should be cast as HTTPS.
*
* @since 1.3.5
*
* @return boolean
*/
public static function should_cast_archived_to_https(): bool {
return (bool) get_option( self::CAST_ARCHIVED_TO_HTTPS, false );
}
Comment thread
gin0115 marked this conversation as resolved.

/**
* Clear all the options.
*
Expand Down Expand Up @@ -588,5 +600,7 @@ public static function clear_all_options(): void {
delete_option( self::LINK_CHECK_DURATION_IN_DAYS );
delete_option( self::SETUP_WIZARD_STEP_KEY );
delete_option( self::SETUP_WIZARD_COMPLETED_KEY );
delete_option( self::ONBOARDING_DATE_KEY );
delete_option( self::CAST_ARCHIVED_TO_HTTPS );
}
}
28 changes: 27 additions & 1 deletion tests/Link/Test_Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Internet_Archive\Wayback_Machine_Link_Fixer\Tests\Link;

use Internet_Archive\Wayback_Machine_Link_Fixer\Link\Link;
use Internet_Archive\Wayback_Machine_Link_Fixer\Settings\Settings;

/**
* Test_Link
Expand Down Expand Up @@ -304,7 +305,8 @@ public function test_can_convert_link_to_json(): void {
$this->assertJson( $json );
$this->assertStringContainsString( '"id":1', $json );
$this->assertStringContainsString( '"href":"https:\/\/example.com"', $json );
$this->assertStringContainsString( '"archived_href":"https:\/\/web.archive.org\/web\/20240101000000\/https:\/\/example.com"', $json );
// Also check we are casting to wp urls.
$this->assertStringContainsString( '"archived_href":"https:\/\/web-wp.archive.org\/web\/20240101000000\/https:\/\/example.com"', $json );
$this->assertStringContainsString( '"redirect_href":"https:\/\/example.com"', $json );
$this->assertStringContainsString( '"checks":[{', $json );
$this->assertStringContainsString( '"date":"20240101000000"', $json );
Expand Down Expand Up @@ -415,4 +417,28 @@ public function test_can_reparse_snapshot_url(): void {
$link->set_archived_href( 'http://web.archive.org/web/20240101000000/https://example.com' );
$this->assertSame( 'http://web-wp.archive.org/web/20240101000000/https://example.com', $link->get_archived_href() );
}

/**
* @testdox When an archive link is fetched, it should be possible to cast to https based on settings.
*
* @since 1.3.5
*
* @return void
*/
public function test_cast_to_https_based_on_settings(): void {
$link = new Link( 'http://example.com' );
$link->set_archived_href( 'http://web.archive.org/web/20240101000000/https://example.com' );

// By default, should not cast to https.
$this->assertSame( 'http://web-wp.archive.org/web/20240101000000/https://example.com', $link->get_archived_href() );

// Set the option to cast to https.
update_option( Settings::CAST_ARCHIVED_TO_HTTPS, true );

// Should now NOT cast to https.
$this->assertSame( 'https://web-wp.archive.org/web/20240101000000/https://example.com', $link->get_archived_href() );

// Clear the option.
delete_option( Settings::CAST_ARCHIVED_TO_HTTPS );
}
}
48 changes: 35 additions & 13 deletions tests/Settings/Test_Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public function setUp(): void {
delete_option( Settings::LINK_EXCLUSIONS );
delete_option( Settings::SCAN_EXISTING_POSTS );
delete_option( Settings::FIXER_OPTION );
delete_option( Settings::CAST_ARCHIVED_TO_HTTPS );

update_option(Settings::PROCESS_LINKS, true);
update_option( Settings::PROCESS_LINKS, true );
}

/**
Expand Down Expand Up @@ -80,7 +81,7 @@ public function test_can_set_drop_tables_on_uninstall(): void {
public function test_can_set_and_get_migrations(): void {
$migration_1 = $this->createMock( Abstract_Migration::class );
$migration_2 = $this->createMock( Abstract_Migration::class );
$migrations = [get_class($migration_1), get_class($migration_2)];
$migrations = array( get_class( $migration_1 ), get_class( $migration_2 ) );
Settings::update_migrations( $migrations );
$this->assertEquals( $migrations, Settings::migrations() );
}
Expand Down Expand Up @@ -260,23 +261,39 @@ public function test_can_change_fixer_option(): void {
* @return void
*/
public function test_should_scan_existing_posts_should_return_false_if_option_is_not_enabled(): void {
update_option(Settings::PROCESS_LINKS, false);
$this->assertFalse(Settings::should_scan_existing_posts());
update_option( Settings::PROCESS_LINKS, false );
$this->assertFalse( Settings::should_scan_existing_posts() );

// Set the scan existing posts to true.
update_option(Settings::SCAN_EXISTING_POSTS, true);
$this->assertFalse(Settings::should_scan_existing_posts());
update_option( Settings::SCAN_EXISTING_POSTS, true );
$this->assertFalse( Settings::should_scan_existing_posts() );

// Set the process links to true.
update_option(Settings::PROCESS_LINKS, true);
$this->assertTrue(Settings::should_scan_existing_posts());
update_option( Settings::PROCESS_LINKS, true );
$this->assertTrue( Settings::should_scan_existing_posts() );

// Set the scan existing posts to false.
update_option(Settings::SCAN_EXISTING_POSTS, false);
update_option( Settings::SCAN_EXISTING_POSTS, false );
}

/**
* @testdox It should be possible to get the cast to HTTPS option.
*
* @since 1.3.5
*
* @return void
*/
public function test_can_get_cast_to_https_option_with_fallbacks(): void {
// By default, the cast to HTTPS option should be false.
$this->assertFalse( Settings::should_cast_archived_to_https() );

// When set.
update_option( Settings::CAST_ARCHIVED_TO_HTTPS, true );
$this->assertTrue( Settings::should_cast_archived_to_https() );
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

/**
* @testdox It should be possible to clear all the settings, this can then be used to clear on uninistall.
* @testdox It should be possible to clear all the settings, this can then be used to clear on uninstall.
*
* @return void
*/
Expand All @@ -302,6 +319,7 @@ public function test_can_clear_all_settings(): void {
update_option( Settings::LINK_CHECK_DURATION_IN_DAYS, 14 );
update_option( Settings::SETUP_WIZARD_STEP_KEY, 'step-2' );
update_option( Settings::SETUP_WIZARD_COMPLETED_KEY, true );
update_option( Settings::CAST_ARCHIVED_TO_HTTPS, true );

// Clear all the settings.
Settings::clear_all_options();
Expand All @@ -327,6 +345,7 @@ public function test_can_clear_all_settings(): void {
$this->assertEmpty( get_option( Settings::LINK_CHECK_DURATION_IN_DAYS ) );
$this->assertEmpty( get_option( Settings::SETUP_WIZARD_STEP_KEY ) );
$this->assertEmpty( get_option( Settings::SETUP_WIZARD_COMPLETED_KEY ) );
$this->assertEmpty( get_option( Settings::CAST_ARCHIVED_TO_HTTPS ) );
}

/**
Expand All @@ -346,9 +365,12 @@ public function test_should_render_html_link_output(): void {
$this->assertFalse( Settings::should_render_html_link_output() );

// Set the option to replace link via a filter.
add_filter( 'iawmlf_should_render_html_link_output', function () {
return true;
} );
add_filter(
'iawmlf_should_render_html_link_output',
function () {
return true;
}
);
$this->assertTrue( Settings::should_render_html_link_output() );

// Clean up.
Expand Down