*/ public static function schema() { $schema = array( 'community_name' => array( 'type' => 'string', 'default' => get_bloginfo( 'name' ), ), 'portal_slug' => array( 'type' => 'slug', 'default' => 'community', ), 'portal_layout' => array( 'type' => 'enum', 'default' => 'standalone', 'options' => array( 'standalone', 'theme' ), ), 'color_mode' => array( 'type' => 'enum', 'default' => 'system', 'options' => array( 'system', 'light', 'dark' ), ), 'brand_color' => array( 'type' => 'color', 'default' => '#3f22ff', ), 'space_icon_color' => array( 'type' => 'enum', 'default' => 'brand', 'options' => array( 'brand', 'space' ), ), 'access' => array( 'type' => 'enum', 'default' => 'public', 'options' => array( 'public', 'members' ), ), 'member_directory' => array( 'type' => 'enum', 'default' => 'members', 'options' => array( 'members', 'off' ), ), 'link_previews' => array( 'type' => 'bool', 'default' => true, ), 'max_upload_mb' => array( 'type' => 'int', 'default' => 5, 'min' => 1, 'max' => 50, ), 'emails_enabled' => array( 'type' => 'bool', 'default' => true, ), 'email_from_name' => array( 'type' => 'string', 'default' => '', ), 'email_from_address' => array( 'type' => 'email', 'default' => '', ), 'member_approval' => array( 'type' => 'bool', 'default' => false, ), 'post_review' => array( 'type' => 'enum', 'default' => 'off', 'options' => array( 'off', 'first', 'all' ), ), 'blocked_words' => array( 'type' => 'textarea', 'default' => '', ), 'blocked_words_action' => array( 'type' => 'enum', 'default' => 'review', 'options' => array( 'review', 'mask', 'block' ), ), 'report_threshold' => array( 'type' => 'int', 'default' => 3, 'min' => 0, 'max' => 20, ), 'messages_enabled' => array( 'type' => 'bool', 'default' => true, ), 'messages_who' => array( 'type' => 'enum', 'default' => 'shared', 'options' => array( 'everyone', 'shared', 'staff' ), ), 'group_chats' => array( 'type' => 'bool', 'default' => true, ), 'group_chat_max' => array( 'type' => 'int', 'default' => 20, 'min' => 3, 'max' => 100, ), 'auth_pages' => array( 'type' => 'bool', 'default' => true, ), 'signup_enabled' => array( 'type' => 'bool', 'default' => (bool) get_option( 'users_can_register' ), ), 'signup_terms_url' => array( 'type' => 'url', 'default' => '', ), 'social_google' => array( 'type' => 'bool', 'default' => false, ), 'social_google_id' => array( 'type' => 'string', 'default' => '', ), 'social_google_secret' => array( 'type' => 'string', 'default' => '', ), 'social_facebook' => array( 'type' => 'bool', 'default' => false, ), 'social_facebook_id' => array( 'type' => 'string', 'default' => '', ), 'social_facebook_secret' => array( 'type' => 'string', 'default' => '', ), 'gamification' => array( 'type' => 'bool', 'default' => true, ), 'leaderboard' => array( 'type' => 'bool', 'default' => true, ), 'points_post' => array( 'type' => 'int', 'default' => 5, 'min' => 0, 'max' => 100, ), 'points_comment' => array( 'type' => 'int', 'default' => 2, 'min' => 0, 'max' => 100, ), 'points_like' => array( 'type' => 'int', 'default' => 1, 'min' => 0, 'max' => 100, ), 'points_daily' => array( 'type' => 'int', 'default' => 1, 'min' => 0, 'max' => 100, ), 'points_event' => array( 'type' => 'int', 'default' => 2, 'min' => 0, 'max' => 100, ), 'levels' => array( 'type' => 'levels', 'default' => \SpaceBay\Gamification\Levels::defaults(), ), 'badges' => array( 'type' => 'badges', 'default' => \SpaceBay\Gamification\Badges::defaults(), ), 'events_enabled' => array( 'type' => 'bool', 'default' => true, ), 'events_who' => array( 'type' => 'enum', 'default' => 'admins', 'options' => array( 'admins', 'members' ), ), 'logo_id' => array( 'type' => 'image', 'default' => 0, ), 'show_community_name' => array( 'type' => 'bool', 'default' => true, ), 'welcome_title' => array( 'type' => 'string', 'default' => __( 'Welcome to our community', 'spacebay' ), ), 'welcome_message' => array( 'type' => 'textarea', 'default' => __( 'Say hello, ask questions and share what you are working on.', 'spacebay' ), ), 'delete_data_on_uninstall' => array( 'type' => 'bool', 'default' => false, ), ); /** * Filters the settings schema so extensions can add their own settings. * * @since 1.0.0 * * @param array $schema Settings schema keyed by setting name. */ return apply_filters( 'spacebay_settings_schema', $schema ); } /** * Returns default values for every setting. * * @return array */ public static function defaults() { return wp_list_pluck( self::schema(), 'default' ); } /** * Returns every setting merged with defaults. * * @return array */ public static function all() { if ( null === self::$cache ) { $stored = get_option( self::OPTION, array() ); self::$cache = wp_parse_args( is_array( $stored ) ? $stored : array(), self::defaults() ); } return self::$cache; } /** * Returns one setting. * * @param string $key Setting name. * @param mixed $fallback Value returned when the setting does not exist. * @return mixed */ public static function get( $key, $fallback = null ) { $all = self::all(); return array_key_exists( $key, $all ) ? $all[ $key ] : $fallback; } /** * Validates and saves a partial set of settings. * * Unknown keys are ignored. Invalid values keep their previous value. * * @param array $input Raw values keyed by setting name. * @return array The saved settings. */ public static function update( array $input ) { $current = self::all(); $schema = self::schema(); $old = $current; foreach ( $input as $key => $value ) { if ( ! isset( $schema[ $key ] ) ) { continue; } $clean = self::sanitize( $value, $schema[ $key ] ); if ( null !== $clean ) { $current[ $key ] = $clean; } } update_option( self::OPTION, $current ); self::$cache = null; if ( $old['portal_slug'] !== $current['portal_slug'] ) { update_option( 'spacebay_flush_rewrite', 1 ); } /** * Fires after SpaceBay settings are saved. * * @since 1.0.0 * * @param array $current New settings. * @param array $old Previous settings. */ do_action( 'spacebay_settings_updated', $current, $old ); return self::all(); } /** * Sanitizes one value according to its schema entry. * * @param mixed $value Raw value. * @param array $field Schema entry. * @return mixed|null Clean value, or null when the value is not valid. */ private static function sanitize( $value, array $field ) { // Settings added through the spacebay_settings_schema filter can bring their own cleaning. if ( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) { return call_user_func( $field['sanitize'], $value ); } switch ( $field['type'] ) { case 'bool': return rest_sanitize_boolean( $value ); case 'int': $value = (int) $value; return max( (int) $field['min'], min( (int) $field['max'], $value ) ); case 'enum': $value = sanitize_key( (string) $value ); return in_array( $value, $field['options'], true ) ? $value : null; case 'slug': $value = Slug::make( (string) $value ); return '' !== $value ? $value : null; case 'color': $value = sanitize_hex_color( (string) $value ); return $value ? strtolower( $value ) : null; case 'email': $value = sanitize_email( (string) $value ); return '' === $value || is_email( $value ) ? $value : null; case 'textarea': return sanitize_textarea_field( (string) $value ); case 'url': return esc_url_raw( trim( (string) $value ), array( 'http', 'https' ) ); case 'levels': return is_array( $value ) ? \SpaceBay\Gamification\Levels::sanitize( $value ) : null; case 'badges': return is_array( $value ) ? \SpaceBay\Gamification\Badges::sanitize( $value ) : null; case 'image': $value = absint( $value ); return 0 === $value || wp_attachment_is_image( $value ) ? $value : null; case 'string': default: return sanitize_text_field( (string) $value ); } } /** * Clears the request cache. Used by tests and after switching sites. * * @return void */ public static function flush_cache() { self::$cache = null; } }