Making WordPress.org


Ignore:
Timestamp:
07/05/2023 04:06:16 AM (3 years ago)
Author:
dd32
Message:

Trac: Add a custom API to fetch/set user preferences.

See #5055.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/trac-notifications/trac-notifications-db.php

    r10420 r12711  
    191191                call_user_func( array( $this->db, $method ), '_notifications', (array) $where );
    192192            }
     193        }
     194    }
     195
     196    /**
     197     * Fetch user preferences from Trac.
     198     *
     199     * @param string $username The username.
     200     * @return array The user preferences. [ key => value ].
     201     */
     202    function get_user_prefs( $username ) {
     203        $data = $this->db->get_results( $this->db->prepare(
     204            'SELECT name, value FROM session_attribute WHERE sid = %s',
     205            $username
     206        ), ARRAY_A );
     207
     208        return array_column( $data, 'value', 'name' );
     209    }
     210
     211    /**
     212     * Set a user preference in Trac.
     213     *
     214     * @param string $username   The username.
     215     * @param string $name       The preference.
     216     * @param string|null $value The value to set. If null, the preference will be deleted.
     217     */
     218    function set_user_pref( $username, $name, $value = null ) {
     219        // The trac column for username is `sid`. All our users are authenticated.
     220        // The session_attribute table has an index: UNIQUE (sid,authenticated,name)
     221
     222        if ( ! is_null( $value ) ) {
     223            $result = $this->db->update(
     224                'session_attribute',
     225                [ 'value' => $value ],
     226                [
     227                    'sid'           => $username,
     228                    'authenticated' => 1,
     229                    'name'          => $name
     230                ]
     231            );
     232
     233            if ( ! $result ) {
     234                // Insert the session if it doesn't exist.
     235                $user_has_visited_trac = $this->db->get_var( $this->db->prepare(
     236                    'SELECT sid FROM session WHERE sid = %s',
     237                    $username
     238                ) );
     239                if ( ! $user_has_visited_trac ) {
     240                    $this->db->insert( 'session', [
     241                        'sid'           => $username,
     242                        'authenticated' => 1,
     243                        'last_visit'    => time(),
     244                    ] );
     245                }
     246
     247                // Set the pref.
     248                $result = $this->db->insert( 'session_attribute', [
     249                    'sid'           => $username,
     250                    'authenticated' => 1,
     251                    'name'          => $name,
     252                    'value'         => $value,
     253                ] );
     254            }
     255
     256            return $result;
     257        } else {
     258            return $this->db->delete( 'session_attribute', [
     259                'sid'           => $username,
     260                'authenticated' => 1,
     261                'name'          => $name
     262            ] );
    193263        }
    194264    }
Note: See TracChangeset for help on using the changeset viewer.