2013-06-11 14:59:42 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-11-03 13:53:50 +00:00
|
|
|
* WooCommerce Message Functions
|
2013-06-11 14:59:42 +00:00
|
|
|
*
|
|
|
|
* Functions for error/message handling and display.
|
|
|
|
*
|
|
|
|
* @author WooThemes
|
|
|
|
* @category Core
|
|
|
|
* @package WooCommerce/Functions
|
|
|
|
* @version 2.1.0
|
|
|
|
*/
|
|
|
|
|
2014-09-20 19:09:43 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
2013-06-11 14:59:42 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Get the count of notices added, either for all notices (default) or for one.
|
2014-09-05 06:37:12 +00:00
|
|
|
* particular notice type specified by $notice_type.
|
2013-11-13 00:16:27 +00:00
|
|
|
*
|
2014-09-05 06:37:12 +00:00
|
|
|
* @since 2.1
|
|
|
|
* @param string $notice_type The name of the notice type - either error, success or notice. [optional]
|
2013-06-11 14:59:42 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2013-11-13 00:16:27 +00:00
|
|
|
function wc_notice_count( $notice_type = '' ) {
|
2015-01-06 11:10:59 +00:00
|
|
|
if ( ! did_action( 'woocommerce_init' ) ) {
|
|
|
|
_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-13 00:16:27 +00:00
|
|
|
$notice_count = 0;
|
2013-11-13 23:56:01 +00:00
|
|
|
$all_notices = WC()->session->get( 'wc_notices', array() );
|
2013-11-13 00:16:27 +00:00
|
|
|
|
2016-08-27 03:11:30 +00:00
|
|
|
if ( isset( $all_notices[ $notice_type ] ) ) {
|
2013-11-13 23:56:01 +00:00
|
|
|
|
2016-08-27 03:11:30 +00:00
|
|
|
$notice_count = absint( sizeof( $all_notices[ $notice_type ] ) );
|
2013-11-13 23:56:01 +00:00
|
|
|
|
|
|
|
} elseif ( empty( $notice_type ) ) {
|
|
|
|
|
|
|
|
foreach ( $all_notices as $notices ) {
|
|
|
|
$notice_count += absint( sizeof( $all_notices ) );
|
2013-11-13 00:16:27 +00:00
|
|
|
}
|
2013-11-13 23:56:01 +00:00
|
|
|
|
2013-11-13 00:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $notice_count;
|
2013-06-11 14:59:42 +00:00
|
|
|
}
|
|
|
|
|
2014-01-07 13:35:01 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Check if a notice has already been added.
|
2014-01-07 13:35:01 +00:00
|
|
|
*
|
2014-09-05 06:37:12 +00:00
|
|
|
* @since 2.1
|
|
|
|
* @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]
|
2014-01-07 13:35:01 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function wc_has_notice( $message, $notice_type = 'success' ) {
|
2015-01-06 11:10:59 +00:00
|
|
|
if ( ! did_action( 'woocommerce_init' ) ) {
|
|
|
|
_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
|
2015-01-06 11:29:40 +00:00
|
|
|
return false;
|
2015-01-06 11:10:59 +00:00
|
|
|
}
|
|
|
|
|
2014-01-07 13:35:01 +00:00
|
|
|
$notices = WC()->session->get( 'wc_notices', array() );
|
|
|
|
$notices = isset( $notices[ $notice_type ] ) ? $notices[ $notice_type ] : array();
|
|
|
|
return array_search( $message, $notices ) !== false;
|
|
|
|
}
|
|
|
|
|
2013-06-11 14:59:42 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Add and store a notice.
|
2013-06-11 14:59:42 +00:00
|
|
|
*
|
2014-09-05 06:37:12 +00:00
|
|
|
* @since 2.1
|
|
|
|
* @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]
|
2013-06-11 14:59:42 +00:00
|
|
|
*/
|
2013-11-13 23:56:01 +00:00
|
|
|
function wc_add_notice( $message, $notice_type = 'success' ) {
|
2015-01-06 11:10:59 +00:00
|
|
|
if ( ! did_action( 'woocommerce_init' ) ) {
|
|
|
|
_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
|
|
|
|
return;
|
|
|
|
}
|
2013-11-13 23:56:01 +00:00
|
|
|
|
|
|
|
$notices = WC()->session->get( 'wc_notices', array() );
|
2013-06-11 14:59:42 +00:00
|
|
|
|
2013-11-13 23:56:01 +00:00
|
|
|
// Backward compatibility
|
2014-09-05 06:37:12 +00:00
|
|
|
if ( 'success' === $notice_type ) {
|
2013-11-13 23:56:01 +00:00
|
|
|
$message = apply_filters( 'woocommerce_add_message', $message );
|
2014-09-05 06:37:12 +00:00
|
|
|
}
|
2013-06-11 14:59:42 +00:00
|
|
|
|
2016-08-27 03:11:30 +00:00
|
|
|
$notices[ $notice_type ][] = apply_filters( 'woocommerce_add_' . $notice_type, $message );
|
2013-11-13 23:56:01 +00:00
|
|
|
|
|
|
|
WC()->session->set( 'wc_notices', $notices );
|
2013-06-11 14:59:42 +00:00
|
|
|
}
|
|
|
|
|
2016-06-14 10:33:28 +00:00
|
|
|
/**
|
|
|
|
* Set all notices at once.
|
|
|
|
* @since 2.6.0
|
|
|
|
*/
|
|
|
|
function wc_set_notices( $notices ) {
|
|
|
|
if ( ! did_action( 'woocommerce_init' ) ) {
|
|
|
|
_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.6' );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
WC()->session->set( 'wc_notices', $notices );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-11 14:59:42 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Unset all notices.
|
2013-11-13 23:56:01 +00:00
|
|
|
*
|
|
|
|
* @since 2.1
|
2013-06-11 14:59:42 +00:00
|
|
|
*/
|
2013-11-13 00:21:33 +00:00
|
|
|
function wc_clear_notices() {
|
2015-01-06 11:10:59 +00:00
|
|
|
if ( ! did_action( 'woocommerce_init' ) ) {
|
|
|
|
_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
|
|
|
|
return;
|
|
|
|
}
|
2013-11-13 23:56:01 +00:00
|
|
|
WC()->session->set( 'wc_notices', null );
|
2013-06-11 14:59:42 +00:00
|
|
|
}
|
2013-11-13 00:23:28 +00:00
|
|
|
|
2013-06-11 14:59:42 +00:00
|
|
|
/**
|
|
|
|
* Prints messages and errors which are stored in the session, then clears them.
|
2013-11-13 23:56:01 +00:00
|
|
|
*
|
|
|
|
* @since 2.1
|
2013-06-11 14:59:42 +00:00
|
|
|
*/
|
2013-11-13 00:23:28 +00:00
|
|
|
function wc_print_notices() {
|
2015-01-06 11:10:59 +00:00
|
|
|
if ( ! did_action( 'woocommerce_init' ) ) {
|
|
|
|
_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
|
|
|
|
return;
|
|
|
|
}
|
2013-06-11 14:59:42 +00:00
|
|
|
|
2013-11-13 23:56:01 +00:00
|
|
|
$all_notices = WC()->session->get( 'wc_notices', array() );
|
2013-11-14 00:19:27 +00:00
|
|
|
$notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'success', 'notice' ) );
|
2013-11-13 04:34:55 +00:00
|
|
|
|
2013-11-13 23:56:01 +00:00
|
|
|
foreach ( $notice_types as $notice_type ) {
|
|
|
|
if ( wc_notice_count( $notice_type ) > 0 ) {
|
2013-11-25 12:45:04 +00:00
|
|
|
wc_get_template( "notices/{$notice_type}.php", array(
|
2016-08-27 01:46:45 +00:00
|
|
|
'messages' => array_filter( $all_notices[ $notice_type ] ),
|
2013-11-13 00:23:28 +00:00
|
|
|
) );
|
|
|
|
}
|
|
|
|
}
|
2013-06-11 14:59:42 +00:00
|
|
|
|
2013-11-13 00:23:28 +00:00
|
|
|
wc_clear_notices();
|
2013-06-11 14:59:42 +00:00
|
|
|
}
|
2015-06-03 13:22:23 +00:00
|
|
|
add_action( 'woocommerce_shortcode_before_product_cat_loop', 'wc_print_notices', 10 );
|
2013-11-13 00:23:28 +00:00
|
|
|
add_action( 'woocommerce_before_shop_loop', 'wc_print_notices', 10 );
|
|
|
|
add_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 );
|
2013-11-13 23:57:06 +00:00
|
|
|
|
2013-11-14 00:10:31 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Print a single notice immediately.
|
2013-11-14 00:10:31 +00:00
|
|
|
*
|
2014-09-05 06:37:12 +00:00
|
|
|
* @since 2.1
|
|
|
|
* @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]
|
2013-11-14 00:10:31 +00:00
|
|
|
*/
|
|
|
|
function wc_print_notice( $message, $notice_type = 'success' ) {
|
2014-09-05 06:37:12 +00:00
|
|
|
if ( 'success' === $notice_type ) {
|
2013-11-14 00:10:31 +00:00
|
|
|
$message = apply_filters( 'woocommerce_add_message', $message );
|
2014-09-05 06:37:12 +00:00
|
|
|
}
|
2013-11-14 00:10:31 +00:00
|
|
|
|
2013-11-25 12:45:04 +00:00
|
|
|
wc_get_template( "notices/{$notice_type}.php", array(
|
2016-08-27 01:46:45 +00:00
|
|
|
'messages' => array( apply_filters( 'woocommerce_add_' . $notice_type, $message ) ),
|
2013-11-14 00:10:31 +00:00
|
|
|
) );
|
|
|
|
}
|
|
|
|
|
2013-11-13 23:57:06 +00:00
|
|
|
/**
|
|
|
|
* Returns all queued notices, optionally filtered by a notice type.
|
2014-09-05 06:37:12 +00:00
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @param string $notice_type The singular name of the notice type - either error, success or notice. [optional]
|
2013-11-28 16:49:30 +00:00
|
|
|
* @return array|mixed
|
2013-11-13 23:57:06 +00:00
|
|
|
*/
|
|
|
|
function wc_get_notices( $notice_type = '' ) {
|
2015-01-06 11:10:59 +00:00
|
|
|
if ( ! did_action( 'woocommerce_init' ) ) {
|
|
|
|
_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
|
|
|
|
return;
|
|
|
|
}
|
2013-11-13 23:57:06 +00:00
|
|
|
|
|
|
|
$all_notices = WC()->session->get( 'wc_notices', array() );
|
|
|
|
|
2016-06-14 10:33:28 +00:00
|
|
|
if ( empty( $notice_type ) ) {
|
2013-11-13 23:57:06 +00:00
|
|
|
$notices = $all_notices;
|
2016-06-14 10:33:28 +00:00
|
|
|
} elseif ( isset( $all_notices[ $notice_type ] ) ) {
|
|
|
|
$notices = $all_notices[ $notice_type ];
|
2013-11-13 23:57:06 +00:00
|
|
|
} else {
|
|
|
|
$notices = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $notices;
|
|
|
|
}
|
2015-01-21 14:15:49 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Add notices for WP Errors.
|
2015-01-21 14:15:49 +00:00
|
|
|
* @param WP_Error $errors
|
|
|
|
*/
|
|
|
|
function wc_add_wp_error_notices( $errors ) {
|
|
|
|
if ( is_wp_error( $errors ) && $errors->get_error_messages() ) {
|
|
|
|
foreach ( $errors->get_error_messages() as $error ) {
|
|
|
|
wc_add_notice( $error, 'error');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|