| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Utility Functions. |
| 4 |
* |
| 5 |
* Utility functions. |
| 6 |
* |
| 7 |
* @package Charitable/Functions/Utility |
| 8 |
* @version 1.0.0 |
| 9 |
* @author Eric Daams |
| 10 |
* @copyright Copyright (c) 2015, Studio 164a |
| 11 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 12 |
*/ |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 15 |
|
| 16 |
/** |
| 17 |
* Orders an array by the priority key. |
| 18 |
* |
| 19 |
* @param array $a |
| 20 |
* @param array $b |
| 21 |
* @return int |
| 22 |
* @since 1.0.0 |
| 23 |
*/ |
| 24 |
function charitable_priority_sort($a, $b) { |
| 25 |
if ( $a['priority'] == $b['priority'] ) { |
| 26 |
return 0; |
| 27 |
} |
| 28 |
|
| 29 |
return $a['priority'] < $b['priority'] ? -1 : 1; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Checks whether function is disabled. |
| 34 |
* |
| 35 |
* Full credit to Pippin Williamson and the EDD team. |
| 36 |
* |
| 37 |
* @param string $function Name of the function. |
| 38 |
* @return bool Whether or not function is disabled. |
| 39 |
* @since 1.0.0 |
| 40 |
*/ |
| 41 |
function charitable_is_func_disabled( $function ) { |
| 42 |
$disabled = explode( ',', ini_get( 'disable_functions' ) ); |
| 43 |
|
| 44 |
return in_array( $function, $disabled ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Verify a nonce. This also just ensures that the nonce is set. |
| 49 |
* |
| 50 |
* @param string $nonce |
| 51 |
* @param string $action |
| 52 |
* @param |
| 53 |
* @return boolean |
| 54 |
* @since 1.0.0 |
| 55 |
*/ |
| 56 |
function charitable_verify_nonce( $nonce, $action, $request_args = array() ) { |
| 57 |
if ( empty( $request_args ) ) { |
| 58 |
$request_args = $_GET; |
| 59 |
} |
| 60 |
|
| 61 |
return isset( $request_args[ $nonce ] ) && wp_verify_nonce( $request_args[ $nonce ], $action ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Retrieve the timezone id. |
| 66 |
* |
| 67 |
* Credit: Pippin Williamson & the rest of the EDD team. |
| 68 |
* |
| 69 |
* @return string |
| 70 |
* @since 1.0.0 |
| 71 |
*/ |
| 72 |
function charitable_get_timezone_id() { |
| 73 |
$timezone = get_option( 'timezone_string' ); |
| 74 |
|
| 75 |
/* If site timezone string exists, return it */ |
| 76 |
if ( $timezone ) { |
| 77 |
return $timezone; |
| 78 |
} |
| 79 |
|
| 80 |
$utc_offset = 3600 * get_option( 'gmt_offset', 0 ); |
| 81 |
|
| 82 |
/* Get UTC offset, if it isn't set return UTC */ |
| 83 |
if ( ! $utc_offset ) { |
| 84 |
return 'UTC'; |
| 85 |
} |
| 86 |
|
| 87 |
/* Attempt to guess the timezone string from the UTC offset */ |
| 88 |
$timezone = timezone_name_from_abbr( '', $utc_offset ); |
| 89 |
|
| 90 |
/* Last try, guess timezone string manually */ |
| 91 |
if ( $timezone === false ) { |
| 92 |
|
| 93 |
$is_dst = date( 'I' ); |
| 94 |
|
| 95 |
foreach ( timezone_abbreviations_list() as $abbr ) { |
| 96 |
foreach ( $abbr as $city ) { |
| 97 |
if ( $city['dst'] == $is_dst && $city['offset'] == $utc_offset ) { |
| 98 |
return $city['timezone_id']; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/* If we still haven't figured out the timezone, fall back to UTC */ |
| 105 |
return 'UTC'; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Ensure a number is a positive integer. |
| 110 |
* |
| 111 |
* @return int|false |
| 112 |
* @since 1.0.0 |
| 113 |
*/ |
| 114 |
function charitable_validate_absint( $i ) { |
| 115 |
return filter_var( $i, FILTER_VALIDATE_INT, array( 'min_range' => 1 ) ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Format an array of strings as a sentence part. |
| 120 |
* |
| 121 |
* If there is one item in the string, this will just return that item. |
| 122 |
* If there are two items, it will return a string like this: "x and y". |
| 123 |
* If there are three or more items, it will return a string like this: "x, y and z". |
| 124 |
* |
| 125 |
* @param string[] $list |
| 126 |
* @return string |
| 127 |
* @since 1.3.0 |
| 128 |
*/ |
| 129 |
function charitable_list_to_sentence_part( $list ) { |
| 130 |
$list = array_values( $list ); |
| 131 |
|
| 132 |
if ( 1 == count( $list ) ) { |
| 133 |
return $list[0]; |
| 134 |
} |
| 135 |
|
| 136 |
if ( 2 == count( $list ) ) { |
| 137 |
return sprintf( _x( '%s and %s', 'x and y', 'charitable' ), $list[0], $list[1] ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
$last = array_pop( $list ); |
| 142 |
|
| 143 |
return sprintf( _x( '%s and %s', 'x and y', 'charitable' ), implode( ', ', $list ), $last ); |
| 144 |
} |