OrderWithdrawalController.php
2 weeks ago
OrderWithdrawalFeatureHighlightNotification.php
2 weeks ago
OrderWithdrawalFormProcessor.php
2 weeks ago
OrderWithdrawalFormState.php
2 weeks ago
OrderWithdrawalFormView.php
2 weeks ago
OrderWithdrawalFeatureHighlightNotification.php
177 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\OrderWithdrawal; |
| 5 | |
| 6 | use Automattic\WooCommerce\Admin\Notes\Note; |
| 7 | use Automattic\WooCommerce\Admin\Notes\DataStore as NotesDataStore; |
| 8 | use Automattic\WooCommerce\Admin\Notes\Notes; |
| 9 | use Automattic\WooCommerce\Internal\RegisterHooksInterface; |
| 10 | use Exception; |
| 11 | |
| 12 | /** |
| 13 | * Adds an inbox notification about the order withdrawal feature for eligible stores. |
| 14 | * |
| 15 | * @internal Just for internal use. |
| 16 | */ |
| 17 | final class OrderWithdrawalFeatureHighlightNotification implements RegisterHooksInterface { |
| 18 | |
| 19 | public const NOTE_NAME = 'wc-admin-order-withdrawal-feature'; |
| 20 | public const CREATED_OPTION = 'woocommerce_order_withdrawal_inbox_notification_created'; |
| 21 | |
| 22 | private const COMING_SOON_OPTION = 'woocommerce_coming_soon'; |
| 23 | private const FEATURES_SETTINGS_URL = 'admin.php?page=wc-settings&tab=advanced§ion=features'; |
| 24 | private const DOCUMENTATION_URL = 'https://woocommerce.com/document/customer-order-withdrawal/'; |
| 25 | |
| 26 | /** |
| 27 | * Register hooks. |
| 28 | * |
| 29 | * @since 11.1.0 |
| 30 | */ |
| 31 | public function register(): void { |
| 32 | add_action( |
| 33 | 'update_option_' . self::COMING_SOON_OPTION, |
| 34 | array( $this, 'maybe_add_note_when_store_goes_live' ), |
| 35 | 10, |
| 36 | 2 |
| 37 | ); |
| 38 | add_action( 'wc_admin_daily', array( $this, 'possibly_add_note' ) ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Add the note when the store is changed from coming soon to live. |
| 43 | * |
| 44 | * This is called when the `woocommerce_coming_soon` option is updated. It checks if the store has gone live and if so, it calls the `possibly_add_note` method to add the note. |
| 45 | * |
| 46 | * @internal |
| 47 | * @since 11.1.0 |
| 48 | * |
| 49 | * @param mixed $old_value Previous option value. |
| 50 | * @param mixed $value New option value. |
| 51 | */ |
| 52 | public function maybe_add_note_when_store_goes_live( $old_value, $value ): void { |
| 53 | if ( 'yes' !== $old_value || 'no' !== $value ) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | $this->possibly_add_note(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Add the note if the store is eligible and it has never been created before. |
| 62 | */ |
| 63 | public function possibly_add_note(): void { |
| 64 | try { |
| 65 | if ( $this->has_note_been_created() ) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | if ( ! $this->is_applicable() ) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | if ( ! add_option( self::CREATED_OPTION, 'yes', '', false ) ) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | $this->get_note()->save(); |
| 78 | } catch ( Exception $exception ) { |
| 79 | delete_option( self::CREATED_OPTION ); |
| 80 | wc_get_logger()->error( |
| 81 | 'Unable to create the order withdrawal inbox notification.', |
| 82 | array( |
| 83 | 'source' => 'order-withdrawal', |
| 84 | 'exception' => $exception, |
| 85 | ) |
| 86 | ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Whether the notification is relevant for the current store settings. |
| 92 | */ |
| 93 | private function is_applicable(): bool { |
| 94 | return 'no' === get_option( self::COMING_SOON_OPTION, 'yes' ) |
| 95 | && $this->store_sells_to_eu_or_all_countries(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get the inbox note. |
| 100 | */ |
| 101 | private function get_note(): Note { |
| 102 | $note = new Note(); |
| 103 | |
| 104 | $note->set_title( |
| 105 | __( 'Enable order withdrawal for EU regulatory requirements', 'woocommerce' ) |
| 106 | ); |
| 107 | $note->set_content( |
| 108 | __( |
| 109 | 'Stores selling to EU countries may need to offer customers a way to withdraw from qualifying orders. Review how to enable the order withdrawal feature in the Advanced settings.', |
| 110 | 'woocommerce' |
| 111 | ) |
| 112 | ); |
| 113 | $note->set_content_data( (object) array() ); |
| 114 | $note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL ); |
| 115 | $note->set_name( self::NOTE_NAME ); |
| 116 | $note->set_source( 'woocommerce-admin' ); |
| 117 | $note->add_action( |
| 118 | 'review-feature-settings', |
| 119 | __( 'Get started', 'woocommerce' ), |
| 120 | admin_url( self::FEATURES_SETTINGS_URL ), |
| 121 | Note::E_WC_ADMIN_NOTE_ACTIONED, |
| 122 | true |
| 123 | ); |
| 124 | $note->add_action( |
| 125 | 'learn-more', |
| 126 | __( 'Learn more', 'woocommerce' ), |
| 127 | self::DOCUMENTATION_URL, |
| 128 | Note::E_WC_ADMIN_NOTE_UNACTIONED |
| 129 | ); |
| 130 | |
| 131 | return $note; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Whether this note has already been created, including soft-deleted notes. |
| 136 | */ |
| 137 | private function has_note_been_created(): bool { |
| 138 | if ( 'yes' === get_option( self::CREATED_OPTION, 'no' ) ) { |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Data store instance. |
| 144 | * |
| 145 | * @var NotesDataStore $data_store |
| 146 | */ |
| 147 | $data_store = Notes::load_data_store(); |
| 148 | $note_ids = $data_store->get_notes_with_name( self::NOTE_NAME ); |
| 149 | |
| 150 | if ( empty( $note_ids ) ) { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | update_option( self::CREATED_OPTION, 'yes', false ); |
| 155 | |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Whether the store sells to at least one EU country. |
| 161 | */ |
| 162 | private function store_sells_to_eu_or_all_countries(): bool { |
| 163 | $woocommerce = function_exists( 'WC' ) ? WC() : null; |
| 164 | |
| 165 | if ( ! $woocommerce || ! $woocommerce->countries instanceof \WC_Countries ) { |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | return ! empty( |
| 170 | array_intersect( |
| 171 | $woocommerce->countries->get_european_union_countries(), |
| 172 | array_keys( $woocommerce->countries->get_allowed_countries() ) |
| 173 | ) |
| 174 | ); |
| 175 | } |
| 176 | } |
| 177 |