| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\FrontEnd; |
| 4 |
|
| 5 |
use WPDeveloper\BetterDocs\Utils\Base; |
| 6 |
use WPDeveloper\BetterDocs\Utils\Views; |
| 7 |
use WPDeveloper\BetterDocs\Utils\Database; |
| 8 |
use WPDeveloper\BetterDocs\Dependencies\DI\Container; |
| 9 |
|
| 10 |
class TemplateLoader extends Base { |
| 11 |
public $is_fse_theme = false; |
| 12 |
private $templates_dir; |
| 13 |
private $block_templates_dir; |
| 14 |
private $container; |
| 15 |
private $database; |
| 16 |
private $views; |
| 17 |
|
| 18 |
public function __construct( Container $container ) { |
| 19 |
$this->container = $container; |
| 20 |
$this->database = $this->container->get( Database::class ); |
| 21 |
$this->views = $this->container->get( Views::class ); |
| 22 |
} |
| 23 |
|
| 24 |
public function init() { |
| 25 |
$this->is_fse_theme = $this->is_fse_theme(); |
| 26 |
|
| 27 |
if ( $this->is_fse_theme ) { |
| 28 |
// @todo: for FSE theme |
| 29 |
} |
| 30 |
|
| 31 |
if ( ! $this->is_fse_theme ) { |
| 32 |
add_filter( 'archive_template', [ $this, 'archive_template' ] ); |
| 33 |
add_filter( 'single_template', [ $this, 'single_template' ] ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Render Thrive Header Markup |
| 39 |
* |
| 40 |
* Outputs the header section and starts the main container for the page content. |
| 41 |
* |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function render_thrive_header() { |
| 45 |
// Retrieve and sanitize the header content allowing limited HTML |
| 46 |
$header_content = thrive_template()->render_theme_hf_section( THRIVE_HEADER_SECTION ); |
| 47 |
echo '<div id="wrapper">' . wp_kses_post( $header_content ) . '<div id="content"><div class="main-container">'; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Render Thrive Footer Markup |
| 52 |
* |
| 53 |
* Outputs the footer section and closes the main content container. |
| 54 |
* |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
public function render_thrive_footer() { |
| 58 |
// Retrieve and sanitize the footer content allowing limited HTML |
| 59 |
$footer_content = thrive_template()->render_theme_hf_section( THRIVE_FOOTER_SECTION ); |
| 60 |
echo '</div></div>' . wp_kses_post( $footer_content ) . '</div>'; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
/** |
| 65 |
* Returns the archive template for the 'docs' custom post type. |
| 66 |
* If the post type is not 'docs' or it's an embed request, returns the original template. |
| 67 |
* If custom archive templates are found in the theme, returns the appropriate template. |
| 68 |
* Otherwise, returns the default archive template based on the selected layout. |
| 69 |
* @param string $template The original template. |
| 70 |
* @return string The archive template. |
| 71 |
*/ |
| 72 |
public function archive_template( $template ) { |
| 73 |
|
| 74 |
if ( is_tax( 'glossaries' ) ) { |
| 75 |
// Use a custom taxonomy template for your custom taxonomy |
| 76 |
|
| 77 |
$_default_template = 'templates/single/layout-1'; |
| 78 |
$layout = 'templates/single/layout-7'; |
| 79 |
|
| 80 |
$eligible_template = $this->views->path( $layout, $_default_template ); |
| 81 |
|
| 82 |
if ( file_exists( $eligible_template ) ) { |
| 83 |
$template = &$eligible_template; |
| 84 |
} |
| 85 |
|
| 86 |
return apply_filters( 'betterdocs_archives_template', $template, $layout, $_default_template, $this->views ); |
| 87 |
} |
| 88 |
|
| 89 |
if ( get_post_type() !== 'docs' ) { |
| 90 |
return $template; |
| 91 |
} |
| 92 |
|
| 93 |
if ( is_embed() ) { |
| 94 |
return $template; |
| 95 |
} |
| 96 |
|
| 97 |
if ( $this->locate_archives() ) { |
| 98 |
return $this->locate_archives(); |
| 99 |
} |
| 100 |
|
| 101 |
$_default_template = 'templates/archives/layout-1'; |
| 102 |
$layout = $this->database->get_theme_mod( 'betterdocs_docs_layout_select', 'layout-7' ); |
| 103 |
$_template = 'templates/archives/' . $layout; |
| 104 |
|
| 105 |
if ( is_tax( 'doc_category' ) ) { |
| 106 |
$category_layout = $this->database->get_theme_mod( 'betterdocs_archive_layout_select', 'layout-7' ); |
| 107 |
if ( $category_layout == 'layout-7' ) { |
| 108 |
$_template = 'templates/archives/categories/' . $category_layout; |
| 109 |
} else { |
| 110 |
$_template = 'templates/taxonomy-doc_category'; |
| 111 |
} |
| 112 |
$_default_template = $_template; |
| 113 |
} elseif ( is_tax( 'doc_tag' ) ) { |
| 114 |
$_template = 'templates/taxonomy-doc_tag'; |
| 115 |
$_default_template = $_template; |
| 116 |
} |
| 117 |
|
| 118 |
$eligible_template = $this->views->path( $_template, $_default_template ); |
| 119 |
|
| 120 |
//Render The Header Footer When Thrive Builder Theme Is Activated |
| 121 |
if ( wp_get_theme() == 'Thrive Theme Builder' ) { |
| 122 |
$this->render_thrive_header_footer(); |
| 123 |
} |
| 124 |
|
| 125 |
if ( file_exists( $eligible_template ) ) { |
| 126 |
$template = &$eligible_template; |
| 127 |
} |
| 128 |
|
| 129 |
return apply_filters( 'betterdocs_archives_template', $template, $layout, $_default_template, $this->views ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Locates custom archive templates from the theme. |
| 134 |
* Checks for 'archive-docs.php', 'taxonomy-doc_category.php', 'taxonomy-doc_tag.php', and 'taxonomy-knowledge_base.php'. |
| 135 |
* Returns the template path if found, otherwise returns false. |
| 136 |
* @return string|false The template path or false if not found. |
| 137 |
*/ |
| 138 |
public function locate_archives() { |
| 139 |
$archive_docs = locate_template( 'archive-docs.php' ); |
| 140 |
$doc_category = locate_template( 'taxonomy-doc_category.php' ); |
| 141 |
$doc_tag = locate_template( 'taxonomy-doc_tag.php' ); |
| 142 |
$knowledge_base = locate_template( 'taxonomy-knowledge_base.php' ); |
| 143 |
|
| 144 |
if ( is_post_type_archive( 'docs' ) && $archive_docs ) { |
| 145 |
return $archive_docs; |
| 146 |
} elseif ( is_tax( 'doc_category' ) && $doc_category ) { |
| 147 |
return $doc_category; |
| 148 |
} elseif ( is_tax( 'doc_tag' ) && $doc_tag ) { |
| 149 |
return $doc_tag; |
| 150 |
} elseif ( is_tax( 'knowledge_base' ) && $archive_docs ) { |
| 151 |
return $knowledge_base; |
| 152 |
} |
| 153 |
|
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Render Thriver Header Footer When Thrive Theme Is Activated |
| 159 |
* |
| 160 |
* @return void |
| 161 |
*/ |
| 162 |
public function render_thrive_header_footer() { |
| 163 |
add_action( 'get_header', [ $this, 'render_thrive_header' ], 10 ); |
| 164 |
add_action( 'get_footer', [ $this, 'render_thrive_footer' ], 10 ); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Returns the template for single 'docs' custom post type. |
| 169 |
* If the current post type is not 'docs', returns the original template. |
| 170 |
* If 'single-docs.php' exists in the theme, returns it. |
| 171 |
* Otherwise, returns the default single template based on the selected layout. |
| 172 |
* @param string $template The original template. |
| 173 |
* @return string The single template. |
| 174 |
*/ |
| 175 |
public function single_template( $template ) { |
| 176 |
if ( ! is_singular( 'docs' ) ) { |
| 177 |
return $template; |
| 178 |
} |
| 179 |
|
| 180 |
// If 'single-docs.php' exists in the theme, return it |
| 181 |
$theme_template = locate_template( 'single-docs.php' ); |
| 182 |
if ( $theme_template ) { |
| 183 |
return $theme_template; |
| 184 |
} |
| 185 |
|
| 186 |
//Render The Header Footer When Thrive Builder Theme Is Activated |
| 187 |
if ( wp_get_theme() == 'Thrive Theme Builder' ) { |
| 188 |
$this->render_thrive_header_footer(); |
| 189 |
} |
| 190 |
|
| 191 |
$_default_template = 'templates/single/layout-1'; |
| 192 |
$layout = $this->database->get_theme_mod( 'betterdocs_single_layout_select', 'layout-8' ); |
| 193 |
$layout = 'templates/single/' . $layout; |
| 194 |
|
| 195 |
$eligible_template = $this->views->path( $layout, $_default_template ); |
| 196 |
|
| 197 |
if ( file_exists( $eligible_template ) ) { |
| 198 |
$template = &$eligible_template; |
| 199 |
} |
| 200 |
|
| 201 |
return apply_filters( 'betterdocs_single_template', $template, $layout, $_default_template, $this->views ); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Summary of is_fse_theme |
| 206 |
* @return bool |
| 207 |
* |
| 208 |
* @suppress PHP0417 |
| 209 |
*/ |
| 210 |
private function is_fse_theme() { |
| 211 |
if ( function_exists( 'wp_is_block_theme' ) ) { |
| 212 |
return (bool) wp_is_block_theme(); |
| 213 |
} |
| 214 |
if ( function_exists( 'gutenberg_is_fse_theme' ) ) { |
| 215 |
return (bool) gutenberg_is_fse_theme(); |
| 216 |
} |
| 217 |
|
| 218 |
return false; |
| 219 |
} |
| 220 |
} |
| 221 |
|