PluginProbe
Filter Everything — WordPress & WooCommerce Filters / 1.9.2.1
Filter Everything — WordPress & WooCommerce Filters v1.9.2.1
1.9.7 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 All 52 releases
filter-everything / src / Settings / BaseSettings.php

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

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