Skip to content

Commit

Permalink
Multiple changes to purge url
Browse files Browse the repository at this point in the history
CLI Purge URL - Rewrote code to check for blog to switch before sending purge request
Change hash function to allow blog switching
  • Loading branch information
Tymotey committed Jul 8, 2024
1 parent 5358608 commit ccf2ac9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
30 changes: 26 additions & 4 deletions cli/purge.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ public function url($args)
{
$data = array(
Router::ACTION => Core::ACTION_QS_PURGE,
Router::VALIDATE_PURGE => Router::get_hash(Router::VALIDATE_PURGE),
);
$url = $args[0];
$deconstructed = wp_parse_url($url);
Expand All @@ -167,9 +166,30 @@ public function url($args)
}

if (is_multisite()) {
$data['switch_blog'] = get_current_blog_id();

if (get_blog_id_from_url($deconstructed['host'], '/') === 0) {
$current_blog = get_current_blog_id();
$path = '/';
// If subfolder install test if we can identify the blog from url.
// If subdomain install: $path needs to remain '/'.
if(!SUBDOMAIN_INSTALL){
// Get subfolder blog link. Try to match to a blog id.
$temp_path = explode('/', $deconstructed['path']);
if(!empty($temp_path[1])){
$path = '/'.$temp_path[1].'/'; // need / at beginning and end.
}
}

// Attempt to get blog id from URL.
$url_blog = get_blog_id_from_url($deconstructed['host'], $path);
if($url_blog && $url_blog !== $current_blog){
// If blog found will switch to it.
switch_to_blog($url_blog);
}
else{
$path = '/';
}

// Test if link can be found.
if (get_blog_id_from_url($deconstructed['host'], $path) === 0) {
WP_CLI::error('Multisite url passed in is invalid.');
return;
}
Expand All @@ -180,6 +200,8 @@ public function url($args)
return;
}
}
// Create security hash.
$data[Router::VALIDATE_PURGE] = Router::get_hash(Router::VALIDATE_PURGE);

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

Expand Down
25 changes: 14 additions & 11 deletions src/router.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,18 @@ public function is_role_simulation()
/**
* Get a security hash
*
* @since 6.3 - Added parameters $item_name and $blog_id.
* @since 3.3
*/
public static function get_hash($item_name = null)
public static function get_hash($item_name = null, $blog_id = null)
{
$save_blog = get_current_blog_id();
// Switch to blog if sent.
if($blog_id && $save_blog != $blog_id ){
switch_to_blog($blog_id);
}

// Change item name if sent.
if(!$item_name){
$item_name = self::ITEM_HASH;
}
Expand All @@ -285,6 +293,11 @@ public static function get_hash($item_name = null)

$hash = Str::rrand(6);
self::update_option($item_name, $hash);
// If needed shitch back to the saved blog.
if($blog_id && $save_blog != $blog_id ){
switch_to_blog($save_blog);
}

return $hash;
}

Expand Down Expand Up @@ -512,17 +525,7 @@ private function verify_action()
return;
}

$save_blog = get_current_blog_id();
if ($_REQUEST['switch_blog']) {
// If request parameter "switch_blog", switch to correct blog to generate hash.
switch_to_blog($_REQUEST['switch_blog']);
}
$hash = Router::get_hash(self::VALIDATE_PURGE);
if ($_REQUEST['switch_blog']) {
// Restore blog if needed.
switch_to_blog($save_blog);
}


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

0 comments on commit ccf2ac9

Please sign in to comment.