PluginProbe
Filter Everything — WordPress & WooCommerce Filters / trunk
Filter Everything — WordPress & WooCommerce Filters vtrunk
1.9.6 1.9.5 1.9.4 1.9.3 1.9.2.2 1.9.2.1 trunk 1.2.1 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.4.1 1.4.4 1.4.5 1.4.8 1.4.9 1.5.0 1.5.1 1.6.0 1.6.1 1.6.2 1.6.3 All 51 releases
filter-everything / src / Settings / BaseSettings.php

BaseSettings.php in Filter Everything — WordPress & WooCommerce Filters trunk, at src/Settings/BaseSettings.php

474 lines 16.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FilterEverything\Filter;
4
5 if ( ! defined('ABSPATH') ) {
6 exit;
7 }
8
9 abstract class BaseSettings implements TabInterface{
10
11 protected $page;
12
13 protected $group;
14
15 protected $optionName;
16
17 protected $options;
18
19 protected function registerSettings($settings, $page, $optionName)
20 {
21 $callbacks = array(
22 'checkbox' => array($this, 'checkboxCallback'),
23 'text' => array($this, 'inputCallback'),
24 'license' => array($this, 'licenseCallback'),
25 'textarea' => array($this, 'textareaCallback'),
26 'editor' => array($this, 'textEditorCallback'),
27 'select' => array($this, 'selectCallback'),
28 'hidden' => array($this, 'hiddenCallback'),
29 'file' => array($this, 'fileCallback'),
30 'number' => array($this, 'numberCallback'),
31 'inProButton' => array($this, 'inProButtonCallback'),
32 'html' => array($this, 'htmlCallback'),
33 );
34
35 foreach ($settings as $sectionId => $section) {
36 $callback = isset($section['callback']) ? $section['callback'] : null;
37 $args = isset($section['args']) ? $section['args'] : array();
38 add_settings_section($sectionId, $section['label'], $callback, $page, $args );
39
40 if( isset( $section['fields'] ) ){
41 foreach ( $section['fields'] as $fieldId => $field) {
42 $title = isset($field['title']) ? $field['title'] : '';
43 $field['label_for'] = $fieldId;
44 $field['option_name'] = $optionName;
45 $field['placeholder'] = isset($field['placeholder']) ? $field['placeholder'] : '';
46 $field['id'] = isset($field['id']) ? $field['id'] : $fieldId;
47 add_settings_field($fieldId, $title, $callbacks[$field['type']], $page, $sectionId, $field);
48 }
49 }
50 }
51 }
52
53 public function checkBoxCallback($args)
54 {
55
56 $args = wp_parse_args( $args, array(
57 'checked' => false,
58 'disabled' => false,
59 'label' => '',
60 ) );
61
62 $checkbox = '<label><input type="checkbox" name="%s[%s]" %s id="%s" %s>%s</label>';
63 $checkbox = apply_filters( 'wpc_settings_field_checkbox', $checkbox, $args );
64 $checked = $this->getOption($args['label_for']);
65
66 if ( $checked == '1' || $args['checked'] === true ) {
67 $checked = 'on';
68 }
69
70 $disabled = ( $args['disabled'] === true ) ? 'disabled' : '';
71
72 printf(
73 $checkbox,
74 esc_attr($args['option_name']),
75 esc_attr($args['label_for']),
76 checked('on', $checked, false),
77 esc_attr($args['id']),
78 esc_attr($disabled),
79 esc_html($args['label'])
80 );
81
82 if( isset( $args['description'] ) ){
83 printf( '<p class="description">%s</p>', esc_html( $args['description'] ) );
84 }
85 }
86
87
88 public function inputCallback($args)
89 {
90 $input = '<input class="%s" type="text" name="%s[%s]" value="%s" placeholder="%s" id="%s">';
91 $class = isset( $args['class'] ) ? $args['class'] : 'regular-text';
92 if( isset( $args['default'] ) ){
93 $value = $this->getOption($args['label_for']) ? $this->getOption($args['label_for']) : $args['default'];
94 }else{
95 $value = $this->getOption($args['label_for']);
96 }
97
98 if (!is_null($value)) {
99 $value = trim($value);
100 }
101
102 printf(
103 $input,
104 esc_attr($class),
105 esc_attr($this->optionName),
106 esc_attr($args['label_for']),
107 esc_attr($value),
108 esc_attr($args['placeholder']),
109 esc_attr($args['id'])
110 );
111
112 if(!empty($args['additional_link'])){
113 echo '<a id="wpc-choose-selector-button" href="' . esc_url($args['additional_link']['url']) . '" class="button">' . esc_html($args['additional_link']['label']) . '</a>';
114 }
115
116 if( isset($args['id']) && $args['id'] === 'posts_container' ){
117 if( flrt_get_option('enable_ajax') === 'on' && ! $value ){
118 printf( '<p class="wpc-warning">%s</p>', esc_html__( 'You must specify the Results container, otherwise AJAX will not work properly', 'filter-everything' ) );
119 }
120
121 }
122
123 if( isset( $args['description'] ) ){
124 printf( '<p class="description">%s</p>', esc_html( $args['description'] ) );
125 }
126
127 }
128
129 public function numberCallback($args)
130 {
131 $input = '<input class="%s" type="number" name="%s[%s]" value="%s" placeholder="%s" id="%s">';
132 $class = isset( $args['class'] ) ? $args['class'] : 'regular-text';
133 if( isset( $args['default'] ) ){
134 $value = $this->getOption($args['label_for']) ? $this->getOption($args['label_for']) : $args['default'];
135 }else{
136 $value = $this->getOption($args['label_for']);
137 }
138
139 if (!is_null($value)) {
140 $value = trim($value);
141 }
142
143 printf(
144 $input,
145 esc_attr($class),
146 esc_attr($this->optionName),
147 esc_attr($args['label_for']),
148 esc_attr($value),
149 esc_attr($args['placeholder']),
150 esc_attr($args['id'])
151 );
152
153 if( isset($args['id']) && $args['id'] === 'posts_container' ){
154
155 if( flrt_get_option('enable_ajax') === 'on' && ! $value ){
156 printf( '<p class="wpc-warning">%s</p>', esc_html__( 'You must specify the Results container, otherwise AJAX will not work properly', 'filter-everything' ) );
157 }
158 }
159
160 if( isset( $args['description'] ) ){
161 printf( '<p class="description">%s</p>', esc_html( $args['description'] ) );
162 }
163
164 }
165
166 public function licenseCallback( $args )
167 {
168 $input = '<input class="%s" type="text" readonly="readonly" name="%s[%s]" value="%s" placeholder="%s" id="%s">';
169 $class = isset( $args['class'] ) ? $args['class'] : 'regular-text';
170
171 $value = str_repeat( '*', 80 );
172 $value = trim($value);
173
174 printf(
175 $input,
176 esc_attr($class),
177 esc_attr($this->optionName),
178 esc_attr($args['label_for']),
179 esc_attr($value),
180 esc_attr($args['placeholder']),
181 esc_attr($args['id'])
182 );
183
184 if( isset( $args['description'] ) ){
185 printf( '<p class="description">%s</p>', esc_html( $args['description'] ) );
186 }
187
188 }
189
190 public function hiddenCallback( $args )
191 {
192 $input = '<input class="%s" type="hidden" name="%s[%s]" value="%s" placeholder="%s" id="%s">';
193 $class = isset( $args['class'] ) ? $args['class'] : 'regular-text';
194 printf(
195 $input,
196 esc_attr($class),
197 esc_attr($this->optionName),
198 esc_attr($args['label_for']),
199 esc_attr($this->getOption($args['label_for'])),
200 esc_attr($args['placeholder']),
201 esc_attr($args['id'])
202 );
203 }
204
205 public function fileCallback( $args )
206 {
207 $input = '<label for="%s" class="flrt-file-upload"><input class="%s" type="file" name="%s[%s]" value="%s" placeholder="%s" id="%s" %s></label>';
208 $class = isset( $args['class'] ) ? $args['class'] : 'regular-text';
209 printf(
210 $input,
211 esc_attr($args['id']),
212 esc_attr($class),
213 esc_attr($this->optionName),
214 esc_attr($args['label_for']),
215 esc_attr($this->getOption($args['label_for'])),
216 esc_attr($args['placeholder']),
217 esc_attr($args['id']),
218 esc_attr($args['required'])
219 );
220 }
221
222 public function selectCallback( $args )
223 {
224 $options = isset($args['options']) ? $args['options'] : [];
225 $disabled = isset($args['disabled']) ? $args['disabled'] : [];
226 $class = isset( $args['class'] ) ? $args['class'] : 'filter-settings-select';
227 $value = $this->getOption($args['label_for']);
228
229 $multiple = ! empty($args['multiple']) ? 'multiple' : null;
230
231 $select = '<select name="%s[%s]%s" class="%s" placeholder="%s" id="%s" %s>';
232
233 printf(
234 $select,
235 esc_attr($this->optionName),
236 esc_attr($args['label_for']),
237 $multiple ? '[]' : '',
238 $class,
239 esc_attr($args['placeholder']),
240 esc_attr($args['id']),
241 $multiple
242 );
243
244 foreach ($options as $key => $option) {
245 if (! empty($value)) {
246 if (is_null($multiple)) {
247 $selected = $key === $value ? 'selected' : '';
248 } else {
249 $selected = in_array($key, $value) ? 'selected' : '';
250 }
251 } else {
252 $selected = '';
253 }
254 $disabled_option = '';
255 if( in_array($key, $disabled) ){
256 $disabled_option = 'disabled';
257 }
258
259 printf('<option value="%s" %s%s>%s</option>', esc_attr($key), $selected, $disabled_option, esc_html($option));
260 }
261
262 print('</select>');
263
264 if (isset($args['help'])) {
265 printf('<p>%s</p>', esc_html($args['help']));
266 }
267
268 if( isset( $args['description'] ) ){
269 printf( '<p class="description">%s</p>', esc_html( $args['description'] ) );
270 }
271
272 }
273
274 public function textareaCallback($args)
275 {
276 $textarea = '<textarea class="filter-settings-input large-text code" name="%s[%s]" placeholder="%s" cols="30" rows="10" id="%s">%s</textarea>';
277
278 $textarea_text = $this->getOption($args['label_for']);
279 if(empty($textarea_text)){
280 $textarea_text = '';
281 }
282
283 printf(
284 $textarea,
285 esc_attr($this->optionName),
286 esc_attr($args['label_for']),
287 esc_attr($args['placeholder']),
288 esc_attr($args['id']),
289 esc_textarea( $textarea_text )
290 );
291
292 }
293
294 public function textEditorCallback($args)
295 {
296 wp_editor(
297 $this->getOption( $args['label_for'] ),
298 esc_attr( $args['id'] ),
299 array( 'textarea_name' => esc_attr( $this->optionName . '[' . $args['label_for'] . ']' ) )
300 );
301
302 }
303
304 public function render()
305 {
306 print('<form action="' . admin_url('options.php') . '" method="post">');
307
308 settings_errors();
309
310 settings_fields($this->group);
311
312 do_action('wpc_before_sections_settings_fields', $this->page );
313
314 $this->doSettingsSections($this->page);
315
316 do_action('wpc_after_sections_settings_fields', $this->page );
317
318 if( apply_filters('wpc_settings_submit_button', true ) ){
319 submit_button();
320 }
321
322 print('</form>');
323 }
324
325 public function doSettingsSections( $page ) {
326 global $wp_settings_sections, $wp_settings_fields;
327
328 if ( ! isset( $wp_settings_sections[ $page ] ) ) {
329 return;
330 }
331
332 foreach ( (array) $wp_settings_sections[ $page ] as $section ) {
333
334 do_action('wpc_before_settings_fields_title', $page );
335
336 if ( $section['title'] ) {
337 echo "<h2>". wp_kses( $section['title'], array( 'span' => array( 'class' => true ) )) ."</h2>\n";
338 }
339
340 do_action('wpc_after_settings_fields_title', $page );
341
342 if ( $section['callback'] ) {
343 call_user_func( $section['callback'], $section );
344 }
345
346 if ( ! isset( $wp_settings_fields ) || ! isset( $wp_settings_fields[ $page ] ) || ! isset( $wp_settings_fields[ $page ][ $section['id'] ] ) ) {
347 continue;
348 }
349
350 $sortable = ( $section['id'] === 'wpc_slugs' ) ? ' wpc-sortable-table' : '';
351
352 echo '<table class="wpc-form-table form-table'.esc_attr($sortable).'" role="presentation">';
353 $this->doSettingsFields( $page, $section['id'] );
354 echo '</table>';
355 }
356 do_action('wpc_after_settings_fields', $page );
357 }
358
359 public function doSettingsFields( $page, $section ) {
360 global $wp_settings_fields;
361
362 if ( ! isset( $wp_settings_fields[ $page ][ $section ] ) ) {
363 return;
364 }
365
366 $i = 1;
367
368 foreach ( (array) $wp_settings_fields[ $page ][ $section ] as $field ) {
369 $class = '';
370
371 if( $field['id'] === 'bottom_widget_compatibility' ){
372 if( flrt_get_option('mobile_filter_settings') === 'show_bottom_widget' ){
373 $field['args']['class'] .= ' wpc-opened';
374 }
375 }
376
377 if( $field['id'] === 'color_swatches_taxonomies' || $field['id'] === 'rounded_swatches' ) {
378 if( flrt_get_experimental_option('use_color_swatches') === 'on' ) {
379 $field['args']['class'] .= ' wpc-opened';
380 }
381 }
382
383 if ( ! empty( $field['args']['class'] ) ) {
384 $class = ' class="' . esc_attr( $field['args']['class'] ) . '"';
385 }
386
387 do_action('wpc_before_settings_field', $field );
388
389 echo "<tr{$class}>";
390
391 $sortable = isset( $field['args']['sortable'] ) ? $i : '';
392
393 if( $sortable ){
394 echo '<td class="wpc-order-td">'.esc_html($sortable).'</td>';
395 }
396
397 $tooltip = isset( $field['args']['tooltip'] ) ? flrt_tooltip( array( 'tooltip' => $field['args']['tooltip'] ) ) : '';
398 $tooltip = wp_kses(
399 $tooltip,
400 array(
401 'strong' => array(),
402 'br' => array(),
403 'a' => array( 'href'=>true, 'title'=>true, 'class'=>true ),
404 'span' => array( 'class'=>true, 'data-tip'=>true)
405 )
406 );
407
408
409 $enabled_in_pro_string = '';
410 $enabled_in_pro_class = '';
411 if( !empty($field['args']['pro_label']) ){
412 $enabled_in_pro_string = $field['args']['pro_label'];
413 $enabled_in_pro_class = 'class="wpc-row-pro-only wpc-pro-badge-text"';
414 }
415
416 if ( ! empty( $field['args']['label_for'] ) ) {
417 echo '<th scope="row" ' . $enabled_in_pro_class. '><label for="' . esc_attr( $field['args']['label_for'] ) . '">' . esc_html($field['title']) . '</label> ' .$tooltip. $enabled_in_pro_string . '</th>'; // $tooltip already escaped
418 } else {
419 echo '<th scope="row">'. esc_html($field['title'] ) . ' ' .$tooltip. '</th>'; // $tooltip already escaped
420 }
421
422 echo '<td>';
423 call_user_func( $field['callback'], $field['args'] );
424 echo '</td>';
425
426 if( $sortable ){
427 $title = ( ! defined( 'FLRT_FILTERS_PRO' ) ) ? __( 'Editing the order of URL prefixes is available in the PRO version', 'filter-everything' ) : '';
428 echo '<td class="wpc-order-sortable-handle-icon"><span class="dashicons dashicons-menu wpc-field-sortable-handle" title="'.esc_attr( $title ).'">&nbsp;</span></td>';
429 }
430
431 echo '</tr>';
432
433 do_action('wpc_after_settings_field', $field );
434
435 $i++;
436 }
437 }
438
439 public function getOption($key, $default = null)
440 {
441 if (is_null($this->options)) {
442 $this->options = get_option($this->optionName);
443 }
444
445 return isset($this->options[$key]) ? $this->options[$key] : $default;
446 }
447
448 public function inProButtonCallback($args)
449 {
450 echo flrt_unlock_in_pro();
451
452 if( isset( $args['description'] ) ){
453 printf( '<p class="description">%s</p>', esc_html( $args['description'] ) );
454 }
455 }
456
457 /**
458 * Read-only field: delegates the cell markup to $args['render'] (callable
459 * receiving $args) or echoes pre-escaped $args['html']. Nothing is saved —
460 * for generated previews, hints and code blocks inside a settings table.
461 */
462 public function htmlCallback($args)
463 {
464 if ( isset( $args['render'] ) && is_callable( $args['render'] ) ) {
465 call_user_func( $args['render'], $args );
466 return;
467 }
468
469 if ( isset( $args['html'] ) ) {
470 echo wp_kses_post( $args['html'] );
471 }
472 }
473 }
474