Skip to:
Content

BuddyPress.org

Changeset 12983


Ignore:
Timestamp:
07/01/2021 08:54:10 PM (5 years ago)
Author:
imath
Message:

BP Nouveau: improve the update() method of the BP_Buttons_Group

  • Create a new method to add buttons.
  • Use it into the update() method to allow it to add new buttons to pre-existing ones.

Props sbrajesh

Fixes #8499

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-templates/bp-nouveau/includes/classes.php

    r12976 r12983  
    44 *
    55 * @since 3.0.0
    6  * @version 3.1.0
     6 * @version 9.0.0
    77 */
    88
     
    4545        public function __construct( $args = array() ) {
    4646                foreach ( $args as $arg ) {
    47                         $r = bp_parse_args(
    48                                 (array) $arg,
    49                                 array(
    50                                         'id'                => '',
    51                                         'position'          => 99,
    52                                         'component'         => '',
    53                                         'must_be_logged_in' => true,
    54                                         'block_self'        => false,
    55                                         'parent_element'    => false,
    56                                         'parent_attr'       => array(),
    57                                         'button_element'    => 'a',
    58                                         'button_attr'       => array(),
    59                                         'link_text'         => '',
    60                                 ),
    61                                 'buttons_group_constructor'
    62                         );
    63 
    64                         // Just don't set the button if a param is missing
    65                         if ( empty( $r['id'] ) || empty( $r['component'] ) || empty( $r['link_text'] ) ) {
    66                                 continue;
    67                         }
    68 
    69                         $r['id'] = sanitize_key( $r['id'] );
    70 
    71                         // If the button already exist don't add it
    72                         if ( isset( $this->group[ $r['id'] ] ) ) {
    73                                 continue;
    74                         }
    75 
    76                         /*
    77                          * If, in bp_nouveau_get_*_buttons(), we pass through a false value for 'parent_element'
    78                          * but we have attributtes for it in the array, let's default to setting a div.
    79                          *
    80                          * Otherwise, the original false value will be passed through to BP buttons.
    81                          * @todo: this needs review, probably trying to be too clever
    82                          */
    83                         if ( ( ! empty( $r['parent_attr'] ) ) && false === $r['parent_element'] ) {
    84                                 $r['parent_element'] = 'div';
    85                         }
    86 
    87                         $this->group[ $r['id'] ] = $r;
     47                        $this->add( $arg );
    8848                }
    8949        }
     
    173133                                        'buttons_group_update'
    174134                                );
    175                         }
    176                 }
     135                        } else {
     136                                $this->add( $params );
     137                        }
     138                }
     139        }
     140
     141        /**
     142         * Adds a button.
     143         *
     144         * @since 9.0.0
     145         *
     146         * @param array $args Required. See the __constructor for a description of this argument.
     147         * @return bool true on success, false on failure to add.
     148         */
     149        private function add( $args ) {
     150                $r = bp_parse_args(
     151                        (array) $args,
     152                        array(
     153                                'id'                => '',
     154                                'position'          => 99,
     155                                'component'         => '',
     156                                'must_be_logged_in' => true,
     157                                'block_self'        => false,
     158                                'parent_element'    => false,
     159                                'parent_attr'       => array(),
     160                                'button_element'    => 'a',
     161                                'button_attr'       => array(),
     162                                'link_text'         => '',
     163                        ),
     164                        'buttons_group_constructor'
     165                );
     166
     167                // Just don't set the button if a param is missing
     168                if ( empty( $r['id'] ) || empty( $r['component'] ) || empty( $r['link_text'] ) ) {
     169                        return false;
     170                }
     171
     172                $r['id'] = sanitize_key( $r['id'] );
     173
     174                // If the button already exist don't add it
     175                if ( isset( $this->group[ $r['id'] ] ) ) {
     176                        return false;
     177                }
     178
     179                /*
     180                 * If, in bp_nouveau_get_*_buttons(), we pass through a false value for 'parent_element'
     181                 * but we have attributtes for it in the array, let's default to setting a div.
     182                 *
     183                 * Otherwise, the original false value will be passed through to BP buttons.
     184                 * @todo: this needs review, probably trying to be too clever
     185                 */
     186                if ( ( ! empty( $r['parent_attr'] ) ) && false === $r['parent_element'] ) {
     187                        $r['parent_element'] = 'div';
     188                }
     189
     190                $this->group[ $r['id'] ] = $r;
     191                return true;
    177192        }
    178193}
Note: See TracChangeset for help on using the changeset viewer.