| @@ -39,15 +39,8 @@ | ||
| 39 | 39 | |
| 40 | 40 | $_plugins = Helper::get_plugins(); |
| 41 | 41 | $is_installed = isset( $_plugins[ $plugin['plugin_file'] ] ); |
| 42 | 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 | 43 | if ( isset( $plugin['is_pro'] ) && $plugin['is_pro'] ) { |
| 51 | 44 | if ( ! $is_installed ) { |
| 52 | 45 | $response['code'] = 'pro_plugin'; |
| 53 | 46 | $response['message'] = 'Pro Plugin'; |
| @@ -58,13 +51,13 @@ | ||
| 58 | 51 | /** |
| 59 | 52 | * @var array|object $api |
| 60 | 53 | */ |
| 61 | 54 | $api = plugins_api( 'plugin_information', [ |
| 62 | - 'slug' => sanitize_key( wp_unslash( $plugin['slug'] ) ), | |
| 63 | - 'fields' => [ | |
| 64 | - 'sections' => false, | |
| 65 | - ], | |
| 66 | - ] ); | |
| 55 | + 'slug' => sanitize_key( wp_unslash( $plugin['slug'] ) ), | |
| 56 | + 'fields' => [ | |
| 57 | + 'sections' => false, | |
| 58 | + ], | |
| 59 | + ] ); | |
| 67 | 60 | |
| 68 | 61 | if ( is_wp_error( $api ) ) { |
| 69 | 62 | $response['message'] = $api->get_error_message(); |
| 70 | 63 | |
| @@ -70,13 +63,8 @@ | ||
| 70 | 63 | |
| 71 | 64 | return $response; |
| 72 | 65 | } |
| 73 | 66 | |
| 74 | - $compatibility = $this->check_compatibility($api); | |
| 75 | - if (!$compatibility['success']) { | |
| 76 | - return $compatibility; | |
| 77 | - } | |
| 78 | - | |
| 79 | 67 | $response['name'] = $api->name; |
| 80 | 68 | |
| 81 | 69 | $skin = new WP_Ajax_Upgrader_Skin(); |
| 82 | 70 | $upgrader = new Plugin_Upgrader( $skin ); |
| @@ -106,12 +94,8 @@ | ||
| 106 | 94 | } |
| 107 | 95 | |
| 108 | 96 | return $response; |
| 109 | 97 | } |
| 110 | - else if($result !== true){ | |
| 111 | - $response['message'] = __('Failed to install plugin', 'templately'); | |
| 112 | - return $response; | |
| 113 | - } | |
| 114 | 98 | |
| 115 | 99 | $install_status = install_plugin_install_status( $api ); |
| 116 | 100 | $plugin['plugin_file'] = $install_status['file']; |
| 117 | 101 | } |
| @@ -137,17 +121,10 @@ | ||
| 137 | 121 | require_once(ABSPATH . 'wp-admin/includes/theme-install.php'); |
| 138 | 122 | |
| 139 | 123 | $response = ['success' => false]; |
| 140 | 124 | |
| 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 | 125 | if (!function_exists('themes_api')) { |
| 149 | - $response['message'] = __('Function themes_api does not exist', 'templately'); | |
| 126 | + $response['message'] = 'Function themes_api does not exist'; | |
| 150 | 127 | return $response; |
| 151 | 128 | } |
| 152 | 129 | |
| 153 | 130 | $api = themes_api('theme_information', [ |
| @@ -161,13 +138,8 @@ | ||
| 161 | 138 | $response['message'] = $api->get_error_message(); |
| 162 | 139 | return $response; |
| 163 | 140 | } |
| 164 | 141 | |
| 165 | - $compatibility = $this->check_compatibility($api); | |
| 166 | - if (!$compatibility['success']) { | |
| 167 | - return $compatibility; | |
| 168 | - } | |
| 169 | - | |
| 170 | 142 | if (!wp_get_theme($theme_slug)->exists()) { |
| 171 | 143 | $upgrader = new Theme_Upgrader(new Automatic_Upgrader_Skin()); |
| 172 | 144 | $result = $upgrader->install($api->download_link); |
| 173 | 145 | |
| @@ -174,51 +146,23 @@ | ||
| 174 | 146 | if (is_wp_error($result)) { |
| 175 | 147 | $response['message'] = $result->get_error_message(); |
| 176 | 148 | return $response; |
| 177 | 149 | } |
| 178 | - else if($result !== true){ | |
| 179 | - $response['message'] = __('Failed to install theme', 'templately'); | |
| 180 | - return $response; | |
| 181 | - } | |
| 182 | 150 | |
| 183 | 151 | $response['install'] = 'success'; |
| 184 | 152 | } |
| 185 | 153 | |
| 186 | - $activate_status = $this->activate_theme($theme_slug); | |
| 154 | + switch_theme($theme_slug); | |
| 187 | 155 | |
| 188 | - if ( is_wp_error( $activate_status ) ) { | |
| 189 | - $response['message'] = $activate_status->get_error_message(); | |
| 190 | - } | |
| 191 | - else if ($activate_status) { | |
| 156 | + if ($theme_slug == get_option('stylesheet')) { | |
| 192 | 157 | $response['success'] = true; |
| 193 | 158 | } else { |
| 194 | - $response['message'] = __('Failed to activate theme', 'templately'); | |
| 159 | + $response['message'] = 'Failed to activate theme'; | |
| 195 | 160 | } |
| 196 | 161 | |
| 197 | 162 | return $response; |
| 198 | 163 | } |
| 199 | 164 | |
| 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 | 165 | private function activate_plugin( $file ) { |
| 222 | 166 | if ( is_plugin_active( $file ) ) { |
| 223 | 167 | return true; |
| 224 | 168 | } |
| @@ -228,29 +172,8 @@ | ||
| 228 | 172 | if ( is_wp_error( $result ) ) { |
| 229 | 173 | return $result; |
| 230 | 174 | } else { |
| 231 | 175 | 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 | 176 | } |
| 254 | 177 | } |
| 255 | 178 | |
| 256 | 179 | return false; |