PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.6.9.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.6.9.2
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 / background-process / class-lp-background-query-items.php

class-lp-background-query-items.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.6.9.2, at inc/background-process/class-lp-background-query-items.php

324 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) || exit;
3
4 if ( ! class_exists( 'LP_Background_Query_Items' ) ) {
5 /**
6 * Class LP_Background_Query_Items
7 *
8 * @since 3.0.0
9 */
10 class LP_Background_Query_Items extends LP_Abstract_Background_Process {
11
12 /**
13 * @var string
14 */
15 protected $action = 'query_items';
16
17 /**
18 * @var int
19 */
20 protected $queue_lock_time = 3600;
21
22 protected $cron_interval = 60 * 24; // minutes
23
24 /**
25 * @var float|int
26 */
27 protected $transient_time = 0;
28
29 /**
30 * LP_Background_Query_Items constructor.
31 */
32 public function __construct() {
33 parent::__construct();
34 $this->transient_time = DAY_IN_SECONDS / 2;
35 }
36
37 /**
38 * @param mixed $data
39 *
40 * @return bool
41 */
42 protected function task( $data ) {
43 parent::task( $data );
44
45 if ( ! isset( $data['callback'] ) ) {
46 return false;
47 }
48
49 if ( ! is_callable( array( $this, $data['callback'] ) ) ) {
50 return false;
51 }
52
53 call_user_func( array( $this, $data['callback'] ) );
54
55 delete_option( 'doing_' . $data['callback'] );
56
57 return false;
58 }
59
60 public function get_plugins_from_wp() {
61 $method = 'query_free_addons';
62 $plugins = get_transient( 'lp_plugins_wp' );
63
64 if ( ! $plugins && ( 'yes' !== get_option( 'doing_' . $method ) ) ) {
65 update_option( 'doing_' . $method, 'yes', 'no' );
66 $this->clear_queue()->push_to_queue(
67 array(
68 'callback' => $method,
69 )
70 )->save()->dispatch();
71 }
72
73 return is_array( $plugins ) ? $plugins : false;
74 }
75
76 public function get_plugins_from_tp() {
77 $method = 'query_premium_addons';
78 $plugins = get_transient( 'lp_plugins_tp' );
79
80 if ( ! $plugins && ( 'yes' !== get_option( 'doing_' . $method ) ) ) {
81 update_option( 'doing_' . $method, 'yes', 'no' );
82 $this->clear_queue()->push_to_queue(
83 array(
84 'callback' => $method,
85 )
86 )->save()->dispatch();
87 }
88
89 return is_array( $plugins ) ? $plugins : false;
90 }
91
92 public function get_related_themes() {
93 $method = 'query_related_themes';
94 $themes = get_transient( 'lp_related_themes' );
95
96 if ( ! $themes && ( 'yes' !== get_option( 'doing_' . $method ) ) ) {
97 update_option( 'doing_' . $method, 'yes', 'no' );
98 $this->clear_queue()->push_to_queue(
99 array(
100 'callback' => $method,
101 )
102 )->save()->dispatch();
103 }
104
105 return is_array( $themes ) ? $themes : false;
106 }
107
108 public function get_last_checked( $type ) {
109 $next = get_option( '_transient_timeout_' . $this->prefix . '_' . $type );
110
111 if ( $next ) {
112 return $next - $this->transient_time;
113 }
114
115 return 0;
116 }
117
118 public function force_update() {
119 $transients = array( 'lp_plugins_wp', 'lp_plugins_tp', 'lp_related_themes' );
120
121 foreach ( $transients as $transient ) {
122 delete_transient( $transient );
123 }
124
125 $this->query_free_addons();
126 $this->query_premium_addons();
127 $this->query_related_themes();
128 }
129
130 /**
131 * Query all free addons from wp.org
132 *
133 * @return array|string
134 */
135 public function query_free_addons() {
136 LP_Plugins_Helper::require_plugins_api();
137
138 $per_page = 20;
139 $paged = 1;
140 $tag = 'learnpress';
141
142 $query_args = array(
143 'page' => $paged,
144 'per_page' => $per_page,
145 'fields' => array(
146 'last_updated' => true,
147 'icons' => true,
148 'active_installs' => true,
149 ),
150 'locale' => get_locale(),
151 'installed_plugins' => LP_Plugins_Helper::get_installed_plugin_slugs(),
152 'author' => 'thimpress',
153 );
154 $plugins = array();
155
156 set_transient( 'lp_plugins_wp', __( 'There is no items found!', 'learnpress' ), $this->transient_time );
157
158 try {
159 $api = plugins_api( 'query_plugins', $query_args );
160 if ( is_wp_error( $api ) ) {
161 throw new Exception( __( 'WP query plugins error!', 'learnpress' ) );
162 }
163 if ( ! is_array( $api->plugins ) ) {
164 throw new Exception( __( 'WP query plugins empty!', 'learnpress' ) );
165 }
166 $all_plugins = get_plugins();
167 // Filter plugins with tag contains 'learnpress'
168 $_plugins = array_filter( $api->plugins, array( 'LP_Plugins_Helper', '_filter_plugin' ) );
169
170 // Ensure that the array is indexed from 0
171 $_plugins = array_values( $_plugins );
172
173 for ( $n = sizeof( $_plugins ), $i = $n - 1; $i >= 0; $i -- ) {
174 $plugin = $_plugins[ $i ];
175
176 if ( ! isset( $plugin->slug ) ) {
177 return;
178 }
179
180 $key = $plugin->slug;
181 foreach ( $all_plugins as $file => $p ) {
182 if ( strpos( $file, $plugin->slug ) !== false ) {
183 $key = $file;
184 break;
185 }
186 }
187 $plugin->source = 'wp';
188 $plugins[ $key ] = (array) $plugin;
189 }
190
191 if ( sizeof( $plugins ) ) {
192 set_transient( 'lp_plugins_wp', $plugins, $this->transient_time );
193 }
194 } catch ( Exception $ex ) {
195 }
196
197 return $plugins;
198 }
199
200 /**
201 * Query all available premium addons from thimpress.com
202 *
203 * @return array|bool
204 */
205 public function query_premium_addons() {
206 $plugins = array();
207 $url = 'https://thimpress.com/?thimpress_get_addons=premium';
208 $response = wp_remote_get( esc_url_raw( $url ), array( 'decompress' => false ) );
209
210 set_transient( 'lp_plugins_tp', __( 'There is no items found!', 'learnpress' ), $this->transient_time );
211
212 if ( is_wp_error( $response ) ) {
213 return false;
214 }
215
216 $response = wp_remote_retrieve_body( $response );
217 $response = json_decode( $response, true );
218
219 if ( ! empty( $response ) ) {
220 $maps = array(
221 'authorize-net-add-on-learnpress' => 'learnpress-authorizenet-payment',
222 '2checkout-add-learnpress' => 'learnpress-2checkout-payment',
223 'commission-add-on-for-learnpress' => 'learnpress-commission',
224 'paid-memberships-pro-add-learnpress' => 'learnpress-paid-membership-pro',
225 'gradebook-add-on-for-learnpress' => 'learnpress-gradebook',
226 'sorting-choice-add-on-for-learnpress' => 'learnpress-sorting-choice',
227 'content-drip-add-on-for-learnpress' => 'learnpress-content-drip',
228 'mycred-add-on-for-learnpress' => 'learnpress-mycred',
229 'random-quiz-add-on-for-learnpress' => 'learnpress-random-quiz',
230 'co-instructors-add-on-for-learnpress' => 'learnpress-co-instructor',
231 'collections-add-on-for-learnpress' => 'learnpress-collections',
232 'woocommerce-add-on-for-learnpress' => 'learnpress-woo-payment',
233 'stripe-add-on-for-learnpress' => 'learnpress-stripe',
234 'certificates-add-on-for-learnpress' => 'learnpress-certificates',
235 'assignments-add-on-for-learnpress' => 'learnpress-assignments',
236 'announcement-add-on-for-learnpress' => 'learnpress-announcements',
237 );
238
239 foreach ( $response as $key => $item ) {
240 $slug = $item['slug'];
241 if ( ! empty( $maps[ $slug ] ) ) {
242 $plugin_file = sprintf( '%1$s/%1$s.php', $maps[ $slug ] );
243 $plugins[ $plugin_file ] = $item;
244 }
245 }
246
247 if ( sizeof( $plugins ) ) {
248 set_transient( 'lp_plugins_tp', $plugins, $this->transient_time );
249 }
250 }
251
252 return $plugins;
253 }
254
255 /**
256 * @return array|bool
257 */
258 public function query_related_themes() {
259
260 set_transient( 'lp_related_themes', __( 'There is no item found!', 'learnpress' ), $this->transient_time );
261
262 $themes = array();
263 $url = 'https://api.envato.com/v1/discovery/search/search/item?site=themeforest.net&username=thimpress';
264 $args = array(
265 'headers' => array(
266 'Authorization' => 'Bearer BmYcBsYXlSoVe0FekueDxqNGz2o3JRaP',
267 ),
268 );
269 $response = wp_remote_request( $url, $args );
270
271 if ( is_wp_error( $response ) ) {
272 error_log( $response->get_error_message() );
273 return false;
274 }
275
276 $response = wp_remote_retrieve_body( $response );
277 $response = json_decode( $response, true );
278
279 if ( ! empty( $response ) && ! empty( $response['matches'] ) ) {
280 $all_themes = array();
281
282 foreach ( $response['matches'] as $theme ) {
283 $all_themes[ $theme['id'] ] = $theme;
284 }
285
286 $education_themes = apply_filters(
287 'learn-press/education-themes',
288 array(
289 '23451388' => 'kindergarten',
290 '22773871' => 'ivy-school',
291 '20370918' => 'wordpress-lms',
292 '14058034' => 'eduma',
293 '17097658' => 'coach',
294 '11797847' => 'lms',
295 )
296 );
297
298 if ( $education_themes ) {
299 $themes['other'] = array_diff_key( $all_themes, $education_themes );
300 $themes['education'] = array_diff_key( $all_themes, $themes['other'] );
301 } else {
302 $themes['other'] = $all_themes;
303 }
304
305 set_transient( 'lp_related_themes', $themes, $this->transient_time );
306
307 } elseif ( ! empty( $response['message'] ) ) {
308 set_transient( 'lp_related_themes', $response['message'], $this->transient_time );
309 }
310
311 return $themes;
312 }
313
314 /**
315 * @return LP_Background_Query_Items
316 */
317 public static function instance() {
318 return parent::instance();
319 }
320 }
321 }
322
323 return LP_Background_Query_Items::instance();
324