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.
|
|
|
|
*
|
2018-01-11 04:54:30 +00:00
|
|
|
* @package WooCommerce/Functions
|
|
|
|
* @version 2.1.0
|
2013-06-11 14:59:42 +00:00
|
|
|
*/
|
|
|
|
|
2014-09-20 19:09:43 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2018-01-11 04:54:30 +00:00
|
|
|
exit;
|
2014-09-20 19:09:43 +00:00
|
|
|
}
|
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
|
|
|
*
|
2018-01-11 04:54:30 +00:00
|
|
|
* @since 2.1
|
|
|
|
* @param string $notice_type Optional. The name of the notice type - either error, success or notice.
|
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' ) ) {
|
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:10:59 +00:00
|
|
|
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
|
|
|
|
2018-01-11 04:47:41 +00:00
|
|
|
$notice_count = count( $all_notices[ $notice_type ] );
|
2013-11-13 23:56:01 +00:00
|
|
|
|
|
|
|
} elseif ( empty( $notice_type ) ) {
|
|
|
|
|
|
|
|
foreach ( $all_notices as $notices ) {
|
2018-01-10 12:43:48 +00:00
|
|
|
$notice_count += count( $notices );
|
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
|
|
|
*
|
2018-01-11 04:54:30 +00:00
|
|
|
* @since 2.1
|
|
|
|
* @param string $message The text to display in the notice.
|
|
|
|
* @param string $notice_type Optional. The name of the notice type - either error, success or notice.
|
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' ) ) {
|
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;
|
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();
|
2019-11-07 23:23:30 +00:00
|
|
|
return array_search( $message, wp_list_pluck( $notices, 'notice' ), true ) !== false;
|
2014-01-07 13:35:01 +00:00
|
|
|
}
|
|
|
|
|
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
|
2020-01-23 19:46:01 +00:00
|
|
|
* @version 3.9.0
|
2019-11-07 23:23:30 +00:00
|
|
|
* @param string $message The text to display in the notice.
|
2018-01-11 04:54:30 +00:00
|
|
|
* @param string $notice_type Optional. The name of the notice type - either error, success or notice.
|
2019-11-07 23:23:30 +00:00
|
|
|
* @param array $data Optional notice data.
|
2013-06-11 14:59:42 +00:00
|
|
|
*/
|
2019-11-07 23:23:30 +00:00
|
|
|
function wc_add_notice( $message, $notice_type = 'success', $data = array() ) {
|
2015-01-06 11:10:59 +00:00
|
|
|
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:10:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-11-13 23:56:01 +00:00
|
|
|
|
|
|
|
$notices = WC()->session->get( 'wc_notices', array() );
|
2013-06-11 14:59:42 +00:00
|
|
|
|
2018-01-11 04:54:30 +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
|
|
|
|
2020-01-24 16:31:05 +00:00
|
|
|
$message = apply_filters( 'woocommerce_add_' . $notice_type, $message );
|
|
|
|
|
|
|
|
if ( ! empty( $message ) ) {
|
|
|
|
$notices[ $notice_type ][] = array(
|
|
|
|
'notice' => apply_filters( 'woocommerce_add_' . $notice_type, $message ),
|
|
|
|
'data' => $data,
|
|
|
|
);
|
|
|
|
}
|
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.
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
2018-01-11 04:54:30 +00:00
|
|
|
* @since 2.6.0
|
2019-11-07 23:23:30 +00:00
|
|
|
* @param array[] $notices Array of notices.
|
2016-06-14 10:33:28 +00:00
|
|
|
*/
|
|
|
|
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' );
|
2016-06-14 10:33:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-11-07 23:23:30 +00:00
|
|
|
|
2016-06-14 10:33:28 +00:00
|
|
|
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' ) ) {
|
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:10:59 +00:00
|
|
|
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
|
2018-04-19 17:26:52 +00:00
|
|
|
* @param bool $return true to return rather than echo. @since 3.5.0.
|
|
|
|
* @return string|null
|
2013-06-11 14:59:42 +00:00
|
|
|
*/
|
2018-04-19 17:26:52 +00:00
|
|
|
function wc_print_notices( $return = false ) {
|
2015-01-06 11:10:59 +00:00
|
|
|
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:10:59 +00:00
|
|
|
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
|
|
|
|
2018-04-19 17:26:52 +00:00
|
|
|
// Buffer output.
|
|
|
|
ob_start();
|
|
|
|
|
2013-11-13 23:56:01 +00:00
|
|
|
foreach ( $notice_types as $notice_type ) {
|
|
|
|
if ( wc_notice_count( $notice_type ) > 0 ) {
|
2019-11-07 23:23:30 +00:00
|
|
|
$messages = array();
|
|
|
|
|
|
|
|
foreach ( $all_notices[ $notice_type ] as $notice ) {
|
|
|
|
$messages[] = isset( $notice['notice'] ) ? $notice['notice'] : $notice;
|
|
|
|
}
|
|
|
|
|
|
|
|
wc_get_template(
|
|
|
|
"notices/{$notice_type}.php",
|
|
|
|
array(
|
|
|
|
'messages' => array_filter( $messages ), // @deprecated 3.9.0
|
|
|
|
'notices' => 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();
|
2018-04-19 17:26:52 +00:00
|
|
|
|
2018-08-21 10:00:48 +00:00
|
|
|
$notices = wc_kses_notice( ob_get_clean() );
|
2018-04-19 17:26:52 +00:00
|
|
|
|
|
|
|
if ( $return ) {
|
2018-04-19 17:37:07 +00:00
|
|
|
return $notices;
|
2018-04-19 17:26:52 +00:00
|
|
|
}
|
2018-04-19 17:37:07 +00:00
|
|
|
|
2019-11-07 23:23:30 +00:00
|
|
|
echo $notices; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
2013-06-11 14:59:42 +00:00
|
|
|
}
|
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
|
2020-01-23 19:46:01 +00:00
|
|
|
* @version 3.9.0
|
2014-09-05 06:37:12 +00:00
|
|
|
* @param string $message The text to display in the notice.
|
2018-01-11 04:54:30 +00:00
|
|
|
* @param string $notice_type Optional. The singular name of the notice type - either error, success or notice.
|
2019-11-07 23:23:30 +00:00
|
|
|
* @param array $data Optional notice data. @since 3.9.0.
|
2013-11-14 00:10:31 +00:00
|
|
|
*/
|
2019-11-07 23:23:30 +00:00
|
|
|
function wc_print_notice( $message, $notice_type = 'success', $data = array() ) {
|
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
|
|
|
|
2019-11-07 23:23:30 +00:00
|
|
|
$message = apply_filters( 'woocommerce_add_' . $notice_type, $message );
|
|
|
|
|
|
|
|
wc_get_template(
|
|
|
|
"notices/{$notice_type}.php",
|
|
|
|
array(
|
|
|
|
'messages' => array( $message ), // @deprecated 3.9.0
|
|
|
|
'notices' => array(
|
|
|
|
array(
|
|
|
|
'notice' => $message,
|
|
|
|
'data' => $data,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
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
|
|
|
*
|
2018-01-11 04:54:30 +00:00
|
|
|
* @since 2.1
|
2020-01-23 19:46:01 +00:00
|
|
|
* @version 3.9.0
|
2018-01-11 04:54:30 +00:00
|
|
|
* @param string $notice_type Optional. The singular name of the notice type - either error, success or notice.
|
2019-11-07 23:23:30 +00:00
|
|
|
* @return array[]
|
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' ) ) {
|
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:10:59 +00:00
|
|
|
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.
|
2018-01-11 04:54:30 +00:00
|
|
|
*
|
|
|
|
* @param WP_Error $errors Errors.
|
2015-01-21 14:15:49 +00:00
|
|
|
*/
|
|
|
|
function wc_add_wp_error_notices( $errors ) {
|
|
|
|
if ( is_wp_error( $errors ) && $errors->get_error_messages() ) {
|
|
|
|
foreach ( $errors->get_error_messages() as $error ) {
|
2016-09-02 01:33:57 +00:00
|
|
|
wc_add_notice( $error, 'error' );
|
2015-01-21 14:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-21 10:00:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters out the same tags as wp_kses_post, but allows tabindex for <a> element.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
* @param string $message Content to filter through kses.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_kses_notice( $message ) {
|
2019-10-16 19:45:50 +00:00
|
|
|
$allowed_tags = array_replace_recursive(
|
|
|
|
wp_kses_allowed_html( 'post' ),
|
|
|
|
array(
|
|
|
|
'a' => array(
|
|
|
|
'tabindex' => true,
|
|
|
|
),
|
2018-08-21 10:00:48 +00:00
|
|
|
)
|
|
|
|
);
|
2019-10-16 19:45:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Kses notice allowed tags.
|
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
* @param array[]|string $allowed_tags An array of allowed HTML elements and attributes, or a context name such as 'post'.
|
|
|
|
*/
|
|
|
|
return wp_kses( $message, apply_filters( 'woocommerce_kses_notice_allowed_tags', $allowed_tags ) );
|
2018-08-21 10:00:48 +00:00
|
|
|
}
|
2019-11-07 23:23:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get notice data attribute.
|
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
* @param array $notice Notice data.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_get_notice_data_attr( $notice ) {
|
2019-11-07 23:34:49 +00:00
|
|
|
if ( empty( $notice['data'] ) ) {
|
2019-11-07 23:23:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-07 23:34:49 +00:00
|
|
|
$attr = '';
|
|
|
|
|
|
|
|
foreach ( $notice['data'] as $key => $value ) {
|
|
|
|
$attr .= sprintf(
|
|
|
|
' data-%1$s="%2$s"',
|
|
|
|
sanitize_title( $key ),
|
|
|
|
esc_attr( $value )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $attr;
|
2019-11-07 23:23:30 +00:00
|
|
|
}
|