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

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

177 lines 5.0 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 ( $is_paid_addon && 'install' === $action ) {
95 $purchase_info = $this->addon_service->validate_and_save_purchase_code( $addon['slug'], (string) $purchase_code );
96 }
97
98 if ( ! empty( $addon['is_org'] ) ) {
99 $link_download = "{$this->addon_service->link_org}{$addon['slug']}.{$addon['version']}.zip";
100 } else {
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 // Download addon from ThimPress server.
104 $path_file = $this->addon_service->download_from_thimpress( $addon, $purchase_code );
105 }
106
107 if ( ! empty( $link_download ) ) {
108 $package = $link_download;
109 } elseif ( ! empty( $path_file ) ) {
110 $package = $path_file;
111 }
112
113 if ( 'update' === $action ) {
114 $this->addon_service->update( $addon, $package );
115 } else {
116 $this->addon_service->install( $addon, $package );
117 }
118
119 if ( ! empty( $path_file ) ) {
120 $lp_file_system->lp_filesystem->delete( $path_file );
121 }
122
123 break;
124 case 'activate':
125 if ( ! current_user_can( 'activate_plugins' ) ) {
126 throw new Exception( __( 'You do not have permission to activate plugins.', 'learnpress' ) );
127 }
128
129 $this->addon_service->activate( $addon );
130 break;
131 case 'deactivate':
132 $this->addon_service->deactivate( $addon );
133 break;
134 case 'update-purchase':
135 $purchase_info = $this->addon_service->validate_and_save_purchase_code(
136 $addon['slug'],
137 (string) $purchase_code
138 );
139 break;
140 default:
141 break;
142 }
143
144 $response->status = Response::STATUS_SUCCESS;
145 $response->message = sprintf(
146 '"%s" %s %s',
147 $addon['name'] ?? '',
148 $action,
149 __( 'successfully', 'learnpress' )
150 );
151
152 $addon_obj = json_decode( wp_json_encode( $addon ) );
153 if ( ! empty( $purchase_info ) ) {
154 $addon_obj->purchase_info = $purchase_info;
155 }
156
157 wp_cache_delete( 'plugins', 'plugins' );
158 $plugins_installed = get_plugins();
159 $plugins_activated = get_option( 'active_plugins', array() );
160 $keys_purchase = LP_Settings::get_option( $this->addon_service->key_purchase_addons, array() );
161
162 $response->data->html = AdminAddonsPage::html_addon_item(
163 $addon_obj,
164 array(
165 'plugins_installed' => $plugins_installed,
166 'plugins_activated' => $plugins_activated,
167 'keys_purchase' => $keys_purchase,
168 )
169 );
170 } catch ( Throwable $e ) {
171 $response->message = $e->getMessage();
172 }
173
174 wp_send_json( $response );
175 }
176 }
177