-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUtil.php
277 lines (234 loc) · 6.21 KB
/
Util.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/**
* Util
*
* @author Pronamic <[email protected]>
* @copyright 2005-2022 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Pay
*/
namespace Pronamic\WordPress\Pay;
use DateInterval;
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
use Pronamic\WordPress\Money\Money;
use SimpleXMLElement;
use WP_Error;
/**
* WordPress utility class
*
* @author Remco Tolsma
* @version 2.5.0
* @since 2.0.1
*/
class Util {
/**
* Format date interval.
*
* @param DateInterval $date_interval Date interval.
*
* @return string
*/
public static function format_date_interval( DateInterval $date_interval ) {
// Periods.
$periods = [];
foreach ( [ 'y', 'm', 'd', 'h', 'i', 's' ] as $period ) {
$value = $date_interval->$period;
// Check value.
if ( 0 === $value ) {
continue;
}
// Format.
$format = '';
switch ( $period ) {
case 'y':
/* translators: %s: number of years */
$format = _n( '%s year', '%s years', $value, 'pronamic_ideal' );
break;
case 'm':
/* translators: %s: number of months */
$format = _n( '%s month', '%s months', $value, 'pronamic_ideal' );
break;
case 'd':
/* translators: %s: number of days */
$format = _n( '%s day', '%s days', $value, 'pronamic_ideal' );
break;
case 'h':
/* translators: %s: number of hours */
$format = _n( '%s hour', '%s hours', $value, 'pronamic_ideal' );
break;
case 'i':
/* translators: %s: number of minutes */
$format = _n( '%s minute', '%s minutes', $value, 'pronamic_ideal' );
break;
case 's':
/* translators: %s: number of seconds */
$format = _n( '%s second', '%s seconds', $value, 'pronamic_ideal' );
break;
}
// Add period.
$periods[] = \sprintf( $format, $value );
}
// Multiple periods.
if ( count( $periods ) > 1 ) {
$last_period = \array_pop( $periods );
$formatted = \implode( ', ', $periods );
return sprintf(
/* translators: 1: formatted periods, 2: last formatted period */
__( '%1$s and %2$s', 'pronamic_ideal' ),
$formatted,
$last_period
);
}
// Single period.
$formatted = \implode( ', ', $periods );
return $formatted;
}
/**
* Format recurrences date interval.
*
* @param DateInterval $date_interval Date interval.
*
* @return string
*/
public static function format_recurrences( DateInterval $date_interval ) {
$formatted_interval = self::format_date_interval( $date_interval );
// Check empty date interval.
if ( empty( $formatted_interval ) ) {
return '—';
}
return sprintf(
/* translators: %s: formatted date interval periods */
__( 'Every %s', 'pronamic_ideal' ),
$formatted_interval
);
}
/**
* Format interval.
*
* @param int $interval The interval number.
* @param string $period The period indicator.
*
* @return string|null
*/
public static function format_interval( $interval, $period ) {
switch ( $period ) {
case 'D':
case 'day':
case 'days':
/* translators: %s: interval */
return sprintf( _n( 'Every %s day', 'Every %s days', $interval, 'pronamic_ideal' ), $interval );
case 'W':
case 'week':
case 'weeks':
/* translators: %s: interval */
return sprintf( _n( 'Every %s week', 'Every %s weeks', $interval, 'pronamic_ideal' ), $interval );
case 'M':
case 'month':
case 'months':
/* translators: %s: interval */
return sprintf( _n( 'Every %s month', 'Every %s months', $interval, 'pronamic_ideal' ), $interval );
case 'Y':
case 'year':
case 'years':
/* translators: %s: interval */
return sprintf( _n( 'Every %s year', 'Every %s years', $interval, 'pronamic_ideal' ), $interval );
}
return null;
}
/**
* Convert single interval period character to full name.
*
* @param string $interval_period string Short interval period (D, W, M or Y).
*
* @return string
*/
public static function to_interval_name( $interval_period ) {
switch ( $interval_period ) {
case 'D':
return 'days';
case 'W':
return 'weeks';
case 'M':
return 'months';
case 'Y':
return 'years';
}
return $interval_period;
}
/**
* Format frequency.
*
* @param int $frequency The number of times.
*
* @return string
*/
public static function format_frequency( $frequency ) {
if ( empty( $frequency ) ) {
return _x( 'Unlimited', 'Recurring payment', 'pronamic_ideal' );
}
/* translators: %s: frequency */
return sprintf( _n( '%s period', '%s periods', $frequency, 'pronamic_ideal' ), $frequency );
}
/**
* Get hidden inputs HTML for data.
*
* @param array $data Array with name and value pairs to convert to hidden HTML input elements.
*
* @return string
*/
public static function html_hidden_fields( $data ) {
$html = '';
foreach ( $data as $name => $value ) {
$html .= sprintf( '<input type="hidden" name="%s" value="%s" />', esc_attr( $name ), esc_attr( $value ) );
}
return $html;
}
/**
* Array to HTML attributes.
*
* @param array $attributes The key and value pairs to convert to HTML attributes.
*
* @return string
*/
public static function array_to_html_attributes( array $attributes ) {
$html = '';
foreach ( $attributes as $key => $value ) {
// Check boolean attribute.
if ( \is_bool( $value ) ) {
if ( $value ) {
$html .= sprintf( '%s ', $key );
}
continue;
}
$html .= sprintf( '%s="%s" ', $key, esc_attr( $value ) );
}
$html = trim( $html );
return $html;
}
/**
* Select options grouped.
*
* @param array $groups The grouped select options.
* @param string $selected_value The selected value.
*
* @return string
*/
public static function select_options_grouped( $groups, $selected_value = null ) {
$html = '';
if ( is_array( $groups ) ) {
foreach ( $groups as $group ) {
$optgroup = isset( $group['name'] ) && ! empty( $group['name'] );
if ( $optgroup ) {
$html .= '<optgroup label="' . $group['name'] . '">';
}
foreach ( $group['options'] as $value => $label ) {
$html .= '<option value="' . $value . '" ' . selected( $selected_value, $value, false ) . '>' . $label . '</option>';
}
if ( $optgroup ) {
$html .= '</optgroup>';
}
}
}
return $html;
}
}