PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 2.3.3
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v2.3.3
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.2 2.2.1 2.2.0 2.1.2 2.1.1 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 All 66 releases
better-payment / includes / Traits / TemplateQuery.php

TemplateQuery.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 2.3.3, at includes/Traits/TemplateQuery.php

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