upsell_installer.php
3.48 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
add_action( 'wp_ajax_emr_plugin_install', 'emr_plugin_install' );
function emr_plugin_install() {
// Run a security check first.
check_admin_referer( 'emr-plugin-install', 'nonce' );
$plugin = isset($_POST['plugin']) ? sanitize_text_field($_POST['plugin']) : null;
if ( ! current_user_can('install_plugins'))
{
// Send back a response.
wp_send_json(array('result'=> false));
die;
}
switch($plugin)
{
case "envira":
$download_url = 'https://downloads.wordpress.org/plugin/envira-gallery-lite.zip';
break;
case 'spio':
$download_url = 'https://downloads.wordpress.org/plugin/shortpixel-image-optimiser.zip';
break;
case 'spai':
$download_url = 'https://downloads.wordpress.org/plugin/shortpixel-adaptive-images.zip';
break;
}
// Install the addon.
if ( ! is_null($download_url ) ) {
//$download_url = esc_url_raw( wp_unslash( $_POST['plugin'] ) );
global $hook_suffix;
// Set the current screen to avoid undefined notices.
set_current_screen();
// Prepare variables.
$method = '';
$url = add_query_arg(
array(
// 'page' => 'envira-gallery-settings',
),
admin_url( 'admin.php' )
);
$url = esc_url( $url );
// Start output bufferring to catch the filesystem form if credentials are needed.
ob_start();
$creds = request_filesystem_credentials( $url, $method, false, false, null );
if ( false === $creds ) {
$form = ob_get_clean();
echo wp_json_encode( array( 'form' => $form ) );
die;
}
// If we are not authenticated, make it happen now.
if ( ! WP_Filesystem( $creds ) ) {
ob_start();
request_filesystem_credentials( $url, $method, true, false, null );
$form = ob_get_clean();
echo wp_json_encode( array( 'form' => $form ) );
die;
}
// We do not need any extra credentials if we have gotten this far, so let's install the plugin.
require_once (ABSPATH . 'wp-admin/includes/class-wp-upgrader.php');
require_once (plugin_dir_path( EMR_ROOT_FILE ) . 'classes/external/upgrader_skin.php');
// Create the plugin upgrader with our custom skin.
$skin = new EMR_Envira_Gallery_Skin();
$installer = new Plugin_Upgrader( $skin );
$installer->install( $download_url );
// Flush the cache and return the newly installed plugin basename.
wp_cache_flush();
if ( $installer->plugin_info() ) {
$plugin_basename = $installer->plugin_info();
ob_clean();
wp_send_json_success( array( 'plugin' => $plugin_basename ) );
die();
}
}
// Send back a response.
wp_send_json(array('result'=> false));
die;
}
add_action( 'wp_ajax_emr_plugin_activate', 'emr_activate' );
/**
* Activates an Envira addon.
*
* @since 1.0.0
*/
function emr_activate() {
// Run a security check first.
check_admin_referer( 'emr-plugin-activate', 'nonce' );
$plugin = isset($_POST['plugin']) ? sanitize_text_field($_POST['plugin']) : null;
if ( ! current_user_can('activate_plugins'))
{
// Send back a response.
wp_send_json(array('result'=> false));
die;
}
switch($plugin)
{
case "envira":
$plugin = 'envira-gallery-lite/envira-gallery-lite.php';
break;
case 'spio':
$plugin = 'shortpixel-image-optimiser/wp-shortpixel.php';
break;
case 'spai':
$plugin = 'shortpixel-adaptive-images/short-pixel-ai.php';
break;
}
// Activate the addon.
if ( ! is_null($plugin) ) {
$activate = activate_plugin( $plugin );
if ( is_wp_error( $activate ) ) {
echo json_encode( array( 'error' => $activate->get_error_message() ) );
die;
}
}
echo json_encode( true );
die;
}