Skip to:
Content

BuddyPress.org

Changeset 12728


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

Location:
trunk/src
Files:
5 edited

Legend:

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

    r12578 r12728  
    8383
    8484/**
    85  * The bp_register_taxonomies hook - Attached to 'bp_init' @ priority 2 above.
     85 * The bp_register_taxonomies hooks - Attached to 'bp_init' @ priority 2 above.
    8686 */
    8787add_action( 'bp_register_taxonomies', 'bp_register_member_types' );
     88add_action( 'bp_register_taxonomies', 'bp_register_type_metadata', 20 );
    8889
    8990/**
  • trunk/src/bp-core/bp-core-dependency.php

    r12578 r12728  
    9494     */
    9595    do_action( 'bp_register_taxonomies' );
     96}
     97
     98/**
     99 * Fire the 'bp_register_type_metadata' action, where plugins should register metadata for their custom BuddyPress types.
     100 *
     101 * @since 7.0.0
     102 */
     103function bp_register_type_metadata() {
     104
     105    /**
     106     * Fires inside the 'bp_register_type_metadata' function, where plugins should register metadata for their custom BuddyPress types.
     107     *
     108     * @since 7.0.0
     109     */
     110    do_action( 'bp_register_type_metadata' );
    96111}
    97112
  • 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 *****************************************************************/
  • trunk/src/bp-core/bp-core-taxonomy.php

    r12727 r12728  
    1616
    1717/**
     18 * Returns default BuddyPress taxonomies.
     19 *
     20 * @since 7.0.0
     21 *
     22 * @return array The BuddyPress default taxonomies.
     23 */
     24function bp_get_default_taxonomies() {
     25    $taxonomies = array(
     26        // Member Type.
     27        bp_get_member_type_tax_name() => array(
     28            'object'    => 'user',
     29            'component' => 'members',
     30            'args'      => bp_get_member_type_tax_args(),
     31        ),
     32        // Email type.
     33        bp_get_email_tax_type()       => array(
     34            'object'    => bp_get_email_post_type(),
     35            'component' => 'core',
     36            'args'      => bp_get_email_tax_type_args(),
     37        ),
     38    );
     39
     40    /**
     41     * This filter should only be used by built-in BuddyPress Components.
     42     *
     43     * @since 7.0.0
     44     *
     45     * @param array $taxonomies The taxonomy arguments used for WordPress registration.
     46     */
     47    return apply_filters( 'bp_get_default_taxonomies', $taxonomies );
     48}
     49
     50/**
    1851 * Register our default taxonomies.
    1952 *
     
    2154 */
    2255function bp_register_default_taxonomies() {
    23     // Member Type.
    24     register_taxonomy( bp_get_member_type_tax_name(), 'user', array(
    25         'public' => false,
    26     ) );
    27 
    28     // Email type.
    29     register_taxonomy(
    30         bp_get_email_tax_type(),
    31         bp_get_email_post_type(),
    32         apply_filters( 'bp_register_email_tax_type', array(
    33             'description'   => _x( 'BuddyPress email types', 'email type taxonomy description', 'buddypress' ),
    34             'labels'        => bp_get_email_tax_type_labels(),
    35             'meta_box_cb'   => 'bp_email_tax_type_metabox',
    36             'public'        => false,
    37             'query_var'     => false,
    38             'rewrite'       => false,
    39             'show_in_menu'  => false,
    40             'show_tagcloud' => false,
    41             'show_ui'       => bp_is_root_blog() && bp_current_user_can( 'bp_moderate' ),
    42         ) )
    43     );
     56    $taxonomies = bp_get_default_taxonomies();
     57
     58    foreach ( $taxonomies as $taxonomy_name => $taxonomy_params ) {
     59        if ( ! isset( $taxonomy_params['object'] ) || ! isset( $taxonomy_params['args'] ) ) {
     60            continue;
     61        }
     62
     63        register_taxonomy(
     64            $taxonomy_name,
     65            $taxonomy_params['object'],
     66            $taxonomy_params['args']
     67        );
     68    }
    4469}
    4570add_action( 'bp_register_taxonomies', 'bp_register_default_taxonomies' );
  • trunk/src/bp-members/bp-members-functions.php

    r12694 r12728  
    26472647
    26482648/**
     2649 * Returns labels used by the member type taxonomy.
     2650 *
     2651 * @since 7.0.0
     2652 *
     2653 * @return array
     2654 */
     2655function bp_get_member_type_tax_labels() {
     2656
     2657    /**
     2658     * Filters Member type taxonomy labels.
     2659     *
     2660     * @since 7.0.0
     2661     *
     2662     * @param array $value Associative array (name => label).
     2663     */
     2664    return apply_filters(
     2665        'bp_get_member_type_tax_labels',
     2666        array(
     2667            'name'                       => _x( 'Member types', 'Member type taxonomy name', 'buddypress' ),
     2668            'singular_name'              => _x( 'Member type', 'Member type taxonomy singular name', 'buddypress' ),
     2669            'search_items'               => _x( 'Search Member types', 'Member type taxonomy search items label', 'buddypress' ),
     2670            'popular_items'              => _x( 'Most used Member types', 'Member type taxonomy popular items label', 'buddypress' ),
     2671            'all_items'                  => _x( 'All Member types', 'Member type taxonomy all items label', 'buddypress' ),
     2672            'edit_item'                  => _x( 'Edit Member type', 'Member type taxonomy edit item label', 'buddypress' ),
     2673            'view_item'                  => _x( 'View Member type', 'Member type taxonomy view item label', 'buddypress' ),
     2674            'update_item'                => _x( 'Update Member type', 'Member type taxonomy update item label', 'buddypress' ),
     2675            'add_new_item'               => _x( 'Add new Member type', 'Member type taxonomy add new item label', 'buddypress' ),
     2676            'new_item_name'              => _x( 'New Member type name', 'Member type taxonomy new item name label', 'buddypress' ),
     2677            'separate_items_with_commas' => _x( 'Separate Member types with commas', 'Member type taxonomy separate items with commas label', 'buddypress' ),
     2678            'add_or_remove_items'        => _x( 'Add or remove Member types', 'Member type taxonomy add or remove items label', 'buddypress' ),
     2679            'choose_from_most_used'      => _x( 'Choose from the most used Member types', 'Member type taxonomy choose from most used label', 'buddypress' ),
     2680            'not_found'                  => _x( 'No Member types found', 'Member type taxonomy not found label', 'buddypress' ),
     2681            'no_terms'                   => _x( 'No Member types', 'Member type taxonomy no terms label', 'buddypress' ),
     2682            'items_list_navigation'      => _x( 'Member types list navigation', 'Member type taxonomy items list navigation label', 'buddypress' ),
     2683            'items_list'                 => _x( 'Member types list', 'Member type taxonomy items list label', 'buddypress' ),
     2684            'back_to_items'              => _x( 'Back to all Member types', 'Member type taxonomy back to items label', 'buddypress' ),
     2685            // Specific to BuddyPress.
     2686            'bp_type_id_label'           => _x( 'Member Type ID', 'BP Member type ID label', 'buddypress' ),
     2687            'bp_type_id_description'     => _x( 'Enter a lower-case string without spaces or special characters (used internally to identify the member type).', 'BP Member type ID description', 'buddypress' ),
     2688        )
     2689    );
     2690}
     2691
     2692/**
     2693 * Returns arguments used by the Member type taxonomy.
     2694 *
     2695 * @since 7.0.0
     2696 *
     2697 * @return array
     2698 */
     2699function bp_get_member_type_tax_args() {
     2700
     2701    /**
     2702     * Filters Member type taxonomy args.
     2703     *
     2704     * @since 7.0.0
     2705     *
     2706     * @param array $value Associative array (key => arg).
     2707     */
     2708    return apply_filters(
     2709        'bp_get_member_type_tax_args',
     2710        array_merge(
     2711            array(
     2712                'description' => _x( 'BuddyPress Member types', 'Member type taxonomy description', 'buddypress' ),
     2713                'labels'      => array_merge( bp_get_member_type_tax_labels(), bp_get_taxonomy_common_labels() ),
     2714            ),
     2715            bp_get_taxonomy_common_args()
     2716        )
     2717    );
     2718}
     2719
     2720/**
     2721 * Registers the Member type metadata.
     2722 *
     2723 * @since 7.0.0
     2724 */
     2725function bp_register_member_type_metadata() {
     2726    $type_taxonomy = bp_get_member_type_tax_name();
     2727
     2728    foreach ( bp_get_type_metadata_schema( false, $type_taxonomy ) as $meta_key => $meta_args ) {
     2729        bp_register_type_meta( $type_taxonomy, $meta_key, $meta_args );
     2730    }
     2731}
     2732add_action( 'bp_register_type_metadata', 'bp_register_member_type_metadata' );
     2733
     2734/**
    26492735 * Register a member type.
    26502736 *
Note: See TracChangeset for help on using the changeset viewer.