| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Utils; |
| 4 |
|
| 5 |
use Automatic_Upgrader_Skin; |
| 6 |
use Plugin_Upgrader; |
| 7 |
use Theme_Upgrader; |
| 8 |
use WP_Ajax_Upgrader_Skin; |
| 9 |
use WP_Filesystem_Base; |
| 10 |
use function activate_plugin; |
| 11 |
use function current_user_can; |
| 12 |
use function install_plugin_install_status; |
| 13 |
use function is_plugin_inactive; |
| 14 |
use function is_wp_error; |
| 15 |
use function plugins_api; |
| 16 |
use function sanitize_key; |
| 17 |
use function wp_unslash; |
| 18 |
|
| 19 |
class Installer extends Base { |
| 20 |
/** |
| 21 |
* Some process take long time to execute |
| 22 |
* for that need to raise the limit. |
| 23 |
*/ |
| 24 |
public static function raise_limits() { |
| 25 |
wp_raise_memory_limit( 'admin' ); |
| 26 |
if ( wp_is_ini_value_changeable( 'max_execution_time' ) ) { |
| 27 |
@ini_set( 'max_execution_time', 0 ); |
| 28 |
} |
| 29 |
@ set_time_limit( 0 ); |
| 30 |
} |
| 31 |
|
| 32 |
public function install( $plugin ): array { |
| 33 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 34 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 35 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 36 |
include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 37 |
|
| 38 |
$response = [ 'success' => false, 'message' => '' ]; |
| 39 |
|
| 40 |
$_plugins = Helper::get_plugins(); |
| 41 |
$is_installed = isset( $_plugins[ $plugin['plugin_file'] ] ); |
| 42 |
|
| 43 |
// Check if the plugin is already active |
| 44 |
if (is_plugin_active($plugin['plugin_file'])) { |
| 45 |
$response['success'] = true; |
| 46 |
$response['slug'] = $plugin['slug']; |
| 47 |
return $response; |
| 48 |
} |
| 49 |
|
| 50 |
if ( isset( $plugin['is_pro'] ) && $plugin['is_pro'] ) { |
| 51 |
if ( ! $is_installed ) { |
| 52 |
$response['code'] = 'pro_plugin'; |
| 53 |
$response['message'] = 'Pro Plugin'; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if ( ! $is_installed ) { |
| 58 |
if(!Helper::current_user_can( 'install_plugins' )){ |
| 59 |
$response['code'] = 'invalid_requirements'; |
| 60 |
$response['message'] = __( 'Sorry, you do not have permission to install a plugin.', 'templately' ); |
| 61 |
return $response; |
| 62 |
} |
| 63 |
|
| 64 |
$plugins_api_args = [ |
| 65 |
'slug' => sanitize_key( wp_unslash( $plugin['slug'] ) ), |
| 66 |
'fields' => [ |
| 67 |
'sections' => false, |
| 68 |
], |
| 69 |
]; |
| 70 |
if(isset($plugin['has_license']) && $plugin['has_license']){ |
| 71 |
$plugins_api_args['has_license'] = $plugin['has_license']; |
| 72 |
} |
| 73 |
/** |
| 74 |
* @var array|object $api |
| 75 |
*/ |
| 76 |
$api = plugins_api( 'plugin_information', $plugins_api_args); |
| 77 |
|
| 78 |
|
| 79 |
if ( is_wp_error( $api ) ) { |
| 80 |
$response['message'] = $api->get_error_message(); |
| 81 |
|
| 82 |
return $response; |
| 83 |
} |
| 84 |
|
| 85 |
$compatibility = $this->check_compatibility($api); |
| 86 |
if (!$compatibility['success']) { |
| 87 |
return $compatibility; |
| 88 |
} |
| 89 |
|
| 90 |
$response['name'] = $api->name; |
| 91 |
|
| 92 |
$skin = new WP_Ajax_Upgrader_Skin(); |
| 93 |
$upgrader = new Plugin_Upgrader( $skin ); |
| 94 |
$result = $upgrader->install( $api->download_link ); |
| 95 |
|
| 96 |
if ( is_wp_error( $result ) ) { |
| 97 |
$response['code'] = $result->get_error_code(); |
| 98 |
$response['message'] = $result->get_error_message(); |
| 99 |
|
| 100 |
return $response; |
| 101 |
} elseif ( is_wp_error( $skin->result ) ) { |
| 102 |
$response['code'] = $skin->result->get_error_code(); |
| 103 |
$response['message'] = $skin->result->get_error_message(); |
| 104 |
|
| 105 |
return $response; |
| 106 |
} elseif ( $skin->get_errors()->has_errors() ) { |
| 107 |
$response['message'] = $skin->get_error_messages(); |
| 108 |
|
| 109 |
return $response; |
| 110 |
} elseif ( is_null( $result ) ) { |
| 111 |
global $wp_filesystem; |
| 112 |
$response['code'] = 'unable_to_connect_to_filesystem'; |
| 113 |
$response['message'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); |
| 114 |
|
| 115 |
if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) { |
| 116 |
$response['message'] = esc_html( $wp_filesystem->errors->get_error_message() ); |
| 117 |
} |
| 118 |
|
| 119 |
return $response; |
| 120 |
} |
| 121 |
else if($result !== true){ |
| 122 |
$response['message'] = __('Failed to install plugin', 'templately'); |
| 123 |
return $response; |
| 124 |
} |
| 125 |
|
| 126 |
$install_status = install_plugin_install_status( $api ); |
| 127 |
$plugin['plugin_file'] = $install_status['file']; |
| 128 |
} |
| 129 |
|
| 130 |
// `$file` was never defined here — the plugin path lives in |
| 131 |
// $plugin['plugin_file'], and it is reassigned above when a fresh install |
| 132 |
// resolves a different one. The undefined name made is_plugin_inactive() |
| 133 |
// see null, which is never "active", so the guard silently collapsed to a |
| 134 |
// bare capability test. It failed closed, but it was not the check written. |
| 135 |
if ( !Helper::current_user_can( 'activate_plugins' ) && is_plugin_inactive( $plugin['plugin_file'] ) ) { |
| 136 |
$response['code'] = 'invalid_requirements'; |
| 137 |
$response['message'] = __( 'Sorry, you do not have permission to activate a plugin.', 'templately' ); |
| 138 |
return $response; |
| 139 |
} |
| 140 |
|
| 141 |
$activate_status = $this->activate_plugin( $plugin['plugin_file'] ); |
| 142 |
|
| 143 |
if ( is_wp_error( $activate_status ) ) { |
| 144 |
$response['message'] = $activate_status->get_error_message(); |
| 145 |
} |
| 146 |
|
| 147 |
if ( $activate_status && ! is_wp_error( $activate_status ) ) { |
| 148 |
$response['success'] = true; |
| 149 |
} |
| 150 |
|
| 151 |
$response['slug'] = $plugin['slug']; |
| 152 |
|
| 153 |
return $response; |
| 154 |
} |
| 155 |
|
| 156 |
public function install_and_activate_theme($theme_slug) { |
| 157 |
require_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'); |
| 158 |
require_once(ABSPATH . 'wp-admin/includes/theme.php'); |
| 159 |
require_once(ABSPATH . 'wp-admin/includes/theme-install.php'); |
| 160 |
|
| 161 |
$response = ['success' => false]; |
| 162 |
|
| 163 |
// Check if the theme is already active |
| 164 |
if ($theme_slug == get_option('stylesheet')) { |
| 165 |
$response['success'] = true; |
| 166 |
$response['message'] = __('Theme is already active', 'templately'); |
| 167 |
return $response; |
| 168 |
} |
| 169 |
|
| 170 |
if (!function_exists('themes_api')) { |
| 171 |
$response['message'] = __('Function themes_api does not exist', 'templately'); |
| 172 |
return $response; |
| 173 |
} |
| 174 |
|
| 175 |
$api = themes_api('theme_information', [ |
| 176 |
'slug' => sanitize_key($theme_slug), |
| 177 |
'fields' => [ |
| 178 |
'sections' => false, |
| 179 |
], |
| 180 |
]); |
| 181 |
|
| 182 |
if (is_wp_error($api)) { |
| 183 |
$response['message'] = $api->get_error_message(); |
| 184 |
return $response; |
| 185 |
} |
| 186 |
|
| 187 |
$compatibility = $this->check_compatibility($api); |
| 188 |
if (!$compatibility['success']) { |
| 189 |
return $compatibility; |
| 190 |
} |
| 191 |
|
| 192 |
if (!wp_get_theme($theme_slug)->exists()) { |
| 193 |
$upgrader = new Theme_Upgrader(new Automatic_Upgrader_Skin()); |
| 194 |
$result = $upgrader->install($api->download_link); |
| 195 |
|
| 196 |
if (is_wp_error($result)) { |
| 197 |
$response['message'] = $result->get_error_message(); |
| 198 |
return $response; |
| 199 |
} |
| 200 |
else if($result !== true){ |
| 201 |
$response['message'] = __('Failed to install theme', 'templately'); |
| 202 |
return $response; |
| 203 |
} |
| 204 |
|
| 205 |
$response['install'] = 'success'; |
| 206 |
} |
| 207 |
|
| 208 |
$activate_status = $this->activate_theme($theme_slug); |
| 209 |
|
| 210 |
if ( is_wp_error( $activate_status ) ) { |
| 211 |
$response['message'] = $activate_status->get_error_message(); |
| 212 |
} |
| 213 |
else if ($activate_status) { |
| 214 |
$response['success'] = true; |
| 215 |
} else { |
| 216 |
$response['message'] = __('Failed to activate theme', 'templately'); |
| 217 |
} |
| 218 |
|
| 219 |
return $response; |
| 220 |
} |
| 221 |
|
| 222 |
public function check_compatibility($api) { |
| 223 |
// Check compatibility with current PHP version |
| 224 |
if (version_compare(PHP_VERSION, $api->requires_php, '<')) { |
| 225 |
return [ |
| 226 |
'success' => false, |
| 227 |
'message' => sprintf(__('The plugin requires PHP version %s or higher. You are running version %s.', 'templately'), $api->requires_php, PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION) |
| 228 |
]; |
| 229 |
} |
| 230 |
|
| 231 |
// Check compatibility with current WP version |
| 232 |
global $wp_version; |
| 233 |
if (version_compare($wp_version, $api->requires, '<')) { |
| 234 |
return [ |
| 235 |
'success' => false, |
| 236 |
'message' => sprintf(__('The plugin requires WordPress version %s or higher. You are running version %s.', 'templately'), $api->requires, $wp_version) |
| 237 |
]; |
| 238 |
} |
| 239 |
|
| 240 |
return ['success' => true]; |
| 241 |
} |
| 242 |
|
| 243 |
private function activate_plugin( $file ) { |
| 244 |
if ( is_plugin_active( $file ) ) { |
| 245 |
return true; |
| 246 |
} |
| 247 |
|
| 248 |
if ( Helper::current_user_can( 'activate_plugins' ) && is_plugin_inactive( $file ) ) { |
| 249 |
$result = activate_plugin( $file ); |
| 250 |
if ( is_wp_error( $result ) ) { |
| 251 |
return $result; |
| 252 |
} else { |
| 253 |
return true; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
return false; |
| 258 |
} |
| 259 |
|
| 260 |
private function activate_theme($theme_slug) { |
| 261 |
if (get_option('stylesheet') == $theme_slug) { |
| 262 |
// The theme is already active |
| 263 |
return true; |
| 264 |
} |
| 265 |
|
| 266 |
if (Helper::current_user_can('switch_themes')) { |
| 267 |
// Activate the theme |
| 268 |
switch_theme($theme_slug); |
| 269 |
|
| 270 |
// Check if the theme was successfully activated |
| 271 |
if (get_option('stylesheet') == $theme_slug) { |
| 272 |
return true; |
| 273 |
} else { |
| 274 |
return new \WP_Error('theme_activation_failed', __('Failed to activate theme', 'templately')); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
return false; |
| 279 |
} |
| 280 |
|
| 281 |
} |