abstracts
6 years ago
admin
6 years ago
api
6 years ago
blocks
7 years ago
cli
7 years ago
customizer
8 years ago
data-stores
4 years ago
emails
7 years ago
export
7 years ago
gateways
4 years ago
import
7 years ago
interfaces
7 years ago
legacy
7 years ago
libraries
6 years ago
log-handlers
7 years ago
payment-tokens
8 years ago
queue
7 years ago
shipping
7 years ago
shortcodes
6 years ago
theme-support
7 years ago
tracks
6 years ago
walkers
8 years ago
widgets
6 years ago
class-wc-ajax.php
6 years ago
class-wc-api.php
7 years ago
class-wc-auth.php
7 years ago
class-wc-autoloader.php
6 years ago
class-wc-background-emailer.php
8 years ago
class-wc-background-updater.php
7 years ago
class-wc-breadcrumb.php
7 years ago
class-wc-cache-helper.php
7 years ago
class-wc-cart-fees.php
8 years ago
class-wc-cart-session.php
7 years ago
class-wc-cart-totals.php
7 years ago
class-wc-cart.php
7 years ago
class-wc-checkout.php
7 years ago
class-wc-cli.php
8 years ago
class-wc-comments.php
7 years ago
class-wc-countries.php
6 years ago
class-wc-coupon.php
7 years ago
class-wc-customer-download-log.php
8 years ago
class-wc-customer-download.php
8 years ago
class-wc-customer.php
7 years ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
7 years ago
class-wc-datetime.php
7 years ago
class-wc-deprecated-action-hooks.php
8 years ago
class-wc-deprecated-filter-hooks.php
7 years ago
class-wc-discounts.php
7 years ago
class-wc-download-handler.php
6 years ago
class-wc-emails.php
7 years ago
class-wc-embed.php
8 years ago
class-wc-form-handler.php
6 years ago
class-wc-frontend-scripts.php
7 years ago
class-wc-geo-ip.php
8 years ago
class-wc-geolite-integration.php
7 years ago
class-wc-geolocation.php
7 years ago
class-wc-https.php
8 years ago
class-wc-install.php
6 years ago
class-wc-integrations.php
8 years ago
class-wc-log-levels.php
7 years ago
class-wc-logger.php
7 years ago
class-wc-meta-data.php
7 years ago
class-wc-order-factory.php
8 years ago
class-wc-order-item-coupon.php
7 years ago
class-wc-order-item-fee.php
7 years ago
class-wc-order-item-meta.php
7 years ago
class-wc-order-item-product.php
7 years ago
class-wc-order-item-shipping.php
7 years ago
class-wc-order-item-tax.php
8 years ago
class-wc-order-item.php
6 years ago
class-wc-order-query.php
7 years ago
class-wc-order-refund.php
8 years ago
class-wc-order.php
7 years ago
class-wc-payment-gateways.php
7 years ago
class-wc-payment-tokens.php
7 years ago
class-wc-post-data.php
7 years ago
class-wc-post-types.php
7 years ago
class-wc-privacy-background-process.php
8 years ago
class-wc-privacy-erasers.php
8 years ago
class-wc-privacy-exporters.php
7 years ago
class-wc-privacy.php
7 years ago
class-wc-product-attribute.php
7 years ago
class-wc-product-download.php
7 years ago
class-wc-product-external.php
8 years ago
class-wc-product-factory.php
7 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
7 years ago
class-wc-product-simple.php
8 years ago
class-wc-product-variable.php
7 years ago
class-wc-product-variation.php
7 years ago
class-wc-query.php
7 years ago
class-wc-regenerate-images-request.php
7 years ago
class-wc-regenerate-images.php
7 years ago
class-wc-register-wp-admin-settings.php
8 years ago
class-wc-session-handler.php
7 years ago
class-wc-shipping-rate.php
8 years ago
class-wc-shipping-zone.php
7 years ago
class-wc-shipping-zones.php
8 years ago
class-wc-shipping.php
7 years ago
class-wc-shortcodes.php
7 years ago
class-wc-structured-data.php
6 years ago
class-wc-tax.php
7 years ago
class-wc-template-loader.php
7 years ago
class-wc-tracker.php
6 years ago
class-wc-validation.php
6 years ago
class-wc-webhook.php
7 years ago
class-woocommerce.php
4 years ago
wc-account-functions.php
7 years ago
wc-attribute-functions.php
7 years ago
wc-cart-functions.php
7 years ago
wc-conditional-functions.php
6 years ago
wc-core-functions.php
7 years ago
wc-coupon-functions.php
7 years ago
wc-deprecated-functions.php
7 years ago
wc-formatting-functions.php
6 years ago
wc-notice-functions.php
7 years ago
wc-order-functions.php
7 years ago
wc-order-item-functions.php
7 years ago
wc-page-functions.php
7 years ago
wc-product-functions.php
7 years ago
wc-rest-functions.php
7 years ago
wc-stock-functions.php
7 years ago
wc-template-functions.php
7 years ago
wc-template-hooks.php
7 years ago
wc-term-functions.php
7 years ago
wc-update-functions.php
7 years ago
wc-user-functions.php
7 years ago
wc-webhook-functions.php
7 years ago
wc-widget-functions.php
8 years ago
wc-notice-functions.php
231 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Message Functions |
| 4 | * |
| 5 | * Functions for error/message handling and display. |
| 6 | * |
| 7 | * @package WooCommerce/Functions |
| 8 | * @version 2.1.0 |
| 9 | */ |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Get the count of notices added, either for all notices (default) or for one. |
| 17 | * particular notice type specified by $notice_type. |
| 18 | * |
| 19 | * @since 2.1 |
| 20 | * @param string $notice_type Optional. The name of the notice type - either error, success or notice. |
| 21 | * @return int |
| 22 | */ |
| 23 | function wc_notice_count( $notice_type = '' ) { |
| 24 | if ( ! did_action( 'woocommerce_init' ) ) { |
| 25 | wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' ); |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | $notice_count = 0; |
| 30 | $all_notices = WC()->session->get( 'wc_notices', array() ); |
| 31 | |
| 32 | if ( isset( $all_notices[ $notice_type ] ) ) { |
| 33 | |
| 34 | $notice_count = count( $all_notices[ $notice_type ] ); |
| 35 | |
| 36 | } elseif ( empty( $notice_type ) ) { |
| 37 | |
| 38 | foreach ( $all_notices as $notices ) { |
| 39 | $notice_count += count( $notices ); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | return $notice_count; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Check if a notice has already been added. |
| 48 | * |
| 49 | * @since 2.1 |
| 50 | * @param string $message The text to display in the notice. |
| 51 | * @param string $notice_type Optional. The name of the notice type - either error, success or notice. |
| 52 | * @return bool |
| 53 | */ |
| 54 | function wc_has_notice( $message, $notice_type = 'success' ) { |
| 55 | if ( ! did_action( 'woocommerce_init' ) ) { |
| 56 | wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' ); |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | $notices = WC()->session->get( 'wc_notices', array() ); |
| 61 | $notices = isset( $notices[ $notice_type ] ) ? $notices[ $notice_type ] : array(); |
| 62 | return array_search( $message, $notices, true ) !== false; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Add and store a notice. |
| 67 | * |
| 68 | * @since 2.1 |
| 69 | * @param string $message The text to display in the notice. |
| 70 | * @param string $notice_type Optional. The name of the notice type - either error, success or notice. |
| 71 | */ |
| 72 | function wc_add_notice( $message, $notice_type = 'success' ) { |
| 73 | if ( ! did_action( 'woocommerce_init' ) ) { |
| 74 | wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' ); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | $notices = WC()->session->get( 'wc_notices', array() ); |
| 79 | |
| 80 | // Backward compatibility. |
| 81 | if ( 'success' === $notice_type ) { |
| 82 | $message = apply_filters( 'woocommerce_add_message', $message ); |
| 83 | } |
| 84 | |
| 85 | $notices[ $notice_type ][] = apply_filters( 'woocommerce_add_' . $notice_type, $message ); |
| 86 | |
| 87 | WC()->session->set( 'wc_notices', $notices ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Set all notices at once. |
| 92 | * |
| 93 | * @since 2.6.0 |
| 94 | * @param mixed $notices Array of notices. |
| 95 | */ |
| 96 | function wc_set_notices( $notices ) { |
| 97 | if ( ! did_action( 'woocommerce_init' ) ) { |
| 98 | wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.6' ); |
| 99 | return; |
| 100 | } |
| 101 | WC()->session->set( 'wc_notices', $notices ); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * Unset all notices. |
| 107 | * |
| 108 | * @since 2.1 |
| 109 | */ |
| 110 | function wc_clear_notices() { |
| 111 | if ( ! did_action( 'woocommerce_init' ) ) { |
| 112 | wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' ); |
| 113 | return; |
| 114 | } |
| 115 | WC()->session->set( 'wc_notices', null ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Prints messages and errors which are stored in the session, then clears them. |
| 120 | * |
| 121 | * @since 2.1 |
| 122 | * @param bool $return true to return rather than echo. @since 3.5.0. |
| 123 | * @return string|null |
| 124 | */ |
| 125 | function wc_print_notices( $return = false ) { |
| 126 | if ( ! did_action( 'woocommerce_init' ) ) { |
| 127 | wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' ); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | $all_notices = WC()->session->get( 'wc_notices', array() ); |
| 132 | $notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'success', 'notice' ) ); |
| 133 | |
| 134 | // Buffer output. |
| 135 | ob_start(); |
| 136 | |
| 137 | foreach ( $notice_types as $notice_type ) { |
| 138 | if ( wc_notice_count( $notice_type ) > 0 ) { |
| 139 | wc_get_template( "notices/{$notice_type}.php", array( |
| 140 | 'messages' => array_filter( $all_notices[ $notice_type ] ), |
| 141 | ) ); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | wc_clear_notices(); |
| 146 | |
| 147 | $notices = wc_kses_notice( ob_get_clean() ); |
| 148 | |
| 149 | if ( $return ) { |
| 150 | return $notices; |
| 151 | } |
| 152 | |
| 153 | echo $notices; // WPCS: XSS ok. |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Print a single notice immediately. |
| 158 | * |
| 159 | * @since 2.1 |
| 160 | * @param string $message The text to display in the notice. |
| 161 | * @param string $notice_type Optional. The singular name of the notice type - either error, success or notice. |
| 162 | */ |
| 163 | function wc_print_notice( $message, $notice_type = 'success' ) { |
| 164 | if ( 'success' === $notice_type ) { |
| 165 | $message = apply_filters( 'woocommerce_add_message', $message ); |
| 166 | } |
| 167 | |
| 168 | wc_get_template( "notices/{$notice_type}.php", array( |
| 169 | 'messages' => array( apply_filters( 'woocommerce_add_' . $notice_type, $message ) ), |
| 170 | ) ); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Returns all queued notices, optionally filtered by a notice type. |
| 175 | * |
| 176 | * @since 2.1 |
| 177 | * @param string $notice_type Optional. The singular name of the notice type - either error, success or notice. |
| 178 | * @return array|mixed |
| 179 | */ |
| 180 | function wc_get_notices( $notice_type = '' ) { |
| 181 | if ( ! did_action( 'woocommerce_init' ) ) { |
| 182 | wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' ); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | $all_notices = WC()->session->get( 'wc_notices', array() ); |
| 187 | |
| 188 | if ( empty( $notice_type ) ) { |
| 189 | $notices = $all_notices; |
| 190 | } elseif ( isset( $all_notices[ $notice_type ] ) ) { |
| 191 | $notices = $all_notices[ $notice_type ]; |
| 192 | } else { |
| 193 | $notices = array(); |
| 194 | } |
| 195 | |
| 196 | return $notices; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Add notices for WP Errors. |
| 201 | * |
| 202 | * @param WP_Error $errors Errors. |
| 203 | */ |
| 204 | function wc_add_wp_error_notices( $errors ) { |
| 205 | if ( is_wp_error( $errors ) && $errors->get_error_messages() ) { |
| 206 | foreach ( $errors->get_error_messages() as $error ) { |
| 207 | wc_add_notice( $error, 'error' ); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Filters out the same tags as wp_kses_post, but allows tabindex for <a> element. |
| 214 | * |
| 215 | * @since 3.5.0 |
| 216 | * @param string $message Content to filter through kses. |
| 217 | * @return string |
| 218 | */ |
| 219 | function wc_kses_notice( $message ) { |
| 220 | return wp_kses( $message, |
| 221 | array_replace_recursive( // phpcs:ignore PHPCompatibility.PHP.NewFunctions.array_replace_recursiveFound |
| 222 | wp_kses_allowed_html( 'post' ), |
| 223 | array( |
| 224 | 'a' => array( |
| 225 | 'tabindex' => true, |
| 226 | ), |
| 227 | ) |
| 228 | ) |
| 229 | ); |
| 230 | } |
| 231 |