Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FrmAppHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FrmAppHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class FrmAppHelper { |
||
7 | public static $db_version = 32; //version of the database we are moving to |
||
8 | public static $pro_db_version = 37; |
||
9 | |||
10 | /** |
||
11 | * @since 2.0 |
||
12 | */ |
||
13 | public static $plug_version = '2.02.06'; |
||
14 | |||
15 | /** |
||
16 | * @since 1.07.02 |
||
17 | * |
||
18 | * @param none |
||
19 | * @return string The version of this plugin |
||
20 | */ |
||
21 | public static function plugin_version() { |
||
24 | |||
25 | public static function plugin_folder() { |
||
28 | |||
29 | public static function plugin_path() { |
||
32 | |||
33 | public static function plugin_url() { |
||
37 | |||
38 | public static function relative_plugin_url() { |
||
41 | |||
42 | /** |
||
43 | * @return string Site URL |
||
44 | */ |
||
45 | public static function site_url() { |
||
48 | |||
49 | /** |
||
50 | * Get the name of this site |
||
51 | * Used for [sitename] shortcode |
||
52 | * |
||
53 | * @since 2.0 |
||
54 | * @return string |
||
55 | */ |
||
56 | public static function site_name() { |
||
59 | |||
60 | public static function make_affiliate_url( $url ) { |
||
67 | |||
68 | public static function get_affiliate() { |
||
69 | return ''; |
||
70 | $affiliate_id = apply_filters( 'frm_affiliate_link', get_option('frm_aff') ); |
||
|
|||
71 | $affiliate_id = strtolower( $affiliate_id ); |
||
72 | $allowed_affiliates = array(); |
||
73 | if ( ! in_array( $affiliate_id, $allowed_affiliates ) ) { |
||
74 | $affiliate_id = false; |
||
75 | } |
||
76 | return $affiliate_id; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Get the Formidable settings |
||
81 | * |
||
82 | * @since 2.0 |
||
83 | * |
||
84 | * @param None |
||
85 | * @return FrmSettings $frm_setings |
||
86 | */ |
||
87 | public static function get_settings() { |
||
88 | global $frm_settings; |
||
89 | if ( empty($frm_settings) ) { |
||
90 | $frm_settings = new FrmSettings(); |
||
91 | } |
||
92 | return $frm_settings; |
||
93 | } |
||
94 | |||
95 | public static function get_menu_name() { |
||
96 | $frm_settings = FrmAppHelper::get_settings(); |
||
97 | return $frm_settings->menu; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Show a message in place of pro features |
||
102 | * |
||
103 | * @since 2.0 |
||
104 | */ |
||
105 | public static function update_message() { |
||
106 | _deprecated_function( __FUNCTION__, '2.0.19' ); |
||
107 | } |
||
108 | |||
109 | public static function pro_is_installed() { |
||
110 | return apply_filters('frm_pro_installed', false); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Check for certain page in Formidable settings |
||
115 | * |
||
116 | * @since 2.0 |
||
117 | * |
||
118 | * @param string $page The name of the page to check |
||
119 | * @return boolean |
||
120 | */ |
||
121 | public static function is_admin_page( $page = 'formidable' ) { |
||
122 | global $pagenow; |
||
123 | $get_page = self::simple_get( 'page', 'sanitize_title' ); |
||
124 | if ( $pagenow ) { |
||
125 | return $pagenow == 'admin.php' && $get_page == $page; |
||
126 | } |
||
127 | |||
128 | return is_admin() && $get_page == $page; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Check for the form preview page |
||
133 | * |
||
134 | * @since 2.0 |
||
135 | * |
||
136 | * @param None |
||
137 | * @return boolean |
||
138 | */ |
||
139 | public static function is_preview_page() { |
||
140 | global $pagenow; |
||
141 | $action = FrmAppHelper::simple_get( 'action', 'sanitize_title' ); |
||
142 | return $pagenow && $pagenow == 'admin-ajax.php' && $action == 'frm_forms_preview'; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Check for ajax except the form preview page |
||
147 | * |
||
148 | * @since 2.0 |
||
149 | * |
||
150 | * @param None |
||
151 | * @return boolean |
||
152 | */ |
||
153 | public static function doing_ajax() { |
||
154 | return defined('DOING_AJAX') && DOING_AJAX && ! self::is_preview_page(); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @since 2.0.8 |
||
159 | */ |
||
160 | public static function prevent_caching() { |
||
161 | global $frm_vars; |
||
162 | return isset( $frm_vars['prevent_caching'] ) && $frm_vars['prevent_caching']; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Check if on an admin page |
||
167 | * |
||
168 | * @since 2.0 |
||
169 | * |
||
170 | * @param None |
||
171 | * @return boolean |
||
172 | */ |
||
173 | public static function is_admin() { |
||
174 | return is_admin() && ( ! defined('DOING_AJAX') || ! DOING_AJAX ); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Check if value contains blank value or empty array |
||
179 | * |
||
180 | * @since 2.0 |
||
181 | * @param mixed $value - value to check |
||
182 | * @param string |
||
183 | * @return boolean |
||
184 | */ |
||
185 | public static function is_empty_value( $value, $empty = '' ) { |
||
186 | return ( is_array( $value ) && empty( $value ) ) || $value == $empty; |
||
187 | } |
||
188 | |||
189 | public static function is_not_empty_value( $value, $empty = '' ) { |
||
190 | return ! self::is_empty_value( $value, $empty ); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Get any value from the $_SERVER |
||
195 | * |
||
196 | * @since 2.0 |
||
197 | * @param string $value |
||
198 | * @return string |
||
199 | */ |
||
200 | public static function get_server_value( $value ) { |
||
201 | return isset( $_SERVER[ $value ] ) ? wp_strip_all_tags( $_SERVER[ $value ] ) : ''; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Check for the IP address in several places |
||
206 | * Used by [ip] shortcode |
||
207 | * |
||
208 | * @return string The IP address of the current user |
||
209 | */ |
||
210 | public static function get_ip_address() { |
||
211 | $ip = ''; |
||
212 | foreach ( array( |
||
213 | 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', |
||
214 | 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR', |
||
215 | ) as $key ) { |
||
216 | if ( ! isset( $_SERVER[ $key ] ) ) { |
||
217 | continue; |
||
218 | } |
||
219 | |||
220 | foreach ( explode( ',', $_SERVER[ $key ] ) as $ip ) { |
||
221 | $ip = trim($ip); // just to be safe |
||
222 | |||
223 | if ( filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false ) { |
||
224 | return $ip; |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | |||
229 | return sanitize_text_field( $ip ); |
||
230 | } |
||
231 | |||
232 | public static function get_param( $param, $default = '', $src = 'get', $sanitize = '' ) { |
||
233 | if ( strpos($param, '[') ) { |
||
234 | $params = explode('[', $param); |
||
235 | $param = $params[0]; |
||
236 | } |
||
237 | |||
238 | if ( $src == 'get' ) { |
||
239 | $value = isset( $_POST[ $param ] ) ? stripslashes_deep( $_POST[ $param ] ) : ( isset( $_GET[ $param ] ) ? stripslashes_deep( $_GET[ $param ] ) : $default ); |
||
240 | if ( ! isset( $_POST[ $param ] ) && isset( $_GET[ $param ] ) && ! is_array( $value ) ) { |
||
241 | $value = stripslashes_deep( htmlspecialchars_decode( urldecode( $_GET[ $param ] ) ) ); |
||
242 | } |
||
243 | self::sanitize_value( $sanitize, $value ); |
||
244 | } else { |
||
245 | $value = self::get_simple_request( array( 'type' => $src, 'param' => $param, 'default' => $default, 'sanitize' => $sanitize ) ); |
||
246 | } |
||
247 | |||
248 | if ( isset( $params ) && is_array( $value ) && ! empty( $value ) ) { |
||
249 | foreach ( $params as $k => $p ) { |
||
250 | if ( ! $k || ! is_array($value) ) { |
||
251 | continue; |
||
252 | } |
||
253 | |||
254 | $p = trim($p, ']'); |
||
255 | $value = isset( $value[ $p ] ) ? $value[ $p ] : $default; |
||
256 | } |
||
257 | } |
||
258 | |||
259 | return $value; |
||
260 | } |
||
261 | |||
262 | public static function get_post_param( $param, $default = '', $sanitize = '' ) { |
||
263 | return self::get_simple_request( array( 'type' => 'post', 'param' => $param, 'default' => $default, 'sanitize' => $sanitize ) ); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * @since 2.0 |
||
268 | * |
||
269 | * @param string $param |
||
270 | * @param string $sanitize |
||
271 | * @param string $default |
||
272 | * @return string|array |
||
273 | */ |
||
274 | public static function simple_get( $param, $sanitize = 'sanitize_text_field', $default = '' ) { |
||
275 | return self::get_simple_request( array( 'type' => 'get', 'param' => $param, 'default' => $default, 'sanitize' => $sanitize ) ); |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Get a GET/POST/REQUEST value and sanitize it |
||
280 | * |
||
281 | * @since 2.0.6 |
||
282 | * @param array $args |
||
283 | * @return string|array |
||
284 | */ |
||
285 | public static function get_simple_request( $args ) { |
||
286 | $defaults = array( |
||
287 | 'param' => '', 'default' => '', |
||
288 | 'type' => 'get', 'sanitize' => 'sanitize_text_field', |
||
289 | ); |
||
290 | $args = wp_parse_args( $args, $defaults ); |
||
291 | |||
292 | $value = $args['default']; |
||
293 | if ( $args['type'] == 'get' ) { |
||
294 | if ( $_GET && isset( $_GET[ $args['param'] ] ) ) { |
||
295 | $value = $_GET[ $args['param'] ]; |
||
296 | } |
||
297 | } else if ( $args['type'] == 'post' ) { |
||
298 | if ( isset( $_POST[ $args['param'] ] ) ) { |
||
299 | $value = stripslashes_deep( maybe_unserialize( $_POST[ $args['param'] ] ) ); |
||
300 | } |
||
301 | } else { |
||
302 | if ( isset( $_REQUEST[ $args['param'] ] ) ) { |
||
303 | $value = $_REQUEST[ $args['param'] ]; |
||
304 | } |
||
305 | } |
||
306 | |||
307 | self::sanitize_value( $args['sanitize'], $value ); |
||
308 | return $value; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Preserve backslashes in a value, but make sure value doesn't get compounding slashes |
||
313 | * |
||
314 | * @since 2.0.8 |
||
315 | * @param string $value |
||
316 | * @return string $value |
||
317 | */ |
||
318 | public static function preserve_backslashes( $value ) { |
||
319 | // If backslashes have already been added, don't add them again |
||
320 | if ( strpos( $value, '\\\\' ) === false ) { |
||
321 | $value = addslashes( $value ); |
||
322 | } |
||
323 | return $value; |
||
324 | } |
||
325 | |||
326 | public static function sanitize_value( $sanitize, &$value ) { |
||
327 | if ( ! empty( $sanitize ) ) { |
||
328 | if ( is_array( $value ) ) { |
||
329 | $temp_values = $value; |
||
330 | foreach ( $temp_values as $k => $v ) { |
||
331 | FrmAppHelper::sanitize_value( $sanitize, $value[ $k ] ); |
||
332 | } |
||
333 | } else { |
||
334 | $value = call_user_func( $sanitize, $value ); |
||
335 | } |
||
336 | } |
||
337 | } |
||
338 | |||
339 | public static function sanitize_request( $sanitize_method, &$values ) { |
||
340 | $temp_values = $values; |
||
341 | foreach ( $temp_values as $k => $val ) { |
||
342 | if ( isset( $sanitize_method[ $k ] ) ) { |
||
343 | $values[ $k ] = call_user_func( $sanitize_method[ $k ], $val ); |
||
344 | } |
||
345 | } |
||
346 | } |
||
347 | |||
348 | public static function sanitize_array( &$values ) { |
||
349 | $temp_values = $values; |
||
350 | foreach ( $temp_values as $k => $val ) { |
||
351 | $values[ $k ] = wp_kses_post( $val ); |
||
352 | } |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Sanitize the value, and allow some HTML |
||
357 | * @since 2.0 |
||
358 | * @param string $value |
||
359 | * @param array $allowed |
||
360 | * @return string |
||
361 | */ |
||
362 | public static function kses( $value, $allowed = array() ) { |
||
363 | $html = array( |
||
364 | 'a' => array( |
||
365 | 'href' => array(), |
||
366 | 'title' => array(), |
||
367 | 'id' => array(), |
||
368 | 'class' => array(), |
||
369 | ), |
||
370 | ); |
||
371 | |||
372 | $allowed_html = array(); |
||
373 | foreach ( $allowed as $a ) { |
||
374 | $allowed_html[ $a ] = isset( $html[ $a ] ) ? $html[ $a ] : array(); |
||
375 | } |
||
376 | |||
377 | return wp_kses( $value, $allowed_html ); |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * Used when switching the action for a bulk action |
||
382 | * @since 2.0 |
||
383 | */ |
||
384 | public static function remove_get_action() { |
||
385 | if ( ! isset($_GET) ) { |
||
386 | return; |
||
387 | } |
||
388 | |||
389 | $new_action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : ( isset( $_GET['action2'] ) ? sanitize_text_field( $_GET['action2'] ) : '' ); |
||
390 | if ( ! empty( $new_action ) ) { |
||
391 | $_SERVER['REQUEST_URI'] = str_replace( '&action=' . $new_action, '', FrmAppHelper::get_server_value( 'REQUEST_URI' ) ); |
||
392 | } |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Check the WP query for a parameter |
||
397 | * |
||
398 | * @since 2.0 |
||
399 | * @return string|array |
||
400 | */ |
||
401 | public static function get_query_var( $value, $param ) { |
||
402 | if ( $value != '' ) { |
||
403 | return $value; |
||
404 | } |
||
405 | |||
406 | global $wp_query; |
||
407 | if ( isset( $wp_query->query_vars[ $param ] ) ) { |
||
408 | $value = $wp_query->query_vars[ $param ]; |
||
409 | } |
||
410 | |||
411 | return $value; |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * @param string $type |
||
416 | */ |
||
417 | public static function trigger_hook_load( $type, $object = null ) { |
||
418 | // only load the form hooks once |
||
419 | $hooks_loaded = apply_filters( 'frm_' . $type . '_hooks_loaded', false, $object ); |
||
420 | if ( ! $hooks_loaded ) { |
||
421 | do_action( 'frm_load_' . $type . '_hooks' ); |
||
422 | } |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * Check cache before fetching values and saving to cache |
||
427 | * |
||
428 | * @since 2.0 |
||
429 | * |
||
430 | * @param string $cache_key The unique name for this cache |
||
431 | * @param string $group The name of the cache group |
||
432 | * @param string $query If blank, don't run a db call |
||
433 | * @param string $type The wpdb function to use with this query |
||
434 | * @return mixed $results The cache or query results |
||
435 | */ |
||
436 | public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) { |
||
437 | $results = wp_cache_get($cache_key, $group); |
||
438 | if ( ! self::is_empty_value( $results, false ) || empty($query) ) { |
||
439 | return $results; |
||
440 | } |
||
441 | |||
442 | if ( 'get_posts' == $type ) { |
||
443 | $results = get_posts($query); |
||
444 | } else if ( 'get_associative_results' == $type ) { |
||
445 | global $wpdb; |
||
446 | $results = $wpdb->get_results( $query, OBJECT_K ); |
||
447 | } else { |
||
448 | global $wpdb; |
||
449 | $results = $wpdb->{$type}($query); |
||
450 | } |
||
451 | |||
452 | if ( ! self::prevent_caching() ) { |
||
453 | wp_cache_set( $cache_key, $results, $group, $time ); |
||
454 | } |
||
455 | |||
456 | return $results; |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * Data that should be stored for a long time can be stored in a transient. |
||
461 | * First check the cache, then check the transient |
||
462 | * @since 2.0 |
||
463 | * @return mixed The cached value or false |
||
464 | */ |
||
465 | public static function check_cache_and_transient( $cache_key ) { |
||
466 | // check caching layer first |
||
467 | $results = self::check_cache( $cache_key ); |
||
468 | if ( $results ) { |
||
469 | return $results; |
||
470 | } |
||
471 | |||
472 | // then check the transient |
||
473 | $results = get_transient($cache_key); |
||
474 | if ( $results ) { |
||
475 | wp_cache_set($cache_key, $results); |
||
476 | } |
||
477 | |||
478 | return $results; |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * @since 2.0 |
||
483 | * @param string $cache_key |
||
484 | */ |
||
485 | public static function delete_cache_and_transient( $cache_key, $group = 'default' ) { |
||
486 | delete_transient($cache_key); |
||
487 | wp_cache_delete( $cache_key, $group ); |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * Delete all caching in a single group |
||
492 | * |
||
493 | * @since 2.0 |
||
494 | * |
||
495 | * @param string $group The name of the cache group |
||
496 | * @return boolean True or False |
||
497 | */ |
||
498 | public static function cache_delete_group( $group ) { |
||
499 | global $wp_object_cache; |
||
500 | |||
501 | if ( is_callable( array( $wp_object_cache, '__get' ) ) ) { |
||
502 | $group_cache = $wp_object_cache->__get('cache'); |
||
503 | |||
504 | if ( isset( $group_cache[ $group ] ) ) { |
||
505 | foreach ( $group_cache[ $group ] as $k => $v ) { |
||
506 | wp_cache_delete( $k, $group ); |
||
507 | } |
||
508 | return true; |
||
509 | } |
||
510 | } else { |
||
511 | $wp_object_cache->flush(); |
||
512 | |||
513 | // When using memcached: if ( property_exists( $wp_object_cache, 'mc' ) ) |
||
514 | // When using redis: if ( is_callable( array( $wp_object_cache, 'redis_status' ) ) && $wp_object_cache->redis_status() ) |
||
515 | } |
||
516 | |||
517 | return false; |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * Check a value from a shortcode to see if true or false. |
||
522 | * True when value is 1, true, 'true', 'yes' |
||
523 | * |
||
524 | * @since 1.07.10 |
||
525 | * |
||
526 | * @param string $value The value to compare |
||
527 | * @return boolean True or False |
||
528 | */ |
||
529 | public static function is_true( $value ) { |
||
532 | |||
533 | /** |
||
534 | * Used to filter shortcode in text widgets |
||
535 | */ |
||
536 | public static function widget_text_filter_callback( $matches ) { |
||
539 | |||
540 | public static function get_pages() { |
||
541 | return get_posts( array( 'post_type' => 'page', 'post_status' => array( 'publish', 'private' ), 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC' ) ); |
||
542 | } |
||
543 | |||
544 | public static function wp_pages_dropdown( $field_name, $page_id, $truncate = false ) { |
||
545 | $pages = self::get_pages(); |
||
546 | $selected = self::get_post_param( $field_name, $page_id, 'absint' ); |
||
547 | ?> |
||
548 | <select name="<?php echo esc_attr($field_name); ?>" id="<?php echo esc_attr($field_name); ?>" class="frm-pages-dropdown"> |
||
549 | <option value=""> </option> |
||
550 | <?php foreach ( $pages as $page ) { ?> |
||
551 | <option value="<?php echo esc_attr($page->ID); ?>" <?php selected( $selected, $page->ID ) ?>> |
||
552 | <?php echo esc_html( $truncate ? self::truncate( $page->post_title, $truncate ) : $page->post_title ); ?> |
||
553 | </option> |
||
554 | <?php } ?> |
||
555 | </select> |
||
556 | <?php |
||
557 | } |
||
558 | |||
559 | public static function post_edit_link( $post_id ) { |
||
567 | |||
568 | public static function wp_roles_dropdown( $field_name, $capability, $multiple = 'single' ) { |
||
577 | |||
578 | public static function roles_options( $capability ) { |
||
594 | |||
595 | public static function frm_capabilities( $type = 'auto' ) { |
||
616 | |||
617 | public static function user_has_permission( $needed_role ) { |
||
638 | |||
639 | /** |
||
640 | * Make sure administrators can see Formidable menu |
||
641 | * |
||
642 | * @since 2.0 |
||
643 | */ |
||
644 | public static function maybe_add_permissions() { |
||
659 | |||
660 | /** |
||
661 | * Make sure admins have permission to see the menu items |
||
662 | * @since 2.0.6 |
||
663 | */ |
||
664 | public static function force_capability( $cap = 'frm_change_settings' ) { |
||
673 | |||
674 | /** |
||
675 | * Check if the user has permision for action. |
||
676 | * Return permission message and stop the action if no permission |
||
677 | * @since 2.0 |
||
678 | * @param string $permission |
||
679 | */ |
||
680 | public static function permission_check( $permission, $show_message = 'show' ) { |
||
689 | |||
690 | /** |
||
691 | * Check user permission and nonce |
||
692 | * @since 2.0 |
||
693 | * @param string $permission |
||
694 | * @return false|string The permission message or false if allowed |
||
695 | */ |
||
696 | public static function permission_nonce_error( $permission, $nonce_name = '', $nonce = '' ) { |
||
714 | |||
715 | public static function checked( $values, $current ) { |
||
720 | |||
721 | public static function check_selected( $values, $current ) { |
||
728 | |||
729 | /** |
||
730 | * Check if current field option is an "other" option |
||
731 | * |
||
732 | * @since 2.0 |
||
733 | * |
||
734 | * @param string $opt_key |
||
735 | * @return boolean Returns true if current field option is an "Other" option |
||
736 | */ |
||
737 | public static function is_other_opt( $opt_key ) { |
||
741 | |||
742 | /** |
||
743 | * Get value that belongs in "Other" text box |
||
744 | * |
||
745 | * @since 2.0 |
||
746 | * |
||
747 | * @param string $opt_key |
||
748 | * @param array $field |
||
749 | * @return string $other_val |
||
750 | */ |
||
751 | public static function get_other_val( $opt_key, $field, $parent = false, $pointer = false ) { |
||
755 | |||
756 | /** |
||
757 | * Check if there is a saved value for the "Other" text field. If so, set it as the $other_val. |
||
758 | * Intended for front-end use |
||
759 | * |
||
760 | * @since 2.0 |
||
761 | * |
||
762 | * @param array $field |
||
763 | * @param boolean $other_opt |
||
764 | * @param string $checked |
||
765 | * @param array $args should include opt_key and field name |
||
766 | * @return string $other_val |
||
767 | */ |
||
768 | public static function prepare_other_input( $field, &$other_opt, &$checked, $args = array() ) { |
||
773 | |||
774 | public static function recursive_function_map( $value, $function ) { |
||
797 | |||
798 | public static function is_assoc( $array ) { |
||
801 | |||
802 | /** |
||
803 | * Flatten a multi-dimensional array |
||
804 | */ |
||
805 | public static function array_flatten( $array, $keys = 'keep' ) { |
||
820 | |||
821 | public static function esc_textarea( $text, $is_rich_text = false ) { |
||
822 | $safe_text = str_replace( '"', '"', $text ); |
||
823 | if ( ! $is_rich_text ) { |
||
824 | $safe_text = htmlspecialchars( $safe_text, ENT_NOQUOTES ); |
||
825 | } |
||
826 | $safe_text = str_replace( '&', '&', $safe_text ); |
||
827 | return apply_filters( 'esc_textarea', $safe_text, $text ); |
||
828 | } |
||
829 | |||
830 | /** |
||
831 | * Add auto paragraphs to text areas |
||
832 | * @since 2.0 |
||
833 | */ |
||
834 | public static function use_wpautop( $content ) { |
||
840 | |||
841 | public static function replace_quotes( $val ) { |
||
848 | |||
849 | /** |
||
850 | * @since 2.0 |
||
851 | * @return string The base Google APIS url for the current version of jQuery UI |
||
852 | */ |
||
853 | public static function jquery_ui_base_url() { |
||
858 | |||
859 | /** |
||
860 | * @param string $handle |
||
861 | */ |
||
862 | public static function script_version( $handle ) { |
||
881 | |||
882 | public static function js_redirect( $url ) { |
||
885 | |||
886 | public static function get_user_id_param( $user_id ) { |
||
908 | |||
909 | public static function get_file_contents( $filename, $atts = array() ) { |
||
921 | |||
922 | /** |
||
923 | * @param string $table_name |
||
924 | * @param string $column |
||
925 | * @param int $id |
||
926 | * @param int $num_chars |
||
927 | */ |
||
928 | public static function get_unique_key( $name = '', $table_name, $column, $id = 0, $num_chars = 5 ) { |
||
958 | |||
959 | /** |
||
960 | * Editing a Form or Entry |
||
961 | * @param string $table |
||
962 | * @return bool|array |
||
963 | */ |
||
964 | public static function setup_edit_vars( $record, $table, $fields = '', $default = false, $post_values = array(), $args = array() ) { |
||
1004 | |||
1005 | private static function fill_field_defaults( $field, $record, array &$values, $args ) { |
||
1054 | |||
1055 | private static function fill_field_opts( $field, array &$field_array, $args ) { |
||
1076 | |||
1077 | /** |
||
1078 | * @param string $table |
||
1079 | */ |
||
1080 | private static function fill_form_opts( $record, $table, $post_values, array &$values ) { |
||
1105 | |||
1106 | /** |
||
1107 | * Set to POST value or default |
||
1108 | */ |
||
1109 | private static function fill_form_defaults( $post_values, array &$values ) { |
||
1132 | |||
1133 | public static function get_meta_value( $field_id, $entry ) { |
||
1137 | |||
1138 | public static function insert_opt_html( $args ) { |
||
1151 | |||
1152 | public static function truncate( $str, $length, $minword = 3, $continue = '...' ) { |
||
1153 | if ( is_array( $str ) ) { |
||
1154 | return ''; |
||
1155 | } |
||
1156 | |||
1157 | $length = (int) $length; |
||
1158 | $str = wp_strip_all_tags( $str ); |
||
1159 | $original_len = self::mb_function( array( 'mb_strlen', 'strlen' ), array( $str ) ); |
||
1160 | |||
1161 | if ( $length == 0 ) { |
||
1162 | return ''; |
||
1163 | } else if ( $length <= 10 ) { |
||
1164 | $sub = self::mb_function( array( 'mb_substr', 'substr' ), array( $str, 0, $length ) ); |
||
1165 | return $sub . (($length < $original_len) ? $continue : ''); |
||
1166 | } |
||
1167 | |||
1168 | $sub = ''; |
||
1169 | $len = 0; |
||
1170 | |||
1171 | $words = self::mb_function( array( 'mb_split', 'explode' ), array( ' ', $str ) ); |
||
1172 | |||
1173 | foreach ( $words as $word ) { |
||
1174 | $part = (($sub != '') ? ' ' : '') . $word; |
||
1175 | $total_len = self::mb_function( array( 'mb_strlen', 'strlen' ), array( $sub . $part ) ); |
||
1176 | if ( $total_len > $length && str_word_count($sub) ) { |
||
1177 | break; |
||
1178 | } |
||
1179 | |||
1180 | $sub .= $part; |
||
1181 | $len += self::mb_function( array( 'mb_strlen', 'strlen' ), array( $part ) ); |
||
1182 | |||
1183 | if ( str_word_count($sub) > $minword && $total_len >= $length ) { |
||
1184 | break; |
||
1185 | } |
||
1186 | |||
1187 | unset($total_len, $word); |
||
1188 | } |
||
1189 | |||
1190 | return $sub . (($len < $original_len) ? $continue : ''); |
||
1191 | } |
||
1192 | |||
1193 | public static function mb_function( $function_names, $args ) { |
||
1201 | |||
1202 | public static function get_formatted_time( $date, $date_format = '', $time_format = '' ) { |
||
1225 | |||
1226 | private static function add_time_to_date( $time_format, $date ) { |
||
1239 | |||
1240 | /** |
||
1241 | * @since 2.0.8 |
||
1242 | */ |
||
1243 | public static function get_localized_date( $date_format, $date ) { |
||
1247 | |||
1248 | /** |
||
1249 | * Gets the time ago in words |
||
1250 | * |
||
1251 | * @param int $from in seconds |
||
1252 | * @param int|string $to in seconds |
||
1253 | * @return string $time_ago |
||
1254 | */ |
||
1255 | public static function human_time_diff( $from, $to = '' ) { |
||
1286 | |||
1287 | /** |
||
1288 | * Get the translatable time strings |
||
1289 | * |
||
1290 | * @since 2.0.20 |
||
1291 | * @return array |
||
1292 | */ |
||
1293 | private static function get_time_strings() { |
||
1304 | |||
1305 | /** |
||
1306 | * Added for < WP 4.0 compatability |
||
1307 | * |
||
1308 | * @since 1.07.10 |
||
1309 | * |
||
1310 | * @param string $term The value to escape |
||
1311 | * @return string The escaped value |
||
1312 | */ |
||
1313 | public static function esc_like( $term ) { |
||
1324 | |||
1325 | /** |
||
1326 | * @param string $order_query |
||
1327 | */ |
||
1328 | public static function esc_order( $order_query ) { |
||
1360 | |||
1361 | /** |
||
1362 | * Make sure this is ordering by either ASC or DESC |
||
1363 | */ |
||
1364 | public static function esc_order_by( &$order_by ) { |
||
1370 | |||
1371 | /** |
||
1372 | * @param string $limit |
||
1373 | */ |
||
1374 | public static function esc_limit( $limit ) { |
||
1394 | |||
1395 | /** |
||
1396 | * Get an array of values ready to go through $wpdb->prepare |
||
1397 | * @since 2.0 |
||
1398 | */ |
||
1399 | public static function prepare_array_values( $array, $type = '%s' ) { |
||
1403 | |||
1404 | public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) { |
||
1405 | if ( empty($where) ) { |
||
1406 | return ''; |
||
1407 | } |
||
1408 | |||
1409 | if ( is_array( $where ) ) { |
||
1410 | global $wpdb; |
||
1411 | FrmDb::get_where_clause_and_values( $where, $starts_with ); |
||
1412 | $where = $wpdb->prepare( $where['where'], $where['values'] ); |
||
1413 | } else { |
||
1414 | $where = $starts_with . $where; |
||
1415 | } |
||
1416 | |||
1417 | return $where; |
||
1418 | } |
||
1419 | |||
1420 | // Pagination Methods |
||
1421 | |||
1422 | /** |
||
1423 | * @param integer $current_p |
||
1424 | */ |
||
1425 | public static function get_last_record_num( $r_count, $current_p, $p_size ) { |
||
1428 | |||
1429 | /** |
||
1430 | * @param integer $current_p |
||
1431 | */ |
||
1432 | public static function get_first_record_num( $r_count, $current_p, $p_size ) { |
||
1439 | |||
1440 | /** |
||
1441 | * @return array |
||
1442 | */ |
||
1443 | public static function json_to_array( $json_vars ) { |
||
1444 | $vars = array(); |
||
1445 | foreach ( $json_vars as $jv ) { |
||
1446 | $jv_name = explode('[', $jv['name']); |
||
1447 | $last = count($jv_name) - 1; |
||
1448 | foreach ( $jv_name as $p => $n ) { |
||
1449 | $name = trim($n, ']'); |
||
1450 | if ( ! isset($l1) ) { |
||
1451 | $l1 = $name; |
||
1452 | } |
||
1453 | |||
1454 | if ( ! isset($l2) ) { |
||
1455 | $l2 = $name; |
||
1456 | } |
||
1457 | |||
1458 | if ( ! isset($l3) ) { |
||
1459 | $l3 = $name; |
||
1460 | } |
||
1461 | |||
1462 | $this_val = ( $p == $last ) ? $jv['value'] : array(); |
||
1463 | |||
1464 | switch ( $p ) { |
||
1465 | case 0: |
||
1466 | $l1 = $name; |
||
1467 | self::add_value_to_array( $name, $l1, $this_val, $vars ); |
||
1468 | break; |
||
1469 | |||
1470 | case 1: |
||
1471 | $l2 = $name; |
||
1472 | self::add_value_to_array( $name, $l2, $this_val, $vars[ $l1 ] ); |
||
1473 | break; |
||
1474 | |||
1475 | View Code Duplication | case 2: |
|
1476 | $l3 = $name; |
||
1477 | self::add_value_to_array( $name, $l3, $this_val, $vars[ $l1 ][ $l2 ] ); |
||
1478 | break; |
||
1479 | |||
1480 | View Code Duplication | case 3: |
|
1481 | $l4 = $name; |
||
1482 | self::add_value_to_array( $name, $l4, $this_val, $vars[ $l1 ][ $l2 ][ $l3 ] ); |
||
1483 | break; |
||
1484 | } |
||
1485 | |||
1486 | unset($this_val, $n); |
||
1487 | } |
||
1488 | |||
1489 | unset($last, $jv); |
||
1490 | } |
||
1491 | |||
1492 | return $vars; |
||
1493 | } |
||
1494 | |||
1495 | /** |
||
1496 | * @param string $name |
||
1497 | * @param string $l1 |
||
1498 | */ |
||
1499 | public static function add_value_to_array( $name, $l1, $val, &$vars ) { |
||
1506 | |||
1507 | public static function maybe_add_tooltip( $name, $class = 'closed', $form_name = '' ) { |
||
1534 | |||
1535 | /** |
||
1536 | * Add the current_page class to that page in the form nav |
||
1537 | */ |
||
1538 | public static function select_current_page( $page, $current_page, $action = array() ) { |
||
1548 | |||
1549 | /** |
||
1550 | * Prepare and json_encode post content |
||
1551 | * |
||
1552 | * @since 2.0 |
||
1553 | * |
||
1554 | * @param array $post_content |
||
1555 | * @return string $post_content ( json encoded array ) |
||
1556 | */ |
||
1557 | public static function prepare_and_encode( $post_content ) { |
||
1578 | |||
1579 | private static function prepare_action_slashes( $val, $key, &$post_content ) { |
||
1597 | |||
1598 | /** |
||
1599 | * Prepare and save settings in styles and actions |
||
1600 | * |
||
1601 | * @param array $settings |
||
1602 | * @param string $group |
||
1603 | * |
||
1604 | * @since 2.0.6 |
||
1605 | */ |
||
1606 | public static function save_settings( $settings, $group ) { |
||
1619 | |||
1620 | /** |
||
1621 | * Since actions are JSON encoded, we don't want any filters messing with it. |
||
1622 | * Remove the filters and then add them back in case any posts or views are |
||
1623 | * also being imported. |
||
1624 | * |
||
1625 | * Used when saving form actions and styles |
||
1626 | * |
||
1627 | * @since 2.0.4 |
||
1628 | */ |
||
1629 | public static function save_json_post( $settings ) { |
||
1643 | |||
1644 | public static function maybe_json_decode( $string ) { |
||
1661 | |||
1662 | /** |
||
1663 | * @since 1.07.10 |
||
1664 | * |
||
1665 | * @param string $post_type The name of the post type that may need to be highlighted |
||
1666 | * echo The javascript to open and highlight the Formidable menu |
||
1667 | */ |
||
1668 | public static function maybe_highlight_menu( $post_type ) { |
||
1682 | |||
1683 | /** |
||
1684 | * Load the JS file on non-Formidable pages in the admin area |
||
1685 | * @since 2.0 |
||
1686 | */ |
||
1687 | public static function load_admin_wide_js( $load = true ) { |
||
1703 | |||
1704 | /** |
||
1705 | * @since 2.0.9 |
||
1706 | */ |
||
1707 | public static function load_font_style() { |
||
1710 | |||
1711 | /** |
||
1712 | * @param string $location |
||
1713 | */ |
||
1714 | public static function localize_script( $location ) { |
||
1715 | $ajax_url = admin_url( 'admin-ajax.php', is_ssl() ? 'admin' : 'http' ); |
||
1716 | $ajax_url = apply_filters( 'frm_ajax_url', $ajax_url ); |
||
1717 | |||
1718 | wp_localize_script( 'formidable', 'frm_js', array( |
||
1719 | 'ajax_url' => $ajax_url, |
||
1720 | 'images_url' => self::plugin_url() . '/images', |
||
1721 | 'loading' => __( 'Loading…' ), |
||
1722 | 'remove' => __( 'Remove', 'formidable' ), |
||
1723 | 'offset' => apply_filters( 'frm_scroll_offset', 4 ), |
||
1724 | 'nonce' => wp_create_nonce( 'frm_ajax' ), |
||
1725 | 'id' => __( 'ID', 'formidable' ), |
||
1726 | 'no_results' => __( 'No results match', 'formidable' ), |
||
1727 | ) ); |
||
1728 | |||
1729 | if ( $location == 'admin' ) { |
||
1730 | $frm_settings = self::get_settings(); |
||
1731 | wp_localize_script( 'formidable_admin', 'frm_admin_js', array( |
||
1732 | 'confirm_uninstall' => __( 'Are you sure you want to do this? Clicking OK will delete all forms, form data, and all other Formidable data. There is no Undo.', 'formidable' ), |
||
1733 | 'desc' => __( '(Click to add description)', 'formidable' ), |
||
1734 | 'blank' => __( '(Blank)', 'formidable' ), |
||
1735 | 'no_label' => __( '(no label)', 'formidable' ), |
||
1736 | 'saving' => esc_attr( __( 'Saving', 'formidable' ) ), |
||
1737 | 'saved' => esc_attr( __( 'Saved', 'formidable' ) ), |
||
1738 | 'ok' => __( 'OK' ), |
||
1739 | 'cancel' => __( 'Cancel', 'formidable' ), |
||
1740 | 'default' => __( 'Default', 'formidable' ), |
||
1741 | 'clear_default' => __( 'Clear default value when typing', 'formidable' ), |
||
1742 | 'no_clear_default' => __( 'Do not clear default value when typing', 'formidable' ), |
||
1743 | 'valid_default' => __( 'Default value will pass form validation', 'formidable' ), |
||
1744 | 'no_valid_default' => __( 'Default value will NOT pass form validation', 'formidable' ), |
||
1745 | 'confirm' => __( 'Are you sure?', 'formidable' ), |
||
1746 | 'conf_delete' => __( 'Are you sure you want to delete this field and all data associated with it?', 'formidable' ), |
||
1765 | |||
1766 | /** |
||
1767 | * echo the message on the plugins listing page |
||
1768 | * @since 1.07.10 |
||
1769 | * |
||
1770 | * @param float $min_version The version the add-on requires |
||
1771 | */ |
||
1772 | public static function min_version_notice( $min_version ) { |
||
1785 | |||
1786 | public static function locales( $type = 'date' ) { |
||
1843 | } |
||
1844 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.