From 85b4ca8c03f506c1a9d8ec08ff4ba7062ed08005 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 7 Jan 2014 13:35:01 +0000 Subject: [PATCH] wc_has_notice function --- includes/class-wc-query.php | 4 ++-- includes/wc-notice-functions.php | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/includes/class-wc-query.php b/includes/class-wc-query.php index 019e0d1982a..575e7ba4f6c 100644 --- a/includes/class-wc-query.php +++ b/includes/class-wc-query.php @@ -89,8 +89,8 @@ class WC_Query { * Get any errors from querystring */ public function get_errors() { - if ( isset( $_GET['wc_error'] ) ) - wc_add_notice( esc_attr( $_GET['wc_error'] ), 'error' ); + if ( ! empty( $_GET['wc_error'] ) && ( $error = sanitize_text_field( urldecode( $_GET['wc_error'] ) ) ) && ! wc_has_notice( $error, 'error' ) ) + wc_add_notice( $error, 'error' ); } /** diff --git a/includes/wc-notice-functions.php b/includes/wc-notice-functions.php index febc6383839..a2dc2522dd6 100644 --- a/includes/wc-notice-functions.php +++ b/includes/wc-notice-functions.php @@ -20,7 +20,6 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly * @return int */ function wc_notice_count( $notice_type = '' ) { - $notice_count = 0; $all_notices = WC()->session->get( 'wc_notices', array() ); @@ -39,6 +38,19 @@ function wc_notice_count( $notice_type = '' ) { return $notice_count; } +/** + * See if a notice has already been added + * + * @param string $message The text to display in the notice. + * @param string $notice_type The singular name of the notice type - either error, success or notice. [optional] + * @return bool + */ +function wc_has_notice( $message, $notice_type = 'success' ) { + $notices = WC()->session->get( 'wc_notices', array() ); + $notices = isset( $notices[ $notice_type ] ) ? $notices[ $notice_type ] : array(); + return array_search( $message, $notices ) !== false; +} + /** * Add and store a notice *