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
OrderWithdrawalController.php
252 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\OrderWithdrawal; |
| 5 | |
| 6 | use Automattic\WooCommerce\Internal\Features\FeaturesController; |
| 7 | use Automattic\WooCommerce\Internal\RegisterHooksInterface; |
| 8 | use Automattic\WooCommerce\Utilities\FeaturesUtil; |
| 9 | |
| 10 | /** |
| 11 | * Registers the order withdrawal endpoint and related feature-gated UI. |
| 12 | * |
| 13 | * @internal Just for internal use. |
| 14 | */ |
| 15 | final class OrderWithdrawalController implements RegisterHooksInterface { |
| 16 | |
| 17 | private const FEATURE_ID = 'order_withdrawal'; |
| 18 | private const ENDPOINT_KEY = 'order-withdrawal'; |
| 19 | private const ENDPOINT_SLUG = 'withdraw-order'; |
| 20 | private const ENDPOINT_OPTION = 'woocommerce_myaccount_order_withdrawal_endpoint'; |
| 21 | |
| 22 | /** |
| 23 | * Form processor. |
| 24 | * |
| 25 | * @var OrderWithdrawalFormProcessor |
| 26 | */ |
| 27 | private OrderWithdrawalFormProcessor $form_processor; |
| 28 | |
| 29 | /** |
| 30 | * Form view. |
| 31 | * |
| 32 | * @var OrderWithdrawalFormView |
| 33 | */ |
| 34 | private OrderWithdrawalFormView $form_view; |
| 35 | |
| 36 | /** |
| 37 | * Feature highlight notification. |
| 38 | * |
| 39 | * @var OrderWithdrawalFeatureHighlightNotification |
| 40 | */ |
| 41 | private OrderWithdrawalFeatureHighlightNotification $feature_highlight_notification; |
| 42 | |
| 43 | /** |
| 44 | * Initialize dependencies. |
| 45 | * |
| 46 | * @param OrderWithdrawalFormProcessor $form_processor Form processor. |
| 47 | * @param OrderWithdrawalFormView $form_view Form view. |
| 48 | * @param OrderWithdrawalFeatureHighlightNotification $feature_highlight_notification Feature highlight notification. |
| 49 | * @internal |
| 50 | * |
| 51 | * @since 11.1.0 |
| 52 | */ |
| 53 | final public function init( OrderWithdrawalFormProcessor $form_processor, OrderWithdrawalFormView $form_view, OrderWithdrawalFeatureHighlightNotification $feature_highlight_notification ): void { // phpcs:ignore Generic.CodeAnalysis.UnnecessaryFinalModifier.Found -- Required by WooCommerce injection method rules. |
| 54 | $this->form_processor = $form_processor; |
| 55 | $this->form_view = $form_view; |
| 56 | $this->feature_highlight_notification = $feature_highlight_notification; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Register hooks. |
| 61 | * |
| 62 | * @since 11.1.0 |
| 63 | */ |
| 64 | public function register(): void { |
| 65 | add_action( FeaturesController::FEATURE_ENABLED_CHANGED_ACTION, array( $this, 'maybe_flush_rewrite_rules' ), 10, 1 ); |
| 66 | add_filter( 'woocommerce_get_query_vars', array( $this, 'add_query_var' ), 10, 1 ); |
| 67 | add_filter( 'woocommerce_endpoint_' . self::ENDPOINT_KEY . '_title', array( $this, 'get_endpoint_title' ), 10, 1 ); |
| 68 | add_filter( 'woocommerce_settings_pages', array( $this, 'add_endpoint_setting' ), 10, 1 ); |
| 69 | add_action( 'woocommerce_account_' . self::ENDPOINT_KEY . '_endpoint', array( $this, 'render_view' ) ); |
| 70 | add_action( 'woocommerce_before_delete_order', array( $this->form_processor, 'delete_order_withdrawal_inbox_note_for_order' ), 10, 1 ); |
| 71 | add_action( 'before_delete_post', array( $this->form_processor, 'delete_order_withdrawal_inbox_note_for_order' ), 10, 1 ); |
| 72 | add_action( 'woocommerce_privacy_remove_order_personal_data', array( $this->form_processor, 'delete_order_withdrawal_inbox_note_for_order' ), 10, 1 ); |
| 73 | add_action( 'init', array( $this, 'maybe_register_feature_highlight_notification' ), 10, 0 ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Whether order withdrawal is enabled. |
| 78 | * |
| 79 | * @since 11.1.0 |
| 80 | */ |
| 81 | public function is_enabled(): bool { |
| 82 | return FeaturesUtil::feature_is_enabled( self::FEATURE_ID ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Whether the current My Account request is for the public order withdrawal endpoint. |
| 87 | * |
| 88 | * @since 11.1.0 |
| 89 | */ |
| 90 | public function is_endpoint_request(): bool { |
| 91 | global $wp; |
| 92 | |
| 93 | return $this->is_enabled() |
| 94 | && isset( $wp->query_vars[ self::ENDPOINT_KEY ] ) |
| 95 | && self::ENDPOINT_KEY === WC()->query->get_current_endpoint(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Queue a rewrite rules flush when the feature is toggled. |
| 100 | * |
| 101 | * @param string $feature_id Feature being toggled. |
| 102 | * |
| 103 | * @since 11.1.0 |
| 104 | */ |
| 105 | public function maybe_flush_rewrite_rules( string $feature_id ): void { |
| 106 | if ( self::FEATURE_ID === $feature_id ) { |
| 107 | update_option( 'woocommerce_queue_flush_rewrite_rules', 'yes' ); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Register the order withdrawal feature highlight notification if the feature is not enabled. |
| 113 | * |
| 114 | * @since 11.1.0 |
| 115 | */ |
| 116 | public function maybe_register_feature_highlight_notification(): void { |
| 117 | if ( ! $this->is_enabled() ) { |
| 118 | $this->feature_highlight_notification->register(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Register the order withdrawal query var. |
| 124 | * |
| 125 | * @param array $query_vars Existing query vars keyed by endpoint key. |
| 126 | * @return array |
| 127 | * |
| 128 | * @since 11.1.0 |
| 129 | */ |
| 130 | public function add_query_var( $query_vars ): array { |
| 131 | if ( ! is_array( $query_vars ) ) { |
| 132 | return array(); |
| 133 | } |
| 134 | |
| 135 | if ( $this->is_enabled() ) { |
| 136 | $query_vars[ self::ENDPOINT_KEY ] = (string) get_option( self::ENDPOINT_OPTION, self::ENDPOINT_SLUG ); |
| 137 | } |
| 138 | |
| 139 | return $query_vars; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Order withdrawal endpoint page title. |
| 144 | * |
| 145 | * @param string $title Default title. |
| 146 | * @return string |
| 147 | * |
| 148 | * @since 11.1.0 |
| 149 | */ |
| 150 | public function get_endpoint_title( $title ): string { |
| 151 | return __( 'Withdraw from contract', 'woocommerce' ); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Add the endpoint setting when the feature is enabled. |
| 156 | * |
| 157 | * @param array $settings Page settings. |
| 158 | * @return array |
| 159 | * |
| 160 | * @since 11.1.0 |
| 161 | */ |
| 162 | public function add_endpoint_setting( $settings ): array { |
| 163 | if ( ! is_array( $settings ) ) { |
| 164 | return array(); |
| 165 | } |
| 166 | |
| 167 | if ( ! $this->is_enabled() ) { |
| 168 | return $settings; |
| 169 | } |
| 170 | |
| 171 | $endpoint_setting = array( |
| 172 | 'title' => __( 'Order withdrawal', 'woocommerce' ), |
| 173 | 'desc' => __( 'Endpoint for the order withdrawal page.', 'woocommerce' ), |
| 174 | 'id' => self::ENDPOINT_OPTION, |
| 175 | 'type' => 'text', |
| 176 | 'default' => self::ENDPOINT_SLUG, |
| 177 | 'desc_tip' => true, |
| 178 | ); |
| 179 | |
| 180 | $new_settings = array(); |
| 181 | $added = false; |
| 182 | |
| 183 | foreach ( $settings as $setting ) { |
| 184 | if ( is_array( $setting ) && self::ENDPOINT_OPTION === ( $setting['id'] ?? '' ) ) { |
| 185 | return $settings; |
| 186 | } |
| 187 | |
| 188 | if ( |
| 189 | ! $added && |
| 190 | is_array( $setting ) && |
| 191 | 'sectionend' === ( $setting['type'] ?? '' ) && |
| 192 | 'account_endpoint_options' === ( $setting['id'] ?? '' ) |
| 193 | ) { |
| 194 | $new_settings[] = $endpoint_setting; |
| 195 | $added = true; |
| 196 | } |
| 197 | |
| 198 | $new_settings[] = $setting; |
| 199 | } |
| 200 | |
| 201 | if ( ! $added ) { |
| 202 | $new_settings[] = $endpoint_setting; |
| 203 | } |
| 204 | |
| 205 | return $new_settings; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Render the order withdrawal view. |
| 210 | * |
| 211 | * @since 11.1.0 |
| 212 | */ |
| 213 | public function render_view(): void { |
| 214 | if ( ! $this->is_enabled() ) { |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | wc_get_template( 'myaccount/form-order-withdrawal.php', $this->get_template_args() ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Get template arguments for the order withdrawal form. |
| 223 | * |
| 224 | * @return array<string,mixed> |
| 225 | */ |
| 226 | private function get_template_args(): array { |
| 227 | return $this->form_view->get_template_args( |
| 228 | $this->form_processor->process_current_request(), |
| 229 | $this->get_form_action_url(), |
| 230 | $this->get_shop_url() |
| 231 | ); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Get a safe shop URL for the confirmation link. |
| 236 | */ |
| 237 | private function get_shop_url(): string { |
| 238 | $shop_url = wc_get_page_permalink( 'shop' ); |
| 239 | |
| 240 | return $shop_url ? $shop_url : home_url( '/' ); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Get the form action URL. |
| 245 | */ |
| 246 | private function get_form_action_url(): string { |
| 247 | $account_url = wc_get_page_permalink( 'myaccount' ); |
| 248 | |
| 249 | return wc_get_endpoint_url( self::ENDPOINT_KEY, '', $account_url ? $account_url : home_url( '/' ) ); |
| 250 | } |
| 251 | } |
| 252 |