woocommerce/includes/wc-notice-functions.php

202 lines
5.8 KiB
PHP
Raw Normal View History

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
*/
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.
*
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
*/
function wc_notice_count( $notice_type = '' ) {
if ( ! did_action( 'woocommerce_init' ) ) {
2016-11-23 16:15:00 +00:00
wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
return;
}
$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
}
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' ) {
if ( ! did_action( 'woocommerce_init' ) ) {
2016-11-23 16:15:00 +00:00
wc_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;
}
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
*/
function wc_add_notice( $message, $notice_type = 'success' ) {
if ( ! did_action( 'woocommerce_init' ) ) {
2016-11-23 16:15:00 +00:00
wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
return;
}
$notices = WC()->session->get( 'wc_notices', array() );
2013-06-11 14:59:42 +00:00
// Backward compatibility
2014-09-05 06:37:12 +00:00
if ( 'success' === $notice_type ) {
$message = apply_filters( 'woocommerce_add_message', $message );
2014-09-05 06:37:12 +00:00
}
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
}
/**
* Set all notices at once.
* @since 2.6.0
*
* @param mixed $notices
*/
function wc_set_notices( $notices ) {
if ( ! did_action( 'woocommerce_init' ) ) {
2016-11-23 16:15:00 +00:00
wc_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.
*
* @since 2.1
2013-06-11 14:59:42 +00:00
*/
function wc_clear_notices() {
if ( ! did_action( 'woocommerce_init' ) ) {
2016-11-23 16:15:00 +00:00
wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
return;
}
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() {
if ( ! did_action( 'woocommerce_init' ) ) {
2016-11-23 16:15:00 +00:00
wc_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
$all_notices = WC()->session->get( 'wc_notices', array() );
$notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'success', 'notice' ) );
foreach ( $notice_types as $notice_type ) {
if ( wc_notice_count( $notice_type ) > 0 ) {
wc_get_template( "notices/{$notice_type}.php", array(
'messages' => array_filter( $all_notices[ $notice_type ] ),
) );
}
}
2013-06-11 14:59:42 +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 );
add_action( 'woocommerce_before_shop_loop', 'wc_print_notices', 10 );
add_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 );
/**
2015-11-03 13:31:20 +00:00
* Print a single notice immediately.
*
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]
*/
function wc_print_notice( $message, $notice_type = 'success' ) {
2014-09-05 06:37:12 +00:00
if ( 'success' === $notice_type ) {
$message = apply_filters( 'woocommerce_add_message', $message );
2014-09-05 06:37:12 +00:00
}
wc_get_template( "notices/{$notice_type}.php", array(
'messages' => array( apply_filters( 'woocommerce_add_' . $notice_type, $message ) ),
) );
}
/**
* 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
*/
function wc_get_notices( $notice_type = '' ) {
if ( ! did_action( 'woocommerce_init' ) ) {
2016-11-23 16:15:00 +00:00
wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
return;
}
$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;
}
/**
2015-11-03 13:31:20 +00:00
* Add notices for WP Errors.
* @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' );
}
}
}