| 1 |
<?php |
| 2 |
namespace FormLayer; |
| 3 |
|
| 4 |
if(!defined('ABSPATH')){ |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
class Util{ |
| 9 |
|
| 10 |
|
| 11 |
static function get_form_field_labels($form_id) { |
| 12 |
$form = get_post($form_id); |
| 13 |
$field_labels = []; |
| 14 |
if ($form) { |
| 15 |
$form_data = json_decode($form->post_content, true); |
| 16 |
if (is_array($form_data) && !empty($form_data['fields'])) { |
| 17 |
foreach ($form_data['fields'] as $field) { |
| 18 |
$name = self::get_field_name($field); |
| 19 |
$field_labels[$name] = !empty($field['label']) ? $field['label'] : $name; |
| 20 |
} |
| 21 |
} |
| 22 |
} |
| 23 |
return $field_labels; |
| 24 |
} |
| 25 |
|
| 26 |
static function get_form_field_types($form_id){ |
| 27 |
$form = get_post($form_id); |
| 28 |
$field_types = []; |
| 29 |
if(!empty($form)){ |
| 30 |
$form_data = json_decode($form->post_content, true); |
| 31 |
if(is_array($form_data) && !empty($form_data['fields'])){ |
| 32 |
foreach($form_data['fields'] as $field){ |
| 33 |
$name = self::get_field_name($field); |
| 34 |
$field_types[$name] = isset($field['type']) ? $field['type'] : 'text'; |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
return $field_types; |
| 40 |
} |
| 41 |
|
| 42 |
static function get_field_name($field) { |
| 43 |
if (!empty($field['name_attr'])) { |
| 44 |
return $field['name_attr']; |
| 45 |
} |
| 46 |
$id = isset($field['id']) ? $field['id'] : sanitize_title(isset($field['label']) ? $field['label'] : ''); |
| 47 |
return 'field_' . $id; |
| 48 |
} |
| 49 |
|
| 50 |
static function get_post_id_by_display_id($display_id) { |
| 51 |
$posts = get_posts([ |
| 52 |
'post_type' => 'formlayer_form', |
| 53 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 54 |
'meta_query' => [ |
| 55 |
[ |
| 56 |
'key' => '_formlayer_display_id', |
| 57 |
'value' => $display_id, |
| 58 |
'compare' => '=' |
| 59 |
] |
| 60 |
], |
| 61 |
'posts_per_page' => 1, |
| 62 |
'post_status' => 'any' |
| 63 |
]); |
| 64 |
return !empty($posts) ? $posts[0]->ID : 0; |
| 65 |
} |
| 66 |
} |