Skip to:
Content

BuddyPress.org

Changeset 12727


Ignore:
Timestamp:
09/21/2020 12:42:58 AM (6 years ago)
Author:
imath
Message:

BP Types: improve the BP Core Taxonomy API

  • Introduces the bp_insert_term() function to save a BP Type into the database.
  • Introduces the bp_get_terms() function to retrieve BP Types from the database.
  • Introduces the bp_delete_term() function to remove a BP Type from the database.

Props mercime, DJPaul, dcavins, boonebgorges, tw2113, sbrajesh

See #7179
See #7181

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-taxonomy.php

    r11119 r12727  
    278278        return $term;
    279279}
     280
     281/**
     282 * Add a new taxonomy term to the database.
     283 *
     284 * @since 7.0.0
     285 *
     286 * @param string $term     The BP term name to add.
     287 * @param string $taxonomy The BP taxonomy to which to add the BP term.
     288 * @param array  $args {
     289 *     Optional. Array of arguments for inserting a BP term.
     290 *     @type string $description The term description. Default empty string.
     291 *     @type string $slug        The term slug to use. Default empty string.
     292 *     @type array  $metas       The term metas to add. Default empty array.
     293 * }
     294 * @return array|WP_Error An array containing the `term_id` and `term_taxonomy_id`,
     295 *                        WP_Error otherwise.
     296 */
     297function bp_insert_term( $term, $taxonomy = '', $args = array() ) {
     298        if ( ! taxonomy_exists( $taxonomy ) ) {
     299                return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.', 'buddypress' ) );
     300        }
     301
     302        $site_id = bp_get_taxonomy_term_site_id( $taxonomy );
     303
     304        $switched = false;
     305        if ( $site_id !== get_current_blog_id() ) {
     306                switch_to_blog( $site_id );
     307                bp_register_taxonomies();
     308                $switched = true;
     309        }
     310
     311        $term_metas = array();
     312        if ( isset( $args['metas'] ) ) {
     313                $term_metas = (array) $args['metas'];
     314                unset( $args['metas'] );
     315        }
     316
     317        /**
     318         * Fires before a BP Term is added to the database.
     319         *
     320         * @since 7.0.0
     321         *
     322         * @param string $term     The BP term name to add.
     323         * @param string $taxonomy The BP taxonomy to which to add the term.
     324         * @param array  $args     Array of arguments for inserting a BP term.
     325         */
     326        do_action( 'bp_before_insert_term', $term, $taxonomy, $args );
     327
     328        $tt_id = wp_insert_term( $term, $taxonomy, $args );
     329
     330        if ( is_wp_error( $tt_id ) ) {
     331                return $tt_id;
     332        }
     333
     334        $term_id = reset( $tt_id );
     335
     336        if ( $term_metas ) {
     337                bp_update_type_metadata( $term_id, $taxonomy, $term_metas );
     338        }
     339
     340        if ( $switched ) {
     341                restore_current_blog();
     342        }
     343
     344        /**
     345         * Fires when taxonomy terms have been set on BuddyPress objects.
     346         *
     347         * @since 7.0.0
     348         *
     349         * @param array  $tt_ids    An array containing the `term_id` and `term_taxonomy_id`.
     350         * @param string $taxonomy  Taxonomy name.
     351         * @param array  $term_metas The term metadata.
     352         */
     353        do_action( 'bp_insert_term', $tt_id, $taxonomy, $term_metas );
     354
     355        return $tt_id;
     356}
     357
     358/**
     359 * Get taxonomy BP Terms from the database.
     360 *
     361 * @since 7.0.0
     362 *
     363 * @param array  $args {
     364 *     Array of arguments to query BP Terms.
     365 *     @see `get_terms()` for full description of arguments in case of a member type.
     366 * }
     367 * @return array The list of terms matching arguments.
     368 */
     369function bp_get_terms( $args = array() ) {
     370        $args = bp_parse_args(
     371                $args,
     372                array(
     373                        'taxonomy'   => '',
     374                        'number'     => '',
     375                        'hide_empty' => false,
     376                ),
     377                'get_terms'
     378        );
     379
     380        if ( ! $args['taxonomy'] ) {
     381                return array();
     382        }
     383
     384        $site_id = bp_get_taxonomy_term_site_id( $args['taxonomy'] );
     385
     386        $switched = false;
     387        if ( $site_id !== get_current_blog_id() ) {
     388                switch_to_blog( $site_id );
     389                bp_register_taxonomies();
     390                $switched = true;
     391        }
     392
     393        $terms = get_terms( $args );
     394
     395        if ( $switched ) {
     396                restore_current_blog();
     397        }
     398
     399        /**
     400         * Filter here to modify the BP Terms found into the database.
     401         *
     402         * @since 7.0.0
     403         *
     404         * @param array $terms The list of terms matching arguments.
     405         * @param array $args  Array of arguments used to query BP Terms.
     406         */
     407        return apply_filters(
     408                'bp_get_terms',
     409                $terms,
     410                $args
     411        );
     412}
     413
     414/**
     415 * Deletes a BP Term.
     416 *
     417 * @since 7.0.0
     418 *
     419 * @param int     $term_id  The BP Term ID. Required.
     420 * @param string  $taxonomy The BP Taxonomy Name. Required.
     421 * @return bool|WP_Error True on success, WP_Error on failure.
     422 */
     423function bp_delete_term( $term_id = 0, $taxonomy = '' ) {
     424        if ( ! $term_id || ! $taxonomy ) {
     425                return new WP_Error( 'missing_arguments', __( 'Sorry, the term ID and the taxonomy are required arguments.', 'buddypress' ) );
     426        }
     427
     428        $site_id = bp_get_taxonomy_term_site_id( $taxonomy );
     429
     430        $switched = false;
     431        if ( $site_id !== get_current_blog_id() ) {
     432                switch_to_blog( $site_id );
     433                bp_register_taxonomies();
     434                $switched = true;
     435        }
     436
     437        /**
     438         * Fires before a BP Term is deleted from the database.
     439         *
     440         * @since 7.0.0
     441         *
     442         * @param int    $term_id  The BP Term ID.
     443         * @param string $taxonomy The BP Taxonomy Name.
     444         */
     445        do_action( 'bp_before_delete_term', $term_id, $taxonomy );
     446
     447        $deleted = wp_delete_term( $term_id, $taxonomy );
     448
     449        if ( $switched ) {
     450                restore_current_blog();
     451        }
     452
     453        if ( is_wp_error( $deleted ) ) {
     454                return $deleted;
     455        }
     456
     457        if ( false === $deleted ) {
     458                return new WP_Error( 'inexistant_term', __( 'Sorry, the term does not exist.', 'buddypress' ) );
     459        }
     460
     461        if ( 0 === $deleted ) {
     462                return new WP_Error( 'default_term', __( 'Sorry, the default term cannot be deleted.', 'buddypress' ) );
     463        }
     464
     465        /**
     466         * Fires once a BP Term has been deleted from the database.
     467         *
     468         * @since 7.0.0
     469         *
     470         * @param boolean $deleted True.
     471         * @param int     $term_id  The deleted BP Term ID.
     472         * @param string  $taxonomy The BP Taxonomy Name of the deleted BP Term ID.
     473         */
     474        do_action( 'bp_delete_term', $deleted, $term_id, $taxonomy );
     475
     476        return $deleted;
     477}
Note: See TracChangeset for help on using the changeset viewer.