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

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

254 lines 7.0 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 ($this->theme_templates_dir()) {
115 $templates['theme'] = scandir($this->theme_templates_dir(), 1);
116 }
117
118 return $templates;
119 }
120
121 /**
122 * Retrieves template list from template directory.
123 *
124 * @return array template list.
125 */
126 protected function get_template_list() {
127 $files = [];
128
129 if ($this->get_template_files()) {
130 foreach ($this->get_template_files() as $key => $handler) {
131 foreach ($handler as $handle) {
132 if (strpos($handle, '.php') !== false) {
133
134 if ($key === 'lite') {
135 $path = sprintf('%s/%s', $this->get_template_dir(), $handle);
136 } else if ($key === 'pro') {
137 $path = sprintf('%s/%s', $this->get_pro_template_dir(), $handle);
138 } else if ($key === 'theme') {
139 $path = sprintf('%s/%s', $this->theme_templates_dir(), $handle);
140 }
141
142 $template_info = get_file_data( $path, $this->template_headers );
143 $template_name = $template_info[ 'Template Name' ];
144
145 if ( $template_name ) {
146 $files[ $template_name ] = $path;
147 }
148 }
149 }
150 }
151 }
152
153 return $files;
154 }
155
156 /**
157 * Retrieves template list from template directory.
158 *
159 * @return array template list.
160 */
161 public function get_template_list_for_dropdown()
162 {
163 $files = [];
164 if ($this->get_template_files()) {
165 foreach ($this->get_template_files() as $key => $handler) {
166 foreach ($handler as $handle) {
167 if (strpos($handle, '.php') !== false) {
168
169 $path = $this->_get_path($key, $handle);
170 $template_info = get_file_data($path, $this->template_headers);
171 $template_name = $template_info['Template Name'];
172 $template_key = str_replace( ' ', '-', strtolower( $template_name ) );
173 if ( $template_name ) {
174 $files[$template_key] = sprintf("%s (%s)", ucfirst($template_name), ucfirst($key));
175 }
176 }
177 }
178 }
179 }
180 return $files;
181 }
182
183 public function _get_path($key, $handle)
184 {
185 $path = '';
186 if ($key === 'lite') {
187 $path = sprintf('%s/%s', $this->get_template_dir(), $handle);
188 } else if ($key === 'pro') {
189 $path = sprintf('%s/%s', $this->get_pro_template_dir(), $handle);
190 } else if ($key === 'theme') {
191 $path = sprintf('%s/%s', $this->theme_templates_dir(), $handle);
192 }
193 return $path;
194 }
195
196 /**
197 * Preparing template options for frontend select
198 *
199 * @return array teplate select options.
200 */
201 private function get_template_options() {
202 $files = [];
203
204 if ( $this->get_template_list() ) {
205 foreach ( $this->get_template_list() as $filename => $path ) {
206 $filename = \str_replace( ' ', '-', $filename );
207
208 $files[ strtolower( $filename ) ] = $path;
209 }
210 }
211
212 return $files;
213 }
214
215 /**
216 * Adding key value pairs in template options.
217 *
218 * @return array
219 */
220 private function template_options() {
221 $keys = array_keys( $this->get_template_options() );
222 $values = array_keys( $this->get_template_list() );
223
224 return array_combine( $keys, $values );
225 }
226
227 /**
228 * Retrieve template
229 *
230 * @param string $filename
231 *
232 * @return string include-able full template path.
233 */
234 public function get_template( $filename ) {
235 if ( in_array( $filename, array_keys( $this->get_template_options() ) ) ) {
236 return $this->get_template_options()[ $filename ];
237 }
238
239 return false;
240 }
241
242 /**
243 * Set default option in frontend select control.
244 *
245 * @return string first option.
246 */
247 public function get_default() {
248 $dt = array_reverse( $this->template_options() );
249
250 return strtolower( array_pop( $dt ) );
251 }
252
253 }
254