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

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