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 = 47; //version of the database we are moving to |
||
| 8 | public static $pro_db_version = 37; //deprecated |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @since 2.0 |
||
| 12 | */ |
||
| 13 | public static $plug_version = '2.05.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 ) { |
||
| 61 | $affiliate_id = self::get_affiliate(); |
||
| 62 | if ( ! empty( $affiliate_id ) ) { |
||
| 63 | $url = str_replace( array( 'http://', 'https://' ), '', $url ); |
||
| 64 | $url = 'http://www.shareasale.com/r.cfm?u='. absint( $affiliate_id ) .'&b=841990&m=64739&afftrack=plugin&urllink=' . urlencode( $url ); |
||
| 65 | } |
||
| 66 | return $url; |
||
| 67 | } |
||
| 68 | |||
| 69 | public static function get_affiliate() { |
||
| 70 | return absint( apply_filters( 'frm_affiliate_id', 0 ) ); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get the Formidable settings |
||
| 75 | * |
||
| 76 | * @since 2.0 |
||
| 77 | * |
||
| 78 | * @param None |
||
| 79 | * @return FrmSettings $frm_setings |
||
| 80 | */ |
||
| 81 | public static function get_settings() { |
||
| 82 | global $frm_settings; |
||
| 83 | if ( empty($frm_settings) ) { |
||
| 84 | $frm_settings = new FrmSettings(); |
||
| 85 | } |
||
| 86 | return $frm_settings; |
||
| 87 | } |
||
| 88 | |||
| 89 | public static function get_menu_name() { |
||
| 90 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 91 | return $frm_settings->menu; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @since 2.02.04 |
||
| 96 | */ |
||
| 97 | public static function ips_saved() { |
||
| 98 | $frm_settings = self::get_settings(); |
||
| 99 | return ! $frm_settings->no_ips; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Show a message in place of pro features |
||
| 104 | * |
||
| 105 | * @since 2.0 |
||
| 106 | */ |
||
| 107 | public static function update_message() { |
||
| 108 | _deprecated_function( __FUNCTION__, '2.0.19' ); |
||
| 109 | } |
||
| 110 | |||
| 111 | public static function pro_is_installed() { |
||
| 112 | return apply_filters('frm_pro_installed', false); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Check for certain page in Formidable settings |
||
| 117 | * |
||
| 118 | * @since 2.0 |
||
| 119 | * |
||
| 120 | * @param string $page The name of the page to check |
||
| 121 | * @return boolean |
||
| 122 | */ |
||
| 123 | public static function is_admin_page( $page = 'formidable' ) { |
||
| 124 | global $pagenow; |
||
| 125 | $get_page = self::simple_get( 'page', 'sanitize_title' ); |
||
| 126 | if ( $pagenow ) { |
||
| 127 | return $pagenow == 'admin.php' && $get_page == $page; |
||
| 128 | } |
||
| 129 | |||
| 130 | return is_admin() && $get_page == $page; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Check for the form preview page |
||
| 135 | * |
||
| 136 | * @since 2.0 |
||
| 137 | * |
||
| 138 | * @param None |
||
| 139 | * @return boolean |
||
| 140 | */ |
||
| 141 | public static function is_preview_page() { |
||
| 142 | global $pagenow; |
||
| 143 | $action = FrmAppHelper::simple_get( 'action', 'sanitize_title' ); |
||
| 144 | return $pagenow && $pagenow == 'admin-ajax.php' && $action == 'frm_forms_preview'; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Check for ajax except the form preview page |
||
| 149 | * |
||
| 150 | * @since 2.0 |
||
| 151 | * |
||
| 152 | * @param None |
||
| 153 | * @return boolean |
||
| 154 | */ |
||
| 155 | public static function doing_ajax() { |
||
| 156 | return self::wp_doing_ajax() && ! self::is_preview_page(); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Use the WP 4.7 wp_doing_ajax function |
||
| 161 | * @sine 2.05.07 |
||
| 162 | */ |
||
| 163 | public static function wp_doing_ajax() { |
||
| 164 | if ( function_exists( 'wp_doing_ajax' ) ) { |
||
| 165 | $doing_ajax = wp_doing_ajax(); |
||
| 166 | } else { |
||
| 167 | $doing_ajax = defined('DOING_AJAX') && DOING_AJAX; |
||
| 168 | } |
||
| 169 | return $doing_ajax; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @since 2.0.8 |
||
| 174 | */ |
||
| 175 | public static function prevent_caching() { |
||
| 176 | global $frm_vars; |
||
| 177 | return isset( $frm_vars['prevent_caching'] ) && $frm_vars['prevent_caching']; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Check if on an admin page |
||
| 182 | * |
||
| 183 | * @since 2.0 |
||
| 184 | * |
||
| 185 | * @param None |
||
| 186 | * @return boolean |
||
| 187 | */ |
||
| 188 | public static function is_admin() { |
||
| 189 | return is_admin() && ! self::wp_doing_ajax(); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Check if value contains blank value or empty array |
||
| 194 | * |
||
| 195 | * @since 2.0 |
||
| 196 | * @param mixed $value - value to check |
||
| 197 | * @param string |
||
| 198 | * @return boolean |
||
| 199 | */ |
||
| 200 | public static function is_empty_value( $value, $empty = '' ) { |
||
| 201 | return ( is_array( $value ) && empty( $value ) ) || $value == $empty; |
||
| 202 | } |
||
| 203 | |||
| 204 | public static function is_not_empty_value( $value, $empty = '' ) { |
||
| 205 | return ! self::is_empty_value( $value, $empty ); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get any value from the $_SERVER |
||
| 210 | * |
||
| 211 | * @since 2.0 |
||
| 212 | * @param string $value |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | public static function get_server_value( $value ) { |
||
| 216 | return isset( $_SERVER[ $value ] ) ? wp_strip_all_tags( $_SERVER[ $value ] ) : ''; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Check for the IP address in several places |
||
| 221 | * Used by [ip] shortcode |
||
| 222 | * |
||
| 223 | * @return string The IP address of the current user |
||
| 224 | */ |
||
| 225 | public static function get_ip_address() { |
||
| 226 | $ip = ''; |
||
| 227 | foreach ( array( |
||
| 228 | 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', |
||
| 229 | 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR', |
||
| 230 | ) as $key ) { |
||
| 231 | if ( ! isset( $_SERVER[ $key ] ) ) { |
||
| 232 | continue; |
||
| 233 | } |
||
| 234 | |||
| 235 | foreach ( explode( ',', $_SERVER[ $key ] ) as $ip ) { |
||
| 236 | $ip = trim($ip); // just to be safe |
||
| 237 | |||
| 238 | if ( filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false ) { |
||
| 239 | return $ip; |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | return sanitize_text_field( $ip ); |
||
| 245 | } |
||
| 246 | |||
| 247 | public static function get_param( $param, $default = '', $src = 'get', $sanitize = '' ) { |
||
| 248 | if ( strpos($param, '[') ) { |
||
| 249 | $params = explode('[', $param); |
||
| 250 | $param = $params[0]; |
||
| 251 | } |
||
| 252 | |||
| 253 | if ( $src == 'get' ) { |
||
| 254 | $value = isset( $_POST[ $param ] ) ? stripslashes_deep( $_POST[ $param ] ) : ( isset( $_GET[ $param ] ) ? stripslashes_deep( $_GET[ $param ] ) : $default ); |
||
| 255 | if ( ! isset( $_POST[ $param ] ) && isset( $_GET[ $param ] ) && ! is_array( $value ) ) { |
||
| 256 | $value = stripslashes_deep( htmlspecialchars_decode( $_GET[ $param ] ) ); |
||
| 257 | } |
||
| 258 | self::sanitize_value( $sanitize, $value ); |
||
| 259 | } else { |
||
| 260 | $value = self::get_simple_request( array( 'type' => $src, 'param' => $param, 'default' => $default, 'sanitize' => $sanitize ) ); |
||
| 261 | } |
||
| 262 | |||
| 263 | if ( isset( $params ) && is_array( $value ) && ! empty( $value ) ) { |
||
| 264 | foreach ( $params as $k => $p ) { |
||
| 265 | if ( ! $k || ! is_array($value) ) { |
||
| 266 | continue; |
||
| 267 | } |
||
| 268 | |||
| 269 | $p = trim($p, ']'); |
||
| 270 | $value = isset( $value[ $p ] ) ? $value[ $p ] : $default; |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | return $value; |
||
| 275 | } |
||
| 276 | |||
| 277 | public static function get_post_param( $param, $default = '', $sanitize = '' ) { |
||
| 278 | return self::get_simple_request( array( 'type' => 'post', 'param' => $param, 'default' => $default, 'sanitize' => $sanitize ) ); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @since 2.0 |
||
| 283 | * |
||
| 284 | * @param string $param |
||
| 285 | * @param string $sanitize |
||
| 286 | * @param string $default |
||
| 287 | * @return string|array |
||
| 288 | */ |
||
| 289 | public static function simple_get( $param, $sanitize = 'sanitize_text_field', $default = '' ) { |
||
| 290 | return self::get_simple_request( array( 'type' => 'get', 'param' => $param, 'default' => $default, 'sanitize' => $sanitize ) ); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Get a GET/POST/REQUEST value and sanitize it |
||
| 295 | * |
||
| 296 | * @since 2.0.6 |
||
| 297 | * @param array $args |
||
| 298 | * @return string|array |
||
| 299 | */ |
||
| 300 | public static function get_simple_request( $args ) { |
||
| 301 | $defaults = array( |
||
| 302 | 'param' => '', 'default' => '', |
||
| 303 | 'type' => 'get', 'sanitize' => 'sanitize_text_field', |
||
| 304 | ); |
||
| 305 | $args = wp_parse_args( $args, $defaults ); |
||
| 306 | |||
| 307 | $value = $args['default']; |
||
| 308 | if ( $args['type'] == 'get' ) { |
||
| 309 | if ( $_GET && isset( $_GET[ $args['param'] ] ) ) { |
||
| 310 | $value = $_GET[ $args['param'] ]; |
||
| 311 | } |
||
| 312 | } else if ( $args['type'] == 'post' ) { |
||
| 313 | if ( isset( $_POST[ $args['param'] ] ) ) { |
||
| 314 | $value = stripslashes_deep( maybe_unserialize( $_POST[ $args['param'] ] ) ); |
||
| 315 | } |
||
| 316 | } else { |
||
| 317 | if ( isset( $_REQUEST[ $args['param'] ] ) ) { |
||
| 318 | $value = $_REQUEST[ $args['param'] ]; |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | self::sanitize_value( $args['sanitize'], $value ); |
||
| 323 | return $value; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Preserve backslashes in a value, but make sure value doesn't get compounding slashes |
||
| 328 | * |
||
| 329 | * @since 2.0.8 |
||
| 330 | * @param string $value |
||
| 331 | * @return string $value |
||
| 332 | */ |
||
| 333 | public static function preserve_backslashes( $value ) { |
||
| 334 | // If backslashes have already been added, don't add them again |
||
| 335 | if ( strpos( $value, '\\\\' ) === false ) { |
||
| 336 | $value = addslashes( $value ); |
||
| 337 | } |
||
| 338 | return $value; |
||
| 339 | } |
||
| 340 | |||
| 341 | public static function sanitize_value( $sanitize, &$value ) { |
||
| 342 | if ( ! empty( $sanitize ) ) { |
||
| 343 | if ( is_array( $value ) ) { |
||
| 344 | $temp_values = $value; |
||
| 345 | foreach ( $temp_values as $k => $v ) { |
||
| 346 | FrmAppHelper::sanitize_value( $sanitize, $value[ $k ] ); |
||
| 347 | } |
||
| 348 | } else { |
||
| 349 | $value = call_user_func( $sanitize, $value ); |
||
| 350 | } |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | public static function sanitize_request( $sanitize_method, &$values ) { |
||
| 355 | $temp_values = $values; |
||
| 356 | foreach ( $temp_values as $k => $val ) { |
||
| 357 | if ( isset( $sanitize_method[ $k ] ) ) { |
||
| 358 | $values[ $k ] = call_user_func( $sanitize_method[ $k ], $val ); |
||
| 359 | } |
||
| 360 | } |
||
| 361 | } |
||
| 362 | |||
| 363 | public static function sanitize_array( &$values ) { |
||
| 364 | $temp_values = $values; |
||
| 365 | foreach ( $temp_values as $k => $val ) { |
||
| 366 | $values[ $k ] = wp_kses_post( $val ); |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Sanitize the value, and allow some HTML |
||
| 372 | * @since 2.0 |
||
| 373 | * @param string $value |
||
| 374 | * @param array|string $allowed 'all' for everything included as defaults |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | public static function kses( $value, $allowed = array() ) { |
||
| 378 | $allowed_html = self::allowed_html( $allowed ); |
||
| 379 | |||
| 380 | return wp_kses( $value, $allowed_html ); |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @since 2.05.03 |
||
| 385 | */ |
||
| 386 | private static function allowed_html( $allowed ) { |
||
| 387 | $html = self::safe_html(); |
||
| 388 | $allowed_html = array(); |
||
| 389 | if ( $allowed == 'all' ) { |
||
| 390 | $allowed_html = $html; |
||
| 391 | } else { |
||
| 392 | foreach ( $allowed as $a ) { |
||
| 393 | $allowed_html[ $a ] = isset( $html[ $a ] ) ? $html[ $a ] : array(); |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | return apply_filters( 'frm_striphtml_allowed_tags', $allowed_html ); |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @since 2.05.03 |
||
| 402 | */ |
||
| 403 | private static function safe_html() { |
||
| 404 | return array( |
||
| 405 | 'a' => array( |
||
| 406 | 'class' => array(), |
||
| 407 | 'href' => array(), |
||
| 408 | 'id' => array(), |
||
| 409 | 'rel' => array(), |
||
| 410 | 'title' => array(), |
||
| 411 | ), |
||
| 412 | 'abbr' => array( |
||
| 413 | 'title' => array(), |
||
| 414 | ), |
||
| 415 | 'b' => array(), |
||
| 416 | 'blockquote' => array( |
||
| 417 | 'cite' => array(), |
||
| 418 | ), |
||
| 419 | 'br' => array(), |
||
| 420 | 'cite' => array( |
||
| 421 | 'title' => array(), |
||
| 422 | ), |
||
| 423 | 'code' => array(), |
||
| 424 | 'del' => array( |
||
| 425 | 'datetime' => array(), |
||
| 426 | 'title' => array(), |
||
| 427 | ), |
||
| 428 | 'dd' => array(), |
||
| 429 | 'div' => array( |
||
| 430 | 'class' => array(), |
||
| 431 | 'id' => array(), |
||
| 432 | 'title' => array(), |
||
| 433 | 'style' => array(), |
||
| 434 | ), |
||
| 435 | 'dl' => array(), |
||
| 436 | 'dt' => array(), |
||
| 437 | 'em' => array(), |
||
| 438 | 'h1' => array(), |
||
| 439 | 'h2' => array(), |
||
| 440 | 'h3' => array(), |
||
| 441 | 'h4' => array(), |
||
| 442 | 'h5' => array(), |
||
| 443 | 'h6' => array(), |
||
| 444 | 'i' => array(), |
||
| 445 | 'img' => array( |
||
| 446 | 'alt' => array(), |
||
| 447 | 'class' => array(), |
||
| 448 | 'height' => array(), |
||
| 449 | 'id' => array(), |
||
| 450 | 'src' => array(), |
||
| 451 | 'width' => array(), |
||
| 452 | ), |
||
| 453 | 'li' => array( |
||
| 454 | 'class' => array(), |
||
| 455 | 'id' => array(), |
||
| 456 | ), |
||
| 457 | 'ol' => array( |
||
| 458 | 'class' => array(), |
||
| 459 | 'id' => array(), |
||
| 460 | ), |
||
| 461 | 'p' => array( |
||
| 462 | 'class' => array(), |
||
| 463 | 'id' => array(), |
||
| 464 | ), |
||
| 465 | 'pre' => array(), |
||
| 466 | 'q' => array( |
||
| 467 | 'cite' => array(), |
||
| 468 | 'title' => array(), |
||
| 469 | ), |
||
| 470 | 'span' => array( |
||
| 471 | 'class' => array(), |
||
| 472 | 'id' => array(), |
||
| 473 | 'title' => array(), |
||
| 474 | 'style' => array(), |
||
| 475 | ), |
||
| 476 | 'strike' => array(), |
||
| 477 | 'strong' => array(), |
||
| 478 | 'ul' => array( |
||
| 479 | 'class' => array(), |
||
| 480 | 'id' => array(), |
||
| 481 | ), |
||
| 482 | ); |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Used when switching the action for a bulk action |
||
| 487 | * @since 2.0 |
||
| 488 | */ |
||
| 489 | public static function remove_get_action() { |
||
| 490 | if ( ! isset($_GET) ) { |
||
| 491 | return; |
||
| 492 | } |
||
| 493 | |||
| 494 | $new_action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : ( isset( $_GET['action2'] ) ? sanitize_text_field( $_GET['action2'] ) : '' ); |
||
| 495 | if ( ! empty( $new_action ) ) { |
||
| 496 | $_SERVER['REQUEST_URI'] = str_replace( '&action=' . $new_action, '', FrmAppHelper::get_server_value( 'REQUEST_URI' ) ); |
||
| 497 | } |
||
| 498 | } |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Check the WP query for a parameter |
||
| 502 | * |
||
| 503 | * @since 2.0 |
||
| 504 | * @return string|array |
||
| 505 | */ |
||
| 506 | public static function get_query_var( $value, $param ) { |
||
| 507 | if ( $value != '' ) { |
||
| 508 | return $value; |
||
| 509 | } |
||
| 510 | |||
| 511 | global $wp_query; |
||
| 512 | if ( isset( $wp_query->query_vars[ $param ] ) ) { |
||
| 513 | $value = $wp_query->query_vars[ $param ]; |
||
| 514 | } |
||
| 515 | |||
| 516 | return $value; |
||
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @param string $type |
||
| 521 | */ |
||
| 522 | public static function trigger_hook_load( $type, $object = null ) { |
||
| 523 | // only load the form hooks once |
||
| 524 | $hooks_loaded = apply_filters( 'frm_' . $type . '_hooks_loaded', false, $object ); |
||
| 525 | if ( ! $hooks_loaded ) { |
||
| 526 | do_action( 'frm_load_' . $type . '_hooks' ); |
||
| 527 | } |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Check a value from a shortcode to see if true or false. |
||
| 532 | * True when value is 1, true, 'true', 'yes' |
||
| 533 | * |
||
| 534 | * @since 1.07.10 |
||
| 535 | * |
||
| 536 | * @param string $value The value to compare |
||
| 537 | * @return boolean True or False |
||
| 538 | */ |
||
| 539 | public static function is_true( $value ) { |
||
| 540 | return ( true === $value || 1 == $value || 'true' == $value || 'yes' == $value ); |
||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Used to filter shortcode in text widgets |
||
| 545 | */ |
||
| 546 | public static function widget_text_filter_callback( $matches ) { |
||
| 547 | _deprecated_function( __METHOD__, '2.5.4' ); |
||
| 548 | return do_shortcode( $matches[0] ); |
||
| 549 | } |
||
| 550 | |||
| 551 | public static function get_pages() { |
||
| 554 | |||
| 555 | public static function wp_pages_dropdown( $field_name, $page_id, $truncate = false ) { |
||
| 569 | |||
| 570 | public static function post_edit_link( $post_id ) { |
||
| 578 | |||
| 579 | public static function wp_roles_dropdown( $field_name, $capability, $multiple = 'single' ) { |
||
| 588 | |||
| 589 | public static function roles_options( $capability ) { |
||
| 605 | |||
| 606 | public static function frm_capabilities( $type = 'auto' ) { |
||
| 627 | |||
| 628 | public static function user_has_permission( $needed_role ) { |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Make sure administrators can see Formidable menu |
||
| 652 | * |
||
| 653 | * @since 2.0 |
||
| 654 | */ |
||
| 655 | public static function maybe_add_permissions() { |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Make sure admins have permission to see the menu items |
||
| 673 | * @since 2.0.6 |
||
| 674 | */ |
||
| 675 | public static function force_capability( $cap = 'frm_change_settings' ) { |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Check if the user has permision for action. |
||
| 687 | * Return permission message and stop the action if no permission |
||
| 688 | * @since 2.0 |
||
| 689 | * @param string $permission |
||
| 690 | */ |
||
| 691 | public static function permission_check( $permission, $show_message = 'show' ) { |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Check user permission and nonce |
||
| 703 | * @since 2.0 |
||
| 704 | * @param string $permission |
||
| 705 | * @return false|string The permission message or false if allowed |
||
| 706 | */ |
||
| 707 | public static function permission_nonce_error( $permission, $nonce_name = '', $nonce = '' ) { |
||
| 725 | |||
| 726 | public static function checked( $values, $current ) { |
||
| 731 | |||
| 732 | public static function check_selected( $values, $current ) { |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Check if current field option is an "other" option |
||
| 742 | * |
||
| 743 | * @since 2.0 |
||
| 744 | * |
||
| 745 | * @param string $opt_key |
||
| 746 | * @return boolean Returns true if current field option is an "Other" option |
||
| 747 | */ |
||
| 748 | public static function is_other_opt( $opt_key ) { |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Get value that belongs in "Other" text box |
||
| 755 | * |
||
| 756 | * @since 2.0 |
||
| 757 | * |
||
| 758 | * @param string $opt_key |
||
| 759 | * @param array $field |
||
| 760 | * @return string $other_val |
||
| 761 | */ |
||
| 762 | public static function get_other_val( $opt_key, $field, $parent = false, $pointer = false ) { |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Check if there is a saved value for the "Other" text field. If so, set it as the $other_val. |
||
| 769 | * Intended for front-end use |
||
| 770 | * |
||
| 771 | * @since 2.0 |
||
| 772 | * |
||
| 773 | * @param array $field |
||
| 774 | * @param boolean $other_opt |
||
| 775 | * @param string $checked |
||
| 776 | * @param array $args should include opt_key and field name |
||
| 777 | * @return string $other_val |
||
| 778 | */ |
||
| 779 | public static function prepare_other_input( $field, &$other_opt, &$checked, $args = array() ) { |
||
| 784 | |||
| 785 | public static function recursive_function_map( $value, $function ) { |
||
| 808 | |||
| 809 | public static function is_assoc( $array ) { |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Flatten a multi-dimensional array |
||
| 815 | */ |
||
| 816 | public static function array_flatten( $array, $keys = 'keep' ) { |
||
| 831 | |||
| 832 | public static function esc_textarea( $text, $is_rich_text = false ) { |
||
| 833 | $safe_text = str_replace( '"', '"', $text ); |
||
| 834 | if ( ! $is_rich_text ) { |
||
| 835 | $safe_text = htmlspecialchars( $safe_text, ENT_NOQUOTES ); |
||
| 836 | } |
||
| 837 | $safe_text = str_replace( '&', '&', $safe_text ); |
||
| 838 | return apply_filters( 'esc_textarea', $safe_text, $text ); |
||
| 839 | } |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Add auto paragraphs to text areas |
||
| 843 | * @since 2.0 |
||
| 844 | */ |
||
| 845 | public static function use_wpautop( $content ) { |
||
| 851 | |||
| 852 | public static function replace_quotes( $val ) { |
||
| 859 | |||
| 860 | /** |
||
| 861 | * @since 2.0 |
||
| 862 | * @return string The base Google APIS url for the current version of jQuery UI |
||
| 863 | */ |
||
| 864 | public static function jquery_ui_base_url() { |
||
| 869 | |||
| 870 | /** |
||
| 871 | * @param string $handle |
||
| 872 | */ |
||
| 873 | public static function script_version( $handle, $default = 0 ) { |
||
| 874 | global $wp_scripts; |
||
| 875 | if ( ! $wp_scripts ) { |
||
| 876 | return $default; |
||
| 877 | } |
||
| 878 | |||
| 879 | $ver = $default; |
||
| 880 | if ( ! isset( $wp_scripts->registered[ $handle ] ) ) { |
||
| 881 | return $ver; |
||
| 882 | } |
||
| 883 | |||
| 884 | $query = $wp_scripts->registered[ $handle ]; |
||
| 885 | if ( is_object( $query ) && ! empty( $query->ver ) ) { |
||
| 886 | $ver = $query->ver; |
||
| 887 | } |
||
| 888 | |||
| 889 | return $ver; |
||
| 890 | } |
||
| 891 | |||
| 892 | public static function js_redirect( $url ) { |
||
| 893 | return '<script type="text/javascript">window.location="' . esc_url_raw( $url ) . '"</script>'; |
||
| 894 | } |
||
| 895 | |||
| 896 | public static function get_user_id_param( $user_id ) { |
||
| 897 | if ( ! $user_id || empty($user_id) || is_numeric($user_id) ) { |
||
| 898 | return $user_id; |
||
| 899 | } |
||
| 900 | |||
| 901 | $user_id = sanitize_text_field( $user_id ); |
||
| 902 | if ( $user_id == 'current' ) { |
||
| 903 | $user_id = get_current_user_id(); |
||
| 904 | } else { |
||
| 905 | if ( is_email($user_id) ) { |
||
| 906 | $user = get_user_by('email', $user_id); |
||
| 907 | } else { |
||
| 908 | $user = get_user_by('login', $user_id); |
||
| 909 | } |
||
| 910 | |||
| 911 | if ( $user ) { |
||
| 912 | $user_id = $user->ID; |
||
| 913 | } |
||
| 914 | unset($user); |
||
| 915 | } |
||
| 916 | |||
| 917 | return $user_id; |
||
| 918 | } |
||
| 919 | |||
| 920 | public static function get_file_contents( $filename, $atts = array() ) { |
||
| 932 | |||
| 933 | /** |
||
| 934 | * @param string $table_name |
||
| 935 | * @param string $column |
||
| 936 | * @param int $id |
||
| 937 | * @param int $num_chars |
||
| 938 | */ |
||
| 939 | public static function get_unique_key( $name = '', $table_name, $column, $id = 0, $num_chars = 5 ) { |
||
| 969 | |||
| 970 | /** |
||
| 971 | * Editing a Form or Entry |
||
| 972 | * @param string $table |
||
| 973 | * @return bool|array |
||
| 974 | */ |
||
| 975 | public static function setup_edit_vars( $record, $table, $fields = '', $default = false, $post_values = array(), $args = array() ) { |
||
| 1015 | |||
| 1016 | private static function fill_field_defaults( $field, $record, array &$values, $args ) { |
||
| 1065 | |||
| 1066 | private static function fill_field_opts( $field, array &$field_array, $args ) { |
||
| 1087 | |||
| 1088 | /** |
||
| 1089 | * @param string $table |
||
| 1090 | */ |
||
| 1091 | private static function fill_form_opts( $record, $table, $post_values, array &$values ) { |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * Set to POST value or default |
||
| 1119 | */ |
||
| 1120 | private static function fill_form_defaults( $post_values, array &$values ) { |
||
| 1121 | $form_defaults = FrmFormsHelper::get_default_opts(); |
||
| 1122 | |||
| 1123 | foreach ( $form_defaults as $opt => $default ) { |
||
| 1124 | if ( ! isset( $values[ $opt ] ) || $values[ $opt ] == '' ) { |
||
| 1125 | $values[ $opt ] = ( $post_values && isset( $post_values['options'][ $opt ] ) ) ? $post_values['options'][ $opt ] : $default; |
||
| 1126 | } |
||
| 1127 | |||
| 1128 | unset($opt, $defaut); |
||
| 1129 | } |
||
| 1130 | |||
| 1131 | if ( ! isset( $values['custom_style'] ) ) { |
||
| 1132 | $values['custom_style'] = self::custom_style_value( $post_values ); |
||
| 1133 | } |
||
| 1134 | |||
| 1135 | foreach ( array( 'before', 'after', 'submit' ) as $h ) { |
||
| 1136 | if ( ! isset( $values[ $h . '_html' ] ) ) { |
||
| 1137 | $values[ $h . '_html' ] = ( isset( $post_values['options'][ $h . '_html' ] ) ? $post_values['options'][ $h . '_html' ] : FrmFormsHelper::get_default_html( $h ) ); |
||
| 1138 | } |
||
| 1139 | unset($h); |
||
| 1140 | } |
||
| 1141 | } |
||
| 1142 | |||
| 1143 | /** |
||
| 1144 | * @since 2.2.10 |
||
| 1145 | * @param array $post_values |
||
| 1146 | * @return boolean|int |
||
| 1147 | */ |
||
| 1148 | public static function custom_style_value( $post_values ) { |
||
| 1149 | if ( ! empty( $post_values ) && isset( $post_values['options']['custom_style'] ) ) { |
||
| 1150 | $custom_style = absint( $post_values['options']['custom_style'] ); |
||
| 1151 | } else { |
||
| 1152 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 1153 | $custom_style = ( $frm_settings->load_style != 'none' ); |
||
| 1154 | } |
||
| 1155 | return $custom_style; |
||
| 1156 | } |
||
| 1157 | |||
| 1158 | public static function get_meta_value( $field_id, $entry ) { |
||
| 1162 | |||
| 1163 | public static function insert_opt_html( $args ) { |
||
| 1176 | |||
| 1177 | public static function truncate( $str, $length, $minword = 3, $continue = '...' ) { |
||
| 1217 | |||
| 1218 | public static function mb_function( $function_names, $args ) { |
||
| 1226 | |||
| 1227 | public static function get_formatted_time( $date, $date_format = '', $time_format = '' ) { |
||
| 1250 | |||
| 1251 | private static function add_time_to_date( $time_format, $date ) { |
||
| 1264 | |||
| 1265 | /** |
||
| 1266 | * @since 2.0.8 |
||
| 1267 | */ |
||
| 1268 | public static function get_localized_date( $date_format, $date ) { |
||
| 1272 | |||
| 1273 | /** |
||
| 1274 | * Gets the time ago in words |
||
| 1275 | * |
||
| 1276 | * @param int $from in seconds |
||
| 1277 | * @param int|string $to in seconds |
||
| 1278 | * @return string $time_ago |
||
| 1279 | */ |
||
| 1280 | public static function human_time_diff( $from, $to = '', $levels = 1 ) { |
||
| 1281 | if ( empty( $to ) ) { |
||
| 1282 | $now = new DateTime; |
||
| 1283 | } else { |
||
| 1284 | $now = new DateTime( '@' . $to ); |
||
| 1285 | } |
||
| 1286 | $ago = new DateTime( '@' . $from ); |
||
| 1287 | |||
| 1288 | // Get the time difference |
||
| 1289 | $diff_object = $now->diff( $ago ); |
||
| 1290 | $diff = get_object_vars( $diff_object ); |
||
| 1291 | |||
| 1292 | // Add week amount and update day amount |
||
| 1293 | $diff['w'] = floor( $diff['d'] / 7 ); |
||
| 1294 | $diff['d'] -= $diff['w'] * 7; |
||
| 1295 | |||
| 1296 | $time_strings = self::get_time_strings(); |
||
| 1297 | |||
| 1298 | foreach ( $time_strings as $k => $v ) { |
||
| 1299 | if ( $diff[ $k ] ) { |
||
| 1300 | $time_strings[ $k ] = $diff[ $k ] . ' ' . ( $diff[ $k ] > 1 ? $v[1] : $v[0] ); |
||
| 1301 | } else { |
||
| 1302 | unset( $time_strings[ $k ] ); |
||
| 1303 | } |
||
| 1304 | } |
||
| 1305 | |||
| 1306 | $levels_deep = apply_filters( 'frm_time_ago_levels', $levels, compact( 'time_strings', 'from', 'to' ) ); |
||
| 1307 | $time_strings = array_slice( $time_strings, 0, $levels_deep ); |
||
| 1308 | $time_ago_string = $time_strings ? implode( ' ', $time_strings ) : '0 ' . __( 'seconds', 'formidable' ); |
||
| 1309 | |||
| 1310 | return $time_ago_string; |
||
| 1311 | } |
||
| 1312 | |||
| 1313 | /** |
||
| 1314 | * Get the translatable time strings |
||
| 1315 | * |
||
| 1316 | * @since 2.0.20 |
||
| 1317 | * @return array |
||
| 1318 | */ |
||
| 1319 | private static function get_time_strings() { |
||
| 1330 | |||
| 1331 | // Pagination Methods |
||
| 1332 | |||
| 1333 | /** |
||
| 1334 | * @param integer $current_p |
||
| 1335 | */ |
||
| 1336 | public static function get_last_record_num( $r_count, $current_p, $p_size ) { |
||
| 1337 | return ( ( $r_count < ( $current_p * $p_size ) ) ? $r_count : ( $current_p * $p_size ) ); |
||
| 1338 | } |
||
| 1339 | |||
| 1340 | /** |
||
| 1341 | * @param integer $current_p |
||
| 1342 | */ |
||
| 1343 | public static function get_first_record_num( $r_count, $current_p, $p_size ) { |
||
| 1344 | if ( $current_p == 1 ) { |
||
| 1345 | return 1; |
||
| 1346 | } else { |
||
| 1347 | return ( self::get_last_record_num( $r_count, ( $current_p - 1 ), $p_size ) + 1 ); |
||
| 1348 | } |
||
| 1349 | } |
||
| 1350 | |||
| 1351 | /** |
||
| 1352 | * @return array |
||
| 1353 | */ |
||
| 1354 | public static function json_to_array( $json_vars ) { |
||
| 1355 | $vars = array(); |
||
| 1356 | foreach ( $json_vars as $jv ) { |
||
| 1357 | $jv_name = explode('[', $jv['name']); |
||
| 1358 | $last = count($jv_name) - 1; |
||
| 1359 | foreach ( $jv_name as $p => $n ) { |
||
| 1360 | $name = trim($n, ']'); |
||
| 1361 | if ( ! isset($l1) ) { |
||
| 1362 | $l1 = $name; |
||
| 1363 | } |
||
| 1364 | |||
| 1365 | if ( ! isset($l2) ) { |
||
| 1366 | $l2 = $name; |
||
| 1367 | } |
||
| 1368 | |||
| 1369 | if ( ! isset($l3) ) { |
||
| 1370 | $l3 = $name; |
||
| 1405 | |||
| 1406 | /** |
||
| 1407 | * @param string $name |
||
| 1408 | * @param string $l1 |
||
| 1409 | */ |
||
| 1410 | public static function add_value_to_array( $name, $l1, $val, &$vars ) { |
||
| 1417 | |||
| 1418 | public static function maybe_add_tooltip( $name, $class = 'closed', $form_name = '' ) { |
||
| 1445 | |||
| 1446 | /** |
||
| 1447 | * Add the current_page class to that page in the form nav |
||
| 1448 | */ |
||
| 1449 | public static function select_current_page( $page, $current_page, $action = array() ) { |
||
| 1459 | |||
| 1460 | /** |
||
| 1461 | * Prepare and json_encode post content |
||
| 1462 | * |
||
| 1463 | * @since 2.0 |
||
| 1464 | * |
||
| 1465 | * @param array $post_content |
||
| 1466 | * @return string $post_content ( json encoded array ) |
||
| 1467 | */ |
||
| 1468 | public static function prepare_and_encode( $post_content ) { |
||
| 1489 | |||
| 1490 | private static function prepare_action_slashes( $val, $key, &$post_content ) { |
||
| 1508 | |||
| 1509 | public static function maybe_json_decode( $string ) { |
||
| 1526 | |||
| 1527 | /** |
||
| 1528 | * @since 1.07.10 |
||
| 1529 | * |
||
| 1530 | * @param string $post_type The name of the post type that may need to be highlighted |
||
| 1531 | * echo The javascript to open and highlight the Formidable menu |
||
| 1532 | */ |
||
| 1533 | public static function maybe_highlight_menu( $post_type ) { |
||
| 1547 | |||
| 1548 | /** |
||
| 1549 | * Load the JS file on non-Formidable pages in the admin area |
||
| 1550 | * @since 2.0 |
||
| 1551 | */ |
||
| 1552 | public static function load_admin_wide_js( $load = true ) { |
||
| 1568 | |||
| 1569 | /** |
||
| 1570 | * @since 2.0.9 |
||
| 1571 | */ |
||
| 1572 | public static function load_font_style() { |
||
| 1575 | |||
| 1576 | /** |
||
| 1577 | * @param string $location |
||
| 1578 | */ |
||
| 1579 | public static function localize_script( $location ) { |
||
| 1633 | |||
| 1634 | /** |
||
| 1635 | * echo the message on the plugins listing page |
||
| 1636 | * @since 1.07.10 |
||
| 1637 | * |
||
| 1638 | * @param float $min_version The version the add-on requires |
||
| 1639 | */ |
||
| 1640 | public static function min_version_notice( $min_version ) { |
||
| 1653 | |||
| 1654 | public static function locales( $type = 'date' ) { |
||
| 1711 | |||
| 1712 | /** |
||
| 1713 | * Prepare and save settings in styles and actions |
||
| 1714 | * |
||
| 1715 | * @param array $settings |
||
| 1716 | * @param string $group |
||
| 1717 | * |
||
| 1718 | * @since 2.0.6 |
||
| 1719 | * @deprecated 2.05.06 |
||
| 1720 | */ |
||
| 1721 | public static function save_settings( $settings, $group ) { |
||
| 1725 | |||
| 1726 | /** |
||
| 1727 | * Since actions are JSON encoded, we don't want any filters messing with it. |
||
| 1728 | * Remove the filters and then add them back in case any posts or views are |
||
| 1729 | * also being imported. |
||
| 1730 | * |
||
| 1731 | * Used when saving form actions and styles |
||
| 1732 | * |
||
| 1733 | * @since 2.0.4 |
||
| 1734 | * @deprecated 2.05.06 |
||
| 1735 | */ |
||
| 1736 | public static function save_json_post( $settings ) { |
||
| 1740 | |||
| 1741 | /** |
||
| 1742 | * Check cache before fetching values and saving to cache |
||
| 1743 | * |
||
| 1744 | * @since 2.0 |
||
| 1745 | * @deprecated 2.05.06 |
||
| 1746 | * |
||
| 1747 | * @param string $cache_key The unique name for this cache |
||
| 1748 | * @param string $group The name of the cache group |
||
| 1749 | * @param string $query If blank, don't run a db call |
||
| 1750 | * @param string $type The wpdb function to use with this query |
||
| 1751 | * @return mixed $results The cache or query results |
||
| 1752 | */ |
||
| 1753 | public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) { |
||
| 1757 | |||
| 1758 | /** |
||
| 1759 | * @deprecated 2.05.06 |
||
| 1760 | */ |
||
| 1761 | public static function set_cache( $cache_key, $results, $group = '', $time = 300 ) { |
||
| 1765 | |||
| 1766 | /** |
||
| 1767 | * Keep track of the keys cached in each group so they can be deleted |
||
| 1768 | * in Redis and Memcache |
||
| 1769 | * @deprecated 2.05.06 |
||
| 1770 | */ |
||
| 1771 | public static function add_key_to_group_cache( $key, $group ) { |
||
| 1775 | |||
| 1776 | public static function get_group_cached_keys( $group ) { |
||
| 1780 | |||
| 1781 | /** |
||
| 1782 | * @since 2.0 |
||
| 1783 | * @deprecated 2.05.06 |
||
| 1784 | * @return mixed The cached value or false |
||
| 1785 | */ |
||
| 1786 | public static function check_cache_and_transient( $cache_key ) { |
||
| 1790 | |||
| 1791 | /** |
||
| 1792 | * @since 2.0 |
||
| 1793 | * @deprecated 2.05.06 |
||
| 1794 | * @param string $cache_key |
||
| 1795 | */ |
||
| 1796 | public static function delete_cache_and_transient( $cache_key, $group = 'default' ) { |
||
| 1800 | |||
| 1801 | /** |
||
| 1802 | * @since 2.0 |
||
| 1803 | * @deprecated 2.05.06 |
||
| 1804 | * |
||
| 1805 | * @param string $group The name of the cache group |
||
| 1806 | */ |
||
| 1807 | public static function cache_delete_group( $group ) { |
||
| 1811 | |||
| 1812 | /** |
||
| 1813 | * Added for < WP 4.0 compatability |
||
| 1814 | * |
||
| 1815 | * @since 1.07.10 |
||
| 1816 | * @deprecated 2.05.06 |
||
| 1817 | * |
||
| 1818 | * @param string $term The value to escape |
||
| 1819 | * @return string The escaped value |
||
| 1820 | */ |
||
| 1821 | public static function esc_like( $term ) { |
||
| 1825 | |||
| 1826 | /** |
||
| 1827 | * @param string $order_query |
||
| 1828 | * @deprecated 2.05.06 |
||
| 1829 | */ |
||
| 1830 | public static function esc_order( $order_query ) { |
||
| 1834 | |||
| 1835 | /** |
||
| 1836 | * Make sure this is ordering by either ASC or DESC |
||
| 1837 | * @deprecated 2.05.06 |
||
| 1838 | */ |
||
| 1839 | public static function esc_order_by( &$order_by ) { |
||
| 1843 | |||
| 1844 | /** |
||
| 1845 | * @param string $limit |
||
| 1846 | * @deprecated 2.05.06 |
||
| 1847 | */ |
||
| 1848 | public static function esc_limit( $limit ) { |
||
| 1852 | |||
| 1853 | /** |
||
| 1854 | * Get an array of values ready to go through $wpdb->prepare |
||
| 1855 | * @since 2.0 |
||
| 1856 | * @deprecated 2.05.06 |
||
| 1857 | */ |
||
| 1858 | public static function prepare_array_values( $array, $type = '%s' ) { |
||
| 1862 | |||
| 1863 | /** |
||
| 1864 | * @deprecated 2.05.06 |
||
| 1865 | */ |
||
| 1866 | public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) { |
||
| 1870 | } |
||
| 1871 |