Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/21/2020 01:21:21 AM (5 years ago)
Author:
imath
Message:

BP Types: merge BP Types saved in DB with the ones registered by code

  • Introduces the bp_get_taxonomy_types() to merge types saved into the database with the types registered by code. It's now possible to edit all properties of the registered by code types except for the type's slug which needs to be unique.
  • Caches these merged types.
  • Introduces the bp_get_member_types_registered_by_code() to only get member types registered by code.
  • Updates the test_bp_get_member_type_should_not_return_unregistered_types() unit test to test_bp_get_registered_by_code_member_type_should_not_return_unregistered_types() to check the previous point new function.
  • Introduces the bp_update_type_metadata() function to update a type's metadata.

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

See #7179
See #7181

File:
1 edited

Legend:

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

    r12728 r12729  
    31443144}
    31453145
     3146/**
     3147 * Update a list of metadata for a given type ID and a given taxonomy.
     3148 *
     3149 * @since 7.0.0
     3150 *
     3151 * @param  integer $type_id    The database ID of the BP Type.
     3152 * @param  string  $taxonomy   The BP Type taxonomy.
     3153 * @param  array   $type_metas An associative array (meta_key=>meta_value).
     3154 * @return boolean             False on failure. True otherwise.
     3155 */
     3156function bp_update_type_metadata( $type_id = 0, $taxonomy = '', $type_metas = array() ) {
     3157    if ( ! $type_id || ! $taxonomy || ! is_array( $type_metas ) ) {
     3158        return false;
     3159    }
     3160
     3161    foreach ( $type_metas as $meta_key => $meta_value ) {
     3162        if ( ! registered_meta_key_exists( 'term', $meta_key, $taxonomy ) ) {
     3163            continue;
     3164        }
     3165
     3166        update_term_meta( $type_id, $meta_key, $meta_value );
     3167    }
     3168
     3169    return true;
     3170}
     3171
     3172/**
     3173 * Get types for a given BP Taxonomy.
     3174 *
     3175 * @since 7.0.0
     3176 *
     3177 * @param string $taxonomy The taxonomy to transform terms in types for.
     3178 * @param array  $types    Existing types to merge with the types found into the database.
     3179 *                         For instance this function is used internally to merge Group/Member
     3180 *                         types registered using code with the ones created by the administrator
     3181 *                         from the Group/Member types Administration screen. If not provided, only
     3182 *                         Types created by the administrator will be returned.
     3183 *                         Optional.
     3184 * @return array           The types of the given taxonomy.
     3185 */
     3186function bp_get_taxonomy_types( $taxonomy = '', $types = array() ) {
     3187    if ( ! $taxonomy ) {
     3188        return $types;
     3189    }
     3190
     3191    $db_types = wp_cache_get( $taxonomy, 'bp_object_terms' );
     3192
     3193    if ( ! $db_types ) {
     3194        $terms = bp_get_terms(
     3195            array(
     3196                'taxonomy' => $taxonomy,
     3197            )
     3198        );
     3199
     3200        if ( ! is_array( $terms ) || ! $terms ) {
     3201            return $types;
     3202        }
     3203
     3204        $type_metadata = array_keys( get_registered_meta_keys( 'term', $taxonomy ) );
     3205
     3206        foreach ( $terms as $term ) {
     3207            $type_name                      = $term->name;
     3208            $db_types[ $type_name ]         = new stdClass();
     3209            $db_types[ $type_name ]->db_id  = $term->term_id;
     3210            $db_types[ $type_name ]->labels = array();
     3211            $db_types[ $type_name ]->name   = $type_name;
     3212
     3213            if ( $type_metadata ) {
     3214                foreach ( $type_metadata as $meta_key ) {
     3215                    $type_key = str_replace( 'bp_type_', '', $meta_key );
     3216                    if ( in_array( $type_key, array( 'name', 'singular_name' ), true ) ) {
     3217                        $db_types[ $type_name ]->labels[ $type_key ] = get_term_meta( $term->term_id, $meta_key, true );
     3218                    } else {
     3219                        $db_types[ $type_name ]->{$type_key} = get_term_meta( $term->term_id, $meta_key, true );
     3220                    }
     3221                }
     3222            }
     3223        }
     3224
     3225        wp_cache_set( $taxonomy, $db_types, 'bp_object_terms' );
     3226    }
     3227
     3228    if ( is_array( $db_types ) ) {
     3229        foreach ( $db_types as $db_type_name => $db_type ) {
     3230            // Override props of registered by code types if customized by the admun user.
     3231            if ( isset( $types[ $db_type_name ] ) && isset( $types[ $db_type_name ]->code ) && $types[ $db_type_name ]->code ) {
     3232                // Merge Labels.
     3233                if ( $db_type->labels ) {
     3234                    foreach ( $db_type->labels as $key_label => $value_label ) {
     3235                        if ( '' !== $value_label ) {
     3236                            $types[ $db_type_name ]->labels[ $key_label ] = $value_label;
     3237                        }
     3238                    }
     3239                }
     3240
     3241                // Merge other properties.
     3242                foreach ( get_object_vars( $types[ $db_type_name ] ) as $key_prop => $value_prop ) {
     3243                    if ( 'labels' === $key_prop || 'name' === $key_prop ) {
     3244                        continue;
     3245                    }
     3246
     3247                    if ( isset( $db_type->{$key_prop} ) && '' !== $db_type->{$key_prop} ) {
     3248                        $types[ $db_type_name  ]->{$key_prop} = $db_type->{$key_prop};
     3249                    }
     3250                }
     3251
     3252                unset( $db_types[ $db_type_name ] );
     3253            }
     3254        }
     3255    }
     3256
     3257    return array_merge( $types, (array) $db_types );
     3258}
     3259
    31463260/** Email *****************************************************************/
    31473261
Note: See TracChangeset for help on using the changeset viewer.