PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3
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.1.7.3, at inc/abstracts/abstract-addon.php

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