PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9
4.4.9 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 All 140 releases
← All changes | inc/abstracts/abstract-addon.php +129 -241 4.1.7.2 → 4.4.9 View file →
@@ -1,6 +1,8 @@
1 1 <?php
2 2
3 +use LearnPress\Helpers\Template;
4 +
3 5 /**
4 6 * Class LP_Addon
5 7 */
6 8 class LP_Addon {
@@ -8,51 +10,59 @@
8 10 * Current version of addon.
9 11 *
10 12 * @var string
11 13 */
12 - public $version = null;
13 -
14 + public $version = 0;
14 15 /**
15 - * Learnpress require addon version
16 - * Case:
16 + * LearnPress require addon version
17 17 *
18 18 * @var array
19 19 */
20 20 private $lp_require_addon_version = '4.0.0';
21 -
22 21 /**
23 22 * Required version for current version of addon.
24 23 *
25 24 * @var string
26 25 */
27 - public $require_version = null;
28 -
26 + public $require_version = 0;
29 27 /**
30 - * Path to addon.
28 + * Path to root file of addon.
31 29 *
32 30 * @var string
33 31 */
34 - public $plugin_file = null;
35 -
32 + public $plugin_file = '';
36 33 /**
37 - * Base addon.
34 + * @var string folder path root of addon.
35 + */
36 + public $plugin_folder_path = '';
37 + /**
38 + * Root folder name addon.
38 39 *
39 40 * @var string
40 41 */
41 - public $plugin_base = null;
42 -
42 + public $plugin_folder_name = '';
43 43 /**
44 - * Addon textdomain name.
44 + * Plugin base addon.
45 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 + *
46 58 * @var string
47 59 */
48 60 public $text_domain = '';
49 -
50 61 /**
51 62 * @var null
52 63 */
53 64 protected $_valid = null;
54 -
55 65 /**
56 66 * Singleton instance of the addon.
57 67 *
58 68 * @var array
@@ -57,14 +67,12 @@
57 67 *
58 68 * @var array
59 69 */
60 70 public static $instances = array();
61 -
62 71 /**
63 72 * @var array
64 73 */
65 74 protected static $_admin_notices = array();
66 -
67 75 /**
68 76 * @var string
69 77 */
70 78 protected $_template_path = '';
@@ -74,69 +82,15 @@
74 82 /**
75 83 * LP_Addon constructor.
76 84 */
77 85 public function __construct() {
78 -
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 );
79 90 $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 91 $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 92 $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 93 }
140 94
141 95 /**
142 96 * Define add-on constants.
@@ -141,9 +95,8 @@
141 95 /**
142 96 * Define add-on constants.
143 97 */
144 98 protected function _define_constants() {
145 -
146 99 }
147 100
148 101 /**
149 102 * Includes add-on files.
@@ -148,68 +101,11 @@
148 101 /**
149 102 * Includes add-on files.
150 103 */
151 104 protected function _includes() {
152 -
153 105 }
154 106
155 107 /**
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 108 * Check required version Addons.
213 109 *
214 110 * @return bool
215 111 */
@@ -224,9 +120,9 @@
224 120 add_action( 'admin_notices', array( $this, 'admin_notice_require_addon_version' ) );
225 121
226 122 // Deactivate plugin .
227 123 if ( ! empty( $this->plugin_base ) ) {
228 - deactivate_plugins( $this->plugin_base );
124 + //deactivate_plugins( $this->plugin_base );
229 125 }
230 126 }
231 127
232 128 return $flag;
@@ -256,9 +152,9 @@
256 152 add_action( 'admin_notices', array( $this, 'admin_notice_require_lp_version' ) );
257 153
258 154 // Deactivate plugin .
259 155 if ( ! empty( $this->plugin_base ) ) {
260 - deactivate_plugins( plugin_basename( $this->plugin_base ) );
156 + //deactivate_plugins( plugin_basename( $this->plugin_base ) );
261 157 }
262 158 }
263 159
264 160 return $flag;
@@ -266,9 +162,18 @@
266 162
267 163 public function admin_notice_require_addon_version() {
268 164 ?>
269 165 <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>
166 + <p>
167 + <?php
168 + printf(
169 + wp_kses_post( __( '<strong>LearnPress version %1$s require %2$s</strong> version %3$s or higher', 'learnpress' ) ),
170 + esc_html( LEARNPRESS_VERSION ),
171 + esc_html( $this->get_name() ),
172 + esc_html( $this->lp_require_addon_version )
173 + );
174 + ?>
175 + </p>
271 176 </div>
272 177 <?php
273 178 }
274 179
@@ -280,11 +185,13 @@
280 185 <div class="error">
281 186 <p>
282 187 <?php
283 188 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'
189 + wp_kses_post(
190 + __(
191 + '<strong>%1$s</strong> add-on version %2$s requires <strong>LearnPress</strong> version %3$s or higher %4$s',
192 + 'learnpress'
193 + )
287 194 ),
288 195 esc_html( $this->get_name() ),
289 196 esc_html( $this->version ),
290 197 esc_html( $this->require_version ),
@@ -313,32 +220,15 @@
313 220 /**
314 221 * Load text domain
315 222 */
316 223 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";
224 + if ( empty( $this->plugin_file ) ) {
225 + return;
326 226 }
327 227
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 -
228 + $text_domain = empty( $this->text_domain ) ? $this->plugin_folder_name : $this->text_domain;
339 229 if ( $text_domain ) {
340 - load_plugin_textdomain( $text_domain, false, plugin_basename( $plugin_path ) . '/languages' );
230 + load_plugin_textdomain( $text_domain, false, $this->plugin_folder_name . '/languages' );
341 231 }
342 232 }
343 233
344 234 /**
@@ -343,15 +233,15 @@
343 233
344 234 /**
345 235 * Load Addon
346 236 *
347 - * @param $instance
348 - * @param $path
237 + * @param string $instance
238 + * @param string $path
349 239 * @param string $plugin_file
350 240 *
351 241 * @return void|mixed
352 242 */
353 - public static function load( $instance, $path, $plugin_file = '' ) {
243 + public static function load( string $instance = '', string $path = '', string $plugin_file = '' ) {
354 244 $plugin_folder = '';
355 245
356 246 if ( $plugin_file ) {
357 247 $plugin_folder = dirname( $plugin_file );
@@ -357,15 +247,17 @@
357 247 $plugin_folder = dirname( $plugin_file );
358 248 }
359 249
360 250 if ( $plugin_folder ) {
361 - $path = "{$plugin_folder}/$path";
251 + $path = "$plugin_folder/$path";
362 252 }
363 253
364 254 if ( ! file_exists( $path ) ) {
365 - self::$_admin_notices['add-on-file-no-exists'] = sprintf(
366 - __( '%s plugin file does not exist.', 'learnpress' ),
367 - $path
255 + error_log(
256 + sprintf(
257 + __( '%s plugin file does not exist.', 'learnpress' ),
258 + $path
259 + )
368 260 );
369 261
370 262 return;
371 263 }
@@ -372,31 +264,30 @@
372 264
373 265 include_once $path;
374 266 $addon_instance = null;
375 267
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 {
268 + if ( ! array_key_exists( $instance, self::$instances ) ) {
269 + if ( class_exists( $instance ) ) {
381 270 $addon_instance = new $instance();
382 271 }
383 - }
384 272
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 - );
273 + if ( ! $addon_instance ) {
274 + error_log(
275 + sprintf(
276 + __( '%s plugin class does not exist.', 'learnpress' ),
277 + $instance
278 + )
279 + );
390 280
391 - return;
392 - }
281 + return;
282 + }
393 283
394 - $addon_instance->plugin_file = $plugin_file;
284 + $addon_instance->plugin_file = $plugin_file;
395 285
396 - self::$instances[ $instance ] = $addon_instance;
286 + self::$instances[ $instance ] = $addon_instance;
287 + }
397 288
398 - return $addon_instance;
289 + return self::$instances[ $instance ];
399 290 }
400 291
401 292 public function get_plugin_url( $sub = '/' ) {
402 293 return plugins_url( $sub, $this->plugin_file );
@@ -402,52 +293,65 @@
402 293 return plugins_url( $sub, $this->plugin_file );
403 294 }
404 295
405 296 /**
406 - * Get template path.
297 + * Get content template of addon.
407 298 *
408 - * @return string
299 + * @param string $template_name
300 + * @param array $args
301 + * @param bool $include
302 + *
303 + * @since 3.0.0
304 + * @version 1.0.1
409 305 */
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 - );
306 + public function get_template( string $template_name = '', array $args = [], bool $include = true ) {
307 + // Check path file not extension php, will add extension .php
308 + if ( ! preg_match( '/\.php$/', $template_name ) ) {
309 + $template_name .= '.php';
417 310 }
311 + $default_path = $this->plugin_folder_path . "/templates/$template_name";
312 + $folder_name_rewrite = learn_press_template_path();
313 + $from_child_theme_path = sprintf(
314 + '%s/%s/%s/%s/%s',
315 + get_stylesheet_directory(),
316 + $folder_name_rewrite,
317 + 'addons',
318 + str_replace( 'learnpress-', '', $this->plugin_folder_name ),
319 + $template_name
320 + );
321 + $from_theme_path = sprintf(
322 + '%s/%s/%s/%s/%s',
323 + get_template_directory(),
324 + $folder_name_rewrite,
325 + 'addons',
326 + str_replace( 'learnpress-', '', $this->plugin_folder_name ),
327 + $template_name
328 + );
418 329
419 - return $this->_template_path;
330 + $path_load = $default_path;
331 + if ( file_exists( $from_child_theme_path ) ) {
332 + $path_load = $from_child_theme_path;
333 + } elseif ( file_exists( $from_theme_path ) ) {
334 + $path_load = $from_theme_path;
335 + }
336 + Template::instance( $include )->get_template( $path_load, $args );
420 337 }
421 338
422 339 /**
423 - * Get content template of addon in theme or inside itself.
340 + * Get content template of addon.
424 341 *
425 342 * @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.
343 + * @param mixed $args
439 344 *
440 - * @param string $template_name
441 - *
442 - * @return string
345 + * @since 4.2.1
346 + * @version 1.0.1
443 347 */
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 - );
348 + public function get_admin_template( string $template_name = '', $args = [] ) {
349 + if ( ! preg_match( '/\.php$/', $template_name ) ) {
350 + $template_name .= '.php';
351 + }
352 + $template_path = "{$this->plugin_folder_path}/inc/admin/views/$template_name";
353 + Template::instance()->get_template( $template_path, $args );
450 354 }
451 355
452 356 /**
453 357 * Output content of admin view file.
@@ -452,9 +356,9 @@
452 356 /**
453 357 * Output content of admin view file.
454 358 *
455 359 * @param string $view
456 - * @param array $args
360 + * @param array $args
457 361 *
458 362 * @since x.x.x
459 363 */
460 364 public function admin_view( $view, $args = array() ) {
@@ -462,28 +366,13 @@
462 366 learn_press_admin_view( $view, $args );
463 367 }
464 368
465 369 /**
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
370 + * @deprecated 4.2.0, wishlist v4.0.8 still use
473 371 */
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 372 public static function instance() {
485 - $name = self::_get_called_class();
373 + return new static();
374 + /*$name = self::_get_called_class();
486 375 if ( false === $name ) {
487 376 return false;
488 377 }
489 378
@@ -490,15 +379,16 @@
490 379 if ( empty( self::$instances[ $name ] ) ) {
491 380 self::$instances[ $name ] = new $name();
492 381 }
493 382
494 - return self::$instances[ $name ];
383 + return self::$instances[ $name ];*/
495 384 }
496 385
497 386 /**
498 387 * @return bool|string
388 + * @deprecated 4.2.0
499 389 */
500 - protected static function _get_called_class() {
390 + /*protected static function _get_called_class() {
501 391 if ( function_exists( 'get_called_class' ) ) {
502 392 return get_called_class();
503 393 }
504 394
@@ -512,8 +402,6 @@
512 402 return false;
513 403 }
514 404
515 405 return $backtrace[2]['args'][0];
516 - }
406 + }*/
517 407 }
518 -
519 -add_action( 'admin_notices', array( 'LP_Addon', 'admin_errors' ) );