-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtmxer.php
79 lines (65 loc) · 1.94 KB
/
htmxer.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
<?php
/**
* Plugin Name: @ HTMXer
* Plugin URI: https://github.com/aiiddqd/htmxer
* Description: Add HTMX to WordPress
* License: GPL-2.0+
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: htmxer
* Domain Path: /languages
* Version: 0.1.241216
*/
namespace HTMXer;
use WP_REST_Request, WP_REST_Server;
require_once __DIR__ . '/includes/Settings.php';
foreach (glob(__DIR__ . '/includes/*.php') as $file) {
require_once $file;
}
foreach (glob(__DIR__ . '/includes/*/config.php') as $file) {
require_once $file;
}
/**
* Example
* url https://example.site/wp-json/htmxer/some_template
* hook add_action('htmxer/some_template', 'callback');
*/
function htmxer_actions(WP_REST_Request $request)
{
$hook = $request->get_param('hook');
$context = $request->get_header('context') ?? [];
if ($context) {
$context = json_decode($context, true);
} else {
$context = [];
}
header('Content-Type: text/html');
do_action('htmxer/' . $hook, $context, $request);
exit;
}
add_action('rest_api_init', function () {
global $wp;
//hack to fix authentication
if (wp_is_serving_rest_request()) {
if (strpos($wp->request, 'wp-json/htmxer/') === 0) {
remove_filter('rest_authentication_errors', 'rest_cookie_check_errors', 100);
}
}
register_rest_route('htmxer', '/(?P<hook>[a-zA-Z0-9\-_]+)', [
'methods' => WP_REST_Server::ALLMETHODS,
'callback' => 'htmxer_actions',
'permission_callback' => '__return_true',
]);
});
add_action('wp_footer', function () {
$context = apply_filters('htmxer/context', []);
if (empty($context)) {
return;
}
?>
<script>
document.body.addEventListener('htmx:configRequest', function (event) {
event.detail.headers['context'] = '<?= json_encode($context) ?>';
});
</script>
<?php
}, 555);