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
Comments update
  • Loading branch information
Tymotey committed Jul 8, 2024
1 parent 5358608 commit 12d258c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
37 changes: 33 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,37 @@ 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 = '/';
$switch_blog = null;
// 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.
}
}

// Get blog id from URL.
$url_blog = get_blog_id_from_url($deconstructed['host'], $path);
if(!$url_blog){
// If not found, is main blog.
$path = '/';
}
else{
// If blog found will switch to it.
$switch_blog = $url_blog;
}

// Switch to blog if needed.
if($switch_blog && $switch_blog!==$current_blog){
switch_to_blog($switch_blog);
}

// Test if link is ok.
if (get_blog_id_from_url($deconstructed['host'], $path) === 0) {
WP_CLI::error('Multisite url passed in is invalid.');
return;
}
Expand All @@ -180,6 +207,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
10 changes: 0 additions & 10 deletions src/router.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,17 +512,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 12d258c

Please sign in to comment.