Skip to:
Content

bbPress.org

Changeset 3911


Ignore:
Timestamp:
05/22/2012 08:00:55 AM (9 years ago)
Author:
johnjamesjacoby
Message:

User Options:

  • Introduce bbp-user-options.php to handle all user option related code.
  • Add $user_options array to main bbPress class to allow for easy, static, per-user option overrides.
  • Tweak action order for option and user_option overrides in bbp-core-hooks.php.
  • Replace all appropriate user_meta calls with user_option calls to automatically prefix keys with blog ID.
  • Update favorites, subscriptions, and topic/reply count option keys to be per-blog in multisite configurations.
  • Fixes #1826.
Location:
branches/plugin
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • branches/plugin/bbp-admin/bbp-tools.php

    r3890 r3911  
    385385        return array( 1, sprintf( $statement, $result ) );
    386386
     387    $key           = $wpdb->prefix . '_bbp_topic_count';
    387388    $insert_values = array();
    388389    foreach ( $insert_rows as $insert_row )
    389         $insert_values[] = "('{$insert_row->post_author}', '_bbp_topic_count', '{$insert_row->_count}')";
     390        $insert_values[] = "('{$insert_row->post_author}', '{$key}', '{$insert_row->_count}')";
    390391
    391392    if ( !count( $insert_values ) )
    392393        return array( 2, sprintf( $statement, $result ) );
    393394
    394     $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '_bbp_topic_count';";
     395    $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';";
    395396    if ( is_wp_error( $wpdb->query( $sql_delete ) ) )
    396397        return array( 3, sprintf( $statement, $result ) );
     
    431432        return array( 1, sprintf( $statement, $result ) );
    432433
     434    $key           = $wpdb->prefix . '_bbp_reply_count';
    433435    $insert_values = array();
    434436    foreach ( $insert_rows as $insert_row )
    435         $insert_values[] = "('{$insert_row->post_author}', '_bbp_reply_count', '{$insert_row->_count}')";
     437        $insert_values[] = "('{$insert_row->post_author}', '{$key}', '{$insert_row->_count}')";
    436438
    437439    if ( !count( $insert_values ) )
    438440        return array( 2, sprintf( $statement, $result ) );
    439441
    440     $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '_bbp_reply_count';";
     442    $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';";
    441443    if ( is_wp_error( $wpdb->query( $sql_delete ) ) )
    442444        return array( 3, sprintf( $statement, $result ) );
     
    471473    $statement = __( 'Removing trashed topics from user favorites… %s', 'bbpress' );
    472474    $result    = __( 'Failed!', 'bbpress' );
    473     $key       = bbp_get_favorites_key();
     475    $key       = $wpdb->prefix . '_bbp_favorites';
    474476    $users     = $wpdb->get_results( "SELECT `user_id`, `meta_value` AS `favorites` FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';" );
    475477
     
    531533    $statement = __( 'Removing trashed topics from user subscriptions… %s', 'bbpress' );
    532534    $result    = __( 'Failed!', 'bbpress' );
    533     $key       = bbp_get_subscriptions_key();
     535    $key       = $wpdb->prefix . '_bbp_subscriptions';
    534536    $users     = $wpdb->get_results( "SELECT `user_id`, `meta_value` AS `subscriptions` FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';" );
    535537
  • branches/plugin/bbp-includes/bbp-common-functions.php

    r3902 r3911  
    693693 * @uses get_option() To get the throttle time
    694694 * @uses get_transient() To get the last posted transient of the ip
    695  * @uses get_user_meta() To get the last posted meta of the user
     695 * @uses bbp_get_user_last_posted() To get the last posted time of the user
    696696 * @uses current_user_can() To check if the current user can throttle
    697697 * @return bool True if there is no flooding, false if there is
     
    715715    } elseif ( !empty( $author_id ) ) {
    716716        $author_id   = (int) $author_id;
    717         $last_posted = get_user_meta( $author_id, '_bbp_last_posted', true );
     717        $last_posted = bbp_get_user_last_posted( $author_id );
    718718
    719719        if ( isset( $last_posted ) && time() < $last_posted + $throttle_time && !current_user_can( 'throttle' ) ) {
  • branches/plugin/bbp-includes/bbp-core-actions.php

    r3830 r3911  
    5959 * Attach various loader actions to the bbp_loaded action.
    6060 * The load order helps to execute code at the correct time.
    61  *                                                        v---Load order
    62  */
    63 add_action( 'bbp_loaded', 'bbp_constants',                2  );
    64 add_action( 'bbp_loaded', 'bbp_boot_strap_globals',       4  );
    65 add_action( 'bbp_loaded', 'bbp_includes',                 6  );
    66 add_action( 'bbp_loaded', 'bbp_setup_globals',            8  );
    67 add_action( 'bbp_loaded', 'bbp_register_theme_directory', 10 );
    68 add_action( 'bbp_loaded', 'bbp_register_theme_packages',  12 );
     61 *                                                         v---Load order
     62 */
     63add_action( 'bbp_loaded', 'bbp_constants',                 2  );
     64add_action( 'bbp_loaded', 'bbp_boot_strap_globals',        4  );
     65add_action( 'bbp_loaded', 'bbp_includes',                  6  );
     66add_action( 'bbp_loaded', 'bbp_setup_globals',             8  );
     67add_action( 'bbp_loaded', 'bbp_setup_option_filters',      10 );
     68add_action( 'bbp_loaded', 'bbp_setup_user_option_filters', 12 );
     69add_action( 'bbp_loaded', 'bbp_register_theme_directory',  14 );
     70add_action( 'bbp_loaded', 'bbp_register_theme_packages',   16 );
    6971
    7072/**
     
    7678 */
    7779add_action( 'bbp_init', 'bbp_load_textdomain',         2   );
    78 add_action( 'bbp_init', 'bbp_setup_option_filters',    4   );
    7980add_action( 'bbp_init', 'bbp_register_post_types',     10  );
    8081add_action( 'bbp_init', 'bbp_register_post_statuses',  12  );
  • branches/plugin/bbp-includes/bbp-reply-functions.php

    r3840 r3911  
    593593 * @uses update_post_meta() To update the reply metas
    594594 * @uses set_transient() To update the flood check transient for the ip
    595  * @uses update_user_meta() To update the last posted meta for the user
     595 * @uses bbp_update_user_last_posted() To update the users last posted time
    596596 * @uses bbp_is_subscriptions_active() To check if the subscriptions feature is
    597597 *                                      activated or not
     
    651651    } else {
    652652        if ( empty( $is_edit ) && !current_user_can( 'throttle' ) ) {
    653             update_user_meta( $author_id, '_bbp_last_posted', time() );
     653            bbp_update_user_last_posted( $author_id );
    654654        }
    655655    }
  • branches/plugin/bbp-includes/bbp-topic-functions.php

    r3840 r3911  
    695695 * @uses update_post_meta() To update the topic metas
    696696 * @uses set_transient() To update the flood check transient for the ip
    697  * @uses update_user_meta() To update the last posted meta for the user
     697 * @uses bbp_update_user_last_posted() To update the users last posted time
    698698 * @uses bbp_is_subscriptions_active() To check if the subscriptions feature is
    699699 *                                      activated or not
     
    754754    } else {
    755755        if ( empty( $is_edit ) && !current_user_can( 'throttle' ) ) {
    756             update_user_meta( $author_id, '_bbp_last_posted', time() );
     756            bbp_update_user_last_posted( $author_id );
    757757        }
    758758    }
  • branches/plugin/bbp-includes/bbp-user-functions.php

    r3910 r3911  
    216216
    217217/**
    218  * Get the meta key for favorites, based on blog prefix
    219  *
    220  * @since bbPress (r3856)
    221  * @param int $blog_id Optional blog id to switch to
    222  * @global WPDB $wpdb
    223  * @return string
    224  */
    225 function bbp_get_favorites_key( $blog_id = 0 ) {
    226     global $wpdb;
    227 
    228     // If blog ID is passed, switch to that blog
    229     if ( !empty( $blog_id ) ) {
    230         switch_to_blog( $blog_id );
    231         $prefix = $wpdb->prefix;
    232         restore_current_blog();
    233 
    234     // Use current blog
    235     } else {
    236         $prefix = $wpdb->prefix;
    237     }
    238 
    239     return apply_filters( 'bbp_get_favorites_key', '_bbp_' . $prefix . 'favorites', $blog_id, $prefix );
    240 }
    241 
    242 /**
    243218 * Get the users who have made the topic favorite
    244219 *
     
    258233
    259234    // Get the users who have favorited the topic
    260     $key   = bbp_get_favorites_key();
     235    $key   = $wpdb->prefix . '_bbp_favorites';
    261236    $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$topic_id}', meta_value) > 0" );
    262237    $users = apply_filters( 'bbp_get_topic_favoriters', $users, $topic_id );
     
    305280 * @param int $user_id Optional. User id
    306281 * @uses bbp_get_user_id() To get the user id
    307  * @uses get_user_meta() To get the user favorites
     282 * @uses get_user_option() To get the user favorites
    308283 * @uses apply_filters() Calls 'bbp_get_user_favorites_topic_ids' with
    309284 *                        the favorites and user id
     
    315290        return false;
    316291
    317     $favorites = (string) get_user_meta( $user_id, bbp_get_favorites_key(), true );
     292    $favorites = (string) get_user_option( '_bbp_favorites', $user_id );
    318293    $favorites = (array) explode( ',', $favorites );
    319294    $favorites = array_filter( $favorites );
     
    379354 * @param int $topic_id Optional. Topic id
    380355 * @uses bbp_get_user_favorites_topic_ids() To get the user favorites
    381  * @uses update_user_meta() To update the user favorites
     356 * @uses update_user_option() To update the user favorites
    382357 * @uses do_action() Calls 'bbp_add_user_favorite' with the user id and topic id
    383358 * @return bool Always true
     
    396371        $favorites   = array_filter( $favorites );
    397372        $favorites   = (string) implode( ',', $favorites );
    398         update_user_meta( $user_id, bbp_get_favorites_key(), $favorites );
     373        update_user_option( $user_id, '_bbp_favorites', $favorites );
    399374    }
    400375
     
    412387 * @param int $topic_id Optional. Topic id
    413388 * @uses bbp_get_user_favorites_topic_ids() To get the user favorites
    414  * @uses update_user_meta() To update the user favorites
    415  * @uses delete_user_meta() To delete the user favorites meta
     389 * @uses update_user_option() To update the user favorites
     390 * @uses delete_user_option() To delete the user favorites meta
    416391 * @uses do_action() Calls 'bbp_remove_user_favorite' with the user & topic id
    417392 * @return bool True if the topic was removed from user's favorites, otherwise
     
    433408        if ( !empty( $favorites ) ) {
    434409            $favorites = implode( ',', $favorites );
    435             update_user_meta( $user_id, bbp_get_favorites_key(), $favorites );
     410            update_user_option( $user_id, '_bbp_favorites', $favorites );
    436411        } else {
    437             delete_user_meta( $user_id, bbp_get_favorites_key() );
     412            delete_user_option( $user_id, '_bbp_favorites' );
    438413        }
    439414    }
     
    546521
    547522/**
    548  * Get the meta key for subscriptions, based on blog prefix
    549  *
    550  * @since bbPress (r3856)
    551  * @global WPDB $wpdb
    552  * @return string
    553  */
    554 function bbp_get_subscriptions_key( $blog_id = 0 ) {
    555     global $wpdb;
    556 
    557     // If blog ID is passed, switch to that blog
    558     if ( !empty( $blog_id ) ) {
    559         switch_to_blog( $blog_id );
    560         $prefix = $wpdb->prefix;
    561         restore_current_blog();
    562 
    563     // Use current blog
    564     } else {
    565         $prefix = $wpdb->prefix;
    566     }
    567 
    568     return apply_filters( 'bbp_get_subscriptions_key', '_bbp_' . $prefix . 'subscriptions', $blog_id, $prefix );
    569 }
    570 
    571 /**
    572523 * Get the users who have subscribed to the topic
    573524 *
     
    584535    global $wpdb;
    585536
    586     $key   = bbp_get_subscriptions_key();
     537    $key   = $wpdb->prefix . '_bbp_subscriptions';
    587538    $users = wp_cache_get( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress' );
    588539    if ( empty( $users ) ) {
     
    635586 * @param int $user_id Optional. User id
    636587 * @uses bbp_get_user_id() To get the user id
    637  * @uses get_user_meta() To get the user's subscriptions
     588 * @uses get_user_option() To get the user's subscriptions
    638589 * @uses apply_filters() Calls 'bbp_get_user_subscribed_topic_ids' with
    639590 *                        the subscriptions and user id
     
    645596        return false;
    646597
    647     $subscriptions = (string) get_user_meta( $user_id, bbp_get_subscriptions_key(), true );
     598    $subscriptions = (string) get_user_option( '_bbp_subscriptions', $user_id );
    648599    $subscriptions = (array) explode( ',', $subscriptions );
    649600    $subscriptions = array_filter( $subscriptions );
     
    711662 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
    712663 * @uses bbp_get_topic() To get the topic
    713  * @uses update_user_meta() To update the user's subscriptions
     664 * @uses update_user_option() To update the user's subscriptions
    714665 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
    715666 * @return bool Always true
     
    729680        $subscriptions   = array_filter( $subscriptions );
    730681        $subscriptions   = (string) implode( ',', $subscriptions );
    731         update_user_meta( $user_id, bbp_get_subscriptions_key(), $subscriptions );
     682        update_user_option( $user_id, '_bbp_subscriptions', $subscriptions );
    732683
    733684        wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress' );
     
    747698 * @param int $topic_id Optional. Topic id
    748699 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
    749  * @uses update_user_meta() To update the user's subscriptions
    750  * @uses delete_user_meta() To delete the user's subscriptions meta
     700 * @uses update_user_option() To update the user's subscriptions
     701 * @uses delete_user_option() To delete the user's subscriptions meta
    751702 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
    752703 *                    topic id
     
    770721        if ( !empty( $subscriptions ) ) {
    771722            $subscriptions = implode( ',', $subscriptions );
    772             update_user_meta( $user_id, bbp_get_subscriptions_key(), $subscriptions );
     723            update_user_option( $user_id, '_bbp_subscriptions', $subscriptions );
    773724        } else {
    774             delete_user_meta( $user_id, bbp_get_subscriptions_key() );
     725            delete_user_option( $user_id, '_bbp_subscriptions' );
    775726        }
    776727
     
    994945
    995946        // stops users being added to current blog when they are edited
    996         if ( $delete_role ) {
     947        if ( true === $delete_role ) {
    997948            delete_user_meta( $user_id, $blog_prefix . 'capabilities' );
    998949        }
     
    10701021    wp_cache_set( 'bbp_total_users', $bbp_total_users, 'bbpress' );
    10711022
    1072     return (int) apply_filters( 'bbp_get_total_users', (int) $bbp_total_users );
     1023    return apply_filters( 'bbp_get_total_users', (int) $bbp_total_users );
    10731024}
    10741025
  • branches/plugin/bbp-includes/bbp-user-template.php

    r3860 r3911  
    15301530}
    15311531
    1532 /** Post Counts ***************************************************************/
    1533 
    1534 /**
    1535  * Output a users topic count
    1536  *
    1537  * @since bbPress (r3632)
    1538  *
    1539  * @param int $user_id
    1540  * @uses bbp_get_user_topic_count()
    1541  * @return string
    1542  */
    1543 function bbp_user_topic_count( $user_id = 0 ) {
    1544     echo bbp_get_user_topic_count( $user_id );
    1545 }
    1546     /**
    1547      * Return a users reply count
    1548      *
    1549      * @since bbPress (r3632)
    1550      *
    1551      * @param int $user_id
    1552      * @uses bbp_get_user_id()
    1553      * @uses get_user_meta()
    1554      * @uses apply_filters()
    1555      * @return string
    1556      */
    1557     function bbp_get_user_topic_count( $user_id = 0 ) {
    1558 
    1559         // Validate user id
    1560         $user_id = bbp_get_user_id( $user_id );
    1561         if ( empty( $user_id ) )
    1562             return false;
    1563 
    1564         $count = get_user_meta( $user_id, '_bbp_topic_count', true );
    1565 
    1566         return apply_filters( 'bbp_get_user_topic_count', (int) $count, $user_id );
    1567     }
    1568 
    1569 /**
    1570  * Output a users reply count
    1571  *
    1572  * @since bbPress (r3632)
    1573  *
    1574  * @param int $user_id
    1575  * @uses bbp_get_user_reply_count()
    1576  * @return string
    1577  */
    1578 function bbp_user_reply_count( $user_id = 0 ) {
    1579     echo bbp_get_user_reply_count( $user_id );
    1580 }
    1581     /**
    1582      * Return a users reply count
    1583      *
    1584      * @since bbPress (r3632)
    1585      *
    1586      * @param int $user_id
    1587      * @uses bbp_get_user_id()
    1588      * @uses get_user_meta()
    1589      * @uses apply_filters()
    1590      * @return string
    1591      */
    1592     function bbp_get_user_reply_count( $user_id = 0 ) {
    1593 
    1594         // Validate user id
    1595         $user_id = bbp_get_user_id( $user_id );
    1596         if ( empty( $user_id ) )
    1597             return false;
    1598 
    1599         $count = get_user_meta( $user_id, '_bbp_reply_count', true );
    1600 
    1601         return apply_filters( 'bbp_get_user_reply_count', (int) $count, $user_id );
    1602     }
    1603 
    1604 /**
    1605  * Output a users total post count
    1606  *
    1607  * @since bbPress (r3632)
    1608  *
    1609  * @param int $user_id
    1610  * @uses bbp_get_user_post_count()
    1611  * @return string
    1612  */
    1613 function bbp_user_post_count( $user_id = 0 ) {
    1614     echo bbp_get_user_post_count( $user_id );
    1615 }
    1616     /**
    1617      * Return a users total post count
    1618      *
    1619      * @since bbPress (r3632)
    1620      *
    1621      * @param int $user_id
    1622      * @uses bbp_get_user_id()
    1623      * @uses get_user_meta()
    1624      * @uses apply_filters()
    1625      * @return string
    1626      */
    1627     function bbp_get_user_post_count( $user_id = 0 ) {
    1628 
    1629         // Validate user id
    1630         $user_id = bbp_get_user_id( $user_id );
    1631         if ( empty( $user_id ) )
    1632             return false;
    1633 
    1634         $topics  = get_user_meta( $user_id, '_bbp_topic_count', true );
    1635         $replies = get_user_meta( $user_id, '_bbp_reply_count', true );
    1636         $count   = (int) $topics + (int) $replies;
    1637 
    1638         return apply_filters( 'bbp_get_user_post_count', (int) $count, $user_id );
    1639     }
    1640 
    16411532?>
  • branches/plugin/bbpress.php

    r3907 r3911  
    262262
    263263    /**
    264      * @var array Optional Overloads default options retrieved from get_option()
     264     * Overloads default options retrieved from get_option()
     265     *
     266     * @var array Optional array( $key => $value );
    265267     */
    266268    public $options = array();
    267269
    268     /** Function Overload *****************************************************/
    269 
    270     /**
    271      * @var array Optional Overloads WordPress functions with new functions.
    272      */
    273     public $functions = array();
     270    /**
     271     * Overloads default user options retrieved from get_user_option()
     272     *
     273     * @var array Optional array( $user_id => array( $key => $value ) );
     274     */
     275    public $user_options = array();
    274276
    275277    /** Singleton *************************************************************/
     
    462464        require( $this->plugin_dir . 'bbp-includes/bbp-user-functions.php'   ); // User functions
    463465        require( $this->plugin_dir . 'bbp-includes/bbp-user-template.php'    ); // User template tags
     466        require( $this->plugin_dir . 'bbp-includes/bbp-user-options.php'     ); // User options
    464467
    465468        /** Admin *************************************************************/
Note: See TracChangeset for help on using the changeset viewer.