/var/www/html_us/wp-content/plugins/woocommerce/src/Admin/PluginsInstaller.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
 * PluginsInstaller
 *
 * Installer to allow plugin installation via URL query.
 */

namespace Automattic\WooCommerce\Admin;

defined'ABSPATH' ) || exit;

use 
Automattic\WooCommerce\Admin\API\Plugins;
use 
Automattic\WooCommerce\Admin\Features\TransientNotices;

/**
 * Class PluginsInstaller
 */
class PluginsInstaller {

    
/**
     * Constructor
     */
    
public static function init() {
        
add_action'admin_init', array( __CLASS__'possibly_install_activate_plugins' ) );
    }

    
/**
     * Check if an install or activation is being requested via URL query.
     */
    
public static function possibly_install_activate_plugins() {
        
/* phpcs:disable WordPress.Security.NonceVerification.Recommended */
        
if (
            ! isset( 
$_GET['plugin_action'] ) ||
            ! isset( 
$_GET['plugins'] ) ||
            ! 
current_user_can'install_plugins' ) ||
            ! isset( 
$_GET['nonce'] )
        ) {
            return;
        }

        
$nonce sanitize_text_fieldwp_unslash$_GET['nonce'] ) );

        if ( ! 
wp_verify_nonce$nonce'install-plugin' ) ) {
            
wp_nonce_ays'install-plugin' );
        }

        
$plugins       sanitize_text_fieldwp_unslash$_GET['plugins'] ) );
        
$plugin_action sanitize_text_fieldwp_unslash$_GET['plugin_action'] ) );
        
/* phpcs:enable WordPress.Security.NonceVerification.Recommended */

        
$plugins_api     = new Plugins();
        
$install_result  null;
        
$activate_result null;

        switch ( 
$plugin_action ) {
            case 
'install':
                
$install_result $plugins_api->install_plugins( array( 'plugins' => $plugins ) );
                break;
            case 
'activate':
                
$activate_result $plugins_api->activate_plugins( array( 'plugins' => $plugins ) );
                break;
            case 
'install-activate':
                
$install_result  $plugins_api->install_plugins( array( 'plugins' => $plugins ) );
                
$activate_result $plugins_api->activate_plugins( array( 'plugins' => implode','$install_result['data']['installed'] ) ) );
                break;
        }

        
self::cache_results$plugins$install_result$activate_result );
        
self::redirect_to_referer();
    }

    
/**
     * Display the results of installation and activation on the page.
     *
     * @param string $plugins Comma separated list of plugins.
     * @param array  $install_result Result of installation.
     * @param array  $activate_result Result of activation.
     */
    
public static function cache_results$plugins$install_result$activate_result ) {
        if ( ! 
$install_result && ! $activate_result ) {
            return;
        }

        if ( 
is_wp_error$install_result ) || is_wp_error$activate_result ) ) {
            
$message $activate_result $activate_result->get_error_message() : $install_result->get_error_message();
        } else {
            
$message $activate_result $activate_result['message'] : $install_result['message'];
        }

        
TransientNotices::add(
            array(
                
'user_id' => get_current_user_id(),
                
'id'      => 'plugin-installer-' str_replace',''-'$plugins ),
                
'status'  => 'success',
                
'content' => $message,
            )
        );
    }

    
/**
     * Redirect back to the referring page if one exists.
     */
    
public static function redirect_to_referer() {
        
$referer wp_get_referer();
        if ( 
$referer && !== strpos$refererwp_login_url() ) ) {
            
wp_safe_redirect$referer );
            exit();
        }

        if ( ! isset( 
$_SERVER['REQUEST_URI'] ) ) {
            return;
        }

        
$url remove_query_arg'plugin_action'wp_unslash$_SERVER['REQUEST_URI'] ) ); // phpcs:ignore sanitization ok.
        
$url remove_query_arg'plugins'$url );
        
wp_safe_redirect$url );
        exit();
    }
}