Skip to:
Content

bbPress.org

Changeset 6739


Ignore:
Timestamp:
11/17/2017 08:12:24 PM (7 years ago)
Author:
johnjamesjacoby
Message:

Engagements: include BBP_User_Engagements_Term class, as term storage example.

This change includes an alternative storage mechanism for engagements. Meta is used by default, but using Taxonomies and Terms for storage instead may be more appealing to certain installation types.

A few related functions were updated to use newer abstractions. See #459.

Location:
trunk/src/includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/core/abstraction.php

    r6734 r6739  
    385385 * @since 2.6.0 bbPress (r6722)
    386386 */
    387 class BBP_User_Engagements_Meta {
     387class BBP_User_Engagements_Base {
     388
     389    /**
     390     *
     391     * @since 2.6.0 bbPress (r6737)
     392     *
     393     * @var string
     394     */
     395    public $type = '';
     396
     397    /**
     398     * Add a user id to an object
     399     *
     400     * @since 2.6.0 bbPress (r6722)
     401     *
     402     * @param int    $object_id The object id
     403     * @param int    $user_id   The user id
     404     * @param string $meta_key  The relationship key
     405     * @param string $meta_type The relationship type (usually 'post')
     406     * @param bool   $unique    Whether meta key should be unique to the object
     407     *
     408     * @return bool Returns true on success, false on failure
     409     */
     410    public function add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post', $unique = false ) {
     411
     412    }
     413
     414    /**
     415     * Remove a user id from an object
     416     *
     417     * @since 2.6.0 bbPress (r6722)
     418     *
     419     * @param int    $object_id The object id
     420     * @param int    $user_id   The user id
     421     * @param string $meta_key  The relationship key
     422     * @param string $meta_type The relationship type (usually 'post')
     423     *
     424     * @return bool Returns true on success, false on failure
     425     */
     426    public function remove_user_from_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
     427
     428    }
     429
     430    /**
     431     * Remove a user id from all objects
     432     *
     433     * @since 2.6.0 bbPress (r6722)
     434     *
     435     * @param int    $user_id   The user id
     436     * @param string $meta_key  The relationship key
     437     * @param string $meta_type The relationship type (usually 'post')
     438     *
     439     * @return bool Returns true on success, false on failure
     440     */
     441    public function remove_user_from_all_objects( $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
     442
     443    }
     444
     445    /**
     446     * Remove an object from all users
     447     *
     448     * @since 2.6.0 bbPress (r6722)
     449     *
     450     * @param int    $object_id The object id
     451     * @param int    $user_id   The user id
     452     * @param string $meta_key  The relationship key
     453     * @param string $meta_type The relationship type (usually 'post')
     454     *
     455     * @return bool Returns true on success, false on failure
     456     */
     457    public function remove_object_from_all_users( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
     458
     459    }
     460
     461    /**
     462     * Remove all users from all objects
     463     *
     464     * @since 2.6.0 bbPress (r6722)
     465     *
     466     * @param string $meta_key  The relationship key
     467     * @param string $meta_type The relationship type (usually 'post')
     468     *
     469     * @return bool Returns true on success, false on failure
     470     */
     471    public function remove_all_users_from_all_objects( $meta_key = '', $meta_type = 'post' ) {
     472
     473    }
     474
     475    /**
     476     * Get users of an object
     477     *
     478     * @since 2.6.0 bbPress (r6722)
     479     *
     480     * @param int    $object_id The object id
     481     * @param string $meta_key  The key used to index this relationship
     482     * @param string $meta_type The type of meta to look in
     483     *
     484     * @return array Returns ids of users
     485     */
     486    public function get_users_for_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
     487
     488    }
     489
     490    /**
     491     * Get the part of the query responsible for JOINing objects to relationships.
     492     *
     493     * @since 2.6.0 bbPress (r6737)
     494     *
     495     * @param array  $args
     496     * @param string $meta_key
     497     * @param string $meta_type
     498     *
     499     * @return array
     500     */
     501    public function get_query( $args = array(), $context_key = '', $meta_key = '', $meta_type = 'post' ) {
     502
     503    }
     504}
     505
     506/**
     507 * Meta strategy for interfacing with User Engagements
     508 *
     509 * @since 2.6.0 bbPress (r6722)
     510 */
     511class BBP_User_Engagements_Meta extends BBP_User_Engagements_Base {
     512
     513    /**
     514     *
     515     * @since 2.6.0 bbPress (r6737)
     516     *
     517     * @var string
     518     */
     519    public $type = 'meta';
    388520
    389521    /**
     
    479611        return wp_parse_id_list( get_metadata( $meta_type, $object_id, $meta_key, false ) );
    480612    }
    481 }
     613
     614    /**
     615     * Get the part of the query responsible for JOINing objects to relationships.
     616     *
     617     * @since 2.6.0 bbPress (r6737)
     618     *
     619     * @param array  $args
     620     * @param string $meta_key
     621     * @param string $meta_type
     622     *
     623     * @return array
     624     */
     625    public function get_query( $args = array(), $context_key = '', $meta_key = '', $meta_type = 'post' ) {
     626
     627        // Backwards compat for pre-2.6.0
     628        if ( is_numeric( $args ) ) {
     629            $args = array(
     630                'meta_query' => array( array(
     631                    'key'     => $meta_key,
     632                    'value'   => bbp_get_user_id( $args, false, false ),
     633                    'compare' => 'NUMERIC'
     634                ) )
     635            );
     636        }
     637
     638        // Default arguments
     639        $defaults = array(
     640            'meta_query' => array( array(
     641                'key'     => $meta_key,
     642                'value'   => bbp_get_displayed_user_id(),
     643                'compare' => 'NUMERIC'
     644            ) )
     645        );
     646
     647        // Parse arguments
     648        return bbp_parse_args( $args, $defaults, $context_key );
     649    }
     650}
     651
     652/**
     653 * Term strategy for interfacing with User Engagements
     654 *
     655 * @since 2.6.0 bbPress (r6737)
     656 */
     657class BBP_User_Engagements_Term extends BBP_User_Engagements_Base {
     658
     659    /**
     660     * Term type
     661     *
     662     * @since 2.6.0 bbPress (r6737)
     663     *
     664     * @var string
     665     */
     666    public $type = 'term';
     667
     668    /**
     669     * Register an engagement taxonomy just-in-time
     670     *
     671     * @since 2.6.0 bbPress (r6737)
     672     *
     673     * @param string $tax_key
     674     * @param string $object_type
     675     */
     676    private function jit_taxonomy( $tax_key = '', $object_type = 'user' ) {
     677
     678        // Bail if taxonomy already exists
     679        if ( taxonomy_exists( $tax_key ) ) {
     680            return;
     681        }
     682
     683        // Register the taxonomy
     684        register_taxonomy( $tax_key, 'bbp_' . $object_type, array(
     685            'labels'                => array(),
     686            'description'           => '',
     687            'public'                => false,
     688            'publicly_queryable'    => false,
     689            'hierarchical'          => false,
     690            'show_ui'               => false,
     691            'show_in_menu'          => false,
     692            'show_in_nav_menus'     => false,
     693            'show_tagcloud'         => false,
     694            'show_in_quick_edit'    => false,
     695            'show_admin_column'     => false,
     696            'meta_box_cb'           => false,
     697            'capabilities'          => array(),
     698            'rewrite'               => false,
     699            'query_var'             => '',
     700            'update_count_callback' => '',
     701            'show_in_rest'          => false,
     702            'rest_base'             => false,
     703            'rest_controller_class' => false,
     704            '_builtin'              => false
     705        ) );
     706    }
     707
     708    /**
     709     * Add a user id to an object
     710     *
     711     * @since 2.6.0 bbPress (r6737)
     712     *
     713     * @param int    $object_id The object id
     714     * @param int    $user_id   The user id
     715     * @param string $meta_key  The relationship key
     716     * @param string $meta_type The relationship type (usually 'post')
     717     * @param bool   $unique    Whether meta key should be unique to the object
     718     *
     719     * @return bool Returns true on success, false on failure
     720     */
     721    public function add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post', $unique = false ) {
     722        $user_key = "{$meta_key}_user_id_{$user_id}";
     723        $tax_key  = "{$meta_key}_{$meta_type}";
     724        $this->jit_taxonomy( $tax_key );
     725
     726        return wp_add_object_terms( $object_id, $user_key, $tax_key );
     727    }
     728
     729    /**
     730     * Remove a user id from an object
     731     *
     732     * @since 2.6.0 bbPress (r6737)
     733     *
     734     * @param int    $object_id The object id
     735     * @param int    $user_id   The user id
     736     * @param string $meta_key  The relationship key
     737     * @param string $meta_type The relationship type (usually 'post')
     738     *
     739     * @return bool Returns true on success, false on failure
     740     */
     741    public function remove_user_from_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
     742        $user_key = "{$meta_key}_user_id_{$user_id}";
     743        $tax_key  = "{$meta_key}_{$meta_type}";
     744        $this->jit_taxonomy( $tax_key );
     745
     746        return wp_remove_object_terms( $object_id, $user_key, $tax_key );
     747    }
     748
     749    /**
     750     * Remove a user id from all objects
     751     *
     752     * @since 2.6.0 bbPress (r6737)
     753     *
     754     * @param int    $user_id   The user id
     755     * @param string $meta_key  The relationship key
     756     * @param string $meta_type The relationship type (usually 'post')
     757     *
     758     * @return bool Returns true on success, false on failure
     759     */
     760    public function remove_user_from_all_objects( $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
     761        $user_key = "{$meta_key}_user_id_{$user_id}";
     762        $tax_key  = "{$meta_key}_{$meta_type}";
     763        $this->jit_taxonomy( $tax_key );
     764        $term     = get_term_by( 'slug', $user_key, $tax_key );
     765
     766        return wp_delete_term( $term->term_id, $tax_key );
     767    }
     768
     769    /**
     770     * Remove an object from all users
     771     *
     772     * @since 2.6.0 bbPress (r6737)
     773     *
     774     * @param int    $object_id The object id
     775     * @param int    $user_id   The user id
     776     * @param string $meta_key  The relationship key
     777     * @param string $meta_type The relationship type (usually 'post')
     778     *
     779     * @return bool Returns true on success, false on failure
     780     */
     781    public function remove_object_from_all_users( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
     782        return wp_delete_object_term_relationships( $object_id, get_object_taxonomies( 'bbp_user' ) );
     783    }
     784
     785    /**
     786     * Remove all users from all objects
     787     *
     788     * @since 2.6.0 bbPress (r6737)
     789     *
     790     * @param string $meta_key  The relationship key
     791     * @param string $meta_type The relationship type (usually 'post')
     792     *
     793     * @return bool Returns true on success, false on failure
     794     */
     795    public function remove_all_users_from_all_objects( $meta_key = '', $meta_type = 'post' ) {
     796        // TODO
     797    }
     798
     799    /**
     800     * Get users of an object
     801     *
     802     * @since 2.6.0 bbPress (r6737)
     803     *
     804     * @param int    $object_id The object id
     805     * @param string $meta_key  The key used to index this relationship
     806     * @param string $meta_type The type of meta to look in
     807     *
     808     * @return array Returns ids of users
     809     */
     810    public function get_users_for_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
     811        $user_key = "{$meta_key}_user_id_";
     812        $tax_key  = "{$meta_key}_{$meta_type}";
     813        $this->jit_taxonomy( $tax_key );
     814
     815        // Get terms
     816        $terms = get_terms( array(
     817            'object_ids' => $object_id,
     818            'taxonomy'   => $tax_key
     819        ) );
     820
     821        // Slug part to replace
     822        $user_ids = array();
     823
     824        // Loop through terms and get the user ID
     825        foreach ( $terms as $term ) {
     826            $user_ids[] = str_replace( $user_key, '', $term->slug );
     827        }
     828
     829        // Parse & return
     830        return wp_parse_id_list( $user_ids );
     831    }
     832
     833    /**
     834     * Get the part of the query responsible for JOINing objects to relationships.
     835     *
     836     * @since 2.6.0 bbPress (r6737)
     837     *
     838     * @param array  $args
     839     * @param string $meta_key
     840     * @param string $meta_type
     841     *
     842     * @return array
     843     */
     844    public function get_query( $args = array(), $context_key = '', $meta_key = '', $meta_type = 'post' ) {
     845        $tax_key  = "{$meta_key}_{$meta_type}";
     846        $user_key = "{$meta_key}_user_id_";
     847
     848        // Make sure the taxonomy is registered
     849        $this->jit_taxonomy( $tax_key );
     850
     851        // Backwards compat for pre-2.6.0
     852        if ( is_numeric( $args ) ) {
     853            $args = array(
     854                'tax_query' => array( array(
     855                    'taxonomy' => $tax_key,
     856                    'terms'    => $user_key . bbp_get_user_id( $args, false, false ),
     857                    'field'    => 'slug'
     858                ) )
     859            );
     860        }
     861
     862        // Default arguments
     863        $defaults = array(
     864            'tax_query' => array( array(
     865                'taxonomy' => $tax_key,
     866                'terms'    => $user_key . bbp_get_displayed_user_id(),
     867                'field'    => 'slug'
     868            ) )
     869        );
     870
     871        // Parse arguments
     872        return bbp_parse_args( $args, $defaults, $context_key );
     873    }
     874}
  • trunk/src/includes/users/engagements.php

    r6723 r6739  
    142142    // Filter & return
    143143    return (bool) apply_filters( 'bbp_is_object_of_user', $retval, $object_id, $user_id, $rel_key, $rel_type );
     144}
     145
     146/**
     147 * Get the query part responsible for JOINing objects to user IDs
     148 *
     149 * @since 2.6.0 bbPress (r6747)
     150 *
     151 * @param array  $args
     152 * @param string $context
     153 * @param string $meta_key
     154 * @param string $meta_type
     155 *
     156 * @return array
     157 */
     158function bbp_get_user_object_query( $args = array(), $context = '', $meta_key = '', $meta_type = 'post' ) {
     159    $retval = bbp_user_engagements_interface()->get_query( $args, "get_user_{$context}", $meta_key, $meta_type );
     160
     161    // Filter & return
     162    return (array) apply_filters( 'bbp_get_user_object_query', $retval, $args, $context, $meta_key, $meta_type );
    144163}
    145164
     
    215234 */
    216235function bbp_get_user_engagements( $args = array() ) {
    217 
    218     // Backwards compat for pre-2.6.0
    219     if ( is_numeric( $args ) ) {
    220         $args = array(
    221             'meta_query' => array( array(
    222                 'key'     => '_bbp_engagement',
    223                 'value'   => bbp_get_user_id( $args, false, false ),
    224                 'compare' => 'NUMERIC'
    225             ) )
    226         );
    227     }
    228 
    229     // Default arguments
    230     $defaults = array(
    231         'meta_query' => array( array(
    232             'key'     => '_bbp_engagement',
    233             'value'   => bbp_get_displayed_user_id(),
    234             'compare' => 'NUMERIC'
    235         ) )
    236     );
    237 
    238     // Parse arguments
    239     $r = bbp_parse_args( $args, $defaults, 'get_user_engagements' );
    240 
    241     // Get the topics
    242     $query   = bbp_has_topics( $r );
    243     $user_id = isset( $r['meta_query'][0]['value'] )
    244         ? $r['meta_query'][0]['value']
    245         : 0;
    246 
    247     // Filter & return
    248     return apply_filters( 'bbp_get_user_engagements', $query, $user_id, $r, $args );
     236    $r     = bbp_get_user_object_query( $args, 'engagements', '_bbp_engagement' );
     237    $query = bbp_has_topics( $r );
     238
     239    // Filter & return
     240    return apply_filters( 'bbp_get_user_engagements', $query, 0, $r, $args );
    249241}
    250242
     
    465457 */
    466458function bbp_get_user_favorites( $args = array() ) {
    467 
    468     // Backwards compat for pre-2.6.0
    469     if ( is_numeric( $args ) ) {
    470         $args = array(
    471             'meta_query' => array( array(
    472                 'key'     => '_bbp_favorite',
    473                 'value'   => bbp_get_user_id( $args, false, false ),
    474                 'compare' => 'NUMERIC'
    475             ) )
    476         );
    477     }
    478 
    479     // Default arguments
    480     $defaults = array(
    481         'meta_query' => array( array(
    482             'key'     => '_bbp_favorite',
    483             'value'   => bbp_get_displayed_user_id(),
    484             'compare' => 'NUMERIC'
    485         ) )
    486     );
    487 
    488     // Parse arguments
    489     $r = bbp_parse_args( $args, $defaults, 'get_user_favorites' );
    490 
    491     // Get the topics
    492     $query   = bbp_has_topics( $r );
    493         $user_id = isset( $r['meta_query'][0]['value'] )
    494         ? $r['meta_query'][0]['value']
    495         : 0;
    496 
    497     // Filter & return
    498     return apply_filters( 'bbp_get_user_favorites', $query, $user_id, $r, $args );
     459    $r     = bbp_get_user_object_query( $args, 'favorites', '_bbp_favorite' );
     460    $query = bbp_has_topics( $r );
     461
     462    // Filter & return
     463    return apply_filters( 'bbp_get_user_favorites', $query, 0, $r, $args );
    499464}
    500465
     
    605570
    606571    // Bail if no topic ID is passed
    607     if ( empty( $_GET['topic_id'] ) ) {
     572    if ( empty( $_GET['object_id'] ) ) {
    608573        return $success;
    609574    }
     
    621586
    622587    // What action is taking place?
    623     $topic_id = bbp_get_topic_id( $_GET['topic_id'] );
     588    $topic_id = bbp_get_topic_id( $_GET['object_id'] );
    624589    $user_id  = bbp_get_user_id( 0, true, true );
    625590
     
    710675 */
    711676function bbp_get_user_topic_subscriptions( $args = array() ) {
    712 
    713     // Backwards compat for pre-2.6.0
    714     if ( is_numeric( $args ) ) {
    715         $args = array(
    716             'meta_query' => array( array(
    717                 'key'     => '_bbp_subscription',
    718                 'value'   => bbp_get_user_id( $args, false, false ),
    719                 'compare' => 'NUMERIC'
    720             ) )
    721         );
    722     }
    723 
    724     // Default arguments
    725     $defaults = array(
    726         'meta_query' => array( array(
    727             'key'     => '_bbp_subscription',
    728             'value'   => bbp_get_displayed_user_id(),
    729             'compare' => 'NUMERIC'
    730         ) )
    731     );
    732 
    733     // Parse arguments
    734     $r = bbp_parse_args( $args, $defaults, 'get_user_topic_subscriptions' );
    735 
    736     // Get the topics
    737     $query   = bbp_has_topics( $r );
    738     $user_id = isset( $r['meta_query'][0]['value'] )
    739         ? $r['meta_query'][0]['value']
    740         : 0;
    741 
    742     // Filter & return
    743     return apply_filters( 'bbp_get_user_topic_subscriptions', $query, $user_id, $r, $args );
     677    $r     = bbp_get_user_object_query( $args, 'topic_subscriptions', '_bbp_subscription' );
     678    $query = bbp_has_topics( $r );
     679
     680    // Filter & return
     681    return apply_filters( 'bbp_get_user_topic_subscriptions', $query, 0, $r, $args );
    744682}
    745683
     
    755693 */
    756694function bbp_get_user_forum_subscriptions( $args = array() ) {
    757 
    758     // Backwards compat for pre-2.6.0
    759     if ( is_numeric( $args ) ) {
    760         $args = array(
    761             'meta_query' => array( array(
    762                 'key'     => '_bbp_subscription',
    763                 'value'   => bbp_get_user_id( $args, false, false ),
    764                 'compare' => 'NUMERIC'
    765             ) )
    766         );
    767     }
    768 
    769     // Default arguments
    770     $defaults = array(
    771         'meta_query' => array( array(
    772             'key'     => '_bbp_subscription',
    773             'value'   => bbp_get_displayed_user_id(),
    774             'compare' => 'NUMERIC'
    775         ) )
    776     );
    777 
    778     // Parse arguments
    779     $r = bbp_parse_args( $args, $defaults, 'get_user_forum_subscriptions' );
    780 
    781     // Get the forums
    782     $query   = bbp_has_forums( $r );
    783     $user_id = isset( $r['meta_query'][0]['value'] )
    784         ? $r['meta_query'][0]['value']
    785         : 0;
    786 
    787     // Filter & return
    788     return apply_filters( 'bbp_get_user_forum_subscriptions', $query, $user_id, $r, $args );
     695    $r     = bbp_get_user_object_query( $args, 'forum_subscriptions', '_bbp_subscription' );
     696    $query = bbp_has_forums( $r );
     697
     698    // Filter & return
     699    return apply_filters( 'bbp_get_user_forum_subscriptions', $query, 0, $r, $args );
    789700}
    790701
Note: See TracChangeset for help on using the changeset viewer.