| @@ -34,20 +34,13 @@ | ||
| 34 | 34 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 35 | 35 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 36 | 36 | include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 37 | 37 | |
| 38 | - $response = [ 'success' => false, 'message' => '' ]; | |
| 38 | + $response = [ 'success' => false ]; | |
| 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'; |
| @@ -54,23 +47,17 @@ | ||
| 54 | 47 | } |
| 55 | 48 | } |
| 56 | 49 | |
| 57 | 50 | 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 | 51 | /** |
| 65 | 52 | * @var array|object $api |
| 66 | 53 | */ |
| 67 | 54 | $api = plugins_api( 'plugin_information', [ |
| 68 | - 'slug' => sanitize_key( wp_unslash( $plugin['slug'] ) ), | |
| 69 | - 'fields' => [ | |
| 70 | - 'sections' => false, | |
| 71 | - ], | |
| 72 | - ] ); | |
| 55 | + 'slug' => sanitize_key( wp_unslash( $plugin['slug'] ) ), | |
| 56 | + 'fields' => [ | |
| 57 | + 'sections' => false, | |
| 58 | + ], | |
| 59 | + ] ); | |
| 73 | 60 | |
| 74 | 61 | if ( is_wp_error( $api ) ) { |
| 75 | 62 | $response['message'] = $api->get_error_message(); |
| 76 | 63 | |
| @@ -76,13 +63,8 @@ | ||
| 76 | 63 | |
| 77 | 64 | return $response; |
| 78 | 65 | } |
| 79 | 66 | |
| 80 | - $compatibility = $this->check_compatibility($api); | |
| 81 | - if (!$compatibility['success']) { | |
| 82 | - return $compatibility; | |
| 83 | - } | |
| 84 | - | |
| 85 | 67 | $response['name'] = $api->name; |
| 86 | 68 | |
| 87 | 69 | $skin = new WP_Ajax_Upgrader_Skin(); |
| 88 | 70 | $upgrader = new Plugin_Upgrader( $skin ); |
| @@ -112,23 +94,13 @@ | ||
| 112 | 94 | } |
| 113 | 95 | |
| 114 | 96 | return $response; |
| 115 | 97 | } |
| 116 | - else if($result !== true){ | |
| 117 | - $response['message'] = __('Failed to install plugin', 'templately'); | |
| 118 | - return $response; | |
| 119 | - } | |
| 120 | 98 | |
| 121 | 99 | $install_status = install_plugin_install_status( $api ); |
| 122 | 100 | $plugin['plugin_file'] = $install_status['file']; |
| 123 | 101 | } |
| 124 | 102 | |
| 125 | - if ( !Helper::current_user_can( 'activate_plugins' ) && is_plugin_inactive( $file ) ) { | |
| 126 | - $response['code'] = 'invalid_requirements'; | |
| 127 | - $response['message'] = __( 'Sorry, you do not have permission to activate a plugin.', 'templately' ); | |
| 128 | - return $response; | |
| 129 | - } | |
| 130 | - | |
| 131 | 103 | $activate_status = $this->activate_plugin( $plugin['plugin_file'] ); |
| 132 | 104 | |
| 133 | 105 | if ( is_wp_error( $activate_status ) ) { |
| 134 | 106 | $response['message'] = $activate_status->get_error_message(); |
| @@ -149,17 +121,10 @@ | ||
| 149 | 121 | require_once(ABSPATH . 'wp-admin/includes/theme-install.php'); |
| 150 | 122 | |
| 151 | 123 | $response = ['success' => false]; |
| 152 | 124 | |
| 153 | - // Check if the theme is already active | |
| 154 | - if ($theme_slug == get_option('stylesheet')) { | |
| 155 | - $response['success'] = true; | |
| 156 | - $response['message'] = __('Theme is already active', 'templately'); | |
| 157 | - return $response; | |
| 158 | - } | |
| 159 | - | |
| 160 | 125 | if (!function_exists('themes_api')) { |
| 161 | - $response['message'] = __('Function themes_api does not exist', 'templately'); | |
| 126 | + $response['message'] = 'Function themes_api does not exist'; | |
| 162 | 127 | return $response; |
| 163 | 128 | } |
| 164 | 129 | |
| 165 | 130 | $api = themes_api('theme_information', [ |
| @@ -173,13 +138,8 @@ | ||
| 173 | 138 | $response['message'] = $api->get_error_message(); |
| 174 | 139 | return $response; |
| 175 | 140 | } |
| 176 | 141 | |
| 177 | - $compatibility = $this->check_compatibility($api); | |
| 178 | - if (!$compatibility['success']) { | |
| 179 | - return $compatibility; | |
| 180 | - } | |
| 181 | - | |
| 182 | 142 | if (!wp_get_theme($theme_slug)->exists()) { |
| 183 | 143 | $upgrader = new Theme_Upgrader(new Automatic_Upgrader_Skin()); |
| 184 | 144 | $result = $upgrader->install($api->download_link); |
| 185 | 145 | |
| @@ -186,83 +146,34 @@ | ||
| 186 | 146 | if (is_wp_error($result)) { |
| 187 | 147 | $response['message'] = $result->get_error_message(); |
| 188 | 148 | return $response; |
| 189 | 149 | } |
| 190 | - else if($result !== true){ | |
| 191 | - $response['message'] = __('Failed to install theme', 'templately'); | |
| 192 | - return $response; | |
| 193 | - } | |
| 194 | 150 | |
| 195 | 151 | $response['install'] = 'success'; |
| 196 | 152 | } |
| 197 | 153 | |
| 198 | - $activate_status = $this->activate_theme($theme_slug); | |
| 154 | + switch_theme($theme_slug); | |
| 199 | 155 | |
| 200 | - if ( is_wp_error( $activate_status ) ) { | |
| 201 | - $response['message'] = $activate_status->get_error_message(); | |
| 202 | - } | |
| 203 | - else if ($activate_status) { | |
| 156 | + if ($theme_slug == get_option('stylesheet')) { | |
| 204 | 157 | $response['success'] = true; |
| 205 | 158 | } else { |
| 206 | - $response['message'] = __('Failed to activate theme', 'templately'); | |
| 159 | + $response['message'] = 'Failed to activate theme'; | |
| 207 | 160 | } |
| 208 | 161 | |
| 209 | 162 | return $response; |
| 210 | 163 | } |
| 211 | 164 | |
| 212 | - public function check_compatibility($api) { | |
| 213 | - // Check compatibility with current PHP version | |
| 214 | - if (version_compare(PHP_VERSION, $api->requires_php, '<')) { | |
| 215 | - return [ | |
| 216 | - 'success' => false, | |
| 217 | - '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) | |
| 218 | - ]; | |
| 219 | - } | |
| 220 | - | |
| 221 | - // Check compatibility with current WP version | |
| 222 | - global $wp_version; | |
| 223 | - if (version_compare($wp_version, $api->requires, '<')) { | |
| 224 | - return [ | |
| 225 | - 'success' => false, | |
| 226 | - 'message' => sprintf(__('The plugin requires WordPress version %s or higher. You are running version %s.', 'templately'), $api->requires, $wp_version) | |
| 227 | - ]; | |
| 228 | - } | |
| 229 | - | |
| 230 | - return ['success' => true]; | |
| 231 | - } | |
| 232 | - | |
| 233 | 165 | private function activate_plugin( $file ) { |
| 234 | 166 | if ( is_plugin_active( $file ) ) { |
| 235 | 167 | return true; |
| 236 | 168 | } |
| 237 | 169 | |
| 238 | - if ( Helper::current_user_can( 'activate_plugins' ) && is_plugin_inactive( $file ) ) { | |
| 170 | + if ( current_user_can( 'activate_plugin', $file ) && is_plugin_inactive( $file ) ) { | |
| 239 | 171 | $result = activate_plugin( $file ); |
| 240 | 172 | if ( is_wp_error( $result ) ) { |
| 241 | 173 | return $result; |
| 242 | 174 | } else { |
| 243 | 175 | return true; |
| 244 | - } | |
| 245 | - } | |
| 246 | - | |
| 247 | - return false; | |
| 248 | - } | |
| 249 | - | |
| 250 | - private function activate_theme($theme_slug) { | |
| 251 | - if (get_option('stylesheet') == $theme_slug) { | |
| 252 | - // The theme is already active | |
| 253 | - return true; | |
| 254 | - } | |
| 255 | - | |
| 256 | - if (Helper::current_user_can('switch_themes')) { | |
| 257 | - // Activate the theme | |
| 258 | - switch_theme($theme_slug); | |
| 259 | - | |
| 260 | - // Check if the theme was successfully activated | |
| 261 | - if (get_option('stylesheet') == $theme_slug) { | |
| 262 | - return true; | |
| 263 | - } else { | |
| 264 | - return new \WP_Error('theme_activation_failed', __('Failed to activate theme', 'templately')); | |
| 265 | 176 | } |
| 266 | 177 | } |
| 267 | 178 | |
| 268 | 179 | return false; |