Skip to:
Content

BuddyPress.org

Changeset 12731


Ignore:
Timestamp:
09/21/2020 01:36:43 AM (4 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

Location:
trunk
Files:
5 edited

Legend:

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

    r12725 r12731  
    1515
    1616// Include WP's list table class.
    17 if ( !class_exists( 'WP_List_Table' ) ) require( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
     17if ( ! class_exists( 'WP_List_Table' ) ) {
     18    require ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
     19}
    1820
    1921// The per_page screen option. Has to be hooked in extremely early.
    20 if ( is_admin() && ! empty( $_REQUEST['page'] ) && 'bp-groups' == $_REQUEST['page'] )
     22if ( is_admin() && ! empty( $_REQUEST['page'] ) && 'bp-groups' == $_REQUEST['page'] ) {
    2123    add_filter( 'set-screen-option', 'bp_groups_admin_screen_options', 10, 3 );
     24}
    2225
    2326/**
     
    4245}
    4346add_action( bp_core_admin_hook(), 'bp_groups_add_admin_menu' );
     47
     48/**
     49 * Redirects the user on the Goups network admin screen when BuddyPress is network activated.
     50 *
     51 * @since 7.0.0
     52 */
     53function bp_group_site_admin_network_admin_redirect() {
     54    wp_safe_redirect( add_query_arg( 'page', 'bp-groups', network_admin_url( 'admin.php' ) ) );
     55    exit();
     56}
     57
     58/**
     59 * Create Groups submenu to manage BuddyPress types.
     60 *
     61 * @since 7.0.0
     62 */
     63function bp_groups_admin_types_menu() {
     64    if ( ! bp_is_root_blog() ) {
     65        return;
     66    }
     67
     68    if ( bp_is_network_activated() && is_network_admin() ) {
     69        // Adds a 'bp-groups' submenu to go to the root blog Group types screen.
     70        $group_type_admin_url = add_query_arg( 'taxonomy', 'bp_group_type', get_admin_url( bp_get_root_blog_id(), 'edit-tags.php' ) );
     71        add_submenu_page(
     72            'bp-groups',
     73            __( 'Group types', 'buddypress' ),
     74            __( 'Group types', 'buddypress' ),
     75            'bp_moderate',
     76            esc_url( $group_type_admin_url )
     77        );
     78    } elseif ( ! is_network_admin() ) {
     79        if ( is_multisite() ) {
     80            // Adds a 'bp-groups' menu to the root blog menu.
     81            $redirect_hook = add_menu_page(
     82                _x( 'Groups', 'Admin Groups page title', 'buddypress' ),
     83                _x( 'Groups', 'Admin Groups menu', 'buddypress' ),
     84                'bp_moderate',
     85                'bp-groups',
     86                '__return_empty_string',
     87                'div'
     88            );
     89
     90            add_action( "load-{$redirect_hook}", 'bp_group_site_admin_network_admin_redirect' );
     91        }
     92
     93        // Add the submenu to manage Group Types.
     94        add_submenu_page(
     95            'bp-groups',
     96            __( 'Group types', 'buddypress' ),
     97            __( 'Group types', 'buddypress' ),
     98            'bp_moderate',
     99            basename( add_query_arg( 'taxonomy', 'bp_group_type', bp_get_admin_url( 'edit-tags.php' ) ) )
     100        );
     101    }
     102}
     103add_action( 'bp_admin_menu', 'bp_groups_admin_types_menu' );
    44104
    45105/**
     
    13801440}
    13811441add_action( bp_core_admin_hook(), 'bp_groups_admin_groups_type_change_notice' );
     1442
     1443/**
     1444 * Checks whether a group type already exists.
     1445 *
     1446 * @since 7.0.0
     1447 *
     1448 * @param  boolean $exists  True if the group type already exists. False otherwise.
     1449 * @param  string  $type_id The group type identifier.
     1450 * @return boolean          True if the group type already exists. False otherwise.
     1451 */
     1452function bp_groups_type_admin_type_exists( $exists = false, $type_id = '' ) {
     1453    if ( ! $type_id ) {
     1454        return $exists;
     1455    }
     1456
     1457    return ! is_null( bp_groups_get_group_type_object( $type_id ) );
     1458}
     1459add_filter( bp_get_group_type_tax_name() . '_check_existing_type', 'bp_groups_type_admin_type_exists', 1, 2 );
     1460
     1461/**
     1462 * Set the feedback messages for the Group Types Admin actions.
     1463 *
     1464 * @since 7.0.0
     1465 *
     1466 * @param array  $messages The feedback messages.
     1467 * @return array           The feedback messages including the ones for the Group Types Admin actions.
     1468 */
     1469function bp_groups_type_admin_updated_messages( $messages = array() ) {
     1470    $type_taxonomy = bp_get_group_type_tax_name();
     1471
     1472    $messages[ $type_taxonomy ] = array(
     1473        0  => '',
     1474        1  => __( 'Please define the Group Type ID field.', 'buddypress' ),
     1475        2  => __( 'Group type successfully added.', 'buddypress' ),
     1476        3  => __( 'Sorry, there was an error and the Group type wasn’t added.', 'buddypress' ),
     1477        // The following one needs to be != 5.
     1478        4  => __( 'Group type successfully updated.', 'buddypress' ),
     1479        5  => __( 'Sorry, this Group type already exists.', 'buddypress' ),
     1480        6  => __( 'Sorry, the Group type was not deleted: it does not exist.', 'buddypress' ),
     1481        7  => __( 'Sorry, This Group type is registered using code, deactivate the plugin or remove the custom code before trying to delete it again.', 'buddypress' ),
     1482        8  => __( 'Sorry, there was an error while trying to delete this Group type.', 'buddypress' ),
     1483        9  => __( 'Group type successfully deleted.', 'buddypress' ),
     1484        10 => __( 'Group type could not be updated due to missing required information.', 'buddypress' ),
     1485    );
     1486
     1487    return $messages;
     1488}
     1489add_filter( 'term_updated_messages', 'bp_groups_type_admin_updated_messages' );
  • 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.
  • trunk/src/bp-groups/classes/class-bp-groups-component.php

    r12607 r12731  
    925925     *
    926926     * @since 2.6.0
     927     * @since 7.0.0 The Group Type taxonomy is registered using the `bp_groups_register_group_type_taxonomy()` function.
    927928     */
    928929    public function register_taxonomies() {
    929         // Group Type.
    930         register_taxonomy( 'bp_group_type', 'bp_group', array(
    931             'public' => false,
    932         ) );
     930
     931        // Just let BP Component fire 'bp_groups_register_taxonomies'.
     932        return parent::register_taxonomies();
    933933    }
    934934
  • trunk/src/bp-groups/classes/class-bp-groups-group.php

    r12430 r12731  
    17471747        $tax_query = new WP_Tax_Query( array(
    17481748            array(
    1749                 'taxonomy' => 'bp_group_type',
     1749                'taxonomy' => bp_get_group_type_tax_name(),
    17501750                'field'    => 'name',
    17511751                'operator' => $operator,
     
    17541754        ) );
    17551755
    1756         $site_id  = bp_get_taxonomy_term_site_id( 'bp_group_type' );
     1756        $site_id  = bp_get_taxonomy_term_site_id( bp_get_group_type_tax_name() );
    17571757        $switched = false;
    17581758        if ( $site_id !== get_current_blog_id() ) {
  • trunk/tests/phpunit/testcases/groups/types.php

    r11739 r12731  
    247247    }
    248248
    249     public function test_groups_get_type_should_not_return_unregistered_types() {
     249    public function test_groups_registered_by_code_group_type_should_not_return_unregistered_types() {
    250250        $g = self::factory()->group->create( array( 'creator_id' => self::$u1 ) );
    251251        bp_groups_register_group_type( 'foo' );
     
    255255        bp_set_object_terms( $g, 'ugh', 'bp_group_type', true );
    256256
    257         $type = bp_groups_get_group_type( $g, false );
     257        $type = bp_groups_get_group_type( $g, false, false );
    258258        $this->assertEquals( array( 'foo' ), $type );
    259259    }
Note: See TracChangeset for help on using the changeset viewer.