| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class WcAdapter. |
| 4 |
* |
| 5 |
* @package Packetery |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Framework; |
| 11 |
|
| 12 |
use Automattic\WooCommerce\Utilities\FeaturesUtil; |
| 13 |
use Automattic\WooCommerce\Utilities\LoggingUtil; |
| 14 |
use stdClass; |
| 15 |
use WC_Admin_Status; |
| 16 |
use WC_Blocks_Utils; |
| 17 |
use WC_Data_Store; |
| 18 |
use WC_Logger; |
| 19 |
use WC_Logger_Interface; |
| 20 |
use WC_Product; |
| 21 |
use WC_Shipping_Rate; |
| 22 |
use WC_Shipping_Zone; |
| 23 |
use WC_Shipping_Zone_Data_Store_Interface; |
| 24 |
use WC_Shipping_Zones; |
| 25 |
|
| 26 |
/** |
| 27 |
* Class WcAdapter. |
| 28 |
* |
| 29 |
* @package Packetery |
| 30 |
*/ |
| 31 |
class WcAdapter { |
| 32 |
use ActionSchedulerTrait; |
| 33 |
use WcCartTrait; |
| 34 |
use WcCustomerTrait; |
| 35 |
use WcPageTrait; |
| 36 |
use WcSessionTrait; |
| 37 |
use WcTaxTrait; |
| 38 |
|
| 39 |
/** |
| 40 |
* Converts weight from global unit to kg. |
| 41 |
* |
| 42 |
* @param int|float $weight Weight. |
| 43 |
* @param string $toUnit To unit. |
| 44 |
* |
| 45 |
* @return float |
| 46 |
*/ |
| 47 |
public function getWeight( $weight, string $toUnit ): float { |
| 48 |
return (float) wc_get_weight( $weight, $toUnit ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Gets WC product by id. |
| 53 |
* |
| 54 |
* @param mixed $productId Product ID. |
| 55 |
* |
| 56 |
* @return WC_Product|null |
| 57 |
*/ |
| 58 |
public function productFactoryGetProduct( $productId ): ?\WC_Product { |
| 59 |
$product = WC()->product_factory->get_product( $productId ); |
| 60 |
if ( $product instanceof WC_Product ) { |
| 61 |
return $product; |
| 62 |
} |
| 63 |
|
| 64 |
return null; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Gets product by post or post id. |
| 69 |
* |
| 70 |
* @param mixed $theProduct Post id or object. |
| 71 |
* |
| 72 |
* @return false|WC_Product|null |
| 73 |
*/ |
| 74 |
public function getProduct( $theProduct ) { |
| 75 |
return wc_get_product( $theProduct ); |
| 76 |
} |
| 77 |
|
| 78 |
public function isCheckout(): bool { |
| 79 |
return is_checkout(); |
| 80 |
} |
| 81 |
|
| 82 |
public function featuresUtilDeclareCompatibility( string $featureId, string $pluginFile, bool $positiveCompatibility = true ): void { |
| 83 |
FeaturesUtil::declare_compatibility( $featureId, $pluginFile, $positiveCompatibility ); |
| 84 |
} |
| 85 |
|
| 86 |
public function storeApiRegisterUpdateCallback( array $args ): void { |
| 87 |
woocommerce_store_api_register_update_callback( $args ); |
| 88 |
} |
| 89 |
|
| 90 |
public function shipToBillingAddressOnly(): bool { |
| 91 |
return wc_ship_to_billing_address_only(); |
| 92 |
} |
| 93 |
|
| 94 |
public function shippingGetPackages(): array { |
| 95 |
return WC()->shipping()->get_packages(); |
| 96 |
} |
| 97 |
|
| 98 |
public function addNotice( string $message, string $noticeType ): void { |
| 99 |
wc_add_notice( $message, $noticeType ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @return WC_Logger|WC_Logger_Interface |
| 104 |
*/ |
| 105 |
public function getLogger() { |
| 106 |
return wc_get_logger(); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @return array<string, array{ |
| 111 |
* name: string, |
| 112 |
* countries: array<int, string> |
| 113 |
* }> |
| 114 |
*/ |
| 115 |
public function countriesGetContinents(): array { |
| 116 |
return WC()->countries->get_continents(); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @param array{contents: array<string, array<string, mixed>>, contents_cost: float, applied_coupons: array, user: array{ID: int}, destination: array<string, string>, cart_subtotal: float, packetery_payment_method: mixed, rates: array<string, WC_Shipping_Rate>} $package |
| 121 |
* |
| 122 |
* @return WC_Shipping_Zone |
| 123 |
*/ |
| 124 |
public function shippingZonesGetZoneMatchingPackage( array $package ): WC_Shipping_Zone { |
| 125 |
return WC_Shipping_Zones::get_zone_matching_package( $package ); |
| 126 |
} |
| 127 |
|
| 128 |
public function hasBlockInPage( int $page, string $blockName ): bool { |
| 129 |
return WC_Blocks_Utils::has_block_in_page( $page, $blockName ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* @param string $objectType |
| 134 |
* |
| 135 |
* @return WC_Data_Store|WC_Shipping_Zone_Data_Store_Interface |
| 136 |
*/ |
| 137 |
public function dataStoreLoad( string $objectType ): object { |
| 138 |
return WC_Data_Store::load( $objectType ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Returns WC_Abstract_Order[] |
| 143 |
* |
| 144 |
* @param array<string, mixed> $args |
| 145 |
* |
| 146 |
* @return array<mixed, mixed> |
| 147 |
*/ |
| 148 |
public function getOrdersWithoutPagination( array $args ): array { |
| 149 |
$results = wc_get_orders( $args ); |
| 150 |
|
| 151 |
if ( is_array( $results ) ) { |
| 152 |
return $results; |
| 153 |
} |
| 154 |
|
| 155 |
if ( $results instanceof stdClass && isset( $results->orders ) && is_array( $results->orders ) ) { |
| 156 |
return $results->orders; |
| 157 |
} |
| 158 |
|
| 159 |
return []; |
| 160 |
} |
| 161 |
|
| 162 |
public function adminStatusStatusReport(): void { |
| 163 |
WC_Admin_Status::status_report(); |
| 164 |
} |
| 165 |
|
| 166 |
public function loggingUtilGetLogDirectory( bool $createDir = true ): ?string { |
| 167 |
if ( class_exists( LoggingUtil::class ) === false ) { |
| 168 |
return null; |
| 169 |
} |
| 170 |
|
| 171 |
return LoggingUtil::get_log_directory( $createDir ); |
| 172 |
} |
| 173 |
} |
| 174 |
|