PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.5
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.5
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.5, at includes/utilities/charitable-utility-functions.php

335 lines 7.8 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 error_log( sprintf( '%s missing from element: ' . json_encode( $item ), $key ) );
34 }
35 }
36
37 if ( $a[ $key ] == $b[ $key ] ) {
38 return 0;
39 }
40
41 return $a[ $key ] < $b[ $key ] ? -1 : 1;
42 }
43
44 /**
45 * Orders an array by the priority key.
46 *
47 * @since 1.0.0
48 *
49 * @param array $a First element.
50 * @param array $b Element to compare against.
51 * @return int
52 */
53 function charitable_priority_sort( $a, $b ) {
54 return charitable_element_key_sort( 'priority', $a, $b );
55 }
56
57 /**
58 * Orders an array by the time key.
59 *
60 * @since 1.5.0
61 *
62 * @param array $a First element.
63 * @param array $b Element to compare against.
64 * @return int
65 */
66 function charitable_timestamp_sort( $a, $b ) {
67 return charitable_element_key_sort( 'time', $a, $b );
68 }
69
70 /**
71 * Checks whether function is disabled.
72 *
73 * Full credit to Pippin Williamson and the EDD team.
74 *
75 * @since 1.0.0
76 *
77 * @param string $function Name of the function.
78 * @return boolean Whether or not function is disabled.
79 */
80 function charitable_is_func_disabled( $function ) {
81 $disabled = explode( ',', ini_get( 'disable_functions' ) );
82
83 return in_array( $function, $disabled );
84 }
85
86 /**
87 * Verify a nonce. This also just ensures that the nonce is set.
88 *
89 * @since 1.0.0
90 *
91 * @param string $nonce The nonce name.
92 * @param string $action The nonce action.
93 * @param array $request_args Request arguments. If not set, will populate with $_GET.
94 * @return boolean
95 */
96 function charitable_verify_nonce( $nonce, $action, $request_args = array() ) {
97 if ( empty( $request_args ) ) {
98 $request_args = $_GET;
99 }
100
101 return isset( $request_args[ $nonce ] ) && wp_verify_nonce( $request_args[ $nonce ], $action );
102 }
103
104 /**
105 * Retrieve the timezone id.
106 *
107 * Credit: Pippin Williamson & the rest of the EDD team.
108 *
109 * @since 1.0.0
110 *
111 * @return string
112 */
113 function charitable_get_timezone_id() {
114 $timezone = get_option( 'timezone_string' );
115
116 /* If site timezone string exists, return it */
117 if ( $timezone ) {
118 return $timezone;
119 }
120
121 $utc_offset = 3600 * get_option( 'gmt_offset', 0 );
122
123 /* Get UTC offset, if it isn't set return UTC */
124 if ( ! $utc_offset ) {
125 return 'UTC';
126 }
127
128 /* Attempt to guess the timezone string from the UTC offset */
129 $timezone = timezone_name_from_abbr( '', $utc_offset );
130
131 /* Last try, guess timezone string manually */
132 if ( false === $timezone ) {
133
134 $is_dst = date( 'I' );
135
136 foreach ( timezone_abbreviations_list() as $abbr ) {
137 foreach ( $abbr as $city ) {
138 if ( $city['dst'] == $is_dst && $city['offset'] == $utc_offset ) {
139 return $city['timezone_id'];
140 }
141 }
142 }
143 }
144
145 /* If we still haven't figured out the timezone, fall back to UTC */
146 return 'UTC';
147 }
148
149 /**
150 * Given an array and a separate array of keys, returns a new array that only contains the
151 * elements in the original array with the specified keys.
152 *
153 * @since 1.5.0
154 *
155 * @param array $original_array The original array we need to pull a subset from.
156 * @param array $subset_keys The keys to use for our subset.
157 * @return array
158 */
159 function charitable_array_subset( array $original_array, $subset_keys ) {
160 return array_intersect_key( $original_array, array_flip( $subset_keys ) );
161 }
162
163 /**
164 * Ensure a number is a positive integer.
165 *
166 * @since 1.0.0
167 *
168 * @param mixed $i Number received.
169 * @return int|false
170 */
171 function charitable_validate_absint( $i ) {
172 return filter_var( $i, FILTER_VALIDATE_INT, array( 'min_range' => 1 ) );
173 }
174
175 /**
176 * Sanitize any checkbox value.
177 *
178 * @since 1.5.0
179 *
180 * @param mixed $value Value set for checkbox, or false.
181 * @return boolean
182 */
183 function charitable_sanitize_checkbox( $value = false ) {
184 return intval( true == $value || 'on' == $value );
185 }
186
187 /**
188 * Format an array of strings as a sentence part.
189 *
190 * If there is one item in the string, this will just return that item.
191 * If there are two items, it will return a string like this: "x and y".
192 * If there are three or more items, it will return a string like this: "x, y and z".
193 *
194 * @since 1.3.0
195 *
196 * @param string[] $list The list.
197 * @return string
198 */
199 function charitable_list_to_sentence_part( $list ) {
200 $list = array_values( $list );
201
202 if ( 1 == count( $list ) ) {
203 return $list[0];
204 }
205
206 if ( 2 == count( $list ) ) {
207 return sprintf(
208 /* translators: %1$s: first list item; %2$s: second list item. */
209 _x( '%1$s and %2$s', 'x and y', 'charitable' ),
210 $list[0],
211 $list[1]
212 );
213 }
214
215 $last = array_pop( $list );
216
217 return sprintf(
218 /* translators: %1$s: all list items except last, comma-separated; %2$s: second list item. */
219 _x( '%1$s and %2$s', 'x and y', 'charitable' ),
220 implode( ', ', $list ),
221 $last
222 );
223 }
224
225 /**
226 * Sanitizes a date passed in the format of January 29, 2009.
227 *
228 * We use WP_Locale to parse the month that the user has set.
229 *
230 * @global WP_Locale $wp_locale
231 *
232 * @since 1.4.10
233 *
234 * @param string $date The date to be sanitized.
235 * @param string $return_format The date format to return. Default is U (timestamp).
236 * @return string|false
237 */
238 function charitable_sanitize_date( $date, $return_format = 'U' ) {
239 global $wp_locale;
240
241 if ( empty( $date ) || ! $date ) {
242 return false;
243 }
244
245 list( $month, $day, $year ) = explode( ' ', $date );
246
247 $day = trim( $day, ',' );
248 $month = 1 + array_search( $month, array_values( $wp_locale->month ), true );
249 $time = mktime( 0, 0, 0, $month, (int) $day, (int) $year );
250
251 if ( 'U' === $return_format ) {
252 return $time;
253 }
254
255 return date( $return_format, $time );
256 }
257
258 /**
259 * Return a string containing the correct number & type of placeholders.
260 *
261 * @since 1.5.0
262 *
263 * @param int $count The number of placeholders to add.
264 * @param string $placeholder Type of placeholder to insert.
265 * @return string
266 */
267 function charitable_get_query_placeholders( $count = 1, $placeholder = '%s' ) {
268 $placeholders = array_fill( 0, $count, $placeholder );
269 return implode( ', ', $placeholders );
270 }
271
272 /**
273 * Return a list of pages in id=>title format for use in a select dropdown.
274 *
275 * @see get_pages
276 *
277 * @since 1.6.0
278 *
279 * @param array $args Optional arguments to be passed to get_pages.
280 * @return array
281 */
282 function charitable_get_pages_options( $args = array() ) {
283 $pages = get_pages( $args );
284
285 if ( ! $pages ) {
286 return array();
287 }
288
289 return array_combine( wp_list_pluck( $pages, 'ID' ), wp_list_pluck( $pages, 'post_title' ) );
290 }
291
292 /**
293 * Checks whether this is localhost.
294 *
295 * This is not fullproof. It uses a safelist of IP addresses.
296 *
297 * @since 1.6.14
298 *
299 * @return boolean
300 */
301 function charitable_is_localhost() {
302
303 if ( false !== ( defined( 'CHARITABLE_FORCE_NO_LOCALHOST_WITH_STRIPE_CONNECT' ) && CHARITABLE_FORCE_NO_LOCALHOST_WITH_STRIPE_CONNECT ) ) {
304 return false;
305 }
306
307 /**
308 * Filter list of localhost IP addresses.
309 *
310 * @since 1.6.14
311 *
312 * @param array $ip_addresses The list of IP addresses.
313 */
314 $safelist = apply_filters(
315 'charitable_localhost_ips',
316 array(
317 '127.0.0.1',
318 '::1',
319 )
320 );
321
322 return in_array( $_SERVER['REMOTE_ADDR'], $safelist );
323 }
324
325 /**
326 * Check whether we are currently using a block theme.
327 *
328 * @since Charitable version 1.6.55
329 *
330 * @return boolean
331 */
332 function charitable_is_block_theme() {
333 return function_exists( 'wp_is_block_theme' ) && wp_is_block_theme();
334 }
335