Make WordPress Core

Opened 2 months ago

Last modified 2 months ago

#60579 new defect (bug)

Notice: Undefined index: name in /wp-includes/class-wp-roles.php on line 293

Reported by: mrsize's profile mrsize Owned by:
Milestone: Awaiting Review Priority: normal
Severity: minor Version: 6.4.3
Component: General Keywords: has-patch
Focuses: Cc:

Description

http://servtest.ovh/notice-wp.jpg

I have some notices on my dashboard, and fix the bug by modifying the function in core file, line 279 :

<?php
        /**
         * Initializes all of the available roles.
         *
         * @since 4.9.0
         */
        public function init_roles() {
    if ( empty( $this->roles ) ) {
        return;
    }

    $this->role_objects = array();
    $this->role_names   = array();
    foreach ( array_keys( $this->roles ) as $role ) {
        // Vérifier si 'name' est défini pour le rôle actuel
        if ( isset( $this->roles[ $role ]['name'] ) ) {
            $this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] );
            $this->role_names[ $role ]   = $this->roles[ $role ]['name'];
        } else {
            // Gérer le cas où 'name' n'est pas défini (éventuellement définir une valeur par défaut)
            $this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] );
            $this->role_names[ $role ]   = 'Nom par défaut';
        }
    }

    /**
     * Fires after the roles have been initialized, allowing plugins to add their own roles.
     *
     * @since 4.7.0
     *
     * @param WP_Roles $wp_roles A reference to the WP_Roles object.
     */
    do_action( 'wp_roles_init', $this );
}

Attachments (2)

notice-wp.jpg (93.9 KB) - added by mrsize 2 months ago.
screenshot of the notice
6144.diff (646 bytes) - added by jucaduca 2 months ago.

Download all attachments as: .zip

Change History (5)

@mrsize
2 months ago

screenshot of the notice

This ticket was mentioned in Slack in #core-test by ankit-k-gupta. View the logs.


2 months ago

This ticket was mentioned in PR #6144 on WordPress/wordpress-develop by @jucaduca.


2 months ago
#2

  • Keywords has-patch added; needs-patch removed

We have a problem when we register roles without 'name', as shown in the image, an error is generated.

https://github.com/WordPress/wordpress-develop/assets/252078/6ae6f2a8-ef91-4dab-b6ec-b30af1b53a77

I don't like the idea of defining a default name, but as tiket suggested, I followed the fix.

I will make other proposals below:

1: ignore roles without names

foreach ( array_keys( $this->roles ) as $role ) {
        if( ! isset( $this->roles[ $role ]['name'] ) ){
                continue;
        }

        $this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] );
        $this->role_names[ $role ]   = $this->roles[ $role ]['name'];
}
`

2: set default name at the beginning

foreach ( array_keys( $this->roles ) as $role ) {
        if( ! isset( $this->roles[ $role ]['name'] ) ){
                $this->roles[ $role ]['name'] = 'undefined'
        }

        $this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] );
        $this->role_names[ $role ]   = $this->roles[ $role ]['name'];
}
`

I don't have context on what the best way is, if anyone could help me I would be very grateful :bowtie:

Trac ticket: https://core.trac.wordpress.org/ticket/60579

@jucaduca
2 months ago

This ticket was mentioned in Slack in #core-test by krupajnanda. View the logs.


2 months ago

Note: See TracTickets for help on using tickets.