*/ public static function defaults() { /** * Filters the default email preferences of members. * * @since 1.0.0 * * @param array $defaults Group => on. */ return apply_filters( 'spacebay_email_preference_defaults', array( 'mentions' => true, 'messages' => true, 'comments' => true, 'spaces' => true, 'likes' => false, 'events' => true, 'badges' => false, 'moderation' => true, ) ); } /** * Returns a member's preferences merged with defaults. * * @param int $user_id User ID. * @return array */ public static function get( $user_id ) { $saved = get_user_option( self::META, $user_id ); $saved = is_array( $saved ) ? $saved : array(); $defaults = self::defaults(); $prefs = array(); foreach ( $defaults as $group => $on ) { $prefs[ $group ] = array_key_exists( $group, $saved ) ? (bool) $saved[ $group ] : (bool) $on; } return $prefs; } /** * Saves a member's preferences. * * @param int $user_id User ID. * @param array $input Group => bool. * @return array Saved preferences. */ public static function update( $user_id, array $input ) { $prefs = self::get( $user_id ); foreach ( array_keys( $prefs ) as $group ) { if ( array_key_exists( $group, $input ) ) { $prefs[ $group ] = rest_sanitize_boolean( $input[ $group ] ); } } update_user_option( $user_id, self::META, $prefs ); return $prefs; } /** * Whether a member wants an email for an action. * * @param int $user_id User ID. * @param string $action Notification action. * @return bool */ public static function wants_email( $user_id, $action ) { $group = self::group( $action ); $prefs = self::get( $user_id ); return '' !== $group && ! empty( $prefs[ $group ] ); } /** * The preference group of an action. * * @param string $action Notification action. * @return string */ public static function group( $action ) { $map = array( 'message' => 'messages', 'mention_post' => 'mentions', 'mention_comment' => 'mentions', 'comment_post' => 'comments', 'reply_comment' => 'comments', 'like_post' => 'likes', 'like_comment' => 'likes', 'space_invite' => 'spaces', 'space_request' => 'spaces', 'space_approved' => 'spaces', 'review_needed' => 'moderation', 'member_pending' => 'moderation', 'post_approved' => 'moderation', 'comment_approved' => 'moderation', 'post_removed' => 'moderation', 'comment_removed' => 'moderation', 'member_approved' => 'moderation', 'event_new' => 'events', 'event_updated' => 'events', 'event_cancelled' => 'events', 'event_reminder' => 'events', 'badge_earned' => 'badges', 'level_up' => 'badges', ); /** * Filters the email preference group of a notification action, so * add-ons can send emails for their own actions. * * @since 1.0.0 * * @param string $group Group key, or '' for no email. * @param string $action Notification action. */ return (string) apply_filters( 'spacebay_notification_group', $map[ $action ] ?? '', $action ); } }