Making WordPress.org

Changeset 12712


Ignore:
Timestamp:
07/05/2023 05:04:40 AM (19 months ago)
Author:
dd32
Message:

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

See [12711].
See #5055.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/trac-notifications
Files:
2 edited

Legend:

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

    r12711 r12712  
    214214     * @param string $username   The username.
    215215     * @param string $name       The preference.
    216      * @param string|null $value The value to set. If null, the preference will be deleted.
     216     * @param string|null $value The value to set.
    217217     */
    218     function set_user_pref( $username, $name, $value = null ) {
     218    function set_user_pref( $username, $name, $value = '' ) {
    219219        // The trac column for username is `sid`. All our users are authenticated.
    220220        // The session_attribute table has an index: UNIQUE (sid,authenticated,name)
    221221
    222         if ( ! is_null( $value ) ) {
     222        $result = $this->db->insert( 'session_attribute', array(
     223            'sid'           => $username,
     224            'authenticated' => 1,
     225            'name'          => $name,
     226            'value'         => $value
     227        ) );
     228
     229        if ( ! $result ) {
    223230            $result = $this->db->update(
    224231                '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', [
     232                array(
     233                    'value'         => $value
     234                ), array(
    249235                    'sid'           => $username,
    250236                    'authenticated' => 1,
    251237                    '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             ] );
    263         }
     238                )
     239            );
     240        }
     241
     242        return $result;
     243    }
     244   
     245    /**
     246     * Delete a user preference.
     247     *
     248     * @param string $username The username.
     249     * @param string $name     The preference.
     250     * @return bool
     251     */
     252    function delete_user_pref( $username, $name ) {
     253        return $this->db->delete( 'session_attribute', array(
     254            'sid'           => $username,
     255            'authenticated' => 1,
     256            'name'          => $name
     257        ) );
    264258    }
    265259
     
    303297            'attachments',
    304298            'comments',
    305             'profile_data',
     299            'profile_data'
    306300        );
    307301    }
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/trac-notifications/trac-notifications-sqlite-driver.php

    r12711 r12712  
    5151    }
    5252
     53    /**
     54     * Delete row(s) from a table.
     55     *
     56     * @return bool If the query ran.
     57     */
    5358    public function delete( $table, $where ) {
    5459        $fields = 'AND ' . implode( ' = %s AND ', array_keys( $where ) ) . ' = %s';
     
    5762            array_values( $where )
    5863        );
    59         $this->db->query( $query );
     64        return (bool) $this->db->query( $query );
    6065    }
    6166
     67    /**
     68     * Insert a row into a table.
     69     *
     70     * @return bool If the query ran.
     71     */
    6272    public function insert( $table, $args ) {
    6373        $fields = "'" . implode( "', '", array_keys( $args ) ) . "'";
     
    7080    }
    7181
     82    /**
     83     * Update a row in a table.
     84     *
     85     * @return bool If the query executed and modified rows.
     86     */
    7287    public function update( $table, $data, $wheres ) {
    73         $values     = [];
    74         $sql_sets   = [];
    75         $sql_wheres = [];
     88        $values     = array();
     89        $sql_sets   = array();
     90        $sql_wheres = array();
    7691
    7792        foreach ( $data as $field => $value ) {
     
    8196        $sql_sets = implode( ', ', $sql_sets );
    8297
    83         foreach ( $wheres as $field => $where ) {
     98        foreach ( $wheres as $field => $value ) {
    8499            $sql_wheres[] = "$field = %s";
    85100            $values[]     = $value;
     
    87102        $sql_wheres = implode( ' AND ', $sql_wheres );
    88103
    89         if ( ! $values ) {
     104        if ( ! $values || ! $sql_wheres ) {
    90105            return false;
    91106        }
     
    96111        );
    97112
    98         return (bool) $this->db->query( $query );
     113        $result = $this->db->query( $query );
     114
     115        return $result && $result->rowCount() > 0;
    99116    }
    100117}
Note: See TracChangeset for help on using the changeset viewer.