Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/21/2020 01:21:21 AM (4 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-members/bp-members-functions.php

    r12728 r12729  
    27642764        'labels'        => array(),
    27652765        'has_directory' => true,
     2766        'code'          => true,
     2767        'db_id'         => 0,
    27662768    ), 'register_member_type' );
    27672769
     
    28632865    $types = buddypress()->members->types;
    28642866
     2867    // Merge with types available into the database.
     2868    if ( ! isset( $args['code'] ) || true !== $args['code'] ) {
     2869        $types = bp_get_taxonomy_types( bp_get_member_type_tax_name(), $types );
     2870    }
     2871
    28652872    $types = wp_filter_object_list( $types, $args, $operator );
    28662873
     
    28852892    return $types;
    28862893}
     2894
     2895/**
     2896 * Only gets the member types registered by code.
     2897 *
     2898 * @since 7.0.0
     2899 *
     2900 * @return array The member types registered by code.
     2901 */
     2902function bp_get_member_types_registered_by_code() {
     2903    return bp_get_member_types(
     2904        array(
     2905            'code' => true,
     2906        ),
     2907        'objects'
     2908    );
     2909}
     2910add_filter( bp_get_member_type_tax_name() . '_registered_by_code', 'bp_get_member_types_registered_by_code' );
     2911
     2912/**
     2913 * Generates missing metadata for a type registered by code.
     2914 *
     2915 * @since 7.0.0
     2916 *
     2917 * @return array The member type metadata.
     2918 */
     2919function bp_set_registered_by_code_member_type_metadata( $metadata = array(), $type = '' ) {
     2920    $member_type = bp_get_member_type_object( $type );
     2921
     2922    foreach ( get_object_vars( $member_type ) as $object_key => $object_value ) {
     2923        if ( 'labels' === $object_key ) {
     2924            foreach ( $object_value as $label_key => $label_value ) {
     2925                $metadata[ 'bp_type_' . $label_key ] = $label_value;
     2926            }
     2927        } elseif ( ! in_array( $object_key, array( 'name', 'code', 'db_id' ), true ) ) {
     2928            $metadata[ 'bp_type_' . $object_key ] = $object_value;
     2929        }
     2930    }
     2931
     2932    /**
     2933     * Save metadata into database to avoid generating metadata
     2934     * each time a type is listed into the Types Admin screen.
     2935     */
     2936    if ( isset( $member_type->db_id ) && $member_type->db_id ) {
     2937        bp_update_type_metadata( $member_type->db_id, bp_get_member_type_tax_name(), $metadata );
     2938    }
     2939
     2940    return $metadata;
     2941}
     2942add_filter( bp_get_member_type_tax_name() . '_set_registered_by_code_metada', 'bp_set_registered_by_code_member_type_metadata', 10, 2 );
     2943
     2944/**
     2945 * Insert member types registered by code not yet saved into the database as WP Terms.
     2946 *
     2947 * @since 7.0.0
     2948 */
     2949function bp_insert_member_types_registered_by_code() {
     2950    $all_types     = bp_get_member_types( array(), 'objects' );
     2951    $unsaved_types = wp_filter_object_list( $all_types, array( 'db_id' => 0 ), 'and', 'name' );
     2952
     2953    if ( $unsaved_types ) {
     2954        foreach ( $unsaved_types as $type_name ) {
     2955            bp_insert_term(
     2956                $type_name,
     2957                bp_get_member_type_tax_name(),
     2958                array(
     2959                    'slug' => $type_name,
     2960                )
     2961            );
     2962        }
     2963    }
     2964}
     2965add_action( bp_get_member_type_tax_name() . '_add_form', 'bp_insert_member_types_registered_by_code', 1 );
    28872966
    28882967/**
     
    29693048 *
    29703049 * @since 2.2.0
     3050 * @since 7.0.0 Adds the `$use_db` parameter.
    29713051 *
    29723052 * @param int  $user_id ID of the user.
    29733053 * @param bool $single  Optional. Whether to return a single type string. If multiple types are found
    29743054 *                      for the user, the oldest one will be returned. Default: true.
     3055 * @param bool $use_db  Optional. Whether to request all member types or only the ones registered by code.
     3056 *                      Default: true.
    29753057 * @return string|array|bool On success, returns a single member type (if $single is true) or an array of member
    29763058 *                           types (if $single is false). Returns false on failure.
    29773059 */
    2978 function bp_get_member_type( $user_id, $single = true ) {
     3060function bp_get_member_type( $user_id, $single = true, $use_db = true ) {
    29793061    $types = wp_cache_get( $user_id, 'bp_member_member_type' );
    29803062
     
    29943076            wp_cache_set( $user_id, $types, 'bp_member_member_type' );
    29953077        }
     3078    }
     3079
     3080    if ( false === $use_db && $types ) {
     3081        $registred_by_code = bp_get_member_types_registered_by_code();
     3082        $ctype_names       = wp_list_pluck( $registred_by_code, 'name' );
     3083        $types             = array_intersect( $types, $ctype_names );
    29963084    }
    29973085
Note: See TracChangeset for help on using the changeset viewer.