| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle output for the Clear Filter. |
| 4 |
* |
| 5 |
* @link https://bootstrapped.ventures |
| 6 |
* @since 3.0.0 |
| 7 |
* |
| 8 |
* @package WP_Ultimate_Post_Grid |
| 9 |
* @subpackage WP_Ultimate_Post_Grid/includes/public |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Handle output for the Clear Filter. |
| 14 |
* |
| 15 |
* @since 3.0.0 |
| 16 |
* @package WP_Ultimate_Post_Grid |
| 17 |
* @subpackage WP_Ultimate_Post_Grid/includes/public |
| 18 |
* @author Brecht Vandersmissen <brecht@bootstrapped.ventures> |
| 19 |
*/ |
| 20 |
class WPUPG_Filter_Clear { |
| 21 |
|
| 22 |
/** |
| 23 |
* Register actions and filters. |
| 24 |
* |
| 25 |
* @since 3.0.0 |
| 26 |
*/ |
| 27 |
public static function init() { |
| 28 |
add_filter( 'wpupg_filter_defaults', array( __CLASS__, 'defaults' ), 9 ); |
| 29 |
add_filter( 'wpupg_filter_sanitize_options', array( __CLASS__, 'sanitize' ), 9, 2 ); |
| 30 |
add_filter( 'wpupg_output_filter', array( __CLASS__, 'output' ), 9, 4 ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Default options for this filter. |
| 35 |
* |
| 36 |
* @since 3.0.0 |
| 37 |
* @param mixed $defaults Current defaults to filter. |
| 38 |
*/ |
| 39 |
public static function defaults( $defaults ) { |
| 40 |
$defaults['clear'] = array( |
| 41 |
'inactive_opacity' => '30', |
| 42 |
'clear_button_text' => __( 'Clear All Selections', 'wp-ultimate-post-grid' ), |
| 43 |
'style' => array( |
| 44 |
'font_size' => '14', |
| 45 |
'background_color' => '#FFFFFF', |
| 46 |
'background_hover_color' => '#FFFFFF', |
| 47 |
'text_color' => '#2E5077', |
| 48 |
'text_hover_color' => '#1C3148', |
| 49 |
'border_color' => '#FFFFFF', |
| 50 |
'border_hover_color' => '#FFFFFF', |
| 51 |
'border_width' => '0', |
| 52 |
'border_radius' => '0', |
| 53 |
'margin_vertical' => '3', |
| 54 |
'margin_horizontal' => '0', |
| 55 |
'padding_vertical' => '0', |
| 56 |
'padding_horizontal' => '0', |
| 57 |
'alignment' => 'left', |
| 58 |
), |
| 59 |
); |
| 60 |
|
| 61 |
return $defaults; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Sanitize filter options when saving. |
| 66 |
* |
| 67 |
* @since 3.0.0 |
| 68 |
* @param mixed $sanitized_options Current sanitized options. |
| 69 |
* @param mixed $filter Filter to sanitize. |
| 70 |
*/ |
| 71 |
public static function sanitize( $sanitized_options, $filter ) { |
| 72 |
if ( 'clear' === $filter['type'] ) { |
| 73 |
$options = $filter['options']; |
| 74 |
|
| 75 |
// Special fields. |
| 76 |
if ( isset( $options['inactive_opacity'] ) ) { $sanitized_options['inactive_opacity'] = max( 0, min( 100, intval( $options['inactive_opacity'] ) ) ); } |
| 77 |
|
| 78 |
// Text fields. |
| 79 |
if ( isset( $options['clear_button_text'] ) ) { $sanitized_options['clear_button_text'] = sanitize_text_field( $options['clear_button_text'] ); } |
| 80 |
|
| 81 |
// Style. |
| 82 |
if ( isset( $options['style'] ) ) { |
| 83 |
$sanitized_style = array(); |
| 84 |
$style = $options['style']; |
| 85 |
|
| 86 |
// Text fields. |
| 87 |
if ( isset( $style['background_color'] ) ) { $sanitized_style['background_color'] = sanitize_text_field( $style['background_color'] ); } |
| 88 |
if ( isset( $style['background_hover_color'] ) ) { $sanitized_style['background_hover_color'] = sanitize_text_field( $style['background_hover_color'] ); } |
| 89 |
if ( isset( $style['border_color'] ) ) { $sanitized_style['border_color'] = sanitize_text_field( $style['border_color'] ); } |
| 90 |
if ( isset( $style['border_hover_color'] ) ) { $sanitized_style['border_hover_color'] = sanitize_text_field( $style['border_hover_color'] ); } |
| 91 |
if ( isset( $style['text_color'] ) ) { $sanitized_style['text_color'] = sanitize_text_field( $style['text_color'] ); } |
| 92 |
if ( isset( $style['text_hover_color'] ) ) { $sanitized_style['text_hover_color'] = sanitize_text_field( $style['text_hover_color'] ); } |
| 93 |
|
| 94 |
// Number fields. |
| 95 |
if ( isset( $style['border_width'] ) ) { $sanitized_style['border_width'] = intval( $style['border_width'] ); } |
| 96 |
if ( isset( $style['border_radius'] ) ) { $sanitized_style['border_radius'] = intval( $style['border_radius'] ); } |
| 97 |
if ( isset( $style['font_size'] ) ) { $sanitized_style['font_size'] = intval( $style['font_size'] ); } |
| 98 |
if ( isset( $style['margin_horizontal'] ) ) { $sanitized_style['margin_horizontal'] = intval( $style['margin_horizontal'] ); } |
| 99 |
if ( isset( $style['margin_vertical'] ) ) { $sanitized_style['margin_vertical'] = intval( $style['margin_vertical'] ); } |
| 100 |
if ( isset( $style['padding_horizontal'] ) ) { $sanitized_style['padding_horizontal'] = intval( $style['padding_horizontal'] ); } |
| 101 |
if ( isset( $style['padding_vertical'] ) ) { $sanitized_style['padding_vertical'] = intval( $style['padding_vertical'] ); } |
| 102 |
|
| 103 |
// Limited options fields. |
| 104 |
$field_options = array( 'left', 'center', 'right' ); |
| 105 |
if ( isset( $style['alignment'] ) && in_array( $style['alignment'], $field_options, true ) ) { |
| 106 |
$sanitized_style['alignment'] = $style['alignment']; |
| 107 |
} |
| 108 |
|
| 109 |
$sanitized_options['style'] = $sanitized_style; |
| 110 |
} |
| 111 |
|
| 112 |
// Combine with defaults. |
| 113 |
$filter_defaults = WPUPG_Filter::get_defaults( 'clear' ); |
| 114 |
$sanitized_options = array_replace_recursive( $filter_defaults, $sanitized_options ); |
| 115 |
} |
| 116 |
|
| 117 |
return $sanitized_options; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Output a specific filter for a specific grid. |
| 122 |
* |
| 123 |
* @since 3.0.0 |
| 124 |
* @param mixed $output Current filter output. |
| 125 |
* @param mixed $grid Grid to output. |
| 126 |
* @param mixed $filter Filter to output. |
| 127 |
* @param mixed $args Optional arguments. |
| 128 |
*/ |
| 129 |
public static function output( $output, $grid, $filter, $args ) { |
| 130 |
if ( 'clear' === $filter['type'] ) { |
| 131 |
$output = ''; |
| 132 |
$output .= self::style( $grid, $filter, $args ); |
| 133 |
$output .= self::html( $grid, $filter, $args ); |
| 134 |
} |
| 135 |
|
| 136 |
return $output; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Output HTML for a filter. |
| 141 |
* |
| 142 |
* @since 3.0.0 |
| 143 |
* @param mixed $grid Grid to output. |
| 144 |
* @param mixed $filter Filter to output. |
| 145 |
* @param mixed $args Optional arguments. |
| 146 |
*/ |
| 147 |
public static function html( $grid, $filter, $args ) { |
| 148 |
$output = ''; |
| 149 |
$options = $filter['options']; |
| 150 |
|
| 151 |
$output .= '<div class="wpupg-filter-item wpupg-filter-clear-button" role="button" tabindex="0">' . $options['clear_button_text'] . '</div>'; |
| 152 |
|
| 153 |
return $output; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Output styling for a filter. |
| 158 |
* |
| 159 |
* @since 3.0.0 |
| 160 |
* @param mixed $grid Grid to output. |
| 161 |
* @param mixed $filter Filter to output. |
| 162 |
* @param mixed $args Optional arguments. |
| 163 |
*/ |
| 164 |
public static function style( $grid, $filter, $args ) { |
| 165 |
$options = $filter['options']; |
| 166 |
$style = $options['style']; |
| 167 |
|
| 168 |
$output = '<style>'; |
| 169 |
|
| 170 |
// Filter styling. |
| 171 |
$filter_selector = '#wpupg-grid-' . $grid->slug_or_id() . '-filter-' . $filter['id']; |
| 172 |
$output .= $filter_selector . ' {'; |
| 173 |
$output .= 'text-align: ' . $style['alignment'] . ';'; |
| 174 |
$output .= 'margin: 0 -' . $style['margin_horizontal'] . 'px;'; |
| 175 |
$output .= '}'; |
| 176 |
|
| 177 |
// Default Item styling. |
| 178 |
$item_selector = $filter_selector . ' .wpupg-filter-item'; |
| 179 |
|
| 180 |
$output .= $item_selector . ' {'; |
| 181 |
|
| 182 |
if ( $options['inactive_opacity'] < 100 ) { |
| 183 |
$output .= 'opacity: ' . $options['inactive_opacity'] / 100 . ';'; |
| 184 |
} |
| 185 |
if ( $style['border_width'] ) { |
| 186 |
$output .= 'border: ' . $style['border_width'] . 'px solid ' . $style['border_color'] . ';'; |
| 187 |
} |
| 188 |
if ( $style['border_radius'] ) { |
| 189 |
$output .= 'border-radius: ' . $style['border_radius'] . 'px;'; |
| 190 |
} |
| 191 |
$output .= 'background-color: ' . $style['background_color'] . ';'; |
| 192 |
$output .= 'color: ' . $style['text_color'] . ';'; |
| 193 |
|
| 194 |
$output .= 'font-size: ' . $style['font_size'] . 'px;'; |
| 195 |
$output .= 'margin: ' . $style['margin_vertical'] . 'px ' . $style['margin_horizontal'] . 'px;'; |
| 196 |
$output .= 'padding: ' . $style['padding_vertical'] . 'px ' . $style['padding_horizontal'] . 'px;'; |
| 197 |
$output .= '}'; |
| 198 |
|
| 199 |
// Focus Item styling. |
| 200 |
$hover_styles = ''; |
| 201 |
$hover_styles .= 'outline: none;'; |
| 202 |
$hover_styles .= 'border-color: ' . $style['border_hover_color'] . ';'; |
| 203 |
$hover_styles .= 'background-color: ' . $style['background_hover_color'] . ';'; |
| 204 |
$hover_styles .= 'color: ' . $style['text_hover_color'] . ';'; |
| 205 |
|
| 206 |
$output .= $item_selector . ':focus {' . $hover_styles . '}'; |
| 207 |
|
| 208 |
// Hover Item, only with mouse pointer. |
| 209 |
$output .= '@media (hover: hover) and (pointer: fine) {'; |
| 210 |
$output .= $item_selector . ':hover {' . $hover_styles . '}'; |
| 211 |
$output .= '}'; |
| 212 |
|
| 213 |
$output .= '</style>'; |
| 214 |
|
| 215 |
return $output; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
WPUPG_Filter_Clear::init(); |
| 220 |
|