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

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