Skip to:
Content

bbPress.org


Ignore:
Timestamp:
10/10/2017 08:40:08 PM (4 years ago)
Author:
johnjamesjacoby
Message:

Engagements: abstract meta strategy into an overload'able class.

This change introduces a class and wrapper function to allow the meta strategy of the new user engagements API to be hot-swapped. This might be helpful on large installations where a dedicated database table makes more sense, or for integrations where features like "Favorites" or "Subscriptions" might already be delegated to other third-party membership plugins. Now, the caller class can be filtered to one that includes custom methods.

See #3068.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/users/engagements.php

    r6619 r6723  
    2020 * @param int    $object_id The object id
    2121 * @param int    $user_id   The user id
    22  * @param string $meta_key  The relationship key
    23  * @param string $meta_type The relationship type (usually 'post')
     22 * @param string $rel_key   The relationship key
     23 * @param string $rel_type The relationship type (usually 'post')
    2424 * @param bool   $unique    Whether meta key should be unique to the object
    2525 *
    2626 * @return bool Returns true on success, false on failure
    2727 */
    28 function bbp_add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post', $unique = false ) {
    29     $retval = add_metadata( $meta_type, $object_id, $meta_key, $user_id, $unique );
    30 
    31     // Filter & return
    32     return (bool) apply_filters( 'bbp_add_user_to_object', (bool) $retval, $object_id, $user_id, $meta_key, $meta_type, $unique );
     28function bbp_add_user_to_object( $object_id = 0, $user_id = 0, $rel_key = '', $rel_type = 'post', $unique = false ) {
     29    $retval = bbp_user_engagements_interface()->add_user_to_object( $object_id, $user_id, $rel_key, $rel_type, $unique );
     30
     31    // Filter & return
     32    return (bool) apply_filters( 'bbp_add_user_to_object', $retval, $object_id, $user_id, $rel_key, $rel_type, $unique );
    3333}
    3434
     
    4040 * @param int    $object_id The object id
    4141 * @param int    $user_id   The user id
    42  * @param string $meta_key  The relationship key
    43  * @param string $meta_type The relationship type (usually 'post')
     42 * @param string $rel_key   The relationship key
     43 * @param string $rel_type The relationship type (usually 'post')
    4444 *
    4545 * @return bool Returns true on success, false on failure
    4646 */
    47 function bbp_remove_user_from_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
    48     $retval = delete_metadata( $meta_type, $object_id, $meta_key, $user_id, false );
    49 
    50     // Filter & return
    51     return (bool) apply_filters( 'bbp_remove_user_from_object', (bool) $retval, $object_id, $user_id, $meta_key, $meta_type );
     47function bbp_remove_user_from_object( $object_id = 0, $user_id = 0, $rel_key = '', $rel_type = 'post' ) {
     48    $retval = bbp_user_engagements_interface()->remove_user_from_object( $object_id, $user_id, $rel_key, $rel_type );
     49
     50    // Filter & return
     51    return (bool) apply_filters( 'bbp_remove_user_from_object', $retval, $object_id, $user_id, $rel_key, $rel_type );
    5252}
    5353
     
    5757 * @since 2.6.0 bbPress (r6109)
    5858 *
    59  * @param int    $user_id   The user id
    60  * @param string $meta_key  The relationship key
    61  * @param string $meta_type The relationship type (usually 'post')
     59 * @param int    $user_id  The user id
     60 * @param string $rel_key  The relationship key
     61 * @param string $rel_type The relationship type (usually 'post')
    6262 *
    6363 * @return bool Returns true on success, false on failure
    6464 */
    65 function bbp_remove_user_from_all_objects( $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
    66     $retval = delete_metadata( $meta_type, null, $meta_key, $user_id, true );
    67 
    68     // Filter & return
    69     return (bool) apply_filters( 'bbp_remove_user_from_all_objects', (bool) $retval, $user_id, $meta_key, $meta_type );
     65function bbp_remove_user_from_all_objects( $user_id = 0, $rel_key = '', $rel_type = 'post' ) {
     66    $retval = bbp_user_engagements_interface()->remove_user_from_all_objects( $user_id, $rel_key, $rel_type );
     67
     68    // Filter & return
     69    return (bool) apply_filters( 'bbp_remove_user_from_all_objects', $retval, $user_id, $rel_key, $rel_type );
    7070}
    7171
     
    7777 * @param int    $object_id The object id
    7878 * @param int    $user_id   The user id
    79  * @param string $meta_key  The relationship key
    80  * @param string $meta_type The relationship type (usually 'post')
     79 * @param string $rel_key   The relationship key
     80 * @param string $rel_type The relationship type (usually 'post')
    8181 *
    8282 * @return bool Returns true on success, false on failure
    8383 */
    84 function bbp_remove_object_from_all_users( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
    85     $retval = delete_metadata( $meta_type, $object_id, $meta_key, null, false );
    86 
    87     // Filter & return
    88     return (bool) apply_filters( 'bbp_remove_object_from_all_users', (bool) $retval, $object_id, $meta_key, $meta_type );
     84function bbp_remove_object_from_all_users( $object_id = 0, $rel_key = '', $rel_type = 'post' ) {
     85    $retval = bbp_user_engagements_interface()->remove_object_from_all_users( $object_id, $rel_key, $rel_type );
     86
     87    // Filter & return
     88    return (bool) apply_filters( 'bbp_remove_object_from_all_users', $retval, $object_id, $rel_key, $rel_type );
    8989}
    9090
     
    9494 * @since 2.6.0 bbPress (r6109)
    9595 *
    96  * @param string $meta_key  The relationship key
    97  * @param string $meta_type The relationship type (usually 'post')
     96 * @param string $rel_key  The relationship key
     97 * @param string $rel_type The relationship type (usually 'post')
    9898 *
    9999 * @return bool Returns true on success, false on failure
    100100 */
    101 function bbp_remove_all_users_from_all_objects( $meta_key = '', $meta_type = 'post' ) {
    102     $retval = delete_metadata( $meta_type, null, $meta_key, null, true );
    103 
    104     // Filter & return
    105     return (bool) apply_filters( 'bbp_remove_all_users_from_all_objects', (bool) $retval, $meta_key, $meta_type );
     101function bbp_remove_all_users_from_all_objects( $rel_key = '', $rel_type = 'post' ) {
     102    $retval = bbp_user_engagements_interface()->remove_all_users_from_all_objects( $rel_key, $rel_type );
     103
     104    // Filter & return
     105    return (bool) apply_filters( 'bbp_remove_all_users_from_all_objects', $retval, $rel_key, $rel_type );
    106106}
    107107
     
    112112 *
    113113 * @param int    $object_id The object id
    114  * @param string $meta_key  The key used to index this relationship
    115  * @param string $meta_type The type of meta to look in
     114 * @param string $rel_key   The key used to index this relationship
     115 * @param string $rel_type The type of meta to look in
    116116 *
    117117 * @return array Returns ids of users
    118118 */
    119 function bbp_get_users_for_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
    120     $meta   = get_metadata( $meta_type, $object_id, $meta_key, false );
    121     $retval = wp_parse_id_list( $meta );
    122 
    123     // Filter & return
    124     return (array) apply_filters( 'bbp_get_users_for_object', $retval, $object_id, $meta_key, $meta_type );
     119function bbp_get_users_for_object( $object_id = 0, $rel_key = '', $rel_type = 'post' ) {
     120    $retval = bbp_user_engagements_interface()->get_users_for_object( $object_id, $rel_key, $rel_type );
     121
     122    // Filter & return
     123    return (array) apply_filters( 'bbp_get_users_for_object', $retval, $object_id, $rel_key, $rel_type );
    125124}
    126125
     
    132131 * @param int    $object_id The object id
    133132 * @param int    $user_id   The user id
    134  * @param string $meta_key  The relationship key
    135  * @param string $meta_type The relationship type (usually 'post')
     133 * @param string $rel_key   The relationship key
     134 * @param string $rel_type The relationship type (usually 'post')
    136135 *
    137136 * @return bool Returns true if object has a user, false if not
    138137 */
    139 function bbp_is_object_of_user( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
    140     $user_ids = bbp_get_users_for_object( $object_id, $meta_key, $meta_type );
     138function bbp_is_object_of_user( $object_id = 0, $user_id = 0, $rel_key = '', $rel_type = 'post' ) {
     139    $user_ids = bbp_get_users_for_object( $object_id, $rel_key, $rel_type );
    141140    $retval   = is_numeric( array_search( $user_id, $user_ids, true ) );
    142141
    143142    // Filter & return
    144     return (bool) apply_filters( 'bbp_is_object_of_user', $retval, $object_id, $user_id, $meta_key, $meta_type );
     143    return (bool) apply_filters( 'bbp_is_object_of_user', $retval, $object_id, $user_id, $rel_key, $rel_type );
    145144}
    146145
     
    981980
    982981/**
     982 * These functions are no longer used in bbPress due to general performance
     983 * concerns on large installations. They are provided here for convenience and
     984 * backwards compatibility only.
     985 */
     986
     987/**
    983988 * Get a user's object IDs
    984989 *
     
    991996 *
    992997 * @param int    $user_id   The user id
    993  * @param string $meta_key  The relationship key
    994  * @param string $meta_type The relationship type (usually 'post')
     998 * @param string $rel_key   The relationship key
     999 * @param string $rel_type The relationship type (usually 'post')
    9951000 * @param array  $args      The arguments to override defaults
    9961001 *
     
    10041009        'user_id'     => 0,
    10051010        'object_type' => bbp_get_topic_post_type(),
    1006         'meta_key'    => '',
    1007         'meta_type'   => 'post',
     1011        'rel_key'     => '',
     1012        'rel_type'    => 'post',
    10081013        'filter'      => 'user_object_ids',
    10091014        'args'        => array()
     
    10121017    // Sanitize arguments
    10131018    $r['user_id']     = bbp_get_user_id( $r['user_id'] );
    1014     $r['meta_key']    = sanitize_key( $r['meta_key'] );
    1015     $r['meta_type']   = sanitize_key( $r['meta_type'] );
     1019    $r['rel_key']     = sanitize_key( $r['rel_key'] );
     1020    $r['rel_type']    = sanitize_key( $r['rel_type'] );
    10161021    $r['object_type'] = sanitize_key( $r['object_type'] );
    10171022    $r['filter']      = sanitize_key( $r['filter'] );
    10181023
    10191024    // Defaults
    1020     if ( 'post' === $r['meta_type'] ) {
     1025    if ( 'post' === $r['rel_type'] ) {
    10211026        $defaults = array(
    10221027            'fields'         => 'ids',
     
    10241029            'posts_per_page' => -1,
    10251030            'meta_query'     => array( array(
    1026                 'key'     => $r['meta_key'],
     1031                'key'     => $r['rel_key'],
    10271032                'value'   => $r['user_id'],
    10281033                'compare' => 'NUMERIC'
     
    10431048
    10441049    // Queries
    1045     if ( 'post' === $r['meta_type'] ) {
     1050    if ( 'post' === $r['rel_type'] ) {
    10461051        $query      = new WP_Query( $query_args );
    10471052        $object_ids = $query->posts;
     
    10641069    return bbp_get_user_object_ids( array(
    10651070        'user_id'     => $user_id,
    1066         'meta_key'    => '_bbp_moderator_id',
     1071        'rel_key'     => '_bbp_moderator_id',
    10671072        'object_type' => bbp_get_forum_post_type(),
    10681073        'filter'      => 'moderator_forum_ids'
     
    10811086function bbp_get_user_engaged_topic_ids( $user_id = 0 ) {
    10821087    return bbp_get_user_object_ids( array(
    1083         'user_id'  => $user_id,
    1084         'meta_key' => '_bbp_engagement',
    1085         'filter'   => 'user_engaged_topic_ids'
     1088        'user_id' => $user_id,
     1089        'rel_key' => '_bbp_engagement',
     1090        'filter'  => 'user_engaged_topic_ids'
    10861091    ) );
    10871092}
     
    10941099 * @param int $user_id Optional. User id
    10951100 *
    1096  * @return array Return array of favorited ids, or empty array
     1101 * @return array Return array of favorite topic ids, or empty array
    10971102 */
    10981103function bbp_get_user_favorites_topic_ids( $user_id = 0 ) {
    10991104    return bbp_get_user_object_ids( array(
    1100         'user_id'  => $user_id,
    1101         'meta_key' => '_bbp_favorite',
    1102         'filter'   => 'user_favorites_topic_ids'
     1105        'user_id' => $user_id,
     1106        'rel_key' => '_bbp_favorite',
     1107        'filter'  => 'user_favorites_topic_ids'
    11031108    ) );
    11041109}
     
    11111116 * @param int $user_id Optional. User id
    11121117 *
    1113  * @return array Return array of subscribed ids, or empty array
     1118 * @return array Return array of subscribed forum ids, or empty array
    11141119 */
    11151120function bbp_get_user_subscribed_forum_ids( $user_id = 0 ) {
    11161121    return bbp_get_user_object_ids( array(
    11171122        'user_id'     => $user_id,
    1118         'meta_key'    => '_bbp_subscription',
     1123        'rel_key'     => '_bbp_subscription',
    11191124        'object_type' => bbp_get_forum_post_type(),
    11201125        'filter'      => 'user_subscribed_forum_ids'
     
    11291134 * @param int $user_id Optional. User id
    11301135 *
    1131  * @return array Return array of subscribed ids, or empty array
     1136 * @return array Return array of subscribed topic ids, or empty array
    11321137 */
    11331138function bbp_get_user_subscribed_topic_ids( $user_id = 0 ) {
    11341139    return bbp_get_user_object_ids( array(
    1135         'user_id'  => $user_id,
    1136         'meta_key' => '_bbp_subscription',
    1137         'filter'   => 'user_subscribed_topic_ids'
     1140        'user_id' => $user_id,
     1141        'rel_key' => '_bbp_subscription',
     1142        'filter'  => 'user_subscribed_topic_ids'
    11381143    ) );
    11391144}
Note: See TracChangeset for help on using the changeset viewer.