class-evf-modules.php
| 1 | <?php |
| 2 | /** |
| 3 | * Modules controller class. |
| 4 | * |
| 5 | * @since 3.0.0 |
| 6 | * |
| 7 | * @package EverestFroms/Classes |
| 8 | */ |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * EVF_Modules Class |
| 14 | */ |
| 15 | class EVF_Modules { |
| 16 | /** |
| 17 | * Endpoint namespace. |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | protected $namespace = 'everest-forms/v1'; |
| 22 | |
| 23 | /** |
| 24 | * Route base. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | protected $rest_base = 'modules'; |
| 29 | |
| 30 | /** |
| 31 | * Register routes. |
| 32 | * |
| 33 | * @since 3.0.0 |
| 34 | * |
| 35 | * @return void |
| 36 | */ |
| 37 | public function register_routes() { |
| 38 | register_rest_route( |
| 39 | $this->namespace, |
| 40 | '/' . $this->rest_base, |
| 41 | array( |
| 42 | 'methods' => 'GET', |
| 43 | 'callback' => array( __CLASS__, 'get_modules' ), |
| 44 | 'permission_callback' => array( __CLASS__, 'check_admin_plugin_activation_permissions' ), |
| 45 | ) |
| 46 | ); |
| 47 | register_rest_route( |
| 48 | $this->namespace, |
| 49 | '/' . $this->rest_base . '/activate', |
| 50 | array( |
| 51 | 'methods' => 'POST', |
| 52 | 'callback' => array( __CLASS__, 'activate_module' ), |
| 53 | 'permission_callback' => array( __CLASS__, 'check_admin_plugin_activation_permissions' ), |
| 54 | ) |
| 55 | ); |
| 56 | register_rest_route( |
| 57 | $this->namespace, |
| 58 | '/' . $this->rest_base . '/deactivate', |
| 59 | array( |
| 60 | 'methods' => 'POST', |
| 61 | 'callback' => array( __CLASS__, 'deactivate_module' ), |
| 62 | 'permission_callback' => array( __CLASS__, 'check_admin_plugin_activation_permissions' ), |
| 63 | ) |
| 64 | ); |
| 65 | |
| 66 | register_rest_route( |
| 67 | $this->namespace, |
| 68 | '/' . $this->rest_base . '/bulk-activate', |
| 69 | array( |
| 70 | 'methods' => 'POST', |
| 71 | 'callback' => array( __CLASS__, 'bulk_activate_modules' ), |
| 72 | 'permission_callback' => array( __CLASS__, 'check_admin_plugin_activation_permissions' ), |
| 73 | ) |
| 74 | ); |
| 75 | register_rest_route( |
| 76 | $this->namespace, |
| 77 | '/' . $this->rest_base . '/bulk-deactivate', |
| 78 | array( |
| 79 | 'methods' => 'POST', |
| 80 | 'callback' => array( __CLASS__, 'bulk_deactivate_modules' ), |
| 81 | 'permission_callback' => array( __CLASS__, 'check_admin_plugin_activation_permissions' ), |
| 82 | ) |
| 83 | ); |
| 84 | register_rest_route( |
| 85 | $this->namespace, |
| 86 | '/' . $this->rest_base . '/activate-license', |
| 87 | array( |
| 88 | 'methods' => 'POST', |
| 89 | 'callback' => array( __CLASS__, 'activate_license' ), |
| 90 | 'permission_callback' => array( __CLASS__, 'check_admin_plugin_activation_permissions' ), |
| 91 | ) |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get Addons Lists. |
| 97 | * |
| 98 | * @since 3.0.0 |
| 99 | * |
| 100 | * @return array Module lists. |
| 101 | */ |
| 102 | public static function get_modules() { |
| 103 | $extension_data = self::get_extensions_data(); |
| 104 | $features_lists = $extension_data->features; |
| 105 | |
| 106 | $enabled_features = get_option( 'everest_forms_enabled_features', array() ); |
| 107 | foreach ( $features_lists as $key => $feature ) { |
| 108 | if ( in_array( $feature->slug, $enabled_features, true ) ) { |
| 109 | $feature->status = 'active'; |
| 110 | } else { |
| 111 | $feature->status = 'inactive'; |
| 112 | } |
| 113 | $feature->link = $feature->link . '&utm_campaign=' . EVF()->utm_campaign; |
| 114 | $feature->type = 'feature'; |
| 115 | $features_lists[ $key ] = $feature; |
| 116 | if ( in_array( $feature->slug, array( 'everest-forms-oxygen-builder', 'everest-forms-bricks-builder', 'everest-forms-divi-builder', 'everest-forms-beaver-builder', 'everest-forms-wpbakery-builder', 'everest-forms-style-customizer' ), true ) ) { |
| 117 | $feature->required_plan = esc_html__( 'Free', 'everest-forms' ); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Get Addons Lists. |
| 122 | $addons_lists = $extension_data->products; |
| 123 | |
| 124 | if ( ! function_exists( 'get_plugins' ) ) { |
| 125 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 126 | } |
| 127 | $installed_plugin_slugs = array_keys( get_plugins() ); |
| 128 | |
| 129 | foreach ( $addons_lists as $key => $addon ) { |
| 130 | $addon_file = $addon->slug . '/' . $addon->slug . '.php'; |
| 131 | if ( in_array( $addon_file, $installed_plugin_slugs, true ) ) { |
| 132 | if ( is_plugin_active( $addon_file ) ) { |
| 133 | $addon->status = 'active'; |
| 134 | } else { |
| 135 | $addon->status = 'inactive'; |
| 136 | } |
| 137 | } else { |
| 138 | $addon->status = 'not-installed'; |
| 139 | } |
| 140 | |
| 141 | if ( in_array( 'personal', $addon->plan, true ) ) { |
| 142 | $addon->required_plan = __( 'Pro', 'everest-forms' ); |
| 143 | } |
| 144 | $addon->link = $addon->link . '&utm_campaign=' . EVF()->utm_campaign; |
| 145 | $addon->type = 'addon'; |
| 146 | $addons_lists[ $key ] = $addon; |
| 147 | } |
| 148 | |
| 149 | $modules_lists = array_merge( $features_lists, $addons_lists ); |
| 150 | |
| 151 | return new \WP_REST_Response( |
| 152 | array( |
| 153 | 'success' => true, |
| 154 | 'modules_lists' => $modules_lists, |
| 155 | ), |
| 156 | 200 |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Active a module. |
| 162 | * |
| 163 | * @since 3.0.0 |
| 164 | * |
| 165 | * @param WP_REST_Request $request Full details about the request. |
| 166 | * |
| 167 | * @return WP_Error|WP_REST_Response |
| 168 | */ |
| 169 | public static function activate_module( $request ) { |
| 170 | if ( ! isset( $request['slug'] ) || empty( trim( $request['slug'] ) ) ) { //phpcs:ignore |
| 171 | |
| 172 | return new \WP_REST_Response( |
| 173 | array( |
| 174 | 'success' => false, |
| 175 | 'message' => esc_html__( 'Module slug is a required field', 'everest-forms' ), |
| 176 | ), |
| 177 | 400 |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | $slug = is_array( $request['slug'] ) ? current( $request['slug'] ) : $request['slug']; |
| 182 | $type = isset( $request['type'] ) ? $request['type'] : ''; |
| 183 | |
| 184 | $slug = sanitize_key( wp_unslash( $request['name'] ) ); |
| 185 | $name = sanitize_text_field( $request['name'] ); |
| 186 | $plugin_slug = wp_unslash( $request['slug'] ) . '/' . wp_unslash( $request['slug'] ) . '.php'; // phpcs:ignore |
| 187 | $plugin = plugin_basename( sanitize_text_field( $plugin_slug ) ); |
| 188 | |
| 189 | $status = array(); |
| 190 | |
| 191 | if ( 'addon' === $type ) { |
| 192 | $status = self::install_addons( $slug, $name, $plugin ); |
| 193 | } else { |
| 194 | $status = self::enable_feature( $request['slug'] ); |
| 195 | } |
| 196 | |
| 197 | if ( isset( $status['success'] ) && ! $status['success'] ) { |
| 198 | |
| 199 | return new \WP_REST_Response( |
| 200 | array( |
| 201 | 'success' => false, |
| 202 | 'message' => __( "Module couldn't be activated at the moment. Please try again later.", 'everest-forms' ), |
| 203 | ), |
| 204 | 400 |
| 205 | ); |
| 206 | } else { |
| 207 | return new \WP_REST_Response( |
| 208 | array( |
| 209 | 'success' => true, |
| 210 | 'message' => __( 'Module activated successfully', 'everest-forms' ), |
| 211 | ), |
| 212 | 200 |
| 213 | ); |
| 214 | } |
| 215 | } |
| 216 | /** |
| 217 | * Handler for installing or activating a addon. |
| 218 | * |
| 219 | * @since 3.0.0 |
| 220 | * |
| 221 | * @param string $slug Slug of the addon to install/activate. |
| 222 | * @param string $name Name of the addon to install/activate. |
| 223 | * @param string $plugin Basename of the addon to install/activate. |
| 224 | * |
| 225 | * @see Plugin_Upgrader |
| 226 | * |
| 227 | * @global WP_Filesystem_Base $wp_filesystem Subclass |
| 228 | */ |
| 229 | public static function install_addons( $slug, $name, $plugin ) { |
| 230 | |
| 231 | $status = array( |
| 232 | 'install' => 'plugin', |
| 233 | 'slug' => $slug, |
| 234 | ); |
| 235 | |
| 236 | $status = self::install_individual_addon( $slug, $plugin, $name, $status ); |
| 237 | |
| 238 | return $status; |
| 239 | } |
| 240 | /** |
| 241 | * Enable a feature. |
| 242 | * |
| 243 | * @since 3.0.0 |
| 244 | * |
| 245 | * @param string $slug Slug of the feature to enable. |
| 246 | * |
| 247 | * @return WP_Error|WP_REST_Response |
| 248 | */ |
| 249 | public static function enable_feature( $slug ) { |
| 250 | // Logic to enable Feature. |
| 251 | $enabled_features = get_option( 'everest_forms_enabled_features', array() ); |
| 252 | array_push( $enabled_features, $slug ); |
| 253 | update_option( 'everest_forms_enabled_features', $enabled_features ); |
| 254 | |
| 255 | /** |
| 256 | * Track module installation. |
| 257 | * |
| 258 | * @since 3.0.5 |
| 259 | */ |
| 260 | do_action( 'evf_feature_track_data_for_tg_user_tracking', $slug ); |
| 261 | |
| 262 | return array( 'success' => true ); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Handler for installing a extension. |
| 267 | * |
| 268 | * @since 3.0.0 |
| 269 | * |
| 270 | * @param string $slug Slug of the addon to install. |
| 271 | * @param string $plugin Plugin file of the addon to install. |
| 272 | * @param string $name Name of the addon to install. |
| 273 | * @param array $status Staus array to track addon installation status. |
| 274 | * |
| 275 | * @see Plugin_Upgrader |
| 276 | * |
| 277 | * @global WP_Filesystem_Base $wp_filesystem Subclass |
| 278 | */ |
| 279 | public static function install_individual_addon( $slug, $plugin, $name, $status ) { |
| 280 | require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 281 | include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 282 | include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 283 | |
| 284 | if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin ) ) { |
| 285 | $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); |
| 286 | $status['plugin'] = $plugin; |
| 287 | $status['pluginName'] = $plugin_data['Name']; |
| 288 | |
| 289 | if ( is_plugin_inactive( $plugin ) ) { |
| 290 | $result = activate_plugin( $plugin ); |
| 291 | |
| 292 | if ( is_wp_error( $result ) ) { |
| 293 | $status['errorCode'] = $result->get_error_code(); |
| 294 | $status['errorMessage'] = $result->get_error_message(); |
| 295 | $status['success'] = false; |
| 296 | return $status; |
| 297 | } |
| 298 | $status['success'] = true; |
| 299 | $status['message'] = __( 'Addons activated successfully', 'everest-forms' ); |
| 300 | return $status; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | if ( 'aicontactform' === $slug && ! evf_string_to_bool( evf_get_license_plan() ) ) { |
| 305 | $args = array( |
| 306 | 'slug' => 'ai-contact-form', |
| 307 | 'fields' => array( |
| 308 | 'short_description' => true, |
| 309 | 'sections' => true, |
| 310 | 'requires' => true, |
| 311 | 'tested' => true, |
| 312 | 'rating' => true, |
| 313 | 'downloaded' => true, |
| 314 | 'last_updated' => true, |
| 315 | 'added' => true, |
| 316 | 'tags' => true, |
| 317 | 'homepage' => true, |
| 318 | 'donate_link' => true, |
| 319 | 'reviews' => true, |
| 320 | 'download_link' => true, |
| 321 | 'screenshots' => true, |
| 322 | 'active_installs' => true, |
| 323 | 'version' => true, |
| 324 | ), |
| 325 | ); |
| 326 | $api = plugins_api( 'plugin_information', $args ); |
| 327 | } else { |
| 328 | $api = json_decode( |
| 329 | EVF_Updater_Key_API::version( |
| 330 | array( |
| 331 | 'license' => get_option( 'everest-forms-pro_license_key' ), |
| 332 | 'item_name' => ! empty( $name ) ? sanitize_text_field( wp_unslash( $name ) ) : '', |
| 333 | ) |
| 334 | ) |
| 335 | ); |
| 336 | } |
| 337 | |
| 338 | if ( is_wp_error( $api ) ) { |
| 339 | $status['success'] = false; |
| 340 | $status['errorMessage'] = $api['msg']; |
| 341 | return $status; |
| 342 | } |
| 343 | |
| 344 | $status['pluginName'] = $api->name; |
| 345 | |
| 346 | $skin = new WP_Ajax_Upgrader_Skin(); |
| 347 | $upgrader = new Plugin_Upgrader( $skin ); |
| 348 | $result = $upgrader->install( $api->download_link ); |
| 349 | |
| 350 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 351 | $status['debug'] = $skin->get_upgrade_messages(); |
| 352 | } |
| 353 | |
| 354 | if ( is_wp_error( $result ) ) { |
| 355 | $status['success'] = false; |
| 356 | $status['errorCode'] = $result->get_error_code(); |
| 357 | $status['errorMessage'] = $result->get_error_message(); |
| 358 | return $status; |
| 359 | } elseif ( is_wp_error( $skin->result ) ) { |
| 360 | $status['success'] = false; |
| 361 | $status['errorCode'] = $skin->result->get_error_code(); |
| 362 | $status['errorMessage'] = $skin->result->get_error_message(); |
| 363 | return $status; |
| 364 | } elseif ( $skin->get_errors()->get_error_code() ) { |
| 365 | $status['success'] = false; |
| 366 | $status['errorMessage'] = $skin->get_error_messages(); |
| 367 | return $status; |
| 368 | } elseif ( is_null( $result ) ) { |
| 369 | global $wp_filesystem; |
| 370 | $status['success'] = false; |
| 371 | $status['errorCode'] = 'unable_to_connect_to_filesystem'; |
| 372 | $status['errorMessage'] = esc_html__( 'Unable to connect to the filesystem. Please confirm your credentials.', 'everest-forms' ); |
| 373 | |
| 374 | // Pass through the error from WP_Filesystem if one was raised. |
| 375 | if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { |
| 376 | $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); |
| 377 | } |
| 378 | return $status; |
| 379 | } |
| 380 | |
| 381 | $api->version = isset( $api->new_version ) ? $api->new_version : ''; |
| 382 | $install_status = install_plugin_install_status( $api ); |
| 383 | activate_plugin( $plugin ); |
| 384 | $status['success'] = true; |
| 385 | $status['message'] = __( 'Addon installed Successfully', 'everest-forms' ); |
| 386 | return $status; |
| 387 | } |
| 388 | /** |
| 389 | * Deactive a module. |
| 390 | * |
| 391 | * @since 3.0.0 |
| 392 | * |
| 393 | * @param WP_REST_Request $request Full details about the request. |
| 394 | * |
| 395 | * @return WP_Error|WP_REST_Response |
| 396 | */ |
| 397 | public static function deactivate_module( $request ) { |
| 398 | if ( ! isset( $request['slug'] ) || empty( trim( $request['slug'] ) ) ) { //phpcs:ignore |
| 399 | |
| 400 | return new \WP_REST_Response( |
| 401 | array( |
| 402 | 'success' => false, |
| 403 | 'message' => esc_html__( 'Addon slug is a required field', 'everest-forms' ), |
| 404 | ), |
| 405 | 400 |
| 406 | ); |
| 407 | } |
| 408 | |
| 409 | $slug = is_array( $request['slug'] ) ? current( $request['slug'] ) : $request['slug']; |
| 410 | $type = isset( $request['type'] ) ? $request['type'] : ''; |
| 411 | |
| 412 | $status = array(); |
| 413 | |
| 414 | if ( 'addon' === $type ) { |
| 415 | $slug = $slug . '/' . $slug . '.php'; |
| 416 | $status = self::deactivate_addon( $slug ); |
| 417 | } else { |
| 418 | $status = self::disable_feature( $slug ); |
| 419 | } |
| 420 | |
| 421 | if ( isset( $status['success'] ) && ! $status['success'] ) { |
| 422 | return new \WP_REST_Response( |
| 423 | array( |
| 424 | 'success' => false, |
| 425 | 'message' => esc_html__( "Module couldn't be deactivated. Please try again later.", 'everest-forms' ), |
| 426 | ), |
| 427 | 400 |
| 428 | ); |
| 429 | } else { |
| 430 | return new \WP_REST_Response( |
| 431 | array( |
| 432 | 'success' => true, |
| 433 | 'message' => esc_html__( 'Module deactivated successfully', 'everest-forms' ), |
| 434 | ), |
| 435 | 200 |
| 436 | ); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Deactive a addon. |
| 442 | * |
| 443 | * @since 3.0.0 |
| 444 | * |
| 445 | * @param string $slug Slug of the addon to deactivate. |
| 446 | * |
| 447 | * @return WP_Error|WP_REST_Response |
| 448 | */ |
| 449 | public static function deactivate_addon( $slug ) { |
| 450 | deactivate_plugins( $slug ); |
| 451 | $active_plugins = get_option( 'active_plugins', array() ); |
| 452 | |
| 453 | return in_array( $slug, $active_plugins, true ) ? array( 'success' => false ) : array( 'success' => true ); |
| 454 | } |
| 455 | /** |
| 456 | * Disable a feature. |
| 457 | * |
| 458 | * @since 3.0.0 |
| 459 | * |
| 460 | * @param string $slug Slug of the feature to disable. |
| 461 | * |
| 462 | * @return WP_Error|WP_REST_Response |
| 463 | */ |
| 464 | public static function disable_feature( $slug ) { |
| 465 | |
| 466 | // Logic to disable Feature. |
| 467 | $enabled_features = get_option( 'everest_forms_enabled_features', array() ); |
| 468 | $enabled_features = array_values( array_diff( $enabled_features, array( $slug ) ) ); |
| 469 | update_option( 'everest_forms_enabled_features', $enabled_features ); |
| 470 | |
| 471 | return in_array( $slug, $enabled_features, true ) ? array( 'success' => false ) : array( 'success' => true ); |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Bulk Activate modules. |
| 476 | * |
| 477 | * @since 3.0.0 |
| 478 | * |
| 479 | * @param WP_REST_Request $request Full details about the request. |
| 480 | * |
| 481 | * @return WP_Error|WP_REST_Response |
| 482 | */ |
| 483 | public static function bulk_activate_modules( $request ) { |
| 484 | |
| 485 | if ( ! isset( $request['moduleData'] ) || empty( $request['moduleData'] ) ) { |
| 486 | |
| 487 | return new \WP_REST_Response( |
| 488 | array( |
| 489 | 'success' => false, |
| 490 | 'message' => esc_html__( 'Please select addons to activate', 'everest-forms' ), |
| 491 | ), |
| 492 | 400 |
| 493 | ); |
| 494 | } |
| 495 | |
| 496 | $feature_slugs = array(); |
| 497 | $addon_slugs = array(); |
| 498 | |
| 499 | foreach ( $request['moduleData'] as $slug => $addon ) { |
| 500 | if ( 'addon' === $addon['type'] ) { |
| 501 | array_push( $addon_slugs, $addon ); |
| 502 | } else { |
| 503 | $feature_slugs[ $slug ] = $addon['name']; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | $failed_modules = array(); |
| 508 | |
| 509 | if ( ! empty( $addon_slugs ) ) { |
| 510 | $failed_modules = array_merge( $failed_modules, self::bulk_install_addons( $addon_slugs ) ); |
| 511 | } |
| 512 | |
| 513 | if ( ! empty( $feature_slugs ) ) { |
| 514 | $failed_modules = array_merge( $failed_modules, self::bulk_enable_feature( $feature_slugs ) ); |
| 515 | } |
| 516 | |
| 517 | if ( count( $failed_modules ) > 0 ) { |
| 518 | return new \WP_REST_Response( |
| 519 | array( |
| 520 | 'success' => false, |
| 521 | /* translators: 1: Failed Addon Names */ |
| 522 | 'message' => sprintf( __( '%1$s activation failed. Please try again sometime later.', 'everest-forms' ), implode( ', ', $failed_modules ) ), |
| 523 | ), |
| 524 | 400 |
| 525 | ); |
| 526 | } else { |
| 527 | return new \WP_REST_Response( |
| 528 | array( |
| 529 | 'success' => true, |
| 530 | 'message' => __( 'All of the selected modules have been activated successfully.', 'everest-forms' ), |
| 531 | ), |
| 532 | 200 |
| 533 | ); |
| 534 | } |
| 535 | } |
| 536 | /** |
| 537 | * Bulk Deactivate Modules. |
| 538 | * |
| 539 | * @since 3.0.0 |
| 540 | * |
| 541 | * @param WP_REST_Request $request Full details about the request. |
| 542 | * |
| 543 | * @return WP_Error|WP_REST_Response |
| 544 | */ |
| 545 | public static function bulk_deactivate_modules( $request ) { |
| 546 | |
| 547 | if ( ! isset( $request['moduleData'] ) || empty( $request['moduleData'] ) ) { |
| 548 | |
| 549 | return new \WP_REST_Response( |
| 550 | array( |
| 551 | 'success' => false, |
| 552 | 'message' => esc_html__( 'Please select a module to deactivate', 'everest-forms' ), |
| 553 | ), |
| 554 | 400 |
| 555 | ); |
| 556 | } |
| 557 | |
| 558 | $feature_slugs = array(); |
| 559 | $addon_slugs = array(); |
| 560 | |
| 561 | foreach ( $request['moduleData'] as $slug => $module ) { |
| 562 | if ( isset( $module['type'] ) && 'addon' === $module['type'] ) { |
| 563 | array_push( $addon_slugs, $module['slug'] ); |
| 564 | } else { |
| 565 | array_push( $feature_slugs, $slug ); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | $deactivated_count = 0; |
| 570 | |
| 571 | if ( ! empty( $addon_slugs ) ) { |
| 572 | $deactivated_count += count( self::bulk_deactivate_addon( $addon_slugs ) ); |
| 573 | } |
| 574 | |
| 575 | if ( ! empty( $feature_slugs ) ) { |
| 576 | $deactivated_count += self::bulk_disable_feature( $feature_slugs ); |
| 577 | } |
| 578 | |
| 579 | if ( count( $request['moduleData'] ) === $deactivated_count ) { |
| 580 | return new \WP_REST_Response( |
| 581 | array( |
| 582 | 'success' => true, |
| 583 | 'message' => esc_html__( 'All of the selected modules have been deactivated.', 'everest-forms' ), |
| 584 | ), |
| 585 | 200 |
| 586 | ); |
| 587 | } else { |
| 588 | return new \WP_REST_Response( |
| 589 | array( |
| 590 | 'success' => false, |
| 591 | 'message' => esc_html__( 'Some of the selected modules may not have been deactivated. Please try again later', 'everest-forms' ), |
| 592 | ), |
| 593 | 400 |
| 594 | ); |
| 595 | } |
| 596 | } |
| 597 | /** |
| 598 | * Handler for installing bulk extension. |
| 599 | * |
| 600 | * @since 3.0.0 |
| 601 | * |
| 602 | * @param array $addon_data Datas of addons to activate. |
| 603 | * |
| 604 | * @see Plugin_Upgrader |
| 605 | * |
| 606 | * @global WP_Filesystem_Base $wp_filesystem Subclass |
| 607 | */ |
| 608 | public static function bulk_install_addons( $addon_data ) { |
| 609 | |
| 610 | $failed_addon = array(); |
| 611 | |
| 612 | foreach ( $addon_data as $addon ) { |
| 613 | $slug = isset( $addon['name'] ) ? sanitize_key( wp_unslash( $addon['name'] ) ) : ''; |
| 614 | $plugin_slug = isset( $addon['slug'] ) ? sanitize_text_field( $addon['slug'] ) : ''; |
| 615 | $name = isset( $addon['name'] ) ? sanitize_text_field( $addon['name'] ) : ''; |
| 616 | $plugin = plugin_basename( sanitize_text_field( $plugin_slug ) ); |
| 617 | $status = array( |
| 618 | 'install' => 'plugin', |
| 619 | 'slug' => $slug, |
| 620 | ); |
| 621 | $status = self::install_individual_addon( $slug, $plugin, $name, $status ); |
| 622 | |
| 623 | if ( isset( $status['success'] ) && '' === $status['success'] ) { |
| 624 | array_push( $failed_addon, $name ); |
| 625 | continue; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | return $failed_addon; |
| 630 | } |
| 631 | /** |
| 632 | * Bulk enable features. |
| 633 | * |
| 634 | * @since 3.0.0 |
| 635 | * |
| 636 | * @param array $feature_data Data of the features to enable. |
| 637 | */ |
| 638 | public static function bulk_enable_feature( $feature_data ) { |
| 639 | $failed_to_enable = array(); // Add Names of failed feature enable process. |
| 640 | |
| 641 | // Logic to enable Feature. |
| 642 | $enabled_features = get_option( 'everest_forms_enabled_features', array() ); |
| 643 | |
| 644 | foreach ( $feature_data as $slug => $name ) { |
| 645 | array_push( $enabled_features, $slug ); |
| 646 | } |
| 647 | |
| 648 | update_option( 'everest_forms_enabled_features', $enabled_features ); |
| 649 | |
| 650 | return $failed_to_enable; |
| 651 | } |
| 652 | /** |
| 653 | * Bulk Deactivate addons. |
| 654 | * |
| 655 | * @since 3.0.0 |
| 656 | * |
| 657 | * @param array $addon_slugs Slugs of the addons to deactivate. |
| 658 | */ |
| 659 | public static function bulk_deactivate_addon( $addon_slugs ) { |
| 660 | |
| 661 | deactivate_plugins( $addon_slugs ); |
| 662 | |
| 663 | $active_plugins = get_option( 'active_plugins', array() ); |
| 664 | |
| 665 | return array_diff( $addon_slugs, $active_plugins ); |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * Bulk disable features. |
| 670 | * |
| 671 | * @since 3.2.0 |
| 672 | * |
| 673 | * @param array $feature_slugs Slugs of the features to disable. |
| 674 | */ |
| 675 | public static function bulk_disable_feature( $feature_slugs ) { |
| 676 | |
| 677 | // Logic to enable Feature. |
| 678 | $enabled_features = get_option( 'everest_forms_enabled_features', array() ); |
| 679 | $enabled_features = array_values( array_diff( $enabled_features, $feature_slugs ) ); |
| 680 | update_option( 'everest_forms_enabled_features', $enabled_features ); |
| 681 | |
| 682 | return count( $feature_slugs ); |
| 683 | } |
| 684 | |
| 685 | /** |
| 686 | * Get section content for the extensions screen. |
| 687 | * |
| 688 | * @return array |
| 689 | */ |
| 690 | public static function get_extensions_data() { |
| 691 | |
| 692 | $extension_data = evf_get_json_file_contents( 'assets/extensions-json/sections/all_extensions.json' ); |
| 693 | return apply_filters( 'everest_forms_extensions_section_data', $extension_data ); |
| 694 | } |
| 695 | /** |
| 696 | * Check if a given request has access to update a setting |
| 697 | * |
| 698 | * @param WP_REST_Request $request Full data about the request. |
| 699 | * @return WP_Error|bool |
| 700 | */ |
| 701 | public static function check_admin_plugin_activation_permissions( $request ) { |
| 702 | return current_user_can( 'activate_plugin' ); |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * Check if a given request has access to update a setting |
| 707 | * |
| 708 | * @param WP_REST_Request $request Full data about the request. |
| 709 | * @return WP_Error|bool |
| 710 | */ |
| 711 | public static function check_admin_plugin_installation_permissions( $request ) { |
| 712 | return current_user_can( 'install_plugins' ) && current_user_can( 'activate_plugin' ); |
| 713 | } |
| 714 | |
| 715 | /** |
| 716 | * Activate the plugin license. |
| 717 | * |
| 718 | * @since 3.0.1 |
| 719 | * |
| 720 | * @param WP_REST_Request $request Full details about the request. |
| 721 | * |
| 722 | * @return WP_Error|WP_REST_Response |
| 723 | */ |
| 724 | public static function activate_license( $request ) { |
| 725 | if ( isset( $request['licenseActivationKey'] ) ) { |
| 726 | $evf_dashboard_plugin_updater = new EVF_Plugin_Updater(); |
| 727 | $evf_dashboard_plugin_activator = $evf_dashboard_plugin_updater->activate_license( $request['licenseActivationKey'] ); |
| 728 | |
| 729 | if ( isset( $evf_dashboard_plugin_activator ) && $evf_dashboard_plugin_activator ) { |
| 730 | return new \WP_REST_Response( |
| 731 | array( |
| 732 | 'status' => true, |
| 733 | 'message' => esc_html__( 'Everest Forms Pro activated successfully.', 'everest-forms' ), |
| 734 | 'code' => 200, |
| 735 | ), |
| 736 | 200 |
| 737 | ); |
| 738 | } else { |
| 739 | return new \WP_REST_Response( |
| 740 | array( |
| 741 | 'status' => true, |
| 742 | 'message' => esc_html__( 'Please enter the valid license key.', 'everest-forms' ), |
| 743 | 'code' => 400, |
| 744 | ), |
| 745 | 200 |
| 746 | ); |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 |