Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Paths
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 2
240
0.00% covered (danger)
0.00%
0 / 1
 admin_url
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 is_current_request_activating_plugin_from_plugins_screen
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
210
1<?php
2/**
3 * A Path & URL utility class for Jetpack.
4 *
5 * @package automattic/jetpack-status
6 */
7
8namespace Automattic\Jetpack;
9
10/**
11 * Class Automattic\Jetpack\Paths
12 *
13 * Used to retrieve information about files.
14 */
15class Paths {
16    /**
17     * Jetpack Admin URL.
18     *
19     * @param array $args Query string args.
20     *
21     * @return string Jetpack admin URL.
22     */
23    public function admin_url( $args = null ) {
24        $args = wp_parse_args( $args, array( 'page' => 'jetpack' ) );
25        $url  = add_query_arg( $args, admin_url( 'admin.php' ) );
26        return $url;
27    }
28
29    /**
30     * Determine if the current request is activating a plugin from the plugins page.
31     *
32     * @param string $plugin Plugin file path to check.
33     * @return bool
34     */
35    public function is_current_request_activating_plugin_from_plugins_screen( $plugin ) {
36        // Filter out common async request contexts
37        if (
38            wp_doing_ajax() ||
39            ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ||
40            ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) ||
41            ( defined( 'WP_CLI' ) && WP_CLI )
42        ) {
43            return false;
44        }
45
46        if ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
47            $request_file = esc_url_raw( wp_unslash( $_SERVER['SCRIPT_NAME'] ) );
48        } elseif ( isset( $_SERVER['REQUEST_URI'] ) ) {
49            list( $request_file ) = explode( '?', esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
50        } else {
51            return false;
52        }
53
54        // Not the plugins page
55        if ( strpos( $request_file, 'wp-admin/plugins.php' ) === false ) {
56            return false;
57        }
58
59        // Same method to get the action as used by plugins.php
60        $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
61        $action        = $wp_list_table->current_action();
62
63        // Not a singular activation
64        // This also means that if the plugin is activated as part of a group ( bulk activation ), this function will return false here.
65        if ( 'activate' !== $action ) {
66            return false;
67        }
68
69        // Check the nonce associated with the plugin activation
70        // We are not changing any data here, so this is not super necessary, it's just a best practice before using the form data from $_REQUEST.
71        check_admin_referer( 'activate-plugin_' . $plugin );
72
73        // Not the right plugin
74        $requested_plugin = isset( $_REQUEST['plugin'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['plugin'] ) ) : null;
75        if ( $requested_plugin !== $plugin ) {
76            return false;
77        }
78
79        return true;
80    }
81}