PluginProbe
Property Hive / 2.2.3
Property Hive v2.2.3
2.3.1 2.3.0 2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 All 261 releases
propertyhive / includes / ph-formatting-functions.php

ph-formatting-functions.php in Property Hive 2.2.3, at includes/ph-formatting-functions.php

281 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PropertyHive Formatting
4 *
5 * Functions for formatting data.
6 *
7 * @author PropertyHive
8 * @category Core
9 * @package PropertyHive/Functions
10 * @version 1.0.0
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly
15 }
16
17 /**
18 * Clean variables using sanitize_text_field.
19 * @param string|array $var
20 * @return string|array
21 */
22 function ph_clean( $var ) {
23
24 if ( is_array( $var ) ) {
25 return array_map( 'ph_clean', $var );
26 } else {
27 return is_scalar( $var ) ? sanitize_text_field( $var ) : $var;
28 }
29 }
30
31 /**
32 * Strip none numeric or comma chars from phone numbers
33 * @param string
34 * @return string
35 */
36
37 function ph_clean_telephone_number( $var ) {
38
39 return preg_replace( "/[^0-9,]/", "", $var );
40 }
41
42 /**
43 * Format monetary number value with decimal and thousands separators for display in a form field
44 * @param string
45 * @return string
46 */
47 function ph_display_price_field( $var, $use_separator_setting = false )
48 {
49 $float_var = (float)$var;
50
51 // If stored value isn't a valid number with decimals, display as it's stored
52 if ( $float_var !== floatval(0) )
53 {
54 // If there are decimals on the number, display them. If not, display none
55 $decimals = $float_var == intval($var) ? 0 : 2;
56
57 if ( !$use_separator_setting )
58 {
59 $decimal_separator = '.';
60 $thousands_separator = ',';
61 }
62 else
63 {
64 // Get custom thousands and decimal and separators, if set, when not displaying in an input
65 $thousands_separator = get_option('propertyhive_price_thousand_separator', ',');
66 $decimal_separator = get_option('propertyhive_price_decimal_separator', '.');
67 }
68
69 $var = number_format( $float_var, $decimals, $decimal_separator, $thousands_separator );
70 }
71
72 return (string)$var;
73 }
74
75 if ( ! function_exists( 'ph_rgb_from_hex' ) ) {
76
77 /**
78 * Hex darker/lighter/contrast functions for colours.
79 *
80 * @param mixed $color
81 * @return string
82 */
83 function ph_rgb_from_hex( $color ) {
84 $color = str_replace( '#', '', $color );
85 // Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF"
86 $color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color );
87
88 $rgb = array();
89 $rgb['R'] = hexdec( $color[0] . $color[1] );
90 $rgb['G'] = hexdec( $color[2] . $color[3] );
91 $rgb['B'] = hexdec( $color[4] . $color[5] );
92
93 return $rgb;
94 }
95 }
96
97 if ( ! function_exists( 'ph_hex_darker' ) ) {
98
99 /**
100 * Hex darker/lighter/contrast functions for colours.
101 *
102 * @param mixed $color
103 * @param int $factor (default: 30)
104 * @return string
105 */
106 function ph_hex_darker( $color, $factor = 30 ) {
107 $base = ph_rgb_from_hex( $color );
108 $color = '#';
109
110 foreach ( $base as $k => $v ) {
111 $amount = $v / 100;
112 $amount = round( $amount * $factor );
113 $new_decimal = $v - $amount;
114
115 $new_hex_component = dechex( $new_decimal );
116 if ( strlen( $new_hex_component ) < 2 ) {
117 $new_hex_component = "0" . $new_hex_component;
118 }
119 $color .= $new_hex_component;
120 }
121
122 return $color;
123 }
124 }
125
126 if ( ! function_exists( 'ph_hex_lighter' ) ) {
127
128 /**
129 * Hex darker/lighter/contrast functions for colours.
130 *
131 * @param mixed $color
132 * @param int $factor (default: 30)
133 * @return string
134 */
135 function ph_hex_lighter( $color, $factor = 30 ) {
136 $base = ph_rgb_from_hex( $color );
137 $color = '#';
138
139 foreach ( $base as $k => $v ) {
140 $amount = 255 - $v;
141 $amount = $amount / 100;
142 $amount = round( $amount * $factor );
143 $new_decimal = $v + $amount;
144
145 $new_hex_component = dechex( $new_decimal );
146 if ( strlen( $new_hex_component ) < 2 ) {
147 $new_hex_component = "0" . $new_hex_component;
148 }
149 $color .= $new_hex_component;
150 }
151
152 return $color;
153 }
154 }
155
156 if ( ! function_exists( 'ph_light_or_dark' ) ) {
157
158 /**
159 * Detect if we should use a light or dark colour on a background colour.
160 *
161 * @param mixed $color
162 * @param string $dark (default: '#000000')
163 * @param string $light (default: '#FFFFFF')
164 * @return string
165 */
166 function ph_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) {
167
168 $hex = str_replace( '#', '', $color );
169
170 $c_r = hexdec( substr( $hex, 0, 2 ) );
171 $c_g = hexdec( substr( $hex, 2, 2 ) );
172 $c_b = hexdec( substr( $hex, 4, 2 ) );
173
174 $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000;
175
176 return $brightness > 155 ? $dark : $light;
177 }
178 }
179
180 if ( ! function_exists( 'ph_format_hex' ) ) {
181
182 /**
183 * Format string as hex.
184 *
185 * @param string $hex
186 * @return string
187 */
188 function ph_format_hex( $hex ) {
189
190 $hex = trim( str_replace( '#', '', $hex ) );
191
192 if ( strlen( $hex ) == 3 ) {
193 $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
194 }
195
196 return $hex ? '#' . $hex : null;
197 }
198 }
199
200 function ph_nl2br($str)
201 {
202 // Match any <ul> or <ol> with their content
203 $pattern = '/(<ul[^>]*>.*?<\/ul>|<ol[^>]*>.*?<\/ol>)/is';
204 $parts = preg_split($pattern, $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
205
206 foreach ($parts as &$part) {
207 // If the part is not a list, apply nl2br
208 if (!preg_match($pattern, $part)) {
209 $part = nl2br($part);
210 }
211 }
212
213 // Reassemble the string
214 return implode('', $parts);
215 }
216
217 function ph_split_address_into_fields( $address )
218 {
219 $fields = [
220 'address_name_number' => '',
221 'address_street' => '',
222 'address_two' => '',
223 'address_three' => '',
224 'address_four' => '',
225 'address_postcode' => '',
226 'address_country' => ''
227 ];
228
229 // Replace newlines with commas and remove consecutive commas
230 $address = preg_replace('/\s*,\s*/', ', ', $address);
231 $address = preg_replace('/\s*\n\s*/', ', ', $address);
232 $address = preg_replace('/,+/', ',', $address);
233
234 // Explode the address by comma
235 $parts = explode(',', $address);
236
237 // Trim whitespace from each part
238 $parts = array_map('trim', $parts);
239
240 // Remove empty parts
241 $parts = array_filter($parts);
242
243 // Check the first part for building number and street
244 if (isset($parts[0])) {
245 if (preg_match('/^(\d+)\s+(.*)$/', $parts[0], $matches)) {
246 $fields['address_name_number'] = $matches[1];
247 $fields['address_street'] = $matches[2];
248 } else {
249 $fields['address_street'] = $parts[0];
250 }
251 array_shift($parts);
252 }
253
254 // Detect postcode in the remaining parts
255 foreach ($parts as $index => $part) {
256 if (preg_match('/[A-Z]{1,2}\d[A-Z\d]? \d[A-Z]{2}/i', $part, $postcodeMatch)) {
257 $fields['address_postcode'] = $postcodeMatch[0];
258 // Split the part containing the postcode
259 $remainingPart = preg_replace('/[A-Z]{1,2}\d[A-Z\d]? \d[A-Z]{2}/i', '', $part);
260 if (!empty(trim($remainingPart))) {
261 array_splice($parts, $index, 1, trim($remainingPart));
262 } else {
263 unset($parts[$index]);
264 }
265 // Check if the next part is the country
266 if (isset($parts[$index + 1])) {
267 $fields['address_country'] = $parts[$index + 1];
268 unset($parts[$index + 1]);
269 }
270 break;
271 }
272 }
273
274 // Assign remaining parts to Address Line 2, Town/City, and County
275 $remainingParts = array_values($parts);
276 if (isset($remainingParts[0])) $fields['address_two'] = $remainingParts[0];
277 if (isset($remainingParts[1])) $fields['address_three'] = $remainingParts[1];
278 if (isset($remainingParts[2])) $fields['address_four'] = $remainingParts[2];
279
280 return $fields;
281 }