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