PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3.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 / admin / helpers / class-lp-plugins-helper.php

class-lp-plugins-helper.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.7.3.1, at inc/admin/helpers/class-lp-plugins-helper.php

428 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class LP_Plugins_Helper {
4 /**
5 * @var int
6 */
7 public static $transient_timeout = HOUR_IN_SECONDS;
8
9 /**
10 * @var array
11 */
12 public static $plugins = array(
13 'installed' => false,
14 'free' => false,
15 'premium' => false,
16 );
17
18 /**
19 * @var LP_Background_Query_Items
20 */
21 protected static $_background_query_items = null;
22
23 /**
24 * @var array
25 */
26 public static $themes = array(
27 'education' => false,
28 'other' => false,
29 );
30
31 public static function require_plugins_api() {
32 global $pagenow;
33
34 if ( ! function_exists( 'plugins_api' ) && 'plugin-install.php' !== $pagenow ) {
35 include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
36 }
37 }
38
39 /**
40 * Get all add-ons for LearnPress has installed.
41 * Identify a plugin is an add-on if it is already existing a tag 'learnpress' inside
42 *
43 * @param string $type
44 *
45 * @return mixed
46 */
47 public static function get_plugins( $type = '' ) {
48 self::require_plugins_api();
49 $plugins = array();
50
51 if ( ! function_exists( 'get_plugins' ) ) {
52 require_once ABSPATH . '/wp-admin/includes/plugin.php';
53 }
54 /*
55 * Delete cache so hook for extra plugin headers works
56 */
57 $all_plugins = get_plugins();
58
59 // If there is no plugin on our site.
60 if ( ! $all_plugins ) {
61 return array_key_exists( $type, self::$plugins ) ? self::$plugins[ $type ] : self::$plugins;
62 }
63
64 $wp_plugins = self::get_plugins_from_wp();
65 $premium_plugins = self::get_premium_plugins();
66 $wp_installed = array();
67 $premium_installed = array();
68
69 // learn_press_debug( wp_list_pluck( $wp_plugins, 'name' ) );
70 foreach ( $all_plugins as $plugin_file => $plugin_data ) {
71
72 // If there is a tag
73 if ( empty( $plugin_data['Tags'] ) ) {
74 continue;
75 }
76
77 // If there is a tag named 'learnpress'
78 $tags = ( preg_split( '/\s*,\s*/', $plugin_data['Tags'] ) );
79 if ( ! in_array( 'learnpress', $tags ) ) {
80 continue;
81 }
82
83 $plugin_slug = dirname( $plugin_file );
84
85 if ( isset( $wp_plugins[ $plugin_file ] ) ) {
86 $plugins[ $plugin_file ] = (array) $wp_plugins[ $plugin_file ];
87 $plugins[ $plugin_file ]['source'] = 'wp';
88
89 $wp_installed[ $plugin_file ] = true;
90 } elseif ( isset( $premium_plugins[ $plugin_file ] ) ) {
91 $plugins[ $plugin_file ] = (array) $premium_plugins[ $plugin_file ];
92 $plugins[ $plugin_file ]['source'] = 'tp';
93 $premium_installed[ $plugin_file ] = true;
94 } else {
95 $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, false, true );
96 $plugins[ $plugin_file ] = array(
97 'name' => $plugin_data['Name'],
98 'slug' => $plugin_slug,
99 'version' => $plugin_data['Version'],
100 'author' => sprintf( '<a href="%s">%s</a>', $plugin_data['AuthorURI'], $plugin_data['Author'] ),
101 'author_profile' => '',
102 'contributors' => array(),
103 'homepage' => $plugin_data['PluginURI'],
104 'short_description' => $plugin_data['Description'],
105 'icons' => self::get_add_on_icons( $plugin_data, $plugin_file ),
106 );
107 if ( ! empty( $plugin_data['Requires at least'] ) ) {
108 $plugins[ $plugin_file ]['requires'] = $plugin_data['Requires at least'];
109 }
110 if ( ! empty( $plugin_data['Tested up to'] ) ) {
111 $plugins[ $plugin_file ]['tested'] = $plugin_data['Tested up to'];
112 }
113 if ( ! empty( $plugin_data['Last updated'] ) ) {
114 $plugins[ $plugin_file ]['last_updated'] = $plugin_data['Last updated'];
115 }
116 }
117 }
118 self::$plugins['installed'] = $plugins;
119
120 if ( is_array( $wp_plugins ) ) {
121 self::$plugins['free'] = array_diff_key( $wp_plugins, (array) $wp_installed );
122 }
123
124 if ( is_array( $premium_plugins ) ) {
125 self::$plugins['premium'] = array_diff_key( $premium_plugins, (array) $premium_installed );
126 }
127
128 // Sort plugins
129 self::_sort_plugins();
130
131 return array_key_exists( $type, self::$plugins ) ? self::$plugins[ $type ] : self::$plugins;
132 }
133
134 /**
135 * Query the list of add-ons from wordpress.org with keyword 'learnpress'
136 * This requires have a keyword named 'learnpress' in plugin header Tags
137 *
138 * @param array
139 *
140 * @return mixed
141 */
142 public static function get_plugins_from_wp( $args = null ) {
143 return LP_Background_Query_Items::instance()->get_plugins_from_wp();
144 }
145
146 /**
147 * Get all premium addons from cache.
148 * If there is no addons, push an action to queue in order to
149 * trying query all addons from thimpress.com in next requesting.
150 *
151 * @return mixed
152 */
153 public static function get_premium_plugins() {
154 return LP_Background_Query_Items::instance()->get_plugins_from_tp();
155 }
156
157 /**
158 * Get our related themes.
159 *
160 * @param string $type
161 * @param array $args
162 *
163 * @return array|mixed
164 */
165 public static function get_related_themes( $type = '', $args = array() ) {
166 $themes = array();
167
168 if ( self::$themes ) {
169 $themes = array_filter( self::$themes );
170 }
171
172 if ( ! $themes ) {
173 self::$themes = LP_Background_Query_Items::instance()->get_related_themes();
174 }
175
176 if ( $type && self::$themes && array_key_exists( $type, self::$themes ) ) {
177 $themes = self::$themes[ $type ];
178 $args = wp_parse_args( $args, array( 'include' => '' ) );
179
180 if ( $themes && $args['include'] ) {
181 $search_results = array();
182 foreach ( $themes as $theme ) {
183 if ( in_array( $theme['id'], $args['include'] ) ) {
184 $search_results[] = $theme;
185 }
186 }
187
188 $themes = $search_results;
189 }
190 } else {
191 $themes = self::$themes;
192 }
193
194 return $themes;
195 }
196
197
198 /**
199 * Count themes.
200 *
201 * @param string $type
202 *
203 * @return int
204 */
205 public static function count_themes( $type = '' ) {
206 $count = 0;
207 $themes = self::get_related_themes();
208
209 if ( $themes ) {
210 if ( array_key_exists( $type, $themes ) ) {
211 $count = ! empty( $themes[ $type ] ) ? sizeof( $themes[ $type ] ) : 0;
212 } else {
213 foreach ( $themes as $k => $v ) {
214 $count += ! empty( $v ) ? sizeof( $v ) : 0;
215 }
216 }
217 }
218
219 return $count;
220 }
221
222 /**
223 * Get action links of a plugin.
224 *
225 * @param object $plugin
226 * @param string $file
227 *
228 * @return array
229 */
230 public static function get_add_on_action_link( $plugin, $file ) {
231 $action_links = array();
232 if ( ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) ) {
233 $name = '';
234 // action links for publish add-ons
235 if ( ! empty( $plugin['source'] ) && $plugin['source'] == 'wp' ) {
236 $status = install_plugin_install_status( $plugin );
237 switch ( $status['status'] ) {
238 case 'install':
239 if ( $status['url'] ) {
240 /* translators: 1: Plugin name and version. */
241 $action_links[] = '<a class="install-now button" data-slug="' . esc_attr( $plugin['slug'] ) . '" href="' . esc_url_raw( $status['url'] ) . '" aria-label="' . esc_attr( sprintf( __( 'Install %s now', 'learnpress' ), $name ) ) . '" data-name="' . esc_attr( $name ) . '" data-success="Installed"><span>' . __( 'Install Now' ) . '</span></a>';
242 }
243
244 break;
245 case 'update_available':
246 if ( $status['url'] ) {
247 /* translators: 1: Plugin name and version */
248 $action_links[] = '<a class="update-now button" data-plugin="' . esc_attr( $status['file'] ) . '" data-slug="' . esc_attr( $plugin['slug'] ) . '" href="' . esc_url_raw( $status['url'] ) . '" aria-label="' . esc_attr( sprintf( __( 'Update %s now', 'learnpress' ), $name ) ) . '" data-name="' . esc_attr( $name ) . '"><span>' . __( 'Update Now' ) . '</span></a>';
249 }
250
251 break;
252 }
253 if ( learn_press_is_plugin_install( $file ) ) {
254 if ( is_plugin_active( $file ) ) {
255 $action_links[] = '<a class="button disable-now" data-slug="' . esc_attr( $plugin['slug'] ) . '" href="' . esc_url_raw( wp_nonce_url( 'plugins.php?action=deactivate&plugin=' . $file, 'deactivate-plugin_' . $file ) ) . '" aria-label="' . esc_attr( sprintf( __( 'Disable %s now', 'learnpress' ), $name ) ) . '" data-name="' . esc_attr( $name ) . '"><span>' . __( 'Disable Now', 'learnpress' ) . '</span></a>';
256 } else {
257 $action_links[] = '<a class="button enable-now" data-slug="' . esc_attr( $plugin['slug'] ) . '" href="' . esc_url_raw( wp_nonce_url( 'plugins.php?action=activate&plugin=' . $file, 'activate-plugin_' . $file ) ) . '" aria-label="' . esc_attr( sprintf( __( 'Enable %s now', 'learnpress' ), $name ) ) . '" data-name="' . esc_attr( $name ) . '"><span>' . __( 'Enable Now', 'learnpress' ) . '</span></a>';
258 }
259 }
260 } else {
261 // action links for premium add-ons installed
262 if ( learn_press_is_plugin_install( $file ) ) {
263 if ( is_plugin_active( $file ) ) {
264 $action_links[] = '<a class="button disable-now" data-slug="' . esc_attr( $plugin['slug'] ) . '" href="' . esc_url_raw( wp_nonce_url( 'plugins.php?action=deactivate&plugin=' . $file, 'deactivate-plugin_' . $file ) ) . '" aria-label="' . esc_attr( sprintf( __( 'Disable %s now', 'learnpress' ), $name ) ) . '" data-name="' . esc_attr( $name ) . '"><span>' . __( 'Disable Now', 'learnpress' ) . '</span></a>';
265 } else {
266 $action_links[] = '<a class="button enable-now" data-slug="' . esc_attr( $plugin['slug'] ) . '" href="' . esc_url_raw( wp_nonce_url( 'plugins.php?action=activate&plugin=' . $file, 'activate-plugin_' . $file ) ) . '" aria-label="' . esc_attr( sprintf( __( 'Enable %s now', 'learnpress' ), $name ) ) . '" data-name="' . esc_attr( $name ) . '"><span>' . __( 'Enable Now', 'learnpress' ) . '</span></a>';
267 }
268 } else {
269 // buy now button for premium add-ons
270 if ( isset( $plugin['permarklink'] ) ) {
271 $action_links[] = '<a class="buy-now button" data-slug="' . esc_attr( $plugin['slug'] ) . '" href="' . esc_url_raw( $plugin['permarklink'] ) . '" aria-label="' . esc_attr( sprintf( __( 'Buy %s now' ), $name ) ) . '" data-name="' . esc_attr( $name ) . '">' . __( 'Buy Now', 'learnpress' ) . '</a>';
272 }
273 }
274 }
275 }
276
277 return $action_links;
278 }
279
280 /**
281 * @param $icons
282 *
283 * @return string
284 */
285 public static function get_add_on_icon( $icons ) {
286 $icon = '';
287 if ( ! empty( $icons['2x'] ) ) {
288 $icon = $icons['2x'];
289 } elseif ( ! empty( $icons['1x'] ) ) {
290 $icon = $icons['1x'];
291 } else {
292 $icon = LP_PLUGIN_URL . 'assets/images/icon-128x128.png';
293 }
294
295 return $icon;
296 }
297
298 /**
299 * Get plugin icon.
300 *
301 * @param object $plugin_data
302 * @param string $plugin_file
303 *
304 * @return array
305 */
306 public static function get_add_on_icons( $plugin_data, $plugin_file ) {
307 $plugin_path = ABSPATH . basename( WP_CONTENT_DIR ) . '/plugins/' . $plugin_file;
308 $icon_path = dirname( $plugin_path ) . '/assets/images';
309 $icons = array(
310 '2x' => '',
311 '1x' => '',
312 );
313
314 $icons_tmp = array(
315 '2x' => 'icon-256x256',
316 '1x' => 'icon-128x128',
317 );
318
319 foreach ( $icons_tmp as $s => $name ) {
320 foreach ( array( 'png', 'svg' ) as $t ) {
321 if ( file_exists( $icon_path . "/{$name}.{$t}" ) ) {
322 $icons[ $s ] = plugins_url( '/', $plugin_path ) . "assets/images/{$name}.{$t}";
323 break;
324 }
325 }
326 }
327
328 return $icons;
329 }
330
331 /**
332 * Count plugins.
333 *
334 * @param string $type
335 *
336 * @return int
337 */
338 public static function count_plugins( $type = '' ) {
339 $plugins = self::get_plugins();
340 if ( $type === 'installed' ) {
341 return ! empty( $plugins['installed'] ) ? sizeof( $plugins['installed'] ) : 0;
342 } else {
343 $wp_plugins = ! empty( $plugins['free'] ) ? sizeof( $plugins['free'] ) : 0;
344 $tp_plugins = ! empty( $plugins['premium'] ) ? sizeof( $plugins['premium'] ) : 0;
345
346 return $wp_plugins + $tp_plugins;
347 }
348 }
349
350 /**
351 * Filter plugin if it slug is starts with 'learnpress'
352 *
353 * @param object $plugin
354 *
355 * @return bool
356 */
357 public static function _filter_plugin( $plugin ) {
358 $slug = '';
359 if ( $plugin ) {
360 $slug = is_array( $plugin ) ? $plugin['slug'] : $plugin->slug;
361 }
362
363 return $slug && preg_match( '!^learnpress-.*!', $slug );
364 }
365
366 /**
367 * Sort plugins.
368 */
369 public static function _sort_plugins() {
370 foreach ( self::$plugins as $k => $plugin ) {
371 if ( is_array( $plugin ) ) {
372 ksort( $plugin );
373 self::$plugins[ $k ] = $plugin;
374 }
375 }
376 }
377
378 /**
379 * Get all slugs of plugins have installed on site
380 *
381 * @return array
382 */
383 public static function get_installed_plugin_slugs() {
384 $slugs = array();
385
386 $plugin_info = get_site_transient( 'update_plugins' );
387 if ( isset( $plugin_info->no_update ) ) {
388 foreach ( $plugin_info->no_update as $plugin ) {
389 $slugs[] = $plugin->slug;
390 }
391 }
392
393 if ( isset( $plugin_info->response ) ) {
394 foreach ( $plugin_info->response as $plugin ) {
395 $slugs[] = $plugin->slug;
396 }
397 }
398
399 return $slugs;
400 }
401
402 /**
403 * Register extra headers for our plugins.
404 *
405 * @param $headers
406 *
407 * @return mixed
408 */
409 public static function add_on_header( $headers ) {
410 $headers['Tags'] = 'Tags';
411 $headers['Requires at least'] = 'Requires at least';
412 $headers['Tested up to'] = 'Tested up to';
413 $headers['Last updated'] = 'Last updated';
414
415 return $headers;
416 }
417 }
418
419 /**
420 * Fixed issue addons page doesn't show installed addons.
421 *
422 * @since 3.2.4
423 */
424 add_filter( 'extra_plugin_headers', array( 'LP_Plugins_Helper', 'add_on_header' ) );
425
426 // Init hooks, etc...
427 // add_action( 'init', array( 'LP_Plugins_Helper', 'init' ) );
428