|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Functions that don't require GravityView or Gravity Forms API access but are used in the plugin to extend PHP and WP functions |
|
4
|
|
|
* @since 1.12 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Get the URL for a CSS file |
|
10
|
|
|
* |
|
11
|
|
|
* If there's a CSS file with the same name as a GravityView CSS file in the current theme directory, it will be used. |
|
12
|
|
|
* Place the CSS file in a `/gravityview/css/` sub-directory. |
|
13
|
|
|
* |
|
14
|
|
|
* Example: /twentysixteen/gravityview/css/gv-default-styles.css |
|
15
|
|
|
* |
|
16
|
|
|
* Will use, in order: |
|
17
|
|
|
* 1) [theme directory]/gravityview/css/ |
|
18
|
|
|
* 2) [gravityview plugin]/css/ (no check performed) |
|
19
|
|
|
* |
|
20
|
|
|
* @since 1.17 |
|
21
|
|
|
* |
|
22
|
|
|
* @uses get_stylesheet_directory() |
|
23
|
|
|
* @uses get_stylesheet_directory_uri() |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $css_file Filename of the CSS file (like gv-default-styles.css) |
|
26
|
|
|
* @param string $dir_path Absolute path to the directory where the CSS file is stored. If empty, uses default GravityView templates CSS folder. |
|
27
|
|
|
* |
|
28
|
|
|
* @return string URL path to the file. |
|
29
|
|
|
*/ |
|
30
|
|
|
function gravityview_css_url( $css_file = '', $dir_path = '' ) { |
|
31
|
|
|
|
|
32
|
|
|
// If there's an overriding CSS file in the current template folder, use it. |
|
33
|
|
|
$template_css_path = trailingslashit( get_stylesheet_directory() ) . 'gravityview/css/' . $css_file; |
|
34
|
|
|
|
|
35
|
|
|
if( file_exists( $template_css_path ) ) { |
|
36
|
|
|
$path = trailingslashit( get_stylesheet_directory_uri() ) . 'gravityview/css/' . $css_file; |
|
37
|
|
|
gravityview()->log->debug( 'Stylesheet override ({css_file})', array( 'css_file' => esc_attr( $css_file ) ) ); |
|
38
|
|
|
} else { |
|
39
|
|
|
// Default: use GravityView CSS file |
|
40
|
|
|
|
|
41
|
|
|
// If no path is provided, assume default plugin templates CSS folder |
|
42
|
|
|
if( '' === $dir_path ) { |
|
43
|
|
|
$dir_path = GRAVITYVIEW_DIR . 'templates/css/'; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// plugins_url() expects a path to a file, not directory. We append a file to be stripped. |
|
47
|
|
|
$path = plugins_url( $css_file, trailingslashit( $dir_path ) . 'stripped-by-plugin_basename.php' ); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $path; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Check whether a variable is not an empty string |
|
55
|
|
|
* |
|
56
|
|
|
* @see /templates/fields/product.php Used to check whether the product array is empty or not |
|
57
|
|
|
* |
|
58
|
|
|
* @since 1.12 |
|
59
|
|
|
* |
|
60
|
|
|
* @param mixed $mixed Variable to check |
|
61
|
|
|
* |
|
62
|
|
|
* @return bool true: $mixed is *not* an empty string; false: $mixed *is* an empty string |
|
63
|
|
|
*/ |
|
64
|
|
|
function gravityview_is_not_empty_string( $mixed = '' ) { |
|
65
|
1 |
|
return ( $mixed !== '' ); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get `get_permalink()` without the home_url() prepended to it. |
|
70
|
|
|
* |
|
71
|
|
|
* get_permalink() does a lot of good stuff: it gets the correct permalink structure for custom post types, pages, |
|
72
|
|
|
* posts, etc. Instead of using `?p={id}`, `?page_id={id}`, or `?p={id}&post_type={post_type}`, by using |
|
73
|
|
|
* get_permalink(), we can use `?p=slug` or `?gravityview={slug}` |
|
74
|
|
|
* |
|
75
|
|
|
* We could do this in a cleaner fashion, but this prevents a lot of code duplication, checking for URL structure, etc. |
|
76
|
|
|
* |
|
77
|
|
|
* @param int|WP_Post $id Optional. Post ID or post object. Default current post. |
|
78
|
|
|
* |
|
79
|
|
|
* @return array URL args, if exists. Empty array if not. |
|
80
|
|
|
*/ |
|
81
|
|
|
function gravityview_get_permalink_query_args( $id = 0 ) { |
|
82
|
|
|
|
|
83
|
4 |
|
$parsed_permalink = parse_url( get_permalink( $id ) ); |
|
84
|
|
|
|
|
85
|
4 |
|
$permalink_args = isset( $parsed_permalink['query'] ) ? $parsed_permalink['query'] : false; |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
4 |
|
if( empty( $permalink_args ) ) { |
|
88
|
|
|
return array(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
4 |
|
parse_str( $permalink_args, $args ); |
|
92
|
|
|
|
|
93
|
4 |
|
return $args; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Similar to the WordPress `selected()`, `checked()`, and `disabled()` functions, except it allows arrays to be passed as current value |
|
99
|
|
|
* |
|
100
|
|
|
* @see selected() WordPress core function |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $value One of the values to compare |
|
103
|
|
|
* @param mixed $current (true) The other value to compare if not just true |
|
104
|
|
|
* @param bool $echo Whether to echo or just return the string |
|
105
|
|
|
* @param string $type The type of checked|selected|disabled we are doing |
|
106
|
|
|
* |
|
107
|
|
|
* @return string html attribute or empty string |
|
108
|
|
|
*/ |
|
109
|
|
|
function gv_selected( $value, $current, $echo = true, $type = 'selected' ) { |
|
110
|
|
|
|
|
111
|
|
|
$output = ''; |
|
112
|
|
|
if( is_array( $current ) ) { |
|
113
|
|
|
if( in_array( $value, $current ) ) { |
|
114
|
|
|
$output = __checked_selected_helper( true, true, false, $type ); |
|
115
|
|
|
} |
|
116
|
|
|
} else { |
|
117
|
|
|
$output = __checked_selected_helper( $value, $current, false, $type ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if( $echo ) { |
|
121
|
|
|
echo $output; |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $output; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
if( ! function_exists( 'gravityview_sanitize_html_class' ) ) { |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* sanitize_html_class doesn't handle spaces (multiple classes). We remedy that. |
|
132
|
|
|
* |
|
133
|
|
|
* @uses sanitize_html_class |
|
134
|
|
|
* |
|
135
|
|
|
* @param string|array $classes Text or array of classes to sanitize |
|
136
|
|
|
* |
|
137
|
|
|
* @return string Sanitized CSS string |
|
138
|
|
|
*/ |
|
139
|
|
|
function gravityview_sanitize_html_class( $classes ) { |
|
140
|
|
|
|
|
141
|
31 |
|
if ( is_string( $classes ) ) { |
|
142
|
31 |
|
$classes = explode( ' ', $classes ); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
// If someone passes something not string or array, we get outta here. |
|
146
|
31 |
|
if ( ! is_array( $classes ) ) { |
|
147
|
|
|
return $classes; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
31 |
|
$classes = array_map( 'trim', $classes ); |
|
151
|
31 |
|
$classes = array_map( 'sanitize_html_class', $classes ); |
|
152
|
31 |
|
$classes = array_filter( $classes ); |
|
153
|
|
|
|
|
154
|
31 |
|
return implode( ' ', $classes ); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Replace multiple newlines, tabs, and spaces with a single space |
|
160
|
|
|
* |
|
161
|
|
|
* First, runs normalize_whitespace() on a string. This replaces multiple lines with a single line, and tabs with spaces. |
|
162
|
|
|
* We then strip any tabs or newlines and replace *those* with a single space. |
|
163
|
|
|
* |
|
164
|
|
|
* @see normalize_whitespace() |
|
165
|
|
|
* @see GravityView_Helper_Functions_Test::test_gravityview_strip_whitespace |
|
166
|
|
|
* @since 1.13 |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $string String to strip whitespace from |
|
169
|
|
|
* |
|
170
|
|
|
* @return string Stripped string! |
|
171
|
|
|
*/ |
|
172
|
|
|
function gravityview_strip_whitespace( $string ) { |
|
173
|
5 |
|
$string = normalize_whitespace( $string ); |
|
174
|
5 |
|
return preg_replace('/[\r\n\t ]+/', ' ', $string ); |
|
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Get the contents of a file using `include()` and `ob_start()` |
|
179
|
|
|
* |
|
180
|
|
|
* @since 1.13 |
|
181
|
|
|
* @since 1.15 Added $object param |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $file_path Full path to a file |
|
184
|
|
|
* @param mixed $object Pass pseudo-global to the included file |
|
185
|
|
|
* @return string Included file contents |
|
186
|
|
|
*/ |
|
187
|
|
|
function gravityview_ob_include( $file_path, $object = NULL ) { |
|
|
|
|
|
|
188
|
3 |
|
if( ! file_exists( $file_path ) ) { |
|
189
|
|
|
gravityview()->log->error( 'File path does not exist. {path}', array( 'path' => $file_path ) ); |
|
190
|
|
|
return ''; |
|
191
|
|
|
} |
|
192
|
3 |
|
ob_start(); |
|
193
|
3 |
|
include( $file_path ); |
|
194
|
3 |
|
return ob_get_clean(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Get an image of our intrepid explorer friend |
|
199
|
|
|
* |
|
200
|
|
|
* @since 1.12 |
|
201
|
|
|
* @since 2.1 Added $class parameter |
|
202
|
|
|
* |
|
203
|
|
|
* @param int $height Height of the cutie in pixels |
|
204
|
|
|
* @param null|string $css_class If defined, use the passed CSS class (can be empty string). Otherwise, use default alignleft (or alignright, based on RTL). |
|
205
|
|
|
* |
|
206
|
|
|
* @return string HTML image tag with floaty's cute mug on it |
|
207
|
|
|
*/ |
|
208
|
|
|
function gravityview_get_floaty( $height = 87, $css_class = null ) { |
|
209
|
|
|
|
|
210
|
2 |
|
$width = $height * 0.7586206897; |
|
211
|
|
|
|
|
212
|
2 |
|
if( function_exists('is_rtl') && is_rtl() ) { |
|
|
|
|
|
|
213
|
|
|
$style = 'margin:10px 10px 10px 0;'; |
|
214
|
|
|
$css_class = is_string( $css_class ) ? $css_class : 'alignright'; |
|
215
|
|
|
} else { |
|
216
|
2 |
|
$style = 'margin:10px 10px 10px 0;'; |
|
217
|
2 |
|
$css_class = is_string( $css_class ) ? $css_class : 'alignleft'; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
2 |
|
return '<img src="'. esc_url( plugins_url( 'assets/images/astronaut-200x263.png', GRAVITYVIEW_FILE ) ) .'" class="'. gravityview_sanitize_html_class( $css_class ).'" height="'.intval( $height ).'" width="'.round( $width, 2 ).'" alt="The GravityView Astronaut Says:" style="'.$style.'" />'; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Intelligently format a number |
|
225
|
|
|
* |
|
226
|
|
|
* If you don't define the number of decimal places, then it will use the existing number of decimal places. This is done |
|
227
|
|
|
* in a way that respects the localization of the site. |
|
228
|
|
|
* |
|
229
|
|
|
* If you do define decimals, it uses number_format_i18n() |
|
230
|
|
|
* |
|
231
|
|
|
* @see number_format_i18n() |
|
232
|
|
|
* |
|
233
|
|
|
* @since 1.13 |
|
234
|
|
|
* |
|
235
|
|
|
* @param int|float|string|double $number A number to format |
|
236
|
|
|
* @param int|string $decimals Optional. Precision of the number of decimal places. Default '' (use existing number of decimals) |
|
237
|
|
|
* |
|
238
|
|
|
* @return string Converted number in string format. |
|
239
|
|
|
*/ |
|
240
|
|
|
function gravityview_number_format( $number, $decimals = '' ) { |
|
241
|
105 |
|
global $wp_locale; |
|
|
|
|
|
|
242
|
|
|
|
|
243
|
105 |
|
if( '' === $decimals ) { |
|
244
|
|
|
|
|
245
|
105 |
|
$decimal_point = isset( $wp_locale ) ? $wp_locale->number_format['decimal_point'] : '.'; |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Calculate the position of the decimal point in the number |
|
249
|
|
|
* @see http://stackoverflow.com/a/2430144/480856 |
|
250
|
|
|
*/ |
|
251
|
105 |
|
$decimals = strlen( substr( strrchr( $number, $decimal_point ), 1 ) ); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
105 |
|
$number = number_format_i18n( $number, (int)$decimals ); |
|
|
|
|
|
|
255
|
|
|
|
|
256
|
105 |
|
return $number; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Convert a whole link into a shorter link for display |
|
262
|
|
|
* |
|
263
|
|
|
* @since 1.1 |
|
264
|
|
|
* |
|
265
|
|
|
* @param string $value Existing URL |
|
266
|
|
|
* @return string If parse_url doesn't find a 'host', returns original value. Otherwise, returns formatted link. |
|
267
|
|
|
*/ |
|
268
|
|
|
function gravityview_format_link( $value = null ) { |
|
269
|
|
|
|
|
|
|
|
|
|
270
|
|
|
|
|
271
|
5 |
|
$parts = parse_url( $value ); |
|
272
|
|
|
|
|
273
|
|
|
// No domain? Strange...show the original text. |
|
274
|
5 |
|
if( empty( $parts['host'] ) ) { |
|
275
|
2 |
|
return $value; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
// Start with empty value for the return URL |
|
279
|
5 |
|
$return = ''; |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* @filter `gravityview_anchor_text_striphttp` Strip scheme from the displayed URL? |
|
283
|
|
|
* @since 1.5.1 |
|
284
|
|
|
* @param boolean $enable Whether to strip the scheme. Return false to show scheme. (default: true)\n |
|
285
|
|
|
* If true: `http://example.com => example.com` |
|
286
|
|
|
*/ |
|
287
|
5 |
|
if( false === apply_filters('gravityview_anchor_text_striphttp', true) ) { |
|
|
|
|
|
|
288
|
|
|
|
|
289
|
|
|
if( isset( $parts['scheme'] ) ) { |
|
290
|
|
|
$return .= $parts['scheme']; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
// The domain, which may contain a subdomain |
|
296
|
5 |
|
$domain = $parts['host']; |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* @filter `gravityview_anchor_text_stripwww` Strip www from the domain? |
|
300
|
|
|
* @since 1.5.1 |
|
301
|
|
|
* @param boolean $enable Whether to strip www. Return false to show www. (default: true)\n |
|
302
|
|
|
* If true: `www.example.com => example.com` |
|
303
|
|
|
*/ |
|
304
|
5 |
|
$strip_www = apply_filters('gravityview_anchor_text_stripwww', true ); |
|
|
|
|
|
|
305
|
|
|
|
|
306
|
5 |
|
if( $strip_www ) { |
|
307
|
5 |
|
$domain = str_replace('www.', '', $domain ); |
|
|
|
|
|
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* @filter `gravityview_anchor_text_nosubdomain` Strip subdomains from the domain? |
|
312
|
|
|
* @since 1.5.1 |
|
313
|
|
|
* @param boolean $enable Whether to strip subdomains. Return false to show subdomains. (default: true)\n |
|
314
|
|
|
* If true: `http://demo.example.com => example.com` \n |
|
315
|
|
|
* If false: `http://demo.example.com => demo.example.com` |
|
316
|
|
|
*/ |
|
317
|
5 |
|
$strip_subdomains = apply_filters('gravityview_anchor_text_nosubdomain', true); |
|
|
|
|
|
|
318
|
|
|
|
|
319
|
5 |
|
if( $strip_subdomains ) { |
|
320
|
|
|
|
|
321
|
4 |
|
$domain = _gravityview_strip_subdomain( $parts['host'] ); |
|
322
|
|
|
|
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
// Add the domain |
|
326
|
5 |
|
$return .= $domain; |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* @filter `gravityview_anchor_text_rootonly` Display link path going only to the base directory, not a sub-directory or file? |
|
330
|
|
|
* @since 1.5.1 |
|
331
|
|
|
* @param boolean $enable Whether to enable "root only". Return false to show full path. (default: true)\n |
|
332
|
|
|
* If true: `http://example.com/sub/directory/page.html => example.com` \n |
|
333
|
|
|
* If false: `http://example.com/sub/directory/page.html => example.com/sub/directory/page.html` |
|
334
|
|
|
*/ |
|
335
|
5 |
|
$root_only = apply_filters('gravityview_anchor_text_rootonly', true); |
|
|
|
|
|
|
336
|
|
|
|
|
337
|
5 |
|
if( empty( $root_only ) ) { |
|
338
|
|
|
|
|
339
|
1 |
|
if( isset( $parts['path'] ) ) { |
|
340
|
1 |
|
$return .= $parts['path']; |
|
341
|
|
|
} |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
/** |
|
345
|
|
|
* @filter `gravityview_anchor_text_noquerystring` Strip the query string from the end of the URL? |
|
346
|
|
|
* @since 1.5.1 |
|
347
|
|
|
* @param boolean $enable Whether to enable "root only". Return false to show full path. (default: true)\n |
|
348
|
|
|
* If true: `http://example.com/?query=example => example.com` |
|
349
|
|
|
*/ |
|
350
|
5 |
|
$strip_query_string = apply_filters('gravityview_anchor_text_noquerystring', true ); |
|
|
|
|
|
|
351
|
|
|
|
|
352
|
5 |
|
if( empty( $strip_query_string ) ) { |
|
353
|
|
|
|
|
354
|
1 |
|
if( isset( $parts['query'] ) ) { |
|
355
|
1 |
|
$return .= '?'.$parts['query']; |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
5 |
|
return $return; |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* Do a _very_ basic match for second-level TLD domains, like `.co.uk` |
|
365
|
|
|
* |
|
366
|
|
|
* Ideally, we'd use https://github.com/jeremykendall/php-domain-parser to check for this, but it's too much work for such a basic functionality. Maybe if it's needed more in the future. So instead, we use [Basic matching regex](http://stackoverflow.com/a/12372310). |
|
367
|
|
|
* @param string $domain Domain to check if it's a TLD or subdomain |
|
|
|
|
|
|
368
|
|
|
* @return string Extracted domain if it has a subdomain |
|
369
|
|
|
*/ |
|
370
|
|
|
function _gravityview_strip_subdomain( $string_maybe_has_subdomain ) { |
|
371
|
|
|
|
|
372
|
2 |
|
if( preg_match("/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.(?:com\.|co\.|net\.|org\.|firm\.|me\.|school\.|law\.|gov\.|mod\.|msk\.|irkutsks\.|sa\.|act\.|police\.|plc\.|ac\.|tm\.|asso\.|biz\.|pro\.|cg\.|telememo\.)?[a-z\.]{2,6})$/i", $string_maybe_has_subdomain, $matches ) ) { |
|
|
|
|
|
|
373
|
2 |
|
return $matches['domain']; |
|
374
|
|
|
} else { |
|
375
|
|
|
return $string_maybe_has_subdomain; |
|
376
|
|
|
} |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* The inverse of gv_empty() |
|
381
|
|
|
* |
|
382
|
|
|
* @since 2.0 |
|
383
|
|
|
* |
|
384
|
|
|
* @param mixed $value Check whether this is not empty |
|
385
|
|
|
* @param bool $zero_is_empty Should the number zero be treated as an empty value? Default: `false` |
|
386
|
|
|
* @param bool $allow_string_booleans Whether to check if 'yes', 'true' => `true` and 'no', 'false' => `false`. Default: `false` |
|
387
|
|
|
* |
|
388
|
|
|
* @return bool |
|
389
|
|
|
*/ |
|
390
|
|
|
function gv_not_empty( $value, $zero_is_empty = false, $allow_string_booleans = false ) { |
|
391
|
|
|
return ! gv_empty( $value, $zero_is_empty, $allow_string_booleans ); |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
/** |
|
395
|
|
|
* Is the value empty? |
|
396
|
|
|
* |
|
397
|
|
|
* Allows you to pass a function instead of just a variable, like the empty() function insists upon (until PHP 5.5) |
|
398
|
|
|
* |
|
399
|
|
|
* Checks whether `false`, `null`, empty string, empty array, object with no vars defined |
|
400
|
|
|
* |
|
401
|
|
|
* @since 1.15.1 |
|
402
|
|
|
* @param mixed $value Check whether this is empty |
|
403
|
|
|
* @param boolean $zero_is_empty Should the number zero be treated as an empty value? |
|
404
|
|
|
* @param boolean $allow_string_booleans Whether to check if 'yes', 'true' => `true` and 'no', 'false' => `false` |
|
405
|
|
|
* @return boolean True: empty; false: not empty |
|
406
|
|
|
*/ |
|
407
|
|
|
function gv_empty( $value, $zero_is_empty = true, $allow_string_booleans = true ) { |
|
408
|
|
|
|
|
409
|
|
|
/** |
|
410
|
|
|
* Arrays with empty values are empty. |
|
411
|
|
|
* |
|
412
|
|
|
* Consider the a missing product field. |
|
413
|
|
|
*/ |
|
414
|
12 |
|
if ( is_array( $value ) ) { |
|
415
|
1 |
|
$values = array(); |
|
416
|
1 |
|
foreach ( $value as $v ) { |
|
417
|
1 |
|
if ( ! gv_empty( $v, $zero_is_empty, $allow_string_booleans ) ) { |
|
418
|
1 |
|
return false; |
|
419
|
|
|
} |
|
420
|
|
|
} |
|
421
|
1 |
|
return true; |
|
422
|
|
|
} |
|
423
|
|
|
|
|
424
|
|
|
if ( |
|
425
|
12 |
|
! isset( $value ) // If it's not set, it's empty! |
|
|
|
|
|
|
426
|
12 |
|
|| false === $value |
|
427
|
12 |
|
|| null === $value |
|
428
|
12 |
|
|| '' === $value // Empty string |
|
429
|
12 |
|
|| array() === $value // Empty array |
|
430
|
12 |
|
|| ( is_object( $value ) && ! get_object_vars( $value ) ) // Empty object |
|
431
|
|
|
) { |
|
432
|
2 |
|
return true; |
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
12 |
|
if ( is_string( $value ) && $allow_string_booleans ) { |
|
436
|
|
|
|
|
437
|
1 |
|
$value = trim( $value ); |
|
438
|
1 |
|
$value = strtolower( $value ); |
|
439
|
|
|
|
|
440
|
1 |
|
if ( in_array( $value, array( 'yes', 'true' ), true ) ) { |
|
441
|
|
|
$value = true; |
|
442
|
1 |
|
} else if( in_array( $value, array( 'no', 'false' ), true ) ) { |
|
443
|
1 |
|
$value = false; |
|
444
|
|
|
} |
|
445
|
|
|
} |
|
446
|
|
|
|
|
447
|
|
|
// If zero isn't empty, then if $value is a number and it's empty, it's zero. Thus, return false. |
|
448
|
12 |
|
if ( ! $zero_is_empty && is_numeric( $value ) && empty( $value ) ) { |
|
449
|
1 |
|
return false; |
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
12 |
|
return empty( $value ); |
|
453
|
|
|
} |
|
454
|
|
|
|
|
455
|
|
|
/** |
|
456
|
|
|
* If content is JSON, decode it. Otherwise, return the passed value |
|
457
|
|
|
* |
|
458
|
|
|
* @since 2.0 |
|
459
|
|
|
* |
|
460
|
|
|
* @see json_decode() for more information about the function parameters |
|
461
|
|
|
* |
|
462
|
|
|
* @param string $value The string that may be decoded |
|
463
|
|
|
* @param bool $assoc [optional] When `true`, returned objects will be converted into associative arrays |
|
464
|
|
|
* @param int $depth [optional] User specified recursion depth. |
|
465
|
|
|
* @param int $options [optional] Bitmask of JSON decode options |
|
466
|
|
|
* |
|
467
|
|
|
* @return array|mixed|object|string If $value is JSON, returns the response from `json_decode()`. Otherwise, returns original value. |
|
468
|
|
|
*/ |
|
469
|
|
|
function gv_maybe_json_decode( $value, $assoc = false, $depth = 512, $options = 0 ) { |
|
|
|
|
|
|
470
|
|
|
|
|
471
|
|
|
if( ! is_string( $value ) ) { |
|
472
|
|
|
return $value; |
|
473
|
|
|
} |
|
474
|
|
|
|
|
475
|
|
|
$decoded = json_decode( $value ); |
|
476
|
|
|
|
|
477
|
|
|
// There was a JSON error (PHP 5.3+) |
|
478
|
|
|
if( function_exists('json_last_error') && JSON_ERROR_NONE !== json_last_error() ) { |
|
|
|
|
|
|
479
|
|
|
return $value; |
|
480
|
|
|
} |
|
481
|
|
|
|
|
482
|
|
|
// It wasn't JSON (PHP < 5.3 fallback) |
|
483
|
|
|
if( is_null( $decoded ) ) { |
|
484
|
|
|
return $value; |
|
485
|
|
|
} |
|
486
|
|
|
|
|
487
|
|
|
return $decoded; |
|
488
|
|
|
} |
|
489
|
|
|
|
|
490
|
|
|
|
|
491
|
|
|
/** |
|
492
|
|
|
* Maps a function to all non-iterable elements of an array or an object. |
|
493
|
|
|
* |
|
494
|
|
|
* @see map_deep() This is an alias of the WP core function `map_deep()`, added in 4.4. Here for legacy purposes. |
|
495
|
|
|
* @since 1.16.3 |
|
496
|
|
|
* |
|
497
|
|
|
* @param mixed $value The array, object, or scalar. |
|
498
|
|
|
* @param callable $callback The function to map onto $value. |
|
499
|
|
|
* |
|
500
|
|
|
* @return mixed The value with the callback applied to all non-arrays and non-objects inside it. |
|
501
|
|
|
*/ |
|
502
|
|
|
function gv_map_deep( $value, $callback ) { |
|
503
|
|
|
|
|
504
|
|
|
// Use the original function, if exists. |
|
505
|
|
|
// Requires WP 4.4+ |
|
506
|
12 |
|
if( function_exists( 'map_deep') ) { |
|
|
|
|
|
|
507
|
12 |
|
return map_deep( $value, $callback ); |
|
508
|
|
|
} |
|
509
|
|
|
|
|
510
|
|
|
// Exact copy of map_deep() code below: |
|
511
|
|
|
if ( is_array( $value ) ) { |
|
512
|
|
|
foreach ( $value as $index => $item ) { |
|
513
|
|
|
$value[ $index ] = gv_map_deep( $item, $callback ); |
|
514
|
|
|
} |
|
515
|
|
|
} elseif ( is_object( $value ) ) { |
|
516
|
|
|
$object_vars = get_object_vars( $value ); |
|
517
|
|
|
foreach ( $object_vars as $property_name => $property_value ) { |
|
518
|
|
|
$value->$property_name = gv_map_deep( $property_value, $callback ); |
|
519
|
|
|
} |
|
520
|
|
|
} else { |
|
521
|
|
|
$value = call_user_func( $callback, $value ); |
|
522
|
|
|
} |
|
523
|
|
|
|
|
524
|
|
|
return $value; |
|
525
|
|
|
} |
|
526
|
|
|
|
|
527
|
|
|
/** |
|
528
|
|
|
* Check whether a string is a expected date format |
|
529
|
|
|
* |
|
530
|
|
|
* @since 1.15.2 |
|
531
|
|
|
* |
|
532
|
|
|
* @param string $datetime The date to check |
|
533
|
|
|
* @param string $expected_format Check whether the date is formatted as expected. Default: Y-m-d |
|
534
|
|
|
* |
|
535
|
|
|
* @return bool True: it's a valid datetime, formatted as expected. False: it's not a date formatted as expected. |
|
536
|
|
|
*/ |
|
537
|
|
|
function gravityview_is_valid_datetime( $datetime, $expected_format = 'Y-m-d' ) { |
|
538
|
|
|
|
|
539
|
|
|
/** |
|
540
|
|
|
* @var bool|DateTime False if not a valid date, (like a relative date). DateTime if a date was created. |
|
541
|
|
|
*/ |
|
542
|
1 |
|
$formatted_date = DateTime::createFromFormat( $expected_format, $datetime ); |
|
|
|
|
|
|
543
|
|
|
|
|
544
|
|
|
/** |
|
545
|
|
|
* @see http://stackoverflow.com/a/19271434/480856 |
|
546
|
|
|
*/ |
|
547
|
1 |
|
return ( $formatted_date && $formatted_date->format( $expected_format ) === $datetime ); |
|
548
|
|
|
} |
|
549
|
|
|
|
|
550
|
|
|
/** |
|
551
|
|
|
* Very commonly needed: get the # of the input based on a full field ID. |
|
552
|
|
|
* |
|
553
|
|
|
* Example: 12.3 => field #12, input #3. Returns: 3 |
|
554
|
|
|
* Example: 7 => field #7, no input. Returns: 0 |
|
555
|
|
|
* |
|
556
|
|
|
* @since 1.16.4 |
|
557
|
|
|
* |
|
558
|
|
|
* @param string $field_id Full ID of field, with or without input ID, like "12.3" or "7". |
|
559
|
|
|
* |
|
560
|
|
|
* @return int If field ID has an input, returns that input number. Otherwise, returns false. |
|
561
|
|
|
*/ |
|
562
|
|
|
function gravityview_get_input_id_from_id( $field_id = '' ) { |
|
563
|
|
|
|
|
564
|
6 |
|
if ( ! is_numeric( $field_id ) ) { |
|
565
|
1 |
|
gravityview()->log->error( '$field_id not numeric', array( 'data' => $field_id ) ); |
|
566
|
1 |
|
return false; |
|
567
|
|
|
} |
|
568
|
|
|
|
|
569
|
6 |
|
$exploded = explode( '.', "{$field_id}" ); |
|
570
|
|
|
|
|
571
|
6 |
|
return isset( $exploded[1] ) ? intval( $exploded[1] ) : false; |
|
572
|
|
|
} |
|
573
|
|
|
|
|
574
|
|
|
/** |
|
575
|
|
|
* Get categories formatted in a way used by GravityView and Gravity Forms input choices |
|
576
|
|
|
* |
|
577
|
|
|
* @since 1.15.3 |
|
578
|
|
|
* |
|
579
|
|
|
* @see get_terms() |
|
580
|
|
|
* |
|
581
|
|
|
* @param array $args Arguments array as used by the get_terms() function. Filtered using `gravityview_get_terms_choices_args` filter. Defaults: { \n |
|
582
|
|
|
* @type string $taxonomy Used as first argument in get_terms(). Default: "category" |
|
583
|
|
|
* @type string $fields Default: 'id=>name' to only fetch term ID and Name \n |
|
584
|
|
|
* @type int $number Limit the total number of terms to fetch. Default: 1000 \n |
|
585
|
|
|
* } |
|
586
|
|
|
* |
|
587
|
|
|
* @return array Multidimensional array with `text` (Category Name) and `value` (Category ID) keys. |
|
588
|
|
|
*/ |
|
589
|
|
|
function gravityview_get_terms_choices( $args = array() ) { |
|
590
|
|
|
|
|
591
|
|
|
$defaults = array( |
|
592
|
|
|
'type' => 'post', |
|
593
|
|
|
'child_of' => 0, |
|
594
|
|
|
'number' => 1000, // Set a reasonable max limit |
|
595
|
|
|
'orderby' => 'name', |
|
596
|
|
|
'order' => 'ASC', |
|
597
|
|
|
'hide_empty' => 0, |
|
598
|
|
|
'hierarchical' => 1, |
|
599
|
|
|
'taxonomy' => 'category', |
|
600
|
|
|
'fields' => 'id=>name', |
|
601
|
|
|
); |
|
602
|
|
|
|
|
603
|
|
|
$args = wp_parse_args( $args, $defaults ); |
|
604
|
|
|
|
|
605
|
|
|
/** |
|
606
|
|
|
* @filter `gravityview_get_terms_choices_args` Modify the arguments passed to `get_terms()` |
|
607
|
|
|
* @see get_terms() |
|
608
|
|
|
* @since 1.15.3 |
|
609
|
|
|
*/ |
|
610
|
|
|
$args = apply_filters( 'gravityview_get_terms_choices_args', $args ); |
|
611
|
|
|
|
|
612
|
|
|
$terms = get_terms( $args['taxonomy'], $args ); |
|
613
|
|
|
|
|
614
|
|
|
$choices = array(); |
|
615
|
|
|
|
|
616
|
|
|
if ( is_array( $terms ) ) { |
|
617
|
|
|
foreach ( $terms as $term_id => $term_name ) { |
|
618
|
|
|
$choices[] = array( |
|
619
|
|
|
'text' => $term_name, |
|
620
|
|
|
'value' => $term_id |
|
|
|
|
|
|
621
|
|
|
); |
|
622
|
|
|
} |
|
623
|
|
|
} |
|
624
|
|
|
|
|
625
|
|
|
return $choices; |
|
626
|
|
|
} |
|
627
|
|
|
|
|
628
|
|
|
/** |
|
629
|
|
|
* Maybe convert jQuery-serialized fields into array, otherwise return $_POST['fields'] array |
|
630
|
|
|
* |
|
631
|
|
|
* Fields are passed as a jQuery-serialized array, created in admin-views.js in the serializeForm method. |
|
632
|
|
|
* |
|
633
|
|
|
* @since 1.16.5 |
|
634
|
|
|
* |
|
635
|
|
|
* @uses GVCommon::gv_parse_str |
|
636
|
|
|
* |
|
637
|
|
|
* @return array Array of fields |
|
638
|
|
|
*/ |
|
639
|
|
|
function _gravityview_process_posted_fields() { |
|
640
|
|
|
$fields = array(); |
|
641
|
|
|
|
|
642
|
|
|
if( !empty( $_POST['gv_fields'] ) ) { |
|
|
|
|
|
|
643
|
|
|
if ( ! is_array( $_POST['gv_fields'] ) ) { |
|
644
|
|
|
|
|
645
|
|
|
// We are not using parse_str() due to max_input_vars limitation with large View configurations |
|
646
|
|
|
$fields_holder = array(); |
|
647
|
|
|
GVCommon::gv_parse_str( $_POST['gv_fields'], $fields_holder ); |
|
|
|
|
|
|
648
|
|
|
|
|
649
|
|
|
if ( isset( $fields_holder['fields'] ) ) { |
|
650
|
|
|
$fields = $fields_holder['fields']; |
|
651
|
|
|
} else { |
|
652
|
|
|
gravityview()->log->error( 'No `fields` key was found after parsing $fields string', array( 'data' => $fields_holder ) ); |
|
653
|
|
|
} |
|
654
|
|
|
|
|
655
|
|
|
} else { |
|
656
|
|
|
$fields = $_POST['gv_fields']; |
|
|
|
|
|
|
657
|
|
|
} |
|
658
|
|
|
} |
|
659
|
|
|
|
|
660
|
|
|
return $fields; |
|
661
|
|
|
} |
|
662
|
|
|
|
|
663
|
|
|
/** |
|
664
|
|
|
* Sanitizes Excel formulas inside CSV output |
|
665
|
|
|
* |
|
666
|
|
|
* @internal |
|
667
|
|
|
* |
|
668
|
|
|
* @since 2.1 |
|
669
|
|
|
* |
|
670
|
|
|
* @param $value |
|
671
|
|
|
* |
|
672
|
|
|
* @return string |
|
673
|
|
|
*/ |
|
674
|
|
|
function gravityview_strip_excel_formulas( $value ) { |
|
675
|
|
|
|
|
676
|
2 |
|
if ( strpos( $value, '=' ) === 0 ) { |
|
|
|
|
|
|
677
|
1 |
|
$value = "'" . $value; |
|
678
|
|
|
} |
|
679
|
|
|
|
|
680
|
|
|
return $value; |
|
681
|
|
|
} |