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
MenusController.php
137 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\StockNotifications\Admin; |
| 6 | |
| 7 | /** |
| 8 | * Menus controller for Customer Stock Notifications. |
| 9 | */ |
| 10 | class MenusController { |
| 11 | |
| 12 | /** |
| 13 | * Notifications page. |
| 14 | * |
| 15 | * @var NotificationsPage |
| 16 | */ |
| 17 | private $notifications_page; |
| 18 | |
| 19 | /** |
| 20 | * Init. |
| 21 | * |
| 22 | * @internal |
| 23 | * |
| 24 | * @param NotificationsPage $notifications_page Notifications page. |
| 25 | * @return void |
| 26 | */ |
| 27 | final public function init( NotificationsPage $notifications_page ): void { |
| 28 | $this->notifications_page = $notifications_page; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Constructor. |
| 33 | */ |
| 34 | public function __construct() { |
| 35 | |
| 36 | add_action( 'admin_menu', array( $this, 'add_menu' ), 10 ); |
| 37 | add_filter( 'woocommerce_screen_ids', array( $this, 'add_screen_ids' ) ); |
| 38 | add_filter( 'set-screen-option', array( $this, 'set_screen_option' ), 10, 3 ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Add Stock Notifications menu item. |
| 43 | * |
| 44 | * @return bool|void |
| 45 | */ |
| 46 | public function add_menu() { |
| 47 | |
| 48 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | $dashboard_page = add_submenu_page( |
| 53 | 'woocommerce', |
| 54 | __( 'Stock Notifications', 'woocommerce' ), |
| 55 | __( 'Notifications', 'woocommerce' ), |
| 56 | 'manage_woocommerce', |
| 57 | 'wc-customer-stock-notifications', |
| 58 | array( $this, 'notifications_page' ) |
| 59 | ); |
| 60 | |
| 61 | add_action( "load-$dashboard_page", array( $this, 'add_screen_options' ) ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Add screen options support. |
| 66 | * |
| 67 | * @return void |
| 68 | */ |
| 69 | public function add_screen_options(): void { |
| 70 | $screen = get_current_screen(); |
| 71 | |
| 72 | if ( ! $screen ) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | add_screen_option( |
| 77 | 'per_page', |
| 78 | array( |
| 79 | 'label' => __( 'Notifications per page', 'woocommerce' ), |
| 80 | 'default' => 10, |
| 81 | 'option' => 'stock_notifications_per_page', |
| 82 | ) |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Save screen options. |
| 88 | * |
| 89 | * @param int $status The status of the screen option. |
| 90 | * @param string $option The option name. |
| 91 | * @param int $value The value of the screen option. |
| 92 | * |
| 93 | * @return int |
| 94 | */ |
| 95 | public function set_screen_option( $status, $option, $value ): int { |
| 96 | if ( 'stock_notifications_per_page' === $option ) { |
| 97 | return (int) $value; |
| 98 | } |
| 99 | return $status; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Displays the Notifications list table. |
| 104 | */ |
| 105 | public function notifications_page() { |
| 106 | |
| 107 | $action = isset( $_GET['notification_action'] ) ? sanitize_text_field( wp_unslash( $_GET['notification_action'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 108 | |
| 109 | if ( ! in_array( $action, array( 'create', 'edit' ), true ) ) { |
| 110 | $action = ''; |
| 111 | } |
| 112 | |
| 113 | switch ( $action ) { |
| 114 | case 'create': |
| 115 | $this->notifications_page->create(); |
| 116 | break; |
| 117 | case 'edit': |
| 118 | $this->notifications_page->edit(); |
| 119 | break; |
| 120 | default: |
| 121 | $this->notifications_page->output(); |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Add screen id to WooCommerce. |
| 128 | * |
| 129 | * @param array $screen_ids List of screen IDs. |
| 130 | * @return array |
| 131 | */ |
| 132 | public static function add_screen_ids( $screen_ids ): array { |
| 133 | $screen_ids[] = 'woocommerce_page_wc-customer-stock-notifications'; |
| 134 | return $screen_ids; |
| 135 | } |
| 136 | } |
| 137 |