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

272 lines 5.3 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 MAX_STATUS_SYNCING_PACKETS_DEFAULT = 100;
20
21 /**
22 * Options data.
23 *
24 * @var array
25 */
26 private $data;
27
28 /**
29 * Provider constructor.
30 */
31 public function __construct() {
32 $data = get_option( 'packetery' );
33 if ( ! $data ) {
34 $data = array();
35 }
36
37 $this->data = $data;
38 }
39
40 /**
41 * Casts data to array.
42 *
43 * @return array Data.
44 */
45 public function data_to_array(): array {
46 return $this->data;
47 }
48
49 /**
50 * Casts data to array.
51 *
52 * @return bool Has any data.
53 */
54 public function has_any(): bool {
55 return ! empty( $this->data );
56 }
57
58 /**
59 * Gets content from options array.
60 *
61 * @param string $key Options array key.
62 *
63 * @return mixed|null Content.
64 */
65 private function get( string $key ) {
66 return ( $this->data[ $key ] ?? null );
67 }
68
69 /**
70 * API key dynamically crafted from API password.
71 *
72 * @return string|null Content.
73 */
74 public function get_api_key(): ?string {
75 return $this->get( 'api_key' );
76 }
77
78 /**
79 * API password from client section.
80 *
81 * @return string|null Content.
82 */
83 public function get_api_password(): ?string {
84 return $this->get( 'api_password' );
85 }
86
87 /**
88 * Sender.
89 *
90 * @return string|null Content.
91 */
92 public function get_sender(): ?string {
93 return $this->get( 'sender' );
94 }
95
96 /**
97 * Carrier label format.
98 *
99 * @return string|null Content.
100 */
101 public function get_carrier_label_format(): ?string {
102 return $this->get( 'carrier_label_format' );
103 }
104
105 /**
106 * Packeta label format.
107 *
108 * @return string|null Content.
109 */
110 public function get_packeta_label_format(): ?string {
111 return $this->get( 'packeta_label_format' );
112 }
113
114 /**
115 * Does user allow label emailing?
116 *
117 * @return bool|null Content.
118 */
119 public function get_allow_label_emailing(): ?bool {
120 return (bool) $this->get( 'allow_label_emailing' );
121 }
122
123 /**
124 * Which payment rate id COD?
125 *
126 * @return string|null Content.
127 */
128 public function getCodPaymentMethod(): ?string {
129 $value = $this->get( 'cod_payment_method' );
130 if ( ! $value ) {
131 return null;
132 }
133
134 return $value;
135 }
136
137 /**
138 * Returns the location of the widget button in the cart
139 *
140 * @return string|null
141 */
142 public function getCheckoutWidgetButtonLocation(): ?string {
143 $value = $this->get( 'checkout_widget_button_location' );
144 if ( ! $value ) {
145 return null;
146 }
147
148 return $value;
149 }
150
151 /**
152 * Order packaging weight.
153 *
154 * @return float
155 */
156 public function getPackagingWeight(): float {
157 $value = $this->get( 'packaging_weight' );
158 if ( is_numeric( $value ) ) {
159 return (float) $value;
160 }
161
162 return 0.0;
163 }
164
165 /**
166 * Max syncing packets.
167 *
168 * @return int
169 */
170 public function getMaxStatusSyncingPackets(): int {
171 $value = $this->get( 'max_status_syncing_packets' );
172 if ( is_numeric( $value ) ) {
173 return (int) $value;
174 }
175
176 return self::MAX_STATUS_SYNCING_PACKETS_DEFAULT;
177 }
178
179 /**
180 * Transform shipping address to contain pickup point address?
181 *
182 * @return bool
183 */
184 public function replaceShippingAddressWithPickupPointAddress(): bool {
185 return (bool) $this->get( 'replace_shipping_address_with_pickup_point_address' );
186 }
187
188 /**
189 * Provides available labels.
190 *
191 * @return array[]
192 */
193 public function getLabelFormats(): array {
194 return [
195 'A6 on A4' => [
196 'name' => __( '1/4 A4, print on A4, 4pcs/page', 'packeta' ),
197 'directLabels' => true,
198 'maxOffset' => 3,
199 ],
200 'A6 on A6' => [
201 'name' => __( '1/4 A4, direct print, 1pc/page', 'packeta' ),
202 'directLabels' => true,
203 'maxOffset' => 0,
204 ],
205 'A7 on A7' => [
206 'name' => __( '1/8 A4, direct print, 1pc/page', 'packeta' ),
207 'directLabels' => false,
208 'maxOffset' => 0,
209 ],
210 'A7 on A4' => [
211 'name' => __( '1/8 A4, print on A4, 8pcs/page', 'packeta' ),
212 'directLabels' => false,
213 'maxOffset' => 7,
214 ],
215 '105x35mm on A4' => [
216 'name' => __( '105x35mm, print on A4, 16 pcs/page', 'packeta' ),
217 'directLabels' => false,
218 'maxOffset' => 15,
219 ],
220 'A8 on A8' => [
221 'name' => __( '1/16 A4, direct print, 1pc/page', 'packeta' ),
222 'directLabels' => false,
223 'maxOffset' => 0,
224 ],
225 ];
226 }
227
228 /**
229 * Gets maximum offset for selected packeta labels format.
230 *
231 * @param string $format Selected format.
232 *
233 * @return int
234 */
235 public function getLabelMaxOffset( string $format ): int {
236 if ( '' === $format ) {
237 return 0;
238 }
239 $availableFormats = $this->getLabelFormats();
240
241 return $availableFormats[ $format ]['maxOffset'];
242 }
243
244 /**
245 * Gets list of packeta labels for select creation.
246 *
247 * @return array
248 */
249 public function getPacketaLabelFormats(): array {
250 $availableFormats = $this->getLabelFormats();
251
252 return array_filter( array_combine( array_keys( $availableFormats ), array_column( $availableFormats, 'name' ) ) );
253 }
254
255 /**
256 * Gets list of carrier labels for select creation.
257 *
258 * @return array
259 */
260 public function getCarrierLabelFormat(): array {
261 $availableFormats = $this->getLabelFormats();
262 $carrierLabelFormats = [];
263 foreach ( $availableFormats as $format => $formatData ) {
264 if ( true === $formatData['directLabels'] ) {
265 $carrierLabelFormats[ $format ] = $formatData['name'];
266 }
267 }
268
269 return $carrierLabelFormats;
270 }
271 }
272