PluginProbe
Sendy / trunk
Sendy vtrunk
3.4.6 3.4.7 trunk 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 All 32 releases
sendy / lib / helpers.php

helpers.php in Sendy trunk, at lib/helpers.php

177 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (! function_exists('sendy_initialize_plugin_url')) {
4 function sendy_initialize_plugin_url(): string
5 {
6 return 'https://app.sendy.nl/plugin/initialize?' . http_build_query([
7 'client_id' => get_option('sendy_client_id'),
8 'client_secret' => get_option('sendy_client_secret'),
9 'redirect_uri' => sendy_oauth_redirect_url(),
10 'name' => get_bloginfo('name'),
11 'type' => 'woocommerce',
12 'state' => wp_create_nonce('sendy_oauth_callback_nonce'),
13 ]);
14 }
15 }
16
17 if (! function_exists('sendy_oauth_redirect_url')) {
18 function sendy_oauth_redirect_url(): string
19 {
20 return admin_url('?sendy_oauth_callback');
21 }
22 }
23
24 if (! function_exists('sendy_fields_generator')) {
25 function sendy_fields_generator(array $fields)
26 {
27 foreach ($fields as $field) {
28 switch ($field['type']) {
29 case 'select':
30 woocommerce_wp_select($field);
31 break;
32
33 case 'number':
34 case 'text':
35 woocommerce_wp_text_input($field);
36 break;
37 }
38 }
39 }
40 }
41
42 if (! function_exists('sendy_is_authenticated')) {
43 function sendy_is_authenticated(): bool
44 {
45 return get_option('sendy_access_token') != '';
46 }
47 }
48
49 if (! function_exists('sendy_flash_admin_notice')) {
50 function sendy_flash_admin_notice(string $type, string $message): void
51 {
52 $messages = get_option('sendy_flash_admin_messages', []);
53
54 $messages[] = [
55 'type' => $type,
56 'message' => $message,
57 ];
58
59 update_option('sendy_flash_admin_messages', $messages);
60 }
61 }
62
63 if (! function_exists('sendy_parse_address')) {
64
65 /**
66 * Extract the street, number and addition from a given string
67 *
68 * @return object{street: string, house_number:string|null, house_number_addition:string|null}
69 */
70 function sendy_parse_address(string $address): stdClass
71 {
72 $address = trim($address);
73 $parts = explode(' ', $address);
74 $partsCount = count($parts);
75
76 $numberPart = null;
77 $number = null;
78 $addition = null;
79
80 // Check the parts if they might contain any house number
81 if ($partsCount == 2) {
82 if (is_numeric(end($parts))) { // House number should be 1
83 $numberPart = end($parts);
84 $streetPart = $parts[0];
85 } elseif (preg_match('/^([0-9]+)([a-zA-Z]+)$/', end($parts))) { // House number should be 1a
86 $numberPart = end($parts);
87 $streetPart = rtrim(substr($address, 0, (0 - strlen($numberPart))));
88 } elseif (preg_match('/^([0-9]+)([\-\\\+\/]+?)([0-9a-zA-Z]+)$/', end($parts))) { // House number should be 1-1someting
89 $numberPart = end($parts);
90 $streetPart = rtrim(substr($address, 0, (0 - strlen($numberPart))));
91 } else {
92 $streetPart = $address;
93 }
94 } elseif ($partsCount >= 3) {
95 if (is_numeric(end($parts))) { // House number 1
96 $numberPart = end($parts);
97 $streetPart = rtrim(substr($address, 0, (0 - strlen($numberPart))));
98 } elseif (preg_match('/^([0-9]+)([\-\\\+\/]?)([0-9a-zA-Z]+)$/', end($parts))) { // House number 1a
99 $numberPart = end($parts);
100 $streetPart = rtrim(substr($address, 0, (0 - strlen($numberPart))));
101 } elseif (preg_match('/^([0-9]+)([a-zA-Z\s\-]+)$/', $parts[$partsCount - 2] . ' ' . $parts[$partsCount - 1])) { // House number 1 a
102 $numberPart = $parts[$partsCount - 2] . ' ' . $parts[$partsCount - 1];
103 $streetPart = rtrim(substr($address, 0, (0 - strlen($numberPart))));
104 } else {
105 $streetPart = $address;
106 }
107 } else {
108 $streetPart = $address;
109 }
110
111 // Convert the number to an object
112 if ($numberPart) {
113 $houseNumberDetails = sendy_parse_house_number($numberPart);
114 $number = $houseNumberDetails->number;
115 $addition = $houseNumberDetails->addition;
116 }
117
118 $object = new \stdClass();
119 $object->street = trim($streetPart);
120 $object->number = $number;
121 $object->addition = $addition;
122
123 return $object;
124 }
125 }
126
127 if (! function_exists('sendy_parse_house_number')) {
128 /**
129 * Split the house number and addition
130 *
131 * @return object{house_number:string, house_number_addition:string|null}
132 */
133 function sendy_parse_house_number(string $number): stdClass
134 {
135 $addition = null;
136
137 if (! ctype_digit($number)) {
138 $addition = ltrim($number, '0123456789');
139 $number = substr($number, 0, (0 - strlen($addition)));
140 }
141
142 $object = new \stdClass();
143 $object->number = trim($number);
144 $object->addition = $addition ? trim($addition) : null;
145
146 return $object;
147 }
148 }
149
150 if (! function_exists('sendy_shipping_method_instance_id')) {
151 /**
152 * Get the instance id of the selected shipment method
153 *
154 * This ID is used to correctly store the selected pickup point and makes it possible to offer pickup point delivery
155 * for multiple carriers.
156 */
157 function sendy_shipping_method_instance_id(): ?int
158 {
159 $selectedMethods = WC()->session->get('chosen_shipping_methods') ?? [];
160
161 $pickupPointDelivery = array_filter(
162 $selectedMethods,
163 function ($item) {
164 return str_starts_with($item, 'sendy_pickup_point');
165 },
166 );
167
168 if (count($pickupPointDelivery) > 0) {
169 [$name, $instanceId] = explode(':', $pickupPointDelivery[0]);
170
171 return (int) $instanceId;
172 }
173
174 return null;
175 }
176 }
177