PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.4.8
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.4.8
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.4.8, at includes/Utils/Enqueue.php

80 lines 2.7 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_localize_script( $handle, $name, $args );
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 ( file_exists( $asset_config_path ) ) {
45 $asset_config = require $asset_config_path;
46
47 if ( $is_js ) {
48 $dependencies = array_unique( array_merge( $asset_config['dependencies'], $dependencies, ['regenerator-runtime'] ) );
49 }
50 $version = $asset_config['version'];
51 }
52 else if ((defined('TEMPLATELY_DEV') && TEMPLATELY_DEV) || (defined('WP_DEBUG') && WP_DEBUG)){
53 $version = time() ?? $version;
54 }
55
56 return [
57 'url' => $url,
58 'dependencies' => $dependencies,
59 'version' => $version,
60 'type' => $is_js ? 'script' : 'style',
61 'args' => null !== $args ? $args : ( $is_js ? false : 'all' ),
62 ];
63 }
64
65 public function asset_url( $filename ){
66 if( filter_var( $filename, FILTER_VALIDATE_URL ) ) {
67 return $filename;
68 }
69
70 return $this->plugin_url . 'assets/' . $filename;
71 }
72
73 public function dist_path( $file ){
74 return path_join( $this->plugin_path, 'assets/' . $file );
75 }
76
77 public function icon( $name ) {
78 return $this->plugin_url . 'assets/images/' . $name . '?v=' . $this->version;
79 }
80 }