/
GFormManager.php
137 lines (106 loc) · 3.75 KB
/
GFormManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace DevonSample;
use BlueFission\Net\HTTP;
use BlueFission\DevString;
require_once ( plugin_dir_path( __FILE__ ) . 'WPUpdateable.php' );
class GFormManager extends WPUpdateable {
const CONFIG_META = '_form_config'; // you probably don't need this
const SESSION_VAR = 'form_snapshot'; // server session variable
protected $_data = array( // explicitly define each form field you will track
'field_1'=>'',
'field_2'=>'',
'field_3'=>'',
'field_4'=>'',
'field_5'=>'',
);
public function snapshot() {
$this->prepare();
$this->font_declaration = $this->check_fonts();
$info = serialize($this->_data);
if ( !HTTP::session(self::SESSION_VAR, $info) ) {
// TODO: Warn somebody
}
}
public function develop() {
$info = unserialize( HTTP::session(self::SESSION_VAR) );
$this->fill_fields( $info );
$this->assign($info);
}
public function load() { // you only need this if creating custom posts form the form
// Get this post to also load in related gravity form data
if ( parent::load() ) {
$config_meta = self::CONFIG_META;
$config = get_post_meta( $this->_post_id, $config_meta, true );
$this->config($config);
if ( !is_numeric($this->lead_id) ) {
$this->lead_id = get_post_custom_values( 'lead_id' );
}
$lead = \GFAPI::get_entry($this->lead_id);
if ( is_wp_error($lead) ) {
return false;
}
$this->load_form_data( $lead, $lead['form_id'] );
}
}
public function load_form_data( $lead, $form_id = '' ) {
$form_id = is_numeric($form_id) ? $form_id : ( isset( $lead['form_id'] ) ? $lead['form_id'] : $lead['id'] );
// No form id
if ( !$form_id ) {
return false;
}
$form = \GFAPI::get_form($form_id);
// Invalid form ID
if ( !$form ) {
return false;
}
$file_fields = array();
$url_field = array();
$values = array();
foreach ( $form['fields'] as $field ) {
if ( isset($field["inputs"]) && is_array( $field['inputs'] ) ) {
foreach ( $field['inputs'] as $input ) {
// Extract best label
$key = $input['label'] ? $input['label'] : \GFCommon::get_label($field, (string)$input["id"]);
// Redundant formatting
$key = strtolower( str_replace(array(' '), array('_'), $key) );
$value = isset($lead[(string)$input['id']]) ? $lead[(string)$input['id']] : "";
$values[$key] = htmlentities(stripslashes($value), ENT_QUOTES);
}
} elseif ( !rgar($field, 'displayOnly') ) {
// Extract best label
$key = isset( $field['adminLabel'] ) && $field['adminLabel'] != "" ? $field['adminLabel'] : ( $field['label'] ? $field['label'] : \GFCommon::get_label($field) );
// More redundant formatting
$key = strtolower( str_replace(array(' '), array('_'), $key) );
$value = isset($lead[$field['id']]) ? $lead[$field['id']] : "";
$values[$key] = htmlentities(stripslashes($value), ENT_QUOTES);
}
}
$values['logo'] = $this->logo ? $this->logo : $values['logo']; // An image field being re-assign from post file data
try {
$this->assign($values);
} catch ( Exception $e ) {
$error = $e->getMessage();
}
if ( !$this->product_id && isset($lead['source_url']) ) { // only needed if used in conjunction with woocommerce fulfillment
$this->product_id = url_to_postid( $lead['source_url'] );
}
return true;
}
private function fill_fields( &$data ) {
//$message = new DevString($data['message']);
// custom assign any field you want here. example:
// $this->field_1 = "Something you want it to say";
foreach ( $data as $key=>$value) {
$data[$key] = html_entity_decode($value, ENT_QUOTES);
}
//print_r($data);
}
public function save() {
$status = parent::save();
$status = update_post_meta($this->_post_id, self::CONFIG_META, $this->_config);
return $status;
}
public function get_data() {
return $this->_data;
}
}