-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResource.php
73 lines (48 loc) · 1.41 KB
/
Resource.php
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
<?php
/**
* Helper for resource hinting
*
* @package ThemePlate
* @since 0.1.0
*/
namespace ThemePlate;
use ThemePlate\Resource\Handler;
use ThemePlate\Resource\Item;
class Resource {
private static array $storage = array();
private static Handler $handler;
public static function hint( string $directive, $resource, array $extra = array() ): void {
self::$storage[ $directive ][] = compact( 'resource', 'extra' );
}
public static function action(): void {
self::$handler = new Handler();
foreach ( self::$storage as $directive => $values ) {
foreach ( $values as $value ) {
if ( ! is_array( $value['resource'] ) ) {
self::handle( $value['resource'], $directive, $value['extra'] );
} else {
( new Item( $value['resource']['href'], $directive ) )
->extra( array_merge( $value['resource'], $value['extra'] ) )->tag();
}
}
}
self::$handler->action();
}
private static function handle( string $resource, string $directive, array $attributes ): void {
$type = 'url';
if ( wp_script_is( $resource ) ) {
$type = 'script';
} elseif ( wp_style_is( $resource ) ) {
$type = 'style';
}
if ( 'url' === $type ) {
$parsed = wp_parse_url( $resource );
if ( empty( $parsed['host'] ) ) {
return;
}
( new Item( $resource, $directive ) )->extra( $attributes )->tag();
} else {
self::$handler->{$type}( $resource, $directive, $attributes );
}
}
}