PluginProbe
Sendy / 3.2.4
Sendy v3.2.4
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 / src / helpers.php

helpers.php in Sendy 3.2.4, at src/helpers.php

147 lines 5.0 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 return admin_url('?sendy_oauth_callback');
20 }
21 }
22
23 if (! function_exists('sendy_fields_generator')) {
24 function sendy_fields_generator(array $fields)
25 {
26 foreach ($fields as $field) {
27 switch ($field['type']) {
28 case 'select':
29 woocommerce_wp_select($field);
30 break;
31
32 case 'number':
33 case 'text':
34 woocommerce_wp_text_input($field);
35 break;
36 }
37 }
38 }
39 }
40
41 if (! function_exists('sendy_is_authenticated')) {
42 function sendy_is_authenticated(): bool {
43 return get_option('sendy_access_token') != '';
44 }
45 }
46
47 if (! function_exists('sendy_flash_admin_notice')) {
48 function sendy_flash_admin_notice(string $type, string $message): void
49 {
50 $messages = get_option('sendy_flash_admin_messages', []);
51
52 $messages[] = [
53 'type' => $type,
54 'message' => $message,
55 ];
56
57 update_option('sendy_flash_admin_messages', $messages);
58 }
59 }
60
61 if (! function_exists('sendy_parse_address')) {
62
63 /**
64 * Extract the street, number and addition from a given string
65 *
66 * @param string $address
67 * @return object{street: string, house_number:string|null, house_number_addition:string|null}
68 */
69 function sendy_parse_address(string $address): stdClass {
70 $address = trim($address);
71 $parts = explode(' ', $address);
72 $partsCount = count($parts);
73
74 $numberPart = null;
75 $number = null;
76 $addition = null;
77
78 // Check the parts if they might contain any house number
79 if ($partsCount == 2) {
80 if (is_numeric(end($parts))) { // House number should be 1
81 $numberPart = end($parts);
82 $streetPart = $parts[0];
83 } elseif (preg_match('/^([0-9]+)([a-zA-Z]+)$/', end($parts))) { // House number should be 1a
84 $numberPart = end($parts);
85 $streetPart = rtrim(substr($address, 0, (0 - strlen($numberPart))));
86 } elseif (preg_match('/^([0-9]+)([\-\\\+\/]+?)([0-9a-zA-Z]+)$/', end($parts))) { // House number should be 1-1someting
87 $numberPart = end($parts);
88 $streetPart = rtrim(substr($address, 0, (0 - strlen($numberPart))));
89 } else {
90 $streetPart = $address;
91 }
92 } elseif ($partsCount >= 3) {
93 if (is_numeric(end($parts))) { // House number 1
94 $numberPart = end($parts);
95 $streetPart = rtrim(substr($address, 0, (0 - strlen($numberPart))));
96 } elseif (preg_match('/^([0-9]+)([\-\\\+\/]?)([0-9a-zA-Z]+)$/', end($parts))) { // House number 1a
97 $numberPart = end($parts);
98 $streetPart = rtrim(substr($address, 0, (0 - strlen($numberPart))));
99 } elseif (preg_match('/^([0-9]+)([a-zA-Z\s\-]+)$/', $parts[$partsCount - 2] . ' ' . $parts[$partsCount - 1])) { // House number 1 a
100 $numberPart = $parts[$partsCount - 2] . ' ' . $parts[$partsCount - 1];
101 $streetPart = rtrim(substr($address, 0, (0 - strlen($numberPart))));
102 } else {
103 $streetPart = $address;
104 }
105 } else {
106 $streetPart = $address;
107 }
108
109 // Convert the number to an object
110 if ($numberPart) {
111 $houseNumberDetails = sendy_parse_house_number($numberPart);
112 $number = $houseNumberDetails->number;
113 $addition = $houseNumberDetails->addition;
114 }
115
116 $object = new \stdClass();
117 $object->street = trim($streetPart);
118 $object->number = $number;
119 $object->addition = $addition;
120
121 return $object;
122 }
123 }
124
125 if (! function_exists('sendy_parse_house_number')) {
126 /**
127 * Split the house number and addition
128 *
129 * @param string $number
130 * @return object{house_number:string, house_number_addition:string|null}
131 */
132 function sendy_parse_house_number(string $number): stdClass {
133 $addition = null;
134
135 if (! ctype_digit($number)) {
136 $addition = ltrim($number, '0123456789');
137 $number = substr($number, 0, (0 - strlen($addition)));
138 }
139
140 $object = new \stdClass();
141 $object->number = trim($number);
142 $object->addition = $addition ? trim($addition) : null;
143
144 return $object;
145 }
146 }
147