Skip to:
Content

BuddyPress.org

Changeset 12801


Ignore:
Timestamp:
11/20/2020 05:47:32 PM (6 years ago)
Author:
imath
Message:

Group Types: improve the bp_group_type_list() template tag

  • Make sure to avoid displaying empty links if the Group Types object’s has_directory property is set to false.
  • To improve consistency with the Groups directory message informing it is filtered according to a Group Type, use singular Group Type names for the texts used into the list.
  • Edit The $r['label'] argument of the bp_get_group_type_list() function so that it accepts an array containing the plural & singular labels to use according to the Group's number of group types it is assigned to.
  • Ensure backward compatibility in case developers are still using a string for this $r['label'] argument.

Fixes #8401

Location:
trunk/src
Files:
3 edited

Legend:

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

    r12789 r12801  
    191191                }
    192192
    193                 return sprintf( '<a href="%s">%s</a>', esc_url( bp_get_group_type_directory_permalink( $group_type ) ), bp_groups_get_group_type_object( $group_type )->labels['name'] );
     193                $group_type_object = bp_groups_get_group_type_object( $group_type );
     194
     195                if ( ! isset( $group_type_object->labels['name'] ) ) {
     196                        return '';
     197                }
     198
     199                $group_type_text = $group_type_object->labels['name'];
     200                if ( isset( $group_type_object->labels['singular_name'] ) && $group_type_object->labels['singular_name'] ) {
     201                        $group_type_text = $group_type_object->labels['singular_name'];
     202                }
     203
     204                if ( empty( $group_type_object->has_directory ) ) {
     205                        return esc_html( $group_type_text );
     206                }
     207
     208                return sprintf(
     209                        '<a href="%s">%s</a>',
     210                        esc_url( bp_get_group_type_directory_permalink( $group_type ) ),
     211                        esc_html( $group_type_text )
     212                );
    194213        }
    195214
    … …  
    207226         *
    208227         * @since 2.7.0
     228         * @since 7.0.0 The `$r['label']` argument now also accept an array containing the
     229         *              plural & singular labels to use according to the Group's number of
     230         *              group types it is assigned to.
    209231         *
    210232         * @param int $group_id Group ID. Defaults to current group ID if on a group page.
    211233         * @param array|string $r {
    212234         *     Array of parameters. All items are optional.
    213          *     @type string $parent_element Element to wrap around the list. Defaults to 'p'.
    214          *     @type array  $parent_attr    Element attributes for parent element. Defaults to
    215          *                                  array( 'class' => 'bp-group-type-list' ).
    216          *     @type string $label          Label to add before the list. Defaults to 'Group Types:'.
    217          *     @type string $label_element  Element to wrap around the label. Defaults to 'strong'.
    218          *     @type array  $label_attr     Element attributes for label element. Defaults to array().
    219          *     @type bool   $show_all       Whether to show all registered group types. Defaults to 'false'. If
    220          *                                 'false', only shows group types with the 'show_in_list' parameter set to
    221          *                                  true. See bp_groups_register_group_type() for more info.
     235         *     @type string       $parent_element Element to wrap around the list. Defaults to 'p'.
     236         *     @type array        $parent_attr    Element attributes for parent element. Defaults to
     237         *                                        array( 'class' => 'bp-group-type-list' ).
     238         *     @type string|array $label          Plural and singular labels to add before the list. Defaults to
     239         *                                        array( 'plural' => 'Group Types:', 'singular' => 'Group Type:' ).
     240         *     @type string       $label_element  Element to wrap around the label. Defaults to 'strong'.
     241         *     @type array        $label_attr     Element attributes for label element. Defaults to array().
     242         *     @type bool         $show_all       Whether to show all registered group types. Defaults to 'false'. If
     243         *                                       'false', only shows group types with the 'show_in_list' parameter set to
     244         *                                        true. See bp_groups_register_group_type() for more info.
    222245         * }
    223246         * @return string
    … …  
    235258                                        'class' => 'bp-group-type-list',
    236259                                ),
    237                                 'label'             => __( 'Group Types:', 'buddypress' ),
     260                                'label'             => array(),
    238261                                'label_element'     => 'strong',
    239262                                'label_attr'        => array(),
    … …  
    245268                );
    246269
     270                // Should the label be output?
     271                $has_label = ! empty( $r['label'] );
     272
     273                // Ensure backward compatibility in case developers are still using a string.
     274                if ( ! is_array( $r['label'] ) ) {
     275                        $r['label'] = array(
     276                                'plural' => __( 'Group Types:', 'buddypress' ),
     277                        );
     278                }
     279
     280                $labels = wp_parse_args(
     281                        $r['label'],
     282                        array(
     283                                'plural'   => __( 'Group Types:', 'buddypress' ),
     284                                'singular' => __( 'Group Type:', 'buddypress' ),
     285                        )
     286                );
     287
    247288                $retval = '';
    248289
    … …  
    257298
    258299                        $before = $after = $label = '';
     300                        $count  = count( $types );
     301
     302                        if ( 1 === $count ) {
     303                                $label_text = $labels['singular'];
     304                        } else {
     305                                $label_text = $labels['plural'];
     306                        }
    259307
    260308                        // Render parent element.
    … …  
    262310                                $parent_elem = new BP_Core_HTML_Element( array(
    263311                                        'element' => $r['parent_element'],
    264                                         'attr'    => $r['parent_attr']
     312                                        'attr'    => $r['parent_attr'],
    265313                                ) );
    266314
    … …  
    275323                                        'element'    => $r['label_element'],
    276324                                        'attr'       => $r['label_attr'],
    277                                         'inner_html' => esc_html( $r['label'] )
     325                                        'inner_html' => esc_html( $label_text ),
    278326                                ) );
    279327                                $label = $label->contents() . ' ';
    280328
    281329                        // No element, just the label.
    282                         } else {
    283                                 $label = esc_html( $r['label'] );
     330                        } elseif ( $has_label ) {
     331                                $label = esc_html( $label_text );
    284332                        }
    285333
  • trunk/src/bp-templates/bp-nouveau/buddypress/groups/single/cover-image-header.php

    r12791 r12801  
    4747                                        bp_get_group_id(),
    4848                                        array(
    49                                                 'label'        => __( 'Group Types', 'buddypress' ),
     49                                                'label'        => array(
     50                                                        'plural'   => __( 'Group Types', 'buddypress' ),
     51                                                        'singular' => __( 'Group Type', 'buddypress' ),
     52                                                ),
    5053                                                'list_element' => 'span',
    5154                                        )
  • trunk/src/bp-templates/bp-nouveau/buddypress/groups/single/group-header.php

    r12791 r12801  
    4444                bp_get_group_id(),
    4545                array(
    46                         'label'        => __( 'Group Types', 'buddypress' ),
     46                        'label'        => array(
     47                                'plural'   => __( 'Group Types', 'buddypress' ),
     48                                'singular' => __( 'Group Type', 'buddypress' ),
     49                        ),
    4750                        'list_element' => 'span',
    4851                )
Note: See TracChangeset for help on using the changeset viewer.