Skip to:
Content

BuddyPress.org


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

BP Types: introduce type metadata registration & add a Member type one

  • Introduces the bp_register_type_meta() function to register metadata for the Type taxonomies.
  • Introduces the bp_get_type_metadata_schema() to get common to all types metadata properties.
  • Adds the bp_register_type_metadata hook. Hook to it to register the Member type metadata.
  • Introduces the bp_get_default_taxonomies() to list the BuddyPress default taxonomies (BP Email & Member Type taxonomies).
  • Improves bp_register_default_taxonomies() so that it use the previous point new function.
  • Adds labels and arguments to the Member Type taxonomy.
  • Introduces the bp_get_taxonomy_common_args() to retrieve common BP Taxonomy arguments.
  • Introduces the bp_get_email_tax_type_args() & bp_get_member_type_tax_args() so that the BP Email & the Member type taxonomies use it to retrieve its arguments enjoying the previous point new function.

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

    r12694 r12728  
    29382938
    29392939/**
     2940 * Returns the BP Taxonomy common arguments.
     2941 *
     2942 * @since 7.0.0
     2943 *
     2944 * @return array The BP Taxonomy common arguments.
     2945 */
     2946function bp_get_taxonomy_common_args() {
     2947    return array(
     2948        'public'        => false,
     2949        'show_in_rest'  => false,
     2950        'query_var'     => false,
     2951        'rewrite'       => false,
     2952        'show_in_menu'  => false,
     2953        'show_tagcloud' => false,
     2954        'show_ui'       => bp_is_root_blog() && bp_current_user_can( 'bp_moderate' ),
     2955    );
     2956}
     2957
     2958/**
     2959 * Returns the BP Taxonomy common labels.
     2960 *
     2961 * @since 7.0.0
     2962 *
     2963 * @return array The BP Taxonomy common labels.
     2964 */
     2965function bp_get_taxonomy_common_labels() {
     2966    return array(
     2967        'bp_type_name'           => _x( 'Name', 'BP Type name label', 'buddypress' ),
     2968        'bp_type_singular_name'  => _x( 'Singular name', 'BP Type singular name label', 'buddypress' ),
     2969        'bp_type_has_directory'  => _x( 'Add Type-Filtered Directory View', 'BP Type has directory checkbox label', 'buddypress' ),
     2970        'bp_type_directory_slug' => _x( 'Custom type directory slug', 'BP Type slug label', 'buddypress' ),
     2971    );
     2972}
     2973
     2974/**
    29402975 * Output the name of the email type taxonomy.
    29412976 *
     
    29993034}
    30003035
     3036/**
     3037 * Return arguments used by the email type taxonomy.
     3038 *
     3039 * @since 7.0.0
     3040 *
     3041 * @return array
     3042 */
     3043function bp_get_email_tax_type_args() {
     3044
     3045    /**
     3046     * Filters emails type taxonomy args.
     3047     *
     3048     * @since 7.0.0
     3049     *
     3050     * @param array $value Associative array (key => arg).
     3051     */
     3052    return apply_filters(
     3053        'bp_register_email_tax_type',
     3054        array_merge(
     3055            array(
     3056                'description'   => _x( 'BuddyPress email types', 'email type taxonomy description', 'buddypress' ),
     3057                'labels'        => bp_get_email_tax_type_labels(),
     3058                'meta_box_cb'   => 'bp_email_tax_type_metabox',
     3059            ),
     3060            bp_get_taxonomy_common_args()
     3061        )
     3062    );
     3063}
     3064
     3065/**
     3066 * Returns the default BuddyPress type metadata schema.
     3067 *
     3068 * @since 7.0.0
     3069 *
     3070 * @param  boolean $suppress_filters Whether to suppress filters. Default `false`.
     3071 * @param  string  $type_taxonomy    Optional. the Type's taxonomy name.
     3072 * @return array                     The default BuddyPress type metadata schema.
     3073 */
     3074function bp_get_type_metadata_schema( $suppress_filters = false, $type_taxonomy = '' ) {
     3075    $schema = array(
     3076        'bp_type_name' => array(
     3077            'description'       => __( 'The name of your type, at the plural form.', 'buddypress' ),
     3078            'type'              => 'string',
     3079            'single'            => true,
     3080            'sanitize_callback' => 'sanitize_text_field',
     3081        ),
     3082        'bp_type_singular_name' => array(
     3083            'description'       => __( 'The name of your type, at the singular form.', 'buddypress' ),
     3084            'type'              => 'string',
     3085            'single'            => true,
     3086            'sanitize_callback' => 'sanitize_text_field',
     3087        ),
     3088        'bp_type_has_directory' => array(
     3089            'description'       => __( 'Add a list of members matching the member type available on the Members Directory page (e.g. site.url/members/type/teacher/).', 'buddypress' ),
     3090            'type'              => 'boolean',
     3091            'single'            => true,
     3092            'sanitize_callback' => 'absint',
     3093        ),
     3094        'bp_type_directory_slug' => array(
     3095            'label'             => __( 'Custom type directory slug', 'buddypress' ),
     3096            'description'       => __( 'If you want to use a slug that is different from the Member Type ID above, enter it here.', 'buddypress' ),
     3097            'type'              => 'string',
     3098            'single'            => true,
     3099            'sanitize_callback' => 'sanitize_title',
     3100        ),
     3101    );
     3102
     3103    if ( true === $suppress_filters ) {
     3104        return $schema;
     3105    }
     3106
     3107    /**
     3108     * Filter here to add new meta to the BuddyPress type metadata.
     3109     *
     3110     * @since 7.0.0
     3111     *
     3112     * @param array  $schema        Associative array (name => arguments).
     3113     * @param string $type_taxonomy The Type's taxonomy name.
     3114     */
     3115    return apply_filters( 'bp_get_type_metadata_schema', $schema, $type_taxonomy );
     3116}
     3117
     3118/**
     3119 * Registers a meta key for BuddyPress types.
     3120 *
     3121 * @since 7.0.0
     3122 *
     3123 * @param string $type_tax The BuddyPress type taxonomy.
     3124 * @param string $meta_key The meta key to register.
     3125 * @param array  $args     Data used to describe the meta key when registered. See
     3126 *                         {@see register_meta()} for a list of supported arguments.
     3127 * @return bool True if the meta key was successfully registered, false if not.
     3128 */
     3129function bp_register_type_meta( $type_tax, $meta_key, array $args ) {
     3130    $taxonomies = wp_list_pluck( bp_get_default_taxonomies(), 'component' );
     3131
     3132    if ( ! isset( $taxonomies[ $type_tax ] ) ) {
     3133        return false;
     3134    }
     3135
     3136    // register_term_meta() was introduced in WP 4.9.8.
     3137    if ( ! function_exists( 'register_term_meta' ) ) {
     3138        $args['object_subtype'] = $type_tax;
     3139
     3140        return register_meta( 'term', $meta_key, $args );
     3141    }
     3142
     3143    return register_term_meta( $type_tax, $meta_key, $args );
     3144}
    30013145
    30023146/** Email *****************************************************************/
Note: See TracChangeset for help on using the changeset viewer.