AgenticCheckoutUtils.php
4 months ago
ArrayUtils.php
2 years ago
CartController.php
4 weeks ago
CartTokenUtils.php
1 year ago
CheckoutTrait.php
4 weeks ago
DraftOrderTrait.php
1 year ago
JsonWebToken.php
11 months ago
LocalPickupUtils.php
5 months ago
NoticeHandler.php
1 year ago
OrderAuthorizationTrait.php
6 months ago
OrderController.php
4 weeks ago
Pagination.php
2 years ago
PaymentUtils.php
1 year ago
ProductItemTrait.php
4 weeks ago
ProductLinksTrait.php
3 months ago
ProductQuery.php
2 months ago
ProductQueryFilters.php
11 months ago
QuantityLimits.php
11 months ago
RateLimits.php
1 year ago
SanitizationUtils.php
2 years ago
ValidationUtils.php
2 years ago
NoticeHandler.php
69 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Utilities; |
| 3 | |
| 4 | use Automattic\WooCommerce\StoreApi\Exceptions\RouteException; |
| 5 | use WP_Error; |
| 6 | |
| 7 | /** |
| 8 | * NoticeHandler class. |
| 9 | * Helper class to handle notices. |
| 10 | */ |
| 11 | class NoticeHandler { |
| 12 | |
| 13 | /** |
| 14 | * Convert queued error notices into an exception. |
| 15 | * |
| 16 | * For example, Payment methods may add error notices during validate_fields call to prevent checkout. |
| 17 | * Since we're not rendering notices at all, we need to convert them to exceptions. |
| 18 | * |
| 19 | * This method will find the first error message and thrown an exception instead. Discards notices once complete. |
| 20 | * |
| 21 | * @throws RouteException If an error notice is detected, Exception is thrown. |
| 22 | * |
| 23 | * @param string $error_code Error code for the thrown exceptions. |
| 24 | */ |
| 25 | public static function convert_notices_to_exceptions( $error_code = 'unknown_server_error' ) { |
| 26 | if ( 0 === wc_notice_count( 'error' ) ) { |
| 27 | wc_clear_notices(); |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | $error_notices = wc_get_notices( 'error' ); |
| 32 | |
| 33 | // Prevent notices from being output later on. |
| 34 | wc_clear_notices(); |
| 35 | |
| 36 | foreach ( $error_notices as $error_notice ) { |
| 37 | throw new RouteException( $error_code, wp_strip_all_tags( $error_notice['notice'] ), 400 ); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Collects queued error notices into a \WP_Error. |
| 43 | * |
| 44 | * For example, cart validation processes may add error notices to prevent checkout. |
| 45 | * Since we're not rendering notices at all, we need to catch them and group them in a single WP_Error instance. |
| 46 | * |
| 47 | * This method will discard notices once complete. |
| 48 | * |
| 49 | * @param string $error_code Error code for the thrown exceptions. |
| 50 | * |
| 51 | * @return \WP_Error The WP_Error object containing all error notices. |
| 52 | */ |
| 53 | public static function convert_notices_to_wp_errors( $error_code = 'unknown_server_error' ) { |
| 54 | $errors = new WP_Error(); |
| 55 | |
| 56 | if ( 0 === wc_notice_count( 'error' ) ) { |
| 57 | return $errors; |
| 58 | } |
| 59 | |
| 60 | $error_notices = wc_get_notices( 'error' ); |
| 61 | |
| 62 | foreach ( $error_notices as $error_notice ) { |
| 63 | $errors->add( $error_code, wp_strip_all_tags( $error_notice['notice'] ) ); |
| 64 | } |
| 65 | |
| 66 | return $errors; |
| 67 | } |
| 68 | } |
| 69 |