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 = 36; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @since 2.0 |
||
| 12 | */ |
||
| 13 | public static $plug_version = '2.02b4'; |
||
| 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() { |
||
| 22 | return self::$plug_version; |
||
| 23 | } |
||
| 24 | |||
| 25 | public static function plugin_folder() { |
||
| 26 | return basename(self::plugin_path()); |
||
| 27 | } |
||
| 28 | |||
| 29 | public static function plugin_path() { |
||
| 30 | return dirname(dirname(dirname(__FILE__))); |
||
| 31 | } |
||
| 32 | |||
| 33 | public static function plugin_url() { |
||
| 34 | //prevously FRM_URL constant |
||
| 35 | return plugins_url( '', self::plugin_path() . '/formidable.php' ); |
||
| 36 | } |
||
| 37 | |||
| 38 | public static function relative_plugin_url() { |
||
| 39 | return str_replace( array( 'https:', 'http:' ), '', self::plugin_url() ); |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @return string Site URL |
||
| 44 | */ |
||
| 45 | public static function site_url() { |
||
| 46 | return site_url(); |
||
| 47 | } |
||
| 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() { |
||
| 57 | return get_option('blogname'); |
||
| 58 | } |
||
| 59 | |||
| 60 | public static function make_affiliate_url( $url ) { |
||
| 61 | $affiliate_id = self::get_affiliate(); |
||
| 62 | if ( ! empty( $affiliate_id ) ) { |
||
| 63 | $url = add_query_arg( 'aff', $affiliate_id, $url ); |
||
| 64 | } |
||
| 65 | return $url; |
||
| 66 | } |
||
| 67 | |||
| 68 | public static function get_affiliate() { |
||
| 69 | $affiliate_id = apply_filters( 'frm_affiliate_link', get_option('frm_aff') ); |
||
| 70 | $affiliate_id = strtolower( $affiliate_id ); |
||
| 71 | $allowed_affiliates = array( 'mojo' ); |
||
| 72 | if ( ! in_array( $affiliate_id, $allowed_affiliates ) ) { |
||
| 73 | $affiliate_id = false; |
||
| 74 | } |
||
| 75 | return $affiliate_id; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Get the Formidable settings |
||
| 80 | * |
||
| 81 | * @since 2.0 |
||
| 82 | * |
||
| 83 | * @param None |
||
| 84 | * @return FrmSettings $frm_setings |
||
| 85 | */ |
||
| 86 | public static function get_settings() { |
||
| 87 | global $frm_settings; |
||
| 88 | if ( empty($frm_settings) ) { |
||
| 89 | $frm_settings = new FrmSettings(); |
||
| 90 | } |
||
| 91 | return $frm_settings; |
||
| 92 | } |
||
| 93 | |||
| 94 | public static function get_menu_name() { |
||
| 95 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 96 | return $frm_settings->menu; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Show a message in place of pro features |
||
| 101 | * |
||
| 102 | * @since 2.0 |
||
| 103 | */ |
||
| 104 | public static function update_message() { |
||
| 105 | _deprecated_function( __FUNCTION__, '2.0.19' ); |
||
| 106 | } |
||
| 107 | |||
| 108 | public static function pro_is_installed() { |
||
| 109 | return apply_filters('frm_pro_installed', false); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Check for certain page in Formidable settings |
||
| 114 | * |
||
| 115 | * @since 2.0 |
||
| 116 | * |
||
| 117 | * @param string $page The name of the page to check |
||
| 118 | * @return boolean |
||
| 119 | */ |
||
| 120 | public static function is_admin_page( $page = 'formidable' ) { |
||
| 121 | global $pagenow; |
||
| 122 | $get_page = self::simple_get( 'page', 'sanitize_title' ); |
||
| 123 | if ( $pagenow ) { |
||
| 124 | return $pagenow == 'admin.php' && $get_page == $page; |
||
| 125 | } |
||
| 126 | |||
| 127 | return is_admin() && $get_page == $page; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Check for the form preview page |
||
| 132 | * |
||
| 133 | * @since 2.0 |
||
| 134 | * |
||
| 135 | * @param None |
||
| 136 | * @return boolean |
||
| 137 | */ |
||
| 138 | public static function is_preview_page() { |
||
| 139 | global $pagenow; |
||
| 140 | $action = FrmAppHelper::simple_get( 'action', 'sanitize_title' ); |
||
| 141 | return $pagenow && $pagenow == 'admin-ajax.php' && $action == 'frm_forms_preview'; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Check for ajax except the form preview page |
||
| 146 | * |
||
| 147 | * @since 2.0 |
||
| 148 | * |
||
| 149 | * @param None |
||
| 150 | * @return boolean |
||
| 151 | */ |
||
| 152 | public static function doing_ajax() { |
||
| 153 | return defined('DOING_AJAX') && DOING_AJAX && ! self::is_preview_page(); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @since 2.0.8 |
||
| 158 | */ |
||
| 159 | public static function prevent_caching() { |
||
| 160 | global $frm_vars; |
||
| 161 | return isset( $frm_vars['prevent_caching'] ) && $frm_vars['prevent_caching']; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Check if on an admin page |
||
| 166 | * |
||
| 167 | * @since 2.0 |
||
| 168 | * |
||
| 169 | * @param None |
||
| 170 | * @return boolean |
||
| 171 | */ |
||
| 172 | public static function is_admin() { |
||
| 173 | return is_admin() && ( ! defined('DOING_AJAX') || ! DOING_AJAX ); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Check if value contains blank value or empty array |
||
| 178 | * |
||
| 179 | * @since 2.0 |
||
| 180 | * @param $value - value to check |
||
| 181 | * @return boolean |
||
| 182 | */ |
||
| 183 | public static function is_empty_value( $value, $empty = '' ) { |
||
| 184 | return ( is_array( $value ) && empty( $value ) ) || $value == $empty; |
||
| 185 | } |
||
| 186 | |||
| 187 | public static function is_not_empty_value( $value, $empty = '' ) { |
||
| 188 | return ! self::is_empty_value( $value, $empty ); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get any value from the $_SERVER |
||
| 193 | * |
||
| 194 | * @since 2.0 |
||
| 195 | * @param string $value |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public static function get_server_value( $value ) { |
||
| 199 | return isset( $_SERVER[ $value ] ) ? wp_strip_all_tags( $_SERVER[ $value ] ) : ''; |
||
|
|
|||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Check for the IP address in several places |
||
| 204 | * Used by [ip] shortcode |
||
| 205 | * |
||
| 206 | * @return string The IP address of the current user |
||
| 207 | */ |
||
| 208 | public static function get_ip_address() { |
||
| 209 | $ip = ''; |
||
| 210 | foreach ( array( |
||
| 211 | 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', |
||
| 212 | 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR', |
||
| 213 | ) as $key ) { |
||
| 214 | if ( ! isset( $_SERVER[ $key ] ) ) { |
||
| 215 | continue; |
||
| 216 | } |
||
| 217 | |||
| 218 | foreach ( explode( ',', $_SERVER[ $key ] ) as $ip ) { |
||
| 219 | $ip = trim($ip); // just to be safe |
||
| 220 | |||
| 221 | if ( filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false ) { |
||
| 222 | return $ip; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | return sanitize_text_field( $ip ); |
||
| 228 | } |
||
| 229 | |||
| 230 | public static function get_param( $param, $default = '', $src = 'get', $sanitize = '' ) { |
||
| 231 | if ( strpos($param, '[') ) { |
||
| 232 | $params = explode('[', $param); |
||
| 233 | $param = $params[0]; |
||
| 234 | } |
||
| 235 | |||
| 236 | if ( $src == 'get' ) { |
||
| 237 | $value = isset( $_POST[ $param ] ) ? stripslashes_deep( $_POST[ $param ] ) : ( isset( $_GET[ $param ] ) ? stripslashes_deep( $_GET[ $param ] ) : $default ); |
||
| 238 | if ( ! isset( $_POST[ $param ] ) && isset( $_GET[ $param ] ) && ! is_array( $value ) ) { |
||
| 239 | $value = stripslashes_deep( htmlspecialchars_decode( urldecode( $_GET[ $param ] ) ) ); |
||
| 240 | } |
||
| 241 | self::sanitize_value( $sanitize, $value ); |
||
| 242 | } else { |
||
| 243 | $value = self::get_simple_request( array( 'type' => $src, 'param' => $param, 'default' => $default, 'sanitize' => $sanitize ) ); |
||
| 244 | } |
||
| 245 | |||
| 246 | if ( isset( $params ) && is_array( $value ) && ! empty( $value ) ) { |
||
| 247 | foreach ( $params as $k => $p ) { |
||
| 248 | if ( ! $k || ! is_array($value) ) { |
||
| 249 | continue; |
||
| 250 | } |
||
| 251 | |||
| 252 | $p = trim($p, ']'); |
||
| 253 | $value = isset( $value[ $p ] ) ? $value[ $p ] : $default; |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | return $value; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * |
||
| 262 | * @param string $param |
||
| 263 | * @param mixed $default |
||
| 264 | * @param string $sanitize |
||
| 265 | */ |
||
| 266 | public static function get_post_param( $param, $default = '', $sanitize = '' ) { |
||
| 267 | return self::get_simple_request( array( 'type' => 'post', 'param' => $param, 'default' => $default, 'sanitize' => $sanitize ) ); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @since 2.0 |
||
| 272 | * |
||
| 273 | * @param string $param |
||
| 274 | * @param string $sanitize |
||
| 275 | * @param string $default |
||
| 276 | */ |
||
| 277 | public static function simple_get( $param, $sanitize = 'sanitize_text_field', $default = '' ) { |
||
| 278 | return self::get_simple_request( array( 'type' => 'get', 'param' => $param, 'default' => $default, 'sanitize' => $sanitize ) ); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Get a GET/POST/REQUEST value and sanitize it |
||
| 283 | * |
||
| 284 | * @since 2.0.6 |
||
| 285 | */ |
||
| 286 | public static function get_simple_request( $args ) { |
||
| 287 | $defaults = array( |
||
| 288 | 'param' => '', 'default' => '', |
||
| 289 | 'type' => 'get', 'sanitize' => 'sanitize_text_field', |
||
| 290 | ); |
||
| 291 | $args = wp_parse_args( $args, $defaults ); |
||
| 292 | |||
| 293 | $value = $args['default']; |
||
| 294 | if ( $args['type'] == 'get' ) { |
||
| 295 | if ( $_GET && isset( $_GET[ $args['param'] ] ) ) { |
||
| 296 | $value = $_GET[ $args['param'] ]; |
||
| 297 | } |
||
| 298 | } else if ( $args['type'] == 'post' ) { |
||
| 299 | if ( isset( $_POST[ $args['param'] ] ) ) { |
||
| 300 | $value = stripslashes_deep( maybe_unserialize( $_POST[ $args['param'] ] ) ); |
||
| 301 | } |
||
| 302 | } else { |
||
| 303 | if ( isset( $_REQUEST[ $args['param'] ] ) ) { |
||
| 304 | $value = $_REQUEST[ $args['param'] ]; |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | self::sanitize_value( $args['sanitize'], $value ); |
||
| 309 | return $value; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Preserve backslashes in a value, but make sure value doesn't get compounding slashes |
||
| 314 | * |
||
| 315 | * @since 2.0.8 |
||
| 316 | * @param string $value |
||
| 317 | * @return string $value |
||
| 318 | */ |
||
| 319 | public static function preserve_backslashes( $value ) { |
||
| 320 | // If backslashes have already been added, don't add them again |
||
| 321 | if ( strpos( $value, '\\\\' ) === false ) { |
||
| 322 | $value = addslashes( $value ); |
||
| 323 | } |
||
| 324 | return $value; |
||
| 325 | } |
||
| 326 | |||
| 327 | public static function sanitize_value( $sanitize, &$value ) { |
||
| 328 | if ( ! empty( $sanitize ) ) { |
||
| 329 | if ( is_array( $value ) ) { |
||
| 330 | $temp_values = $value; |
||
| 331 | foreach ( $temp_values as $k => $v ) { |
||
| 332 | FrmAppHelper::sanitize_value( $sanitize, $value[ $k ] ); |
||
| 333 | } |
||
| 334 | } else { |
||
| 335 | $value = call_user_func( $sanitize, $value ); |
||
| 336 | } |
||
| 337 | } |
||
| 338 | } |
||
| 339 | |||
| 340 | public static function sanitize_request( $sanitize_method, &$values ) { |
||
| 341 | $temp_values = $values; |
||
| 342 | foreach ( $temp_values as $k => $val ) { |
||
| 343 | if ( isset( $sanitize_method[ $k ] ) ) { |
||
| 344 | $values[ $k ] = call_user_func( $sanitize_method[ $k ], $val ); |
||
| 345 | } |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | public static function sanitize_array( &$values ) { |
||
| 350 | $temp_values = $values; |
||
| 351 | foreach ( $temp_values as $k => $val ) { |
||
| 352 | $values[ $k ] = wp_kses_post( $val ); |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Sanitize the value, and allow some HTML |
||
| 358 | * @since 2.0 |
||
| 359 | */ |
||
| 360 | public static function kses( $value, $allowed = array() ) { |
||
| 361 | $html = array( |
||
| 362 | 'a' => array( |
||
| 363 | 'href' => array(), |
||
| 364 | 'title' => array(), |
||
| 365 | 'id' => array(), |
||
| 366 | 'class' => array(), |
||
| 367 | ), |
||
| 368 | ); |
||
| 369 | |||
| 370 | $allowed_html = array(); |
||
| 371 | foreach ( $allowed as $a ) { |
||
| 372 | $allowed_html[ $a ] = isset( $html[ $a ] ) ? $html[ $a ] : array(); |
||
| 373 | } |
||
| 374 | |||
| 375 | return wp_kses( $value, $allowed_html ); |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Used when switching the action for a bulk action |
||
| 380 | * @since 2.0 |
||
| 381 | */ |
||
| 382 | public static function remove_get_action() { |
||
| 383 | if ( ! isset($_GET) ) { |
||
| 384 | return; |
||
| 385 | } |
||
| 386 | |||
| 387 | $new_action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : ( isset( $_GET['action2'] ) ? sanitize_text_field( $_GET['action2'] ) : '' ); |
||
| 388 | if ( ! empty( $new_action ) ) { |
||
| 389 | $_SERVER['REQUEST_URI'] = str_replace( '&action=' . $new_action, '', FrmAppHelper::get_server_value( 'REQUEST_URI' ) ); |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Check the WP query for a parameter |
||
| 395 | * |
||
| 396 | * @since 2.0 |
||
| 397 | * @return string|array |
||
| 398 | */ |
||
| 399 | public static function get_query_var( $value, $param ) { |
||
| 400 | if ( $value != '' ) { |
||
| 401 | return $value; |
||
| 402 | } |
||
| 403 | |||
| 404 | global $wp_query; |
||
| 405 | if ( isset( $wp_query->query_vars[ $param ] ) ) { |
||
| 406 | $value = $wp_query->query_vars[ $param ]; |
||
| 407 | } |
||
| 408 | |||
| 409 | return $value; |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @param string $type |
||
| 414 | */ |
||
| 415 | public static function trigger_hook_load( $type, $object = null ) { |
||
| 416 | // only load the form hooks once |
||
| 417 | $hooks_loaded = apply_filters( 'frm_' . $type . '_hooks_loaded', false, $object ); |
||
| 418 | if ( ! $hooks_loaded ) { |
||
| 419 | do_action( 'frm_load_' . $type . '_hooks' ); |
||
| 420 | } |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Check cache before fetching values and saving to cache |
||
| 425 | * |
||
| 426 | * @since 2.0 |
||
| 427 | * |
||
| 428 | * @param string $cache_key The unique name for this cache |
||
| 429 | * @param string $group The name of the cache group |
||
| 430 | * @param string $query If blank, don't run a db call |
||
| 431 | * @param string $type The wpdb function to use with this query |
||
| 432 | * @return mixed $results The cache or query results |
||
| 433 | */ |
||
| 434 | public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) { |
||
| 435 | $results = wp_cache_get($cache_key, $group); |
||
| 436 | if ( ! self::is_empty_value( $results, false ) || empty($query) ) { |
||
| 437 | return $results; |
||
| 438 | } |
||
| 439 | |||
| 440 | if ( 'get_posts' == $type ) { |
||
| 441 | $results = get_posts($query); |
||
| 442 | } else { |
||
| 443 | global $wpdb; |
||
| 444 | $results = $wpdb->{$type}($query); |
||
| 445 | } |
||
| 446 | |||
| 447 | if ( ! self::prevent_caching() ) { |
||
| 448 | wp_cache_set( $cache_key, $results, $group, $time ); |
||
| 449 | } |
||
| 450 | |||
| 451 | return $results; |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Data that should be stored for a long time can be stored in a transient. |
||
| 456 | * First check the cache, then check the transient |
||
| 457 | * @since 2.0 |
||
| 458 | * @return mixed The cached value or false |
||
| 459 | */ |
||
| 460 | public static function check_cache_and_transient( $cache_key ) { |
||
| 461 | // check caching layer first |
||
| 462 | $results = self::check_cache( $cache_key ); |
||
| 463 | if ( $results ) { |
||
| 464 | return $results; |
||
| 465 | } |
||
| 466 | |||
| 467 | // then check the transient |
||
| 468 | $results = get_transient($cache_key); |
||
| 469 | if ( $results ) { |
||
| 470 | wp_cache_set($cache_key, $results); |
||
| 471 | } |
||
| 472 | |||
| 473 | return $results; |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @since 2.0 |
||
| 478 | * @param string $cache_key |
||
| 479 | */ |
||
| 480 | public static function delete_cache_and_transient( $cache_key ) { |
||
| 481 | delete_transient($cache_key); |
||
| 482 | wp_cache_delete($cache_key); |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Delete all caching in a single group |
||
| 487 | * |
||
| 488 | * @since 2.0 |
||
| 489 | * |
||
| 490 | * @param string $group The name of the cache group |
||
| 491 | * @return boolean True or False |
||
| 492 | */ |
||
| 493 | public static function cache_delete_group( $group ) { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Check a value from a shortcode to see if true or false. |
||
| 519 | * True when value is 1, true, 'true', 'yes' |
||
| 520 | * |
||
| 521 | * @since 1.07.10 |
||
| 522 | * |
||
| 523 | * @param string $value The value to compare |
||
| 524 | * @return boolean True or False |
||
| 525 | */ |
||
| 526 | public static function is_true( $value ) { |
||
| 527 | return ( true === $value || 1 == $value || 'true' == $value || 'yes' == $value ); |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Used to filter shortcode in text widgets |
||
| 532 | */ |
||
| 533 | public static function widget_text_filter_callback( $matches ) { |
||
| 534 | return do_shortcode( $matches[0] ); |
||
| 535 | } |
||
| 536 | |||
| 537 | public static function get_pages() { |
||
| 538 | return get_posts( array( 'post_type' => 'page', 'post_status' => array( 'publish', 'private' ), 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC' ) ); |
||
| 539 | } |
||
| 540 | |||
| 541 | public static function wp_pages_dropdown( $field_name, $page_id, $truncate = false ) { |
||
| 542 | $pages = self::get_pages(); |
||
| 543 | $selected = self::get_post_param( $field_name, $page_id, 'absint' ); |
||
| 544 | ?> |
||
| 545 | <select name="<?php echo esc_attr($field_name); ?>" id="<?php echo esc_attr($field_name); ?>" class="frm-pages-dropdown"> |
||
| 546 | <option value=""> </option> |
||
| 547 | <?php foreach ( $pages as $page ) { ?> |
||
| 548 | <option value="<?php echo esc_attr($page->ID); ?>" <?php selected( $selected, $page->ID ) ?>> |
||
| 549 | <?php echo esc_html( $truncate ? self::truncate( $page->post_title, $truncate ) : $page->post_title ); ?> |
||
| 550 | </option> |
||
| 551 | <?php } ?> |
||
| 552 | </select> |
||
| 553 | <?php |
||
| 554 | } |
||
| 555 | |||
| 556 | public static function post_edit_link( $post_id ) { |
||
| 557 | $post = get_post($post_id); |
||
| 564 | |||
| 565 | public static function wp_roles_dropdown( $field_name, $capability, $multiple = 'single' ) { |
||
| 574 | |||
| 575 | public static function roles_options( $capability ) { |
||
| 591 | |||
| 592 | public static function frm_capabilities( $type = 'auto' ) { |
||
| 613 | |||
| 614 | public static function user_has_permission( $needed_role ) { |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Make sure administrators can see Formidable menu |
||
| 638 | * |
||
| 639 | * @since 2.0 |
||
| 640 | */ |
||
| 641 | public static function maybe_add_permissions() { |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Make sure admins have permission to see the menu items |
||
| 659 | * @since 2.0.6 |
||
| 660 | */ |
||
| 661 | public static function force_capability( $cap = 'frm_change_settings' ) { |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Check if the user has permision for action. |
||
| 673 | * Return permission message and stop the action if no permission |
||
| 674 | * @since 2.0 |
||
| 675 | * @param string $permission |
||
| 676 | */ |
||
| 677 | public static function permission_check( $permission, $show_message = 'show' ) { |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Check user permission and nonce |
||
| 689 | * @since 2.0 |
||
| 690 | * @param string $permission |
||
| 691 | * @return false|string The permission message or false if allowed |
||
| 692 | */ |
||
| 693 | public static function permission_nonce_error( $permission, $nonce_name = '', $nonce = '' ) { |
||
| 711 | |||
| 712 | public static function checked( $values, $current ) { |
||
| 717 | |||
| 718 | public static function check_selected( $values, $current ) { |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Check if current field option is an "other" option |
||
| 728 | * |
||
| 729 | * @since 2.0 |
||
| 730 | * |
||
| 731 | * @param string $opt_key |
||
| 732 | * @return boolean Returns true if current field option is an "Other" option |
||
| 733 | */ |
||
| 734 | public static function is_other_opt( $opt_key ) { |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Get value that belongs in "Other" text box |
||
| 741 | * |
||
| 742 | * @since 2.0 |
||
| 743 | * |
||
| 744 | * @param string $opt_key |
||
| 745 | * @param array $field |
||
| 746 | * @return string $other_val |
||
| 747 | */ |
||
| 748 | public static function get_other_val( $opt_key, $field, $parent = false, $pointer = false ) { |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Check if there is a saved value for the "Other" text field. If so, set it as the $other_val. |
||
| 755 | * Intended for front-end use |
||
| 756 | * |
||
| 757 | * @since 2.0 |
||
| 758 | * |
||
| 759 | * @param array $field |
||
| 760 | * @param boolean $other_opt |
||
| 761 | * @param string $checked |
||
| 762 | * @param array $args should include opt_key and field name |
||
| 763 | * @return string $other_val |
||
| 764 | */ |
||
| 765 | public static function prepare_other_input( $field, &$other_opt, &$checked, $args = array() ) { |
||
| 770 | |||
| 771 | public static function recursive_function_map( $value, $function ) { |
||
| 794 | |||
| 795 | public static function is_assoc( $array ) { |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Flatten a multi-dimensional array |
||
| 801 | */ |
||
| 802 | public static function array_flatten( $array, $keys = 'keep' ) { |
||
| 817 | |||
| 818 | public static function esc_textarea( $text, $is_rich_text = false ) { |
||
| 826 | |||
| 827 | /** |
||
| 828 | * Add auto paragraphs to text areas |
||
| 829 | * @since 2.0 |
||
| 830 | */ |
||
| 831 | public static function use_wpautop( $content ) { |
||
| 837 | |||
| 838 | public static function replace_quotes( $val ) { |
||
| 845 | |||
| 846 | /** |
||
| 847 | * @since 2.0 |
||
| 848 | * @return string The base Google APIS url for the current version of jQuery UI |
||
| 849 | */ |
||
| 850 | public static function jquery_ui_base_url() { |
||
| 855 | |||
| 856 | /** |
||
| 857 | * @param string $handle |
||
| 858 | */ |
||
| 859 | public static function script_version( $handle ) { |
||
| 878 | |||
| 879 | public static function js_redirect( $url ) { |
||
| 882 | |||
| 883 | public static function get_user_id_param( $user_id ) { |
||
| 905 | |||
| 906 | public static function get_file_contents( $filename, $atts = array() ) { |
||
| 918 | |||
| 919 | /** |
||
| 920 | * @param string $table_name |
||
| 921 | * @param string $column |
||
| 922 | * @param int $id |
||
| 923 | * @param int $num_chars |
||
| 924 | */ |
||
| 925 | public static function get_unique_key( $name = '', $table_name, $column, $id = 0, $num_chars = 5 ) { |
||
| 955 | |||
| 956 | /** |
||
| 957 | * Editing a Form or Entry |
||
| 958 | * @param string $table |
||
| 959 | * @return bool|array |
||
| 960 | */ |
||
| 961 | public static function setup_edit_vars( $record, $table, $fields = '', $default = false, $post_values = array(), $args = array() ) { |
||
| 1001 | |||
| 1002 | private static function fill_field_defaults( $field, $record, array &$values, $args ) { |
||
| 1051 | |||
| 1052 | private static function fill_field_opts( $field, array &$field_array, $args ) { |
||
| 1073 | |||
| 1074 | /** |
||
| 1075 | * @param string $table |
||
| 1076 | */ |
||
| 1077 | private static function fill_form_opts( $record, $table, $post_values, array &$values ) { |
||
| 1102 | |||
| 1103 | /** |
||
| 1104 | * Set to POST value or default |
||
| 1105 | */ |
||
| 1106 | private static function fill_form_defaults( $post_values, array &$values ) { |
||
| 1129 | |||
| 1130 | public static function get_meta_value( $field_id, $entry ) { |
||
| 1134 | |||
| 1135 | public static function insert_opt_html( $args ) { |
||
| 1148 | |||
| 1149 | public static function truncate( $str, $length, $minword = 3, $continue = '...' ) { |
||
| 1189 | |||
| 1190 | public static function mb_function( $function_names, $args ) { |
||
| 1198 | |||
| 1199 | public static function get_formatted_time( $date, $date_format = '', $time_format = '' ) { |
||
| 1222 | |||
| 1223 | private static function add_time_to_date( $time_format, $date ) { |
||
| 1236 | |||
| 1237 | /** |
||
| 1238 | * @since 2.0.8 |
||
| 1239 | */ |
||
| 1240 | public static function get_localized_date( $date_format, $date ) { |
||
| 1244 | |||
| 1245 | /** |
||
| 1246 | * Gets the time ago in words |
||
| 1247 | * |
||
| 1248 | * @param int $from in seconds |
||
| 1249 | * @param int|string $to in seconds |
||
| 1250 | * @return string $time_ago |
||
| 1251 | */ |
||
| 1252 | public static function human_time_diff( $from, $to = '' ) { |
||
| 1283 | |||
| 1284 | /** |
||
| 1285 | * Get the translatable time strings |
||
| 1286 | * |
||
| 1287 | * @since 2.0.20 |
||
| 1288 | * @return array |
||
| 1289 | */ |
||
| 1290 | private static function get_time_strings() { |
||
| 1301 | |||
| 1302 | /** |
||
| 1303 | * Added for < WP 4.0 compatability |
||
| 1304 | * |
||
| 1305 | * @since 1.07.10 |
||
| 1306 | * |
||
| 1307 | * @param string $term The value to escape |
||
| 1308 | * @return string The escaped value |
||
| 1309 | */ |
||
| 1310 | public static function esc_like( $term ) { |
||
| 1321 | |||
| 1322 | /** |
||
| 1323 | * @param string $order_query |
||
| 1324 | */ |
||
| 1325 | public static function esc_order( $order_query ) { |
||
| 1357 | |||
| 1358 | /** |
||
| 1359 | * Make sure this is ordering by either ASC or DESC |
||
| 1360 | */ |
||
| 1361 | public static function esc_order_by( &$order_by ) { |
||
| 1367 | |||
| 1368 | /** |
||
| 1369 | * @param string $limit |
||
| 1370 | */ |
||
| 1371 | public static function esc_limit( $limit ) { |
||
| 1391 | |||
| 1392 | /** |
||
| 1393 | * Get an array of values ready to go through $wpdb->prepare |
||
| 1394 | * @since 2.0 |
||
| 1395 | */ |
||
| 1396 | public static function prepare_array_values( $array, $type = '%s' ) { |
||
| 1400 | |||
| 1401 | public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) { |
||
| 1416 | |||
| 1417 | // Pagination Methods |
||
| 1418 | |||
| 1419 | /** |
||
| 1420 | * @param integer $current_p |
||
| 1421 | */ |
||
| 1422 | public static function get_last_record_num( $r_count, $current_p, $p_size ) { |
||
| 1425 | |||
| 1426 | /** |
||
| 1427 | * @param integer $current_p |
||
| 1428 | */ |
||
| 1429 | public static function get_first_record_num( $r_count, $current_p, $p_size ) { |
||
| 1436 | |||
| 1437 | /** |
||
| 1438 | * @return array |
||
| 1439 | */ |
||
| 1440 | public static function json_to_array( $json_vars ) { |
||
| 1491 | |||
| 1492 | /** |
||
| 1493 | * @param string $name |
||
| 1494 | * @param string $l1 |
||
| 1495 | */ |
||
| 1496 | public static function add_value_to_array( $name, $l1, $val, &$vars ) { |
||
| 1503 | |||
| 1504 | public static function maybe_add_tooltip( $name, $class = 'closed', $form_name = '' ) { |
||
| 1531 | |||
| 1532 | /** |
||
| 1533 | * Add the current_page class to that page in the form nav |
||
| 1534 | */ |
||
| 1535 | public static function select_current_page( $page, $current_page, $action = array() ) { |
||
| 1545 | |||
| 1546 | /** |
||
| 1547 | * Prepare and json_encode post content |
||
| 1548 | * |
||
| 1549 | * @since 2.0 |
||
| 1550 | * |
||
| 1551 | * @param array $post_content |
||
| 1552 | * @return string $post_content ( json encoded array ) |
||
| 1553 | */ |
||
| 1554 | public static function prepare_and_encode( $post_content ) { |
||
| 1575 | |||
| 1576 | private static function prepare_action_slashes( $val, $key, &$post_content ) { |
||
| 1594 | |||
| 1595 | /** |
||
| 1596 | * Prepare and save settings in styles and actions |
||
| 1597 | * |
||
| 1598 | * @param array $settings |
||
| 1599 | * @param string $group |
||
| 1600 | * |
||
| 1601 | * @since 2.0.6 |
||
| 1602 | */ |
||
| 1603 | public static function save_settings( $settings, $group ) { |
||
| 1616 | |||
| 1617 | /** |
||
| 1618 | * Since actions are JSON encoded, we don't want any filters messing with it. |
||
| 1619 | * Remove the filters and then add them back in case any posts or views are |
||
| 1620 | * also being imported. |
||
| 1621 | * |
||
| 1622 | * Used when saving form actions and styles |
||
| 1623 | * |
||
| 1624 | * @since 2.0.4 |
||
| 1625 | */ |
||
| 1626 | public static function save_json_post( $settings ) { |
||
| 1640 | |||
| 1641 | public static function maybe_json_decode( $string ) { |
||
| 1658 | |||
| 1659 | /** |
||
| 1660 | * @since 1.07.10 |
||
| 1661 | * |
||
| 1662 | * @param string $post_type The name of the post type that may need to be highlighted |
||
| 1663 | * echo The javascript to open and highlight the Formidable menu |
||
| 1664 | */ |
||
| 1665 | public static function maybe_highlight_menu( $post_type ) { |
||
| 1679 | |||
| 1680 | /** |
||
| 1681 | * Load the JS file on non-Formidable pages in the admin area |
||
| 1682 | * @since 2.0 |
||
| 1683 | */ |
||
| 1684 | public static function load_admin_wide_js( $load = true ) { |
||
| 1700 | |||
| 1701 | /** |
||
| 1702 | * @since 2.0.9 |
||
| 1703 | */ |
||
| 1704 | public static function load_font_style() { |
||
| 1707 | |||
| 1708 | /** |
||
| 1709 | * @param string $location |
||
| 1710 | */ |
||
| 1711 | public static function localize_script( $location ) { |
||
| 1760 | |||
| 1761 | /** |
||
| 1762 | * echo the message on the plugins listing page |
||
| 1763 | * @since 1.07.10 |
||
| 1764 | * |
||
| 1765 | * @param float $min_version The version the add-on requires |
||
| 1766 | */ |
||
| 1767 | public static function min_version_notice( $min_version ) { |
||
| 1780 | |||
| 1781 | public static function locales( $type = 'date' ) { |
||
| 1838 | } |
||
| 1839 |