PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.7.4
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.7.4
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / Utils / Enqueue.php

Enqueue.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.7.4, at includes/Utils/Enqueue.php

88 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Templately\Utils;
3
4 class Enqueue extends Base {
5 private $plugin_url;
6 private $plugin_path;
7 private $version;
8
9 public function __construct( $plugin_url, $plugin_path, $version ){
10 $this->plugin_url = $plugin_url;
11 $this->plugin_path = $plugin_path;
12 $this->version = $version;
13 }
14
15 public function enqueue( $handle, $filename, $dependencies = [], $args = null ) {
16 $config = $this->asset_config( $filename, $dependencies, $args );
17 $this->call_wp_func( 'wp_enqueue', $handle, $config );
18 }
19
20 public function register( $handle, $filename, $dependencies = [], $args = null ) {
21 $config = $this->asset_config( $filename, $dependencies, $args );
22 $this->call_wp_func( 'wp_register', $handle, $config );
23 }
24
25 public function localize( $handle, $name, $args ){
26 wp_add_inline_script( $handle, 'var ' . $name . ' = ' . wp_json_encode( $args ) . ';', 'before' );
27 }
28
29 private function call_wp_func( $action, $handle, $config ) {
30 call_user_func( $action . '_' . $config['type'], $handle, $config['url'], $config['dependencies'], $config['version'], $config['args'] );
31
32 if ( 'script' === $config['type'] && in_array( 'wp-i18n', $config['dependencies'], true ) ) {
33 wp_set_script_translations( $handle, 'templately' );
34 }
35 }
36
37 public function asset_config( $filename, $dependencies = [], $args = null ) {
38 $is_js = preg_match( '/\.js$/', $filename );
39 $basename = preg_replace( '/\.\w+$/', '', $filename );
40 $url = $this->asset_url( $filename );
41 $version = $this->version;
42 $asset_config_path = $this->dist_path( $basename . '.asset.php' );
43
44 if ( ! $is_js && ! file_exists( $asset_config_path ) ) {
45 $js_basename = str_replace( 'css/', 'js/', $basename );
46 $js_asset_path = $this->dist_path( $js_basename . '.asset.php' );
47 if ( file_exists( $js_asset_path ) ) {
48 $asset_config_path = $js_asset_path;
49 }
50 }
51
52 if ( file_exists( $asset_config_path ) ) {
53 $asset_config = require $asset_config_path;
54
55 if ( $is_js ) {
56 $dependencies = array_unique( array_merge( $asset_config['dependencies'], $dependencies, ['regenerator-runtime'] ) );
57 }
58 $version = $asset_config['version'];
59 }
60 else if ((defined('TEMPLATELY_DEV') && TEMPLATELY_DEV) || (defined('WP_DEBUG') && WP_DEBUG)){
61 $version = time();
62 }
63
64 return [
65 'url' => $url,
66 'dependencies' => $dependencies,
67 'version' => $version,
68 'type' => $is_js ? 'script' : 'style',
69 'args' => null !== $args ? $args : ( $is_js ? false : 'all' ),
70 ];
71 }
72
73 public function asset_url( $filename ){
74 if( filter_var( $filename, FILTER_VALIDATE_URL ) ) {
75 return $filename;
76 }
77
78 return $this->plugin_url . 'assets/' . $filename;
79 }
80
81 public function dist_path( $file ){
82 return path_join( $this->plugin_path, 'assets/' . $file );
83 }
84
85 public function icon( $name ) {
86 return $this->plugin_url . 'assets/images/' . $name . '?v=' . $this->version;
87 }
88 }