0, 'event_slug' => 'event', 'venue_slug' => 'venue', 'organizer_slug' => 'organizer', 'category_base' => 'events/category', 'tag_base' => 'events/tag', 'tec_urls' => 1, 'currency' => 'USD', 'currency_symbol' => '$', 'list_per_page' => 10, 'default_view' => 'month', 'show_past_in_list' => 0, 'rsvp_default' => 0, 'rsvp_notify_email' => '', 'rsvp_from_name' => '', 'rsvp_collect_phone' => 0, 'load_css' => 1, 'map_provider' => 'google', 'show_map' => 0, 'delete_on_uninstall' => 0, ); } /** * All settings merged with defaults. * * @return array */ public static function all() { if ( null === self::$cache ) { $saved = get_option( self::OPTION, array() ); self::$cache = wp_parse_args( is_array( $saved ) ? $saved : array(), self::defaults() ); } return self::$cache; } /** * Get one setting. * * @param string $key Key. * @return mixed */ public static function get( $key ) { $all = self::all(); return isset( $all[ $key ] ) ? $all[ $key ] : null; } /** * Update several settings. * * @param array $values Values. */ public static function update( array $values ) { $all = array_merge( self::all(), $values ); update_option( self::OPTION, $all ); self::$cache = null; } /** * Sanitise a full settings array from the settings form. * * @param array $input Raw input. * @return array */ public static function sanitize( $input ) { $input = is_array( $input ) ? $input : array(); $current = self::all(); $defaults = self::defaults(); $out = $current; foreach ( array( 'event_slug', 'venue_slug', 'organizer_slug' ) as $key ) { if ( isset( $input[ $key ] ) ) { $slug = sanitize_title( $input[ $key ] ); $out[ $key ] = '' !== $slug ? $slug : $defaults[ $key ]; } } foreach ( array( 'category_base', 'tag_base' ) as $key ) { if ( isset( $input[ $key ] ) ) { $parts = array_filter( array_map( 'sanitize_title', explode( '/', (string) $input[ $key ] ) ) ); $out[ $key ] = $parts ? implode( '/', $parts ) : $defaults[ $key ]; } } if ( isset( $input['events_page_id'] ) ) { $out['events_page_id'] = absint( $input['events_page_id'] ); } if ( isset( $input['currency'] ) ) { $cur = strtoupper( preg_replace( '/[^A-Za-z]/', '', (string) $input['currency'] ) ); $out['currency'] = 3 === strlen( $cur ) ? $cur : $defaults['currency']; } if ( isset( $input['currency_symbol'] ) ) { $out['currency_symbol'] = mb_substr( sanitize_text_field( $input['currency_symbol'] ), 0, 5 ); } if ( isset( $input['list_per_page'] ) ) { $out['list_per_page'] = max( 1, min( 100, absint( $input['list_per_page'] ) ) ); } if ( isset( $input['default_view'] ) ) { $out['default_view'] = in_array( $input['default_view'], array( 'month', 'week', 'list' ), true ) ? $input['default_view'] : 'month'; } if ( isset( $input['map_provider'] ) ) { $out['map_provider'] = in_array( $input['map_provider'], array( 'google', 'osm', 'none' ), true ) ? $input['map_provider'] : 'google'; } if ( isset( $input['rsvp_notify_email'] ) ) { $out['rsvp_notify_email'] = sanitize_email( $input['rsvp_notify_email'] ); } if ( isset( $input['rsvp_from_name'] ) ) { $out['rsvp_from_name'] = sanitize_text_field( $input['rsvp_from_name'] ); } // Checkboxes: from the settings form a missing box means off; from code only given keys change. foreach ( array( 'tec_urls', 'show_past_in_list', 'rsvp_default', 'rsvp_collect_phone', 'load_css', 'delete_on_uninstall', 'show_map' ) as $key ) { if ( ! empty( $input['_form'] ) || array_key_exists( $key, $input ) ) { $out[ $key ] = empty( $input[ $key ] ) ? 0 : 1; } } /** * Filters the sanitised settings before they are saved. Add-ons copy their own keys from $input here. * * @param array $out Sanitised settings (all current settings, with the changes applied). * @param array $input Raw input from the settings form. */ return apply_filters( 'eventwright_settings_sanitize', $out, $input ); } /** * Clear the cache (used after updates by other code and tests). */ public static function flush() { self::$cache = null; } }