Нема описа

wc-notice-functions.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. if ( ! defined( 'ABSPATH' ) ) {
  11. exit;
  12. }
  13. /**
  14. * Get the count of notices added, either for all notices (default) or for one.
  15. * particular notice type specified by $notice_type.
  16. *
  17. * @since 2.1
  18. * @param string $notice_type Optional. The name of the notice type - either error, success or notice.
  19. * @return int
  20. */
  21. function wc_notice_count( $notice_type = '' ) {
  22. if ( ! did_action( 'woocommerce_init' ) ) {
  23. wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
  24. return;
  25. }
  26. $notice_count = 0;
  27. $all_notices = WC()->session->get( 'wc_notices', array() );
  28. if ( isset( $all_notices[ $notice_type ] ) ) {
  29. $notice_count = count( $all_notices[ $notice_type ] );
  30. } elseif ( empty( $notice_type ) ) {
  31. foreach ( $all_notices as $notices ) {
  32. $notice_count += count( $notices );
  33. }
  34. }
  35. return $notice_count;
  36. }
  37. /**
  38. * Check if a notice has already been added.
  39. *
  40. * @since 2.1
  41. * @param string $message The text to display in the notice.
  42. * @param string $notice_type Optional. The name of the notice type - either error, success or notice.
  43. * @return bool
  44. */
  45. function wc_has_notice( $message, $notice_type = 'success' ) {
  46. if ( ! did_action( 'woocommerce_init' ) ) {
  47. wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
  48. return false;
  49. }
  50. $notices = WC()->session->get( 'wc_notices', array() );
  51. $notices = isset( $notices[ $notice_type ] ) ? $notices[ $notice_type ] : array();
  52. return array_search( $message, wp_list_pluck( $notices, 'notice' ), true ) !== false;
  53. }
  54. /**
  55. * Add and store a notice.
  56. *
  57. * @since 2.1
  58. * @version 3.9.0
  59. * @param string $message The text to display in the notice.
  60. * @param string $notice_type Optional. The name of the notice type - either error, success or notice.
  61. * @param array $data Optional notice data.
  62. */
  63. function wc_add_notice( $message, $notice_type = 'success', $data = array() ) {
  64. if ( ! did_action( 'woocommerce_init' ) ) {
  65. wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
  66. return;
  67. }
  68. $notices = WC()->session->get( 'wc_notices', array() );
  69. // Backward compatibility.
  70. if ( 'success' === $notice_type ) {
  71. $message = apply_filters( 'woocommerce_add_message', $message );
  72. }
  73. $message = apply_filters( 'woocommerce_add_' . $notice_type, $message );
  74. if ( ! empty( $message ) ) {
  75. $notices[ $notice_type ][] = array(
  76. 'notice' => $message,
  77. 'data' => $data,
  78. );
  79. }
  80. WC()->session->set( 'wc_notices', $notices );
  81. }
  82. /**
  83. * Set all notices at once.
  84. *
  85. * @since 2.6.0
  86. * @param array[] $notices Array of notices.
  87. */
  88. function wc_set_notices( $notices ) {
  89. if ( ! did_action( 'woocommerce_init' ) ) {
  90. wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.6' );
  91. return;
  92. }
  93. WC()->session->set( 'wc_notices', $notices );
  94. }
  95. /**
  96. * Unset all notices.
  97. *
  98. * @since 2.1
  99. */
  100. function wc_clear_notices() {
  101. if ( ! did_action( 'woocommerce_init' ) ) {
  102. wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
  103. return;
  104. }
  105. WC()->session->set( 'wc_notices', null );
  106. }
  107. /**
  108. * Prints messages and errors which are stored in the session, then clears them.
  109. *
  110. * @since 2.1
  111. * @param bool $return true to return rather than echo. @since 3.5.0.
  112. * @return string|null
  113. */
  114. function wc_print_notices( $return = false ) {
  115. if ( ! did_action( 'woocommerce_init' ) ) {
  116. wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
  117. return;
  118. }
  119. $all_notices = WC()->session->get( 'wc_notices', array() );
  120. $notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'success', 'notice' ) );
  121. // Buffer output.
  122. ob_start();
  123. foreach ( $notice_types as $notice_type ) {
  124. if ( wc_notice_count( $notice_type ) > 0 ) {
  125. $messages = array();
  126. foreach ( $all_notices[ $notice_type ] as $notice ) {
  127. $messages[] = isset( $notice['notice'] ) ? $notice['notice'] : $notice;
  128. }
  129. wc_get_template(
  130. "notices/{$notice_type}.php",
  131. array(
  132. 'messages' => array_filter( $messages ), // @deprecated 3.9.0
  133. 'notices' => array_filter( $all_notices[ $notice_type ] ),
  134. )
  135. );
  136. }
  137. }
  138. wc_clear_notices();
  139. $notices = wc_kses_notice( ob_get_clean() );
  140. if ( $return ) {
  141. return $notices;
  142. }
  143. echo $notices; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  144. }
  145. /**
  146. * Print a single notice immediately.
  147. *
  148. * @since 2.1
  149. * @version 3.9.0
  150. * @param string $message The text to display in the notice.
  151. * @param string $notice_type Optional. The singular name of the notice type - either error, success or notice.
  152. * @param array $data Optional notice data. @since 3.9.0.
  153. */
  154. function wc_print_notice( $message, $notice_type = 'success', $data = array() ) {
  155. if ( 'success' === $notice_type ) {
  156. $message = apply_filters( 'woocommerce_add_message', $message );
  157. }
  158. $message = apply_filters( 'woocommerce_add_' . $notice_type, $message );
  159. wc_get_template(
  160. "notices/{$notice_type}.php",
  161. array(
  162. 'messages' => array( $message ), // @deprecated 3.9.0
  163. 'notices' => array(
  164. array(
  165. 'notice' => $message,
  166. 'data' => $data,
  167. ),
  168. ),
  169. )
  170. );
  171. }
  172. /**
  173. * Returns all queued notices, optionally filtered by a notice type.
  174. *
  175. * @since 2.1
  176. * @version 3.9.0
  177. * @param string $notice_type Optional. The singular name of the notice type - either error, success or notice.
  178. * @return array[]
  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. $all_notices = WC()->session->get( 'wc_notices', array() );
  186. if ( empty( $notice_type ) ) {
  187. $notices = $all_notices;
  188. } elseif ( isset( $all_notices[ $notice_type ] ) ) {
  189. $notices = $all_notices[ $notice_type ];
  190. } else {
  191. $notices = array();
  192. }
  193. return $notices;
  194. }
  195. /**
  196. * Add notices for WP Errors.
  197. *
  198. * @param WP_Error $errors Errors.
  199. */
  200. function wc_add_wp_error_notices( $errors ) {
  201. if ( is_wp_error( $errors ) && $errors->get_error_messages() ) {
  202. foreach ( $errors->get_error_messages() as $error ) {
  203. wc_add_notice( $error, 'error' );
  204. }
  205. }
  206. }
  207. /**
  208. * Filters out the same tags as wp_kses_post, but allows tabindex for <a> element.
  209. *
  210. * @since 3.5.0
  211. * @param string $message Content to filter through kses.
  212. * @return string
  213. */
  214. function wc_kses_notice( $message ) {
  215. $allowed_tags = array_replace_recursive(
  216. wp_kses_allowed_html( 'post' ),
  217. array(
  218. 'a' => array(
  219. 'tabindex' => true,
  220. ),
  221. )
  222. );
  223. /**
  224. * Kses notice allowed tags.
  225. *
  226. * @since 3.9.0
  227. * @param array[]|string $allowed_tags An array of allowed HTML elements and attributes, or a context name such as 'post'.
  228. */
  229. return wp_kses( $message, apply_filters( 'woocommerce_kses_notice_allowed_tags', $allowed_tags ) );
  230. }
  231. /**
  232. * Get notice data attribute.
  233. *
  234. * @since 3.9.0
  235. * @param array $notice Notice data.
  236. * @return string
  237. */
  238. function wc_get_notice_data_attr( $notice ) {
  239. if ( empty( $notice['data'] ) ) {
  240. return;
  241. }
  242. $attr = '';
  243. foreach ( $notice['data'] as $key => $value ) {
  244. $attr .= sprintf(
  245. ' data-%1$s="%2$s"',
  246. sanitize_title( $key ),
  247. esc_attr( $value )
  248. );
  249. }
  250. return $attr;
  251. }