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