| @@ -17,9 +17,20 @@ | ||
| 17 | 17 | * @subpackage WP_Ultimate_Post_Grid/includes/public |
| 18 | 18 | * @author Brecht Vandersmissen <brecht@bootstrapped.ventures> |
| 19 | 19 | */ |
| 20 | 20 | class WPUPG_Filter { |
| 21 | + | |
| 21 | 22 | /** |
| 23 | + * Register actions and filters. | |
| 24 | + * | |
| 25 | + * @since 3.8.0 | |
| 26 | + */ | |
| 27 | + public static function init() { | |
| 28 | + add_filter( 'wpupg_filter_defaults', array( __CLASS__, 'responsive_defaults' ), 99 ); | |
| 29 | + add_filter( 'wpupg_filter_sanitize_options', array( __CLASS__, 'responsive_sanitize' ), 99, 2 ); | |
| 30 | + } | |
| 31 | + | |
| 32 | + /** | |
| 22 | 33 | * Get filter defaults. |
| 23 | 34 | * |
| 24 | 35 | * @since 3.0.0 |
| 25 | 36 | * @param mixed $type Optional filter type to get the defaults for. |
| @@ -38,8 +49,52 @@ | ||
| 38 | 49 | } |
| 39 | 50 | } |
| 40 | 51 | |
| 41 | 52 | /** |
| 53 | + * Default resposive options for all filters. | |
| 54 | + * | |
| 55 | + * @since 3.8.0 | |
| 56 | + * @param mixed $defaults Current defaults to filter. | |
| 57 | + */ | |
| 58 | + public static function responsive_defaults( $defaults ) { | |
| 59 | + foreach ( $defaults as $filter => $options ) { | |
| 60 | + $defaults[ $filter ]['responsive'] = array( | |
| 61 | + 'desktop' => 'show', | |
| 62 | + 'tablet' => 'show', | |
| 63 | + 'mobile' => 'show', | |
| 64 | + ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + return $defaults; | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * Sanitize resposive options for all filters when saving. | |
| 72 | + * | |
| 73 | + * @since 3.8.0 | |
| 74 | + * @param mixed $sanitized_options Current sanitized options. | |
| 75 | + * @param mixed $filter Filter to sanitize. | |
| 76 | + */ | |
| 77 | + public static function responsive_sanitize( $sanitized_options, $filter ) { | |
| 78 | + if ( isset( $filter['options']['responsive'] ) ) { | |
| 79 | + $sanitized_responsive = array(); | |
| 80 | + | |
| 81 | + // Limited options fields. | |
| 82 | + $field_options = array( 'show', 'hide', 'toggle_open', 'toggle_closed' ); | |
| 83 | + | |
| 84 | + foreach ( array( 'desktop', 'tablet', 'mobile' ) as $size ) { | |
| 85 | + if ( isset( $filter['options']['responsive'][ $size ] ) && in_array( $filter['options']['responsive'][ $size ], $field_options, true ) ) { | |
| 86 | + $sanitized_responsive[ $size ] = $filter['options']['responsive'][ $size ]; | |
| 87 | + } | |
| 88 | + } | |
| 89 | + | |
| 90 | + $sanitized_options['responsive'] = $sanitized_responsive; | |
| 91 | + } | |
| 92 | + | |
| 93 | + return $sanitized_options; | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 42 | 97 | * Get filter with defaults. |
| 43 | 98 | * |
| 44 | 99 | * @since 3.0.0 |
| 45 | 100 | * @param mixed $filter Filter to get the defaults for. |
| @@ -45,14 +100,42 @@ | ||
| 45 | 100 | * @param mixed $filter Filter to get the defaults for. |
| 46 | 101 | */ |
| 47 | 102 | public static function filter_with_defaults( $filter ) { |
| 48 | 103 | if ( $filter && isset( $filter['type'] ) ) { |
| 104 | + // Set default options. | |
| 49 | 105 | $defaults = self::get_defaults( $filter['type'] ); |
| 50 | - | |
| 51 | 106 | if ( $defaults ) { |
| 52 | 107 | $filter['options'] = array_replace_recursive( $defaults, $filter['options'] ); |
| 53 | 108 | } |
| 109 | + | |
| 110 | + // Set default label. | |
| 111 | + if ( ! isset( $filter['label'] ) ) { | |
| 112 | + $filter['label'] = ''; | |
| 113 | + } | |
| 54 | 114 | } |
| 55 | 115 | |
| 56 | 116 | return $filter; |
| 57 | 117 | } |
| 118 | + | |
| 119 | + /** | |
| 120 | + * Get general filter style defaults. | |
| 121 | + * | |
| 122 | + * @since 3.1.0 | |
| 123 | + */ | |
| 124 | + public static function get_general_style_defaults() { | |
| 125 | + $defaults = apply_filters( 'wpupg_filter_general_style_defaults', array( | |
| 126 | + 'display' => 'block', | |
| 127 | + 'alignment' => 'left', | |
| 128 | + 'spacing_vertical' => 10, | |
| 129 | + 'spacing_horizontal' => 10, | |
| 130 | + 'width' => 250, | |
| 131 | + 'label_display' => 'block', | |
| 132 | + 'label_alignment' => 'left', | |
| 133 | + 'label_font_size' => 14, | |
| 134 | + 'label_style' => 'bold', | |
| 135 | + ) ); | |
| 136 | + | |
| 137 | + return $defaults; | |
| 138 | + } | |
| 58 | 139 | } |
| 140 | + | |
| 141 | +WPUPG_Filter::init(); | |