Repository navigation
Expand file tree
/
Copy pathCustomData.php
More file actions
114 lines (72 loc) · 2.63 KB
/
Copy pathCustomData.php
File metadata and controls
114 lines (72 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* Helper for registered dependencies
*
* @package ThemePlate
* @since 0.1.0
*/
namespace ThemePlate\Enqueue;
use WP_Dependencies;
class CustomData {
/** @var array<string, array<string, mixed>> */
private array $scripts = array();
/** @var array<string, array<string, mixed>> */
private array $styles = array();
/**
* @param array<string, mixed> $data
*
* @return array<string, mixed>
*/
public function filter( array $data, string $type ): array {
if ( ! in_array( $type, array( 'scripts', 'styles' ), true ) ) {
_doing_it_wrong( __METHOD__, esc_attr( 'Only "scripts" and "styles" are known types' ), '2.3.0' );
return array();
}
_deprecated_function( __METHOD__, '2.4.0', esc_html( __NAMESPACE__ . '\\' . ucfirst( $type ) . 'Tag::filter_attributes' ) );
return ( 'scripts' === $type ? ScriptsTag::filter_attributes( $data ) : StylesTag::filter_attributes( $data ) );
}
/** @param array<string, mixed> $data */
public function add( string $type, string $handle, array $data ): void {
if ( ! in_array( $type, array( 'script', 'style' ), true ) ) {
_doing_it_wrong( __METHOD__, esc_attr( 'Only "script" and "style" are known types' ), '2.2.0' );
return;
}
_deprecated_function( __METHOD__, '2.4.0', esc_html( self::class . '::' . $type ) );
$this->{$type . 's'}[ $handle ] = $data;
}
/** @param array<string, mixed> $data */
public function script( string $handle, array $data ): void {
$this->scripts[ $handle ] = $data;
}
/** @param array<string, mixed> $data */
public function style( string $handle, array $data ): void {
$this->styles[ $handle ] = $data;
}
public function init(): void {
global $wp_scripts, $wp_styles;
/** @var WP_Dependencies $dependencies */
foreach ( array( $wp_scripts, $wp_styles ) as $dependencies ) {
if ( empty( $dependencies->queue ) ) {
continue;
}
$type = get_class( $dependencies );
$type = strtolower( substr( $type, 3 ) );
foreach ( $dependencies->registered as $dependency ) {
$specified = ( 'scripts' === $type ? ScriptsTag::filter_attributes( $dependency->extra ) : StylesTag::filter_attributes( $dependency->extra ) );
if ( array() !== $specified ) {
$this->{$type}[ $dependency->handle ] = $specified;
}
}
}
}
public function action(): void {
if ( array() !== $this->scripts ) {
$script_tag = new ScriptsTag( $this->scripts );
add_filter( 'script_loader_tag', array( $script_tag, 'filter' ), 10, 2 );
}
if ( array() !== $this->styles ) {
$style_tag = new StylesTag( $this->styles );
add_filter( 'style_loader_tag', array( $style_tag, 'filter' ), 10, 2 );
}
}
}