-
Notifications
You must be signed in to change notification settings - Fork 40
/
class-installer.php
157 lines (141 loc) · 3.97 KB
/
class-installer.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
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
147
148
149
150
151
152
153
154
155
156
157
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Install suggested plugins
*/
if ( !class_exists('cmplz_installer') ){
class cmplz_installer {
private $slug = '';
public $action;
public function __construct($slug) {
if (!function_exists('is_plugin_active')) {
include_once(ABSPATH . 'wp-admin/includes/plugin.php');
}
if ( !current_user_can('install_plugins') ) return;
$this->slug = $slug;
}
/**
* Check if plugin is downloaded
* @return bool
*/
public function plugin_is_downloaded(){
return file_exists(trailingslashit(WP_PLUGIN_DIR).$this->get_activation_slug() );
}
/**
* Check if plugin is activated
* @return bool
*/
public function plugin_is_activated(){
return is_plugin_active($this->get_activation_slug());
}
/**
* Install plugin
* @param string $step
*
* @return void
*/
public function install($step){
if ( !current_user_can('install_plugins') ) return;
ob_start();
if ( $step === 'download' ) {
$this->download_plugin();
}
if ( $step === 'activate' ) {
$this->activate_plugin();
}
ob_get_clean();
}
/**
* Get slug to activate plugin with
* @return string
*/
public function get_activation_slug(){
$slugs = [
'burst-pro' => 'burst-pro/burst-pro.php',
'burst-statistics' => 'burst-statistics/burst.php',
'really-simple-ssl' => 'really-simple-ssl/rlrsssl-really-simple-ssl.php',
'complianz-terms-conditions' => 'complianz-terms-conditions/complianz-terms-conditions.php',
];
return $slugs[$this->slug];
}
/**
* Cancel shepherd tour
* @return void
*/
public function cancel_tour(){
$prefixes = [
'burst-pro' => 'burst',
'burst-statistics' => 'burst',
'really-simple-ssl' => 'rsssl',
'complianz-terms-conditions' => 'cmplz_tc',
];
$prefix = $prefixes[$this->slug];
update_site_option( $prefix.'_tour_started', false );
update_site_option( $prefix.'_tour_shown_once', true );
delete_transient($prefix.'_redirect_to_settings');
}
/**
* Download the plugin
* @return bool
*/
public function download_plugin() {
if ( !current_user_can('install_plugins') ) {
return false;
}
if ( get_transient("rsssl_plugin_download_active")!==$this->slug ) {
set_transient("rsssl_plugin_download_active", $this->slug,MINUTE_IN_SECONDS );
$info = $this->get_plugin_info();
$download_link = esc_url_raw( $info->versions['trunk'] );
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
$skin = new WP_Ajax_Upgrader_Skin();
$upgrader = new Plugin_Upgrader( $skin );
$result = $upgrader->install( $download_link );
if (is_wp_error($result)){
return false;
}
delete_transient("rsssl_plugin_download_active");
}
return true;
}
/**
* Activate the plugin
*
* @return bool
*/
public function activate_plugin() {
if ( !current_user_can('install_plugins')) {
return false;
}
$slug = $this->get_activation_slug();
//when activated from the network admin, we assume the user wants network activated
$networkwide = is_multisite() && is_network_admin();
if ( !defined('DOING_CRON') ) {
define( 'DOING_CRON', true);
}
$result = activate_plugin( $slug, '', $networkwide );
if ( is_wp_error($result) ){
return false;
}
$this->cancel_tour();
return true;
}
/**
* Get plugin info
* @return array|WP_Error
*/
public function get_plugin_info()
{
require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
$plugin_info = get_transient('cmplz_'.$this->slug . '_plugin_info');
if ( empty($plugin_info) ) {
$plugin_info = plugins_api('plugin_information', array('slug' => $this->slug));
if ( !is_wp_error($plugin_info) ) {
set_transient('cmplz_'.$this->slug . '_plugin_info', $plugin_info, WEEK_IN_SECONDS);
}
}
return $plugin_info;
}
}
}