PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9.1
4.4.9.1 4.4.9 4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 All 141 releases
learnpress / inc / Ajax / AddonsAjax.php

AddonsAjax.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.9.1, at inc/Ajax/AddonsAjax.php

187 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class AddonsAjax
4 *
5 * Handle add-on actions (install, update, activate, deactivate, update-purchase)
6 * via the LearnPress AJAX dispatcher.
7 *
8 * @since 4.4.7
9 * @version 1.0.0
10 */
11
12 namespace LearnPress\Ajax;
13
14 use Exception;
15 use LearnPress\Helpers\Response;
16 use LearnPress\Models\UserModel;
17 use LearnPress\Services\AddonService;
18 use LearnPress\TemplateHooks\Admin\AdminAddonsPage;
19 use LP_Helper;
20 use LP_Request;
21 use LP_Settings;
22 use LP_WP_Filesystem;
23 use Throwable;
24
25 defined( 'ABSPATH' ) || exit;
26
27 /**
28 * Class AddonsAjax
29 */
30 class AddonsAjax extends AbstractAjax {
31 /**
32 * @var AddonService
33 */
34 private $addon_service;
35
36 /**
37 * AddonsAjax constructor.
38 */
39 public function __construct() {
40 $this->addon_service = AddonService::instance();
41 }
42
43 /**
44 * Handle addon action: install, update, activate, deactivate, update-purchase.
45 *
46 * @return void
47 */
48 public function addon_action() {
49 $response = new Response();
50 $lp_file_system = LP_WP_Filesystem::instance();
51
52 try {
53 if ( is_multisite() && ! is_super_admin() ) {
54 throw new Exception( __( 'You do not have permission.', 'learnpress' ) );
55 } elseif ( ! current_user_can( UserModel::ROLE_ADMINISTRATOR ) ) {
56 throw new Exception( __( 'You do not have permission.', 'learnpress' ) );
57 }
58
59 $params = LP_Helper::json_decode( LP_Request::get_param( 'data' ), true );
60 if ( ! is_array( $params ) ) {
61 throw new Exception( __( 'Params is invalid!', 'learnpress' ) );
62 }
63
64 $action = sanitize_key( $params['action_type'] ?? '' );
65 if ( empty( $action ) ) {
66 throw new Exception( __( 'Action is invalid!', 'learnpress' ) );
67 }
68
69 $addon = $params['addon'] ?? [];
70 if ( empty( $addon ) || ! is_array( $addon ) ) {
71 throw new Exception( __( 'Params is invalid!', 'learnpress' ) );
72 }
73
74 $purchase_code = sanitize_text_field( $params['purchase_code'] ?? '' );
75
76 switch ( $action ) {
77 case 'install':
78 case 'update':
79 if ( ! current_user_can( 'install_plugins' ) ) {
80 throw new Exception( __( 'You do not have permission to install plugins.', 'learnpress' ) );
81 }
82
83 $link_download = '';
84 $path_file = '';
85 $package = '';
86 $is_paid_addon = empty( $addon['is_free'] ) && empty( $addon['is_org'] );
87 $purchase_info = null;
88
89 if ( $is_paid_addon && 'update' === $action && empty( $purchase_code ) ) {
90 $purchase_codes = LP_Settings::get_option( $this->addon_service->key_purchase_addons, array() );
91 $purchase_code = $purchase_codes[ $addon['slug'] ] ?? '';
92 }
93
94 if ( ! empty( $addon['is_org'] ) ) {
95 $link_download = "{$this->addon_service->link_org}{$addon['slug']}.{$addon['version']}.zip";
96 } else {
97 if ( 'install' === $action ) {
98 // Allow active key for site if site active on DB "updates" is empty.
99 $this->addon_service->active_site( $addon['slug'], $purchase_code );
100 }
101
102 $purchase_info = $this->addon_service->validate_and_save_purchase_code(
103 $addon['slug'],
104 $purchase_code
105 );
106 // Download addon from ThimPress server.
107 $path_file = $this->addon_service->download_from_thimpress( $addon, $purchase_code );
108 }
109
110 if ( ! empty( $link_download ) ) {
111 $package = $link_download;
112 } elseif ( ! empty( $path_file ) ) {
113 $package = $path_file;
114 }
115
116 if ( 'update' === $action ) {
117 $this->addon_service->update( $addon, $package );
118 } else {
119 $this->addon_service->install( $addon, $package );
120 }
121
122 if ( ! empty( $path_file ) ) {
123 $lp_file_system->lp_filesystem->delete( $path_file );
124 }
125
126 break;
127 case 'activate':
128 if ( ! current_user_can( 'activate_plugins' ) ) {
129 throw new Exception( __( 'You do not have permission to activate plugins.', 'learnpress' ) );
130 }
131
132 $this->addon_service->activate( $addon );
133 break;
134 case 'deactivate':
135 $this->addon_service->deactivate( $addon );
136 break;
137 case 'update-purchase':
138 // If empty, try key old activated
139 if ( empty( $purchase_code ) ) {
140 $purchase_codes = lp_settings::get_option( $this->addon_service->key_purchase_addons, array() );
141 $purchase_code = $purchase_codes[ $addon['slug'] ] ?? '';
142 }
143
144 $this->addon_service->active_site(
145 $addon['slug'],
146 $purchase_code
147 );
148 $purchase_info = $this->addon_service->validate_and_save_purchase_code( $addon['slug'], $purchase_code );
149 break;
150 default:
151 break;
152 }
153
154 $response->status = Response::STATUS_SUCCESS;
155 $response->message = sprintf(
156 '"%s" %s %s',
157 $addon['name'] ?? '',
158 $action,
159 __( 'successfully', 'learnpress' )
160 );
161
162 $addon_obj = json_decode( wp_json_encode( $addon ) );
163 if ( ! empty( $purchase_info ) ) {
164 $addon_obj->purchase_info = $purchase_info;
165 }
166
167 wp_cache_delete( 'plugins', 'plugins' );
168 $plugins_installed = get_plugins();
169 $plugins_activated = get_option( 'active_plugins', array() );
170 $keys_purchase = LP_Settings::get_option( $this->addon_service->key_purchase_addons, array() );
171
172 $response->data->html = AdminAddonsPage::html_addon_item(
173 $addon_obj,
174 array(
175 'plugins_installed' => $plugins_installed,
176 'plugins_activated' => $plugins_activated,
177 'keys_purchase' => $keys_purchase,
178 )
179 );
180 } catch ( Throwable $e ) {
181 $response->message = $e->getMessage();
182 }
183
184 wp_send_json( $response );
185 }
186 }
187