-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTMX.php
101 lines (68 loc) · 1.81 KB
/
HTMX.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
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
<?php
/**
* @package ThemePlate
*/
namespace ThemePlate;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use ThemePlate\HTMX\Handler;
use ThemePlate\HTMX\Helpers;
use ThemePlate\HTMX\Router;
use ThemePlate\HTMX\Loader;
class HTMX {
public readonly Router $router;
public readonly Loader $loader;
protected ?string $cdn = null;
public function __construct( string $identifier = '' ) {
$this->router = new Router( $identifier );
$this->loader = new Loader( Helpers::caller_path() . $this->router->prefix );
}
public function setup(): void {
$iterator = new RecursiveDirectoryIterator( $this->loader->location );
foreach ( new RecursiveIteratorIterator( $iterator ) as $item ) {
if ( ! $item->isFile() || $item->getExtension() !== 'php' ) {
continue;
}
$path = str_replace(
array(
$this->loader->location . DIRECTORY_SEPARATOR,
'.php',
),
'',
$item->getPathname()
);
$handler = new Handler( 'HX-Request' );
foreach ( Helpers::HTTP_METHODS as $method ) {
$handler->handle( $method, array( $this->loader, 'load' ) );
}
$this->router->add( $path, $handler );
}
$this->router->init();
}
public function cdn( string $version = 'latest' ): HTMX {
$this->cdn = $version;
return $this;
}
public function assets(): void {
if ( $this->cdn ) {
$version = '@' . $this->cdn;
if ( '@latest' === $version ) {
$version = '';
}
wp_enqueue_script(
'htmx',
'https://unpkg.com/htmx.org' . $version,
array(),
$this->cdn,
array(
'in_footer' => true,
)
);
}
$config = sprintf(
"document.body.addEventListener('htmx:configRequest', function(evt) {evt.detail.headers['HX-Nonce'] = '%s'});",
wp_create_nonce( $this->router->prefix )
);
wp_add_inline_script( 'htmx', $config );
}
}