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
NotificationsPage.php
123 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\StockNotifications\Admin; |
| 6 | |
| 7 | use Automattic\WooCommerce\Internal\StockNotifications\Admin\ListTable; |
| 8 | use Automattic\WooCommerce\Internal\StockNotifications\Admin\NotificationCreatePage; |
| 9 | use Automattic\WooCommerce\Internal\StockNotifications\Admin\NotificationEditPage; |
| 10 | |
| 11 | /** |
| 12 | * Notifications admin page for Customer Stock Notifications. |
| 13 | */ |
| 14 | class NotificationsPage { |
| 15 | |
| 16 | /** |
| 17 | * Page URL. |
| 18 | * |
| 19 | * @const PAGE_URL |
| 20 | */ |
| 21 | const PAGE_URL = 'admin.php?page=wc-customer-stock-notifications'; |
| 22 | |
| 23 | /** |
| 24 | * Notices option name. |
| 25 | */ |
| 26 | const ADMIN_NOTICE_OPTION_NAME = 'wc_customer_stock_notifications_admin_notice'; |
| 27 | |
| 28 | /** |
| 29 | * Render page. |
| 30 | */ |
| 31 | public function output() { |
| 32 | $table = wc_get_container()->get( ListTable::class ); |
| 33 | $table->process_actions(); |
| 34 | $this->output_admin_notice(); |
| 35 | $table->prepare_items(); |
| 36 | include __DIR__ . '/Templates/html-admin-notifications.php'; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Create notification. |
| 41 | */ |
| 42 | public function create() { |
| 43 | $create_page = new NotificationCreatePage(); |
| 44 | $create_page->output(); |
| 45 | $this->output_admin_notice(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Edit notification. |
| 50 | */ |
| 51 | public function edit() { |
| 52 | $edit_page = wc_get_container()->get( NotificationEditPage::class ); |
| 53 | $edit_page->output(); |
| 54 | $this->output_admin_notice(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Add a notice to the admin notices. |
| 59 | * |
| 60 | * @param string $message The notice message. |
| 61 | * @param string $type The notice type (optional). |
| 62 | * @return void |
| 63 | */ |
| 64 | public static function add_notice( $message, $type = 'info' ) { |
| 65 | if ( empty( $message ) ) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | $notice_data = get_option( self::ADMIN_NOTICE_OPTION_NAME ); |
| 70 | if ( false !== $notice_data ) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | if ( ! in_array( $type, array( 'error', 'warning', 'success', 'info' ), true ) ) { |
| 75 | $type = 'info'; |
| 76 | } |
| 77 | |
| 78 | $notice_data = array( |
| 79 | 'message' => $message, |
| 80 | 'type' => $type, |
| 81 | ); |
| 82 | |
| 83 | update_option( self::ADMIN_NOTICE_OPTION_NAME, $notice_data ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Display admin notices. |
| 88 | * |
| 89 | * @return void |
| 90 | */ |
| 91 | public function output_admin_notice(): void { |
| 92 | if ( ! function_exists( 'wp_admin_notice' ) ) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | $notice_data = get_option( self::ADMIN_NOTICE_OPTION_NAME ); |
| 97 | if ( false === $notice_data ) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | // Check if invalid data. |
| 102 | if ( empty( $notice_data ) || ! is_array( $notice_data ) || empty( $notice_data['message'] ) ) { |
| 103 | delete_option( self::ADMIN_NOTICE_OPTION_NAME ); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | $type = in_array( $notice_data['type'], array( 'error', 'warning', 'success', 'info' ), true ) |
| 108 | ? $notice_data['type'] |
| 109 | : 'info'; |
| 110 | |
| 111 | \wp_admin_notice( |
| 112 | $notice_data['message'], |
| 113 | array( |
| 114 | 'type' => $type, |
| 115 | 'id' => self::ADMIN_NOTICE_OPTION_NAME, |
| 116 | 'dismissible' => false, |
| 117 | ) |
| 118 | ); |
| 119 | |
| 120 | delete_option( self::ADMIN_NOTICE_OPTION_NAME ); |
| 121 | } |
| 122 | } |
| 123 |