Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion htmxer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: htmxer
* Domain Path: /languages
* Version: 0.1.250405
* Version: 0.250915
*/

namespace HTMXer;
Expand Down
65 changes: 65 additions & 0 deletions includes/Endpoints.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

function htmxer_url($route)
{
$route = sanitize_text_field($route);
return home_url(user_trailingslashit('htmxer/'.ltrim($route, '/')));
}

function htmxer_hook($route)
{
return 'htmxer/ep/'.esc_url($route);
}

add_action('init', 'add_htmx_endpoint');
function add_htmx_endpoint()
{
// Add rewrite endpoint for 'htmxer' at the root
add_rewrite_endpoint('htmxer', EP_ROOT);

// Optional: Flush rewrite rules to ensure the endpoint is registered
// Note: This should only run once, e.g., on plugin activation
// Remove this in production after initial setup to avoid performance issues
// flush_rewrite_rules();
}

// Add the endpoint to query vars
add_filter('query_vars', 'add_htmx_query_vars');
function add_htmx_query_vars($vars)
{
$vars[] = 'htmxer';
return $vars;
}

// Handle the HTMX endpoint request
add_action('template_redirect', 'handle_htmx_endpoint');
function handle_htmx_endpoint()
{
// Check if this is an HTMX request
if (get_query_var('htmxer', false) !== false) {
$route = get_query_var('htmxer');
$route = esc_html($route);
$route = esc_attr($route);
$route = sanitize_text_field($route);

header('Content-Type: text/html; charset=UTF-8');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
header('Expires: 0');

try {
// $user = wp_get_current_user();
// var_dump($route);
do_action('htmxer/ep/'.$route);
http_response_code(200);
} catch (Exception $e) {
echo '<p>Произошла ошибка: '.esc_html($e->getMessage()).'</p>';
http_response_code(500);
}


exit;

}
}

4 changes: 3 additions & 1 deletion includes/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

add_action('wp_head', function (){
$config = apply_filters('htmxer/config', []);
echo '<meta name="htmx-config" content=\'' . json_encode($config) . '\'>';
?>
<meta name="htmx-config" content="<?= json_encode($config) ?>">
<?php
});

Settings::init();
Expand Down