PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.8.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.8.0
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / FrontEnd / TemplateLoader.php

TemplateLoader.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.8.0, at includes/FrontEnd/TemplateLoader.php

264 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WPDeveloper\BetterDocs\FrontEnd;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8
9 use WPDeveloper\BetterDocs\Utils\Base;
10 use WPDeveloper\BetterDocs\Utils\Views;
11 use WPDeveloper\BetterDocs\Utils\Database;
12 use WPDeveloper\BetterDocs\Dependencies\DI\Container;
13
14 class TemplateLoader extends Base {
15 public $is_fse_theme = false;
16 private $templates_dir;
17 private $block_templates_dir;
18 private $container;
19 private $database;
20 private $views;
21
22 public function __construct( Container $container ) {
23 $this->container = $container;
24 $this->database = $this->container->get( Database::class );
25 $this->views = $this->container->get( Views::class );
26 }
27
28 public function init() {
29 $this->is_fse_theme = $this->is_fse_theme();
30
31 if ( $this->is_fse_theme ) {
32 // Block themes have no PHP template-hierarchy hook for the glossaries
33 // (encyclopedia) taxonomy, so single glossary entries fall through to the
34 // theme's default block template and render the generic docs/archive
35 // output instead of the BetterDocs glossary template (fbs-79870). Force
36 // the glossary template via template_include, which runs after
37 // locate_block_template() so it wins over the block-template fallback.
38 add_filter( 'template_include', [ $this, 'fse_glossaries_template' ], 99 );
39 }
40
41 if ( ! $this->is_fse_theme ) {
42 add_filter( 'archive_template', [ $this, 'archive_template' ] );
43 add_filter( 'single_template', [ $this, 'single_template' ] );
44 }
45 }
46
47 /**
48 * Load the BetterDocs glossary template for single glossary (encyclopedia)
49 * taxonomy pages under block (FSE) themes. Mirrors the glossaries branch of
50 * archive_template() used for classic themes. Runs late on template_include
51 * so it overrides the block-template fallback that would otherwise render the
52 * generic docs archive. (fbs-79870)
53 *
54 * @param string $template The template path resolved so far.
55 * @return string
56 */
57 public function fse_glossaries_template( $template ) {
58 if ( ! is_tax( 'glossaries' ) ) {
59 return $template;
60 }
61
62 $_default_template = 'templates/single/layout-1';
63 $layout = 'templates/single/layout-7';
64 $eligible_template = $this->views->path( $layout, $_default_template );
65
66 if ( file_exists( $eligible_template ) ) {
67 $template = $eligible_template;
68 }
69
70 return apply_filters( 'betterdocs_archives_template', $template, $layout, $_default_template, $this->views );
71 }
72
73 /**
74 * Render Thrive Header Markup
75 *
76 * Outputs the header section and starts the main container for the page content.
77 *
78 * @return void
79 */
80 public function render_thrive_header() {
81 // Retrieve and sanitize the header content allowing limited HTML
82 $header_content = thrive_template()->render_theme_hf_section( THRIVE_HEADER_SECTION );
83 echo '<div id="wrapper">' . wp_kses_post( $header_content ) . '<div id="content"><div class="main-container">';
84 }
85
86 /**
87 * Render Thrive Footer Markup
88 *
89 * Outputs the footer section and closes the main content container.
90 *
91 * @return void
92 */
93 public function render_thrive_footer() {
94 // Retrieve and sanitize the footer content allowing limited HTML
95 $footer_content = thrive_template()->render_theme_hf_section( THRIVE_FOOTER_SECTION );
96 echo '</div></div>' . wp_kses_post( $footer_content ) . '</div>';
97 }
98
99
100 /**
101 * Returns the archive template for the 'docs' custom post type.
102 * If the post type is not 'docs' or it's an embed request, returns the original template.
103 * If custom archive templates are found in the theme, returns the appropriate template.
104 * Otherwise, returns the default archive template based on the selected layout.
105 * @param string $template The original template.
106 * @return string The archive template.
107 */
108 public function archive_template( $template ) {
109 if( is_tax('doc_category') ) {
110 // If 'archive-doc_category.php' exists in the theme, return it
111 $theme_template = locate_template( 'archive-doc_category.php' );
112 if ( $theme_template ) {
113 return $theme_template;
114 }
115 }
116
117 if ( is_tax( 'glossaries' ) ) {
118 // Use a custom taxonomy template for your custom taxonomy
119
120 $_default_template = 'templates/single/layout-1';
121 $layout = 'templates/single/layout-7';
122
123 $eligible_template = $this->views->path( $layout, $_default_template );
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 if ( get_post_type() !== 'docs' && ! is_tax('doc_category') && ! is_tax( 'doc_tag' ) && ! is_tax( 'knowledge_base' ) ) {
133 return $template;
134 }
135
136 if ( is_embed() ) {
137 return $template;
138 }
139
140 if ( $this->locate_archives() ) {
141 return $this->locate_archives();
142 }
143
144 $_default_template = 'templates/archives/layout-1';
145 $layout = $this->database->get_theme_mod( 'betterdocs_docs_layout_select', 'layout-7' );
146 $_template = 'templates/archives/' . $layout;
147
148 if ( is_tax( 'doc_category' ) ) {
149 $category_layout = $this->database->get_theme_mod( 'betterdocs_archive_layout_select', 'layout-7' );
150 if ( $category_layout == 'layout-7' || $category_layout == 'layout-8' ) {
151 $_template = 'templates/archives/categories/' . $category_layout;
152 } else {
153 $_template = 'templates/taxonomy-doc_category';
154 }
155 $_default_template = $_template;
156 } elseif ( is_tax( 'doc_tag' ) ) {
157 $_template = 'templates/taxonomy-doc_tag';
158 $_default_template = $_template;
159 }
160
161 $eligible_template = $this->views->path( $_template, $_default_template );
162
163 //Render The Header Footer When Thrive Builder Theme Is Activated
164 if ( wp_get_theme() == 'Thrive Theme Builder' ) {
165 $this->render_thrive_header_footer();
166 }
167
168 if ( file_exists( $eligible_template ) ) {
169 $template = &$eligible_template;
170 }
171
172 return apply_filters( 'betterdocs_archives_template', $template, $layout, $_default_template, $this->views );
173 }
174
175 /**
176 * Locates custom archive templates from the theme.
177 * Checks for 'archive-docs.php', 'taxonomy-doc_category.php', 'taxonomy-doc_tag.php', and 'taxonomy-knowledge_base.php'.
178 * Returns the template path if found, otherwise returns false.
179 * @return string|false The template path or false if not found.
180 */
181 public function locate_archives() {
182 $archive_docs = locate_template( 'archive-docs.php' );
183 $doc_category = locate_template( 'taxonomy-doc_category.php' );
184 $doc_tag = locate_template( 'taxonomy-doc_tag.php' );
185 $knowledge_base = locate_template( 'taxonomy-knowledge_base.php' );
186
187 if ( is_post_type_archive( 'docs' ) && $archive_docs ) {
188 return $archive_docs;
189 } elseif ( is_tax( 'doc_category' ) && $doc_category ) {
190 return $doc_category;
191 } elseif ( is_tax( 'doc_tag' ) && $doc_tag ) {
192 return $doc_tag;
193 } elseif ( is_tax( 'knowledge_base' ) && $archive_docs ) {
194 return $knowledge_base;
195 }
196
197 return false;
198 }
199
200 /**
201 * Render Thriver Header Footer When Thrive Theme Is Activated
202 *
203 * @return void
204 */
205 public function render_thrive_header_footer() {
206 add_action( 'get_header', [ $this, 'render_thrive_header' ], 10 );
207 add_action( 'get_footer', [ $this, 'render_thrive_footer' ], 10 );
208 }
209
210 /**
211 * Returns the template for single 'docs' custom post type.
212 * If the current post type is not 'docs', returns the original template.
213 * If 'single-docs.php' exists in the theme, returns it.
214 * Otherwise, returns the default single template based on the selected layout.
215 * @param string $template The original template.
216 * @return string The single template.
217 */
218 public function single_template( $template ) {
219 if ( ! is_singular( 'docs' ) ) {
220 return $template;
221 }
222
223 // If 'single-docs.php' exists in the theme, return it
224 $theme_template = locate_template( 'single-docs.php' );
225 if ( $theme_template ) {
226 return $theme_template;
227 }
228
229 //Render The Header Footer When Thrive Builder Theme Is Activated
230 if ( wp_get_theme() == 'Thrive Theme Builder' ) {
231 $this->render_thrive_header_footer();
232 }
233
234 $_default_template = 'templates/single/layout-1';
235 $layout = $this->database->get_theme_mod( 'betterdocs_single_layout_select', 'layout-8' );
236 $layout = 'templates/single/' . $layout;
237
238 $eligible_template = $this->views->path( $layout, $_default_template );
239
240 if ( file_exists( $eligible_template ) ) {
241 $template = &$eligible_template;
242 }
243
244 return apply_filters( 'betterdocs_single_template', $template, $layout, $_default_template, $this->views );
245 }
246
247 /**
248 * Summary of is_fse_theme
249 * @return bool
250 *
251 * @suppress PHP0417
252 */
253 private function is_fse_theme() {
254 if ( function_exists( 'wp_is_block_theme' ) ) {
255 return (bool) wp_is_block_theme();
256 }
257 if ( function_exists( 'gutenberg_is_fse_theme' ) ) {
258 return (bool) gutenberg_is_fse_theme();
259 }
260
261 return false;
262 }
263 }
264