PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.2.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.2.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.2.0, at includes/FrontEnd/TemplateLoader.php

228 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if( is_tax('doc_category') ) {
74 // If 'archive-doc_category.php' exists in the theme, return it
75 $theme_template = locate_template( 'archive-doc_category.php' );
76 if ( $theme_template ) {
77 return $theme_template;
78 }
79 }
80
81 if ( is_tax( 'glossaries' ) ) {
82 // Use a custom taxonomy template for your custom taxonomy
83
84 $_default_template = 'templates/single/layout-1';
85 $layout = 'templates/single/layout-7';
86
87 $eligible_template = $this->views->path( $layout, $_default_template );
88
89 if ( file_exists( $eligible_template ) ) {
90 $template = &$eligible_template;
91 }
92
93 return apply_filters( 'betterdocs_archives_template', $template, $layout, $_default_template, $this->views );
94 }
95
96 if ( get_post_type() !== 'docs' && ! is_tax('doc_category') && ! is_tax( 'doc_tag' ) && ! is_tax( 'knowledge_base' ) ) {
97 return $template;
98 }
99
100 if ( is_embed() ) {
101 return $template;
102 }
103
104 if ( $this->locate_archives() ) {
105 return $this->locate_archives();
106 }
107
108 $_default_template = 'templates/archives/layout-1';
109 $layout = $this->database->get_theme_mod( 'betterdocs_docs_layout_select', 'layout-7' );
110 $_template = 'templates/archives/' . $layout;
111
112 if ( is_tax( 'doc_category' ) ) {
113 $category_layout = $this->database->get_theme_mod( 'betterdocs_archive_layout_select', 'layout-7' );
114 if ( $category_layout == 'layout-7' || $category_layout == 'layout-8' ) {
115 $_template = 'templates/archives/categories/' . $category_layout;
116 } else {
117 $_template = 'templates/taxonomy-doc_category';
118 }
119 $_default_template = $_template;
120 } elseif ( is_tax( 'doc_tag' ) ) {
121 $_template = 'templates/taxonomy-doc_tag';
122 $_default_template = $_template;
123 }
124
125 $eligible_template = $this->views->path( $_template, $_default_template );
126
127 //Render The Header Footer When Thrive Builder Theme Is Activated
128 if ( wp_get_theme() == 'Thrive Theme Builder' ) {
129 $this->render_thrive_header_footer();
130 }
131
132 if ( file_exists( $eligible_template ) ) {
133 $template = &$eligible_template;
134 }
135
136 return apply_filters( 'betterdocs_archives_template', $template, $layout, $_default_template, $this->views );
137 }
138
139 /**
140 * Locates custom archive templates from the theme.
141 * Checks for 'archive-docs.php', 'taxonomy-doc_category.php', 'taxonomy-doc_tag.php', and 'taxonomy-knowledge_base.php'.
142 * Returns the template path if found, otherwise returns false.
143 * @return string|false The template path or false if not found.
144 */
145 public function locate_archives() {
146 $archive_docs = locate_template( 'archive-docs.php' );
147 $doc_category = locate_template( 'taxonomy-doc_category.php' );
148 $doc_tag = locate_template( 'taxonomy-doc_tag.php' );
149 $knowledge_base = locate_template( 'taxonomy-knowledge_base.php' );
150
151 if ( is_post_type_archive( 'docs' ) && $archive_docs ) {
152 return $archive_docs;
153 } elseif ( is_tax( 'doc_category' ) && $doc_category ) {
154 return $doc_category;
155 } elseif ( is_tax( 'doc_tag' ) && $doc_tag ) {
156 return $doc_tag;
157 } elseif ( is_tax( 'knowledge_base' ) && $archive_docs ) {
158 return $knowledge_base;
159 }
160
161 return false;
162 }
163
164 /**
165 * Render Thriver Header Footer When Thrive Theme Is Activated
166 *
167 * @return void
168 */
169 public function render_thrive_header_footer() {
170 add_action( 'get_header', [ $this, 'render_thrive_header' ], 10 );
171 add_action( 'get_footer', [ $this, 'render_thrive_footer' ], 10 );
172 }
173
174 /**
175 * Returns the template for single 'docs' custom post type.
176 * If the current post type is not 'docs', returns the original template.
177 * If 'single-docs.php' exists in the theme, returns it.
178 * Otherwise, returns the default single template based on the selected layout.
179 * @param string $template The original template.
180 * @return string The single template.
181 */
182 public function single_template( $template ) {
183 if ( ! is_singular( 'docs' ) ) {
184 return $template;
185 }
186
187 // If 'single-docs.php' exists in the theme, return it
188 $theme_template = locate_template( 'single-docs.php' );
189 if ( $theme_template ) {
190 return $theme_template;
191 }
192
193 //Render The Header Footer When Thrive Builder Theme Is Activated
194 if ( wp_get_theme() == 'Thrive Theme Builder' ) {
195 $this->render_thrive_header_footer();
196 }
197
198 $_default_template = 'templates/single/layout-1';
199 $layout = $this->database->get_theme_mod( 'betterdocs_single_layout_select', 'layout-8' );
200 $layout = 'templates/single/' . $layout;
201
202 $eligible_template = $this->views->path( $layout, $_default_template );
203
204 if ( file_exists( $eligible_template ) ) {
205 $template = &$eligible_template;
206 }
207
208 return apply_filters( 'betterdocs_single_template', $template, $layout, $_default_template, $this->views );
209 }
210
211 /**
212 * Summary of is_fse_theme
213 * @return bool
214 *
215 * @suppress PHP0417
216 */
217 private function is_fse_theme() {
218 if ( function_exists( 'wp_is_block_theme' ) ) {
219 return (bool) wp_is_block_theme();
220 }
221 if ( function_exists( 'gutenberg_is_fse_theme' ) ) {
222 return (bool) gutenberg_is_fse_theme();
223 }
224
225 return false;
226 }
227 }
228