| 1 |
<?php |
| 2 |
|
| 3 |
namespace Booktics\Service\Templates; |
| 4 |
|
| 5 |
use Booktics\Contracts\Hookable_Service_Contract; |
| 6 |
|
| 7 |
/** |
| 8 |
* Single_Service_Template class handles custom template loading for single service pages |
| 9 |
* |
| 10 |
* @package Booktics\Service\Templates |
| 11 |
*/ |
| 12 |
class Single_Service_Template implements Hookable_Service_Contract { |
| 13 |
|
| 14 |
/** |
| 15 |
* Register template filter hook |
| 16 |
* |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public function register() { |
| 20 |
add_filter( |
| 21 |
'template_include', array( |
| 22 |
$this, |
| 23 |
'custom_single_service_template', |
| 24 |
) |
| 25 |
); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Loads custom template for single service pages |
| 30 |
* |
| 31 |
* @param string $template Current template path |
| 32 |
* |
| 33 |
* @return string Modified template path |
| 34 |
*/ |
| 35 |
public function custom_single_service_template( $template ) { |
| 36 |
if ( is_single() ) { |
| 37 |
global $post; // Add this line to access the global $post variable |
| 38 |
|
| 39 |
// Check if the post type is 'booktics-service' |
| 40 |
if ( 'booktics-service' !== $post->post_type ) { |
| 41 |
return $template; |
| 42 |
} |
| 43 |
if ( $post->post_status === 'deactive' ) { |
| 44 |
wp_head(); |
| 45 |
?> |
| 46 |
<div |
| 47 |
class="booktics-shortcode-wrapper booktics-shortcode-notice-wrapper"> |
| 48 |
<div> |
| 49 |
<h3><?php echo esc_html__( 'Service Unavailable', 'booktics' ); ?></h3> |
| 50 |
</div> |
| 51 |
</div> |
| 52 |
<?php |
| 53 |
wp_footer(); |
| 54 |
exit; |
| 55 |
} |
| 56 |
// Load a custom template file |
| 57 |
$custom_template = BOOKTICS_DIR . '/templates/service/custom-single-service-template.php'; |
| 58 |
|
| 59 |
if ( file_exists( $custom_template ) ) { |
| 60 |
return $custom_template; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// Return the original template if no custom template is found |
| 65 |
return $template; |
| 66 |
} |
| 67 |
} |
| 68 |
|