Skip to:
Content

BuddyPress.org


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

BP Types: add functions to generate the Group types WP Admin screens

  • Adds labels and arguments to the Group Type taxonomy.
  • Adds the Group type's Administration screens.
  • Introduces the bp_groups_register_group_type_taxonomy() function to register the Group type's taxonomy and stop doing it from the BP_Groups_Component::register_taxonomies() method.
  • Introduces the bp_get_group_type_metadata_schema() to add custom metadata for the Group type's taxonomy.
  • Introduces the bp_get_group_types_registered_by_code() to only get Group types registered by code.
  • Stops using the hardcoded bp_group_type identifier in favor of the filterable bp_get_group_type_tax_name() function.
  • Updates the test_groups_get_type_should_not_return_unregistered_types() unit test to test_groups_registered_by_code_group_type_should_not_return_unregistered_types() to check the previous point new function.

Props mercime, DJPaul, dcavins, boonebgorges

Fixes #7179

File:
1 edited

Legend:

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

    r12694 r12731  
    24652465
    24662466/**
     2467 * Output the slug of the Group type taxonomy.
     2468 *
     2469 * @since 7.0.0
     2470 */
     2471function bp_group_type_tax_name() {
     2472        echo bp_get_group_type_tax_name();
     2473}
     2474
     2475        /**
     2476         * Return the slug of the Group type taxonomy.
     2477         *
     2478         * @since 7.0.0
     2479         *
     2480         * @return string The unique Group taxonomy slug.
     2481         */
     2482        function bp_get_group_type_tax_name() {
     2483                /**
     2484                 * Filters the slug of the Group type taxonomy.
     2485                 *
     2486                 * @since 7.0.0
     2487                 *
     2488                 * @param string $value Group type taxonomy slug.
     2489                 */
     2490                return apply_filters( 'bp_get_group_type_tax_name', 'bp_group_type' );
     2491        }
     2492
     2493/**
     2494 * Returns labels used by the Group type taxonomy.
     2495 *
     2496 * @since 7.0.0
     2497 *
     2498 * @return array
     2499 */
     2500function bp_get_group_type_tax_labels() {
     2501
     2502        /**
     2503         * Filters Group type taxonomy labels.
     2504         *
     2505         * @since 7.0.0
     2506         *
     2507         * @param array $value Associative array (name => label).
     2508         */
     2509        return apply_filters(
     2510                'bp_get_group_type_tax_labels',
     2511                array(
     2512                        'name'                          => _x( 'Group types', 'Group type taxonomy name', 'buddypress' ),
     2513                        'singular_name'                 => _x( 'Group type', 'Group type taxonomy singular name', 'buddypress' ),
     2514                        'search_items'                  => _x( 'Search Group types', 'Group type taxonomy search items label', 'buddypress' ),
     2515                        'popular_items'                 => _x( 'Most used Group types', 'Group type taxonomy popular items label', 'buddypress' ),
     2516                        'all_items'                     => _x( 'All Group types', 'Group type taxonomy all items label', 'buddypress' ),
     2517                        'edit_item'                     => _x( 'Edit Group type', 'Group type taxonomy edit item label', 'buddypress' ),
     2518                        'view_item'                     => _x( 'View Group type', 'Group type taxonomy view item label', 'buddypress' ),
     2519                        'update_item'                   => _x( 'Update Group type', 'Group type taxonomy update item label', 'buddypress' ),
     2520                        'add_new_item'                  => _x( 'Add new Group type', 'Group type taxonomy add new item label', 'buddypress' ),
     2521                        'new_item_name'                 => _x( 'New Group type name', 'Group type taxonomy new item name label', 'buddypress' ),
     2522                        'separate_items_with_commas'    => _x( 'Separate Group types with commas', 'Group type taxonomy separate items with commas label', 'buddypress' ),
     2523                        'add_or_remove_items'           => _x( 'Add or remove Group types', 'Group type taxonomy add or remove items label', 'buddypress' ),
     2524                        'choose_from_most_used'         => _x( 'Choose from the most used Group types', 'Group type taxonomy choose from most used label', 'buddypress' ),
     2525                        'not_found'                     => _x( 'No Group types found', 'Group type taxonomy not found label', 'buddypress' ),
     2526                        'no_terms'                      => _x( 'No Group types', 'Group type taxonomy no terms label', 'buddypress' ),
     2527                        'items_list_navigation'         => _x( 'Group types list navigation', 'Group type taxonomy items list navigation label', 'buddypress' ),
     2528                        'items_list'                    => _x( 'Group types list', 'Group type taxonomy items list label', 'buddypress' ),
     2529                        'back_to_items'                 => _x( 'Back to all Group types', 'Group type taxonomy back to items label', 'buddypress' ),
     2530                        // Specific to BuddyPress.
     2531                        'bp_type_id_label'              => _x( 'Group Type ID', 'BP Member type ID label', 'buddypress' ),
     2532                        'bp_type_id_description'        => _x( 'Enter a lower-case string without spaces or special characters (used internally to identify the group type).', 'BP Group type ID description', 'buddypress' ),
     2533                        'bp_type_show_in_create_screen' => _x( 'Add to Available Types on Create Screen', 'BP Group type show in create screen', 'buddypress' ),
     2534                        'bp_type_show_in_list'          => _x( 'Include when Group Types are Listed for a Group', 'BP Group type show in list', 'buddypress' ),
     2535                )
     2536        );
     2537}
     2538
     2539/**
     2540 * Returns arguments used by the Group type taxonomy.
     2541 *
     2542 * @since 7.0.0
     2543 *
     2544 * @return array
     2545 */
     2546function bp_get_group_type_tax_args() {
     2547
     2548        /**
     2549         * Filters Group type taxonomy args.
     2550         *
     2551         * @since 7.0.0
     2552         *
     2553         * @param array $value Associative array (key => arg).
     2554         */
     2555        return apply_filters(
     2556                'bp_get_group_type_tax_args',
     2557                array_merge(
     2558                        array(
     2559                                'description' => _x( 'BuddyPress Group types', 'Group type taxonomy description', 'buddypress' ),
     2560                                'labels'      => array_merge( bp_get_group_type_tax_labels(), bp_get_taxonomy_common_labels() ),
     2561                        ),
     2562                        bp_get_taxonomy_common_args()
     2563                )
     2564        );
     2565}
     2566
     2567/**
     2568 * Register the Group Types taxonomy.
     2569 *
     2570 * @since 7.0.0
     2571 *
     2572 * @param array $taxonomies BuddyPress default taxonomies.
     2573 * @return array            BuddyPress default taxonomies.
     2574 */
     2575function bp_groups_register_group_type_taxonomy( $taxonomies = array() ) {
     2576        return array_merge(
     2577                $taxonomies,
     2578                array(
     2579                        // Group Type.
     2580                        bp_get_group_type_tax_name() => array(
     2581                                'object'    => 'bp_group',
     2582                                'component' => 'groups',
     2583                                'args'      => bp_get_group_type_tax_args(),
     2584                        ),
     2585                )
     2586        );
     2587}
     2588add_filter( 'bp_get_default_taxonomies', 'bp_groups_register_group_type_taxonomy', 1 );
     2589
     2590/**
    24672591 * Fire the 'bp_groups_register_group_types' action.
    24682592 *
     
    24782602}
    24792603add_action( 'bp_register_taxonomies', 'bp_groups_register_group_types' );
     2604
     2605/**
     2606 * Extend generic Type metadata schema to match Group Type needs.
     2607 *
     2608 * @since 7.0.0
     2609 *
     2610 * @param array  $schema   The generic Type metadata schema.
     2611 * @param string $taxonomy The taxonomy name the schema applies to.
     2612 * @return array           The Group Type metadata schema.
     2613 */
     2614function bp_get_group_type_metadata_schema( $schema = array(), $taxonomy = '' ) {
     2615        if ( bp_get_group_type_tax_name() === $taxonomy ) {
     2616                if ( isset( $schema['bp_type_has_directory']['description'] ) ) {
     2617                        $schema['bp_type_has_directory']['description'] = __( 'Add a list of groups matching the member type available on the Groups Directory page (e.g. site.url/groups/type/ninja/).', 'buddypress' );
     2618                }
     2619
     2620                if ( isset( $schema['bp_type_directory_slug']['description'] ) ) {
     2621                        $schema['bp_type_directory_slug']['description'] = __( 'If you want to use a slug that is different from the Group Type ID above, enter it here.', 'buddypress' );
     2622                }
     2623
     2624                $schema = array_merge(
     2625                        $schema,
     2626                        array(
     2627                                'bp_type_show_in_create_screen' => array(
     2628                                        'description'       => __( 'Include this group type during group creation and when a group administrator is on the group’s “Manage > Settings” page.', 'buddypress' ),
     2629                                        'type'              => 'boolean',
     2630                                        'single'            => true,
     2631                                        'sanitize_callback' => 'absint',
     2632                                ),
     2633                                'bp_type_show_in_list'          => array(
     2634                                        'description'       => __( 'Include this group type when group types are listed, like in the group header.', 'buddypress' ),
     2635                                        'type'              => 'boolean',
     2636                                        'single'            => true,
     2637                                        'sanitize_callback' => 'absint',
     2638                                ),
     2639                        )
     2640                );
     2641        }
     2642
     2643        return $schema;
     2644}
     2645add_filter( 'bp_get_type_metadata_schema', 'bp_get_group_type_metadata_schema', 1, 2 );
     2646
     2647/**
     2648 * Registers the Group type metadata.
     2649 *
     2650 * @since 7.0.0
     2651 */
     2652function bp_register_group_type_metadata() {
     2653        $type_taxonomy = bp_get_group_type_tax_name();
     2654
     2655        foreach ( bp_get_type_metadata_schema( false, $type_taxonomy ) as $meta_key => $meta_args ) {
     2656                bp_register_type_meta( $type_taxonomy, $meta_key, $meta_args );
     2657        }
     2658}
     2659add_action( 'bp_register_type_metadata', 'bp_register_group_type_metadata', 11 );
    24802660
    24812661/**
     
    25262706                'create_screen_checked' => false,
    25272707                'labels'                => array(),
     2708                'code'                  => true,
     2709                'db_id'                 => 0,
    25282710        ), 'register_group_type' );
    25292711
     
    26182800        $types = wp_filter_object_list( $types, $args, $operator );
    26192801
     2802        // Merge with types available into the database.
     2803        if ( ! isset( $args['code'] ) || true !== $args['code'] ) {
     2804                $types = bp_get_taxonomy_types( bp_get_group_type_tax_name(), $types );
     2805        }
     2806
    26202807        /**
    26212808         * Filters the array of group type objects.
     
    26562843        return $types[ $group_type ];
    26572844}
     2845
     2846/**
     2847 * Only gets the group types registered by code.
     2848 *
     2849 * @since 7.0.0
     2850 *
     2851 * @return array The group types registered by code.
     2852 */
     2853function bp_get_group_types_registered_by_code() {
     2854        return bp_groups_get_group_types(
     2855                array(
     2856                        'code' => true,
     2857                ),
     2858                'objects'
     2859        );
     2860}
     2861add_filter( bp_get_group_type_tax_name() . '_registered_by_code', 'bp_get_group_types_registered_by_code' );
     2862
     2863/**
     2864 * Generates missing metadata for a type registered by code.
     2865 *
     2866 * @since 7.0.0
     2867 *
     2868 * @return array The group type metadata.
     2869 */
     2870function bp_set_registered_by_code_group_type_metadata( $metadata = array(), $type = '' ) {
     2871        $group_type = bp_groups_get_group_type_object( $type );
     2872
     2873        foreach ( get_object_vars( $group_type ) as $object_key => $object_value ) {
     2874                if ( 'labels' === $object_key ) {
     2875                        foreach ( $object_value as $label_key => $label_value ) {
     2876                                $metadata[ 'bp_type_' . $label_key ] = $label_value;
     2877                        }
     2878                } elseif ( ! in_array( $object_key, array( 'name', 'code', 'db_id' ), true ) ) {
     2879                        $metadata[ 'bp_type_' . $object_key ] = $object_value;
     2880                }
     2881        }
     2882
     2883        /**
     2884         * Save metadata into database to avoid generating metadata
     2885         * each time a type is listed into the Types Admin screen.
     2886         */
     2887        if ( isset( $group_type->db_id ) && $group_type->db_id ) {
     2888                bp_update_type_metadata( $group_type->db_id, bp_get_group_type_tax_name(), $metadata );
     2889        }
     2890
     2891        return $metadata;
     2892}
     2893add_filter( bp_get_group_type_tax_name() . '_set_registered_by_code_metada', 'bp_set_registered_by_code_group_type_metadata', 10, 2 );
     2894
     2895/**
     2896 * Insert group types registered by code not yet saved into the database as WP Terms.
     2897 *
     2898 * @since 7.0.0
     2899 */
     2900function bp_insert_group_types_registered_by_code() {
     2901        $all_types     = bp_groups_get_group_types( array(), 'objects' );
     2902        $unsaved_types = wp_filter_object_list( $all_types, array( 'db_id' => 0 ), 'and', 'name' );
     2903
     2904        if ( $unsaved_types ) {
     2905                foreach ( $unsaved_types as $type_name ) {
     2906                        bp_insert_term(
     2907                                $type_name,
     2908                                bp_get_group_type_tax_name(),
     2909                                array(
     2910                                        'slug' => $type_name,
     2911                                )
     2912                        );
     2913                }
     2914        }
     2915}
     2916add_action( bp_get_group_type_tax_name() . '_add_form', 'bp_insert_group_types_registered_by_code', 1 );
    26582917
    26592918/**
     
    26862945        }
    26872946
    2688         $retval = bp_set_object_terms( $group_id, $group_type, 'bp_group_type', $append );
     2947        $retval = bp_set_object_terms( $group_id, $group_type, bp_get_group_type_tax_name(), $append );
    26892948
    26902949        // Bust the cache if the type has been updated.
     
    27112970 *
    27122971 * @since 2.6.0
     2972 * @since 7.0.0 Adds the `$use_db` parameter.
    27132973 *
    27142974 * @param int  $group_id ID of the group.
    27152975 * @param bool $single   Optional. Whether to return a single type string. If multiple types are found
    27162976 *                       for the group, the oldest one will be returned. Default: true.
     2977 * @param bool $use_db   Optional. Whether to request all group types or only the ones registered by code.
     2978 *                       Default: true.
    27172979 * @return string|array|bool On success, returns a single group type (if `$single` is true) or an array of group
    27182980 *                           types (if `$single` is false). Returns false on failure.
    27192981 */
    2720 function bp_groups_get_group_type( $group_id, $single = true ) {
     2982function bp_groups_get_group_type( $group_id, $single = true, $use_db = true ) {
    27212983        $types = wp_cache_get( $group_id, 'bp_groups_group_type' );
    27222984
    27232985        if ( false === $types ) {
    2724                 $raw_types = bp_get_object_terms( $group_id, 'bp_group_type' );
     2986                $raw_types = bp_get_object_terms( $group_id, bp_get_group_type_tax_name() );
    27252987
    27262988                if ( ! is_wp_error( $raw_types ) ) {
     
    27362998                        wp_cache_set( $group_id, $types, 'bp_groups_group_type' );
    27372999                }
     3000        }
     3001
     3002        if ( false === $use_db && $types ) {
     3003                $registred_by_code = bp_get_group_types_registered_by_code();
     3004                $ctype_names       = wp_list_pluck( $registred_by_code, 'name' );
     3005                $types             = array_intersect( $types, $ctype_names );
    27383006        }
    27393007
     
    27793047        }
    27803048
    2781         $deleted = bp_remove_object_terms( $group_id, $group_type, 'bp_group_type' );
     3049        $deleted = bp_remove_object_terms( $group_id, $group_type, bp_get_group_type_tax_name() );
    27823050
    27833051        // Bust the case, if the type has been removed.
Note: See TracChangeset for help on using the changeset viewer.