PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 1.07.11
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v1.07.11
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / helpers / FrmXMLHelper.php

FrmXMLHelper.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 1.07.11, at classes/helpers/FrmXMLHelper.php

255 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( !defined('ABSPATH') ) die('You are not allowed to call this page directly.');
3
4 class FrmXMLHelper{
5
6 public static function get_xml_values($opt, $padding){
7 if(is_array($opt)){
8 foreach($opt as $ok => $ov){
9 echo "\n". $padding;
10 echo '<'. (is_numeric($ok) ? 'key:' : '') . $ok .'>';
11 self::get_xml_values($ov, $padding .' ');
12 if(is_array($ov))
13 echo "\n". $padding;
14 echo '</'. (is_numeric($ok) ? 'key:' : '') . $ok .'>';
15 }
16 }else{
17 echo self::cdata($opt);
18 }
19 }
20
21 public static function import_xml($file){
22 $defaults = array(
23 'forms' => 0, 'fields' => 0, 'terms' => 0,
24 );
25
26 $imported = array(
27 'imported' => $defaults,
28 'updated' => $defaults,
29 'forms' => array(),
30 );
31
32 unset($defaults);
33
34 if ( !defined('WP_IMPORTING') ) {
35 define('WP_IMPORTING', true);
36 }
37
38 if ( !class_exists('DOMDocument') ) {
39 return new WP_Error( 'SimpleXML_parse_error', __( 'Your server does not have XML enabled', 'formidable' ), libxml_get_errors() );
40 }
41
42 $dom = new DOMDocument;
43 $success = $dom->loadXML( file_get_contents( $file ) );
44 if ( !$success ) {
45 return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this XML file', 'formidable' ), libxml_get_errors() );
46 }
47
48 $xml = simplexml_import_dom( $dom );
49 unset( $dom );
50
51 // halt if loading produces an error
52 if ( !$xml ) {
53 return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this XML file', 'formidable' ), libxml_get_errors() );
54 }
55
56 // add terms, forms (form and field ids), posts (post ids), and entries to db, in that order
57
58 // grab cats, tags and terms
59 if ( isset($xml->term) ) {
60 $imported = self::import_xml_terms($xml->term, $imported);
61 unset($xml->term);
62 }
63
64 if ( isset($xml->form) ) {
65 $imported = self::import_xml_forms($xml->form, $imported);
66 unset($xml->form);
67 }
68
69 $return = apply_filters('frm_importing_xml', $imported, $xml );
70
71 return $return;
72 }
73
74 public static function import_xml_terms($terms, $imported) {
75 foreach ( $terms as $t ) {
76 if ( term_exists((string) $t->term_slug, (string) $t->term_taxonomy) ) {
77 continue;
78 }
79
80 $term_id = wp_insert_term( (string) $t->term_name, (string) $t->term_taxonomy, array(
81 'slug' => (string) $t->term_slug,
82 'description' => (string) $t->term_description,
83 'term_parent' => (string) $t->term_parent,
84 'slug' => (string) $t->term_slug,
85 ));
86
87 if ( $term_id ) {
88 $imported['imported']['terms']++;
89 }
90
91 unset($term_id);
92 unset($t);
93 }
94
95 return $imported;
96 }
97
98 public static function import_xml_forms($forms, $imported) {
99 $frm_form = new FrmForm();
100 $frm_field = new FrmField();
101
102 foreach ( $forms as $item ) {
103 $form = array(
104 'id' => (int) $item->id,
105 'form_key' => (string) $item->form_key,
106 'name' => (string) $item->name,
107 'description' => (string) $item->description,
108 'options' => (string) $item->options,
109 'logged_in' => (int) $item->logged_in,
110 'is_template' => (int) $item->is_template,
111 'default_template' => (int) $item->default_template,
112 'editable' => (int) $item->editable,
113 'status' => (string) $item->status,
114 'created_at' => date('Y-m-d H:i:s', strtotime((string) $item->created_at)),
115 );
116
117 $form['options'] = FrmAppHelper::maybe_json_decode($form['options']);
118
119 // if template, allow to edit if form keys match, otherwise, creation date must also match
120 $edit_query = array('form_key' => $form['form_key'], 'is_template' => $form['is_template']);
121 if ( !$form['is_template'] ) {
122 $edit_query['created_at'] = $form['created_at'];
123 }
124
125 $edit_query = apply_filters('frm_match_xml_form', $edit_query, $form);
126
127 $this_form = $frm_form->getAll($edit_query, '', 1);
128 unset($edit_query);
129
130 if ( !empty($this_form) ) {
131 $form_id = $this_form->id;
132 $frm_form->update($form_id, $form );
133 $imported['updated']['forms']++;
134
135 $form_fields = $frm_field->getAll(array('fi.form_id' => $form_id), 'field_order');
136 $old_fields = array();
137 foreach ( $form_fields as $f ) {
138 $old_fields[$f->id] = $f;
139 $old_fields[$f->field_key] = $f->id;
140 unset($f);
141 }
142 $form_fields = $old_fields;
143 unset($old_fields);
144 } else {
145 //form does not exist, so create it
146 if ( $form_id = $frm_form->create( $form ) ) {
147 $imported['imported']['forms']++;
148 }
149 }
150
151 foreach ( $item->field as $field ) {
152 $f = array(
153 'id' => (int) $field->id,
154 'field_key' => (string) $field->field_key,
155 'name' => (string) $field->name,
156 'description' => (string) $field->description,
157 'type' => (string) $field->type,
158 'default_value' => FrmAppHelper::maybe_json_decode( (string) $field->default_value),
159 'field_order' => (int) $field->field_order,
160 'form_id' => (int) $form_id,
161 'required' => (int) $field->required,
162 'options' => FrmAppHelper::maybe_json_decode( (string) $field->options),
163 'field_options' => FrmAppHelper::maybe_json_decode( (string) $field->field_options)
164 );
165
166 if ( is_array($f['default_value']) && in_array($f['type'], array('text', 'email', 'url', 'textarea', 'number', 'phone', 'date', 'time', 'image', 'hidden', 'password', 'tag')) ) {
167 if ( count($f['default_value']) === 1 ) {
168 $f['default_value'] = '['. reset($f['default_value']) .']';
169 } else {
170 $f['default_value'] = reset($f['default_value']);
171 }
172 }
173
174 $f = apply_filters('frm_duplicated_field', $f);
175
176 if ( $this_form ) {
177 // check for field to edit by field id
178 if ( isset($form_fields[$f['id']]) ) {
179 $frm_field->update( $f['id'], $f );
180 $imported['updated']['fields']++;
181
182 unset($form_fields[$f['id']]);
183
184 //unset old field key
185 if ( isset($form_fields[$f['field_key']]) ) {
186 unset($form_fields[$f['field_key']]);
187 }
188 } else if ( isset($form_fields[$f['field_key']]) ) {
189 // check for field to edit by field key
190 unset($f['id']);
191
192 $frm_field->update( $form_fields[$f['field_key']], $f );
193 $imported['updated']['fields']++;
194
195 unset($form_fields[$form_fields[$f['field_key']]]); //unset old field id
196 unset($form_fields[$f['field_key']]); //unset old field key
197 } else if ( $frm_field->create( $f ) ) {
198 // if no matching field id or key in this form, create the field
199 $imported['imported']['fields']++;
200 }
201 } else if ( $frm_field->create( $f ) ) {
202 $imported['imported']['fields']++;
203 }
204
205 unset($field);
206 }
207
208
209 // Delete any fields attached to this form that were not included in the template
210 if ( isset($form_fields) && !empty($form_fields) ) {
211 foreach ($form_fields as $field){
212 if ( is_object($field) ) {
213 $frm_field->destroy($field->id);
214 }
215 unset($field);
216 }
217 unset($form_fields);
218 }
219
220
221 // Update field ids/keys to new ones
222 do_action('frm_after_duplicate_form', $form_id, $form);
223
224 $imported['forms'][ (int) $item->id] = $form_id;
225
226 unset($form);
227 unset($item);
228 }
229
230 unset($frm_form);
231 unset($frm_field);
232
233 return $imported;
234 }
235
236 public static function cdata( $str ) {
237 $str = maybe_unserialize($str);
238 if ( is_array($str) ) {
239 $str = json_encode($str);
240 } else if (seems_utf8( $str ) == false ) {
241 $str = utf8_encode( $str );
242 }
243
244 if ( is_numeric($str) ) {
245 return $str;
246 }
247
248 // $str = ent2ncr(esc_html($str));
249 $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
250
251 return $str;
252 }
253
254 }
255