PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 1.7.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v1.7.2
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 / elementor / template-query.php

template-query.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 1.7.2, at includes/elementor/template-query.php

258 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 Better_Docs_Elementor\Traits;
4
5 if ( !defined( 'ABSPATH' ) ) {
6 exit;
7 } // Exit if accessed directly
8
9 trait Template_Query
10 {
11 public $current_widget_name = '';
12
13 public function set_widget_name( $name = '' ) {
14 $this->current_widget_name = $name;
15 }
16
17 /**
18 * Retrieves Template name from file header.
19 *
20 * @array
21 */
22 private $template_headers = [
23 'Template Name' => 'Template Name',
24 ];
25
26 /**
27 * Prepare the directory name from the following widget name.
28 *
29 * @access private
30 *
31 *
32 * @return string $widget_name
33 */
34 private function process_directory_name()
35 {
36 if ( empty( $this->current_widget_name ) ) {
37 $this->current_widget_name = $this->get_name();
38 }
39 $widget_name = str_replace('betterdocs-', '', $this->current_widget_name);
40 $widget_name = str_replace('-', ' ', $widget_name);
41 $widget_name = ucwords($widget_name);
42 $widget_name = str_replace(' ', '-', $widget_name);
43
44 return $widget_name;
45 }
46
47 /**
48 * Retrieve `Theme Template Directory`
49 *
50 * @return string templates directory from the active theme.
51 */
52 private function theme_templates_dir() {
53 $current_theme = wp_get_theme();
54
55 $dir = sprintf(
56 '%s/%s/Template/%s',
57 $current_theme->theme_root,
58 $current_theme->stylesheet,
59 $this->process_directory_name()
60 );
61
62 if ( is_dir( $dir ) ) {
63 $file = scandir( $dir );
64 $file = array_pop( $file );
65
66 return pathinfo( $file, PATHINFO_EXTENSION ) === 'php' ? $dir : false;
67 }
68
69 return false;
70 }
71
72 /**
73 * Retrieves the lite plugin template directory path.
74 *
75 * @return string templates directory path of lite version.
76 */
77 private function get_template_dir() {
78 return \sprintf(
79 '%sincludes/elementor/Template/%s',
80 BETTERDOCS_DIR_PATH, $this->process_directory_name()
81 );
82 }
83
84 /**
85 * Retrieves the pro plugin template directory path.
86 *
87 * @return string templates directory path of pro version.
88 */
89 private function get_pro_template_dir()
90 {
91 if (!apply_filters('is_plugin_active', 'betterdocs-pro/betterdocs-pro.php')) {
92 return false;
93 }
94
95 return \sprintf(
96 '%sincludes/elementor/Template/%s',
97 BETTERDOCS_PRO_ROOT_DIR_PATH, $this->process_directory_name()
98 );
99 }
100
101 /**
102 * Collecting templates from different sources.
103 *
104 * @return array
105 */
106 private function get_template_files()
107 {
108 $templates = [];
109
110 if (is_dir($this->get_template_dir())) {
111 $templates['lite'] = scandir($this->get_template_dir(), 1);
112 }
113
114 if( is_dir( $this->get_pro_template_dir() ) ) {
115 $templates['pro'] = scandir($this->get_pro_template_dir(), 1);
116 }
117
118 if ($this->theme_templates_dir()) {
119 $templates['theme'] = scandir($this->theme_templates_dir(), 1);
120 }
121
122 return $templates;
123 }
124
125 /**
126 * Retrieves template list from template directory.
127 *
128 * @return array template list.
129 */
130 protected function get_template_list() {
131 $files = [];
132
133 if ($this->get_template_files()) {
134 foreach ($this->get_template_files() as $key => $handler) {
135 foreach ($handler as $handle) {
136 if (strpos($handle, '.php') !== false) {
137
138 if ($key === 'lite') {
139 $path = sprintf('%s/%s', $this->get_template_dir(), $handle);
140 } else if ($key === 'pro') {
141 $path = sprintf('%s/%s', $this->get_pro_template_dir(), $handle);
142 } else if ($key === 'theme') {
143 $path = sprintf('%s/%s', $this->theme_templates_dir(), $handle);
144 }
145
146 $template_info = get_file_data( $path, $this->template_headers );
147 $template_name = $template_info[ 'Template Name' ];
148
149 if ( $template_name ) {
150 $files[ $template_name ] = $path;
151 }
152 }
153 }
154 }
155 }
156
157 return $files;
158 }
159
160 /**
161 * Retrieves template list from template directory.
162 *
163 * @return array template list.
164 */
165 public function get_template_list_for_dropdown()
166 {
167 $files = [];
168 if ($this->get_template_files()) {
169 foreach ($this->get_template_files() as $key => $handler) {
170 foreach ($handler as $handle) {
171 if (strpos($handle, '.php') !== false) {
172
173 $path = $this->_get_path($key, $handle);
174 $template_info = get_file_data($path, $this->template_headers);
175 $template_name = $template_info['Template Name'];
176 $template_key = str_replace( ' ', '-', strtolower( $template_name ) );
177 if ( $template_name ) {
178 $files[$template_key] = sprintf("%s (%s)", ucfirst($template_name), ucfirst($key));
179 }
180 }
181 }
182 }
183 }
184 return $files;
185 }
186
187 public function _get_path($key, $handle)
188 {
189 $path = '';
190 if ($key === 'lite') {
191 $path = sprintf('%s/%s', $this->get_template_dir(), $handle);
192 } else if ($key === 'pro') {
193 $path = sprintf('%s/%s', $this->get_pro_template_dir(), $handle);
194 } else if ($key === 'theme') {
195 $path = sprintf('%s/%s', $this->theme_templates_dir(), $handle);
196 }
197 return $path;
198 }
199
200 /**
201 * Preparing template options for frontend select
202 *
203 * @return array teplate select options.
204 */
205 private function get_template_options() {
206 $files = [];
207
208 if ( $this->get_template_list() ) {
209 foreach ( $this->get_template_list() as $filename => $path ) {
210 $filename = \str_replace( ' ', '-', $filename );
211
212 $files[ strtolower( $filename ) ] = $path;
213 }
214 }
215
216 return $files;
217 }
218
219 /**
220 * Adding key value pairs in template options.
221 *
222 * @return array
223 */
224 private function template_options() {
225 $keys = array_keys( $this->get_template_options() );
226 $values = array_keys( $this->get_template_list() );
227
228 return array_combine( $keys, $values );
229 }
230
231 /**
232 * Retrieve template
233 *
234 * @param string $filename
235 *
236 * @return string include-able full template path.
237 */
238 public function get_template( $filename ) {
239 if ( in_array( $filename, array_keys( $this->get_template_options() ) ) ) {
240 return $this->get_template_options()[ $filename ];
241 }
242
243 return false;
244 }
245
246 /**
247 * Set default option in frontend select control.
248 *
249 * @return string first option.
250 */
251 public function get_default() {
252 $dt = array_reverse( $this->template_options() );
253
254 return strtolower( array_pop( $dt ) );
255 }
256
257 }
258