2013-06-11 14:59:42 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WooCommerce Message Functions
|
|
|
|
*
|
|
|
|
* Functions for error/message handling and display.
|
|
|
|
*
|
|
|
|
* @author WooThemes
|
|
|
|
* @category Core
|
|
|
|
* @package WooCommerce/Functions
|
|
|
|
* @version 2.1.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
|
|
|
|
|
|
|
/**
|
2013-11-13 23:56:01 +00:00
|
|
|
* Get the count of notices added, either for all notices (default) or for one particular notice type specified
|
|
|
|
* by $notice_type.
|
2013-11-13 00:16:27 +00:00
|
|
|
*
|
2013-11-13 23:56:01 +00:00
|
|
|
* @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 = '' ) {
|
2013-06-11 14:59:42 +00:00
|
|
|
|
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
|
|
|
|
2013-11-13 23:56:01 +00:00
|
|
|
if ( isset( $all_notices[$notice_type] ) ) {
|
|
|
|
|
|
|
|
$notice_count = absint( sizeof( $all_notices[$notice_type] ) );
|
|
|
|
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-11-13 00:19:09 +00:00
|
|
|
* Add and store a notice
|
2013-06-11 14:59:42 +00:00
|
|
|
*
|
2013-11-13 00:19:09 +00:00
|
|
|
* @param string $message The text to display in the notice.
|
2013-11-13 23:56:01 +00:00
|
|
|
* @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' ) {
|
|
|
|
|
|
|
|
$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
|
|
|
|
if ( 'success' === $notice_type )
|
|
|
|
$message = apply_filters( 'woocommerce_add_message', $message );
|
2013-06-11 14:59:42 +00:00
|
|
|
|
2013-11-13 23:56:01 +00:00
|
|
|
$notices[$notice_type][] = apply_filters( 'woocommerce_add_' . $notice_type, $message );
|
|
|
|
|
|
|
|
WC()->session->set( 'wc_notices', $notices );
|
2013-06-11 14:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-11-13 00:21:33 +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() {
|
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() {
|
2013-06-11 14:59:42 +00:00
|
|
|
|
2013-11-13 23:56:01 +00:00
|
|
|
$all_notices = WC()->session->get( 'wc_notices', array() );
|
|
|
|
$notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'message', '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-13 23:21:07 +00:00
|
|
|
woocommerce_get_template( "notices/{$notice_type}.php", array(
|
2013-11-13 23:56:01 +00:00
|
|
|
"{$notice_type}s" => $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
|
|
|
}
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all queued notices, optionally filtered by a notice type.
|
|
|
|
*
|
|
|
|
* @param string $notice_type The singular name of the notice type - either error, success or notice. [optional]
|
|
|
|
*/
|
|
|
|
function wc_get_notices( $notice_type = '' ) {
|
|
|
|
|
|
|
|
$all_notices = WC()->session->get( 'wc_notices', array() );
|
|
|
|
|
|
|
|
if ( empty ( $notice_type ) ) {
|
|
|
|
$notices = $all_notices;
|
|
|
|
} elseif ( isset( $all_notices[$notice_type] ) ) {
|
|
|
|
$notices = $all_notices[$notice_type];
|
|
|
|
} else {
|
|
|
|
$notices = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $notices;
|
|
|
|
}
|