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