| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class LP_Abstract_Assets |
| 5 |
* |
| 6 |
* Abstract class for managing assets |
| 7 |
*/ |
| 8 |
abstract class LP_Abstract_Assets { |
| 9 |
|
| 10 |
protected $_cache = ''; |
| 11 |
|
| 12 |
/** |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
protected $_scripts = array(); |
| 16 |
|
| 17 |
/** |
| 18 |
* @var array |
| 19 |
*/ |
| 20 |
protected $_styles = array(); |
| 21 |
|
| 22 |
/** |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
protected $_script_data = array(); |
| 26 |
/** |
| 27 |
* Path file min |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
public static $_min_assets = '.min'; |
| 32 |
/** |
| 33 |
* Version file asset |
| 34 |
* |
| 35 |
* @var mixed|string |
| 36 |
*/ |
| 37 |
public static $_version_assets = LEARNPRESS_VERSION; |
| 38 |
/** |
| 39 |
* Path file |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
public static $_folder_source = ''; |
| 44 |
|
| 45 |
/** |
| 46 |
* LP_Abstract_Assets constructor. |
| 47 |
*/ |
| 48 |
protected function __construct() { |
| 49 |
$priority = 1000; |
| 50 |
|
| 51 |
if ( LP_Debug::is_debug() ) { |
| 52 |
self::$_min_assets = ''; |
| 53 |
self::$_version_assets = uniqid(); |
| 54 |
self::$_folder_source = 'src/'; |
| 55 |
} |
| 56 |
|
| 57 |
if ( is_admin() ) { |
| 58 |
add_action( 'admin_enqueue_scripts', array( $this, 'load_scripts' ) ); |
| 59 |
add_action( 'admin_print_footer_scripts', array( $this, 'localize_printed_admin_scripts' ) ); |
| 60 |
} else { |
| 61 |
add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ), $priority ); |
| 62 |
add_action( 'wp_print_scripts', array( $this, 'localize_printed_scripts' ), $priority + 10 ); |
| 63 |
//add_action( 'wp_print_footer_scripts', array( $this, 'localize_printed_scripts' ), $priority + 10 ); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
abstract function load_scripts(); |
| 68 |
|
| 69 |
/** |
| 70 |
* Default scripts |
| 71 |
* |
| 72 |
* @return array |
| 73 |
*/ |
| 74 |
protected function _get_scripts(): array { |
| 75 |
return array(); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Default styles |
| 80 |
* |
| 81 |
* @return array |
| 82 |
*/ |
| 83 |
protected function _get_styles(): array { |
| 84 |
return array(); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Register/Enqueue script |
| 89 |
* |
| 90 |
* @param string $page_current |
| 91 |
* @author tungnx |
| 92 |
* @since 4.0.0 |
| 93 |
* @version 1.0.1 |
| 94 |
*/ |
| 95 |
protected function handle_js( string $page_current = '' ) { |
| 96 |
$scripts = $this->_get_scripts(); |
| 97 |
/** |
| 98 |
* @var LP_Asset_Key[] $scripts |
| 99 |
*/ |
| 100 |
foreach ( $scripts as $handle => $script ) { |
| 101 |
if ( ! $script instanceof LP_Asset_Key ) { |
| 102 |
continue; |
| 103 |
} |
| 104 |
|
| 105 |
// For version addon. |
| 106 |
if ( ! LP_Debug::is_debug() && ! empty( $script->_version ) ) { |
| 107 |
self::$_version_assets = $script->_version; |
| 108 |
} |
| 109 |
// End |
| 110 |
|
| 111 |
wp_register_script( $handle, $script->_url, $script->_deps, self::$_version_assets, $script->_in_footer ); |
| 112 |
|
| 113 |
if ( ! $script->_only_register ) { |
| 114 |
$can_load_js = $this->check_can_load_asset( $handle, $page_current, $script->_screens, $script->_exclude_screens ); |
| 115 |
|
| 116 |
if ( $can_load_js ) { |
| 117 |
wp_enqueue_script( $handle ); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Set translate on file js of folder js/dist |
| 124 |
* Path translate of a string on file ".pot" if have must map to js/dist |
| 125 |
*/ |
| 126 |
wp_set_script_translations( 'lp-quiz', 'learnpress' ); |
| 127 |
wp_set_script_translations( 'lp-admin', 'learnpress' ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Register/Enqueue style |
| 132 |
* |
| 133 |
* @param string $page_current |
| 134 |
* @author tungnx |
| 135 |
* @since 4.1.3 |
| 136 |
* @version 1.0.0 |
| 137 |
*/ |
| 138 |
protected function handle_style( string $page_current = '' ) { |
| 139 |
$styles = $this->_get_styles(); |
| 140 |
if ( $styles ) { |
| 141 |
/** |
| 142 |
* @var LP_Asset_Key[] $style |
| 143 |
*/ |
| 144 |
foreach ( $styles as $handle => $style ) { |
| 145 |
if ( ! $style instanceof LP_Asset_Key ) { |
| 146 |
continue; |
| 147 |
} |
| 148 |
|
| 149 |
// For version addon. |
| 150 |
if ( ! LP_Debug::is_debug() && ! empty( $style->_version ) ) { |
| 151 |
self::$_version_assets = $style->_version; |
| 152 |
} |
| 153 |
// End |
| 154 |
|
| 155 |
wp_register_style( $handle, $style->_url, $style->_deps, self::$_version_assets ); |
| 156 |
|
| 157 |
if ( ! $style->_only_register ) { |
| 158 |
$can_load_style = $this->check_can_load_asset( $handle, $page_current, $style->_screens, $style->_exclude_screens ); |
| 159 |
|
| 160 |
if ( $can_load_style ) { |
| 161 |
wp_enqueue_style( $handle ); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Check file assets can load on pages |
| 170 |
* |
| 171 |
* @param string $handle |
| 172 |
* @param string $page_current |
| 173 |
* @param array $include_screens |
| 174 |
* @param array $exclude_screens |
| 175 |
* @author tungnx |
| 176 |
* @since 4.1.3 |
| 177 |
* @version 1.0.0 |
| 178 |
* |
| 179 |
* @return bool |
| 180 |
*/ |
| 181 |
protected function check_can_load_asset( string $handle, string $page_current, array $include_screens, array $exclude_screens ): bool { |
| 182 |
$can_load = false; |
| 183 |
|
| 184 |
if ( ! empty( $include_screens ) ) { |
| 185 |
if ( in_array( $page_current, $include_screens ) ) { |
| 186 |
$can_load = true; |
| 187 |
} |
| 188 |
} elseif ( ! empty( $exclude_screens ) ) { |
| 189 |
if ( ! in_array( $page_current, $exclude_screens ) ) { |
| 190 |
$can_load = true; |
| 191 |
} |
| 192 |
} else { |
| 193 |
$can_load = true; |
| 194 |
} |
| 195 |
|
| 196 |
$is_on = 'admin'; |
| 197 |
if ( ! is_admin() ) { |
| 198 |
$is_on = 'frontend'; |
| 199 |
} |
| 200 |
|
| 201 |
return apply_filters( |
| 202 |
'learnpress/' . $is_on . '/can-load-assets/' . $handle, |
| 203 |
$can_load, |
| 204 |
$page_current, |
| 205 |
$include_screens |
| 206 |
); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Register style |
| 211 |
* |
| 212 |
* @param $handle |
| 213 |
* @param $src |
| 214 |
* @param array $deps |
| 215 |
* @param bool $ver |
| 216 |
* @param string $media |
| 217 |
*/ |
| 218 |
public function register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) { |
| 219 |
if ( ! isset( $this->_styles[ $handle ] ) ) { |
| 220 |
$this->_styles[ $handle ] = array( $handle, $src, $deps, $ver, $media ); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Register script |
| 226 |
* |
| 227 |
* @param $handle |
| 228 |
* @param $src |
| 229 |
* @param array $deps |
| 230 |
* @param bool $ver |
| 231 |
* @param bool $in_footer |
| 232 |
*/ |
| 233 |
public function register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) { |
| 234 |
if ( ! isset( $this->_scripts[ $handle ] ) ) { |
| 235 |
$this->_scripts[ $handle ] = array( $handle, $src, $deps, $ver, $in_footer ); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Enqueue style |
| 241 |
* |
| 242 |
* @param $handle |
| 243 |
* @param string $src |
| 244 |
* @param array $deps |
| 245 |
* @param bool $ver |
| 246 |
* @param string $media |
| 247 |
*/ |
| 248 |
public function enqueue_style( $handle, $src = '', $deps = array(), $ver = false, $media = 'all' ) { |
| 249 |
$this->register_style( $handle, $src, $deps, $ver, $media ); |
| 250 |
if ( did_action( 'init' ) || did_action( 'admin_enqueue_scripts' ) || did_action( 'wp_enqueue_scripts' ) || did_action( 'login_enqueue_scripts' ) ) { |
| 251 |
wp_enqueue_style( $handle, $src, $deps, $ver, $media ); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Enqueue script |
| 257 |
* |
| 258 |
* @param $handle |
| 259 |
* @param $src |
| 260 |
* @param array $deps |
| 261 |
* @param bool $ver |
| 262 |
* @param bool $in_footer |
| 263 |
*/ |
| 264 |
public function enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) { |
| 265 |
$this->register_script( $handle, $src, $deps, $ver, $in_footer ); |
| 266 |
if ( did_action( 'init' ) || did_action( 'admin_enqueue_scripts' ) || did_action( 'wp_enqueue_scripts' ) || did_action( 'login_enqueue_scripts' ) ) { |
| 267 |
wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
public function add_script_data( $handle, $key_or_array, $value = '' ) { |
| 272 |
if ( empty( $this->_script_data[ $handle ] ) ) { |
| 273 |
$this->_script_data[ $handle ] = array(); |
| 274 |
} |
| 275 |
|
| 276 |
if ( func_num_args() == 2 && is_array( $key_or_array ) ) { |
| 277 |
$this->_script_data[ $handle ] = LP_Helper::array_merge_recursive( $this->_script_data[ $handle ], $key_or_array ); |
| 278 |
} else { |
| 279 |
$this->_script_data[ $handle ][ $key_or_array ] = $value; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
protected function _get_wp_styles() { |
| 284 |
global $wp_styles; |
| 285 |
|
| 286 |
if ( empty( $wp_styles ) ) { |
| 287 |
$wp_styles = new WP_Styles(); |
| 288 |
} |
| 289 |
|
| 290 |
return $wp_styles; |
| 291 |
} |
| 292 |
|
| 293 |
protected function _get_wp_scripts() { |
| 294 |
global $wp_scripts; |
| 295 |
|
| 296 |
if ( empty( $wp_scripts ) ) { |
| 297 |
$wp_scripts = new WP_Scripts(); |
| 298 |
} |
| 299 |
|
| 300 |
return $wp_scripts; |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* @param $handle |
| 305 |
* |
| 306 |
* @return string |
| 307 |
*/ |
| 308 |
public function get_script_var_name( $handle ) { |
| 309 |
$handle = str_replace( array( 'learn-press', 'lp', '_', '-' ), ' ', $handle ); |
| 310 |
$handle = ucwords( $handle ); |
| 311 |
|
| 312 |
return 'lp' . str_replace( ' ', '', $handle ) . 'Settings'; |
| 313 |
} |
| 314 |
|
| 315 |
public function localize_printed_scripts( $side = '' ) { |
| 316 |
$scripts_data = $this->_get_script_data(); |
| 317 |
|
| 318 |
if ( is_array( $scripts_data ) && is_array( $this->_script_data ) ) { |
| 319 |
$scripts_data = LP_Helper::array_merge_recursive( $scripts_data, $this->_script_data ); |
| 320 |
} elseif ( is_array( $this->_script_data ) ) { |
| 321 |
$scripts_data = $this->_script_data; |
| 322 |
} |
| 323 |
|
| 324 |
if ( ! $scripts_data ) { |
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
global $wp_scripts; |
| 329 |
|
| 330 |
if ( ! $wp_scripts ) { |
| 331 |
$wp_scripts = new WP_Scripts(); |
| 332 |
} |
| 333 |
|
| 334 |
foreach ( $scripts_data as $handle => $data ) { |
| 335 |
$data = apply_filters( 'learn-press/script-data', $data, $handle ); |
| 336 |
wp_localize_script( $handle, $this->get_script_var_name( $handle ), $data ); |
| 337 |
|
| 338 |
// comment by tungnx |
| 339 |
// Edit: Use in certificate - Nhamdv. |
| 340 |
if ( isset( $wp_scripts->registered[ $handle ] ) ) { |
| 341 |
if ( isset( $wp_scripts->registered[ $handle ]->extra['data'] ) ) { |
| 342 |
if ( $wp_scripts->registered[ $handle ]->extra['data'] ) { |
| 343 |
$data = $wp_scripts->registered[ $handle ]->extra['data']; |
| 344 |
$data = preg_replace_callback( '~:"(([0-9]+)([.,]?)([0-9]?)|true|false)"~', array( $this, '_valid_json_number' ), $data ); |
| 345 |
|
| 346 |
$wp_scripts->registered[ $handle ]->extra['data'] = $data; |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
if ( is_admin() ) { |
| 352 |
$wp_scripts->print_extra_script( $handle ); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
} |
| 357 |
|
| 358 |
public function localize_printed_admin_scripts() { |
| 359 |
$this->localize_printed_scripts( 'admin' ); |
| 360 |
} |
| 361 |
|
| 362 |
protected function _valid_json_number( $m ) { |
| 363 |
return str_replace( array( ':"', '"' ), array( ':', '' ), $m[0] ); |
| 364 |
} |
| 365 |
|
| 366 |
protected function _get_script_data() { |
| 367 |
return array(); |
| 368 |
} |
| 369 |
|
| 370 |
public function add_localize( $handle, $key_or_array, $value = '' ) { |
| 371 |
if ( empty( $this->_script_data[ $handle ] ) ) { |
| 372 |
$this->_script_data[ $handle ] = array(); |
| 373 |
} |
| 374 |
if ( is_array( $key_or_array ) ) { |
| 375 |
$this->_script_data[ $handle ] = array_merge( $this->_script_data[ $handle ], $key_or_array ); |
| 376 |
} else { |
| 377 |
$this->_script_data[ $handle ][ $key_or_array ] = $value; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Shortcut to Addon file url. |
| 383 |
* |
| 384 |
* @param string $file |
| 385 |
* |
| 386 |
* @return string |
| 387 |
*/ |
| 388 |
public function url( string $file = '' ): string { |
| 389 |
return LP_PLUGIN_URL . "assets/{$file}"; |
| 390 |
} |
| 391 |
|
| 392 |
/*public static function add_param() { |
| 393 |
|
| 394 |
}*/ |
| 395 |
} |
| 396 |
|