| 1 |
<?php |
| 2 |
if(!defined('ABSPATH')) die('You are not allowed to call this page directly.'); |
| 3 |
|
| 4 |
if(class_exists('FrmAppHelper')) |
| 5 |
return; |
| 6 |
|
| 7 |
class FrmAppHelper{ |
| 8 |
public static $db_version = 11; //version of the database we are moving to (skip 12) |
| 9 |
public static $pro_db_version = 25; |
| 10 |
|
| 11 |
/* |
| 12 |
* @since 1.07.02 |
| 13 |
* |
| 14 |
* @param none |
| 15 |
* @return float The version of this plugin |
| 16 |
*/ |
| 17 |
public static function plugin_version(){ |
| 18 |
$plugin_data = get_file_data( WP_PLUGIN_DIR .'/formidable/formidable.php', array('Version' => 'Version'), 'plugin' ); |
| 19 |
return $plugin_data['Version']; |
| 20 |
} |
| 21 |
|
| 22 |
public static function plugin_path(){ |
| 23 |
return dirname(dirname(dirname(__FILE__))); |
| 24 |
} |
| 25 |
|
| 26 |
public static function plugin_url($url=''){ |
| 27 |
//prevously FRM_URL constant |
| 28 |
if(empty($url)) |
| 29 |
$url = plugins_url('', 'formidable/formidable.php'); |
| 30 |
if(is_ssl() and !preg_match('/^https:\/\/.*\..*$/', $url)) |
| 31 |
$url = str_replace('http://', 'https://', $url); |
| 32 |
|
| 33 |
return $url; |
| 34 |
} |
| 35 |
|
| 36 |
public static function site_url(){ |
| 37 |
$url = self::plugin_url(site_url()); |
| 38 |
return $url; |
| 39 |
} |
| 40 |
|
| 41 |
public static function get_param($param, $default='', $src='get'){ |
| 42 |
if(strpos($param, '[')){ |
| 43 |
$params = explode('[', $param); |
| 44 |
$param = $params[0]; |
| 45 |
} |
| 46 |
|
| 47 |
if($src == 'get'){ |
| 48 |
$value = (isset($_POST[$param]) ? stripslashes_deep($_POST[$param]) : (isset($_GET[$param]) ? stripslashes_deep($_GET[$param]) : $default)); |
| 49 |
if(!isset($_POST[$param]) and isset($_GET[$param]) and !is_array($value)) |
| 50 |
$value = stripslashes_deep(htmlspecialchars_decode(urldecode($_GET[$param]))); |
| 51 |
}else{ |
| 52 |
$value = isset($_POST[$param]) ? stripslashes_deep(maybe_unserialize($_POST[$param])) : $default; |
| 53 |
} |
| 54 |
|
| 55 |
if(isset($params) and is_array($value) and !empty($value)){ |
| 56 |
foreach($params as $k => $p){ |
| 57 |
if(!$k or !is_array($value)) |
| 58 |
continue; |
| 59 |
|
| 60 |
$p = trim($p, ']'); |
| 61 |
$value = (isset($value[$p])) ? $value[$p] : $default; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return $value; |
| 66 |
} |
| 67 |
|
| 68 |
public static function get_post_param($param, $default=''){ |
| 69 |
return isset($_POST[$param]) ? stripslashes_deep(maybe_unserialize($_POST[$param])) : $default; |
| 70 |
} |
| 71 |
|
| 72 |
/* |
| 73 |
* Check a value from a shortcode to see if true or false. |
| 74 |
* True when value is 1, true, 'true', 'yes' |
| 75 |
* |
| 76 |
* @since 1.07.10 |
| 77 |
* |
| 78 |
* @param string $value The value to compare |
| 79 |
* @return boolean True or False |
| 80 |
*/ |
| 81 |
public static function is_true($value) { |
| 82 |
return ( true === $value || 1 == $value || 'true' == $value || 'yes' == $value ); |
| 83 |
} |
| 84 |
|
| 85 |
public static function load_scripts($scripts){ |
| 86 |
foreach ( (array) $scripts as $s ) { |
| 87 |
wp_enqueue_script($s); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public static function load_styles($styles){ |
| 92 |
foreach ( (array) $styles as $s ) { |
| 93 |
wp_enqueue_style($s); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public static function get_pages(){ |
| 98 |
return get_posts( array('post_type' => 'page', 'post_status' => array('publish', 'private'), 'numberposts' => 999, 'orderby' => 'title', 'order' => 'ASC')); |
| 99 |
} |
| 100 |
|
| 101 |
public static function wp_pages_dropdown($field_name, $page_id, $truncate=false){ |
| 102 |
$pages = FrmAppHelper::get_pages(); |
| 103 |
?> |
| 104 |
<select name="<?php echo $field_name; ?>" id="<?php echo $field_name; ?>" class="frm-pages-dropdown"> |
| 105 |
<option value=""></option> |
| 106 |
<?php foreach($pages as $page){ ?> |
| 107 |
<option value="<?php echo $page->ID; ?>" <?php echo (((isset($_POST[$field_name]) and $_POST[$field_name] == $page->ID) or (!isset($_POST[$field_name]) and $page_id == $page->ID))?' selected="selected"':''); ?>><?php echo ($truncate)? FrmAppHelper::truncate($page->post_title, $truncate) : $page->post_title; ?> </option> |
| 108 |
<?php } ?> |
| 109 |
</select> |
| 110 |
<?php |
| 111 |
} |
| 112 |
|
| 113 |
public static function wp_roles_dropdown($field_name, $capability){ |
| 114 |
global $frm_vars; |
| 115 |
$field_value = FrmAppHelper::get_param($field_name); |
| 116 |
if(!isset($frm_vars['editable_roles']) or !$frm_vars['editable_roles']) |
| 117 |
$frm_vars['editable_roles'] = get_editable_roles(); |
| 118 |
|
| 119 |
?> |
| 120 |
<select name="<?php echo $field_name; ?>" id="<?php echo $field_name; ?>" class="frm-pages-dropdown"> |
| 121 |
<?php foreach($frm_vars['editable_roles'] as $role => $details){ |
| 122 |
$name = translate_user_role($details['name'] ); ?> |
| 123 |
<option value="<?php echo esc_attr($role) ?>" <?php echo (((isset($_POST[$field_name]) and $_POST[$field_name] == $role) or (!isset($_POST[$field_name]) and $capability == $role))?' selected="selected"':''); ?>><?php echo $name ?> </option> |
| 124 |
<?php |
| 125 |
unset($role); |
| 126 |
unset($details); |
| 127 |
} ?> |
| 128 |
</select> |
| 129 |
<?php |
| 130 |
} |
| 131 |
|
| 132 |
public static function frm_capabilities($type = 'auto'){ |
| 133 |
global $frm_vars; |
| 134 |
$cap = array( |
| 135 |
'frm_view_forms' => __('View Forms and Templates', 'formidable'), |
| 136 |
'frm_edit_forms' => __('Add/Edit Forms and Templates', 'formidable'), |
| 137 |
'frm_delete_forms' => __('Delete Forms and Templates', 'formidable'), |
| 138 |
'frm_change_settings' => __('Access this Settings Page', 'formidable') |
| 139 |
); |
| 140 |
if ( $frm_vars['pro_is_installed'] || 'pro' == $type ) { |
| 141 |
$cap['frm_view_entries'] = __('View Entries from Admin Area', 'formidable'); |
| 142 |
$cap['frm_create_entries'] = __('Add Entries from Admin Area', 'formidable'); |
| 143 |
$cap['frm_edit_entries'] = __('Edit Entries from Admin Area', 'formidable'); |
| 144 |
$cap['frm_delete_entries'] = __('Delete Entries from Admin Area', 'formidable'); |
| 145 |
$cap['frm_view_reports'] = __('View Reports', 'formidable'); |
| 146 |
$cap['frm_edit_displays'] = __('Add/Edit Views', 'formidable'); |
| 147 |
} |
| 148 |
return $cap; |
| 149 |
} |
| 150 |
|
| 151 |
public static function user_has_permission($needed_role){ |
| 152 |
if($needed_role == '-1') |
| 153 |
return false; |
| 154 |
|
| 155 |
if($needed_role == '' or current_user_can($needed_role)) |
| 156 |
return true; |
| 157 |
|
| 158 |
$roles = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ); |
| 159 |
foreach ($roles as $role){ |
| 160 |
if (current_user_can($role)) |
| 161 |
return true; |
| 162 |
if ($role == $needed_role) |
| 163 |
break; |
| 164 |
} |
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
public static function checked($values, $current){ |
| 169 |
if(FrmAppHelper::check_selected($values, $current)) |
| 170 |
echo ' checked="checked"'; |
| 171 |
} |
| 172 |
|
| 173 |
public static function check_selected($values, $current){ |
| 174 |
//if(is_array($current)) |
| 175 |
// $current = (isset($current['value'])) ? $current['value'] : $current['label']; |
| 176 |
|
| 177 |
if(is_array($values)) |
| 178 |
$values = array_map(array('FrmAppHelper', 'recursive_trim'), $values); |
| 179 |
else |
| 180 |
$values = trim($values); |
| 181 |
$current = trim($current); |
| 182 |
|
| 183 |
/*if(is_array($values)) |
| 184 |
$values = array_map('htmlentities', $values); |
| 185 |
else |
| 186 |
$values = htmlentities($values); |
| 187 |
|
| 188 |
$values = preg_replace("/&#?[a-z0-9]{2,8};/i", "", $values); |
| 189 |
$current = preg_replace("/&#?[a-z0-9]{2,8};/i", "", $current); |
| 190 |
*/ |
| 191 |
|
| 192 |
if((is_array($values) && in_array($current, $values)) or (!is_array($values) and $values == $current)) |
| 193 |
return true; |
| 194 |
else |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
public static function recursive_trim(&$value) { |
| 199 |
if (is_array($value)) |
| 200 |
$value = array_map(array('FrmAppHelper', 'recursive_trim'), $value); |
| 201 |
else |
| 202 |
$value = trim($value); |
| 203 |
|
| 204 |
return $value; |
| 205 |
} |
| 206 |
|
| 207 |
public static function esc_textarea( $text ) { |
| 208 |
$safe_text = str_replace('"', '"', $text); |
| 209 |
$safe_text = htmlspecialchars( $safe_text, ENT_NOQUOTES ); |
| 210 |
return apply_filters( 'esc_textarea', $safe_text, $text ); |
| 211 |
} |
| 212 |
|
| 213 |
public static function replace_quotes($val){ |
| 214 |
$val = str_replace(array('“', '”', '‘', '’'), array('"', '"', "'", "'"), $val); |
| 215 |
return $val; |
| 216 |
} |
| 217 |
|
| 218 |
public static function script_version($handle, $list='scripts'){ |
| 219 |
global $wp_scripts; |
| 220 |
if(!$wp_scripts) |
| 221 |
return false; |
| 222 |
|
| 223 |
$ver = 0; |
| 224 |
|
| 225 |
if ( isset($wp_scripts->registered[$handle]) ) |
| 226 |
$query = $wp_scripts->registered[$handle]; |
| 227 |
|
| 228 |
if ( is_object( $query ) ) |
| 229 |
$ver = $query->ver; |
| 230 |
|
| 231 |
return $ver; |
| 232 |
} |
| 233 |
|
| 234 |
public static function js_redirect($url){ |
| 235 |
return '<script type="text/javascript">window.location="'. $url .'"</script>'; |
| 236 |
} |
| 237 |
|
| 238 |
public static function get_file_contents($filename){ |
| 239 |
if (is_file($filename)){ |
| 240 |
ob_start(); |
| 241 |
include $filename; |
| 242 |
$contents = ob_get_contents(); |
| 243 |
ob_end_clean(); |
| 244 |
return $contents; |
| 245 |
} |
| 246 |
return false; |
| 247 |
} |
| 248 |
|
| 249 |
public static function get_unique_key($name='', $table_name, $column, $id = 0, $num_chars = 6){ |
| 250 |
global $wpdb; |
| 251 |
|
| 252 |
$key = ''; |
| 253 |
|
| 254 |
if (!empty($name)){ |
| 255 |
$key = sanitize_key($name); |
| 256 |
} |
| 257 |
|
| 258 |
if(empty($key)){ |
| 259 |
$max_slug_value = pow(36, $num_chars); |
| 260 |
$min_slug_value = 37; // we want to have at least 2 characters in the slug |
| 261 |
$key = base_convert( rand($min_slug_value, $max_slug_value), 10, 36 ); |
| 262 |
} |
| 263 |
|
| 264 |
if (is_numeric($key) or in_array($key, array('id', 'key', 'created-at', 'detaillink', 'editlink', 'siteurl', 'evenodd'))) |
| 265 |
$key = $key .'a'; |
| 266 |
|
| 267 |
$query = "SELECT $column FROM $table_name WHERE $column = %s AND ID != %d LIMIT 1"; |
| 268 |
$key_check = $wpdb->get_var($wpdb->prepare($query, $key, $id)); |
| 269 |
|
| 270 |
if ($key_check or is_numeric($key_check)){ |
| 271 |
$suffix = 2; |
| 272 |
do { |
| 273 |
$alt_post_name = substr($key, 0, 200-(strlen($suffix)+1)). "$suffix"; |
| 274 |
$key_check = $wpdb->get_var($wpdb->prepare($query, $alt_post_name, $id)); |
| 275 |
$suffix++; |
| 276 |
} while ($key_check || is_numeric($key_check)); |
| 277 |
$key = $alt_post_name; |
| 278 |
} |
| 279 |
return $key; |
| 280 |
} |
| 281 |
|
| 282 |
//Editing a Form or Entry |
| 283 |
public static function setup_edit_vars($record, $table, $fields='', $default=false, $post_values=array()){ |
| 284 |
if(!$record) return false; |
| 285 |
global $frm_entry_meta, $frm_settings, $frm_vars; |
| 286 |
|
| 287 |
if(empty($post_values)) |
| 288 |
$post_values = stripslashes_deep($_POST); |
| 289 |
|
| 290 |
$values = array('id' => $record->id, 'fields' => array()); |
| 291 |
|
| 292 |
foreach ( array('name', 'description') as $var ) { |
| 293 |
$default_val = isset($record->{$var}) ? $record->{$var} : ''; |
| 294 |
$values[$var] = FrmAppHelper::get_param($var, $default_val); |
| 295 |
unset($var, $default_val); |
| 296 |
} |
| 297 |
|
| 298 |
if ( apply_filters('frm_use_wpautop', true) ) { |
| 299 |
$values['description'] = wpautop(str_replace( '<br>', '<br />', $values['description'])); |
| 300 |
} |
| 301 |
|
| 302 |
foreach ( (array) $fields as $field ) { |
| 303 |
|
| 304 |
if ($default){ |
| 305 |
$meta_value = $field->default_value; |
| 306 |
}else{ |
| 307 |
if($record->post_id and class_exists('FrmProEntryMetaHelper') and isset($field->field_options['post_field']) and $field->field_options['post_field']){ |
| 308 |
if(!isset($field->field_options['custom_field'])) |
| 309 |
$field->field_options['custom_field'] = ''; |
| 310 |
$meta_value = FrmProEntryMetaHelper::get_post_value($record->post_id, $field->field_options['post_field'], $field->field_options['custom_field'], array('truncate' => false, 'type' => $field->type, 'form_id' => $field->form_id, 'field' => $field)); |
| 311 |
}else if(isset($record->metas)){ |
| 312 |
$meta_value = isset($record->metas[$field->id]) ? $record->metas[$field->id] : false; |
| 313 |
}else{ |
| 314 |
$meta_value = $frm_entry_meta->get_entry_meta_by_field($record->id, $field->id); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
$field_type = isset($post_values['field_options']['type_'.$field->id]) ? $post_values['field_options']['type_'.$field->id] : $field->type; |
| 319 |
$new_value = isset($post_values['item_meta'][$field->id]) ? maybe_unserialize($post_values['item_meta'][$field->id]) : $meta_value; |
| 320 |
|
| 321 |
$field_array = array( |
| 322 |
'id' => $field->id, |
| 323 |
'value' => $new_value, |
| 324 |
'default_value' => $field->default_value, |
| 325 |
'name' => $field->name, |
| 326 |
'description' => $field->description, |
| 327 |
'type' => apply_filters('frm_field_type', $field_type, $field, $new_value), |
| 328 |
'options' => $field->options, |
| 329 |
'required' => $field->required, |
| 330 |
'field_key' => $field->field_key, |
| 331 |
'field_order' => $field->field_order, |
| 332 |
'form_id' => $field->form_id |
| 333 |
); |
| 334 |
|
| 335 |
/*if(in_array($field_array['type'], array('checkbox', 'radio', 'select')) and !empty($field_array['options'])){ |
| 336 |
foreach((array)$field_array['options'] as $opt_key => $opt){ |
| 337 |
if(!is_array($opt)) |
| 338 |
$field_array['options'][$opt_key] = array('label' => $opt); |
| 339 |
unset($opt); |
| 340 |
unset($opt_key); |
| 341 |
} |
| 342 |
}*/ |
| 343 |
|
| 344 |
$opt_defaults = FrmFieldsHelper::get_default_field_opts($field_array['type'], $field, true); |
| 345 |
|
| 346 |
foreach ($opt_defaults as $opt => $default_opt){ |
| 347 |
$field_array[$opt] = ($post_values && isset($post_values['field_options'][$opt.'_'.$field->id]) ) ? maybe_unserialize($post_values['field_options'][$opt.'_'.$field->id]) : (isset($field->field_options[$opt]) ? $field->field_options[$opt] : $default_opt); |
| 348 |
if($opt == 'blank' and $field_array[$opt] == ''){ |
| 349 |
$field_array[$opt] = $frm_settings->blank_msg; |
| 350 |
}else if($opt == 'invalid' and $field_array[$opt] == ''){ |
| 351 |
if($field_type == 'captcha') |
| 352 |
$field_array[$opt] = $frm_settings->re_msg; |
| 353 |
else |
| 354 |
$field_array[$opt] = sprintf(__('%s is invalid', 'formidable'), $field_array['name']); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
unset($opt_defaults); |
| 359 |
|
| 360 |
if ($field_array['custom_html'] == '') |
| 361 |
$field_array['custom_html'] = FrmFieldsHelper::get_default_html($field_type); |
| 362 |
|
| 363 |
if ($field_array['size'] == '') |
| 364 |
$field_array['size'] = isset($frm_vars['sidebar_width']) ? $frm_vars['sidebar_width'] : ''; |
| 365 |
|
| 366 |
$field_array = apply_filters('frm_setup_edit_fields_vars', $field_array, $field, $values['id']); |
| 367 |
|
| 368 |
if(!isset($field_array['unique']) or !$field_array['unique']) |
| 369 |
$field_array['unique_msg'] = ''; |
| 370 |
|
| 371 |
foreach((array)$field->field_options as $k => $v){ |
| 372 |
if(!isset($field_array[$k])) |
| 373 |
$field_array[$k] = $v; |
| 374 |
unset($k); |
| 375 |
unset($v); |
| 376 |
} |
| 377 |
|
| 378 |
$values['fields'][$field->id] = $field_array; |
| 379 |
|
| 380 |
unset($field); |
| 381 |
} |
| 382 |
|
| 383 |
$frm_form = new FrmForm(); |
| 384 |
$form = $frm_form->getOne( $table == 'entries' ? $record->form_id : $record->id ); |
| 385 |
unset($frm_form); |
| 386 |
|
| 387 |
if ($form){ |
| 388 |
$values['form_name'] = (isset($record->form_id)) ? $form->name : ''; |
| 389 |
if (is_array($form->options)){ |
| 390 |
foreach ($form->options as $opt => $value){ |
| 391 |
if(in_array($opt, array('email_to', 'reply_to', 'reply_to_name'))) |
| 392 |
$values['notification'][0][$opt] = isset($post_values["notification[0][$opt]"]) ? maybe_unserialize($post_values["notification[0][$opt]"]) : $value; |
| 393 |
|
| 394 |
$values[$opt] = isset($post_values[$opt]) ? maybe_unserialize($post_values[$opt]) : $value; |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
$form_defaults = FrmFormsHelper::get_default_opts(); |
| 400 |
|
| 401 |
//set to posted value or default |
| 402 |
foreach ($form_defaults as $opt => $default){ |
| 403 |
if (!isset($values[$opt]) or $values[$opt] == ''){ |
| 404 |
if($opt == 'notification'){ |
| 405 |
$values[$opt] = ($post_values and isset($post_values[$opt])) ? $post_values[$opt] : $default; |
| 406 |
|
| 407 |
foreach($default as $o => $d){ |
| 408 |
if($o == 'email_to') |
| 409 |
$d = ''; //allow blank email address |
| 410 |
$values[$opt][0][$o] = ($post_values and isset($post_values[$opt][0][$o])) ? $post_values[$opt][0][$o] : $d; |
| 411 |
unset($o); |
| 412 |
unset($d); |
| 413 |
} |
| 414 |
}else{ |
| 415 |
$values[$opt] = ($post_values and isset($post_values['options'][$opt])) ? $post_values['options'][$opt] : $default; |
| 416 |
} |
| 417 |
}else if($values[$opt] == 'notification'){ |
| 418 |
foreach($values[$opt] as $k => $n){ |
| 419 |
foreach($default as $o => $d){ |
| 420 |
if(!isset($n[$o])) |
| 421 |
$values[$opt][$k][$o] = ($post_values and isset($post_values[$opt][$k][$o])) ? $post_values[$opt][$k][$o] : $d; |
| 422 |
unset($o); |
| 423 |
unset($d); |
| 424 |
} |
| 425 |
unset($k); |
| 426 |
unset($n); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
unset($opt); |
| 431 |
unset($defaut); |
| 432 |
} |
| 433 |
|
| 434 |
if (!isset($values['custom_style'])) |
| 435 |
$values['custom_style'] = ($post_values and isset($post_values['options']['custom_style'])) ? $_POST['options']['custom_style'] : ($frm_settings->load_style != 'none'); |
| 436 |
|
| 437 |
foreach(array('before', 'after', 'submit') as $h){ |
| 438 |
if (!isset($values[$h.'_html'])) |
| 439 |
$values[$h .'_html'] = (isset($post_values['options'][$h .'_html']) ? $post_values['options'][$h .'_html'] : FrmFormsHelper::get_default_html($h)); |
| 440 |
unset($h); |
| 441 |
} |
| 442 |
|
| 443 |
if ($table == 'entries') |
| 444 |
$values = FrmEntriesHelper::setup_edit_vars( $values, $record ); |
| 445 |
else if ($table == 'forms') |
| 446 |
$values = FrmFormsHelper::setup_edit_vars( $values, $record, $post_values ); |
| 447 |
|
| 448 |
return $values; |
| 449 |
} |
| 450 |
|
| 451 |
public static function insert_opt_html($args){ |
| 452 |
extract($args); |
| 453 |
|
| 454 |
$class = ''; |
| 455 |
|
| 456 |
if ( in_array( $type, array( 'email', 'user_id', 'hidden', 'select', 'radio', 'checkbox', 'phone', 'text' ) ) ) { |
| 457 |
$class .= 'show_frm_not_email_to'; |
| 458 |
} |
| 459 |
?> |
| 460 |
<li> |
| 461 |
<a class="frmids frm_insert_code alignright <?php echo $class ?>" data-code="<?php echo esc_attr($id) ?>" href="javascript:void(0)">[<?php echo $id ?>]</a> |
| 462 |
<a class="frmkeys frm_insert_code alignright <?php echo $class ?>" data-code="<?php echo esc_attr($key) ?>" href="javascript:void(0)">[<?php echo FrmAppHelper::truncate($key, 10) ?>]</a> |
| 463 |
<a class="frm_insert_code <?php echo $class ?>" data-code="<?php echo esc_attr($id) ?>" href="javascript:void(0)"><?php echo FrmAppHelper::truncate($name, 60) ?></a> |
| 464 |
</li> |
| 465 |
<?php |
| 466 |
} |
| 467 |
|
| 468 |
public static function get_us_states(){ |
| 469 |
return apply_filters('frm_us_states', array( |
| 470 |
'AL' => 'Alabama', 'AK' => 'Alaska', 'AR' => 'Arkansas', 'AZ' => 'Arizona', |
| 471 |
'CA' => 'California', 'CO' => 'Colorado', 'CT' => 'Connecticut', 'DE' => 'Delaware', |
| 472 |
'DC' => 'District of Columbia', |
| 473 |
'FL' => 'Florida', 'GA' => 'Georgia', 'HI' => 'Hawaii', 'ID' => 'Idaho', |
| 474 |
'IL' => 'Illinois', 'IN' => 'Indiana', 'IA' => 'Iowa', 'KS' => 'Kansas', |
| 475 |
'KY' => 'Kentucky', 'LA' => 'Louisiana', 'ME' => 'Maine','MD' => 'Maryland', |
| 476 |
'MA' => 'Massachusetts', 'MI' => 'Michigan', 'MN' => 'Minnesota', 'MS' => 'Mississippi', |
| 477 |
'MO' => 'Missouri', 'MT' => 'Montana', 'NE' => 'Nebraska', 'NV' => 'Nevada', |
| 478 |
'NH' => 'New Hampshire', 'NJ' => 'New Jersey', 'NM' => 'New Mexico', 'NY' => 'New York', |
| 479 |
'NC' => 'North Carolina', 'ND' => 'North Dakota', 'OH' => 'Ohio', 'OK' => 'Oklahoma', |
| 480 |
'OR' => 'Oregon', 'PA' => 'Pennsylvania', 'RI' => 'Rhode Island', 'SC' => 'South Carolina', |
| 481 |
'SD' => 'South Dakota', 'TN' => 'Tennessee', 'TX' => 'Texas', 'UT' => 'Utah', |
| 482 |
'VT' => 'Vermont', 'VA' => 'Virginia', 'WA' => 'Washington', 'WV' => 'West Virginia', |
| 483 |
'WI' => 'Wisconsin', 'WY' => 'Wyoming' |
| 484 |
)); |
| 485 |
} |
| 486 |
|
| 487 |
public static function get_countries(){ |
| 488 |
return apply_filters('frm_countries', array( |
| 489 |
__('Afghanistan', 'formidable'), __('Albania', 'formidable'), __('Algeria', 'formidable'), |
| 490 |
__('American Samoa', 'formidable'), __('Andorra', 'formidable'), __('Angola', 'formidable'), |
| 491 |
__('Anguilla', 'formidable'), __('Antarctica', 'formidable'), __('Antigua and Barbuda', 'formidable'), |
| 492 |
__('Argentina', 'formidable'), __('Armenia', 'formidable'), __('Aruba', 'formidable'), |
| 493 |
__('Australia', 'formidable'), __('Austria', 'formidable'), __('Azerbaijan', 'formidable'), |
| 494 |
__('Bahamas', 'formidable'), __('Bahrain', 'formidable'), __('Bangladesh', 'formidable'), |
| 495 |
__('Barbados', 'formidable'), __('Belarus', 'formidable'), __('Belgium', 'formidable'), |
| 496 |
__('Belize', 'formidable'), __('Benin', 'formidable'), __('Bermuda', 'formidable'), |
| 497 |
__('Bhutan', 'formidable'), __('Bolivia', 'formidable'), __('Bosnia and Herzegovina', 'formidable'), |
| 498 |
__('Botswana', 'formidable'), __('Brazil', 'formidable'), __('Brunei', 'formidable'), |
| 499 |
__('Bulgaria', 'formidable'), __('Burkina Faso', 'formidable'), __('Burundi', 'formidable'), |
| 500 |
__('Cambodia', 'formidable'), __('Cameroon', 'formidable'), __('Canada', 'formidable'), |
| 501 |
__('Cape Verde', 'formidable'), __('Cayman Islands', 'formidable'), __('Central African Republic', 'formidable'), |
| 502 |
__('Chad', 'formidable'), __('Chile', 'formidable'), __('China', 'formidable'), |
| 503 |
__('Colombia', 'formidable'), __('Comoros', 'formidable'), __('Congo', 'formidable'), |
| 504 |
__('Costa Rica', 'formidable'), __('Côte d\'Ivoire', 'formidable'), __('Croatia', 'formidable'), |
| 505 |
__('Cuba', 'formidable'), __('Cyprus', 'formidable'), __('Czech Republic', 'formidable'), |
| 506 |
__('Denmark', 'formidable'), __('Djibouti', 'formidable'), __('Dominica', 'formidable'), |
| 507 |
__('Dominican Republic', 'formidable'), __('East Timor', 'formidable'), __('Ecuador', 'formidable'), |
| 508 |
__('Egypt', 'formidable'), __('El Salvador', 'formidable'), __('Equatorial Guinea', 'formidable'), |
| 509 |
__('Eritrea', 'formidable'), __('Estonia', 'formidable'), __('Ethiopia', 'formidable'), |
| 510 |
__('Fiji', 'formidable'), __('Finland', 'formidable'), __('France', 'formidable'), |
| 511 |
__('French Guiana', 'formidable'), __('French Polynesia', 'formidable'), __('Gabon', 'formidable'), |
| 512 |
__('Gambia', 'formidable'), __('Georgia', 'formidable'), __('Germany', 'formidable'), |
| 513 |
__('Ghana', 'formidable'), __('Gibraltar', 'formidable'), __('Greece', 'formidable'), |
| 514 |
__('Greenland', 'formidable'), __('Grenada', 'formidable'), __('Guam', 'formidable'), |
| 515 |
__('Guatemala', 'formidable'), __('Guinea', 'formidable'), __('Guinea-Bissau', 'formidable'), |
| 516 |
__('Guyana', 'formidable'), __('Haiti', 'formidable'), __('Honduras', 'formidable'), |
| 517 |
__('Hong Kong', 'formidable'), __('Hungary', 'formidable'), __('Iceland', 'formidable'), |
| 518 |
__('India', 'formidable'), __('Indonesia', 'formidable'), __('Iran', 'formidable'), |
| 519 |
__('Iraq', 'formidable'), __('Ireland', 'formidable'), __('Israel', 'formidable'), |
| 520 |
__('Italy', 'formidable'), __('Jamaica', 'formidable'), __('Japan', 'formidable'), |
| 521 |
__('Jordan', 'formidable'), __('Kazakhstan', 'formidable'), __('Kenya', 'formidable'), |
| 522 |
__('Kiribati', 'formidable'), __('North Korea', 'formidable'), __('South Korea', 'formidable'), |
| 523 |
__('Kuwait', 'formidable'), __('Kyrgyzstan', 'formidable'), __('Laos', 'formidable'), |
| 524 |
__('Latvia', 'formidable'), __('Lebanon', 'formidable'), __('Lesotho', 'formidable'), |
| 525 |
__('Liberia', 'formidable'), __('Libya', 'formidable'), __('Liechtenstein', 'formidable'), |
| 526 |
__('Lithuania', 'formidable'), __('Luxembourg', 'formidable'), __('Macedonia', 'formidable'), |
| 527 |
__('Madagascar', 'formidable'), __('Malawi', 'formidable'), __('Malaysia', 'formidable'), |
| 528 |
__('Maldives', 'formidable'), __('Mali', 'formidable'), __('Malta', 'formidable'), |
| 529 |
__('Marshall Islands', 'formidable'), __('Mauritania', 'formidable'), __('Mauritius', 'formidable'), |
| 530 |
__('Mexico', 'formidable'), __('Micronesia', 'formidable'), __('Moldova', 'formidable'), |
| 531 |
__('Monaco', 'formidable'), __('Mongolia', 'formidable'), __('Montenegro', 'formidable'), |
| 532 |
__('Montserrat', 'formidable'), __('Morocco', 'formidable'), __('Mozambique', 'formidable'), |
| 533 |
__('Myanmar', 'formidable'), __('Namibia', 'formidable'), __('Nauru', 'formidable'), |
| 534 |
__('Nepal', 'formidable'), __('Netherlands', 'formidable'), __('New Zealand', 'formidable'), |
| 535 |
__('Nicaragua', 'formidable'), __('Niger', 'formidable'), __('Nigeria', 'formidable'), |
| 536 |
__('Norway', 'formidable'), __('Northern Mariana Islands', 'formidable'), __('Oman', 'formidable'), |
| 537 |
__('Pakistan', 'formidable'), __('Palau', 'formidable'), __('Palestine', 'formidable'), |
| 538 |
__('Panama', 'formidable'), __('Papua New Guinea', 'formidable'), __('Paraguay', 'formidable'), |
| 539 |
__('Peru', 'formidable'), __('Philippines', 'formidable'), __('Poland', 'formidable'), |
| 540 |
__('Portugal', 'formidable'), __('Puerto Rico', 'formidable'), __('Qatar', 'formidable'), |
| 541 |
__('Romania', 'formidable'), __('Russia', 'formidable'), __('Rwanda', 'formidable'), |
| 542 |
__('Saint Kitts and Nevis', 'formidable'), __('Saint Lucia', 'formidable'), |
| 543 |
__('Saint Vincent and the Grenadines', 'formidable'), __('Samoa', 'formidable'), |
| 544 |
__('San Marino', 'formidable'), __('Sao Tome and Principe', 'formidable'), __('Saudi Arabia', 'formidable'), |
| 545 |
__('Senegal', 'formidable'), __('Serbia and Montenegro', 'formidable'), __('Seychelles', 'formidable'), |
| 546 |
__('Sierra Leone', 'formidable'), __('Singapore', 'formidable'), __('Slovakia', 'formidable'), |
| 547 |
__('Slovenia', 'formidable'), __('Solomon Islands', 'formidable'), __('Somalia', 'formidable'), |
| 548 |
__('South Africa', 'formidable'), __('Spain', 'formidable'), __('Sri Lanka', 'formidable'), |
| 549 |
__('Sudan', 'formidable'), __('Suriname', 'formidable'), __('Swaziland', 'formidable'), |
| 550 |
__('Sweden', 'formidable'), __('Switzerland', 'formidable'), __('Syria', 'formidable'), |
| 551 |
__('Taiwan', 'formidable'), __('Tajikistan', 'formidable'), __('Tanzania', 'formidable'), |
| 552 |
__('Thailand', 'formidable'), __('Togo', 'formidable'), __('Tonga', 'formidable'), |
| 553 |
__('Trinidad and Tobago', 'formidable'), __('Tunisia', 'formidable'), __('Turkey', 'formidable'), |
| 554 |
__('Turkmenistan', 'formidable'), __('Tuvalu', 'formidable'), __('Uganda', 'formidable'), |
| 555 |
__('Ukraine', 'formidable'), __('United Arab Emirates', 'formidable'), __('United Kingdom', 'formidable'), |
| 556 |
__('United States', 'formidable'), __('Uruguay', 'formidable'), __('Uzbekistan', 'formidable'), |
| 557 |
__('Vanuatu', 'formidable'), __('Vatican City', 'formidable'), __('Venezuela', 'formidable'), |
| 558 |
__('Vietnam', 'formidable'), __('Virgin Islands, British', 'formidable'), |
| 559 |
__('Virgin Islands, U.S.', 'formidable'), __('Yemen', 'formidable'), __('Zambia', 'formidable'), |
| 560 |
__('Zimbabwe', 'formidable') |
| 561 |
)); |
| 562 |
} |
| 563 |
|
| 564 |
public static function truncate($str, $length, $minword = 3, $continue = '...'){ |
| 565 |
if(is_array($str)) |
| 566 |
return; |
| 567 |
|
| 568 |
$length = (int)$length; |
| 569 |
$str = strip_tags($str); |
| 570 |
$original_len = (function_exists('mb_strlen')) ? mb_strlen($str) : strlen($str); |
| 571 |
|
| 572 |
if($length == 0){ |
| 573 |
return ''; |
| 574 |
}else if($length <= 10){ |
| 575 |
$sub = (function_exists('mb_substr')) ? mb_substr($str, 0, $length) : substr($str, 0, $length); |
| 576 |
return $sub . (($length < $original_len) ? $continue : ''); |
| 577 |
} |
| 578 |
|
| 579 |
$sub = ''; |
| 580 |
$len = 0; |
| 581 |
|
| 582 |
$words = (function_exists('mb_split')) ? mb_split(' ', $str) : explode(' ', $str); |
| 583 |
|
| 584 |
foreach ($words as $word){ |
| 585 |
$part = (($sub != '') ? ' ' : '') . $word; |
| 586 |
$total_len = (function_exists('mb_strlen')) ? mb_strlen($sub . $part) : strlen($sub. $part); |
| 587 |
if ( $total_len > $length && str_word_count($sub) ) { |
| 588 |
break; |
| 589 |
} |
| 590 |
|
| 591 |
$sub .= $part; |
| 592 |
$len += (function_exists('mb_strlen')) ? mb_strlen($part) : strlen($part); |
| 593 |
|
| 594 |
if ( str_word_count($sub) > $minword && $total_len >= $length ) { |
| 595 |
break; |
| 596 |
} |
| 597 |
|
| 598 |
unset($total_len, $word); |
| 599 |
} |
| 600 |
|
| 601 |
return $sub . (($len < $original_len) ? $continue : ''); |
| 602 |
} |
| 603 |
|
| 604 |
/* |
| 605 |
* Added for < 4.0 compatability |
| 606 |
* |
| 607 |
* @since 1.07.10 |
| 608 |
* |
| 609 |
* @param $term The value to escape |
| 610 |
* @return string The escaped value |
| 611 |
*/ |
| 612 |
public static function esc_like($term) { |
| 613 |
global $wpdb; |
| 614 |
if ( method_exists($wpdb, 'esc_like') ) { // WP 4.0 |
| 615 |
$term = $wpdb->esc_like( $term ); |
| 616 |
} else { |
| 617 |
$term = like_escape( $term ); |
| 618 |
} |
| 619 |
|
| 620 |
return $term; |
| 621 |
} |
| 622 |
|
| 623 |
public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ){ |
| 624 |
if ( empty($where) ) { |
| 625 |
return ''; |
| 626 |
} |
| 627 |
|
| 628 |
if(is_array($where)){ |
| 629 |
global $frmdb, $wpdb; |
| 630 |
extract($frmdb->get_where_clause_and_values( $where )); |
| 631 |
$where = $wpdb->prepare($where, $values); |
| 632 |
}else{ |
| 633 |
$where = $starts_with . $where; |
| 634 |
} |
| 635 |
|
| 636 |
return $where; |
| 637 |
} |
| 638 |
|
| 639 |
// Pagination Methods |
| 640 |
public static function getLastRecordNum($r_count,$current_p,$p_size){ |
| 641 |
return (($r_count < ($current_p * $p_size))?$r_count:($current_p * $p_size)); |
| 642 |
} |
| 643 |
|
| 644 |
public static function getFirstRecordNum($r_count,$current_p,$p_size){ |
| 645 |
if($current_p == 1) |
| 646 |
return 1; |
| 647 |
else |
| 648 |
return (self::getLastRecordNum($r_count,($current_p - 1),$p_size) + 1); |
| 649 |
} |
| 650 |
|
| 651 |
public static function getRecordCount($where="", $table_name){ |
| 652 |
global $wpdb; |
| 653 |
$query = 'SELECT COUNT(*) FROM ' . $table_name . FrmAppHelper::prepend_and_or_where(' WHERE ', $where); |
| 654 |
return $wpdb->get_var($query); |
| 655 |
} |
| 656 |
|
| 657 |
public static function get_referer_query($query) { |
| 658 |
if (strpos($query, "google.")) { |
| 659 |
//$pattern = '/^.*\/search.*[\?&]q=(.*)$/'; |
| 660 |
$pattern = '/^.*[\?&]q=(.*)$/'; |
| 661 |
} else if (strpos($query, "bing.com")) { |
| 662 |
$pattern = '/^.*q=(.*)$/'; |
| 663 |
} else if (strpos($query, "yahoo.")) { |
| 664 |
$pattern = '/^.*[\?&]p=(.*)$/'; |
| 665 |
} else if (strpos($query, "ask.")) { |
| 666 |
$pattern = '/^.*[\?&]q=(.*)$/'; |
| 667 |
} else { |
| 668 |
return false; |
| 669 |
} |
| 670 |
preg_match($pattern, $query, $matches); |
| 671 |
$querystr = substr($matches[1], 0, strpos($matches[1], '&')); |
| 672 |
return urldecode($querystr); |
| 673 |
} |
| 674 |
|
| 675 |
public static function get_referer_info(){ |
| 676 |
$referrerinfo = ''; |
| 677 |
$keywords = array(); |
| 678 |
$i = 1; |
| 679 |
if(isset($_SESSION) and isset($_SESSION['frm_http_referer']) and $_SESSION['frm_http_referer']){ |
| 680 |
foreach ($_SESSION['frm_http_referer'] as $referer) { |
| 681 |
$referrerinfo .= str_pad("Referer $i: ",20) . $referer. "\r\n"; |
| 682 |
$keywords_used = FrmAppHelper::get_referer_query($referer); |
| 683 |
if ($keywords_used) |
| 684 |
$keywords[] = $keywords_used; |
| 685 |
|
| 686 |
$i++; |
| 687 |
} |
| 688 |
|
| 689 |
$referrerinfo .= "\r\n"; |
| 690 |
}else{ |
| 691 |
$referrerinfo = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; |
| 692 |
} |
| 693 |
|
| 694 |
$i = 1; |
| 695 |
if(isset($_SESSION) and isset($_SESSION['frm_http_pages']) and $_SESSION['frm_http_pages']){ |
| 696 |
foreach ($_SESSION['frm_http_pages'] as $page) { |
| 697 |
$referrerinfo .= str_pad("Page visited $i: ",20) . $page. "\r\n"; |
| 698 |
$i++; |
| 699 |
} |
| 700 |
|
| 701 |
$referrerinfo .= "\r\n"; |
| 702 |
} |
| 703 |
|
| 704 |
$i = 1; |
| 705 |
foreach ($keywords as $keyword) { |
| 706 |
$referrerinfo .= str_pad("Keyword $i: ",20) . $keyword. "\r\n"; |
| 707 |
$i++; |
| 708 |
} |
| 709 |
$referrerinfo .= "\r\n"; |
| 710 |
|
| 711 |
return $referrerinfo; |
| 712 |
} |
| 713 |
|
| 714 |
public static function json_to_array($json_vars){ |
| 715 |
$vars = array(); |
| 716 |
foreach($json_vars as $jv){ |
| 717 |
$jv_name = explode('[', $jv['name']); |
| 718 |
$last = count($jv_name)-1; |
| 719 |
foreach($jv_name as $p => $n){ |
| 720 |
$name = trim($n, ']'); |
| 721 |
$this_val = ($p == $last) ? $jv['value'] : array(); |
| 722 |
|
| 723 |
if(!$p){ |
| 724 |
$l1 = $name; |
| 725 |
if($name == '') |
| 726 |
$vars[] = $this_val; |
| 727 |
else if(!isset($vars[$l1])) |
| 728 |
$vars[$l1] = $this_val; |
| 729 |
} |
| 730 |
|
| 731 |
if($p == 1){ |
| 732 |
$l2 = $name; |
| 733 |
if($name == '') |
| 734 |
$vars[$l1][] = $this_val; |
| 735 |
else if(!isset($vars[$l1][$l2])) |
| 736 |
$vars[$l1][$l2] = $this_val; |
| 737 |
} |
| 738 |
|
| 739 |
if($p == 2){ |
| 740 |
$l3 = $name; |
| 741 |
if($name == '') |
| 742 |
$vars[$l1][$l2][] = $this_val; |
| 743 |
else if(!isset($vars[$l1][$l2][$l3])) |
| 744 |
$vars[$l1][$l2][$l3] = $this_val; |
| 745 |
} |
| 746 |
|
| 747 |
unset($this_val); |
| 748 |
unset($n); |
| 749 |
} |
| 750 |
|
| 751 |
unset($last); |
| 752 |
unset($jv); |
| 753 |
} |
| 754 |
|
| 755 |
return $vars; |
| 756 |
} |
| 757 |
|
| 758 |
public static function maybe_json_decode($string){ |
| 759 |
$new_string = json_decode($string, true); |
| 760 |
if ( function_exists('json_last_error') ) { // php 5.3+ |
| 761 |
if ( json_last_error() == JSON_ERROR_NONE ) { |
| 762 |
$string = $new_string; |
| 763 |
} |
| 764 |
} else if ( isset($new_string) ) { // php < 5.3 fallback |
| 765 |
$string = $new_string; |
| 766 |
} |
| 767 |
return $string; |
| 768 |
} |
| 769 |
|
| 770 |
public static function check_mem_use($function='', $start_mem=0) { |
| 771 |
$mem = memory_get_usage(true) - $start_mem; |
| 772 |
|
| 773 |
//error_log($mem .' '. $function); |
| 774 |
return $start_mem + $mem; |
| 775 |
} |
| 776 |
|
| 777 |
/* |
| 778 |
* @since 1.07.10 |
| 779 |
* |
| 780 |
* @param string $post_type The name of the post type that may need to be highlighted |
| 781 |
* @return echo The javascript to open and highlight the Formidable menu |
| 782 |
*/ |
| 783 |
public static function maybe_highlight_menu($post_type) { |
| 784 |
global $post, $pagenow; |
| 785 |
|
| 786 |
if ( isset($_REQUEST['post_type']) && $_REQUEST['post_type'] != $post_type ) { |
| 787 |
return; |
| 788 |
} |
| 789 |
|
| 790 |
if ( is_object($post) && $post->post_type != $post_type ) { |
| 791 |
return; |
| 792 |
} |
| 793 |
|
| 794 |
echo <<<HTML |
| 795 |
<script type="text/javascript"> |
| 796 |
jQuery(document).ready(function(){ |
| 797 |
jQuery('#toplevel_page_formidable').removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu wp-menu-open'); |
| 798 |
jQuery('#toplevel_page_formidable a.wp-has-submenu').removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu wp-menu-open'); |
| 799 |
}); |
| 800 |
</script> |
| 801 |
HTML; |
| 802 |
} |
| 803 |
|
| 804 |
/* |
| 805 |
* @since 1.07.10 |
| 806 |
* |
| 807 |
* @param float $min_version The version the add-on requires |
| 808 |
* @return echo The message on the plugins listing page |
| 809 |
*/ |
| 810 |
public static function min_version_notice($min_version) { |
| 811 |
$frm_version = self::plugin_version(); |
| 812 |
|
| 813 |
// check if Formidable meets minimum requirements |
| 814 |
if ( version_compare($frm_version, $min_version, '>=') ) { |
| 815 |
return; |
| 816 |
} |
| 817 |
|
| 818 |
$wp_list_table = _get_list_table('WP_Plugins_List_Table'); |
| 819 |
echo '<tr class="plugin-update-tr active"><th colspan="' . $wp_list_table->get_column_count() . '" class="check-column plugin-update colspanchange"><div class="update-message">'. |
| 820 |
__('You are running an outdated version of Formidable. This plugin may not work correctly if you do not update Formidable.', 'formidable') . |
| 821 |
'</div></td></tr>'; |
| 822 |
} |
| 823 |
} |
| 824 |
|