PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.14
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.14
1.8.13.1 1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 All 212 releases
charitable / includes / utilities / charitable-utility-functions.php

charitable-utility-functions.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.14, at includes/utilities/charitable-utility-functions.php

432 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Charitable Utility Functions.
4 *
5 * Utility functions.
6 *
7 * @package Charitable/Functions/Utility
8 * @author David Bisset
9 * @copyright Copyright (c) 2022, WP Charitable LLC
10 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
11 * @since 1.0.0
12 * @version 1.6.55
13 */
14
15 // Exit if accessed directly.
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit;
18 }
19
20 /**
21 * Orders an array by a particular key.
22 *
23 * @since 1.5.0
24 *
25 * @param strign $key The key to sort by.
26 * @param array $a First element.
27 * @param array $b Element to compare against.
28 * @return int
29 */
30 function charitable_element_key_sort( $key, $a, $b ) {
31 foreach ( array( $a, $b ) as $item ) {
32 if ( ! array_key_exists( $key, $item ) ) {
33 if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) {
34 error_log( sprintf( '%s missing from element: ' . json_encode( $item ), $key ) );
35 }
36 }
37 }
38
39 if ( $a[ $key ] == $b[ $key ] ) {
40 return 0;
41 }
42
43 return $a[ $key ] < $b[ $key ] ? -1 : 1;
44 }
45
46 /**
47 * Orders an array by the priority key.
48 *
49 * @since 1.0.0
50 *
51 * @param array $a First element.
52 * @param array $b Element to compare against.
53 * @return int
54 */
55 function charitable_priority_sort( $a, $b ) {
56 return charitable_element_key_sort( 'priority', $a, $b );
57 }
58
59 /**
60 * Orders an array by the time key.
61 *
62 * @since 1.5.0
63 *
64 * @param array $a First element.
65 * @param array $b Element to compare against.
66 * @return int
67 */
68 function charitable_timestamp_sort( $a, $b ) {
69 return charitable_element_key_sort( 'time', $a, $b );
70 }
71
72 /**
73 * Checks whether function is disabled.
74 *
75 * Full credit to Pippin Williamson and the EDD team.
76 *
77 * @since 1.0.0
78 *
79 * @param string $function Name of the function.
80 * @return boolean Whether or not function is disabled.
81 */
82 function charitable_is_func_disabled( $function ) {
83 $disabled = explode( ',', ini_get( 'disable_functions' ) );
84
85 return in_array( $function, $disabled );
86 }
87
88 /**
89 * Verify a nonce. This also just ensures that the nonce is set.
90 *
91 * @since 1.0.0
92 *
93 * @param string $nonce The nonce name.
94 * @param string $action The nonce action.
95 * @param array $request_args Request arguments. If not set, will populate with $_GET.
96 * @return boolean
97 */
98 function charitable_verify_nonce( $nonce, $action, $request_args = array() ) {
99 if ( empty( $request_args ) ) {
100 $request_args = $_GET;
101 }
102
103 return isset( $request_args[ $nonce ] ) && wp_verify_nonce( $request_args[ $nonce ], $action );
104 }
105
106 /**
107 * Retrieve the timezone id.
108 *
109 * Credit: Pippin Williamson & the rest of the EDD team.
110 *
111 * @since 1.0.0
112 *
113 * @return string
114 */
115 function charitable_get_timezone_id() {
116 $timezone = get_option( 'timezone_string' );
117
118 /* If site timezone string exists, return it */
119 if ( $timezone ) {
120 return $timezone;
121 }
122
123 $utc_offset = 3600 * get_option( 'gmt_offset', 0 );
124
125 /* Get UTC offset, if it isn't set return UTC */
126 if ( ! $utc_offset ) {
127 return 'UTC';
128 }
129
130 /* Attempt to guess the timezone string from the UTC offset */
131 $timezone = timezone_name_from_abbr( '', $utc_offset );
132
133 /* Last try, guess timezone string manually */
134 if ( false === $timezone ) {
135
136 $is_dst = date( 'I' );
137
138 foreach ( timezone_abbreviations_list() as $abbr ) {
139 foreach ( $abbr as $city ) {
140 if ( $city['dst'] == $is_dst && $city['offset'] == $utc_offset ) {
141 return $city['timezone_id'];
142 }
143 }
144 }
145 }
146
147 /* If we still haven't figured out the timezone, fall back to UTC */
148 return 'UTC';
149 }
150
151 /**
152 * Given an array and a separate array of keys, returns a new array that only contains the
153 * elements in the original array with the specified keys.
154 *
155 * @since 1.5.0
156 *
157 * @param array $original_array The original array we need to pull a subset from.
158 * @param array $subset_keys The keys to use for our subset.
159 * @return array
160 */
161 function charitable_array_subset( array $original_array, $subset_keys ) {
162 return array_intersect_key( $original_array, array_flip( $subset_keys ) );
163 }
164
165 /**
166 * Ensure a number is a positive integer.
167 *
168 * @since 1.0.0
169 *
170 * @param mixed $i Number received.
171 * @return int|false
172 */
173 function charitable_validate_absint( $i ) {
174 return filter_var( $i, FILTER_VALIDATE_INT, array( 'min_range' => 1 ) );
175 }
176
177 /**
178 * Ensure a string is a valid email..
179 *
180 * @since 1.7.0.9
181 *
182 * @param mixed $i Number received.
183 * @return int|false
184 */
185 function charitable_validate_email( $i ) {
186 return filter_var( $i, FILTER_VALIDATE_EMAIL );
187 }
188
189 /**
190 * Sanitize any checkbox value.
191 *
192 * @since 1.5.0
193 *
194 * @param mixed $value Value set for checkbox, or false.
195 * @return boolean
196 */
197 function charitable_sanitize_checkbox( $value = false ) {
198 return intval( true == $value || 'on' == $value );
199 }
200
201 /**
202 * Format an array of strings as a sentence part.
203 *
204 * If there is one item in the string, this will just return that item.
205 * If there are two items, it will return a string like this: "x and y".
206 * If there are three or more items, it will return a string like this: "x, y and z".
207 *
208 * @since 1.3.0
209 *
210 * @param string[] $list The list.
211 * @return string
212 */
213 function charitable_list_to_sentence_part( $list ) {
214 $list = array_values( $list );
215
216 if ( 1 == count( $list ) ) {
217 return $list[0];
218 }
219
220 if ( 2 == count( $list ) ) {
221 return sprintf(
222 /* translators: %1$s: first list item; %2$s: second list item. */
223 _x( '%1$s and %2$s', 'x and y', 'charitable' ),
224 $list[0],
225 $list[1]
226 );
227 }
228
229 $last = array_pop( $list );
230
231 return sprintf(
232 /* translators: %1$s: all list items except last, comma-separated; %2$s: second list item. */
233 _x( '%1$s and %2$s', 'x and y', 'charitable' ),
234 implode( ', ', $list ),
235 $last
236 );
237 }
238
239 /**
240 * Sanitizes a date passed in the format of January 29, 2009.
241 *
242 * We use WP_Locale to parse the month that the user has set.
243 *
244 * @global WP_Locale $wp_locale
245 *
246 * @since 1.4.10
247 *
248 * @param string $date The date to be sanitized.
249 * @param string $return_format The date format to return. Default is U (timestamp).
250 * @return string|false
251 */
252 function charitable_sanitize_date( $date, $return_format = 'U' ) {
253 global $wp_locale;
254
255 if ( empty( $date ) || ! $date ) {
256 return false;
257 }
258
259 list( $month, $day, $year ) = explode( ' ', $date );
260
261 $day = trim( $day, ',' );
262 $month = 1 + array_search( $month, array_values( $wp_locale->month ), true );
263 $time = mktime( 0, 0, 0, $month, (int) $day, (int) $year );
264
265 if ( 'U' === $return_format ) {
266 return $time;
267 }
268
269 return date( $return_format, $time );
270 }
271
272 /**
273 * Sanitizes a date passed in the format of yyyy/mm/dd.
274 *
275 * @since 1.7.0.8
276 *
277 * @param string $date The date to be sanitized.
278 * @param string $return_format The date format to return. Default is U (timestamp).
279 * @return string|false
280 */
281 function charitable_sanitize_date_alt_format( $date, $return_format = 'U' ) {
282 global $wp_locale;
283
284 if ( empty( $date ) || ! $date ) {
285 return false;
286 }
287
288 $date_array = explode( '/', $date );
289
290 if ( count( $date_array ) !== 3 ) {
291 return false;
292 }
293
294 $day = intval( $date_array[ 2 ] );
295 $month = intval( $date_array[ 1 ] );
296 $year = intval( $date_array[ 0 ] );
297
298 $day = trim( $day, ',' );
299 $month = trim( $month, ',' );
300 $time = mktime( 0, 0, 0, $month, (int) $day, (int) $year );
301
302 if ( 'U' === $return_format ) {
303 return $time;
304 }
305
306 return date( $return_format, $time );
307 }
308
309 /**
310 * Sanitizes a date passed in the format of mm/dd/yyyy for form filters.
311 *
312 * @since 1.7.0.11
313 *
314 * @param string $date The date to be sanitized.
315 * @return string|false
316 */
317 function charitable_sanitize_date_filter_format( $date = false ) {
318
319 if ( empty( $date ) || ! $date ) {
320 return false;
321 }
322
323 // Convert the date to a timestamp.
324 $timestamp = strtotime( $date );
325
326 // If it's not empty and not equal to -1, then convert it to the mm/dd/yyyy format.
327 $return_value = ( empty( $timestamp ) && -1 !== $timestamp ) ? '' : date( 'Y/m/d', $timestamp );
328
329 return $return_value;
330 }
331
332 /**
333 * Sanitizes a date passed in the format of mm/dd/yyyy for form filters.
334 *
335 * @since 1.7.0.11
336 *
337 * @param string $date The date to be sanitized.
338 * @return string|false
339 */
340 function charitable_sanitize_date_export_format( $date = false ) {
341
342 if ( empty( $date ) || ! $date ) {
343 return false;
344 }
345
346 // Convert the date to a timestamp.
347 $timestamp = strtotime( $date );
348
349 // If it's not empty and not equal to -1, then convert it to the mm/dd/yyyy format.
350 $return_value = ( empty( $timestamp ) && -1 !== $timestamp ) ? '' : date( 'Y/m/d', $timestamp );
351
352 return $return_value;
353 }
354
355 /**
356 * Return a string containing the correct number & type of placeholders.
357 *
358 * @since 1.5.0
359 *
360 * @param int $count The number of placeholders to add.
361 * @param string $placeholder Type of placeholder to insert.
362 * @return string
363 */
364 function charitable_get_query_placeholders( $count = 1, $placeholder = '%s' ) {
365 $placeholders = array_fill( 0, $count, $placeholder );
366 return implode( ', ', $placeholders );
367 }
368
369 /**
370 * Return a list of pages in id=>title format for use in a select dropdown.
371 *
372 * @see get_pages
373 *
374 * @since 1.6.0
375 *
376 * @param array $args Optional arguments to be passed to get_pages.
377 * @return array
378 */
379 function charitable_get_pages_options( $args = array() ) {
380 $pages = get_pages( $args );
381
382 if ( ! $pages ) {
383 return array();
384 }
385
386 return array_combine( wp_list_pluck( $pages, 'ID' ), wp_list_pluck( $pages, 'post_title' ) );
387 }
388
389 /**
390 * Checks whether this is localhost.
391 *
392 * This is not fullproof. It uses a safelist of IP addresses.
393 *
394 * @since 1.6.14
395 *
396 * @return boolean
397 */
398 function charitable_is_localhost() {
399
400 if ( false !== ( defined( 'CHARITABLE_FORCE_NO_LOCALHOST_WITH_STRIPE_CONNECT' ) && CHARITABLE_FORCE_NO_LOCALHOST_WITH_STRIPE_CONNECT ) ) {
401 return false;
402 }
403
404 /**
405 * Filter list of localhost IP addresses.
406 *
407 * @since 1.6.14
408 *
409 * @param array $ip_addresses The list of IP addresses.
410 */
411 $safelist = apply_filters(
412 'charitable_localhost_ips',
413 array(
414 '127.0.0.1',
415 '::1',
416 )
417 );
418
419 return in_array( $_SERVER['REMOTE_ADDR'], $safelist );
420 }
421
422 /**
423 * Check whether we are currently using a block theme.
424 *
425 * @since Charitable version 1.6.55
426 *
427 * @return boolean
428 */
429 function charitable_is_block_theme() {
430 return function_exists( 'wp_is_block_theme' ) && wp_is_block_theme();
431 }
432