PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.8.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.8.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 / Editors / Elementor / Traits / TemplateQuery.php

TemplateQuery.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.8.2, at includes/Editors/Elementor/Traits/TemplateQuery.php

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