PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.3.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.3.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.3.8, at inc/rest-api/v1/frontend/class-lp-rest-addon-controller.php

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