CheckoutFieldsSchema
11 months ago
Email
1 year ago
CheckoutFields.php
11 months ago
CheckoutFieldsAdmin.php
2 years ago
CheckoutFieldsFrontend.php
11 months ago
CheckoutLink.php
11 months ago
CreateAccount.php
1 year ago
DraftOrders.php
2 months ago
FeatureGating.php
1 year ago
GoogleAnalytics.php
2 years ago
Hydration.php
5 months ago
Notices.php
1 year ago
functions.php
2 years ago
Notices.php
131 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks\Domain\Services; |
| 3 | |
| 4 | use Automattic\WooCommerce\Blocks\Domain\Package; |
| 5 | use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils; |
| 6 | |
| 7 | /** |
| 8 | * Service class for adding new-style Notices to WooCommerce core. |
| 9 | * |
| 10 | * @internal |
| 11 | */ |
| 12 | class Notices { |
| 13 | /** |
| 14 | * Holds the Package instance |
| 15 | * |
| 16 | * @var Package |
| 17 | */ |
| 18 | private $package; |
| 19 | |
| 20 | /** |
| 21 | * Templates used for notices. |
| 22 | * |
| 23 | * @var array |
| 24 | */ |
| 25 | private $notice_templates = array( |
| 26 | 'notices/error.php', |
| 27 | 'notices/notice.php', |
| 28 | 'notices/success.php', |
| 29 | ); |
| 30 | |
| 31 | /** |
| 32 | * Constructor |
| 33 | * |
| 34 | * @param Package $package An instance of the package class. |
| 35 | */ |
| 36 | public function __construct( Package $package ) { |
| 37 | $this->package = $package; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Initialize notice hooks. |
| 42 | */ |
| 43 | public function init() { |
| 44 | add_action( |
| 45 | 'after_setup_theme', |
| 46 | function () { |
| 47 | /** |
| 48 | * Allow classic theme developers to opt-in to using block notices. |
| 49 | * |
| 50 | * @since 8.8.0 |
| 51 | * @param bool $use_block_notices_in_classic_theme Whether to use block notices in classic theme. |
| 52 | * @return bool |
| 53 | */ |
| 54 | if ( wp_is_block_theme() || apply_filters( 'woocommerce_use_block_notices_in_classic_theme', false ) ) { |
| 55 | add_filter( 'wc_get_template', [ $this, 'get_notices_template' ], 10, 5 ); |
| 56 | } |
| 57 | } |
| 58 | ); |
| 59 | |
| 60 | add_filter( 'woocommerce_kses_notice_allowed_tags', [ $this, 'add_kses_notice_allowed_tags' ] ); |
| 61 | add_action( 'wp_head', [ $this, 'enqueue_notice_styles' ] ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Allow SVG icon in notices. |
| 66 | * |
| 67 | * @param array $allowed_tags Allowed tags. |
| 68 | * @return array |
| 69 | */ |
| 70 | public function add_kses_notice_allowed_tags( $allowed_tags ) { |
| 71 | $svg_args = array( |
| 72 | 'svg' => array( |
| 73 | 'aria-hidden' => true, |
| 74 | 'xmlns' => true, |
| 75 | 'width' => true, |
| 76 | 'height' => true, |
| 77 | 'viewbox' => true, |
| 78 | 'focusable' => true, |
| 79 | ), |
| 80 | 'path' => array( |
| 81 | 'd' => true, |
| 82 | ), |
| 83 | ); |
| 84 | return array_merge( $allowed_tags, $svg_args ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Replaces core notice templates with those from blocks. |
| 89 | * |
| 90 | * The new notice templates match block components with matching icons and styling. The differences are: |
| 91 | * 1. Core has notices for info, success, and error notices, blocks has notices for info, success, error, |
| 92 | * warning, and a default notice type. |
| 93 | * 2. The block notices use different CSS classes to the core notices. Core uses `woocommerce-message`, `is-info` |
| 94 | * and `is-error` classes, blocks uses `wc-block-components-notice-banner is-error`, |
| 95 | * `wc-block-components-notice-banner is-info`, and `wc-block-components-notice-banner is-success`. |
| 96 | * 3. The markup of the notices is different, with the block notices using SVG icons and a slightly different |
| 97 | * structure to accommodate this. |
| 98 | * |
| 99 | * @param string $template Located template path. |
| 100 | * @param string $template_name Template name. |
| 101 | * @param array $args Template arguments. |
| 102 | * @param string $template_path Template path. |
| 103 | * @param string $default_path Default path. |
| 104 | * @return string |
| 105 | */ |
| 106 | public function get_notices_template( $template, $template_name, $args, $template_path, $default_path ) { |
| 107 | if ( in_array( $template_name, $this->notice_templates, true ) ) { |
| 108 | $directory = get_stylesheet_directory(); |
| 109 | $file = $directory . '/woocommerce/' . $template_name; |
| 110 | |
| 111 | if ( file_exists( $file ) ) { |
| 112 | return $file; |
| 113 | } |
| 114 | |
| 115 | $template = $this->package->get_path( 'templates/block-' . $template_name ); |
| 116 | wp_enqueue_style( 'wc-blocks-style' ); |
| 117 | } |
| 118 | |
| 119 | return $template; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Replaces all notices with the new block-based notices. |
| 124 | * |
| 125 | * @return void |
| 126 | */ |
| 127 | public function enqueue_notice_styles() { |
| 128 | wp_enqueue_style( 'wc-blocks-style' ); |
| 129 | } |
| 130 | } |
| 131 |