PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.10
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.10
4.4.10 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 All 142 releases
learnpress / inc / Ajax / AddonsAjax.php

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

212 lines 5.9 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 * @since 4.4.9
48 * @version 1.0.2
49 */
50 public function addon_action() {
51 $response = new Response();
52 $lp_file_system = LP_WP_Filesystem::instance();
53 $message = '';
54
55 try {
56 if ( is_multisite() && ! is_super_admin() ) {
57 throw new Exception( __( 'You do not have permission.', 'learnpress' ) );
58 } elseif ( ! current_user_can( UserModel::ROLE_ADMINISTRATOR ) ) {
59 throw new Exception( __( 'You do not have permission.', 'learnpress' ) );
60 }
61
62 $params = LP_Helper::json_decode( LP_Request::get_param( 'data' ), true );
63 if ( ! is_array( $params ) ) {
64 throw new Exception( __( 'Params is invalid!', 'learnpress' ) );
65 }
66
67 $action = sanitize_key( $params['action_type'] ?? '' );
68 if ( empty( $action ) ) {
69 throw new Exception( __( 'Action is invalid!', 'learnpress' ) );
70 }
71
72 $addon = $params['addon'] ?? [];
73 if ( empty( $addon ) || ! is_array( $addon ) ) {
74 throw new Exception( __( 'Params is invalid!', 'learnpress' ) );
75 }
76
77 $purchase_code = sanitize_text_field( $params['purchase_code'] ?? '' );
78
79 switch ( $action ) {
80 case 'install':
81 case 'update':
82 if ( ! current_user_can( 'install_plugins' ) ) {
83 throw new Exception( __( 'You do not have permission to install plugins.', 'learnpress' ) );
84 }
85
86 $link_download = '';
87 $path_file = '';
88 $package = '';
89 $is_paid_addon = empty( $addon['is_free'] ) && empty( $addon['is_org'] );
90 $purchase_info = null;
91
92 if ( $is_paid_addon && 'update' === $action && empty( $purchase_code ) ) {
93 $purchase_codes = LP_Settings::get_option( $this->addon_service->key_purchase_addons, array() );
94 $purchase_code = $purchase_codes[ $addon['slug'] ] ?? '';
95 }
96
97 if ( ! empty( $addon['is_org'] ) ) {
98 $link_download = "{$this->addon_service->link_org}{$addon['slug']}.{$addon['version']}.zip";
99 } else {
100 if ( 'install' === $action ) {
101 // Allow active key for site if site active on DB "updates" is empty.
102 $this->addon_service->active_site( $addon['slug'], $purchase_code );
103 }
104
105 $purchase_info = $this->addon_service->get_addon_purchase_info(
106 $addon['slug'],
107 $purchase_code
108 );
109 // Download addon from ThimPress server.
110 $path_file = $this->addon_service->download_from_thimpress( $addon, $purchase_code );
111 }
112
113 if ( ! empty( $link_download ) ) {
114 $package = $link_download;
115 } elseif ( ! empty( $path_file ) ) {
116 $package = $path_file;
117 }
118
119 if ( 'update' === $action ) {
120 $this->addon_service->update( $addon, $package );
121 } else {
122 $this->addon_service->install( $addon, $package );
123 }
124
125 if ( ! empty( $path_file ) ) {
126 $lp_file_system->lp_filesystem->delete( $path_file );
127 }
128
129 $message = sprintf(
130 /* translators: %s: addon name */
131 __( 'Installed successfully %s', 'learnpress' ),
132 $addon['name'] ?? ''
133 );
134 if ( 'update' === $action ) {
135 $message = sprintf(
136 /* translators: %s: addon name */
137 __( 'Updated successfully %s', 'learnpress' ),
138 $addon['name'] ?? ''
139 );
140 }
141
142 break;
143 case 'activate':
144 if ( ! current_user_can( 'activate_plugins' ) ) {
145 throw new Exception( __( 'You do not have permission to activate plugins.', 'learnpress' ) );
146 }
147
148 $this->addon_service->activate( $addon );
149 $message = sprintf(
150 /* translators: %s: addon name */
151 __( 'Activated successfully %s', 'learnpress' ),
152 $addon['name'] ?? ''
153 );
154 break;
155 case 'deactivate':
156 $this->addon_service->deactivate( $addon );
157 $message = sprintf(
158 /* translators: %s: addon name */
159 __( 'Deactivated successfully %s', 'learnpress' ),
160 $addon['name'] ?? ''
161 );
162 break;
163 case 'update-purchase':
164 $this->addon_service->active_site(
165 $addon['slug'],
166 $purchase_code
167 );
168 $purchase_info = $this->addon_service->get_addon_purchase_info( $addon['slug'], $purchase_code );
169 $message = sprintf(
170 /* translators: %s: addon name */
171 __( 'Updated purchase code successfully for %s', 'learnpress' ),
172 $addon['name'] ?? ''
173 );
174 break;
175 default:
176 break;
177 }
178
179 $response->status = Response::STATUS_SUCCESS;
180 $response->message = ! empty( $message ) ? $message : sprintf(
181 '"%s" %s %s',
182 $addon['name'] ?? '',
183 $action,
184 __( 'successfully', 'learnpress' )
185 );
186
187 $addon_obj = json_decode( wp_json_encode( $addon ) );
188 if ( ! empty( $purchase_info ) ) {
189 $addon_obj->purchase_info = $purchase_info;
190 }
191
192 wp_cache_delete( 'plugins', 'plugins' );
193 $plugins_installed = get_plugins();
194 $plugins_activated = get_option( 'active_plugins', array() );
195 $keys_purchase = LP_Settings::get_option( $this->addon_service->key_purchase_addons, array() );
196
197 $response->data->html = AdminAddonsPage::html_addon_item(
198 $addon_obj,
199 array(
200 'plugins_installed' => $plugins_installed,
201 'plugins_activated' => $plugins_activated,
202 'keys_purchase' => $keys_purchase,
203 )
204 );
205 } catch ( Throwable $e ) {
206 $response->message = $e->getMessage();
207 }
208
209 wp_send_json( $response );
210 }
211 }
212