| 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 |
if ( !Helper::current_user_can( 'activate_plugins' ) && is_plugin_inactive( $file ) ) { |
| 131 |
$response['code'] = 'invalid_requirements'; |
| 132 |
$response['message'] = __( 'Sorry, you do not have permission to activate a plugin.', 'templately' ); |
| 133 |
return $response; |
| 134 |
} |
| 135 |
|
| 136 |
$activate_status = $this->activate_plugin( $plugin['plugin_file'] ); |
| 137 |
|
| 138 |
if ( is_wp_error( $activate_status ) ) { |
| 139 |
$response['message'] = $activate_status->get_error_message(); |
| 140 |
} |
| 141 |
|
| 142 |
if ( $activate_status && ! is_wp_error( $activate_status ) ) { |
| 143 |
$response['success'] = true; |
| 144 |
} |
| 145 |
|
| 146 |
$response['slug'] = $plugin['slug']; |
| 147 |
|
| 148 |
return $response; |
| 149 |
} |
| 150 |
|
| 151 |
public function install_and_activate_theme($theme_slug) { |
| 152 |
require_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'); |
| 153 |
require_once(ABSPATH . 'wp-admin/includes/theme.php'); |
| 154 |
require_once(ABSPATH . 'wp-admin/includes/theme-install.php'); |
| 155 |
|
| 156 |
$response = ['success' => false]; |
| 157 |
|
| 158 |
// Check if the theme is already active |
| 159 |
if ($theme_slug == get_option('stylesheet')) { |
| 160 |
$response['success'] = true; |
| 161 |
$response['message'] = __('Theme is already active', 'templately'); |
| 162 |
return $response; |
| 163 |
} |
| 164 |
|
| 165 |
if (!function_exists('themes_api')) { |
| 166 |
$response['message'] = __('Function themes_api does not exist', 'templately'); |
| 167 |
return $response; |
| 168 |
} |
| 169 |
|
| 170 |
$api = themes_api('theme_information', [ |
| 171 |
'slug' => sanitize_key($theme_slug), |
| 172 |
'fields' => [ |
| 173 |
'sections' => false, |
| 174 |
], |
| 175 |
]); |
| 176 |
|
| 177 |
if (is_wp_error($api)) { |
| 178 |
$response['message'] = $api->get_error_message(); |
| 179 |
return $response; |
| 180 |
} |
| 181 |
|
| 182 |
$compatibility = $this->check_compatibility($api); |
| 183 |
if (!$compatibility['success']) { |
| 184 |
return $compatibility; |
| 185 |
} |
| 186 |
|
| 187 |
if (!wp_get_theme($theme_slug)->exists()) { |
| 188 |
$upgrader = new Theme_Upgrader(new Automatic_Upgrader_Skin()); |
| 189 |
$result = $upgrader->install($api->download_link); |
| 190 |
|
| 191 |
if (is_wp_error($result)) { |
| 192 |
$response['message'] = $result->get_error_message(); |
| 193 |
return $response; |
| 194 |
} |
| 195 |
else if($result !== true){ |
| 196 |
$response['message'] = __('Failed to install theme', 'templately'); |
| 197 |
return $response; |
| 198 |
} |
| 199 |
|
| 200 |
$response['install'] = 'success'; |
| 201 |
} |
| 202 |
|
| 203 |
$activate_status = $this->activate_theme($theme_slug); |
| 204 |
|
| 205 |
if ( is_wp_error( $activate_status ) ) { |
| 206 |
$response['message'] = $activate_status->get_error_message(); |
| 207 |
} |
| 208 |
else if ($activate_status) { |
| 209 |
$response['success'] = true; |
| 210 |
} else { |
| 211 |
$response['message'] = __('Failed to activate theme', 'templately'); |
| 212 |
} |
| 213 |
|
| 214 |
return $response; |
| 215 |
} |
| 216 |
|
| 217 |
public function check_compatibility($api) { |
| 218 |
// Check compatibility with current PHP version |
| 219 |
if (version_compare(PHP_VERSION, $api->requires_php, '<')) { |
| 220 |
return [ |
| 221 |
'success' => false, |
| 222 |
'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) |
| 223 |
]; |
| 224 |
} |
| 225 |
|
| 226 |
// Check compatibility with current WP version |
| 227 |
global $wp_version; |
| 228 |
if (version_compare($wp_version, $api->requires, '<')) { |
| 229 |
return [ |
| 230 |
'success' => false, |
| 231 |
'message' => sprintf(__('The plugin requires WordPress version %s or higher. You are running version %s.', 'templately'), $api->requires, $wp_version) |
| 232 |
]; |
| 233 |
} |
| 234 |
|
| 235 |
return ['success' => true]; |
| 236 |
} |
| 237 |
|
| 238 |
private function activate_plugin( $file ) { |
| 239 |
if ( is_plugin_active( $file ) ) { |
| 240 |
return true; |
| 241 |
} |
| 242 |
|
| 243 |
if ( Helper::current_user_can( 'activate_plugins' ) && is_plugin_inactive( $file ) ) { |
| 244 |
$result = activate_plugin( $file ); |
| 245 |
if ( is_wp_error( $result ) ) { |
| 246 |
return $result; |
| 247 |
} else { |
| 248 |
return true; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
return false; |
| 253 |
} |
| 254 |
|
| 255 |
private function activate_theme($theme_slug) { |
| 256 |
if (get_option('stylesheet') == $theme_slug) { |
| 257 |
// The theme is already active |
| 258 |
return true; |
| 259 |
} |
| 260 |
|
| 261 |
if (Helper::current_user_can('switch_themes')) { |
| 262 |
// Activate the theme |
| 263 |
switch_theme($theme_slug); |
| 264 |
|
| 265 |
// Check if the theme was successfully activated |
| 266 |
if (get_option('stylesheet') == $theme_slug) { |
| 267 |
return true; |
| 268 |
} else { |
| 269 |
return new \WP_Error('theme_activation_failed', __('Failed to activate theme', 'templately')); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
return false; |
| 274 |
} |
| 275 |
|
| 276 |
} |