Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
180 lines (148 loc) · 6.45 KB

File metadata and controls

180 lines (148 loc) · 6.45 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* @group https-migration
*/
class Tests_HTTPS_Migration extends WP_UnitTestCase {
/**
* @ticket 51437
*/
public function test_wp_should_replace_insecure_home_url() {
// Should return false because site is not using HTTPS.
$this->force_wp_is_using_https( false );
$this->assertFalse( wp_should_replace_insecure_home_url() );
// Should still return false because HTTPS migration flag is not set.
$this->force_wp_is_using_https( true );
$this->assertFalse( wp_should_replace_insecure_home_url() );
// Should return false because HTTPS migration flag is marked as not required.
update_option( 'https_migration_required', '0' );
$this->assertFalse( wp_should_replace_insecure_home_url() );
// Should return true because HTTPS migration flag is marked as required.
update_option( 'https_migration_required', '1' );
$this->assertTrue( wp_should_replace_insecure_home_url() );
// Should be overridable via filter.
add_filter( 'wp_should_replace_insecure_home_url', '__return_false' );
$this->assertFalse( wp_should_replace_insecure_home_url() );
}
/**
* @ticket 51437
*/
public function test_wp_replace_insecure_home_url() {
$http_url = home_url( '', 'http' );
$https_url = home_url( '', 'https' );
$http_block_data = array(
'id' => 3,
'url' => $http_url . '/wp-content/uploads/2021/01/image.jpg',
);
$https_block_data = array(
'id' => 3,
'url' => $https_url . '/wp-content/uploads/2021/01/image.jpg',
);
$content = '
<!-- wp:paragraph -->
<p><a href="%1$s">This is a link.</a></p>
<!-- /wp:paragraph -->
<!-- wp:custom-media %2$s -->
<img src="%3$s" alt="">
<!-- /wp:custom-media -->
';
$http_content = sprintf( $content, $http_url, wp_json_encode( $http_block_data ), $http_block_data['url'] );
$https_content = sprintf( $content, $https_url, wp_json_encode( $https_block_data ), $https_block_data['url'] );
// Replaces URLs, including its encoded variant.
add_filter( 'wp_should_replace_insecure_home_url', '__return_true' );
$this->assertSame( $https_content, wp_replace_insecure_home_url( $http_content ) );
// Does not replace anything if determined as unnecessary.
add_filter( 'wp_should_replace_insecure_home_url', '__return_false' );
$this->assertSame( $http_content, wp_replace_insecure_home_url( $http_content ) );
}
/**
* @ticket 51437
*/
public function test_wp_update_urls_to_https() {
remove_all_filters( 'option_home' );
remove_all_filters( 'option_siteurl' );
remove_all_filters( 'home_url' );
remove_all_filters( 'site_url' );
$http_url = 'http://example.org';
$https_url = 'https://example.org';
// Set up options to use HTTP URLs.
update_option( 'home', $http_url );
update_option( 'siteurl', $http_url );
// Update URLs to HTTPS (successfully).
$this->assertTrue( wp_update_urls_to_https() );
$this->assertSame( $https_url, get_option( 'home' ) );
$this->assertSame( $https_url, get_option( 'siteurl' ) );
// Switch options back to use HTTP URLs, but now add filter to
// force option value which will make the update irrelevant.
update_option( 'home', $http_url );
update_option( 'siteurl', $http_url );
$this->force_option( 'home', $http_url );
// Update URLs to HTTPS. While the update technically succeeds, it does not take effect due to the enforced
// option. Therefore the change is expected to be reverted.
$this->assertFalse( wp_update_urls_to_https() );
$this->assertSame( $http_url, get_option( 'home' ) );
$this->assertSame( $http_url, get_option( 'siteurl' ) );
}
/**
* @ticket 51437
*/
public function test_wp_update_https_migration_required() {
// Changing HTTP to HTTPS on a site with content should result in flag being set, requiring migration.
update_option( 'fresh_site', '0' );
wp_update_https_migration_required( 'http://example.org', 'https://example.org' );
$this->assertTrue( get_option( 'https_migration_required' ) );
// Changing another part than the scheme should delete/reset the flag because changing those parts (e.g. the
// domain) can have further implications.
wp_update_https_migration_required( 'http://example.org', 'https://another-example.org' );
$this->assertFalse( get_option( 'https_migration_required' ) );
// Changing HTTP to HTTPS on a site without content should result in flag being set, but not requiring migration.
update_option( 'fresh_site', '1' );
wp_update_https_migration_required( 'http://example.org', 'https://example.org' );
$this->assertFalse( get_option( 'https_migration_required' ) );
// Changing (back) from HTTPS to HTTP should delete/reset the flag.
wp_update_https_migration_required( 'https://example.org', 'http://example.org' );
$this->assertFalse( get_option( 'https_migration_required' ) );
}
/**
* @ticket 51437
*/
public function test_wp_should_replace_insecure_home_url_integration() {
// Setup (a site on HTTP, with existing content).
remove_all_filters( 'option_home' );
remove_all_filters( 'option_siteurl' );
remove_all_filters( 'home_url' );
remove_all_filters( 'site_url' );
$http_url = 'http://example.org';
$https_url = 'https://example.org';
update_option( 'home', $http_url );
update_option( 'siteurl', $http_url );
update_option( 'fresh_site', '0' );
// Should return false when URLs are HTTP.
$this->assertFalse( wp_should_replace_insecure_home_url() );
// Should still return false because only one of the two URLs was updated to its HTTPS counterpart.
update_option( 'home', $https_url );
$this->assertFalse( wp_should_replace_insecure_home_url() );
// Should return true because now both URLs are updated to their HTTPS counterpart.
update_option( 'siteurl', $https_url );
$this->assertTrue( wp_should_replace_insecure_home_url() );
// Should return false because the domains of 'home' and 'siteurl' do not match, and we shouldn't make any
// assumptions about such special cases.
update_option( 'siteurl', 'https://wp.example.org' );
$this->assertFalse( wp_should_replace_insecure_home_url() );
}
private function force_wp_is_using_https( $enabled ) {
$scheme = $enabled ? 'https' : 'http';
$replace_scheme = static function ( $url ) use ( $scheme ) {
return str_replace( array( 'http://', 'https://' ), $scheme . '://', $url );
};
add_filter( 'home_url', $replace_scheme, 99 );
add_filter( 'site_url', $replace_scheme, 99 );
}
private function force_option( $option, $value ) {
add_filter(
"option_$option",
static function () use ( $value ) {
return $value;
}
);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.