| 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 |
$this->load_text_domain(); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Define add-on constants. |
| 97 |
*/ |
| 98 |
protected function _define_constants() { |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Includes add-on files. |
| 103 |
*/ |
| 104 |
protected function _includes() { |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Check required version Addons. |
| 109 |
* |
| 110 |
* @return bool |
| 111 |
*/ |
| 112 |
public function check_require_version_addon(): bool { |
| 113 |
$flag = true; |
| 114 |
|
| 115 |
if ( version_compare( $this->lp_require_addon_version, $this->version, '>' ) ) { |
| 116 |
$flag = false; |
| 117 |
} |
| 118 |
|
| 119 |
if ( ! $flag ) { |
| 120 |
add_action( 'admin_notices', array( $this, 'admin_notice_require_addon_version' ) ); |
| 121 |
|
| 122 |
// Deactivate plugin . |
| 123 |
if ( ! empty( $this->plugin_base ) ) { |
| 124 |
//deactivate_plugins( $this->plugin_base ); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return $flag; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Check required version LP on Addon. |
| 133 |
* Should define require on each Addons by key "Require_LP_Version". |
| 134 |
* After long time, when ready standard, need check not key "Require_LP_Version" will deactivate addon. |
| 135 |
* |
| 136 |
* @return bool |
| 137 |
*/ |
| 138 |
public function check_require_version_lp(): bool { |
| 139 |
|
| 140 |
$flag = true; |
| 141 |
|
| 142 |
// If addon not set, return false. |
| 143 |
if ( empty( $this->require_version ) ) { |
| 144 |
$flag = false; |
| 145 |
} |
| 146 |
|
| 147 |
if ( version_compare( $this->require_version, LEARNPRESS_VERSION, '>' ) ) { |
| 148 |
$flag = false; |
| 149 |
} |
| 150 |
|
| 151 |
if ( ! $flag ) { |
| 152 |
add_action( 'admin_notices', array( $this, 'admin_notice_require_lp_version' ) ); |
| 153 |
|
| 154 |
// Deactivate plugin . |
| 155 |
if ( ! empty( $this->plugin_base ) ) { |
| 156 |
//deactivate_plugins( plugin_basename( $this->plugin_base ) ); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return $flag; |
| 161 |
} |
| 162 |
|
| 163 |
public function admin_notice_require_addon_version() { |
| 164 |
?> |
| 165 |
<div class="notice notice-error"> |
| 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> |
| 176 |
</div> |
| 177 |
<?php |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Admin notices |
| 182 |
*/ |
| 183 |
public function admin_notice_require_lp_version() { |
| 184 |
?> |
| 185 |
<div class="error"> |
| 186 |
<p> |
| 187 |
<?php |
| 188 |
printf( |
| 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 |
) |
| 194 |
), |
| 195 |
esc_html( $this->get_name() ), |
| 196 |
esc_html( $this->version ), |
| 197 |
esc_html( $this->require_version ), |
| 198 |
'| Can addon invalid key Require_LP_Version' |
| 199 |
); |
| 200 |
?> |
| 201 |
</p> |
| 202 |
</div> |
| 203 |
<?php |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* @return mixed |
| 208 |
*/ |
| 209 |
public function get_name() { |
| 210 |
preg_match( '/([A-z].*)\/([A-z].*)/', $this->plugin_base, $match ); |
| 211 |
|
| 212 |
$name_addon = ''; |
| 213 |
if ( isset( $match[1] ) ) { |
| 214 |
$name_addon = $match[1]; |
| 215 |
} |
| 216 |
|
| 217 |
return $name_addon; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Load text domain |
| 222 |
*/ |
| 223 |
public function load_text_domain() { |
| 224 |
if ( empty( $this->plugin_file ) ) { |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
$text_domain = empty( $this->text_domain ) ? $this->plugin_folder_name : $this->text_domain; |
| 229 |
if ( $text_domain ) { |
| 230 |
load_plugin_textdomain( $text_domain, false, $this->plugin_folder_name . '/languages' ); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Load Addon |
| 236 |
* |
| 237 |
* @param string $instance |
| 238 |
* @param string $path |
| 239 |
* @param string $plugin_file |
| 240 |
* |
| 241 |
* @return void|mixed |
| 242 |
*/ |
| 243 |
public static function load( string $instance = '', string $path = '', string $plugin_file = '' ) { |
| 244 |
$plugin_folder = ''; |
| 245 |
|
| 246 |
if ( $plugin_file ) { |
| 247 |
$plugin_folder = dirname( $plugin_file ); |
| 248 |
} |
| 249 |
|
| 250 |
if ( $plugin_folder ) { |
| 251 |
$path = "$plugin_folder/$path"; |
| 252 |
} |
| 253 |
|
| 254 |
if ( ! file_exists( $path ) ) { |
| 255 |
error_log( |
| 256 |
sprintf( |
| 257 |
__( '%s plugin file does not exist.', 'learnpress' ), |
| 258 |
$path |
| 259 |
) |
| 260 |
); |
| 261 |
|
| 262 |
return; |
| 263 |
} |
| 264 |
|
| 265 |
include_once $path; |
| 266 |
$addon_instance = null; |
| 267 |
|
| 268 |
if ( ! array_key_exists( $instance, self::$instances ) ) { |
| 269 |
if ( class_exists( $instance ) ) { |
| 270 |
$addon_instance = new $instance(); |
| 271 |
} |
| 272 |
|
| 273 |
if ( ! $addon_instance ) { |
| 274 |
error_log( |
| 275 |
sprintf( |
| 276 |
__( '%s plugin class does not exist.', 'learnpress' ), |
| 277 |
$instance |
| 278 |
) |
| 279 |
); |
| 280 |
|
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
$addon_instance->plugin_file = $plugin_file; |
| 285 |
|
| 286 |
self::$instances[ $instance ] = $addon_instance; |
| 287 |
} |
| 288 |
|
| 289 |
return self::$instances[ $instance ]; |
| 290 |
} |
| 291 |
|
| 292 |
public function get_plugin_url( $sub = '/' ) { |
| 293 |
return plugins_url( $sub, $this->plugin_file ); |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Get content template of addon. |
| 298 |
* |
| 299 |
* @param string $template_name |
| 300 |
* @param array $args |
| 301 |
* @param bool $include |
| 302 |
* |
| 303 |
* @since 3.0.0 |
| 304 |
* @version 1.0.1 |
| 305 |
*/ |
| 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'; |
| 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 |
); |
| 329 |
|
| 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 ); |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Get content template of addon. |
| 341 |
* |
| 342 |
* @param string $template_name |
| 343 |
* @param mixed $args |
| 344 |
* |
| 345 |
* @since 4.2.1 |
| 346 |
* @version 1.0.1 |
| 347 |
*/ |
| 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 ); |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Output content of admin view file. |
| 358 |
* |
| 359 |
* @param string $view |
| 360 |
* @param array $args |
| 361 |
* |
| 362 |
* @since x.x.x |
| 363 |
*/ |
| 364 |
public function admin_view( $view, $args = array() ) { |
| 365 |
$args['plugin_file'] = $this->plugin_file; |
| 366 |
learn_press_admin_view( $view, $args ); |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* @deprecated 4.2.0, wishlist v4.0.8 still use |
| 371 |
*/ |
| 372 |
public static function instance() { |
| 373 |
return new static(); |
| 374 |
/*$name = self::_get_called_class(); |
| 375 |
if ( false === $name ) { |
| 376 |
return false; |
| 377 |
} |
| 378 |
|
| 379 |
if ( empty( self::$instances[ $name ] ) ) { |
| 380 |
self::$instances[ $name ] = new $name(); |
| 381 |
} |
| 382 |
|
| 383 |
return self::$instances[ $name ];*/ |
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
* @return bool|string |
| 388 |
* @deprecated 4.2.0 |
| 389 |
*/ |
| 390 |
/*protected static function _get_called_class() { |
| 391 |
if ( function_exists( 'get_called_class' ) ) { |
| 392 |
return get_called_class(); |
| 393 |
} |
| 394 |
|
| 395 |
$backtrace = debug_backtrace(); |
| 396 |
|
| 397 |
if ( empty( $backtrace[2] ) ) { |
| 398 |
return false; |
| 399 |
} |
| 400 |
|
| 401 |
if ( empty( $backtrace[2]['args'][0] ) ) { |
| 402 |
return false; |
| 403 |
} |
| 404 |
|
| 405 |
return $backtrace[2]['args'][0]; |
| 406 |
}*/ |
| 407 |
} |
| 408 |
|