PluginProbe
Packeta / 1.4
Packeta v1.4
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, at src/Packetery/Module/Options/Provider.php

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