PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
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 4.2.0 All 139 releases
learnpress / inc / rest-api / v1 / frontend / class-lp-rest-addon-controller.php

class-lp-rest-addon-controller.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.8, at inc/rest-api/v1/frontend/class-lp-rest-addon-controller.php

231 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use LearnPress\Helpers\Response;
4 use LearnPress\Helpers\Template;
5 use LearnPress\Models\UserModel;
6
7 defined( 'ABSPATH' ) || exit;
8
9 /**
10 * REST API LP manager addons.
11 */
12 class LP_REST_Addon_Controller extends LP_Abstract_REST_Controller {
13 /**
14 * @var LP_Manager_Addons $lp_addons
15 */
16 private $lp_addons;
17
18 /**
19 * LP_REST_Addon_Controller constructor.
20 */
21 public function __construct() {
22 $this->namespace = 'lp/v1';
23 $this->rest_base = 'addon';
24
25 require_once LP_PLUGIN_PATH . 'inc/class-lp-manager-addons.php';
26 $this->lp_addons = LP_Manager_Addons::instance();
27
28 parent::__construct();
29 }
30
31 public function register_routes() {
32 $this->routes = array(
33 'all' => array(
34 array(
35 'methods' => WP_REST_Server::READABLE,
36 'callback' => array( $this, 'list_addons' ),
37 'permission_callback' => [ $this, 'permission_callback' ],
38 ),
39 ),
40 'action-n' => array(
41 array(
42 'methods' => WP_REST_Server::CREATABLE,
43 'callback' => array( $this, 'action' ),
44 'permission_callback' => [ $this, 'permission_callback' ],
45 ),
46 ),
47 );
48
49 parent::register_routes();
50 }
51
52 /**
53 * Check user permission
54 * 1. Multisite: Check super admin
55 * 2. Single site: Check user is administrator
56 *
57 * @return bool
58 */
59 public function permission_callback() {
60 return is_multisite() ? is_super_admin() : current_user_can( UserModel::ROLE_ADMINISTRATOR );
61 }
62
63 /**
64 * Get list addons
65 *
66 * @param WP_REST_Request $request
67 *
68 * @return LP_REST_Response
69 * @version 1.0.1
70 * @since 4.2.1
71 */
72 public function list_addons( WP_REST_Request $request ): LP_REST_Response {
73 $response = new LP_REST_Response();
74
75 try {
76 $params = $request->get_params();
77 $lp_addon = LP_Manager_Addons::instance();
78 $res = wp_remote_get( $lp_addon->url_list_addons, [ 'timeout' => 30 ] );
79 if ( is_wp_error( $res ) ) {
80 throw new Exception( $res->get_error_message() );
81 }
82
83 $addons = LP_Helper::json_decode( wp_remote_retrieve_body( $res ) );
84
85 // Get list addons purchased.
86 $addons_purchase = LP_Settings::get_option( $lp_addon->key_purchase_addons, [] );
87 if ( ! empty( $addons_purchase ) ) {
88 $args = [
89 'method' => 'POST',
90 'body' => [
91 'addons_purchase' => $addons_purchase,
92 ],
93 'timeout' => 30,
94 'user-agent' => site_url(),
95 ];
96
97 $result = wp_remote_post( $lp_addon->link_addons_purchased, $args );
98 if ( is_wp_error( $result ) ) {
99 throw new Exception( $result->get_error_message() );
100 }
101
102 $data_str = wp_remote_retrieve_body( $result );
103 if ( preg_match( '/^Error.*/', $data_str ) ) {
104 throw new Exception( $data_str );
105 }
106
107 $data = LP_Helper::json_decode( $data_str );
108
109 foreach ( $addons as $key => $addon ) {
110 if ( isset( $data->{$key} ) ) {
111 $addons->{$key}->purchase_info = $data->{$key};
112 }
113 }
114 }
115 // End get list addons purchased.
116
117 if ( isset( $params['return_obj'] ) ) {
118 $response->status = 'success';
119 $response->data = $addons;
120
121 return $response;
122 }
123
124 ob_start();
125 Template::instance()->get_admin_template( 'addons.php', compact( 'addons' ) );
126 $response->data->html = ob_get_clean();
127 $response->data->addons = $addons;
128
129 $response->status = 'success';
130 $response->message = __( 'Get addons successfully', 'learnpress' );
131 } catch ( Throwable $e ) {
132 error_log( __METHOD__ . ':' . $e->getMessage() );
133 $response->message = $e->getMessage();
134 }
135
136 return $response;
137 }
138
139 /**
140 * Action addon
141 *
142 * @param WP_REST_Request $request
143 *
144 * @return Response
145 * @version 1.0.1
146 * @since 4.2.1
147 */
148 public function action( WP_REST_Request $request ): Response {
149 $response = new Response();
150 $response->data = '';
151 $lp_file_system = LP_WP_Filesystem::instance();
152
153 try {
154 $action = $request->get_param( 'action' );
155 if ( empty( $action ) ) {
156 throw new Exception( __( 'Action is invalid!', 'learnpress' ) );
157 }
158
159 $addon = $request->get_param( 'addon' );
160 if ( empty( $addon ) ) {
161 throw new Exception( __( 'Params is invalid!', 'learnpress' ) );
162 }
163
164 $purchase_code = $request->get_param( 'purchase_code' );
165
166 switch ( $action ) {
167 case 'install':
168 case 'update':
169 if ( ! current_user_can( 'install_plugins' ) ) {
170 throw new Exception( __( 'You do not have permission to install plugins.', 'learnpress' ) );
171 }
172
173 $link_download = '';
174 $path_file = '';
175 $package = '';
176
177 if ( $addon['is_org'] ) {
178 $link_download = "{$this->lp_addons->link_org}{$addon['slug']}.{$addon['version']}.zip";
179 } else {
180 // Allow active key for site if site active on DB "updates" is empty.
181 $this->lp_addons->active_site( $addon['slug'], $purchase_code );
182 // Download addon from ThimPress server.
183 $path_file = $this->lp_addons->download_from_thimpress( $addon, $purchase_code );
184 }
185
186 if ( ! empty( $link_download ) ) {
187 $package = $link_download;
188 } elseif ( ! empty( $path_file ) ) {
189 $package = $path_file;
190 }
191
192 if ( 'update' === $action ) {
193 $this->lp_addons->update( $addon, $package );
194 } else {
195 $this->lp_addons->install( $addon, $package );
196 }
197
198 if ( ! empty( $path_file ) ) {
199 $lp_file_system->lp_filesystem->delete( $path_file );
200 }
201
202 break;
203 case 'activate':
204 if ( ! current_user_can( 'activate_plugins' ) ) {
205 throw new Exception( __( 'You do not have permission to activate plugins.', 'learnpress' ) );
206 }
207
208 $this->lp_addons->activate( $addon );
209 break;
210 case 'deactivate':
211 $this->lp_addons->deactivate( $addon );
212 break;
213 case 'update-purchase':
214 $key_purchase = LP_Settings::get_option( $this->lp_addons->key_purchase_addons, [] );
215 $key_purchase[ $addon['slug'] ] = $purchase_code;
216 LP_Settings::update_option( $this->lp_addons->key_purchase_addons, $key_purchase );
217 break;
218 default:
219 break;
220 }
221
222 $response->status = 'success';
223 $response->message = sprintf( '"%s" %s <strong>%s</strong>', $addon['name'], $action, __( 'successfully', 'learnpress' ) );
224 } catch ( Throwable $e ) {
225 $response->message = $e->getMessage();
226 }
227
228 return $response;
229 }
230 }
231