Skip to content

Commit ccf2ac9

Browse files
committed
Multiple changes to purge url
CLI Purge URL - Rewrote code to check for blog to switch before sending purge request Change hash function to allow blog switching
1 parent 5358608 commit ccf2ac9

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

cli/purge.cls.php

+26-4
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ public function url($args)
157157
{
158158
$data = array(
159159
Router::ACTION => Core::ACTION_QS_PURGE,
160-
Router::VALIDATE_PURGE => Router::get_hash(Router::VALIDATE_PURGE),
161160
);
162161
$url = $args[0];
163162
$deconstructed = wp_parse_url($url);
@@ -167,9 +166,30 @@ public function url($args)
167166
}
168167

169168
if (is_multisite()) {
170-
$data['switch_blog'] = get_current_blog_id();
171-
172-
if (get_blog_id_from_url($deconstructed['host'], '/') === 0) {
169+
$current_blog = get_current_blog_id();
170+
$path = '/';
171+
// If subfolder install test if we can identify the blog from url.
172+
// If subdomain install: $path needs to remain '/'.
173+
if(!SUBDOMAIN_INSTALL){
174+
// Get subfolder blog link. Try to match to a blog id.
175+
$temp_path = explode('/', $deconstructed['path']);
176+
if(!empty($temp_path[1])){
177+
$path = '/'.$temp_path[1].'/'; // need / at beginning and end.
178+
}
179+
}
180+
181+
// Attempt to get blog id from URL.
182+
$url_blog = get_blog_id_from_url($deconstructed['host'], $path);
183+
if($url_blog && $url_blog !== $current_blog){
184+
// If blog found will switch to it.
185+
switch_to_blog($url_blog);
186+
}
187+
else{
188+
$path = '/';
189+
}
190+
191+
// Test if link can be found.
192+
if (get_blog_id_from_url($deconstructed['host'], $path) === 0) {
173193
WP_CLI::error('Multisite url passed in is invalid.');
174194
return;
175195
}
@@ -180,6 +200,8 @@ public function url($args)
180200
return;
181201
}
182202
}
203+
// Create security hash.
204+
$data[Router::VALIDATE_PURGE] = Router::get_hash(Router::VALIDATE_PURGE);
183205

184206
WP_CLI::debug('url is ' . $url);
185207

src/router.cls.php

+14-11
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,18 @@ public function is_role_simulation()
269269
/**
270270
* Get a security hash
271271
*
272+
* @since 6.3 - Added parameters $item_name and $blog_id.
272273
* @since 3.3
273274
*/
274-
public static function get_hash($item_name = null)
275+
public static function get_hash($item_name = null, $blog_id = null)
275276
{
277+
$save_blog = get_current_blog_id();
278+
// Switch to blog if sent.
279+
if($blog_id && $save_blog != $blog_id ){
280+
switch_to_blog($blog_id);
281+
}
282+
283+
// Change item name if sent.
276284
if(!$item_name){
277285
$item_name = self::ITEM_HASH;
278286
}
@@ -285,6 +293,11 @@ public static function get_hash($item_name = null)
285293

286294
$hash = Str::rrand(6);
287295
self::update_option($item_name, $hash);
296+
// If needed shitch back to the saved blog.
297+
if($blog_id && $save_blog != $blog_id ){
298+
switch_to_blog($save_blog);
299+
}
300+
288301
return $hash;
289302
}
290303

@@ -512,17 +525,7 @@ private function verify_action()
512525
return;
513526
}
514527

515-
$save_blog = get_current_blog_id();
516-
if ($_REQUEST['switch_blog']) {
517-
// If request parameter "switch_blog", switch to correct blog to generate hash.
518-
switch_to_blog($_REQUEST['switch_blog']);
519-
}
520528
$hash = Router::get_hash(self::VALIDATE_PURGE);
521-
if ($_REQUEST['switch_blog']) {
522-
// Restore blog if needed.
523-
switch_to_blog($save_blog);
524-
}
525-
526529

527530
// Validate request for action Core::ACTION_QS_PURGE. test if request parameter isset and is correct.
528531
if( $action == Core::ACTION_QS_PURGE && ( !isset($_REQUEST[Router::VALIDATE_PURGE]) || $_REQUEST[Router::VALIDATE_PURGE] != $hash ) ){

0 commit comments

Comments
 (0)