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

520 lines 10.1 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 $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 $instance
348 * @param $path
349 * @param string $plugin_file
350 *
351 * @return void|mixed
352 */
353 public static function load( $instance, $path, $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 self::$_admin_notices['add-on-file-no-exists'] = sprintf(
366 __( '%s plugin file does not exist.', 'learnpress' ),
367 $path
368 );
369
370 return;
371 }
372
373 include_once $path;
374 $addon_instance = null;
375
376 if ( class_exists( $instance ) ) {
377 $addon_instance = null;
378 if ( is_callable( array( $instance, 'instance' ) ) ) {
379 $addon_instance = call_user_func( array( $instance, 'instance' ) );
380 } else {
381 $addon_instance = new $instance();
382 }
383 }
384
385 if ( ! $addon_instance ) {
386 self::$_admin_notices['add-on-class-no-exists'] = sprintf(
387 __( '%s plugin class does not exist.', 'learnpress' ),
388 $instance
389 );
390
391 return;
392 }
393
394 $addon_instance->plugin_file = $plugin_file;
395
396 self::$instances[ $instance ] = $addon_instance;
397
398 return $addon_instance;
399 }
400
401 public function get_plugin_url( $sub = '/' ) {
402 return plugins_url( $sub, $this->plugin_file );
403 }
404
405 /**
406 * Get template path.
407 *
408 * @return string
409 */
410 public function get_template_path() {
411 if ( empty( $this->_template_path ) ) {
412 $this->_template_path = learn_press_template_path() . '/addons/' . preg_replace(
413 '!^learnpress-!',
414 '',
415 dirname( $this->get_plugin_slug() )
416 );
417 }
418
419 return $this->_template_path;
420 }
421
422 /**
423 * Get content template of addon in theme or inside itself.
424 *
425 * @param string $template_name
426 * @param array $args
427 */
428 public function get_template( $template_name, $args = array() ) {
429 learn_press_get_template(
430 $template_name,
431 $args,
432 $this->get_template_path(),
433 dirname( $this->plugin_file ) . '/templates/'
434 );
435 }
436
437 /**
438 * Locate template of addon in theme or inside itself.
439 *
440 * @param string $template_name
441 *
442 * @return string
443 */
444 public function locate_template( $template_name ) {
445 return learn_press_locate_template(
446 $template_name,
447 $this->get_template_path(),
448 dirname( $this->plugin_file ) . '/templates/'
449 );
450 }
451
452 /**
453 * Output content of admin view file.
454 *
455 * @param string $view
456 * @param array $args
457 *
458 * @since x.x.x
459 */
460 public function admin_view( $view, $args = array() ) {
461 $args['plugin_file'] = $this->plugin_file;
462 learn_press_admin_view( $view, $args );
463 }
464
465 /**
466 * Get content of admin view file.
467 *
468 * @param string $view
469 * @param array $args
470 *
471 * @return string
472 * @since x.x.x
473 */
474 public function admin_view_content( $view, $args = array() ) {
475 ob_start();
476 $this->admin_view( $view, $args );
477
478 return ob_get_clean();
479 }
480
481 /**
482 * @return mixed
483 */
484 public static function instance() {
485 $name = self::_get_called_class();
486 if ( false === $name ) {
487 return false;
488 }
489
490 if ( empty( self::$instances[ $name ] ) ) {
491 self::$instances[ $name ] = new $name();
492 }
493
494 return self::$instances[ $name ];
495 }
496
497 /**
498 * @return bool|string
499 */
500 protected static function _get_called_class() {
501 if ( function_exists( 'get_called_class' ) ) {
502 return get_called_class();
503 }
504
505 $backtrace = debug_backtrace();
506
507 if ( empty( $backtrace[2] ) ) {
508 return false;
509 }
510
511 if ( empty( $backtrace[2]['args'][0] ) ) {
512 return false;
513 }
514
515 return $backtrace[2]['args'][0];
516 }
517 }
518
519 add_action( 'admin_notices', array( 'LP_Addon', 'admin_errors' ) );
520