abstracts
5 years ago
admin
5 years ago
cli
5 years ago
customizer
5 years ago
data-stores
4 years ago
emails
5 years ago
export
5 years ago
gateways
5 years ago
import
5 years ago
integrations
5 years ago
interfaces
5 years ago
legacy
5 years ago
libraries
5 years ago
log-handlers
5 years ago
payment-tokens
5 years ago
queue
5 years ago
shipping
5 years ago
shortcodes
5 years ago
theme-support
5 years ago
tracks
5 years ago
traits
5 years ago
walkers
5 years ago
wccom-site
5 years ago
widgets
5 years ago
class-wc-ajax.php
5 years ago
class-wc-api.php
5 years ago
class-wc-auth.php
5 years ago
class-wc-autoloader.php
5 years ago
class-wc-background-emailer.php
5 years ago
class-wc-background-updater.php
5 years ago
class-wc-breadcrumb.php
5 years ago
class-wc-cache-helper.php
5 years ago
class-wc-cart-fees.php
5 years ago
class-wc-cart-session.php
5 years ago
class-wc-cart-totals.php
5 years ago
class-wc-cart.php
5 years ago
class-wc-checkout.php
5 years ago
class-wc-cli.php
5 years ago
class-wc-comments.php
5 years ago
class-wc-countries.php
5 years ago
class-wc-coupon.php
5 years ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
5 years ago
class-wc-customer.php
5 years ago
class-wc-data-exception.php
5 years ago
class-wc-data-store.php
5 years ago
class-wc-datetime.php
5 years ago
class-wc-deprecated-action-hooks.php
5 years ago
class-wc-deprecated-filter-hooks.php
5 years ago
class-wc-discounts.php
5 years ago
class-wc-download-handler.php
5 years ago
class-wc-emails.php
5 years ago
class-wc-embed.php
5 years ago
class-wc-form-handler.php
5 years ago
class-wc-frontend-scripts.php
5 years ago
class-wc-geo-ip.php
5 years ago
class-wc-geolite-integration.php
5 years ago
class-wc-geolocation.php
5 years ago
class-wc-https.php
5 years ago
class-wc-install.php
4 years ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
5 years ago
class-wc-logger.php
5 years ago
class-wc-meta-data.php
5 years ago
class-wc-order-factory.php
5 years ago
class-wc-order-item-coupon.php
5 years ago
class-wc-order-item-fee.php
5 years ago
class-wc-order-item-meta.php
5 years ago
class-wc-order-item-product.php
5 years ago
class-wc-order-item-shipping.php
5 years ago
class-wc-order-item-tax.php
5 years ago
class-wc-order-item.php
5 years ago
class-wc-order-query.php
5 years ago
class-wc-order-refund.php
5 years ago
class-wc-order.php
5 years ago
class-wc-payment-gateways.php
5 years ago
class-wc-payment-tokens.php
5 years ago
class-wc-post-data.php
5 years ago
class-wc-post-types.php
5 years ago
class-wc-privacy-background-process.php
5 years ago
class-wc-privacy-erasers.php
5 years ago
class-wc-privacy-exporters.php
5 years ago
class-wc-privacy.php
5 years ago
class-wc-product-attribute.php
5 years ago
class-wc-product-download.php
5 years ago
class-wc-product-external.php
5 years ago
class-wc-product-factory.php
5 years ago
class-wc-product-grouped.php
5 years ago
class-wc-product-query.php
5 years ago
class-wc-product-simple.php
5 years ago
class-wc-product-variable.php
5 years ago
class-wc-product-variation.php
5 years ago
class-wc-query.php
5 years ago
class-wc-rate-limiter.php
5 years ago
class-wc-regenerate-images-request.php
5 years ago
class-wc-regenerate-images.php
5 years ago
class-wc-register-wp-admin-settings.php
5 years ago
class-wc-rest-authentication.php
5 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
5 years ago
class-wc-shipping-rate.php
5 years ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
5 years ago
class-wc-shipping.php
5 years ago
class-wc-shortcodes.php
5 years ago
class-wc-structured-data.php
5 years ago
class-wc-tax.php
5 years ago
class-wc-template-loader.php
5 years ago
class-wc-tracker.php
5 years ago
class-wc-validation.php
5 years ago
class-wc-webhook.php
5 years ago
class-woocommerce.php
4 years ago
wc-account-functions.php
5 years ago
wc-attribute-functions.php
5 years ago
wc-cart-functions.php
5 years ago
wc-conditional-functions.php
5 years ago
wc-core-functions.php
5 years ago
wc-coupon-functions.php
5 years ago
wc-deprecated-functions.php
5 years ago
wc-formatting-functions.php
5 years ago
wc-notice-functions.php
5 years ago
wc-order-functions.php
5 years ago
wc-order-item-functions.php
5 years ago
wc-page-functions.php
5 years ago
wc-product-functions.php
5 years ago
wc-rest-functions.php
5 years ago
wc-stock-functions.php
5 years ago
wc-template-functions.php
5 years ago
wc-template-hooks.php
5 years ago
wc-term-functions.php
5 years ago
wc-update-functions.php
4 years ago
wc-user-functions.php
5 years ago
wc-webhook-functions.php
5 years ago
wc-widget-functions.php
5 years ago
wc-notice-functions.php
295 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, wp_list_pluck( $notices, 'notice' ), true ) !== false; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Add and store a notice. |
| 67 | * |
| 68 | * @since 2.1 |
| 69 | * @version 3.9.0 |
| 70 | * @param string $message The text to display in the notice. |
| 71 | * @param string $notice_type Optional. The name of the notice type - either error, success or notice. |
| 72 | * @param array $data Optional notice data. |
| 73 | */ |
| 74 | function wc_add_notice( $message, $notice_type = 'success', $data = array() ) { |
| 75 | if ( ! did_action( 'woocommerce_init' ) ) { |
| 76 | wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' ); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | $notices = WC()->session->get( 'wc_notices', array() ); |
| 81 | |
| 82 | // Backward compatibility. |
| 83 | if ( 'success' === $notice_type ) { |
| 84 | $message = apply_filters( 'woocommerce_add_message', $message ); |
| 85 | } |
| 86 | |
| 87 | $message = apply_filters( 'woocommerce_add_' . $notice_type, $message ); |
| 88 | |
| 89 | if ( ! empty( $message ) ) { |
| 90 | $notices[ $notice_type ][] = array( |
| 91 | 'notice' => apply_filters( 'woocommerce_add_' . $notice_type, $message ), |
| 92 | 'data' => $data, |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | WC()->session->set( 'wc_notices', $notices ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Set all notices at once. |
| 101 | * |
| 102 | * @since 2.6.0 |
| 103 | * @param array[] $notices Array of notices. |
| 104 | */ |
| 105 | function wc_set_notices( $notices ) { |
| 106 | if ( ! did_action( 'woocommerce_init' ) ) { |
| 107 | wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.6' ); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | WC()->session->set( 'wc_notices', $notices ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Unset all notices. |
| 116 | * |
| 117 | * @since 2.1 |
| 118 | */ |
| 119 | function wc_clear_notices() { |
| 120 | if ( ! did_action( 'woocommerce_init' ) ) { |
| 121 | wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' ); |
| 122 | return; |
| 123 | } |
| 124 | WC()->session->set( 'wc_notices', null ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Prints messages and errors which are stored in the session, then clears them. |
| 129 | * |
| 130 | * @since 2.1 |
| 131 | * @param bool $return true to return rather than echo. @since 3.5.0. |
| 132 | * @return string|null |
| 133 | */ |
| 134 | function wc_print_notices( $return = false ) { |
| 135 | if ( ! did_action( 'woocommerce_init' ) ) { |
| 136 | wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' ); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | $all_notices = WC()->session->get( 'wc_notices', array() ); |
| 141 | $notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'success', 'notice' ) ); |
| 142 | |
| 143 | // Buffer output. |
| 144 | ob_start(); |
| 145 | |
| 146 | foreach ( $notice_types as $notice_type ) { |
| 147 | if ( wc_notice_count( $notice_type ) > 0 ) { |
| 148 | $messages = array(); |
| 149 | |
| 150 | foreach ( $all_notices[ $notice_type ] as $notice ) { |
| 151 | $messages[] = isset( $notice['notice'] ) ? $notice['notice'] : $notice; |
| 152 | } |
| 153 | |
| 154 | wc_get_template( |
| 155 | "notices/{$notice_type}.php", |
| 156 | array( |
| 157 | 'messages' => array_filter( $messages ), // @deprecated 3.9.0 |
| 158 | 'notices' => array_filter( $all_notices[ $notice_type ] ), |
| 159 | ) |
| 160 | ); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | wc_clear_notices(); |
| 165 | |
| 166 | $notices = wc_kses_notice( ob_get_clean() ); |
| 167 | |
| 168 | if ( $return ) { |
| 169 | return $notices; |
| 170 | } |
| 171 | |
| 172 | echo $notices; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Print a single notice immediately. |
| 177 | * |
| 178 | * @since 2.1 |
| 179 | * @version 3.9.0 |
| 180 | * @param string $message The text to display in the notice. |
| 181 | * @param string $notice_type Optional. The singular name of the notice type - either error, success or notice. |
| 182 | * @param array $data Optional notice data. @since 3.9.0. |
| 183 | */ |
| 184 | function wc_print_notice( $message, $notice_type = 'success', $data = array() ) { |
| 185 | if ( 'success' === $notice_type ) { |
| 186 | $message = apply_filters( 'woocommerce_add_message', $message ); |
| 187 | } |
| 188 | |
| 189 | $message = apply_filters( 'woocommerce_add_' . $notice_type, $message ); |
| 190 | |
| 191 | wc_get_template( |
| 192 | "notices/{$notice_type}.php", |
| 193 | array( |
| 194 | 'messages' => array( $message ), // @deprecated 3.9.0 |
| 195 | 'notices' => array( |
| 196 | array( |
| 197 | 'notice' => $message, |
| 198 | 'data' => $data, |
| 199 | ), |
| 200 | ), |
| 201 | ) |
| 202 | ); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Returns all queued notices, optionally filtered by a notice type. |
| 207 | * |
| 208 | * @since 2.1 |
| 209 | * @version 3.9.0 |
| 210 | * @param string $notice_type Optional. The singular name of the notice type - either error, success or notice. |
| 211 | * @return array[] |
| 212 | */ |
| 213 | function wc_get_notices( $notice_type = '' ) { |
| 214 | if ( ! did_action( 'woocommerce_init' ) ) { |
| 215 | wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' ); |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | $all_notices = WC()->session->get( 'wc_notices', array() ); |
| 220 | |
| 221 | if ( empty( $notice_type ) ) { |
| 222 | $notices = $all_notices; |
| 223 | } elseif ( isset( $all_notices[ $notice_type ] ) ) { |
| 224 | $notices = $all_notices[ $notice_type ]; |
| 225 | } else { |
| 226 | $notices = array(); |
| 227 | } |
| 228 | |
| 229 | return $notices; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Add notices for WP Errors. |
| 234 | * |
| 235 | * @param WP_Error $errors Errors. |
| 236 | */ |
| 237 | function wc_add_wp_error_notices( $errors ) { |
| 238 | if ( is_wp_error( $errors ) && $errors->get_error_messages() ) { |
| 239 | foreach ( $errors->get_error_messages() as $error ) { |
| 240 | wc_add_notice( $error, 'error' ); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Filters out the same tags as wp_kses_post, but allows tabindex for <a> element. |
| 247 | * |
| 248 | * @since 3.5.0 |
| 249 | * @param string $message Content to filter through kses. |
| 250 | * @return string |
| 251 | */ |
| 252 | function wc_kses_notice( $message ) { |
| 253 | $allowed_tags = array_replace_recursive( |
| 254 | wp_kses_allowed_html( 'post' ), |
| 255 | array( |
| 256 | 'a' => array( |
| 257 | 'tabindex' => true, |
| 258 | ), |
| 259 | ) |
| 260 | ); |
| 261 | |
| 262 | /** |
| 263 | * Kses notice allowed tags. |
| 264 | * |
| 265 | * @since 3.9.0 |
| 266 | * @param array[]|string $allowed_tags An array of allowed HTML elements and attributes, or a context name such as 'post'. |
| 267 | */ |
| 268 | return wp_kses( $message, apply_filters( 'woocommerce_kses_notice_allowed_tags', $allowed_tags ) ); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Get notice data attribute. |
| 273 | * |
| 274 | * @since 3.9.0 |
| 275 | * @param array $notice Notice data. |
| 276 | * @return string |
| 277 | */ |
| 278 | function wc_get_notice_data_attr( $notice ) { |
| 279 | if ( empty( $notice['data'] ) ) { |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | $attr = ''; |
| 284 | |
| 285 | foreach ( $notice['data'] as $key => $value ) { |
| 286 | $attr .= sprintf( |
| 287 | ' data-%1$s="%2$s"', |
| 288 | sanitize_title( $key ), |
| 289 | esc_attr( $value ) |
| 290 | ); |
| 291 | } |
| 292 | |
| 293 | return $attr; |
| 294 | } |
| 295 |