Helper.php
73 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\COT; |
| 4 | |
| 5 | use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController; |
| 6 | use Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer; |
| 7 | use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore; |
| 8 | use Automattic\WooCommerce\Utilities\OrderUtil; |
| 9 | use WPML\FP\Maybe; |
| 10 | use WPML\FP\Obj; |
| 11 | use function WPML\FP\invoke; |
| 12 | |
| 13 | class Helper { |
| 14 | |
| 15 | |
| 16 | const WC_ORDERS = 'wc-orders'; |
| 17 | |
| 18 | public static function getTableExists() { |
| 19 | return self::callStaticMethod( DataSynchronizer::class, 'get_table_exists', false ); |
| 20 | } |
| 21 | |
| 22 | public static function getTableName() { |
| 23 | return self::callStaticMethod( OrdersTableDataStore::class, 'get_orders_table_name', null ); |
| 24 | } |
| 25 | |
| 26 | public static function getMetaTableName() { |
| 27 | return self::callStaticMethod( OrdersTableDataStore::class, 'get_meta_table_name', null ); |
| 28 | } |
| 29 | |
| 30 | public static function isUsageEnabled() { |
| 31 | return self::callStaticMethod( CustomOrdersTableController::class, 'custom_orders_table_usage_is_enabled', false ); |
| 32 | } |
| 33 | |
| 34 | public static function isOrder( int $id ) : bool { |
| 35 | return OrderUtil::is_order( $id ); |
| 36 | } |
| 37 | |
| 38 | private static function callStaticMethod( $wcClass, $method, $default ) { |
| 39 | return Maybe::fromNullable( self::getFromContainer( $wcClass ) ) |
| 40 | ->map( invoke( $method ) ) |
| 41 | ->getOrElse( $default ); |
| 42 | } |
| 43 | |
| 44 | private static function getFromContainer( $wcClass ) { |
| 45 | try { |
| 46 | $object = \wc_get_container()->get( $wcClass ); |
| 47 | } catch ( \Exception $e ) { |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | return $object; |
| 52 | } |
| 53 | |
| 54 | private static function isOrderAdminScreen( $action ) { |
| 55 | return is_admin() |
| 56 | && Obj::prop( 'page', $_GET ) === self::WC_ORDERS |
| 57 | && Obj::prop( 'action', $_GET ) === $action; |
| 58 | } |
| 59 | |
| 60 | public static function isOrderCreateAdminScreen() { |
| 61 | return self::isOrderAdminScreen( 'new' ); |
| 62 | } |
| 63 | |
| 64 | public static function isOrderListAdminScreen() { |
| 65 | return self::isOrderAdminScreen( null ) || self::isOrderAdminScreen( '-1' ); |
| 66 | } |
| 67 | |
| 68 | public static function isOrderEditAdminScreen() { |
| 69 | return self::isOrderAdminScreen( 'edit' ) && isset( $_GET['id'] ); |
| 70 | } |
| 71 | |
| 72 | } |
| 73 |