Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Plugins_Page
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
42
0.00% covered (danger)
0.00%
0 / 1
 prevent_redirect
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2/**
3 * Prevent redirects away from plugins.php after plugin activation.
4 *
5 * @package brianhenryie/bh-wp-plugins-page
6 */
7
8namespace BrianHenryIE\WP_Plugins_Page\Admin;
9
10/**
11 * Filters wp_redirect to cancel redirecting away from plugins.php.
12 */
13class Plugins_Page {
14
15    /**
16     * Captures redirects and cancels AJAX redirects during plugin-install, redirects to plugins.php for other requests.
17     *
18     * This really should cancel all redirects but the WordPress documentation encourages code that often ends in exit.
19     *
20     * @hooked wp_redirect
21     * @see wp_redirect()
22     *
23     * @param string     $location The URL to redirect to.
24     * @param int|string $status The HTTP status being used.
25     *
26     * @return string|bool A location or flag to cancel redirecting.
27     *
28     * phpcs:disable WordPress.Security.NonceVerification.Recommended
29     * phpcs:disable WordPress.Security.NonceVerification.Missing
30     */
31    public function prevent_redirect( string $location, int|string $status ): string|bool {
32
33        global $pagenow;
34
35        if ( 'plugins.php' === $pagenow && false === stristr( $location, 'plugins.php' ) ) {
36
37            return admin_url( add_query_arg( array_merge( $_POST, $_GET ), 'plugins.php' ) );
38        }
39
40        if ( 'admin-ajax.php' === $pagenow
41            && isset( $_POST['plugin-install'] )
42            && 'plugin-install' === sanitize_text_field( wp_unslash( $_POST['plugin-install'] ) )
43        ) {
44            return false;
45        }
46
47        return $location;
48    }
49}