Skip to:
Content

bbPress.org

Changeset 6771


Ignore:
Timestamp:
01/23/2018 10:24:15 PM (7 years ago)
Author:
johnjamesjacoby
Message:

Admin Notices: remove create_function() usage from tools feedback.

This change removes the last remaining deprecated function call for full PHP 7.2 support, while also making admin area notices a bit more flexible in the process.

Notices are now stored in an bbpress()->admin->notices array, and are output to the page via the bbp_admin_notices sub-action. This ensures that they'll only be made visible when bbPress is active, and keeps them contained to places where bbPress is already hooked in.

It was also previously possible for any user to dismiss the database upgrade notice, even if they were not capable of seeing it, so this is now fixed as well.

Location:
trunk/src/includes/admin
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/admin/classes/class-bbp-admin.php

    r6734 r6771  
    7676    public $tools = array();
    7777
     78    /** Notices ***************************************************************/
     79
     80    /**
     81     * @var array Array of notices to output on 'bbp_admin_notices' action
     82     */
     83    public $notices = array();
     84
    7885    /** Functions *************************************************************/
    7986
     
    150157        /** General Actions ***************************************************/
    151158
    152         add_action( 'bbp_admin_init',              array( $this, 'setup_notices'           ) );
    153         add_action( 'bbp_admin_init',              array( $this, 'hide_notices'            ) );
     159        add_action( 'bbp_activation',              array( $this, 'new_install'             ) ); // Add menu item to settings menu
    154160        add_action( 'bbp_admin_menu',              array( $this, 'admin_menus'             ) ); // Add menu item to settings menu
    155         add_action( 'bbp_admin_head',              array( $this, 'admin_head'              ) ); // Add some general styling to the admin area
    156         add_action( 'bbp_admin_notices',           array( $this, 'activation_notice'       ) ); // Add notice if not using a bbPress theme
     161        add_action( 'bbp_admin_head',              array( $this, 'admin_head'              ) ); // Add general styling to the admin area
    157162        add_action( 'bbp_register_admin_style',    array( $this, 'register_admin_style'    ) ); // Add green admin style
    158163        add_action( 'bbp_register_admin_settings', array( $this, 'register_admin_settings' ) ); // Add settings
    159         add_action( 'bbp_activation',              array( $this, 'new_install'             ) ); // Add menu item to settings menu
    160164        add_action( 'admin_enqueue_scripts',       array( $this, 'enqueue_styles'          ) ); // Add enqueued CSS
    161165        add_action( 'admin_enqueue_scripts',       array( $this, 'enqueue_scripts'         ) ); // Add enqueued JS
     166
     167        /** Notices ***********************************************************/
     168
     169        add_action( 'bbp_admin_init',    array( $this, 'setup_notices'  ) );
     170        add_action( 'bbp_admin_init',    array( $this, 'hide_notices'   ) );
     171        add_action( 'bbp_admin_notices', array( $this, 'output_notices' ) );
    162172
    163173        /** Ajax **************************************************************/
     
    215225
    216226            // Add tools feedback
    217             bbp_admin_tools_feedback( $message, 'notice-bbpress', false );
     227            $this->add_notice( $message, 'notice-bbpress', false );
    218228        }
    219229    }
     
    228238        // Bail if not hiding a notice
    229239        if ( empty( $_GET['bbp-hide-notice'] ) ) {
     240            return;
     241        }
     242
     243        // Bail if user cannot visit upgrade page (cannot clear notice either!)
     244        if ( current_user_can( 'bbp_tools_upgrade_page' ) ) {
    230245            return;
    231246        }
     
    242257                break;
    243258        }
     259    }
     260
     261    /**
     262     * Output all admin area notices
     263     *
     264     * @since 2.6.0 bbPress (r6771)
     265     */
     266    public function output_notices() {
     267
     268        // Bail if no notices
     269        if ( empty( $this->notices ) || ! is_array( $this->notices ) ) {
     270            return;
     271        }
     272
     273        // Start an output buffer
     274        ob_start();
     275
     276        // Loop through notices, and add them to buffer
     277        foreach ( $this->notices as $notice ) {
     278            echo $notice;
     279        }
     280
     281        // Output the current buffer
     282        echo ob_get_clean();
     283    }
     284
     285    /**
     286     * Add a notice to the notices array
     287     *
     288     * @since 2.6.0 bbPress (r6771)
     289     *
     290     * @param string|WP_Error $message        A message to be displayed or {@link WP_Error}
     291     * @param string          $class          Optional. A class to be added to the message div
     292     * @param bool            $is_dismissible Optional. True to dismiss, false to persist
     293     *
     294     * @return void
     295     */
     296    public function add_notice( $message, $class = false, $is_dismissible = true ) {
     297
     298        // One message as string
     299        if ( is_string( $message ) ) {
     300            $message       = '<p>' . $message . '</p>';
     301            $default_class ='updated';
     302
     303        // Messages as objects
     304        } elseif ( is_wp_error( $message ) ) {
     305            $errors  = $message->get_error_messages();
     306
     307            switch ( count( $errors ) ) {
     308                case 0:
     309                    return false;
     310
     311                case 1:
     312                    $message = '<p>' . $errors[0] . '</p>';
     313                    break;
     314
     315                default:
     316                    $message = '<ul>' . "\n\t" . '<li>' . implode( '</li>' . "\n\t" . '<li>', $errors ) . '</li>' . "\n" . '</ul>';
     317                    break;
     318            }
     319
     320            $default_class = 'is-error';
     321
     322        // Message is an unknown format, so bail
     323        } else {
     324            return false;
     325        }
     326
     327        // CSS Classes
     328        $classes = ! empty( $class )
     329            ? array( $class )
     330            : array( $default_class );
     331
     332        // Add dismissible class
     333        if ( ! empty( $is_dismissible ) ) {
     334            array_push( $classes, 'is-dismissible' );
     335        }
     336
     337        // Assemble the message
     338        $message = '<div id="message" class="notice ' . implode( ' ', array_map( 'esc_attr', $classes ) ) . '">' . $message . '</div>';
     339        $message = str_replace( "'", "\'", $message );
     340
     341        // Avoid malformed notices variable
     342        if ( ! is_array( $this->notices ) ) {
     343            $this->notices = array();
     344        }
     345
     346        // Add notice to notices array
     347        $this->notices[] = $message;
    244348    }
    245349
     
    363467     * @since 2.1.0 bbPress (r3767)
    364468     *
    365      * @return type
     469     * @return void
    366470     */
    367471    public static function new_install() {
     472
     473        // Bail if not a new install
    368474        if ( ! bbp_is_install() ) {
    369475            return;
     
    528634            }
    529635        }
    530     }
    531 
    532     /**
    533      * Admin area activation notice
    534      *
    535      * Shows a nag message in admin area about the theme not supporting bbPress
    536      *
    537      * @since 2.0.0 bbPress (r2743)
    538      */
    539     public function activation_notice() {
    540         // @todo - something fun
    541636    }
    542637
  • trunk/src/includes/admin/forums.php

    r6573 r6771  
    6767
    6868        // Check if there are any bbp_toggle_forum_* requests on admin_init, also have a message displayed
    69         add_action( 'load-edit.php',  array( $this, 'toggle_forum'        ) );
    70         add_action( 'admin_notices', array( $this, 'toggle_forum_notice' ) );
     69        add_action( 'load-edit.php',     array( $this, 'toggle_forum'        ) );
     70        add_action( 'bbp_admin_notices', array( $this, 'toggle_forum_notice' ) );
    7171
    7272        // Contextual Help
  • trunk/src/includes/admin/replies.php

    r6697 r6771  
    7474
    7575        // Check if there are any bbp_toggle_reply_* requests on admin_init, also have a message displayed
    76         add_action( 'load-edit.php',  array( $this, 'toggle_reply'        ) );
    77         add_action( 'admin_notices', array( $this, 'toggle_reply_notice' ) );
     76        add_action( 'load-edit.php',     array( $this, 'toggle_reply'        ) );
     77        add_action( 'bbp_admin_notices', array( $this, 'toggle_reply_notice' ) );
    7878
    7979        // Add ability to filter topics and replies per forum
  • trunk/src/includes/admin/tools/common.php

    r6704 r6771  
    6262 */
    6363function bbp_admin_tools_feedback( $message, $class = false, $is_dismissible = true ) {
    64 
    65     // One message as string
    66     if ( is_string( $message ) ) {
    67         $message       = '<p>' . $message . '</p>';
    68         $default_class ='updated';
    69 
    70     // Messages as objects
    71     } elseif ( is_wp_error( $message ) ) {
    72         $errors  = $message->get_error_messages();
    73 
    74         switch ( count( $errors ) ) {
    75             case 0:
    76                 return false;
    77 
    78             case 1:
    79                 $message = '<p>' . $errors[0] . '</p>';
    80                 break;
    81 
    82             default:
    83                 $message = '<ul>' . "\n\t" . '<li>' . implode( '</li>' . "\n\t" . '<li>', $errors ) . '</li>' . "\n" . '</ul>';
    84                 break;
    85         }
    86 
    87         $default_class = 'is-error';
    88 
    89     // Message is an unknown format, so bail
    90     } else {
    91         return false;
    92     }
    93 
    94     // CSS Classes
    95     $classes = ! empty( $class )
    96         ? array( $class )
    97         : array( $default_class );
    98 
    99     // Add dismissible class
    100     if ( ! empty( $is_dismissible ) ) {
    101         array_push( $classes, 'is-dismissible' );
    102     }
    103 
    104     // Assemble the message
    105     $message = '<div id="message" class="notice ' . implode( ' ', array_map( 'esc_attr', $classes ) ) . '">' . $message . '</div>';
    106     $message = str_replace( "'", "\'", $message );
    107 
    108     // Ugh
    109     $lambda  = create_function( '', "echo '$message';" );
    110     add_action( 'admin_notices', $lambda );
    111 
    112     return $lambda;
     64    return bbp_admin()->add_notice( $message, $class, $is_dismissible );
    11365}
    11466
  • trunk/src/includes/admin/topics.php

    r6697 r6771  
    7878
    7979        // Check if there are any bbp_toggle_topic_* requests on admin_init, also have a message displayed
    80         add_action( 'load-edit.php',  array( $this, 'toggle_topic'        ) );
    81         add_action( 'admin_notices', array( $this, 'toggle_topic_notice' ) );
     80        add_action( 'load-edit.php',     array( $this, 'toggle_topic'        ) );
     81        add_action( 'bbp_admin_notices', array( $this, 'toggle_topic_notice' ) );
    8282
    8383        // Add ability to filter topics and replies per forum
Note: See TracChangeset for help on using the changeset viewer.