| 1 |
<?php |
| 2 |
|
| 3 |
namespace FilterEverything\Filter; |
| 4 |
|
| 5 |
if ( ! defined('WPINC') ) { |
| 6 |
wp_die(); |
| 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 |
'textarea' => array($this, 'textareaCallback'), |
| 25 |
'editor' => array($this, 'textEditorCallback'), |
| 26 |
'select' => array($this, 'selectCallback'), |
| 27 |
'hidden' => array($this, 'hiddenCallback') |
| 28 |
); |
| 29 |
|
| 30 |
foreach ($settings as $sectionId => $section) { |
| 31 |
$callback = isset($section['callback']) ? $section['callback'] : null; |
| 32 |
add_settings_section($sectionId, $section['label'], $callback, $page); |
| 33 |
|
| 34 |
if( isset( $section['fields'] ) ){ |
| 35 |
foreach ( $section['fields'] as $fieldId => $field) { |
| 36 |
$title = isset($field['title']) ? $field['title'] : ''; |
| 37 |
$field['label_for'] = $fieldId; |
| 38 |
$field['option_name'] = $optionName; |
| 39 |
$field['placeholder'] = isset($field['placeholder']) ? $field['placeholder'] : ''; |
| 40 |
$field['id'] = isset($field['id']) ? $field['id'] : $fieldId; |
| 41 |
add_settings_field($fieldId, $title, $callbacks[$field['type']], $page, $sectionId, $field); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
public function checkBoxCallback($args) |
| 48 |
{ |
| 49 |
$checkbox = '<label><input type="checkbox" name="%s[%s]" %s id="%s">%s</label>'; |
| 50 |
$checkbox = apply_filters( 'wpc_settings_field_checkbox', $checkbox, $args ); |
| 51 |
$checked = $this->getOption($args['label_for']); |
| 52 |
|
| 53 |
if ($checked == '1') { |
| 54 |
$checked = 'on'; |
| 55 |
} |
| 56 |
|
| 57 |
printf( |
| 58 |
$checkbox, |
| 59 |
esc_attr($args['option_name']), |
| 60 |
esc_attr($args['label_for']), |
| 61 |
checked('on', $checked, false), |
| 62 |
esc_attr($args['id']), |
| 63 |
esc_html($args['label']) |
| 64 |
); |
| 65 |
|
| 66 |
if( isset( $args['description'] ) ){ |
| 67 |
printf( '<p class="description">%s</p>', esc_html( $args['description'] ) ); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public function inputCallback($args) |
| 72 |
{ |
| 73 |
$input = '<input class="%s" type="text" name="%s[%s]" value="%s" placeholder="%s" id="%s">'; |
| 74 |
$class = isset( $args['class'] ) ? $args['class'] : 'regular-text'; |
| 75 |
if( isset( $args['default'] ) ){ |
| 76 |
$value = $this->getOption($args['label_for']) ? $this->getOption($args['label_for']) : $args['default']; |
| 77 |
}else{ |
| 78 |
$value = $this->getOption($args['label_for']); |
| 79 |
} |
| 80 |
|
| 81 |
$value = trim($value); |
| 82 |
|
| 83 |
printf( |
| 84 |
$input, |
| 85 |
esc_attr($class), |
| 86 |
esc_attr($this->optionName), |
| 87 |
esc_attr($args['label_for']), |
| 88 |
esc_attr($value), |
| 89 |
esc_attr($args['placeholder']), |
| 90 |
esc_attr($args['id']) |
| 91 |
); |
| 92 |
|
| 93 |
if( isset($args['id']) && $args['id'] === 'posts_container' ){ |
| 94 |
|
| 95 |
if( flrt_get_option('enable_ajax') === 'on' && ! $value ){ |
| 96 |
printf( '<p class="wpc-warning">%s</p>', esc_html__( 'You must specify Posts Container, otherwise AJAX will not work properly', 'filter-everything' ) ); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if( isset( $args['description'] ) ){ |
| 101 |
printf( '<p class="description">%s</p>', esc_html( $args['description'] ) ); |
| 102 |
} |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
public function hiddenCallback( $args ) |
| 107 |
{ |
| 108 |
$input = '<input class="%s" type="hidden" name="%s[%s]" value="%s" placeholder="%s" id="%s">'; |
| 109 |
$class = isset( $args['class'] ) ? $args['class'] : 'regular-text'; |
| 110 |
printf( |
| 111 |
$input, |
| 112 |
esc_attr($class), |
| 113 |
esc_attr($this->optionName), |
| 114 |
esc_attr($args['label_for']), |
| 115 |
esc_attr($this->getOption($args['label_for'])), |
| 116 |
esc_attr($args['placeholder']), |
| 117 |
esc_attr($args['id']) |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
public function selectCallback($args) |
| 122 |
{ |
| 123 |
$options = isset($args['options']) ? $args['options'] : []; |
| 124 |
$class = isset( $args['class'] ) ? $args['class'] : 'filter-settings-select'; |
| 125 |
$value = $this->getOption($args['label_for']); |
| 126 |
|
| 127 |
$multiple = ! empty($args['multiple']) ? 'multiple' : null; |
| 128 |
|
| 129 |
$select = '<select name="%s[%s]%s" class="%s" placeholder="%s" id="%s" %s>'; |
| 130 |
|
| 131 |
printf( |
| 132 |
$select, |
| 133 |
esc_attr($this->optionName), |
| 134 |
esc_attr($args['label_for']), |
| 135 |
$multiple ? '[]' : '', |
| 136 |
$class, |
| 137 |
esc_attr($args['placeholder']), |
| 138 |
esc_attr($args['id']), |
| 139 |
$multiple |
| 140 |
); |
| 141 |
|
| 142 |
foreach ($options as $key => $option) { |
| 143 |
if (! empty($value)) { |
| 144 |
if (is_null($multiple)) { |
| 145 |
$selected = $key === $value ? 'selected' : ''; |
| 146 |
} else { |
| 147 |
$selected = in_array($key, $value) ? 'selected' : ''; |
| 148 |
} |
| 149 |
} else { |
| 150 |
$selected = ''; |
| 151 |
} |
| 152 |
|
| 153 |
printf('<option value="%s" %s>%s</option>', esc_attr($key), $selected, esc_html($option)); |
| 154 |
} |
| 155 |
|
| 156 |
print('</select>'); |
| 157 |
|
| 158 |
if (isset($args['help'])) { |
| 159 |
printf('<p>%s</p>', esc_html($args['help'])); |
| 160 |
} |
| 161 |
|
| 162 |
if( isset( $args['description'] ) ){ |
| 163 |
printf( '<p class="description">%s</p>', esc_html( $args['description'] ) ); |
| 164 |
} |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
public function textareaCallback($args) |
| 169 |
{ |
| 170 |
$textarea = '<textarea class="filter-settings-input large-text code" name="%s[%s]" placeholder="%s" cols="30" rows="10" id="%s">%s</textarea>'; |
| 171 |
|
| 172 |
printf( |
| 173 |
$textarea, |
| 174 |
esc_attr($this->optionName), |
| 175 |
esc_attr($args['label_for']), |
| 176 |
esc_attr($args['placeholder']), |
| 177 |
esc_attr($args['id']), |
| 178 |
esc_textarea( $this->getOption($args['label_for']) ) |
| 179 |
); |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
public function textEditorCallback($args) |
| 184 |
{ |
| 185 |
wp_editor( |
| 186 |
$this->getOption( $args['label_for'] ), |
| 187 |
esc_attr( $args['id'] ), |
| 188 |
array( 'textarea_name' => esc_attr( $this->optionName . '[' . $args['label_for'] . ']' ) ) |
| 189 |
); |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
public function render() |
| 194 |
{ |
| 195 |
print('<form action="' . admin_url('options.php') . '" method="post">'); |
| 196 |
|
| 197 |
settings_errors(); |
| 198 |
|
| 199 |
settings_fields($this->group); |
| 200 |
|
| 201 |
do_action('wpc_before_sections_settings_fields', $this->page ); |
| 202 |
|
| 203 |
$this->doSettingsSections($this->page); |
| 204 |
|
| 205 |
do_action('wpc_after_sections_settings_fields', $this->page ); |
| 206 |
|
| 207 |
submit_button(); |
| 208 |
print('</form>'); |
| 209 |
} |
| 210 |
|
| 211 |
public function doSettingsSections( $page ) { |
| 212 |
global $wp_settings_sections, $wp_settings_fields; |
| 213 |
|
| 214 |
if ( ! isset( $wp_settings_sections[ $page ] ) ) { |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
foreach ( (array) $wp_settings_sections[ $page ] as $section ) { |
| 219 |
|
| 220 |
do_action('wpc_before_settings_fields_title', $page ); |
| 221 |
|
| 222 |
if ( $section['title'] ) { |
| 223 |
echo "<h2>". wp_kses( $section['title'], array( 'span' => array( 'class' => true ) )) ."</h2>\n"; |
| 224 |
} |
| 225 |
|
| 226 |
do_action('wpc_after_settings_fields_title', $page ); |
| 227 |
|
| 228 |
if ( $section['callback'] ) { |
| 229 |
call_user_func( $section['callback'], $section ); |
| 230 |
} |
| 231 |
|
| 232 |
if ( ! isset( $wp_settings_fields ) || ! isset( $wp_settings_fields[ $page ] ) || ! isset( $wp_settings_fields[ $page ][ $section['id'] ] ) ) { |
| 233 |
continue; |
| 234 |
} |
| 235 |
|
| 236 |
$sortable = ( $section['id'] === 'wpc_slugs' ) ? ' wpc-sortable-table' : ''; |
| 237 |
|
| 238 |
echo '<table class="wpc-form-table form-table'.esc_attr($sortable).'" role="presentation">'; |
| 239 |
$this->doSettingsFields( $page, $section['id'] ); |
| 240 |
echo '</table>'; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
public function doSettingsFields( $page, $section ) { |
| 245 |
global $wp_settings_fields; |
| 246 |
|
| 247 |
if ( ! isset( $wp_settings_fields[ $page ][ $section ] ) ) { |
| 248 |
return; |
| 249 |
} |
| 250 |
$i = 1; |
| 251 |
|
| 252 |
foreach ( (array) $wp_settings_fields[ $page ][ $section ] as $field ) { |
| 253 |
$class = ''; |
| 254 |
|
| 255 |
if( $field['id'] === 'bottom_widget_compatibility' ){ |
| 256 |
if( flrt_get_option('show_bottom_widget') === 'on' ){ |
| 257 |
$field['args']['class'] .= ' wpc-opened'; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
if ( ! empty( $field['args']['class'] ) ) { |
| 262 |
$class = ' class="' . esc_attr( $field['args']['class'] ) . '"'; |
| 263 |
} |
| 264 |
|
| 265 |
echo "<tr{$class}>"; |
| 266 |
|
| 267 |
$sortable = isset( $field['args']['sortable'] ) ? $i : ''; |
| 268 |
if( $sortable ){ |
| 269 |
echo '<td class="wpc-order-td">'.esc_html($sortable).'</td>'; |
| 270 |
} |
| 271 |
|
| 272 |
$tooltip = isset( $field['args']['tooltip'] ) ? flrt_tooltip( array( 'tooltip' => $field['args']['tooltip'] ) ) : ''; |
| 273 |
$tooltip = wp_kses( |
| 274 |
$tooltip, |
| 275 |
array( |
| 276 |
'strong' => array(), |
| 277 |
'br' => array(), |
| 278 |
'a' => array( 'href'=>true, 'title'=>true, 'class'=>true ), |
| 279 |
'span' => array( 'class'=>true, 'data-tip'=>true) |
| 280 |
) |
| 281 |
); |
| 282 |
|
| 283 |
if ( ! empty( $field['args']['label_for'] ) ) { |
| 284 |
echo '<th scope="row"><label for="' . esc_attr( $field['args']['label_for'] ) . '">' . esc_html($field['title']) . '</label> ' .$tooltip. '</th>'; // $tooltip already escaped |
| 285 |
} else { |
| 286 |
echo '<th scope="row">'. esc_html($field['title'] ) . ' ' .$tooltip. '</th>'; // $tooltip already escaped |
| 287 |
} |
| 288 |
|
| 289 |
echo '<td>'; |
| 290 |
call_user_func( $field['callback'], $field['args'] ); |
| 291 |
echo '</td>'; |
| 292 |
|
| 293 |
if( $sortable ){ |
| 294 |
$title = ( ! defined( 'FLRT_FILTERS_PRO' ) ) ? __( 'Editing the order of URL prefixes is available in PRO version', 'filter-everything' ) : ''; |
| 295 |
echo '<td class="wpc-order-sortable-handle-icon"><span class="dashicons dashicons-menu wpc-field-sortable-handle" title="'.esc_attr( $title ).'"> </span></td>'; |
| 296 |
} |
| 297 |
|
| 298 |
echo '</tr>'; |
| 299 |
$i++; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
public function getOption($key, $default = null) |
| 304 |
{ |
| 305 |
if (is_null($this->options)) { |
| 306 |
$this->options = get_option($this->optionName); |
| 307 |
} |
| 308 |
|
| 309 |
return isset($this->options[$key]) ? $this->options[$key] : $default; |
| 310 |
} |
| 311 |
} |
| 312 |
|