PluginProbe
Packeta / 1.4.2
Packeta v1.4.2
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Options / Provider.php

Provider.php in Packeta 1.4.2, at src/Packetery/Module/Options/Provider.php

379 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Provider
4 *
5 * @package Packetery
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Options;
11
12 use Packetery\Module\Order\PacketSynchronizer;
13
14 /**
15 * Class Provider
16 *
17 * @package Packetery
18 */
19 class Provider {
20
21 const OPTION_NAME_PACKETERY = 'packetery';
22 const OPTION_NAME_PACKETERY_SYNC = 'packetery_sync';
23
24 const DEFAULT_VALUE_PACKETA_LABEL_FORMAT = 'A6 on A4';
25 const DEFAULT_VALUE_CARRIER_LABEL_FORMAT = self::DEFAULT_VALUE_PACKETA_LABEL_FORMAT;
26 const MAX_STATUS_SYNCING_PACKETS_DEFAULT = 100;
27 const MAX_DAYS_OF_PACKET_STATUS_SYNCING_DEFAULT = 14;
28 const FORCE_PACKET_CANCEL_DEFAULT = true;
29
30 /**
31 * Options data.
32 *
33 * @var array
34 */
35 private $data;
36
37 /**
38 * Sync data.
39 *
40 * @var array
41 */
42 private $syncData;
43
44 /**
45 * Provider constructor.
46 */
47 public function __construct() {
48 $data = get_option( self::OPTION_NAME_PACKETERY );
49 if ( ! $data ) {
50 $data = array();
51 }
52
53 $syncData = get_option( self::OPTION_NAME_PACKETERY_SYNC );
54 if ( ! $syncData ) {
55 $syncData = [];
56 }
57
58 $this->data = $data;
59 $this->syncData = $syncData;
60 }
61
62 /**
63 * Casts data to array.
64 *
65 * @param string $optionName Option name of settings.
66 *
67 * @return array Data.
68 */
69 public function data_to_array( ?string $optionName = null ): array {
70 if ( self::OPTION_NAME_PACKETERY === $optionName ) {
71 return $this->data;
72 }
73
74 if ( self::OPTION_NAME_PACKETERY_SYNC === $optionName ) {
75 return $this->syncData;
76 }
77
78 if ( null === $optionName ) {
79 return [
80 self::OPTION_NAME_PACKETERY => $this->data,
81 self::OPTION_NAME_PACKETERY_SYNC => $this->syncData,
82 ];
83 }
84 }
85
86 /**
87 * Tells if provider has any data,
88 *
89 * @param string $optionName Option name of settings.
90 *
91 * @return bool Has any data.
92 */
93 public function has_any( string $optionName ): bool {
94 return ! empty( $this->data_to_array( $optionName ) );
95 }
96
97 /**
98 * Gets content from options array.
99 *
100 * @param string $key Options array key.
101 *
102 * @return mixed|null Content.
103 */
104 private function get( string $key ) {
105 return ( $this->data[ $key ] ?? null );
106 }
107
108 /**
109 * API key dynamically crafted from API password.
110 *
111 * @return string|null Content.
112 */
113 public function get_api_key(): ?string {
114 return $this->get( 'api_key' );
115 }
116
117 /**
118 * API password from client section.
119 *
120 * @return string|null Content.
121 */
122 public function get_api_password(): ?string {
123 return $this->get( 'api_password' );
124 }
125
126 /**
127 * Sender.
128 *
129 * @return string|null Content.
130 */
131 public function get_sender(): ?string {
132 return $this->get( 'sender' );
133 }
134
135 /**
136 * Carrier label format.
137 *
138 * @return string Content.
139 */
140 public function get_carrier_label_format(): string {
141 return $this->get( 'carrier_label_format' ) ?? self::DEFAULT_VALUE_CARRIER_LABEL_FORMAT;
142 }
143
144 /**
145 * Packeta label format.
146 *
147 * @return string Content.
148 */
149 public function get_packeta_label_format(): string {
150 return $this->get( 'packeta_label_format' ) ?? self::DEFAULT_VALUE_PACKETA_LABEL_FORMAT;
151 }
152
153 /**
154 * Does user allow label emailing?
155 *
156 * @return bool|null Content.
157 */
158 public function get_allow_label_emailing(): ?bool {
159 return (bool) $this->get( 'allow_label_emailing' );
160 }
161
162 /**
163 * Which payment rate id COD?
164 *
165 * @return string|null Content.
166 */
167 public function getCodPaymentMethod(): ?string {
168 $value = $this->get( 'cod_payment_method' );
169 if ( ! $value ) {
170 return null;
171 }
172
173 return $value;
174 }
175
176 /**
177 * Returns the location of the widget button in the cart
178 *
179 * @return string|null
180 */
181 public function getCheckoutWidgetButtonLocation(): ?string {
182 $value = $this->get( 'checkout_widget_button_location' );
183 if ( ! $value ) {
184 return null;
185 }
186
187 return $value;
188 }
189
190 /**
191 * Order packaging weight.
192 *
193 * @return float
194 */
195 public function getPackagingWeight(): float {
196 $value = $this->get( 'packaging_weight' );
197 if ( is_numeric( $value ) ) {
198 return (float) $value;
199 }
200
201 return 0.0;
202 }
203
204 /**
205 * Max syncing packets.
206 *
207 * @return int
208 */
209 public function getMaxStatusSyncingPackets(): int {
210 $value = ( $this->syncData['max_status_syncing_packets'] ?? null );
211 if ( is_numeric( $value ) ) {
212 return (int) $value;
213 }
214
215 return self::MAX_STATUS_SYNCING_PACKETS_DEFAULT;
216 }
217
218 /**
219 * Max days of packet status syncing.
220 *
221 * @return int
222 */
223 public function getMaxDaysOfPacketStatusSyncing(): int {
224 $value = ( $this->syncData['max_days_of_packet_status_syncing'] ?? null );
225 if ( is_numeric( $value ) ) {
226 return (int) $value;
227 }
228
229 return self::MAX_DAYS_OF_PACKET_STATUS_SYNCING_DEFAULT;
230 }
231
232 /**
233 * Status syncing order statuses.
234 *
235 * @return array
236 */
237 public function getStatusSyncingOrderStatuses(): array {
238 $value = ( $this->syncData['status_syncing_order_statuses'] ?? null );
239 if ( is_array( $value ) ) {
240 return $value;
241 }
242
243 return [];
244 }
245
246 /**
247 * Status syncing order statuses.
248 *
249 * @return array
250 */
251 public function getExistingStatusSyncingOrderStatuses(): array {
252 $statuses = $this->getStatusSyncingOrderStatuses();
253 $choices = array_column( Page::getOrderStatusesChoiceData(), 'key' );
254
255 return array_intersect( $statuses, $choices );
256 }
257
258 /**
259 * Status syncing packet statuses.
260 *
261 * @return array
262 */
263 public function getStatusSyncingPacketStatuses(): array {
264 $value = ( $this->syncData['status_syncing_packet_statuses'] ?? null );
265 if ( is_array( $value ) ) {
266 return $value;
267 }
268
269 return array_keys( PacketSynchronizer::getPacketStatuses(), true, true );
270 }
271
272 /**
273 * Transform shipping address to contain pickup point address?
274 *
275 * @return bool
276 */
277 public function replaceShippingAddressWithPickupPointAddress(): bool {
278 return (bool) $this->get( 'replace_shipping_address_with_pickup_point_address' );
279 }
280
281 /**
282 * Tells if packet cancellation should be forced.
283 *
284 * @return bool
285 */
286 public function isPacketCancellationForced(): bool {
287 $value = $this->get( 'force_packet_cancel' );
288 if ( null !== $value ) {
289 return (bool) $value;
290 }
291
292 return self::FORCE_PACKET_CANCEL_DEFAULT;
293 }
294
295 /**
296 * Provides available labels.
297 *
298 * @return array[]
299 */
300 public function getLabelFormats(): array {
301 return [
302 'A6 on A4' => [
303 'name' => __( '1/4 A4, print on A4, 4pcs/page', 'packeta' ),
304 'directLabels' => true,
305 'maxOffset' => 3,
306 ],
307 'A6 on A6' => [
308 'name' => __( '1/4 A4, direct print, 1pc/page', 'packeta' ),
309 'directLabels' => true,
310 'maxOffset' => 0,
311 ],
312 'A7 on A7' => [
313 'name' => __( '1/8 A4, direct print, 1pc/page', 'packeta' ),
314 'directLabels' => false,
315 'maxOffset' => 0,
316 ],
317 'A7 on A4' => [
318 'name' => __( '1/8 A4, print on A4, 8pcs/page', 'packeta' ),
319 'directLabels' => false,
320 'maxOffset' => 7,
321 ],
322 '105x35mm on A4' => [
323 'name' => __( '105x35mm, print on A4, 16 pcs/page', 'packeta' ),
324 'directLabels' => false,
325 'maxOffset' => 15,
326 ],
327 'A8 on A8' => [
328 'name' => __( '1/16 A4, direct print, 1pc/page', 'packeta' ),
329 'directLabels' => false,
330 'maxOffset' => 0,
331 ],
332 ];
333 }
334
335 /**
336 * Gets maximum offset for selected packeta labels format.
337 *
338 * @param string $format Selected format.
339 *
340 * @return int
341 */
342 public function getLabelMaxOffset( string $format ): int {
343 if ( '' === $format ) {
344 return 0;
345 }
346 $availableFormats = $this->getLabelFormats();
347
348 return $availableFormats[ $format ]['maxOffset'];
349 }
350
351 /**
352 * Gets list of packeta labels for select creation.
353 *
354 * @return array
355 */
356 public function getPacketaLabelFormats(): array {
357 $availableFormats = $this->getLabelFormats();
358
359 return array_filter( array_combine( array_keys( $availableFormats ), array_column( $availableFormats, 'name' ) ) );
360 }
361
362 /**
363 * Gets list of carrier labels for select creation.
364 *
365 * @return array
366 */
367 public function getCarrierLabelFormat(): array {
368 $availableFormats = $this->getLabelFormats();
369 $carrierLabelFormats = [];
370 foreach ( $availableFormats as $format => $formatData ) {
371 if ( true === $formatData['directLabels'] ) {
372 $carrierLabelFormats[ $format ] = $formatData['name'];
373 }
374 }
375
376 return $carrierLabelFormats;
377 }
378 }
379