Changeset 12729 for trunk/src/bp-members/bp-members-functions.php
- Timestamp:
- 09/21/2020 01:21:21 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-members/bp-members-functions.php
r12728 r12729 2764 2764 'labels' => array(), 2765 2765 'has_directory' => true, 2766 'code' => true, 2767 'db_id' => 0, 2766 2768 ), 'register_member_type' ); 2767 2769 … … 2863 2865 $types = buddypress()->members->types; 2864 2866 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 2865 2872 $types = wp_filter_object_list( $types, $args, $operator ); 2866 2873 … … 2885 2892 return $types; 2886 2893 } 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 */ 2902 function bp_get_member_types_registered_by_code() { 2903 return bp_get_member_types( 2904 array( 2905 'code' => true, 2906 ), 2907 'objects' 2908 ); 2909 } 2910 add_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 */ 2919 function 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 } 2942 add_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 */ 2949 function 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 } 2965 add_action( bp_get_member_type_tax_name() . '_add_form', 'bp_insert_member_types_registered_by_code', 1 ); 2887 2966 2888 2967 /** … … 2969 3048 * 2970 3049 * @since 2.2.0 3050 * @since 7.0.0 Adds the `$use_db` parameter. 2971 3051 * 2972 3052 * @param int $user_id ID of the user. 2973 3053 * @param bool $single Optional. Whether to return a single type string. If multiple types are found 2974 3054 * 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. 2975 3057 * @return string|array|bool On success, returns a single member type (if $single is true) or an array of member 2976 3058 * types (if $single is false). Returns false on failure. 2977 3059 */ 2978 function bp_get_member_type( $user_id, $single = true ) {3060 function bp_get_member_type( $user_id, $single = true, $use_db = true ) { 2979 3061 $types = wp_cache_get( $user_id, 'bp_member_member_type' ); 2980 3062 … … 2994 3076 wp_cache_set( $user_id, $types, 'bp_member_member_type' ); 2995 3077 } 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 ); 2996 3084 } 2997 3085
Note: See TracChangeset
for help on using the changeset viewer.