| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Utils; |
| 4 |
|
| 5 |
class Constants { |
| 6 |
const ORDER_STATUS_PROCESSING = 'processing'; |
| 7 |
|
| 8 |
const ORDER_STATUS_COMPLETED = 'completed'; |
| 9 |
|
| 10 |
const ORDER_STATUS_PAYMENT_CONFIRMED = 'payment_confirmed'; |
| 11 |
|
| 12 |
const ORDER_STATUS_DRAFT = 'draft'; |
| 13 |
|
| 14 |
const ORDER_STATUS_PAYMENT = 'pending_payment'; |
| 15 |
|
| 16 |
const ORDER_STATUS_PENDING_PAYMENT = 'pending_payment'; |
| 17 |
|
| 18 |
const ORDER_STATUS_PAID = 'paid'; |
| 19 |
|
| 20 |
const ORDER_STATUS_ON_HOLD = 'on_hold'; |
| 21 |
|
| 22 |
const ORDER_STATUS_CANCELED = 'canceled'; |
| 23 |
|
| 24 |
const ORDER_STATUS_REFUNDED = 'refunded'; |
| 25 |
|
| 26 |
const ORDER_STATUS_FAILED = 'failed'; |
| 27 |
|
| 28 |
const ORDER_STATUS_TRASH = 'trash'; |
| 29 |
|
| 30 |
const BILLING_ADDRESS_TYPE = 'billing_address'; |
| 31 |
|
| 32 |
const SHIPPING_ADDRESS_TYPE = 'shipping_address'; |
| 33 |
|
| 34 |
const ORDER_STATUS_ACTIVE = 'active'; |
| 35 |
|
| 36 |
const ORDER_STATUS_CANCELLED = 'cancelled'; |
| 37 |
|
| 38 |
const ORDER_STATUS_EXPIRED = 'expired'; |
| 39 |
|
| 40 |
const ORDER_STATUS_RENEWED = 'renewed'; |
| 41 |
|
| 42 |
// Shipping statuses |
| 43 |
const READY_FOR_SHIP = 'ready_for_ship'; |
| 44 |
|
| 45 |
const AWAITING_SHIPMENT = 'awaiting_shipment'; |
| 46 |
|
| 47 |
const SHIPPED = 'shipped'; |
| 48 |
|
| 49 |
const ON_THE_WAY = 'on_the_way'; |
| 50 |
|
| 51 |
const OUT_FOR_DELIVERY = 'out_for_delivery'; |
| 52 |
|
| 53 |
const DELIVERED = 'delivered'; |
| 54 |
|
| 55 |
const RETURNED = 'returned'; |
| 56 |
|
| 57 |
/** |
| 58 |
* The shipping/fulfilment lifecycle, in forward order. Index position is the |
| 59 |
* single source of truth for "forward-only" transition checks — a lower index |
| 60 |
* may never be set after a higher one. Shared by the admin shipping AJAX and |
| 61 |
* the multi-vendor fulfilment REST endpoint so the two can't drift. |
| 62 |
* |
| 63 |
* @return string[] |
| 64 |
*/ |
| 65 |
public static function get_shipping_statuses(): array { |
| 66 |
return [ |
| 67 |
self::READY_FOR_SHIP, |
| 68 |
self::AWAITING_SHIPMENT, |
| 69 |
self::SHIPPED, |
| 70 |
self::ON_THE_WAY, |
| 71 |
self::OUT_FOR_DELIVERY, |
| 72 |
self::DELIVERED, |
| 73 |
self::RETURNED, |
| 74 |
]; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Human-readable label for a shipping status value. |
| 79 |
*/ |
| 80 |
public static function get_shipping_status_label( string $status ): string { |
| 81 |
$labels = [ |
| 82 |
self::READY_FOR_SHIP => __( 'Ready for ship', 'storeengine' ), |
| 83 |
self::AWAITING_SHIPMENT => __( 'Awaiting shipment', 'storeengine' ), |
| 84 |
self::SHIPPED => __( 'Shipped', 'storeengine' ), |
| 85 |
self::ON_THE_WAY => __( 'On the way', 'storeengine' ), |
| 86 |
self::OUT_FOR_DELIVERY => __( 'Out for delivery', 'storeengine' ), |
| 87 |
self::DELIVERED => __( 'Delivered', 'storeengine' ), |
| 88 |
self::RETURNED => __( 'Returned', 'storeengine' ), |
| 89 |
]; |
| 90 |
|
| 91 |
return $labels[ $status ] ?? ucwords( str_replace( '_', ' ', $status ) ); |
| 92 |
} |
| 93 |
|
| 94 |
// @TODO Need implementation. |
| 95 |
const SUBSCRIPTION_STATUS_ACTIVE = 'active'; |
| 96 |
|
| 97 |
const SUBSCRIPTION_STATUS_PENDING = 'pending'; |
| 98 |
|
| 99 |
const SUBSCRIPTION_STATUS_PENDING_PAYMENT = 'pending_payment'; |
| 100 |
|
| 101 |
const SUBSCRIPTION_STATUS_RENEWED = 'renewed'; |
| 102 |
|
| 103 |
const SUBSCRIPTION_STATUS_EXPIRED = 'expired'; |
| 104 |
|
| 105 |
const SUBSCRIPTION_STATUS_ON_HOLD = 'on_hold'; |
| 106 |
|
| 107 |
const SUBSCRIPTION_STATUS_DRAFT = 'draft'; |
| 108 |
|
| 109 |
const SUBSCRIPTION_STATUS_ARCHIVE = 'archive'; |
| 110 |
|
| 111 |
const SUBSCRIPTION_STATUS_CANCELLED = 'cancelled'; |
| 112 |
|
| 113 |
/** |
| 114 |
* A container for all defined constants. |
| 115 |
* |
| 116 |
* @access public |
| 117 |
* @static |
| 118 |
* |
| 119 |
* @var array |
| 120 |
*/ |
| 121 |
public static array $set_constants = []; |
| 122 |
|
| 123 |
/** |
| 124 |
* Checks if a "constant" has been set in constants Manager |
| 125 |
* and has a truthy value (e.g. not null, not false, not 0, any string). |
| 126 |
* |
| 127 |
* @param string $name The name of the constant. |
| 128 |
* |
| 129 |
* @return bool |
| 130 |
*/ |
| 131 |
public static function is_true( string $name ): bool { |
| 132 |
return self::is_defined( $name ) && self::get_constant( $name ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Checks if a "constant" has been set in constants Manager, and if not, |
| 137 |
* checks if the constant was defined with define( 'name', 'value ). |
| 138 |
* |
| 139 |
* @param string $name The name of the constant. |
| 140 |
* |
| 141 |
* @return bool |
| 142 |
*/ |
| 143 |
public static function is_defined( string $name ): bool { |
| 144 |
return array_key_exists( $name, self::$set_constants ) || defined( $name ); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Attempts to retrieve the "constant" from constants Manager, and if it hasn't been set, |
| 149 |
* then attempts to get the constant with the constant() function. If that also hasn't |
| 150 |
* been set, attempts to get a value from filters. |
| 151 |
* |
| 152 |
* @param string $name The name of the constant. |
| 153 |
* |
| 154 |
* @return mixed null if the constant does not exist or the value of the constant. |
| 155 |
*/ |
| 156 |
public static function get_constant( string $name ) { |
| 157 |
if ( array_key_exists( $name, self::$set_constants ) ) { |
| 158 |
return self::$set_constants[ $name ]; |
| 159 |
} |
| 160 |
|
| 161 |
if ( defined( $name ) ) { |
| 162 |
return constant( $name ); |
| 163 |
} |
| 164 |
|
| 165 |
if ( defined( '\\' . $name ) ) { |
| 166 |
return constant( '\\' . $name ); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Filters the value of the constant. |
| 171 |
* |
| 172 |
* @param null The constant value to be filtered. The default is null. |
| 173 |
* @param String $name The constant name. |
| 174 |
*/ |
| 175 |
return apply_filters( 'storeengine/constant/default_value', null, $name ); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Sets the value of the "constant" within constants Manager. |
| 180 |
* |
| 181 |
* @param string $name The name of the constant. |
| 182 |
* @param int|float|string|bool|array|null $value The value of the constant. |
| 183 |
*/ |
| 184 |
public static function set_constant( string $name, $value ) { |
| 185 |
self::$set_constants[ $name ] = $value; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Will unset a "constant" from constants Manager if the constant exists. |
| 190 |
* |
| 191 |
* @param string $name The name of the constant. |
| 192 |
* |
| 193 |
* @return bool Whether the constant was removed. |
| 194 |
*/ |
| 195 |
public static function clear_single_constant( string $name ): bool { |
| 196 |
if ( ! array_key_exists( $name, self::$set_constants ) ) { |
| 197 |
return false; |
| 198 |
} |
| 199 |
|
| 200 |
unset( self::$set_constants[ $name ] ); |
| 201 |
|
| 202 |
return true; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Resets all the constants within constants Manager. |
| 207 |
*/ |
| 208 |
public static function clear_constants() { |
| 209 |
self::$set_constants = []; |
| 210 |
} |
| 211 |
} |
| 212 |
|