Skip to:
Content

bbPress.org


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.

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.