| 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 |
* Script handle for the api-fetch `_method` shim (assets/js/api-fetch-method-param.js). |
| 35 |
* |
| 36 |
* Pro references this handle by its string value, guarded by |
| 37 |
* wp_script_is( ..., 'registered' ), so the value is a compatibility contract. |
| 38 |
*/ |
| 39 |
public const API_FETCH_METHOD_PARAM_HANDLE = 'wcpos-api-fetch-method-param'; |
| 40 |
|
| 41 |
/** |
| 42 |
* POS Menu IDs. |
| 43 |
* |
| 44 |
* @var string[] Unique menu identifier. |
| 45 |
*/ |
| 46 |
private $menu_ids = array(); |
| 47 |
|
| 48 |
/** |
| 49 |
* Registered screen handlers. |
| 50 |
* |
| 51 |
* @var array |
| 52 |
*/ |
| 53 |
private $screen_handlers = array(); |
| 54 |
|
| 55 |
/** |
| 56 |
* Constructor. |
| 57 |
* |
| 58 |
* NOTE: WordPress fires the admin_menu hook before the admin_init. |
| 59 |
* 1. admin_menu |
| 60 |
* 2. admin_init |
| 61 |
* 3. current_screen |
| 62 |
* |
| 63 |
* We need admin_menu at priority 5 so that we can hook the Analytics menu before WooCommerce. |
| 64 |
*/ |
| 65 |
public function __construct() { |
| 66 |
add_action( 'admin_menu', array( $this, 'admin_menu' ), 5 ); |
| 67 |
add_action( 'admin_init', array( $this, 'init' ) ); |
| 68 |
add_action( 'admin_init', array( $this, 'register_scripts' ) ); |
| 69 |
add_action( 'current_screen', array( $this, 'current_screen' ) ); |
| 70 |
|
| 71 |
// register the screen handlers. |
| 72 |
$this->screen_handlers = array( |
| 73 |
'options-permalink' => Permalink::class, |
| 74 |
'product' => Single_Product::class, |
| 75 |
'edit-product' => List_Products::class, |
| 76 |
'shop_order' => Single_Order::class, |
| 77 |
'edit-shop_order' => List_Orders::class, |
| 78 |
'plugins' => Plugins::class, |
| 79 |
'wcpos_template' => Single_Template::class, |
| 80 |
'woocommerce_page_wc-orders' => array( $this, 'handle_wc_hpos_orders_screen' ), |
| 81 |
'woocommerce_page_wc-admin' => array( $this, 'handle_wc_analytics_screen' ), |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Load admin subclasses. |
| 87 |
*/ |
| 88 |
public function init(): void { |
| 89 |
new Notices(); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Register the shared admin scripts that WCPOS bundles depend on. |
| 94 |
* |
| 95 |
* The api-fetch shim routes PUT/PATCH/DELETE through POST + `?_method=` |
| 96 |
* so wp-admin mutations survive hosts that 403 the bare verbs or the |
| 97 |
* `X-HTTP-Method-Override` header (see the shim's header comment). It is |
| 98 |
* only registered here; each bundle that uses `wp.apiFetch` enqueues it |
| 99 |
* by listing self::API_FETCH_METHOD_PARAM_HANDLE as a dependency. |
| 100 |
*/ |
| 101 |
public function register_scripts(): void { |
| 102 |
wp_register_script( |
| 103 |
self::API_FETCH_METHOD_PARAM_HANDLE, |
| 104 |
PLUGIN_URL . 'assets/js/api-fetch-method-param.js', |
| 105 |
array( 'wp-api-fetch' ), |
| 106 |
VERSION, |
| 107 |
true |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Fires before the administration menu loads in the admin. |
| 113 |
*/ |
| 114 |
public function admin_menu(): void { |
| 115 |
$menu = new Admin\Menu(); |
| 116 |
$this->menu_ids = array( |
| 117 |
'toplevel' => $menu->toplevel_screen_id, |
| 118 |
'settings' => $menu->settings_screen_id, |
| 119 |
); |
| 120 |
|
| 121 |
// Add the settings screen using the WCPOS menu ID. |
| 122 |
$this->screen_handlers[ $this->menu_ids['settings'] ] = Settings::class; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Conditionally load subclasses based on admin screen. |
| 127 |
* |
| 128 |
* @param WP_Screen $current_screen Current screen object. |
| 129 |
*/ |
| 130 |
public function current_screen( $current_screen ): void { |
| 131 |
/* |
| 132 |
* Backwards compatibility for WCPOS Pro 1.4.2 and below. |
| 133 |
* DO NOT USE THIS! |
| 134 |
* |
| 135 |
* @TODO: Remove in WCPOS 2.0.0. |
| 136 |
*/ |
| 137 |
$this->screen_handlers['product'] = apply_filters( 'woocommerce_pos_single_product_admin_class', Single_Product::class ); |
| 138 |
|
| 139 |
/** |
| 140 |
* Filters the screen handlers for WCPOS admin screens. |
| 141 |
* |
| 142 |
* @hook woocommerce_pos_admin_screen_handlers |
| 143 |
* |
| 144 |
* @since 1.4.10 |
| 145 |
* |
| 146 |
* @param array $handlers Associative array of screen IDs and their corresponding handlers. |
| 147 |
* Handler can be a class name or a callback array. |
| 148 |
* @param WP_Screen $current_screen The current WP_Screen object being loaded in the admin. |
| 149 |
*/ |
| 150 |
$handlers = apply_filters( 'woocommerce_pos_admin_screen_handlers', $this->screen_handlers, $current_screen ); |
| 151 |
|
| 152 |
// Check if the current screen has a handler. |
| 153 |
if ( isset( $handlers[ $current_screen->id ] ) ) { |
| 154 |
$handler = $handlers[ $current_screen->id ]; |
| 155 |
|
| 156 |
if ( \is_array( $handler ) && method_exists( $handler[0], $handler[1] ) ) { |
| 157 |
\call_user_func( $handler ); |
| 158 |
} elseif ( class_exists( $handler ) ) { |
| 159 |
new $handler(); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Handle WooCommerce HPOS orders screen. |
| 166 |
*/ |
| 167 |
public function handle_wc_hpos_orders_screen(): void { |
| 168 |
if ( isset( $_GET['action'] ) && 'edit' === $_GET['action'] ) { |
| 169 |
new HPOS_Single_Order(); |
| 170 |
} else { |
| 171 |
new HPOS_List_Orders(); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Handle WooCommerce analytics screen. |
| 177 |
*/ |
| 178 |
public function handle_wc_analytics_screen(): void { |
| 179 |
if ( class_exists( '\Automattic\WooCommerce\Admin\PageController' ) ) { |
| 180 |
$wc_admin_page_controller = PageController::get_instance(); |
| 181 |
if ( $wc_admin_page_controller ) { |
| 182 |
$wc_admin_current_page = $wc_admin_page_controller->get_current_page(); |
| 183 |
$id = $wc_admin_current_page['id'] ?? null; |
| 184 |
$parent = $wc_admin_current_page['parent'] ?? null; |
| 185 |
|
| 186 |
if ( 'woocommerce-analytics' === $id || 'woocommerce-analytics' === $parent ) { |
| 187 |
new Analytics(); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|