| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
class ESHB_Templates { |
| 4 |
|
| 5 |
|
| 6 |
private static $_instance = null; |
| 7 |
|
| 8 |
public static function instance() { |
| 9 |
|
| 10 |
if ( is_null( self::$_instance ) ) { |
| 11 |
self::$_instance = new self(); |
| 12 |
} |
| 13 |
return self::$_instance; |
| 14 |
|
| 15 |
} |
| 16 |
|
| 17 |
public function __construct() { |
| 18 |
|
| 19 |
add_action( 'plugins_loaded', [ $this, 'init' ] ); |
| 20 |
|
| 21 |
} |
| 22 |
|
| 23 |
public function init() { |
| 24 |
add_filter('template_include', [$this, 'eshb_accomodation_templates']); |
| 25 |
add_filter( 'get_block_templates', [$this, 'eshb_register_accomodation_block_template'], 10, 3 ); |
| 26 |
add_filter('the_content', [$this, 'eshb_page_contents']); |
| 27 |
add_filter('the_content', [$this, 'eshb_search_result_page_contents']); |
| 28 |
} |
| 29 |
|
| 30 |
function eshb_is_block_theme() { |
| 31 |
return function_exists( 'wp_is_block_theme' ) && wp_is_block_theme(); |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
public function eshb_accomodation_templates($template) { |
| 36 |
// Check if the post type is 'eshb_accomodation' |
| 37 |
if (is_singular('eshb_accomodation')) { |
| 38 |
if ( $this->eshb_is_block_theme() ) { |
| 39 |
return $template; |
| 40 |
} |
| 41 |
$eshb_settings = get_option('eshb_settings', []); |
| 42 |
$template_style = 'style-one'; |
| 43 |
|
| 44 |
// Check if plugin settings and archive-page are available |
| 45 |
if (isset($eshb_settings['single-page-template-style']) && !empty($eshb_settings['single-page-template-style'])) { |
| 46 |
$template_style = $eshb_settings['single-page-template-style']; // Return original content if settings are not properly set |
| 47 |
} |
| 48 |
|
| 49 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended |
| 50 |
if (!empty($_GET['template-style'])) { |
| 51 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended |
| 52 |
if(!empty($_GET['template-style'])){ |
| 53 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended |
| 54 |
$template_style = sanitize_text_field( wp_unslash( $_GET['template-style'] ) ); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// Get the page ID from the plugin settings |
| 59 |
$page_id = $eshb_settings['archive-page']; |
| 60 |
|
| 61 |
// Check if the custom template exists in your plugin directory |
| 62 |
$plugin_template = ESHB_PL_PATH . 'public/templates/single/single-eshb_accomodation-' . $template_style . '.php'; |
| 63 |
$theme_template = get_stylesheet_directory() . '/easy-hotel/templates/single/single-eshb_accomodation-' . $template_style . '.php'; |
| 64 |
$child_theme_template = get_template_directory() . '/easy-hotel/templates/single/single-eshb_accomodation-' . $template_style . '.php'; |
| 65 |
|
| 66 |
if (file_exists($child_theme_template)) { |
| 67 |
$template = $child_theme_template; |
| 68 |
} elseif (file_exists($theme_template)) { |
| 69 |
$template = $theme_template; |
| 70 |
} else { |
| 71 |
$template = $plugin_template; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
if (file_exists($template)) { |
| 76 |
// Use the custom template |
| 77 |
return $template; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if (is_post_type_archive('eshb_accomodation') || is_tax( 'eshb_category' )) { |
| 82 |
if ( $this->eshb_is_block_theme() ) { |
| 83 |
return $template; |
| 84 |
} |
| 85 |
// Check if the custom template exists in your plugin directory |
| 86 |
$plugin_template = ESHB_PL_PATH . 'public/templates/archive-eshb_accomodation.php'; |
| 87 |
$theme_template = get_stylesheet_directory() . '/easy-hotel/templates/archive-eshb_accomodation.php'; |
| 88 |
$child_theme_template = get_template_directory() . '/easy-hotel/templates/archive-eshb_accomodation.php'; |
| 89 |
|
| 90 |
if (file_exists($child_theme_template)) { |
| 91 |
$template = $child_theme_template; |
| 92 |
} elseif (file_exists($theme_template)) { |
| 93 |
$template = $theme_template; |
| 94 |
} else { |
| 95 |
$template = $plugin_template; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
if (file_exists($template)) { |
| 100 |
// Use the custom template |
| 101 |
return $template; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
// Return the default template if the custom template is not found |
| 106 |
return $template; |
| 107 |
} |
| 108 |
|
| 109 |
public function eshb_page_contents($content) { |
| 110 |
// Guard against recursion: rendering the grid runs get_the_excerpt(), |
| 111 |
// which re-applies the 'the_content' filter. Without this guard that |
| 112 |
// re-entry would render the grid again, causing an infinite loop. |
| 113 |
static $rendering = false; |
| 114 |
if ($rendering) { |
| 115 |
return $content; |
| 116 |
} |
| 117 |
|
| 118 |
// Retrieve the plugin settings |
| 119 |
$eshb_settings = get_option('eshb_settings'); |
| 120 |
|
| 121 |
// Check if plugin settings and archive-page are available |
| 122 |
if (empty($eshb_settings) || empty($eshb_settings['archive-page'])) { |
| 123 |
return $content; // Return original content if settings are not properly set |
| 124 |
} |
| 125 |
|
| 126 |
// Get the page ID from the plugin settings |
| 127 |
$page_id = $eshb_settings['archive-page']; |
| 128 |
|
| 129 |
|
| 130 |
// Get the current page ID |
| 131 |
$selected_page_id = get_queried_object_id(); |
| 132 |
|
| 133 |
|
| 134 |
// If the current page ID matches the specified archive page ID, replace content with shortcode |
| 135 |
if ($selected_page_id == $page_id) { |
| 136 |
$rendering = true; |
| 137 |
$output = do_shortcode('[eshb_accomodation_grid]'); |
| 138 |
$rendering = false; |
| 139 |
return $output; |
| 140 |
} |
| 141 |
|
| 142 |
// If IDs don't match, return the original content |
| 143 |
return $content; |
| 144 |
} |
| 145 |
|
| 146 |
public function eshb_search_result_page_contents($content) { |
| 147 |
// Guard against recursion: rendering the grid runs get_the_excerpt(), |
| 148 |
// which re-applies the 'the_content' filter. Without this guard that |
| 149 |
// re-entry would render the grid again, causing an infinite loop. |
| 150 |
static $rendering = false; |
| 151 |
if ($rendering) { |
| 152 |
return $content; |
| 153 |
} |
| 154 |
|
| 155 |
// Retrieve the plugin settings |
| 156 |
$eshb_settings = get_option('eshb_settings'); |
| 157 |
|
| 158 |
// Check if plugin settings and archive-page are available |
| 159 |
if (empty($eshb_settings) || empty($eshb_settings['search-result-page'])) { |
| 160 |
return $content; // Return original content if settings are not properly set |
| 161 |
} |
| 162 |
|
| 163 |
// Get the page ID from the plugin settings |
| 164 |
$search_result_page_id = $eshb_settings['search-result-page']; |
| 165 |
|
| 166 |
// Get the current page ID |
| 167 |
$selected_page_id = get_queried_object_id(); |
| 168 |
|
| 169 |
|
| 170 |
// If the current page ID matches the specified archive page ID, replace content with shortcode |
| 171 |
if($selected_page_id == $search_result_page_id){ |
| 172 |
$rendering = true; |
| 173 |
$output = do_shortcode('[eshb_accomodation_search_result]'); |
| 174 |
$rendering = false; |
| 175 |
return $output; |
| 176 |
} |
| 177 |
|
| 178 |
// If IDs don't match, return the original content |
| 179 |
return $content; |
| 180 |
} |
| 181 |
|
| 182 |
public function eshb_register_accomodation_block_template( $templates, $query, $template_type ) { |
| 183 |
|
| 184 |
if ( |
| 185 |
! function_exists( 'wp_is_block_theme' ) || |
| 186 |
! wp_is_block_theme() || |
| 187 |
$template_type !== 'wp_template' || |
| 188 |
empty( $query['slug__in'] ) || |
| 189 |
! is_array( $query['slug__in'] ) |
| 190 |
) { |
| 191 |
return $templates; |
| 192 |
} |
| 193 |
|
| 194 |
$map = [ |
| 195 |
'single-eshb_accomodation' => 'eshb_get_accomodation_single_block_template', |
| 196 |
'archive-eshb_accomodation' => 'eshb_get_accomodation_archive_block_template', |
| 197 |
]; |
| 198 |
|
| 199 |
foreach ( $map as $slug => $callback ) { |
| 200 |
if ( in_array( $slug, $query['slug__in'], true ) && method_exists( $this, $callback ) ) { |
| 201 |
$template = $this->$callback(); |
| 202 |
if ( $template ) { |
| 203 |
$templates[] = $template; |
| 204 |
} |
| 205 |
break; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
return $templates; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
function eshb_get_accomodation_single_block_template() { |
| 214 |
|
| 215 |
$file = ESHB_PL_PATH . 'public/templates/single/block/single-eshb_accomodation.html'; |
| 216 |
|
| 217 |
if ( ! file_exists( $file ) ) { |
| 218 |
return null; |
| 219 |
} |
| 220 |
|
| 221 |
$content = file_get_contents( $file ); |
| 222 |
|
| 223 |
if ( empty( $content ) ) { |
| 224 |
return null; |
| 225 |
} |
| 226 |
|
| 227 |
$template = new WP_Block_Template(); |
| 228 |
$template->id = 'easyhotel-accomodation//single-eshb_accomodation'; |
| 229 |
$template->theme = get_stylesheet(); |
| 230 |
$template->slug = 'single-eshb_accomodation'; |
| 231 |
$template->title = __( 'Accommodation Single', 'easy-hotel' ); |
| 232 |
$template->description = __( 'Single template for Accommodation CPT', 'easy-hotel' ); |
| 233 |
$template->content = $content; |
| 234 |
$template->source = 'plugin'; |
| 235 |
$template->type = 'wp_template'; |
| 236 |
$template->status = 'publish'; |
| 237 |
$template->is_custom = true; |
| 238 |
|
| 239 |
return $template; |
| 240 |
} |
| 241 |
|
| 242 |
function eshb_get_accomodation_archive_block_template() { |
| 243 |
|
| 244 |
$file = ESHB_PL_PATH . 'public/templates/archive/block/archive-eshb_accomodation.html'; |
| 245 |
|
| 246 |
if ( ! file_exists( $file ) ) { |
| 247 |
return null; |
| 248 |
} |
| 249 |
|
| 250 |
$content = file_get_contents( $file ); |
| 251 |
|
| 252 |
if ( empty( $content ) ) { |
| 253 |
return null; |
| 254 |
} |
| 255 |
|
| 256 |
$template = new WP_Block_Template(); |
| 257 |
$template->id = 'easyhotel-accomodation//archive-eshb_accomodation'; |
| 258 |
$template->theme = get_stylesheet(); |
| 259 |
$template->slug = 'archive-eshb_accomodation'; |
| 260 |
$template->title = __( 'Accommodation Archive', 'easy-hotel' ); |
| 261 |
$template->description = __( 'Archive template for Accommodation CPT', 'easy-hotel' ); |
| 262 |
$template->content = $content; |
| 263 |
$template->source = 'plugin'; |
| 264 |
$template->type = 'wp_template'; |
| 265 |
$template->status = 'publish'; |
| 266 |
$template->is_custom = true; |
| 267 |
|
| 268 |
return $template; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
} |
| 273 |
ESHB_Templates::instance(); |