Skip to:
Content

bbPress.org


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.