diff --git a/inc/Engine/Admin/PerformanceMonitoring/Subscriber.php b/inc/Engine/Admin/PerformanceMonitoring/Subscriber.php index 9ee0111fd3..6ea6fdf978 100644 --- a/inc/Engine/Admin/PerformanceMonitoring/Subscriber.php +++ b/inc/Engine/Admin/PerformanceMonitoring/Subscriber.php @@ -167,6 +167,7 @@ public static function get_subscribed_events(): array { '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 ], ]; } @@ -523,4 +524,23 @@ public function on_update_reset_credit( $new_version, $old_version ) { $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 ) { + if ( 'delete_pm' !== $action ) { + return ''; + } + + return 'rocket_insights'; + } } diff --git a/inc/Engine/Common/Utils.php b/inc/Engine/Common/Utils.php index d825416d6a..9b1c6ed4a4 100644 --- a/inc/Engine/Common/Utils.php +++ b/inc/Engine/Common/Utils.php @@ -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( @@ -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; + } }