Templates
4 weeks ago
AdminManager.php
10 months ago
ListTable.php
10 months ago
MenusController.php
10 months ago
NotificationCreatePage.php
10 months ago
NotificationEditPage.php
4 weeks ago
NotificationsPage.php
4 weeks ago
SettingsController.php
10 months ago
NotificationEditPage.php
151 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\StockNotifications\Admin; |
| 6 | |
| 7 | use Automattic\WooCommerce\Internal\StockNotifications\Notification; |
| 8 | use Automattic\WooCommerce\Internal\StockNotifications\Enums\NotificationStatus; |
| 9 | use Automattic\WooCommerce\Internal\StockNotifications\Admin\NotificationsPage; |
| 10 | use Automattic\WooCommerce\Internal\StockNotifications\Factory; |
| 11 | use Automattic\WooCommerce\Internal\StockNotifications\Emails\EmailManager; |
| 12 | use Automattic\WooCommerce\Internal\StockNotifications\Admin\ListTable; |
| 13 | use Automattic\WooCommerce\Internal\StockNotifications\Enums\NotificationCancellationSource; |
| 14 | |
| 15 | /** |
| 16 | * Notification create page for Customer Stock Notifications. |
| 17 | */ |
| 18 | class NotificationEditPage { |
| 19 | |
| 20 | /** |
| 21 | * Email manager. |
| 22 | * |
| 23 | * @var EmailManager |
| 24 | */ |
| 25 | private EmailManager $email_manager; |
| 26 | |
| 27 | /** |
| 28 | * Init the service. |
| 29 | * |
| 30 | * @internal |
| 31 | * |
| 32 | * @param EmailManager $email_manager The email manager. |
| 33 | */ |
| 34 | final public function init( EmailManager $email_manager ): void { |
| 35 | $this->email_manager = $email_manager; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Render page. |
| 40 | */ |
| 41 | public function output() { |
| 42 | $table = new ListTable(); |
| 43 | $notification_id = isset( $_GET['notification_id'] ) ? absint( wp_unslash( $_GET['notification_id'] ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 44 | if ( $notification_id ) { |
| 45 | $notification = Factory::get_notification( $notification_id ); |
| 46 | } |
| 47 | |
| 48 | if ( ! $notification instanceof Notification ) { |
| 49 | $notice_message = __( 'Notification not found.', 'woocommerce' ); |
| 50 | NotificationsPage::add_notice( $notice_message, 'error' ); |
| 51 | wp_safe_redirect( admin_url( NotificationsPage::PAGE_URL ) ); |
| 52 | exit; |
| 53 | } |
| 54 | |
| 55 | $this->process_edit_form( $notification ); |
| 56 | $table->process_delete_action(); |
| 57 | |
| 58 | $signed_up_customers = $table->data_store->query( |
| 59 | array( |
| 60 | 'product_id' => $notification->get_product_id(), |
| 61 | 'return' => 'count', |
| 62 | ) |
| 63 | ); |
| 64 | |
| 65 | include __DIR__ . '/Templates/html-admin-notification-edit.php'; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Update notification. |
| 70 | * |
| 71 | * @param Notification $notification The notification object. |
| 72 | * @return void |
| 73 | */ |
| 74 | public function process_edit_form( Notification $notification ) { |
| 75 | |
| 76 | if ( empty( $_POST ) || empty( $_POST['wc_customer_stock_notification_action'] ) ) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | check_admin_referer( 'woocommerce-customer-stock-notification-edit', 'customer_stock_notification_edit_security' ); |
| 81 | |
| 82 | $action = wc_clean( wp_unslash( $_POST['wc_customer_stock_notification_action'] ) ); |
| 83 | switch ( $action ) { |
| 84 | case 'activate_notification': |
| 85 | $notification->set_status( NotificationStatus::ACTIVE ); |
| 86 | $result = $notification->save(); |
| 87 | if ( is_wp_error( $result ) ) { |
| 88 | $notice_message = $result->get_error_message(); |
| 89 | NotificationsPage::add_notice( $notice_message, 'error' ); |
| 90 | } else { |
| 91 | $notice_message = __( 'Notification updated.', 'woocommerce' ); |
| 92 | NotificationsPage::add_notice( $notice_message, 'success' ); |
| 93 | } |
| 94 | break; |
| 95 | case 'cancel_notification': |
| 96 | $notification->set_status( NotificationStatus::CANCELLED ); |
| 97 | $notification->set_date_cancelled( time() ); |
| 98 | $notification->set_cancellation_source( NotificationCancellationSource::ADMIN ); |
| 99 | $result = $notification->save(); |
| 100 | if ( is_wp_error( $result ) ) { |
| 101 | $notice_message = $result->get_error_message(); |
| 102 | NotificationsPage::add_notice( $notice_message, 'error' ); |
| 103 | } else { |
| 104 | $notice_message = __( 'Notification updated.', 'woocommerce' ); |
| 105 | NotificationsPage::add_notice( $notice_message, 'success' ); |
| 106 | } |
| 107 | break; |
| 108 | case 'send_notification': |
| 109 | $product = $notification->get_product(); |
| 110 | |
| 111 | if ( ! $product || ! $product->is_in_stock() ) { |
| 112 | $notice_message = __( 'Failed to send notification. Please make sure that the listed product is available.', 'woocommerce' ); |
| 113 | NotificationsPage::add_notice( $notice_message, 'error' ); |
| 114 | } else { |
| 115 | $this->email_manager->send_stock_notification_email( $notification ); |
| 116 | $notification->set_status( NotificationStatus::SENT ); |
| 117 | $notification->set_date_notified( time() ); |
| 118 | $notification->save(); |
| 119 | // translators: %s user email. |
| 120 | $notice_message = sprintf( __( 'Notification sent to "%s".', 'woocommerce' ), $notification->get_user_email() ); |
| 121 | NotificationsPage::add_notice( $notice_message, 'success' ); |
| 122 | } |
| 123 | break; |
| 124 | case 'send_verification_email': |
| 125 | if ( NotificationStatus::PENDING !== $notification->get_status() ) { |
| 126 | $notice_message = __( 'Cannot resend verification: this notification is already verified or cancelled.', 'woocommerce' ); |
| 127 | NotificationsPage::add_notice( $notice_message, 'error' ); |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | $this->email_manager->send_verify_email( $notification ); |
| 132 | // translators: %s user email. |
| 133 | $notice_message = sprintf( __( 'Verification email sent to "%s".', 'woocommerce' ), $notification->get_user_email() ); |
| 134 | NotificationsPage::add_notice( $notice_message, 'success' ); |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | // Construct edit url. |
| 139 | $edit_url = add_query_arg( |
| 140 | array( |
| 141 | 'notification_action' => 'edit', |
| 142 | 'notification_id' => $notification->get_id(), |
| 143 | ), |
| 144 | NotificationsPage::PAGE_URL |
| 145 | ); |
| 146 | |
| 147 | wp_safe_redirect( $edit_url ); |
| 148 | exit; |
| 149 | } |
| 150 | } |
| 151 |