| @@ -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,29 +47,18 @@ | ||
| 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 | - $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 | 51 | /** |
| 74 | 52 | * @var array|object $api |
| 75 | 53 | */ |
| 76 | - $api = plugins_api( 'plugin_information', $plugins_api_args); | |
| 54 | + $api = plugins_api( 'plugin_information', [ | |
| 55 | + 'slug' => sanitize_key( wp_unslash( $plugin['slug'] ) ), | |
| 56 | + 'fields' => [ | |
| 57 | + 'sections' => false, | |
| 58 | + ], | |
| 59 | + ] ); | |
| 77 | 60 | |
| 78 | - | |
| 79 | 61 | if ( is_wp_error( $api ) ) { |
| 80 | 62 | $response['message'] = $api->get_error_message(); |
| 81 | 63 | |
| 82 | 64 | return $response; |
| @@ -81,13 +63,8 @@ | ||
| 81 | 63 | |
| 82 | 64 | return $response; |
| 83 | 65 | } |
| 84 | 66 | |
| 85 | - $compatibility = $this->check_compatibility($api); | |
| 86 | - if (!$compatibility['success']) { | |
| 87 | - return $compatibility; | |
| 88 | - } | |
| 89 | - | |
| 90 | 67 | $response['name'] = $api->name; |
| 91 | 68 | |
| 92 | 69 | $skin = new WP_Ajax_Upgrader_Skin(); |
| 93 | 70 | $upgrader = new Plugin_Upgrader( $skin ); |
| @@ -117,23 +94,13 @@ | ||
| 117 | 94 | } |
| 118 | 95 | |
| 119 | 96 | return $response; |
| 120 | 97 | } |
| 121 | - else if($result !== true){ | |
| 122 | - $response['message'] = __('Failed to install plugin', 'templately'); | |
| 123 | - return $response; | |
| 124 | - } | |
| 125 | 98 | |
| 126 | 99 | $install_status = install_plugin_install_status( $api ); |
| 127 | 100 | $plugin['plugin_file'] = $install_status['file']; |
| 128 | 101 | } |
| 129 | 102 | |
| 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 | 103 | $activate_status = $this->activate_plugin( $plugin['plugin_file'] ); |
| 137 | 104 | |
| 138 | 105 | if ( is_wp_error( $activate_status ) ) { |
| 139 | 106 | $response['message'] = $activate_status->get_error_message(); |
| @@ -154,17 +121,10 @@ | ||
| 154 | 121 | require_once(ABSPATH . 'wp-admin/includes/theme-install.php'); |
| 155 | 122 | |
| 156 | 123 | $response = ['success' => false]; |
| 157 | 124 | |
| 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 | 125 | if (!function_exists('themes_api')) { |
| 166 | - $response['message'] = __('Function themes_api does not exist', 'templately'); | |
| 126 | + $response['message'] = 'Function themes_api does not exist'; | |
| 167 | 127 | return $response; |
| 168 | 128 | } |
| 169 | 129 | |
| 170 | 130 | $api = themes_api('theme_information', [ |
| @@ -178,13 +138,8 @@ | ||
| 178 | 138 | $response['message'] = $api->get_error_message(); |
| 179 | 139 | return $response; |
| 180 | 140 | } |
| 181 | 141 | |
| 182 | - $compatibility = $this->check_compatibility($api); | |
| 183 | - if (!$compatibility['success']) { | |
| 184 | - return $compatibility; | |
| 185 | - } | |
| 186 | - | |
| 187 | 142 | if (!wp_get_theme($theme_slug)->exists()) { |
| 188 | 143 | $upgrader = new Theme_Upgrader(new Automatic_Upgrader_Skin()); |
| 189 | 144 | $result = $upgrader->install($api->download_link); |
| 190 | 145 | |
| @@ -191,83 +146,34 @@ | ||
| 191 | 146 | if (is_wp_error($result)) { |
| 192 | 147 | $response['message'] = $result->get_error_message(); |
| 193 | 148 | return $response; |
| 194 | 149 | } |
| 195 | - else if($result !== true){ | |
| 196 | - $response['message'] = __('Failed to install theme', 'templately'); | |
| 197 | - return $response; | |
| 198 | - } | |
| 199 | 150 | |
| 200 | 151 | $response['install'] = 'success'; |
| 201 | 152 | } |
| 202 | 153 | |
| 203 | - $activate_status = $this->activate_theme($theme_slug); | |
| 154 | + switch_theme($theme_slug); | |
| 204 | 155 | |
| 205 | - if ( is_wp_error( $activate_status ) ) { | |
| 206 | - $response['message'] = $activate_status->get_error_message(); | |
| 207 | - } | |
| 208 | - else if ($activate_status) { | |
| 156 | + if ($theme_slug == get_option('stylesheet')) { | |
| 209 | 157 | $response['success'] = true; |
| 210 | 158 | } else { |
| 211 | - $response['message'] = __('Failed to activate theme', 'templately'); | |
| 159 | + $response['message'] = 'Failed to activate theme'; | |
| 212 | 160 | } |
| 213 | 161 | |
| 214 | 162 | return $response; |
| 215 | 163 | } |
| 216 | 164 | |
| 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 | 165 | private function activate_plugin( $file ) { |
| 239 | 166 | if ( is_plugin_active( $file ) ) { |
| 240 | 167 | return true; |
| 241 | 168 | } |
| 242 | 169 | |
| 243 | - if ( Helper::current_user_can( 'activate_plugins' ) && is_plugin_inactive( $file ) ) { | |
| 170 | + if ( current_user_can( 'activate_plugin', $file ) && is_plugin_inactive( $file ) ) { | |
| 244 | 171 | $result = activate_plugin( $file ); |
| 245 | 172 | if ( is_wp_error( $result ) ) { |
| 246 | 173 | return $result; |
| 247 | 174 | } else { |
| 248 | 175 | 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 | 176 | } |
| 271 | 177 | } |
| 272 | 178 | |
| 273 | 179 | return false; |