From a2c132720bc442fc1a8a4f4c51f5416499eebd74 Mon Sep 17 00:00:00 2001 From: Erick Hitter <git-contrib@ethitter.com> Date: Tue, 12 Sep 2017 22:25:40 -0700 Subject: [PATCH] Standardize admin notice output --- includes/class-delete-all.php | 20 +++++--------- includes/class-main.php | 46 +++++++++++++++++++++++--------- includes/class-move-to-trash.php | 20 +++++--------- 3 files changed, 47 insertions(+), 39 deletions(-) diff --git a/includes/class-delete-all.php b/includes/class-delete-all.php index 111352f..dc540b5 100644 --- a/includes/class-delete-all.php +++ b/includes/class-delete-all.php @@ -112,31 +112,25 @@ class Delete_All { public static function admin_notices() { $screen = get_current_screen(); + $type = ''; + $message = ''; + if ( isset( $_REQUEST[ self::ADMIN_NOTICE_KEY ] ) ) { if ( 1 === (int) $_REQUEST[ self::ADMIN_NOTICE_KEY ] ) { - $class = 'notice-success'; + $type = 'success'; $message = __( 'Success! The trash will be emptied shortly.', 'bulk-edit-cron-offload' ); } else { - $class = 'notice-error'; + $type = 'error'; $message = __( 'A request to empty the trash is already pending for this post type.', 'bulk-edit-cron-offload' ); } } elseif ( 'edit' === $screen->base && isset( $_REQUEST['post_status'] ) && 'trash' === $_REQUEST['post_status'] ) { if ( self::action_next_scheduled( $screen->post_type ) ) { - $class = 'notice-warning'; + $type = 'warning'; $message = __( 'A pending request to empty the trash will be processed soon.', 'bulk-edit-cron-offload' ); } } - // Nothing to display. - if ( ! isset( $class ) || ! isset( $message ) ) { - return; - } - - ?> - <div class="notice <?php echo esc_attr( $class ); ?>"> - <p><?php echo esc_html( $message ); ?></p> - </div> - <?php + Main::render_admin_notice( $type, $message ); } /** diff --git a/includes/class-main.php b/includes/class-main.php index ec22ea5..2597d9b 100644 --- a/includes/class-main.php +++ b/includes/class-main.php @@ -191,6 +191,25 @@ class Main { unset( $_REQUEST['delete_all2'] ); } + /** + * Create cron event + * + * @param object $vars Bulk-request variables. + * @return bool + */ + public static function schedule_processing( $vars ) { + return false !== wp_schedule_single_event( time(), self::CRON_EVENT, array( $vars ) ); + } + + /** + * + * @param object $vars Bulk-request variables. + * @return int + */ + public static function next_scheduled( $vars ) { + return (int) wp_next_scheduled( self::CRON_EVENT, array( $vars ) ); + } + /** * Redirect, including a flag to indicate if the bulk process was scheduled successfully * @@ -215,22 +234,23 @@ class Main { } /** - * Create cron event + * Render an admin message of a given type * - * @param object $vars Bulk-request variables. - * @return bool + * @param string $type Message type. + * @param string $message Message to output. + * @return void */ - public static function schedule_processing( $vars ) { - return false !== wp_schedule_single_event( time(), self::CRON_EVENT, array( $vars ) ); - } + public static function render_admin_notice( $type, $message ) { + // Lacking what's required. + if ( empty( $type ) || empty( $message ) ) { + return; + } - /** - * - * @param object $vars Bulk-request variables. - * @return int - */ - public static function next_scheduled( $vars ) { - return (int) wp_next_scheduled( self::CRON_EVENT, array( $vars ) ); + ?> + <div class="notice <?php echo esc_attr( 'notice-' . $type ); ?>"> + <p><?php echo esc_html( $message ); ?></p> + </div> + <?php } } diff --git a/includes/class-move-to-trash.php b/includes/class-move-to-trash.php index 587a5fb..d137aea 100644 --- a/includes/class-move-to-trash.php +++ b/includes/class-move-to-trash.php @@ -100,12 +100,15 @@ class Move_To_Trash { public static function admin_notices() { $screen = get_current_screen(); + $type = ''; + $message = ''; + if ( isset( $_REQUEST[ self::ADMIN_NOTICE_KEY ] ) ) { if ( 1 === (int) $_REQUEST[ self::ADMIN_NOTICE_KEY ] ) { - $class = 'notice-success'; + $type = 'success'; $message = __( 'Success! The selected posts will be moved to the trash shortly.', 'bulk-edit-cron-offload' ); } else { - $class = 'notice-error'; + $type = 'error'; $message = __( 'The selected posts are already scheduled to be moved to the trash.', 'bulk-edit-cron-offload' ); } } elseif ( 'edit' === $screen->base ) { @@ -116,21 +119,12 @@ class Move_To_Trash { $status = isset( $_REQUEST['post_status'] ) ? $_REQUEST['post_status'] : 'all'; if ( self::get_all_pending_actions( $screen->post_type, $status ) ) { - $class = 'notice-warning'; + $type = 'warning'; $message = __( 'Some items that would normally be shown here are waiting to be moved to the trash. These items are hidden until they are moved.', 'bulk-edit-cron-offload' ); } } - // Nothing to display. - if ( ! isset( $class ) || ! isset( $message ) ) { - return; - } - - ?> - <div class="notice <?php echo esc_attr( $class ); ?>"> - <p><?php echo esc_html( $message ); ?></p> - </div> - <?php + Main::render_admin_notice( $type, $message ); } /** -- GitLab