PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.2.0
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.2.0
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 / abstracts / abstract-addon.php

abstract-addon.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.2.0, at inc/abstracts/abstract-addon.php

536 lines 11.1 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
5 /**
6 * Class LP_Addon
7 */
8 class LP_Addon {
9 /**
10 * Current version of addon.
11 *
12 * @var string
13 */
14 public $version = 0;
15 /**
16 * LearnPress require addon version
17 *
18 * @var array
19 */
20 private $lp_require_addon_version = '4.0.0';
21 /**
22 * Required version for current version of addon.
23 *
24 * @var string
25 */
26 public $require_version = 0;
27 /**
28 * Path to root file of addon.
29 *
30 * @var string
31 */
32 public $plugin_file = '';
33 /**
34 * @var string folder path root of addon.
35 */
36 public $plugin_folder_path = '';
37 /**
38 * Root folder name addon.
39 *
40 * @var string
41 */
42 public $plugin_folder_name = '';
43 /**
44 * Plugin base addon.
45 *
46 * @var string contain root folder path and root file of addon
47 */
48 public $plugin_base = '';
49 /**
50 * Base name addon.
51 *
52 * @var string root file name php of addon
53 */
54 public $plugin_base_name = '';
55 /**
56 * Addon text-domain name.
57 *
58 * @var string
59 */
60 public $text_domain = '';
61 /**
62 * @var null
63 */
64 protected $_valid = null;
65 /**
66 * Singleton instance of the addon.
67 *
68 * @var array
69 */
70 public static $instances = array();
71 /**
72 * @var array
73 */
74 protected static $_admin_notices = array();
75 /**
76 * @var string
77 */
78 protected $_template_path = '';
79
80 protected static $on_activate_plugins = array();
81
82 /**
83 * LP_Addon constructor.
84 */
85 public function __construct() {
86 $this->plugin_base = plugin_basename( $this->plugin_file );
87 $this->plugin_base_name = basename( $this->plugin_file );
88 $this->plugin_folder_path = dirname( $this->plugin_file );
89 $this->plugin_folder_name = str_replace( array( '/', $this->plugin_base_name ), '', $this->plugin_base );
90 $this->_define_constants();
91 $this->_includes();
92 remove_action( 'plugins_loaded', array( 'LP_Addon_Announcements', 'instance' ) );
93 add_action( 'init', array( $this, 'init' ) );
94 }
95
96 /**
97 * @deprecated 4.2.0
98 */
99 public static function admin_errors() {
100 _deprecated_function( __METHOD__, '4.2.0' );
101 if ( ! self::$_admin_notices ) {
102 return;
103 }
104
105 foreach ( self::$_admin_notices as $notice ) {
106 ?>
107 <div class="error"><p><?php echo wp_kses_post( $notice ); ?></p></div>
108 <?php
109 }
110 }
111
112 public function _plugin_links( $links ) {
113 if ( method_exists( $this, 'plugin_links' ) ) {
114 $plugin_links = call_user_func( array( $this, 'plugin_links' ) );
115
116 if ( is_callable( array( $this, 'plugin_links' ) ) && $plugin_links ) {
117 $links = array_merge( $links, $plugin_links );
118 }
119 }
120
121 return $links;
122 }
123
124 /**
125 * Init
126 */
127 public function init() {
128 $this->load_text_domain();
129
130 add_filter(
131 "plugin_action_links_$this->plugin_base",
132 array(
133 $this,
134 '_plugin_links',
135 )
136 );
137
138 $this->_init_hooks();
139 $this->_enqueue_assets();
140 }
141
142 /**
143 * Define add-on constants.
144 */
145 protected function _define_constants() {
146
147 }
148
149 /**
150 * Includes add-on files.
151 */
152 protected function _includes() {
153
154 }
155
156 /**
157 * Init add-on hooks.
158 */
159 protected function _init_hooks() {
160
161 }
162
163 /**
164 * Enqueue scripts.
165 */
166 protected function _enqueue_assets() {
167
168 }
169
170 /**
171 * Get plugin slug from plugin file.
172 *
173 * @return bool|string
174 * @deprecated 4.2.0
175 */
176 public function get_plugin_slug() {
177 _deprecated_function( __METHOD__, '4.2.0' );
178 return $this->plugin_base;
179 }
180
181 /**
182 * Check required version of LP.
183 *
184 * @return bool|null
185 * @deprecated 4.0.0
186 * @todo clean, remove code
187 */
188 /*protected function _check_version() {
189 if ( null === $this->_valid ) {
190 $this->_valid = true;
191
192 if ( $this->require_version ) {
193 if ( version_compare( $this->require_version, LEARNPRESS_VERSION, '>' ) ) {
194 add_action( 'admin_notices', array( $this, '_admin_notices' ) );
195
196 // Deactivate plugin .
197 deactivate_plugins( plugin_basename( $this->plugin_file ) );
198
199 $this->_valid = false;
200 }
201 }
202 }
203
204 return $this->_valid;
205 }*/
206
207 /**
208 * Check required version Addons.
209 *
210 * @return bool
211 */
212 public function check_require_version_addon(): bool {
213 $flag = true;
214
215 if ( version_compare( $this->lp_require_addon_version, $this->version, '>' ) ) {
216 $flag = false;
217 }
218
219 if ( ! $flag ) {
220 add_action( 'admin_notices', array( $this, 'admin_notice_require_addon_version' ) );
221
222 // Deactivate plugin .
223 if ( ! empty( $this->plugin_base ) ) {
224 deactivate_plugins( $this->plugin_base );
225 }
226 }
227
228 return $flag;
229 }
230
231 /**
232 * Check required version LP on Addon.
233 * Should define require on each Addons by key "Require_LP_Version".
234 * After long time, when ready standard, need check not key "Require_LP_Version" will deactivate addon.
235 *
236 * @return bool
237 */
238 public function check_require_version_lp(): bool {
239
240 $flag = true;
241
242 // If addon not set, return false.
243 if ( empty( $this->require_version ) ) {
244 $flag = false;
245 }
246
247 if ( version_compare( $this->require_version, LEARNPRESS_VERSION, '>' ) ) {
248 $flag = false;
249 }
250
251 if ( ! $flag ) {
252 add_action( 'admin_notices', array( $this, 'admin_notice_require_lp_version' ) );
253
254 // Deactivate plugin .
255 if ( ! empty( $this->plugin_base ) ) {
256 deactivate_plugins( plugin_basename( $this->plugin_base ) );
257 }
258 }
259
260 return $flag;
261 }
262
263 public function admin_notice_require_addon_version() {
264 ?>
265 <div class="notice notice-error">
266 <p><?php echo( '<strong>LearnPress version ' . LEARNPRESS_VERSION . ' require ' . $this->get_name() . '</strong> version ' . $this->lp_require_addon_version . ' or higher' ); ?></p>
267 </div>
268 <?php
269 }
270
271 /**
272 * Admin notices
273 */
274 public function admin_notice_require_lp_version() {
275 ?>
276 <div class="error">
277 <p>
278 <?php
279 printf(
280 __(
281 '<strong>%1$s</strong> add-on version %2$s requires <strong>LearnPress</strong> version %3$s or higher %4$s',
282 'learnpress'
283 ),
284 esc_html( $this->get_name() ),
285 esc_html( $this->version ),
286 esc_html( $this->require_version ),
287 '| Can addon invalid key Require_LP_Version'
288 );
289 ?>
290 </p>
291 </div>
292 <?php
293 }
294
295 /**
296 * @return mixed
297 */
298 public function get_name() {
299 preg_match( '/([A-z].*)\/([A-z].*)/', $this->plugin_base, $match );
300
301 $name_addon = '';
302 if ( isset( $match[1] ) ) {
303 $name_addon = $match[1];
304 }
305
306 return $name_addon;
307 }
308
309 /**
310 * Load text domain
311 */
312 public function load_text_domain() {
313 if ( empty( $this->plugin_file ) ) {
314 return;
315 }
316
317 $plugin_path = dirname( $this->plugin_file );
318 $plugin_folder = basename( $plugin_path );
319 $text_domain = empty( $this->text_domain ) ? $plugin_folder : $this->text_domain;
320 $locale = apply_filters( 'plugin_locale', get_locale(), $plugin_folder );
321 $domain_files = array();
322
323 if ( is_admin() ) {
324 $domain_files[] = WP_LANG_DIR . "/$plugin_folder/$plugin_folder-admin-$locale.mo";
325 $domain_files[] = WP_LANG_DIR . "/plugins/$plugin_folder-admin-$locale.mo";
326 }
327
328 $domain_files[] = WP_CONTENT_DIR . "/plugins/$plugin_folder/languages/$plugin_folder-$locale.mo";
329 $domain_files[] = WP_LANG_DIR . "/$plugin_folder/$plugin_folder-$locale.mo";
330
331 foreach ( $domain_files as $file ) {
332 if ( ! file_exists( $file ) ) {
333 continue;
334 }
335
336 load_textdomain( $text_domain, $file );
337 }
338
339 if ( $text_domain ) {
340 load_plugin_textdomain( $text_domain, false, plugin_basename( $plugin_path ) . '/languages' );
341 }
342 }
343
344 /**
345 * Load Addon
346 *
347 * @param string $instance
348 * @param string $path
349 * @param string $plugin_file
350 *
351 * @return void|mixed
352 */
353 public static function load( string $instance = '', string $path = '', string $plugin_file = '' ) {
354 $plugin_folder = '';
355
356 if ( $plugin_file ) {
357 $plugin_folder = dirname( $plugin_file );
358 }
359
360 if ( $plugin_folder ) {
361 $path = "$plugin_folder/$path";
362 }
363
364 if ( ! file_exists( $path ) ) {
365 error_log(
366 sprintf(
367 __( '%s plugin file does not exist.', 'learnpress' ),
368 $path
369 )
370 );
371
372 return;
373 }
374
375 include_once $path;
376 $addon_instance = null;
377
378 if ( ! array_key_exists( $instance, self::$instances ) ) {
379 if ( class_exists( $instance ) ) {
380 $addon_instance = new $instance();
381 }
382
383 if ( ! $addon_instance ) {
384 error_log(
385 sprintf(
386 __( '%s plugin class does not exist.', 'learnpress' ),
387 $instance
388 )
389 );
390
391 return;
392 }
393
394 $addon_instance->plugin_file = $plugin_file;
395
396 self::$instances[ $instance ] = $addon_instance;
397 }
398
399 return self::$instances[ $instance ];
400 }
401
402 public function get_plugin_url( $sub = '/' ) {
403 return plugins_url( $sub, $this->plugin_file );
404 }
405
406 /**
407 * Get template path.
408 *
409 * @return string
410 * @deprecated 4.2.0
411 */
412 public function get_template_path() {
413 _deprecated_function( __FUNCTION__, '4.2.0', 'LP_Addon::get_template' );
414 if ( empty( $this->_template_path ) ) {
415 $this->_template_path = learn_press_template_path() . '/addons/' . preg_replace(
416 '!^learnpress-!',
417 '',
418 $this->plugin_folder_name
419 );
420 }
421
422 return $this->_template_path;
423 }
424
425 /**
426 * Get content template of addon.
427 *
428 * @param string $template_name
429 * @param mixed $args
430 * @since 3.0.0
431 * @version 1.0.1
432 */
433 public function get_template( string $template_name = '', $args = [] ) {
434 $default_path = $this->plugin_folder_path . "/templates/$template_name";
435 $folder_name_rewrite = learn_press_template_path();
436 $from_theme_path = sprintf(
437 '%s/%s/%s/%s/%s',
438 get_template_directory(),
439 $folder_name_rewrite,
440 'addons',
441 str_replace( 'learnpress-', '', $this->plugin_folder_name ),
442 $template_name
443 );
444 $path_load = file_exists( $from_theme_path ) ? $from_theme_path : $default_path;
445 Template::instance()->get_template( $path_load, $args );
446 }
447
448 /**
449 * Locate template of addon in theme or inside itself.
450 *
451 * @param string $template_name
452 *
453 * @return string
454 * @deprecated 4.2.0
455 */
456 public function locate_template( $template_name ) {
457 _deprecated_function( __FUNCTION__, '4.2.0', 'LP_Addon::get_template' );
458 return learn_press_locate_template(
459 $template_name,
460 $this->get_template_path(),
461 dirname( $this->plugin_file ) . '/templates/'
462 );
463 }
464
465 /**
466 * Output content of admin view file.
467 *
468 * @param string $view
469 * @param array $args
470 *
471 * @since x.x.x
472 */
473 public function admin_view( $view, $args = array() ) {
474 $args['plugin_file'] = $this->plugin_file;
475 learn_press_admin_view( $view, $args );
476 }
477
478 /**
479 * Get content of admin view file.
480 *
481 * @param string $view
482 * @param array $args
483 *
484 * @return string
485 * @since x.x.x
486 */
487 public function admin_view_content( $view, $args = array() ) {
488 ob_start();
489 $this->admin_view( $view, $args );
490
491 return ob_get_clean();
492 }
493
494 /**
495 * @return mixed
496 * @deprecated 4.2.0
497 * using on the addons: co-instructor(4.0.1), wishlist(4.0.4), announcements(4.0.3), course-review, coming-soon ...
498 */
499 public static function instance() {
500 $name = self::_get_called_class();
501 if ( false === $name ) {
502 return false;
503 }
504
505 if ( empty( self::$instances[ $name ] ) ) {
506 self::$instances[ $name ] = new $name();
507 }
508
509 return self::$instances[ $name ];
510 }
511
512 /**
513 * @return bool|string
514 * @deprecated 4.2.0
515 */
516 protected static function _get_called_class() {
517 if ( function_exists( 'get_called_class' ) ) {
518 return get_called_class();
519 }
520
521 $backtrace = debug_backtrace();
522
523 if ( empty( $backtrace[2] ) ) {
524 return false;
525 }
526
527 if ( empty( $backtrace[2]['args'][0] ) ) {
528 return false;
529 }
530
531 return $backtrace[2]['args'][0];
532 }
533 }
534
535 //add_action( 'admin_notices', array( 'LP_Addon', 'admin_errors' ) );
536