Skip to content
Draft
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
20 changes: 20 additions & 0 deletions inc/Engine/Admin/PerformanceMonitoring/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
'rocket_options_changed' => 'maybe_cancel_automatic_retest_job',
'rocket_insights_retest' => 'retest_all_pages',
'wp_rocket_upgrade' => [ 'on_update_reset_credit', 10, 2 ],
'rocket_http_referer_tab_hash' => [ 'append_rocket_insights_tab_hash', 10, 2 ],
];
}

Expand Down Expand Up @@ -523,4 +524,23 @@
$this->controller->reset_credit();
}
}

/**
* Appends the 'rocket_insights' tab hash for the 'delete_pm' action.
*
* This method is used as a callback for the 'rocket_http_referer_tab_hash' filter.
* It returns 'rocket_insights' as the tab hash only when the action is 'delete_pm'.
* For all other actions, it returns an empty string.
*
* @param string $tab_hash The current tab hash value.
* @param string $action The current action being performed.
* @return string The tab hash to append, or an empty string if not applicable.
*/
public function append_rocket_insights_tab_hash( string $tab_hash, string $action ) {

Check warning on line 539 in inc/Engine/Admin/PerformanceMonitoring/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Admin/PerformanceMonitoring/Subscriber.php#L539

Avoid unused parameters such as '$tab_hash'.
if ( 'delete_pm' !== $action ) {
return '';
}

return 'rocket_insights';
}
}
59 changes: 58 additions & 1 deletion inc/Engine/Common/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static function get_nonce_post_url( string $action, array $params = [] ):

if ( ! empty( $_SERVER['REQUEST_URI'] ) ) {
$referer_url = filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL );
$params['wp_http_referer'] = rawurlencode( remove_query_arg( 'fl_builder', $referer_url ) );
$params['wp_http_referer'] = rawurlencode( remove_query_arg( 'fl_builder', $referer_url ) . self::get_filtered_tab_hash( $action ) );
}

return wp_nonce_url(
Expand All @@ -71,4 +71,61 @@ public static function get_nonce_post_url( string $action, array $params = [] ):
$action
);
}

/**
* Retrieves the filtered tab hash for the admin interface based on the current action.
*
* This method applies the 'rocket_http_referer_tab_hash' filter to allow customization
* of the tab hash value used in admin URLs. It ensures that the returned tab hash is
* within the list of allowed tab hashes. If the filtered tab hash is not allowed or is empty,
* an empty string is returned. Otherwise, the tab hash is returned prefixed with '#'.
*
* @since 3.20.1
*
* @param string $action The current action being performed.
*
* @return string The filtered and validated tab hash value, prefixed with '#', or an empty string if not valid.
*/
private static function get_filtered_tab_hash( string $action ): string {
$allowed_tab_hashes = [
'dashboard',
'rocket_insights',
'file_optimization',
'media',
'preload',
'advanced_cache',
'database',
'page_cdn',
'heartbeat',
'addons',
'imagify',
'tools',
'tutorials',
'plugins',
];

/**
* Filters the tab hash for the admin interface.
*
* This filter allows customization of the tab hash value used in admin URLs.
*
* @param string $tab_hash The current tab hash value (default: empty string).
* @param string $action The current action being performed.
*
* @return string The filtered tab hash value.
*/
$tab_hash = wpm_apply_filters_typed( 'string', 'rocket_http_referer_tab_hash', '', $action );

// Return early for default value.
if ( '' === $tab_hash ) {
return '';
}

// Return early if not valid tab hash.
if ( ! in_array( $tab_hash, $allowed_tab_hashes, true ) ) {
return '';
}

return '#' . $tab_hash;
}
}
Loading