PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.3.9.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.3.9.1
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 / class-lp-manager-addons.php

class-lp-manager-addons.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.3.9.1, at inc/class-lp-manager-addons.php

309 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class LP_Manager_Addons
4 *
5 * @author ThimPress
6 * @version 1.0.0
7 * @since 4.2.1
8 */
9
10 defined( 'ABSPATH' ) || exit;
11
12 class LP_Manager_Addons {
13 protected static $_instance;
14 /**
15 * @var string Link get list addons.
16 */
17 public $url_list_addons = 'https://learnpress.github.io/learnpress/version-addons.json';
18 //public $url_list_addons = LP_PLUGIN_URL . '/version-addons.json';
19 /**
20 * @var string $link_addon_action Link download plugin from Thimpress.
21 */
22 private $link_addon_action = 'https://updates.thimpress.com/thim-addon-market/download-addon';
23 public $link_addons_purchased = 'https://updates.thimpress.com/thim-addon-market/info-addons-purchased';
24 //public $link_addon_action = 'http://updates/thim-addon-market/download-addon';
25 /**
26 * @var string $link_addon_action Link active site.
27 */
28 private $link_active_site = 'https://updates.thimpress.com/thim-addon-market/active-site';
29 /**
30 * @var string link download plugin from org.
31 */
32 public $link_org = 'https://downloads.wordpress.org/plugin/';
33 /**
34 * @var WP_Ajax_Upgrader_Skin $upgrader_skin
35 */
36 public $upgrader_skin;
37 /**
38 * @var Plugin_Upgrader $plugin_upgrader
39 */
40 public $plugin_upgrader;
41 /**
42 *
43 */
44 public $key_purchase_addons = 'purchase_addons';
45
46 /**
47 * Constructor
48 */
49 protected function __construct() {
50 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
51 include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
52
53 $this->upgrader_skin = new WP_Ajax_Upgrader_Skin();
54 $this->plugin_upgrader = new Plugin_Upgrader( $this->upgrader_skin );
55 }
56
57 /**
58 * Singleton
59 *
60 * @return LP_Manager_Addons
61 */
62 public static function instance(): LP_Manager_Addons {
63 if ( self::$_instance == null ) {
64 self::$_instance = new self();
65 }
66
67 return self::$_instance;
68 }
69
70 /**
71 * Download addon from Thimpress.
72 *
73 * return string
74 * @throws Exception
75 */
76 public function download_from_thimpress( array $addon = [], string $purchase_code = '' ): string {
77 $lp_file_system = LP_WP_Filesystem::instance();
78 $link_download = $this->link_addon_action;
79 $args = [
80 'method' => 'POST',
81 'body' => [
82 'addon' => $addon['slug'],
83 'version' => 'lastest',
84 ],
85 'user-agent' => site_url(),
86 ];
87
88 if ( 0 == $addon['is_free'] ) {
89 $key_purchase = LP_Settings::get_option( $this->key_purchase_addons, [] );
90 $key_purchase[ $addon['slug'] ] = $purchase_code;
91 LP_Settings::update_option( $this->key_purchase_addons, $key_purchase );
92
93 $args['body']['purchase_code'] = $purchase_code;
94 }
95
96 $result = wp_remote_post( $link_download, $args );
97 if ( is_wp_error( $result ) ) {
98 throw new Exception( $result->get_error_message() );
99 }
100
101 $data = wp_remote_retrieve_body( $result );
102 if ( preg_match( '/^Error.*/', $data ) ) {
103 throw new Exception( $data );
104 }
105
106 // Create file temp zip addon to install with
107 $wp_upload_dir = wp_upload_dir( null, false );
108 $name = 'addon.zip';
109 $path_file = $wp_upload_dir['basedir'] . DIRECTORY_SEPARATOR . $name;
110 $lp_file_system->put_contents( $path_file, $data );
111
112 return $path_file;
113 }
114
115 /**
116 * Install plugin.
117 *
118 * @param array $addon
119 * @param string $package The full local path or URI of the package.
120 *
121 * @return void
122 * @throws Exception
123 */
124 public function install( array $addon = [], string $package = '' ) {
125 $result_install = $this->plugin_upgrader->install( $package );
126 if ( is_wp_error( $result_install ) ) {
127 throw new Exception( $result_install->get_error_message() );
128 } elseif ( ! $result_install ) {
129 throw new Exception( __( 'Install failed!', 'learnpress' ) );
130 }
131
132 $result_active = activate_plugin( $addon['basename'] );
133 if ( is_wp_error( $result_active ) ) {
134 throw new Exception( $result_active->get_error_message() );
135 }
136 }
137
138 /**
139 * Update plugin.
140 *
141 * @param array $addon
142 * @param string $package
143 *
144 * @throws Exception
145 */
146 public function update( array $addon = [], string $package = '' ) {
147 $is_activate = is_plugin_active( $addon['basename'] );
148 // Must call this function to upgrade success.
149 wp_update_plugins();
150
151 $args_upgrade = [
152 'package' => $package,
153 'destination' => WP_PLUGIN_DIR,
154 'clear_destination' => false,
155 'clear_working' => true,
156 'hook_extra' => [],
157 'abort_if_destination_exists' => false,
158 ];
159 $result = $this->plugin_upgrader->run( $args_upgrade );
160 if ( ! $result ) {
161 throw new Exception( __( 'Update failed!', 'learnpress' ) );
162 }
163
164 if ( $is_activate ) {
165 $this->activate( $addon );
166 }
167 }
168
169 /**
170 * Activate plugin.
171 *
172 * @param array $addon
173 *
174 * @return bool|int|true|WP_Error
175 * @throws Exception
176 */
177 public function activate( array $addon = [] ) {
178 if ( isset( $addon['dependency'] ) ) {
179 foreach ( $addon['dependency'] as $addon_slug => $addon_label ) {
180 if ( ! is_plugin_active( $addon_slug ) ) {
181 throw new Exception( sprintf( 'Please activate "%s" plugin before activate this add-on', $addon_label ) );
182 }
183 }
184 }
185
186 $result_active = activate_plugin( $addon['basename'] ?? '' );
187 if ( is_wp_error( $result_active ) ) {
188 throw new Exception( $result_active->get_error_message() );
189 }
190
191 return $result_active;
192 }
193
194 /**
195 * Deactivate plugin.
196 *
197 * @param array $addon
198 *
199 * @return void
200 */
201 public function deactivate( array $addon = [] ) {
202 deactivate_plugins( $addon['basename'] ?? '' );
203 }
204
205 /**
206 * Active site if install plugin via upload zip has "purchase code".
207 *
208 * @param $addon_slug
209 * @param $purchase_code
210 *
211 * @return void
212 */
213 public function active_site( $addon_slug, $purchase_code ) {
214 try {
215 $args = [
216 'method' => 'POST',
217 'body' => [
218 'addon' => $addon_slug,
219 'purchase_code' => $purchase_code,
220 ],
221 'user-agent' => site_url(),
222 ];
223
224 $result = wp_remote_post( $this->link_active_site, $args );
225 if ( is_wp_error( $result ) ) {
226 throw new Exception( $result->get_error_message() );
227 }
228
229 $data = wp_remote_retrieve_body( $result );
230 if ( preg_match( '/^Error.*/', $data ) ) {
231 throw new Exception( $data );
232 }
233
234 // Save keys purchase code of addons to table WP Options.
235 $key_purchases = LP_Settings::get_option( LP_Manager_Addons::instance()->key_purchase_addons, [] );
236 $key_purchases[ $addon_slug ] = $purchase_code;
237 LP_Settings::update_option( LP_Manager_Addons::instance()->key_purchase_addons, $key_purchases );
238 } catch ( Throwable $e ) {
239 error_log( $e->getMessage() );
240 }
241 }
242
243 /**
244 * Get list addon have new version.
245 *
246 * @return array
247 */
248 public function list_addon_new_version(): array {
249 $addon_contr = new LP_REST_Addon_Controller();
250 $request = new WP_REST_Request();
251 $request->set_param( 'return_obj', true );
252 $addons_rs = $addon_contr->list_addons( $request );
253 $addons_new_version = [];
254 if ( 'success' === $addons_rs->status ) {
255 $addons = $addons_rs->data;
256 $plugins = get_plugins();
257
258 foreach ( $addons as $addon ) {
259 if ( isset( $plugins[ $addon->basename ] ) ) {
260 $version = $plugins[ $addon->basename ]['Version'];
261 if ( version_compare( $version, $addon->version, '<' ) ) {
262 $addons_new_version[] = $addon;
263 }
264 }
265 }
266 }
267
268 return $addons_new_version;
269 }
270
271 /**
272 * Check addons purchased need extend.
273 *
274 * @return bool
275 * @throws Exception
276 * @since 4.2.5.9
277 * @version 1.0.0
278 */
279 public function check_addons_purchased_need_extend(): bool {
280 $addon_contr = new LP_REST_Addon_Controller();
281 $request = new WP_REST_Request();
282 $request->set_param( 'return_obj', true );
283 $addons_rs = $addon_contr->list_addons( $request );
284 foreach ( $addons_rs->data as $addon ) {
285 if ( isset( $addon->purchase_info ) ) {
286 $addon_purchased = $addon->purchase_info;
287 $date_expired_str = $addon_purchased->date_expire ?? '';
288 // Test
289 //$date_expired_str = '2024-02-01';
290 //$date_expired_str = '2023-01-12';
291 // End
292 $date_expired = new DateTime( $date_expired_str );
293 $date_now = new DateTime( gmdate( 'Y-m-d' ) );
294 $date_diff = date_diff( $date_now, $date_expired );
295 $number_days_remaining = $date_diff->days;
296 if ( $date_diff->invert ) {
297 $number_days_remaining = 0;
298 }
299
300 if ( $number_days_remaining <= 60 ) {
301 return true;
302 }
303 }
304 }
305
306 return false;
307 }
308 }
309