Make WordPress Core

Ticket #33542: wp-user-preferences.php

File wp-user-preferences.php, 2.6 KB (added by johnjamesjacoby, 9 years ago)

A plugin-style example API

Line 
1<?php
2
3/**
4 * Plugin Name: WP User Preferences
5 * Plugin URI:  https://wordpress.org/plugins/wp-user-preferences/
6 * Description: Prefer user settings over site & network options
7 * Author:      John James Jacoby
8 * Version:     0.1.0
9 * Author URI:  https://profiles.wordpress.org/johnjamesjacoby/
10 * License:     GPL v2 or later
11 */
12
13// Exit if accessed directly
14defined( 'ABSPATH' ) || exit;
15
16/**
17 * Get preference for the currently logged in user. If no user is logged in,
18 * a site or network preference will be returned if one is set.
19 *
20 * @since 0.1.0
21 *
22 * @param  string  $key
23 * @return mixed
24 */
25function wp_get_current_user_preference( $key = '' ) {
26        return wp_get_user_preference( get_current_user_id(), $key );
27}
28
29/**
30 * Get a user preference
31 *
32 * @since 0.1.0
33 *
34 * @param  int     $user_id
35 * @param  string  $key
36 *
37 * @return mixed
38 */
39function wp_get_user_preference( $user_id = 0, $key = '' ) {
40
41        // Get user/site/network preference map
42        $keys = wp_map_user_preference_key( $key );
43
44        // Check usermeta first
45        $retval = get_usermeta( $user_id, $keys['user'] );
46
47        // Nothing, so check site option
48        if ( false === $retval ) {
49                $retval = get_option( $keys['site'] );
50
51                // Nothing, so check network option if multisite
52                if ( false === $retval && is_multisite() ) {
53                        $retval = get_site_option( $keys['network'] );
54                }
55        }
56
57        // Filter & return
58        return apply_filters( 'wp_get_user_preference', $retval, $user_id, $key );
59}
60
61/**
62 * Return an array of key/value pairs for user/site/network settings, based on
63 * the usermeta key being passed in.
64 *
65 * This function exists because not all user, site, & network metadata keys are
66 * the same, even though they return the same type of data back. By passing in
67 * the usermeta key, we can make decisions about what site & network keys map to
68 * a user preference, and fallback to them if no user preference is set yet.
69 *
70 * @since 0.1.0
71 *
72 * @param   string  $key
73 *
74 * @return  array
75 */
76function wp_map_user_preference_key( $key = '' ) {
77
78        // Which usermeta key are we mapping to?
79        switch ( $key ) {
80
81                // These keys are different between user/site/network
82                case 'some_other_preference' :
83                        $retval = array(
84                                'user'    => 'one',
85                                'site'    => 'two',
86                                'network' => 'three'
87                        );
88                        break;
89
90                // These keys are the same between user/site/network
91                case 'admin_color' :
92                case 'timezone' :
93                case 'time_format' :
94                case 'date_format' :
95                case 'WPLANG' :
96                default :
97
98                        // Default return value
99                        $retval = array(
100                                'user'    => $key,
101                                'site'    => $key,
102                                'network' => $key
103                        );
104
105                        break;
106        }
107
108        // Filter & return
109        return apply_filters( 'wp_map_user_preference_key', $retval, $key );
110}