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
← All changes | inc/class-lp-manager-addons.php +309 -308 4.3.84.4.8 View file →
@@ -1,308 +1,309 @@
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 -}
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 + // For addon must buy.
89 + if ( 0 == $addon['is_free'] ) {
90 + $key_purchase = LP_Settings::get_option( $this->key_purchase_addons, [] );
91 + $key_purchase[ $addon['slug'] ] = $purchase_code;
92 + LP_Settings::update_option( $this->key_purchase_addons, $key_purchase );
93 +
94 + $args['body']['purchase_code'] = $purchase_code;
95 + }
96 +
97 + $result = wp_remote_post( $link_download, $args );
98 + if ( is_wp_error( $result ) ) {
99 + throw new Exception( $result->get_error_message() );
100 + }
101 +
102 + $data = wp_remote_retrieve_body( $result );
103 + if ( preg_match( '/^Error.*/', $data ) ) {
104 + throw new Exception( $data );
105 + }
106 +
107 + // Create file temp zip addon to install with
108 + $wp_upload_dir = wp_upload_dir( null, false );
109 + $name = 'addon.zip';
110 + $path_file = $wp_upload_dir['basedir'] . DIRECTORY_SEPARATOR . $name;
111 + $lp_file_system->put_contents( $path_file, $data );
112 +
113 + return $path_file;
114 + }
115 +
116 + /**
117 + * Install plugin.
118 + *
119 + * @param array $addon
120 + * @param string $package The full local path or URI of the package.
121 + *
122 + * @return void
123 + * @throws Exception
124 + */
125 + public function install( array $addon = [], string $package = '' ) {
126 + $result_install = $this->plugin_upgrader->install( $package );
127 + if ( is_wp_error( $result_install ) ) {
128 + throw new Exception( $result_install->get_error_message() );
129 + } elseif ( ! $result_install ) {
130 + throw new Exception( __( 'Install failed!', 'learnpress' ) );
131 + }
132 +
133 + $result_active = activate_plugin( $addon['basename'] );
134 + if ( is_wp_error( $result_active ) ) {
135 + throw new Exception( $result_active->get_error_message() );
136 + }
137 + }
138 +
139 + /**
140 + * Update plugin.
141 + *
142 + * @param array $addon
143 + * @param string $package
144 + *
145 + * @throws Exception
146 + */
147 + public function update( array $addon = [], string $package = '' ) {
148 + $is_activate = is_plugin_active( $addon['basename'] );
149 + // Must call this function to upgrade success.
150 + wp_update_plugins();
151 +
152 + $args_upgrade = [
153 + 'package' => $package,
154 + 'destination' => WP_PLUGIN_DIR,
155 + 'clear_destination' => false,
156 + 'clear_working' => true,
157 + 'hook_extra' => [],
158 + 'abort_if_destination_exists' => false,
159 + ];
160 + $result = $this->plugin_upgrader->run( $args_upgrade );
161 + if ( ! $result ) {
162 + throw new Exception( __( 'Update failed!', 'learnpress' ) );
163 + }
164 +
165 + if ( $is_activate ) {
166 + $this->activate( $addon );
167 + }
168 + }
169 +
170 + /**
171 + * Activate plugin.
172 + *
173 + * @param array $addon
174 + *
175 + * @return bool|int|true|WP_Error
176 + * @throws Exception
177 + */
178 + public function activate( array $addon = [] ) {
179 + if ( isset( $addon['dependency'] ) ) {
180 + foreach ( $addon['dependency'] as $addon_slug => $addon_label ) {
181 + if ( ! is_plugin_active( $addon_slug ) ) {
182 + throw new Exception( sprintf( 'Please activate "%s" plugin before activate this add-on', $addon_label ) );
183 + }
184 + }
185 + }
186 +
187 + $result_active = activate_plugin( $addon['basename'] ?? '' );
188 + if ( is_wp_error( $result_active ) ) {
189 + throw new Exception( $result_active->get_error_message() );
190 + }
191 +
192 + return $result_active;
193 + }
194 +
195 + /**
196 + * Deactivate plugin.
197 + *
198 + * @param array $addon
199 + *
200 + * @return void
201 + */
202 + public function deactivate( array $addon = [] ) {
203 + deactivate_plugins( $addon['basename'] ?? '' );
204 + }
205 +
206 + /**
207 + * Active site if install plugin via upload zip has "purchase code".
208 + *
209 + * @param $addon_slug
210 + * @param $purchase_code
211 + *
212 + * @return void
213 + */
214 + public function active_site( $addon_slug, $purchase_code ) {
215 + try {
216 + $args = [
217 + 'method' => 'POST',
218 + 'body' => [
219 + 'addon' => $addon_slug,
220 + 'purchase_code' => $purchase_code,
221 + ],
222 + 'user-agent' => site_url(),
223 + ];
224 +
225 + $result = wp_remote_post( $this->link_active_site, $args );
226 + if ( is_wp_error( $result ) ) {
227 + throw new Exception( $result->get_error_message() );
228 + }
229 +
230 + $data = wp_remote_retrieve_body( $result );
231 + if ( preg_match( '/^Error.*/', $data ) ) {
232 + throw new Exception( $data );
233 + }
234 +
235 + // Save keys purchase code of addons to table WP Options.
236 + $key_purchases = LP_Settings::get_option( LP_Manager_Addons::instance()->key_purchase_addons, [] );
237 + $key_purchases[ $addon_slug ] = $purchase_code;
238 + LP_Settings::update_option( LP_Manager_Addons::instance()->key_purchase_addons, $key_purchases );
239 + } catch ( Throwable $e ) {
240 + error_log( $e->getMessage() );
241 + }
242 + }
243 +
244 + /**
245 + * Get list addon have new version.
246 + *
247 + * @return array
248 + */
249 + public function list_addon_new_version(): array {
250 + $addon_contr = new LP_REST_Addon_Controller();
251 + $request = new WP_REST_Request();
252 + $request->set_param( 'return_obj', true );
253 + $addons_rs = $addon_contr->list_addons( $request );
254 + $addons_new_version = [];
255 + if ( 'success' === $addons_rs->status ) {
256 + $addons = $addons_rs->data;
257 + $plugins = get_plugins();
258 +
259 + foreach ( $addons as $addon ) {
260 + if ( isset( $plugins[ $addon->basename ] ) ) {
261 + $version = $plugins[ $addon->basename ]['Version'];
262 + if ( version_compare( $version, $addon->version, '<' ) ) {
263 + $addons_new_version[] = $addon;
264 + }
265 + }
266 + }
267 + }
268 +
269 + return $addons_new_version;
270 + }
271 +
272 + /**
273 + * Check addons purchased need extend.
274 + *
275 + * @return bool
276 + * @throws Exception
277 + * @since 4.2.5.9
278 + * @version 1.0.0
279 + */
280 + public function check_addons_purchased_need_extend(): bool {
281 + $addon_contr = new LP_REST_Addon_Controller();
282 + $request = new WP_REST_Request();
283 + $request->set_param( 'return_obj', true );
284 + $addons_rs = $addon_contr->list_addons( $request );
285 + foreach ( $addons_rs->data as $addon ) {
286 + if ( isset( $addon->purchase_info ) ) {
287 + $addon_purchased = $addon->purchase_info;
288 + $date_expired_str = $addon_purchased->date_expire ?? '';
289 + // Test
290 + //$date_expired_str = '2024-02-01';
291 + //$date_expired_str = '2023-01-12';
292 + // End
293 + $date_expired = new DateTime( $date_expired_str );
294 + $date_now = new DateTime( gmdate( 'Y-m-d' ) );
295 + $date_diff = date_diff( $date_now, $date_expired );
296 + $number_days_remaining = $date_diff->days;
297 + if ( $date_diff->invert ) {
298 + $number_days_remaining = 0;
299 + }
300 +
301 + if ( $number_days_remaining <= 60 ) {
302 + return true;
303 + }
304 + }
305 + }
306 +
307 + return false;
308 + }
309 +}