111 lines
3.2 KiB
PHP
111 lines
3.2 KiB
PHP
<?php
|
|
// Copyright 2026 Umbra Tools (umbratools.com)
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
/**
|
|
* Short URL Rewrites - Override BookHelper for direct template rendering
|
|
*
|
|
* @package BD_Murphy
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Override BookHelper short link handling
|
|
* This runs earlier to redirect to the correct book page template
|
|
*/
|
|
function bdm_override_shortlink() {
|
|
if (is_admin()) {
|
|
return;
|
|
}
|
|
|
|
// First check for custom short links (bd_shortlink post type)
|
|
$request_uri = $_SERVER['REQUEST_URI'];
|
|
$request_uri = trim(parse_url($request_uri, PHP_URL_PATH), '/');
|
|
|
|
if (!empty($request_uri)) {
|
|
// Check for bd_shortlink post type
|
|
global $wpdb;
|
|
|
|
$shortlink_id = $wpdb->get_var($wpdb->prepare(
|
|
"SELECT ID FROM {$wpdb->posts} WHERE post_type = 'bd_shortlink' AND post_name = %s AND post_status = 'publish' LIMIT 1",
|
|
$request_uri
|
|
));
|
|
|
|
if ($shortlink_id) {
|
|
$target_url = get_post_meta($shortlink_id, '_target_url', true);
|
|
$redirect_type = get_post_meta($shortlink_id, '_click_count', true) ? get_post_meta($shortlink_id, '_redirect_type', true) : '301';
|
|
$redirect_type = $redirect_type ?: '301';
|
|
|
|
// Increment click count
|
|
$click_count = get_post_meta($shortlink_id, '_click_count', true);
|
|
update_post_meta($shortlink_id, '_click_count', intval($click_count) + 1);
|
|
|
|
// Redirect
|
|
wp_redirect($target_url, intval($redirect_type));
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// Then check for book short links
|
|
$short_slug = get_query_var('book_short_slug');
|
|
|
|
if (empty($short_slug)) {
|
|
return;
|
|
}
|
|
|
|
$book_id = $wpdb->get_var($wpdb->prepare(
|
|
"SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'short_link_slug' AND meta_value = %s LIMIT 1",
|
|
$short_slug
|
|
));
|
|
|
|
if (!$book_id) {
|
|
return;
|
|
}
|
|
|
|
$post = get_post($book_id);
|
|
|
|
if (!$post || $post->post_type !== 'book') {
|
|
return;
|
|
}
|
|
|
|
// Directly query the book and render single-book.php
|
|
global $wp_query, $post;
|
|
$wp_query = new WP_Query(array('p' => $book_id, 'post_type' => 'book'));
|
|
$post = get_post($book_id);
|
|
|
|
// Get the template
|
|
$page_template = get_post_meta($book_id, '_wp_page_template', true);
|
|
$template = $page_template ?: 'single-book.php';
|
|
|
|
// Include the template directly
|
|
$template_file = get_template_directory() . '/' . $template;
|
|
if (!file_exists($template_file)) {
|
|
$template_file = get_template_directory() . '/single-book.php';
|
|
}
|
|
|
|
// Set up WordPress for the template
|
|
setup_postdata($post);
|
|
|
|
// Include the template using get_template_part
|
|
$template_name = str_replace('.php', '', basename($template_file));
|
|
get_template_part($template_name);
|
|
exit;
|
|
}
|
|
add_action('template_redirect', 'bdm_override_shortlink', 1);
|
|
|
|
/**
|
|
* Add book_short_slug query var
|
|
*
|
|
* @param array $vars Query vars.
|
|
* @return array
|
|
*/
|
|
function bdm_add_book_short_slug_var($vars) {
|
|
$vars[] = 'book_short_slug';
|
|
$vars[] = 'short_link_slug';
|
|
return $vars;
|
|
}
|
|
add_filter('query_vars', 'bdm_add_book_short_slug_var');
|