| 1 |
<?php |
| 2 |
/** |
| 3 |
* WP Admin Class |
| 4 |
* conditionally loads classes for WP Admin. |
| 5 |
* |
| 6 |
* @author Paul Kilmurray <paul@kilbot.com.au> |
| 7 |
* |
| 8 |
* @see http://www.wcpos.com |
| 9 |
* @package WCPOS\WooCommercePOS |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace WCPOS\WooCommercePOS; |
| 13 |
|
| 14 |
use Automattic\WooCommerce\Admin\PageController; |
| 15 |
use WCPOS\WooCommercePOS\Admin\Analytics; |
| 16 |
use WCPOS\WooCommercePOS\Admin\Notices; |
| 17 |
use WCPOS\WooCommercePOS\Admin\Orders\HPOS_List_Orders; |
| 18 |
use WCPOS\WooCommercePOS\Admin\Orders\HPOS_Single_Order; |
| 19 |
use WCPOS\WooCommercePOS\Admin\Orders\List_Orders; |
| 20 |
use WCPOS\WooCommercePOS\Admin\Orders\Single_Order; |
| 21 |
use WCPOS\WooCommercePOS\Admin\Permalink; |
| 22 |
use WCPOS\WooCommercePOS\Admin\Plugins; |
| 23 |
use WCPOS\WooCommercePOS\Admin\Products\List_Products; |
| 24 |
use WCPOS\WooCommercePOS\Admin\Products\Single_Product; |
| 25 |
use WCPOS\WooCommercePOS\Admin\Settings; |
| 26 |
use WCPOS\WooCommercePOS\Admin\Templates\Single_Template; |
| 27 |
use WP_Screen; |
| 28 |
|
| 29 |
/** |
| 30 |
* Admin class. |
| 31 |
*/ |
| 32 |
class Admin { |
| 33 |
/** |
| 34 |
* POS Menu IDs. |
| 35 |
* |
| 36 |
* @var string[] Unique menu identifier. |
| 37 |
*/ |
| 38 |
private $menu_ids = array(); |
| 39 |
|
| 40 |
/** |
| 41 |
* Registered screen handlers. |
| 42 |
* |
| 43 |
* @var array |
| 44 |
*/ |
| 45 |
private $screen_handlers = array(); |
| 46 |
|
| 47 |
/** |
| 48 |
* Constructor. |
| 49 |
* |
| 50 |
* NOTE: WordPress fires the admin_menu hook before the admin_init. |
| 51 |
* 1. admin_menu |
| 52 |
* 2. admin_init |
| 53 |
* 3. current_screen |
| 54 |
* |
| 55 |
* We need admin_menu at priority 5 so that we can hook the Analytics menu before WooCommerce. |
| 56 |
*/ |
| 57 |
public function __construct() { |
| 58 |
add_action( 'admin_menu', array( $this, 'admin_menu' ), 5 ); |
| 59 |
add_action( 'admin_init', array( $this, 'init' ) ); |
| 60 |
add_action( 'current_screen', array( $this, 'current_screen' ) ); |
| 61 |
|
| 62 |
// register the screen handlers. |
| 63 |
$this->screen_handlers = array( |
| 64 |
'options-permalink' => Permalink::class, |
| 65 |
'product' => Single_Product::class, |
| 66 |
'edit-product' => List_Products::class, |
| 67 |
'shop_order' => Single_Order::class, |
| 68 |
'edit-shop_order' => List_Orders::class, |
| 69 |
'plugins' => Plugins::class, |
| 70 |
'wcpos_template' => Single_Template::class, |
| 71 |
'woocommerce_page_wc-orders' => array( $this, 'handle_wc_hpos_orders_screen' ), |
| 72 |
'woocommerce_page_wc-admin' => array( $this, 'handle_wc_analytics_screen' ), |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Load admin subclasses. |
| 78 |
*/ |
| 79 |
public function init(): void { |
| 80 |
new Notices(); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Fires before the administration menu loads in the admin. |
| 85 |
*/ |
| 86 |
public function admin_menu(): void { |
| 87 |
$menu = new Admin\Menu(); |
| 88 |
$this->menu_ids = array( |
| 89 |
'toplevel' => $menu->toplevel_screen_id, |
| 90 |
'settings' => $menu->settings_screen_id, |
| 91 |
); |
| 92 |
|
| 93 |
// Add the settings screen using the WCPOS menu ID. |
| 94 |
$this->screen_handlers[ $this->menu_ids['settings'] ] = Settings::class; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Conditionally load subclasses based on admin screen. |
| 99 |
* |
| 100 |
* @param WP_Screen $current_screen Current screen object. |
| 101 |
*/ |
| 102 |
public function current_screen( $current_screen ): void { |
| 103 |
/* |
| 104 |
* Backwards compatibility for WCPOS Pro 1.4.2 and below. |
| 105 |
* DO NOT USE THIS! |
| 106 |
* |
| 107 |
* @TODO: Remove in WCPOS 2.0.0. |
| 108 |
*/ |
| 109 |
$this->screen_handlers['product'] = apply_filters( 'woocommerce_pos_single_product_admin_class', Single_Product::class ); |
| 110 |
|
| 111 |
/** |
| 112 |
* Filters the screen handlers for WCPOS admin screens. |
| 113 |
* |
| 114 |
* @hook woocommerce_pos_admin_screen_handlers |
| 115 |
* |
| 116 |
* @since 1.4.10 |
| 117 |
* |
| 118 |
* @param array $handlers Associative array of screen IDs and their corresponding handlers. |
| 119 |
* Handler can be a class name or a callback array. |
| 120 |
* @param WP_Screen $current_screen The current WP_Screen object being loaded in the admin. |
| 121 |
*/ |
| 122 |
$handlers = apply_filters( 'woocommerce_pos_admin_screen_handlers', $this->screen_handlers, $current_screen ); |
| 123 |
|
| 124 |
// Check if the current screen has a handler. |
| 125 |
if ( isset( $handlers[ $current_screen->id ] ) ) { |
| 126 |
$handler = $handlers[ $current_screen->id ]; |
| 127 |
|
| 128 |
if ( \is_array( $handler ) && method_exists( $handler[0], $handler[1] ) ) { |
| 129 |
\call_user_func( $handler ); |
| 130 |
} elseif ( class_exists( $handler ) ) { |
| 131 |
new $handler(); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Handle WooCommerce HPOS orders screen. |
| 138 |
*/ |
| 139 |
public function handle_wc_hpos_orders_screen(): void { |
| 140 |
if ( isset( $_GET['action'] ) && 'edit' === $_GET['action'] ) { |
| 141 |
new HPOS_Single_Order(); |
| 142 |
} else { |
| 143 |
new HPOS_List_Orders(); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Handle WooCommerce analytics screen. |
| 149 |
*/ |
| 150 |
public function handle_wc_analytics_screen(): void { |
| 151 |
if ( class_exists( '\Automattic\WooCommerce\Admin\PageController' ) ) { |
| 152 |
$wc_admin_page_controller = PageController::get_instance(); |
| 153 |
if ( $wc_admin_page_controller ) { |
| 154 |
$wc_admin_current_page = $wc_admin_page_controller->get_current_page(); |
| 155 |
$id = $wc_admin_current_page['id'] ?? null; |
| 156 |
$parent = $wc_admin_current_page['parent'] ?? null; |
| 157 |
|
| 158 |
if ( 'woocommerce-analytics' === $id || 'woocommerce-analytics' === $parent ) { |
| 159 |
new Analytics(); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|