PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.2.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.2.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / ajax / addons.php

addons.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.2.0, at includes/ajax/addons.php

120 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace StoreEngine\Ajax;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 use StoreEngine\Addons as AddonsRegistry;
10 use StoreEngine\Classes\AbstractAjaxHandler;
11 use StoreEngine\Classes\Exceptions\StoreEngineException;
12 use StoreEngine\Utils\Helper;
13
14 class Addons extends AbstractAjaxHandler {
15 public function __construct() {
16 $this->actions = [
17 'get_all_addons' => [
18 'capability' => 'manage_options',
19 'callback' => [ $this, 'get_all_addons' ],
20 ],
21 'saved_addon_status' => [
22 'capability' => 'manage_options',
23 'callback' => [ $this, 'saved_addon_status' ],
24 'fields' => [
25 'addon_name' => 'string',
26 'addon_slug' => 'string',
27 'status' => 'boolean',
28 'required_plugin' => [
29 'plugin_dir_path' => 'string',
30 'plugin_name' => 'string',
31 ],
32 ],
33 ],
34 ];
35 }
36
37 protected function get_all_addons() {
38 wp_send_json_success( json_decode( get_option( STOREENGINE_ADDONS_SETTINGS_NAME, '{}' ) ) );
39 }
40
41
42 protected function saved_addon_status( $payload ) {
43 if ( empty( $payload['addon_name'] ) ) {
44 wp_send_json_error( __( 'Addon name is required.', 'storeengine' ) );
45 }
46
47 if ( empty( $payload['addon_slug'] ) ) {
48 wp_send_json_error( __( 'Addon slug is required.', 'storeengine' ) );
49 }
50
51 if ( ! array_key_exists( 'status', $payload ) ) {
52 wp_send_json_error( __( 'Addon status is required.', 'storeengine' ) );
53 }
54
55 if ( $payload['status'] && ! empty( $payload['required_plugin'] ) && is_array( $payload['required_plugin'] ) ) {
56 foreach ( $payload['required_plugin'] as $plugin ) {
57 if ( ! Helper::is_plugin_active( $plugin['plugin_dir_path'] ) ) {
58 wp_send_json_error( sprintf(
59 /* translators: %1$s. Plugin Name, %2$s. Addon name */
60 esc_html__( '%1$s Plugin is required to activate %2$s addon.', 'storeengine' ),
61 esc_html( $plugin['plugin_name'] ),
62 esc_html( $payload['addon_name'] )
63 ) );
64 }
65 }
66 }
67
68 $addon_slug = $payload['addon_slug'];
69
70 // Sibling-addon dependency checks (e.g. POS needs Inventory + Inventory Pro).
71 if ( $payload['status'] ) {
72 $missing = AddonsRegistry::get_missing_requirements( $addon_slug );
73 if ( $missing ) {
74 wp_send_json_error( sprintf(
75 /* translators: %1$s. Required addon list, %2$s. Addon name being activated */
76 esc_html__( 'Activate %1$s first — required by %2$s.', 'storeengine' ),
77 esc_html( implode( ', ', $missing ) ),
78 esc_html( $payload['addon_name'] )
79 ) );
80 }
81 } else {
82 $dependents = AddonsRegistry::get_active_dependents( $addon_slug );
83 if ( $dependents ) {
84 wp_send_json_error( sprintf(
85 /* translators: %1$s. Dependent addon list, %2$s. Addon name being deactivated */
86 esc_html__( 'Cannot deactivate %2$s while %1$s depends on it. Deactivate %1$s first.', 'storeengine' ),
87 esc_html( implode( ', ', $dependents ) ),
88 esc_html( $payload['addon_name'] )
89 ) );
90 }
91 }
92
93 // Saved Data.
94 $saved_addons = json_decode( get_option( STOREENGINE_ADDONS_SETTINGS_NAME, '{}' ), true );
95
96 if ( ! is_array( $saved_addons ) ) {
97 $saved_addons = [];
98 }
99
100 // Update status.
101 $addon_slug = $payload['addon_slug'];
102 $saved_addons[ $addon_slug ] = $payload['status'];
103
104 try {
105 // Fire Addon Action
106 if ( $payload['status'] ) {
107 do_action( "storeengine/addons/activated_{$addon_slug}" );
108 } else {
109 do_action( "storeengine/addons/deactivated_{$addon_slug}" );
110 }
111
112 update_option( STOREENGINE_ADDONS_SETTINGS_NAME, wp_json_encode( $saved_addons ) );
113
114 wp_send_json_success( $saved_addons );
115 } catch ( StoreEngineException $e ) {
116 wp_send_json_error( $e->getMessage() );
117 }
118 }
119 }
120