woocommerce/includes/wc-notice-functions.php

112 lines
2.8 KiB
PHP
Raw Normal View History

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
/**
* Get the count of notices added, either for all notices (default) or for one particular notice type specified
* by $notice_type.
*
* @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
*/
function wc_notice_count( $notice_type = '' ) {
2013-06-11 14:59:42 +00:00
$notice_count = 0;
$all_notices = WC()->session->get( 'wc_notices', array() );
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 ) );
}
}
return $notice_count;
2013-06-11 14:59:42 +00:00
}
/**
* Add and store a notice
2013-06-11 14:59:42 +00:00
*
* @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
*/
function wc_add_notice( $message, $notice_type = 'success' ) {
$notices = WC()->session->get( 'wc_notices', array() );
2013-06-11 14:59:42 +00:00
// Backward compatibility
if ( 'success' === $notice_type )
$message = apply_filters( 'woocommerce_add_message', $message );
2013-06-11 14:59:42 +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
}
/**
* Unset all notices
*
* @since 2.1
2013-06-11 14:59:42 +00:00
*/
function wc_clear_notices() {
WC()->session->set( 'wc_notices', null );
2013-06-11 14:59:42 +00:00
}
2013-06-11 14:59:42 +00:00
/**
* Prints messages and errors which are stored in the session, then clears them.
*
* @since 2.1
2013-06-11 14:59:42 +00:00
*/
function wc_print_notices() {
2013-06-11 14:59:42 +00:00
$all_notices = WC()->session->get( 'wc_notices', array() );
$notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'message', 'notice' ) );
foreach ( $notice_types as $notice_type ) {
if ( wc_notice_count( $notice_type ) > 0 ) {
woocommerce_get_template( "notices/{$notice_type}.php", array(
"{$notice_type}s" => $all_notices[$notice_type]
) );
}
}
2013-06-11 14:59:42 +00:00
wc_clear_notices();
2013-06-11 14:59:42 +00:00
}
add_action( 'woocommerce_before_shop_loop', 'wc_print_notices', 10 );
add_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 );
/**
* 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;
}