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
NotificationCreatePage.php
121 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 | |
| 11 | /** |
| 12 | * Notification create page for Customer Stock Notifications. |
| 13 | */ |
| 14 | class NotificationCreatePage { |
| 15 | |
| 16 | /** |
| 17 | * Render page. |
| 18 | */ |
| 19 | public function output() { |
| 20 | $this->process_create_form(); |
| 21 | include __DIR__ . '/Templates/html-admin-notification-create.php'; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Create and save notification. |
| 26 | */ |
| 27 | public function process_create_form() { |
| 28 | if ( empty( $_POST ) ) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | check_admin_referer( 'woocommerce-customer-stock-notification-create', 'customer_stock_notification_create_security' ); |
| 33 | |
| 34 | if ( ! isset( $_POST['save'] ) ) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | if ( ! isset( $_POST['product_id'] ) || empty( $_POST['product_id'] ) ) { |
| 39 | NotificationsPage::add_notice( __( 'Please select a product.', 'woocommerce' ), 'error' ); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | if ( empty( $_POST['user_id'] ) && empty( $_POST['user_email'] ) ) { |
| 44 | NotificationsPage::add_notice( __( 'Please select a customer.', 'woocommerce' ), 'error' ); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | // Posted data. |
| 49 | $posted_data = array(); |
| 50 | $posted_data['product_id'] = absint( wp_unslash( $_POST['product_id'] ) ); |
| 51 | |
| 52 | if ( isset( $_POST['user_id'] ) && ! empty( $_POST['user_id'] ) ) { |
| 53 | |
| 54 | $posted_data['user_id'] = absint( wp_unslash( $_POST['user_id'] ) ); |
| 55 | if ( 0 === $posted_data['user_id'] ) { |
| 56 | NotificationsPage::add_notice( __( 'Please select a customer.', 'woocommerce' ), 'error' ); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | $user = get_user_by( 'id', $posted_data['user_id'] ); |
| 61 | $posted_data['user_email'] = is_a( $user, 'WP_User' ) ? $user->user_email : ''; |
| 62 | |
| 63 | } elseif ( isset( $_POST['user_email'] ) && ! empty( $_POST['user_email'] ) ) { |
| 64 | |
| 65 | $posted_data['user_email'] = sanitize_text_field( wp_unslash( $_POST['user_email'] ) ); |
| 66 | if ( ! filter_var( $posted_data['user_email'], FILTER_VALIDATE_EMAIL ) ) { |
| 67 | NotificationsPage::add_notice( __( 'Please enter a valid email address.', 'woocommerce' ), 'error' ); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | $user = get_user_by( 'email', $posted_data['user_email'] ); |
| 72 | $posted_data['user_id'] = is_a( $user, 'WP_User' ) ? $user->ID : 0; |
| 73 | } |
| 74 | |
| 75 | // Check if a notification already exists for the same product and customer. |
| 76 | $notification_ids = \WC_Data_Store::load( 'stock_notification' )->query( $posted_data ); |
| 77 | if ( count( $notification_ids ) > 0 ) { |
| 78 | $notice_message = sprintf( |
| 79 | // translators: %s: notification edit url. |
| 80 | __( |
| 81 | 'A <a href="%s">notification</a> for the same product and customer already exists in your database.', |
| 82 | 'woocommerce' |
| 83 | ), |
| 84 | admin_url( NotificationsPage::PAGE_URL . '¬ification_action=edit¬ification_id=' . $notification_ids[0] ) |
| 85 | ); |
| 86 | NotificationsPage::add_notice( $notice_message, 'error' ); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | // Save notification. |
| 91 | $notification = new Notification(); |
| 92 | $notification->set_status( NotificationStatus::ACTIVE ); |
| 93 | $notification->set_product_id( $posted_data['product_id'] ); |
| 94 | $notification->set_user_id( $posted_data['user_id'] ); |
| 95 | $notification->set_user_email( $posted_data['user_email'] ); |
| 96 | $result = $notification->save(); |
| 97 | |
| 98 | if ( is_wp_error( $result ) ) { |
| 99 | $notice_message = $result->get_error_message(); |
| 100 | NotificationsPage::add_notice( $notice_message, 'error' ); |
| 101 | return; |
| 102 | } else { |
| 103 | |
| 104 | $notice_message = __( 'Notification created.', 'woocommerce' ); |
| 105 | NotificationsPage::add_notice( $notice_message, 'success' ); |
| 106 | |
| 107 | // Construct edit url. |
| 108 | $edit_url = add_query_arg( |
| 109 | array( |
| 110 | 'notification_action' => 'edit', |
| 111 | 'notification_id' => $notification->get_id(), |
| 112 | ), |
| 113 | NotificationsPage::PAGE_URL |
| 114 | ); |
| 115 | |
| 116 | wp_safe_redirect( $edit_url ); |
| 117 | exit; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 |