Skip to:
Content

bbPress.org

Changeset 6876


Ignore:
Timestamp:
12/04/2018 06:31:09 PM (3 years ago)
Author:
johnjamesjacoby
Message:

Engagements: next pass at back-compat for pre-2.6 user strategy.

  • Renames BBP_User_Engagements_Back_Compat to BBP_User_Engagements_User so the approach is more clear.
  • Fill in get_query() method, left todo from r6844.
  • Move active strategy into a preloaded option, default to meta
  • Set active strategy to user on failed auto-upgrade to 2.6 to maintain backwards compatibility
  • Introduces sub-actions to assist with abstracting the engagement strategy setup

Fixes #3211.

Location:
trunk/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bbpress.php

    r6854 r6876  
    299299        $this->errors         = new WP_Error(); // Feedback
    300300
    301         /** Engagements *******************************************************/
    302 
    303         $this->engagements    = new BBP_User_Engagements_Meta(); // Meta strategy interface
    304 
    305301        /** Deprecated ********************************************************/
    306302
     
    407403            'setup_theme',              // Setup the default theme compat
    408404            'setup_current_user',       // Setup currently logged in user
     405            'setup_engagements',        // Setup user engagements strategy
    409406            'roles_init',               // User roles init
    410407            'register_meta',            // Register meta (forum|topic|reply|user)
     
    792789
    793790    /**
     791     * Setup the user engagements strategy
     792     *
     793     * @since 2.6.0 bbPress (r6875)
     794     */
     795    public function setup_engagements() {
     796
     797        // Default (always exists)
     798        $default    = "BBP_User_Engagements_Meta";
     799
     800        // Configured (Might not exist)
     801        $strategy   = ucwords( bbp_engagements_strategy() );
     802        $class_name = "BBP_User_Engagements_{$strategy}";
     803
     804        // Setup the engagements interface
     805        $this->engagements = class_exists( $class_name )
     806            ? new $class_name
     807            : new $default;
     808    }
     809
     810    /**
    794811     * Initialize forum-specific roles
    795812     *
  • trunk/src/includes/common/engagements.php

    r6844 r6876  
    2929
    3030/**
    31  * Meta strategy for interfacing with User Engagements
     31 * Base strategy class for interfacing with User Engagements, which other
     32 * classes will extend.
    3233 *
    3334 * @since 2.6.0 bbPress (r6722)
     
    525526
    526527/**
    527  * Backwards compatibility strategy for interfacing with User Engagements
     528 * User strategy for interfacing with User Engagements
    528529 *
    529530 * @since 2.6.0 bbPress (r6844)
    530531 */
    531 class BBP_User_Engagements_Back_Compat extends BBP_User_Engagements_Base {
     532class BBP_User_Engagements_User extends BBP_User_Engagements_Base {
    532533
    533534    /**
     
    538539     * @var string
    539540     */
    540     public $type = 'compat';
     541    public $type = 'user';
    541542
    542543    /**
     
    546547     *
    547548     * @param string $meta_key
     549     * @param int    $object_id
     550     * @param bool   $prefix
    548551     *
    549552     * @return string
    550553     */
    551     private function get_user_option_key( $meta_key = '', $object_id = 0 ) {
     554    private function get_user_option_key( $meta_key = '', $object_id = 0, $prefix = false ) {
    552555        switch ( $meta_key ) {
    553556
     
    578581        }
    579582
     583        // Maybe prefix the key (for use in raw database queries)
     584        if ( true === $prefix ) {
     585            $key = bbp_db()->get_blog_prefix() . $key;
     586        }
     587
    580588        // Return the old (pluralized) user option key
    581589        return $key;
     
    590598     * @return array
    591599     */
    592     private function format_results( $results = '' ) {
    593         return wp_parse_id_list( array_filter( $results ) );
     600    private function parse_comma_list( $results = '' ) {
     601        return array_filter( wp_parse_id_list( $results ) );
    594602    }
    595603
     
    610618        $retval     = false;
    611619        $option_key = $this->get_user_option_key( $meta_key, $object_id );
    612         $object_ids = $this->format_results( get_user_option( $option_key, $user_id ) );
     620        $object_ids = $this->parse_comma_list( get_user_option( $option_key, $user_id ) );
     621        $exists     = array_search( $object_id, $object_ids );
    613622
    614623        // Not already added, so add it
    615         if ( ! in_array( $object_id, $object_ids, true ) ) {
     624        if ( false === $exists ) {
    616625            $object_ids[] = $object_id;
    617             $object_ids   = implode( ',', $this->format_results( $object_ids ) );
     626            $object_ids   = implode( ',', $this->parse_comma_list( $object_ids ) );
    618627            $retval       = update_user_option( $user_id, $option_key, $object_ids );
    619628        }
     
    638647        $retval     = false;
    639648        $option_key = $this->get_user_option_key( $meta_key, $object_id );
    640         $object_ids = $this->format_results( get_user_option( $option_key, $user_id ) );
     649        $object_ids = $this->parse_comma_list( get_user_option( $option_key, $user_id ) );
     650        $exists     = array_search( $object_id, $object_ids );
    641651
    642652        // Exists, so remove it
    643         if ( in_array( $object_id, $object_ids, true ) ) {
    644             unset( $object_ids[ $object_id ] );
    645 
    646             $object_ids = implode( ',', $this->format_results( $object_ids ) );
    647             $retval     = update_user_option( $user_id, $option_key, $object_ids );
     653        if ( false !== $exists ) {
     654            unset( $object_ids[ $exists ] );
     655
     656            $object_ids = implode( ',', $this->parse_comma_list( $object_ids ) );
     657            $retval     = ! empty( $object_ids )
     658                ? update_user_option( $user_id, $option_key, $object_ids )
     659                : delete_user_option( $user_id, $option_key );
    648660        }
    649661
     
    719731
    720732        // Query for users
    721         $option_key = $this->get_user_option_key( $meta_key );
     733        $option_key = $this->get_user_option_key( $meta_key, 0, true );
    722734        $bbp_db     = bbp_db();
    723735        $user_ids   = $bbp_db->get_col( "SELECT user_id FROM {$bbp_db->usermeta} WHERE meta_key = '{$option_key}'" );
     
    759771     */
    760772    public function get_users_for_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
    761         $option_key = $this->get_user_option_key( $meta_key, $object_id );
     773        $option_key = $this->get_user_option_key( $meta_key, $object_id, true );
    762774        $bbp_db     = bbp_db();
    763775        $user_ids   = $bbp_db->get_col( "SELECT user_id FROM {$bbp_db->usermeta} WHERE meta_key = '{$option_key}' and FIND_IN_SET('{$object_id}', meta_value) > 0" );
     
    778790     */
    779791    public function get_query( $args = array(), $context_key = '', $meta_key = '', $meta_type = 'post' ) {
    780         // TODO
    781         return array();
     792        $user_id    = bbp_get_user_id( $args, true, true );
     793        $option_key = $this->get_user_option_key( $meta_key );
     794        $object_ids = $this->parse_comma_list( get_user_option( $option_key, $user_id ) );
     795
     796        // Maybe include these post IDs
     797        if ( ! empty( $object_ids ) ) {
     798            $args = array(
     799                'post__in' => $object_ids
     800            );
     801        }
     802
     803        // Parse arguments
     804        return bbp_parse_args( $args, array(), $context_key );
    782805    }
    783806}
  • trunk/src/includes/core/actions.php

    r6792 r6876  
    8383add_action( 'bbp_init', 'bbp_add_rewrite_rules', 30  );
    8484add_action( 'bbp_init', 'bbp_add_permastructs',  40  );
     85add_action( 'bbp_init', 'bbp_setup_engagements', 50  );
    8586add_action( 'bbp_init', 'bbp_ready',             999 );
    8687
  • trunk/src/includes/core/options.php

    r6870 r6876  
    6565            '_bbp_theme_package_id'       => 'default', // The ID for the current theme package
    6666            '_bbp_settings_integration'   => 'basic',   // How to integrate into wp-admin
     67            '_bbp_engagements_strategy'   => 'meta',    // How to interact with engagements
    6768
    6869            /** Per Page **********************************************************/
     
    690691 *
    691692 * @param bool $default Optional. Default value false
    692  * @return bool To deeply integrate settings, or not
     693 * @return string How to integrate settings
    693694 */
    694695function bbp_settings_integration( $default = 'basic' ) {
     
    704705    }
    705706
    706     // Fallback to 'none' if invalid
     707    // Fallback to 'basic' if invalid
    707708    if ( ! in_array( $integration, array( 'basic', 'deep', 'compact' ), true ) ) {
    708709        $integration = 'basic';
     
    711712    // Filter & return
    712713    return apply_filters( 'bbp_settings_integration', $integration, $default );
     714}
     715
     716/**
     717 * How to interact with engagements
     718 *
     719 * There are 3 possible strategies:
     720 * - 'meta' 2.6 and higher. Uses multiple postmeta keys.
     721 * - 'user' Pre-2.6. Uses comma-separated string of IDs in usermeta.
     722 * - 'term' Alternate. Uses taxonomy term relationships.
     723 *
     724 * @since 2.6.0 bbPress (r6875)
     725 *
     726 * @param bool $default Optional. Default value false
     727 * @return string How to interact with engagements
     728 */
     729function bbp_engagements_strategy( $default = 'meta' ) {
     730
     731    // Get the option value
     732    $integration = get_option( '_bbp_engagements_strategy', $default );
     733
     734    // Fallback to 'meta' if invalid
     735    if ( ! in_array( $integration, array( 'meta', 'user', 'term' ), true ) ) {
     736        $integration = 'meta';
     737    }
     738
     739    // Filter & return
     740    return apply_filters( 'bbp_engagements_strategy', $integration, $default );
    713741}
    714742
  • trunk/src/includes/core/sub-actions.php

    r6814 r6876  
    145145function bbp_setup_current_user() {
    146146    do_action( 'bbp_setup_current_user' );
     147}
     148
     149/**
     150 * Setup the user engagements strategy
     151 *
     152 * @since 2.6.0 bbPress (r6875)
     153 */
     154function bbp_setup_engagements() {
     155    do_action( 'bbp_setup_engagements' );
    147156}
    148157
  • trunk/src/includes/core/update.php

    r6823 r6876  
    353353            } else {
    354354                update_option( '_bbp_db_upgrade_skipped', $raw_db_version );
     355
     356                // Set strategy to pre-2.6 on large network
     357                update_option( '_bbp_engagements_strategy', 'user' );
    355358            }
    356359        }
     
    368371            } else {
    369372                update_option( '_bbp_db_upgrade_skipped', $raw_db_version );
     373
     374                // Set strategy to pre-2.6 on large network
     375                update_option( '_bbp_engagements_strategy', 'user' );
    370376            }
    371377        }
  • trunk/src/includes/users/engagements.php

    r6828 r6876  
    461461 * @param array $args Optional. Arguments to pass into bbp_has_topics()
    462462 *
    463  * @return bool True if user has favorites, otherwise false
     463 * @return array Array of topics if user has favorites, otherwise empty array
    464464 */
    465465function bbp_get_user_favorites( $args = array() ) {
    466466    $r     = bbp_get_user_object_query( $args, 'favorites', '_bbp_favorite' );
    467     $query = bbp_has_topics( $r );
     467    $query = ! empty( $r )
     468        ? bbp_has_topics( $r )
     469        : array();
    468470
    469471    // Filter & return
     
    679681 * @param array $args Optional. Arguments to pass into bbp_has_topics()
    680682 *
    681  * @return bool True if user has topic subscriptions, otherwise false
     683 * @return array Array of topics if user has topic subscriptions, otherwise empty array
    682684 */
    683685function bbp_get_user_topic_subscriptions( $args = array() ) {
    684686    $r     = bbp_get_user_object_query( $args, 'topic_subscriptions', '_bbp_subscription' );
    685     $query = bbp_has_topics( $r );
     687    $query = ! empty( $r )
     688        ? bbp_has_topics( $r )
     689        : array();
    686690
    687691    // Filter & return
     
    697701 * @param array $args Optional. Arguments to pass into bbp_has_forums()
    698702 *
    699  * @return bool True if user has forum subscriptions, otherwise false
     703 * @return array Array of forums if user has forum subscriptions, otherwise empty array
    700704 */
    701705function bbp_get_user_forum_subscriptions( $args = array() ) {
    702706    $r     = bbp_get_user_object_query( $args, 'forum_subscriptions', '_bbp_subscription' );
    703     $query = bbp_has_forums( $r );
     707    $query = ! empty( $r )
     708        ? bbp_has_forums( $r )
     709        : array();
    704710
    705711    // Filter & return
Note: See TracChangeset for help on using the changeset viewer.