| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Packetery\Module\Email; |
| 6 |
|
| 7 |
use Packetery\Core\Entity\Order; |
| 8 |
use Packetery\Module\Framework\WpAdapter; |
| 9 |
use Packetery\Module\Order\Repository; |
| 10 |
|
| 11 |
class EmailShortcodes { |
| 12 |
/** @var WpAdapter */ |
| 13 |
private $wpAdapter; |
| 14 |
|
| 15 |
/** @var Repository */ |
| 16 |
private $orderRepository; |
| 17 |
|
| 18 |
public function __construct( |
| 19 |
WpAdapter $wpAdapter, |
| 20 |
Repository $orderRepository |
| 21 |
) { |
| 22 |
$this->wpAdapter = $wpAdapter; |
| 23 |
$this->orderRepository = $orderRepository; |
| 24 |
} |
| 25 |
|
| 26 |
public function register(): void { |
| 27 |
$this->wpAdapter->addShortcode( 'packeta_tracking_number', [ $this, 'trackingNumber' ] ); |
| 28 |
$this->wpAdapter->addShortcode( 'packeta_tracking_url', [ $this, 'trackingUrl' ] ); |
| 29 |
$this->wpAdapter->addShortcode( 'packeta_pickup_point_id', [ $this, 'pickupPointId' ] ); |
| 30 |
$this->wpAdapter->addShortcode( 'packeta_pickup_place', [ $this, 'pickupPointPlace' ] ); |
| 31 |
$this->wpAdapter->addShortcode( 'packeta_pickup_point_name', [ $this, 'pickupPointName' ] ); |
| 32 |
$this->wpAdapter->addShortcode( 'packeta_pickup_point_address', [ $this, 'pickupPointAddress' ] ); |
| 33 |
$this->wpAdapter->addShortcode( 'packeta_pickup_point_street', [ $this, 'pickupPointStreet' ] ); |
| 34 |
$this->wpAdapter->addShortcode( 'packeta_pickup_point_city', [ $this, 'pickupPointCity' ] ); |
| 35 |
$this->wpAdapter->addShortcode( 'packeta_pickup_point_zip', [ $this, 'pickupPointZip' ] ); |
| 36 |
$this->wpAdapter->addShortcode( 'packeta_pickup_point_country', [ $this, 'pickupPointCountry' ] ); |
| 37 |
$this->wpAdapter->addShortcode( 'packeta_carrier_name', [ $this, 'carrierName' ] ); |
| 38 |
|
| 39 |
$this->wpAdapter->addShortcode( 'packeta_if_packet_submitted', [ $this, 'ifPacketSubmitted' ] ); |
| 40 |
$this->wpAdapter->addShortcode( 'packeta_if_pickup_point', [ $this, 'ifPickupPoint' ] ); |
| 41 |
$this->wpAdapter->addShortcode( 'packeta_if_carrier', [ $this, 'ifExternalCarrier' ] ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @param array<string,mixed> $shortcodeAttributes |
| 46 |
*/ |
| 47 |
private function findOrder( array $shortcodeAttributes ): ?Order { |
| 48 |
$orderId = $shortcodeAttributes['order_id'] ?? null; |
| 49 |
if ( $orderId === null || is_numeric( $orderId ) === false ) { |
| 50 |
return null; |
| 51 |
} |
| 52 |
|
| 53 |
return $this->orderRepository->findById( (int) $orderId ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @param array<string,mixed> $shortcodeAttributes |
| 58 |
*/ |
| 59 |
public function trackingNumber( array $shortcodeAttributes ): string { |
| 60 |
$order = $this->findOrder( $shortcodeAttributes ); |
| 61 |
if ( $order === null ) { |
| 62 |
return ''; |
| 63 |
} |
| 64 |
|
| 65 |
return $order->getPacketBarcode() ?? ''; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @param array<string,mixed> $shortcodeAttributes |
| 70 |
*/ |
| 71 |
public function trackingUrl( array $shortcodeAttributes ): string { |
| 72 |
$order = $this->findOrder( $shortcodeAttributes ); |
| 73 |
if ( $order === null ) { |
| 74 |
return ''; |
| 75 |
} |
| 76 |
|
| 77 |
return $order->getPacketTrackingUrl() ?? ''; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @param array<string,mixed> $shortcodeAttributes |
| 82 |
*/ |
| 83 |
public function pickupPointId( array $shortcodeAttributes ): string { |
| 84 |
$order = $this->findOrder( $shortcodeAttributes ); |
| 85 |
if ( $order === null ) { |
| 86 |
return ''; |
| 87 |
} |
| 88 |
|
| 89 |
$pickupPoint = $order->getPickupPoint(); |
| 90 |
if ( $pickupPoint === null ) { |
| 91 |
return ''; |
| 92 |
} |
| 93 |
|
| 94 |
return $pickupPoint->getId() ?? ''; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @param array<string,mixed> $shortcodeAttributes |
| 99 |
*/ |
| 100 |
public function pickupPointPlace( array $shortcodeAttributes ): string { |
| 101 |
$order = $this->findOrder( $shortcodeAttributes ); |
| 102 |
if ( $order === null ) { |
| 103 |
return ''; |
| 104 |
} |
| 105 |
|
| 106 |
$pickupPoint = $order->getPickupPoint(); |
| 107 |
if ( $pickupPoint === null ) { |
| 108 |
return ''; |
| 109 |
} |
| 110 |
|
| 111 |
return $pickupPoint->getPlace() ?? ''; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* @param array<string,mixed> $shortcodeAttributes |
| 116 |
*/ |
| 117 |
public function pickupPointName( array $shortcodeAttributes ): string { |
| 118 |
$order = $this->findOrder( $shortcodeAttributes ); |
| 119 |
if ( $order === null ) { |
| 120 |
return ''; |
| 121 |
} |
| 122 |
|
| 123 |
$pickupPoint = $order->getPickupPoint(); |
| 124 |
if ( $pickupPoint === null ) { |
| 125 |
return ''; |
| 126 |
} |
| 127 |
|
| 128 |
return $pickupPoint->getName() ?? ''; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* @param array<string,mixed> $shortcodeAttributes |
| 133 |
*/ |
| 134 |
public function pickupPointAddress( array $shortcodeAttributes ): string { |
| 135 |
$order = $this->findOrder( $shortcodeAttributes ); |
| 136 |
if ( $order === null ) { |
| 137 |
return ''; |
| 138 |
} |
| 139 |
|
| 140 |
$pickupPoint = $order->getPickupPoint(); |
| 141 |
if ( $pickupPoint === null ) { |
| 142 |
return ''; |
| 143 |
} |
| 144 |
|
| 145 |
return $pickupPoint->getFullAddress(); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* @param array<string,mixed> $shortcodeAttributes |
| 150 |
*/ |
| 151 |
public function pickupPointStreet( array $shortcodeAttributes ): string { |
| 152 |
$order = $this->findOrder( $shortcodeAttributes ); |
| 153 |
if ( $order === null ) { |
| 154 |
return ''; |
| 155 |
} |
| 156 |
|
| 157 |
$pickupPoint = $order->getPickupPoint(); |
| 158 |
if ( $pickupPoint === null ) { |
| 159 |
return ''; |
| 160 |
} |
| 161 |
|
| 162 |
return $pickupPoint->getStreet() ?? ''; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* @param array<string,mixed> $shortcodeAttributes |
| 167 |
*/ |
| 168 |
public function pickupPointCity( array $shortcodeAttributes ): string { |
| 169 |
$order = $this->findOrder( $shortcodeAttributes ); |
| 170 |
if ( $order === null ) { |
| 171 |
return ''; |
| 172 |
} |
| 173 |
|
| 174 |
$pickupPoint = $order->getPickupPoint(); |
| 175 |
if ( $pickupPoint === null ) { |
| 176 |
return ''; |
| 177 |
} |
| 178 |
|
| 179 |
return $pickupPoint->getCity() ?? ''; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* @param array<string,mixed> $shortcodeAttributes |
| 184 |
*/ |
| 185 |
public function pickupPointZip( array $shortcodeAttributes ): string { |
| 186 |
$order = $this->findOrder( $shortcodeAttributes ); |
| 187 |
if ( $order === null ) { |
| 188 |
return ''; |
| 189 |
} |
| 190 |
|
| 191 |
$pickupPoint = $order->getPickupPoint(); |
| 192 |
if ( $pickupPoint === null ) { |
| 193 |
return ''; |
| 194 |
} |
| 195 |
|
| 196 |
return $pickupPoint->getZip() ?? ''; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* @param array<string,mixed> $shortcodeAttributes |
| 201 |
*/ |
| 202 |
public function pickupPointCountry( array $shortcodeAttributes ): string { |
| 203 |
$order = $this->findOrder( $shortcodeAttributes ); |
| 204 |
if ( $order === null ) { |
| 205 |
return ''; |
| 206 |
} |
| 207 |
|
| 208 |
$pickupPoint = $order->getPickupPoint(); |
| 209 |
if ( $pickupPoint === null ) { |
| 210 |
return ''; |
| 211 |
} |
| 212 |
|
| 213 |
return $order->getShippingCountry() ?? ''; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* @param array<string,mixed> $shortcodeAttributes |
| 218 |
*/ |
| 219 |
public function carrierName( array $shortcodeAttributes ): string { |
| 220 |
$order = $this->findOrder( $shortcodeAttributes ); |
| 221 |
if ( $order === null ) { |
| 222 |
return ''; |
| 223 |
} |
| 224 |
|
| 225 |
return $order->getCarrier()->getName(); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* @param array<string,mixed> $shortcodeAttributes |
| 230 |
*/ |
| 231 |
public function ifPacketSubmitted( array $shortcodeAttributes, string $content = '' ): string { |
| 232 |
$order = $this->findOrder( $shortcodeAttributes ); |
| 233 |
if ( $order === null || $order->getPacketId() === null ) { |
| 234 |
return ''; |
| 235 |
} |
| 236 |
|
| 237 |
return $this->wpAdapter->doShortcode( $content ); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* @param array<string,mixed> $shortcodeAttributes |
| 242 |
*/ |
| 243 |
public function ifPickupPoint( array $shortcodeAttributes, string $content = '' ): string { |
| 244 |
$order = $this->findOrder( $shortcodeAttributes ); |
| 245 |
if ( $order === null || $order->getPickupPoint() === null ) { |
| 246 |
return ''; |
| 247 |
} |
| 248 |
|
| 249 |
return $this->wpAdapter->doShortcode( $content ); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* @param array<string,mixed> $shortcodeAttributes |
| 254 |
*/ |
| 255 |
public function ifExternalCarrier( array $shortcodeAttributes, string $content = '' ): string { |
| 256 |
$order = $this->findOrder( $shortcodeAttributes ); |
| 257 |
if ( $order === null ) { |
| 258 |
return ''; |
| 259 |
} |
| 260 |
|
| 261 |
if ( $order->isExternalCarrier() === false ) { |
| 262 |
return ''; |
| 263 |
} |
| 264 |
|
| 265 |
return $this->wpAdapter->doShortcode( $content ); |
| 266 |
} |
| 267 |
} |
| 268 |
|