Skip to:
Content

bbPress.org

Ticket #2959: 2959.diff

File 2959.diff, 8.1 KB (added by jmdodd, 5 years ago)
  • src/includes/users/functions.php

     
    169169        return apply_filters( 'bbp_current_author_ua', $retval );
    170170}
    171171
     172/** User Taxonomy Terms *******************************************************/
     173
     174/**
     175 * Check if the user term is set on an object
     176 *
     177 * @since 2.7 bbPress ()
     178 *
     179 * @param int $object_id The object id
     180 * @param int $user_id The user id
     181 * @param string $taxonomy The object user taxonomy
     182 * @uses get_post_meta() To check if the user term is set on the object
     183 * @uses apply_filters() Calls 'bbp_object_has_user_term' with the object id,
     184 *                        user id, and taxonomy
     185 * @return bool Returns true if the user term is set on the object for the
     186 *               taxonomy, otherwise false
     187 */
     188function bbp_object_has_user_term( $object_id = 0, $user_id = 0, $taxonomy = '' ) {
     189        if ( empty( $object_id ) || empty( $user_id ) || empty( $taxonomy ) ) {
     190                return;
     191        }
     192
     193        $user_ids = get_post_meta( $object_id, $taxonomy, false );
     194        $retval = (bool) in_array( $user_id, $user_ids );
     195        return apply_filters( 'bbp_object_has_user_term', $retval, $object_id, $user_id, $taxonomy );
     196}
     197
     198/**
     199 * Set a user term on an object
     200 *
     201 * @since 2.7 bbPress ()
     202 *
     203 * @param int $object_id The object id
     204 * @param int $user_id The user id
     205 * @param string $taxonomy The user term taxonomy
     206 * @uses bbp_object_has_user_term() To check if the term has already been set
     207 * @uses add_post_meta() To set the term on the object
     208 * @uses apply_filters() Calls 'bbp_add_object_user_term' with the object id, user
     209 *                        id, and taxonomy
     210 * @return bool Returns true if the user taxonomy term is added to the object,
     211 *               otherwise false
     212 */
     213function bbp_add_object_user_term( $object_id = 0, $user_id = 0, $taxonomy = '' ) {
     214        if ( empty( $object_id ) || empty( $user_id ) || empty( $taxonomy ) ) {
     215                return;
     216        }
     217
     218        if ( bbp_object_has_user_term( $object_id, $user_id, $taxonomy ) ) {
     219                $retval = false;
     220        } else {
     221                $retval = (bool) add_post_meta( $object_id, $taxonomy, $user_id, false );
     222        }
     223        return apply_filters( 'bbp_add_object_user_term', $retval, $object_id, $user_id, $taxonomy );
     224}
     225
     226/**
     227 * Remove a user term from an object
     228 *
     229 * @since 2.7 bbPress ()
     230 *
     231 * @param int $object_id The post id
     232 * @param int $user_id The user id
     233 * @param string $taxonomy The user term taxonomy
     234 * @uses bbp_object_has_user_term() To check if the term is set
     235 * @uses delete_post_meta() To remove the term from the object
     236 * @uses apply_filters() Calls 'bbp_remove_object_user_term' with the object
     237 *                        id, user id, and taxonomy
     238 * @return bool Returns true is the user taxonomy term is removed from the object,
     239 *               otherwise false
     240 */
     241function bbp_remove_object_user_term( $object_id = 0, $user_id = 0, $taxonomy = '' ) {
     242        if ( empty( $object_id ) || empty( $user_id ) || empty( $taxonomy ) ) {
     243                return;
     244        }
     245
     246        if ( ! bbp_object_has_user_term( $object_id, $user_id, $taxonomy ) ) {
     247                $retval = false;
     248        } else {
     249                $retval = delete_post_meta( $object_id, $taxonomy, $user_id );
     250        }
     251        return apply_filters( 'bbp_remove_object_user_term', $retval, $object_id, $user_id, $taxonomy );
     252}
     253
     254/**
     255 * Get objects for a user taxonomy term
     256 *
     257 * @since 2.7 bbPress ()
     258 *
     259 * @param int $user_id The user id
     260 * @param string $taxonomy The user term taxonomy
     261 * @param string $object_type Optional. The object type
     262 * @uses post_type_exists() To validate the object type
     263 * @uses get_posts() To get the objects with a meta query
     264 * @uses apply_filters() Calls 'bbp_get_objects_by_user_term' with the objects,
     265 *                        user_id, taxonomy, and object type
     266 * @return array Returns the objects with the user taxonomy term
     267 */
     268function bbp_get_objects_by_user_term( $user_id = 0, $taxonomy = '', $object_type = null ) {
     269        if ( empty( $user_id ) || empty( $taxonomy ) || $object_type && ! post_type_exists( $object_type ) ) {
     270                return;
     271        }
     272
     273        $args = array(
     274                'meta_key'    => $taxonomy,
     275                'meta_type'   => 'NUMERIC',
     276                'meta_value'  => $user_id,
     277                'numberposts' => -1
     278        );
     279        if ( $object_type ) {
     280                $args['post_type'] = $object_type;
     281        }
     282        $objects = get_posts( $args );
     283        return apply_filters( 'bbp_get_objects_by_user_term', $objects, $user_id, $taxonomy, $object_type );
     284}
     285
     286/**
     287 * Get user taxonomy terms for an object
     288 *
     289 * @since 2.7 bbPress ()
     290 *
     291 * @param int $object_id The object id
     292 * @param string $taxonomy The user term taxonomy
     293 * @uses get_post_meta() To get the user taxonomy terms
     294 * @uses apply_filters() Calls 'bbp_get_user_terms_by_object' with the user
     295 *                        taxonomy terms, object id, and taxonomy
     296 * @return array Returns the user taxonomy terms of the object
     297 */
     298function bbp_get_user_terms_by_object( $object_id = 0, $taxonomy = '' ) {
     299        if ( empty( $object_id ) || empty( $taxonomy ) ) {
     300                return;
     301        }
     302
     303        $retval = get_post_meta( $object_id, $taxonomy, false );
     304        return apply_filters( 'bbp_get_user_terms_by_object', $retval, $object_id, $taxonomy );
     305}
     306
    172307/** Favorites *****************************************************************/
    173308
    174309/**
  • tests/phpunit/testcases/users/functions/functions.php

     
    8686        }
    8787
    8888        /**
     89         * @covers ::bbp_object_has_user_term
     90         */
     91        public function test_bbp_object_has_user_term() {
     92                $u = $this->factory->user->create();
     93                $t = $this->factory->topic->create();
     94
     95                $r = bbp_object_has_user_term( $t, $u, $this->user_taxonomy );
     96
     97                $this->assertFalse( $r );
     98
     99                // Add user term.
     100                add_post_meta( $t, $this->user_taxonomy, $u, false );
     101
     102                $r = bbp_object_has_user_term( $t, $u, $this->user_taxonomy );
     103
     104                $this->assertTrue( $r );
     105        }
     106
     107        /**
     108         * @covers ::bbp_add_object_user_term
     109         */
     110        public function test_bbp_add_object_user_term() {
     111                $u = $this->factory->user->create_many( 3 );
     112                $t = $this->factory->topic->create();
     113
     114                // Add object terms.
     115                foreach ( $u as $k => $v ) {
     116                        bbp_add_object_user_term( $t, $v, $this->user_taxonomy );
     117                }
     118
     119                $r = get_post_meta( $t, $this->user_taxonomy, false );
     120
     121                $this->assertCount( 3, $r );
     122        }
     123
     124        /**
     125         * @covers ::bbp_remove_object_user_term
     126         */
     127        public function test_bbp_remove_object_user_term() {
     128                $u = $this->factory->user->create();
     129                $t = $this->factory->topic->create();
     130
     131                // Add object terms.
     132                add_post_meta( $t, $this->user_taxonomy, $u, false );
     133
     134                $r = get_post_meta( $t, $this->user_taxonomy, false );
     135
     136                $this->assertCount( 1, $r );
     137
     138                $r = bbp_remove_object_user_term( $t, $u, $this->user_taxonomy );
     139
     140                $this->assertTrue( $r );
     141
     142                $r = get_post_meta( $t, $this->user_taxonomy, false );
     143
     144                $this->assertCount( 0, $r );
     145        }
     146
     147        /**
     148         * @covers ::bbp_get_objects_by_user_term
     149         */
     150        public function test_bbp_get_objects_by_user_term() {
     151                $u = $this->factory->user->create();
     152                $t = $this->factory->topic->create_many( 3 );
     153
     154                // Add object terms.
     155                foreach ( $t as $k => $v ) {
     156                        add_post_meta( $v, $this->user_taxonomy, $u, false );
     157                }
     158
     159                $r = bbp_get_objects_by_user_term( $u, $this->user_taxonomy, 'topic' );
     160
     161                $this->assertCount( 3, $r );
     162        }
     163
     164        /**
     165         * @covers ::bbp_get_user_terms_by_object
     166         */
     167        public function test_bbp_get_user_terms_by_object() {
     168                $u = $this->factory->user->create_many( 3 );
     169                $t = $this->factory->topic->create();
     170
     171                // Add object terms.
     172                foreach ( $u as $k => $v ) {
     173                        add_post_meta( $t, $this->user_taxonomy, $u, false );
     174                }
     175
     176                $r = bbp_get_user_terms_by_object( $t, $this->user_taxonomy );
     177
     178                $this->assertCount( 3, $r );
     179        }
     180
     181        /**
    89182         * @covers ::bbp_edit_user_handler
    90183         * @todo   Implement test_bbp_edit_user_handler().
    91184         */
     
    172265                        'This test has not been implemented yet.'
    173266                );
    174267        }
     268
     269        public function setUp() {
     270                parent::setUp();
     271
     272                $this->user_taxonomy = '_bbp_test_user_taxonomy';
     273        }
    175274}