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 FrmXMLHelper 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 FrmXMLHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class FrmXMLHelper { |
||
| 7 | |||
| 8 | public static function get_xml_values( $opt, $padding ) { |
||
| 9 | if ( is_array( $opt ) ) { |
||
| 10 | foreach ( $opt as $ok => $ov ) { |
||
| 11 | echo "\n" . $padding; |
||
|
|
|||
| 12 | $tag = ( is_numeric( $ok ) ? 'key:' : '' ) . $ok; |
||
| 13 | echo '<' . $tag . '>'; |
||
| 14 | self::get_xml_values( $ov, $padding . ' ' ); |
||
| 15 | if ( is_array( $ov ) ) { |
||
| 16 | echo "\n" . $padding; |
||
| 17 | } |
||
| 18 | echo '</' . $tag . '>'; |
||
| 19 | } |
||
| 20 | } else { |
||
| 21 | echo self::cdata( $opt ); |
||
| 22 | } |
||
| 23 | } |
||
| 24 | |||
| 25 | public static function import_xml( $file ) { |
||
| 26 | $defaults = array( |
||
| 27 | 'forms' => 0, 'fields' => 0, 'terms' => 0, |
||
| 28 | 'posts' => 0, 'views' => 0, 'actions' => 0, |
||
| 29 | 'styles' => 0, |
||
| 30 | ); |
||
| 31 | |||
| 32 | $imported = array( |
||
| 33 | 'imported' => $defaults, |
||
| 34 | 'updated' => $defaults, |
||
| 35 | 'forms' => array(), |
||
| 36 | 'terms' => array(), |
||
| 37 | ); |
||
| 38 | |||
| 39 | unset($defaults); |
||
| 40 | |||
| 41 | if ( ! defined( 'WP_IMPORTING' ) ) { |
||
| 42 | define('WP_IMPORTING', true); |
||
| 43 | } |
||
| 44 | |||
| 45 | if ( ! class_exists( 'DOMDocument' ) ) { |
||
| 46 | return new WP_Error( 'SimpleXML_parse_error', __( 'Your server does not have XML enabled', 'formidable' ), libxml_get_errors() ); |
||
| 47 | } |
||
| 48 | |||
| 49 | $dom = new DOMDocument; |
||
| 50 | $success = $dom->loadXML( file_get_contents( $file ) ); |
||
| 51 | if ( ! $success ) { |
||
| 52 | return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this XML file', 'formidable' ), libxml_get_errors() ); |
||
| 53 | } |
||
| 54 | |||
| 55 | if ( ! function_exists('simplexml_import_dom') ) { |
||
| 56 | return new WP_Error( 'SimpleXML_parse_error', __( 'Your server is missing the simplexml_import_dom function', 'formidable' ), libxml_get_errors() ); |
||
| 57 | } |
||
| 58 | |||
| 59 | $xml = simplexml_import_dom( $dom ); |
||
| 60 | unset( $dom ); |
||
| 61 | |||
| 62 | // halt if loading produces an error |
||
| 63 | if ( ! $xml ) { |
||
| 64 | return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this XML file', 'formidable' ), libxml_get_errors() ); |
||
| 65 | } |
||
| 66 | |||
| 67 | // add terms, forms (form and field ids), posts (post ids), and entries to db, in that order |
||
| 68 | foreach ( array( 'term', 'form', 'view' ) as $item_type ) { |
||
| 69 | // grab cats, tags, and terms, or forms or posts |
||
| 70 | if ( isset($xml->{$item_type} ) ) { |
||
| 71 | $function_name = 'import_xml_' . $item_type . 's'; |
||
| 72 | $imported = self::$function_name( $xml->{$item_type}, $imported ); |
||
| 73 | unset( $function_name, $xml->{$item_type} ); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | $return = apply_filters('frm_importing_xml', $imported, $xml ); |
||
| 78 | |||
| 79 | return $return; |
||
| 80 | } |
||
| 81 | |||
| 82 | public static function import_xml_terms( $terms, $imported ) { |
||
| 83 | foreach ( $terms as $t ) { |
||
| 84 | if ( term_exists( (string) $t->term_slug, (string) $t->term_taxonomy ) ) { |
||
| 85 | continue; |
||
| 86 | } |
||
| 87 | |||
| 88 | $parent = self::get_term_parent_id( $t ); |
||
| 89 | |||
| 90 | $term = wp_insert_term( (string) $t->term_name, (string) $t->term_taxonomy, array( |
||
| 91 | 'slug' => (string) $t->term_slug, |
||
| 92 | 'description' => (string) $t->term_description, |
||
| 93 | 'parent' => empty( $parent ) ? 0 : $parent, |
||
| 94 | )); |
||
| 95 | |||
| 96 | if ( $term && is_array( $term ) ) { |
||
| 97 | $imported['imported']['terms']++; |
||
| 98 | $imported['terms'][ (int) $t->term_id ] = $term['term_id']; |
||
| 99 | } |
||
| 100 | |||
| 101 | unset( $term, $t ); |
||
| 102 | } |
||
| 103 | |||
| 104 | return $imported; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @since 2.0.8 |
||
| 109 | */ |
||
| 110 | private static function get_term_parent_id( $t ) { |
||
| 111 | $parent = (string) $t->term_parent; |
||
| 112 | if ( ! empty( $parent ) ) { |
||
| 113 | $parent = term_exists( (string) $t->term_parent, (string) $t->term_taxonomy ); |
||
| 114 | if ( $parent ) { |
||
| 115 | $parent = $parent['term_id']; |
||
| 116 | } else { |
||
| 117 | $parent = 0; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | return $parent; |
||
| 121 | } |
||
| 122 | |||
| 123 | public static function import_xml_forms( $forms, $imported ) { |
||
| 124 | $child_forms = array(); |
||
| 125 | |||
| 126 | // Import child forms first |
||
| 127 | self::put_child_forms_first( $forms ); |
||
| 128 | |||
| 129 | foreach ( $forms as $item ) { |
||
| 130 | $form = self::fill_form( $item ); |
||
| 131 | |||
| 132 | self::update_custom_style_setting_on_import( $form ); |
||
| 133 | |||
| 134 | $this_form = self::maybe_get_form( $form ); |
||
| 135 | |||
| 136 | $old_id = false; |
||
| 137 | $form_fields = false; |
||
| 138 | if ( ! empty( $this_form ) ) { |
||
| 139 | $form_id = $this_form->id; |
||
| 140 | $old_id = $this_form->id |
||
| 141 | self::update_form( $this_form, $form, $imported ); |
||
| 142 | |||
| 143 | $form_fields = self::get_form_fields( $form_id ); |
||
| 144 | } else { |
||
| 145 | $form_id = FrmForm::create( $form ); |
||
| 146 | if ( $form_id ) { |
||
| 147 | $imported['imported']['forms']++; |
||
| 148 | // Keep track of whether this specific form was updated or not |
||
| 149 | $imported['form_status'][ $form_id ] = 'imported'; |
||
| 150 | self::track_imported_child_forms( (int) $form_id, $form['parent_form_id'], $child_forms ); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | self::import_xml_fields( $item->field, $form_id, $this_form, $form_fields, $imported ); |
||
| 155 | |||
| 156 | self::delete_removed_fields( $form_fields ); |
||
| 157 | |||
| 158 | // Update field ids/keys to new ones |
||
| 159 | do_action( 'frm_after_duplicate_form', $form_id, $form, array( 'old_id' => $old_id ) ); |
||
| 160 | |||
| 161 | $imported['forms'][ (int) $item->id ] = $form_id; |
||
| 162 | |||
| 163 | // Send pre 2.0 form options through function that creates actions |
||
| 164 | self::migrate_form_settings_to_actions( $form['options'], $form_id, $imported, true ); |
||
| 165 | |||
| 166 | do_action( 'frm_after_import_form', $form_id, $form ); |
||
| 167 | |||
| 168 | unset($form, $item); |
||
| 169 | } |
||
| 170 | |||
| 171 | self::maybe_update_child_form_parent_id( $imported['forms'], $child_forms ); |
||
| 172 | |||
| 173 | return $imported; |
||
| 174 | } |
||
| 175 | |||
| 176 | private static function fill_form( $item ) { |
||
| 177 | $form = array( |
||
| 178 | 'id' => (int) $item->id, |
||
| 179 | 'form_key' => (string) $item->form_key, |
||
| 180 | 'name' => (string) $item->name, |
||
| 181 | 'description' => (string) $item->description, |
||
| 182 | 'options' => (string) $item->options, |
||
| 183 | 'logged_in' => (int) $item->logged_in, |
||
| 184 | 'is_template' => (int) $item->is_template, |
||
| 185 | 'default_template' => (int) $item->default_template, |
||
| 186 | 'editable' => (int) $item->editable, |
||
| 187 | 'status' => (string) $item->status, |
||
| 188 | 'parent_form_id' => isset( $item->parent_form_id ) ? (int) $item->parent_form_id : 0, |
||
| 189 | 'created_at' => date( 'Y-m-d H:i:s', strtotime( (string) $item->created_at ) ), |
||
| 190 | ); |
||
| 191 | $form['options'] = FrmAppHelper::maybe_json_decode( $form['options'] ); |
||
| 192 | return $form; |
||
| 193 | } |
||
| 194 | |||
| 195 | private static function maybe_get_form( $form ) { |
||
| 196 | // if template, allow to edit if form keys match, otherwise, creation date must also match |
||
| 197 | $edit_query = array( 'form_key' => $form['form_key'], 'is_template' => $form['is_template'] ); |
||
| 198 | if ( ! $form['is_template'] ) { |
||
| 199 | $edit_query['created_at'] = $form['created_at']; |
||
| 200 | } |
||
| 201 | |||
| 202 | $edit_query = apply_filters( 'frm_match_xml_form', $edit_query, $form ); |
||
| 203 | |||
| 204 | return FrmForm::getAll( $edit_query, '', 1 ); |
||
| 205 | } |
||
| 206 | |||
| 207 | private static function update_form( $this_form, $form, &$imported ) { |
||
| 208 | $form_id = $this_form->id; |
||
| 209 | FrmForm::update( $form_id, $form ); |
||
| 210 | $imported['updated']['forms']++; |
||
| 211 | // Keep track of whether this specific form was updated or not |
||
| 212 | $imported['form_status'][ $form_id ] = 'updated'; |
||
| 213 | } |
||
| 214 | |||
| 215 | private static function get_form_fields( $form_id ) { |
||
| 216 | $form_fields = FrmField::get_all_for_form( $form_id, '', 'exclude', 'exclude' ); |
||
| 217 | $old_fields = array(); |
||
| 218 | foreach ( $form_fields as $f ) { |
||
| 219 | $old_fields[ $f->id ] = $f; |
||
| 220 | $old_fields[ $f->field_key ] = $f->id; |
||
| 221 | unset($f); |
||
| 222 | } |
||
| 223 | $form_fields = $old_fields; |
||
| 224 | return $form_fields; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Delete any fields attached to this form that were not included in the template |
||
| 229 | */ |
||
| 230 | private static function delete_removed_fields( $form_fields ) { |
||
| 231 | if ( ! empty( $form_fields ) ) { |
||
| 232 | foreach ( $form_fields as $field ) { |
||
| 233 | if ( is_object( $field ) ) { |
||
| 234 | FrmField::destroy( $field->id ); |
||
| 235 | } |
||
| 236 | unset( $field ); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Put child forms first so they will be imported before parents |
||
| 243 | * |
||
| 244 | * @since 2.0.16 |
||
| 245 | * @param array $forms |
||
| 246 | */ |
||
| 247 | private static function put_child_forms_first( &$forms ) { |
||
| 248 | $child_forms = array(); |
||
| 249 | $regular_forms = array(); |
||
| 250 | |||
| 251 | foreach ( $forms as $form ) { |
||
| 252 | $parent_form_id = isset( $form->parent_form_id) ? (int) $form->parent_form_id : 0; |
||
| 253 | |||
| 254 | if ( $parent_form_id ) { |
||
| 255 | $child_forms[] = $form; |
||
| 256 | } else { |
||
| 257 | $regular_forms[] = $form; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | $forms = array_merge( $child_forms, $regular_forms ); |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Keep track of all imported child forms |
||
| 266 | * |
||
| 267 | * @since 2.0.16 |
||
| 268 | * @param int $form_id |
||
| 269 | * @param int $parent_form_id |
||
| 270 | * @param array $child_forms |
||
| 271 | */ |
||
| 272 | private static function track_imported_child_forms( $form_id, $parent_form_id, &$child_forms ) { |
||
| 273 | if ( $parent_form_id ) { |
||
| 274 | $child_forms[ $form_id ] = $parent_form_id; |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Update the parent_form_id on imported child forms |
||
| 280 | * Child forms are imported first so their parent_form_id will need to be updated after the parent is imported |
||
| 281 | * |
||
| 282 | * @since 2.0.6 |
||
| 283 | * @param array $imported_forms |
||
| 284 | * @param array $child_forms |
||
| 285 | */ |
||
| 286 | private static function maybe_update_child_form_parent_id( $imported_forms, $child_forms ) { |
||
| 287 | foreach ( $child_forms as $child_form_id => $old_parent_form_id ) { |
||
| 288 | |||
| 289 | if ( isset( $imported_forms[ $old_parent_form_id ] ) && $imported_forms[ $old_parent_form_id ] != $old_parent_form_id ) { |
||
| 290 | // Update all children with this old parent_form_id |
||
| 291 | $new_parent_form_id = (int) $imported_forms[ $old_parent_form_id ]; |
||
| 292 | |||
| 293 | FrmForm::update( $child_form_id, array( 'parent_form_id' => $new_parent_form_id ) ); |
||
| 294 | } |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Import all fields for a form |
||
| 300 | * @since 2.0.13 |
||
| 301 | * |
||
| 302 | * TODO: Cut down on params |
||
| 303 | */ |
||
| 304 | private static function import_xml_fields( $xml_fields, $form_id, $this_form, &$form_fields, &$imported ) { |
||
| 305 | $in_section = 0; |
||
| 306 | |||
| 307 | foreach ( $xml_fields as $field ) { |
||
| 308 | $f = self::fill_field( $field, $form_id ); |
||
| 309 | |||
| 310 | if ( is_array($f['default_value']) && in_array($f['type'], array( |
||
| 311 | 'text', 'email', 'url', 'textarea', |
||
| 312 | 'number','phone', 'date', 'time', |
||
| 313 | 'hidden', 'password', 'tag', 'image', |
||
| 314 | )) ) { |
||
| 315 | if ( count($f['default_value']) === 1 ) { |
||
| 316 | $f['default_value'] = '[' . reset( $f['default_value'] ) . ']'; |
||
| 317 | } else { |
||
| 318 | $f['default_value'] = reset($f['default_value']); |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | self::maybe_update_in_section_variable( $in_section, $f ); |
||
| 323 | self::maybe_update_form_select( $f, $imported ); |
||
| 324 | self::maybe_update_get_values_form_setting( $imported, $f ); |
||
| 325 | |||
| 326 | if ( ! empty($this_form) ) { |
||
| 327 | // check for field to edit by field id |
||
| 328 | if ( isset( $form_fields[ $f['id'] ] ) ) { |
||
| 329 | FrmField::update( $f['id'], $f ); |
||
| 330 | $imported['updated']['fields']++; |
||
| 331 | |||
| 332 | unset( $form_fields[ $f['id'] ] ); |
||
| 333 | |||
| 334 | //unset old field key |
||
| 335 | if ( isset( $form_fields[ $f['field_key'] ] ) ) { |
||
| 336 | unset( $form_fields[ $f['field_key'] ] ); |
||
| 337 | } |
||
| 338 | } else if ( isset( $form_fields[ $f['field_key'] ] ) ) { |
||
| 339 | // check for field to edit by field key |
||
| 340 | unset($f['id']); |
||
| 341 | |||
| 342 | FrmField::update( $form_fields[ $f['field_key'] ], $f ); |
||
| 343 | $imported['updated']['fields']++; |
||
| 344 | |||
| 345 | unset( $form_fields[ $form_fields[ $f['field_key'] ] ] ); //unset old field id |
||
| 346 | unset( $form_fields[ $f['field_key'] ] ); //unset old field key |
||
| 347 | } else { |
||
| 348 | // if no matching field id or key in this form, create the field |
||
| 349 | self::create_imported_field( $f, $imported ); |
||
| 350 | } |
||
| 351 | } else { |
||
| 352 | |||
| 353 | self::create_imported_field( $f, $imported ); |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | private static function fill_field( $field, $form_id ) { |
||
| 359 | return array( |
||
| 360 | 'id' => (int) $field->id, |
||
| 361 | 'field_key' => (string) $field->field_key, |
||
| 362 | 'name' => (string) $field->name, |
||
| 363 | 'description' => (string) $field->description, |
||
| 364 | 'type' => (string) $field->type, |
||
| 365 | 'default_value' => FrmAppHelper::maybe_json_decode( (string) $field->default_value), |
||
| 366 | 'field_order' => (int) $field->field_order, |
||
| 367 | 'form_id' => (int) $form_id, |
||
| 368 | 'required' => (int) $field->required, |
||
| 369 | 'options' => FrmAppHelper::maybe_json_decode( (string) $field->options), |
||
| 370 | 'field_options' => FrmAppHelper::maybe_json_decode( (string) $field->field_options ), |
||
| 371 | ); |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Update the current in_section value |
||
| 376 | * |
||
| 377 | * @since 2.0.25 |
||
| 378 | * @param int $in_section |
||
| 379 | * @param array $f |
||
| 380 | */ |
||
| 381 | private static function maybe_update_in_section_variable( &$in_section, &$f ) { |
||
| 382 | // If we're at the end of a section, switch $in_section is 0 |
||
| 383 | if ( in_array( $f['type'], array( 'end_divider', 'break', 'form' ) ) ) { |
||
| 384 | $in_section = 0; |
||
| 385 | } |
||
| 386 | |||
| 387 | // Update the current field's in_section value |
||
| 388 | if ( ! isset( $f['field_options']['in_section'] ) ) { |
||
| 389 | $f['field_options']['in_section'] = $in_section; |
||
| 390 | } |
||
| 391 | |||
| 392 | // If we're starting a new section, switch $in_section to ID of divider |
||
| 393 | if ( $f['type'] == 'divider' ) { |
||
| 394 | $in_section = $f['id']; |
||
| 395 | } |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Switch the form_select on a repeating field or embedded form if it needs to be switched |
||
| 400 | * |
||
| 401 | * @since 2.0.16 |
||
| 402 | * @param array $f |
||
| 403 | * @param array $imported |
||
| 404 | */ |
||
| 405 | private static function maybe_update_form_select( &$f, $imported ) { |
||
| 406 | if ( ! isset( $imported['forms'] ) ) { |
||
| 407 | return; |
||
| 408 | } |
||
| 409 | |||
| 410 | if ( $f['type'] == 'form' || ( $f['type'] == 'divider' && FrmField::is_option_true( $f['field_options'], 'repeat' ) ) ) { |
||
| 411 | if ( FrmField::is_option_true( $f['field_options'], 'form_select' ) ) { |
||
| 412 | $form_select = $f['field_options']['form_select']; |
||
| 413 | if ( isset( $imported['forms'][ $form_select ] ) ) { |
||
| 414 | $f['field_options']['form_select'] = $imported['forms'][ $form_select ]; |
||
| 415 | } |
||
| 416 | } |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Update the get_values_form setting if the form was imported |
||
| 422 | * |
||
| 423 | * @since 2.01.0 |
||
| 424 | * @param array $imported |
||
| 425 | * @param array $f |
||
| 426 | */ |
||
| 427 | private static function maybe_update_get_values_form_setting( $imported, &$f ) { |
||
| 428 | if ( ! isset( $imported['forms'] ) ) { |
||
| 429 | return; |
||
| 430 | } |
||
| 431 | |||
| 432 | if ( FrmField::is_option_true_in_array( $f['field_options'], 'get_values_form' ) ) { |
||
| 433 | $old_form = $f['field_options']['get_values_form']; |
||
| 434 | if ( isset( $imported['forms'][ $old_form ] ) ) { |
||
| 435 | $f['field_options']['get_values_form'] = $imported['forms'][ $old_form ]; |
||
| 436 | } |
||
| 437 | } |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Create an imported field |
||
| 442 | * |
||
| 443 | * @since 2.0.25 |
||
| 444 | * @param array $f |
||
| 445 | * @param array $imported |
||
| 446 | */ |
||
| 447 | private static function create_imported_field( $f, &$imported ) { |
||
| 448 | $new_id = FrmField::create( $f ); |
||
| 449 | if ( $new_id != false ) { |
||
| 450 | $imported['imported']['fields']++; |
||
| 451 | do_action( 'frm_after_field_is_imported', $f, $new_id ); |
||
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Updates the custom style setting on import |
||
| 457 | * Convert the post slug to an ID |
||
| 458 | * |
||
| 459 | * @since 2.0.19 |
||
| 460 | * @param array $form |
||
| 461 | * |
||
| 462 | */ |
||
| 463 | private static function update_custom_style_setting_on_import( &$form ) { |
||
| 464 | if ( ! isset( $form['options']['custom_style'] ) ) { |
||
| 465 | return; |
||
| 466 | } |
||
| 467 | |||
| 468 | if ( is_numeric( $form['options']['custom_style'] ) ) { |
||
| 469 | // Set to default |
||
| 470 | $form['options']['custom_style'] = 1; |
||
| 471 | } else { |
||
| 472 | // Replace the style name with the style ID on import |
||
| 473 | global $wpdb; |
||
| 474 | $table = $wpdb->prefix . 'posts'; |
||
| 475 | $where = array( |
||
| 476 | 'post_name' => $form['options']['custom_style'], |
||
| 477 | 'post_type' => 'frm_styles', |
||
| 478 | ); |
||
| 479 | $select = 'ID'; |
||
| 480 | $style_id = FrmDb::get_var( $table, $where, $select ); |
||
| 481 | |||
| 482 | if ( $style_id ) { |
||
| 483 | $form['options']['custom_style'] = $style_id; |
||
| 484 | } else { |
||
| 485 | // save the old style to maybe update after styles import |
||
| 486 | $form['options']['old_style'] = $form['options']['custom_style']; |
||
| 487 | |||
| 488 | // Set to default |
||
| 489 | $form['options']['custom_style'] = 1; |
||
| 490 | } |
||
| 491 | } |
||
| 492 | } |
||
| 493 | |||
| 494 | /** |
||
| 495 | * After styles are imported, check for any forms that were linked |
||
| 496 | * and link them back up. |
||
| 497 | * |
||
| 498 | * @since 2.2.7 |
||
| 499 | */ |
||
| 500 | private static function update_custom_style_setting_after_import( $form_id ) { |
||
| 501 | $form = FrmForm::getOne( $form_id ); |
||
| 502 | |||
| 503 | if ( $form && isset( $form->options['old_style'] ) ) { |
||
| 504 | $form = (array) $form; |
||
| 505 | $saved_style = $form['options']['custom_style']; |
||
| 506 | $form['options']['custom_style'] = $form['options']['old_style']; |
||
| 507 | self::update_custom_style_setting_on_import( $form ); |
||
| 508 | $has_changed = ( $form['options']['custom_style'] != $saved_style && $form['options']['custom_style'] != $form['options']['old_style'] ); |
||
| 509 | if ( $has_changed ) { |
||
| 510 | FrmForm::update( $form['id'], $form ); |
||
| 511 | } |
||
| 512 | } |
||
| 513 | } |
||
| 514 | |||
| 515 | public static function import_xml_views( $views, $imported ) { |
||
| 516 | $imported['posts'] = array(); |
||
| 517 | $form_action_type = FrmFormActionsController::$action_post_type; |
||
| 518 | |||
| 519 | $post_types = array( |
||
| 520 | 'frm_display' => 'views', |
||
| 521 | $form_action_type => 'actions', |
||
| 522 | 'frm_styles' => 'styles', |
||
| 523 | ); |
||
| 524 | |||
| 525 | foreach ( $views as $item ) { |
||
| 526 | $post = array( |
||
| 527 | 'post_title' => (string) $item->title, |
||
| 528 | 'post_name' => (string) $item->post_name, |
||
| 529 | 'post_type' => (string) $item->post_type, |
||
| 530 | 'post_password' => (string) $item->post_password, |
||
| 531 | 'guid' => (string) $item->guid, |
||
| 532 | 'post_status' => (string) $item->status, |
||
| 533 | 'post_author' => FrmAppHelper::get_user_id_param( (string) $item->post_author ), |
||
| 534 | 'post_id' => (int) $item->post_id, |
||
| 535 | 'post_parent' => (int) $item->post_parent, |
||
| 536 | 'menu_order' => (int) $item->menu_order, |
||
| 537 | 'post_content' => FrmFieldsHelper::switch_field_ids( (string) $item->content ), |
||
| 538 | 'post_excerpt' => FrmFieldsHelper::switch_field_ids( (string) $item->excerpt ), |
||
| 539 | 'is_sticky' => (string) $item->is_sticky, |
||
| 540 | 'comment_status' => (string) $item->comment_status, |
||
| 541 | 'post_date' => (string) $item->post_date, |
||
| 542 | 'post_date_gmt' => (string) $item->post_date_gmt, |
||
| 543 | 'ping_status' => (string) $item->ping_status, |
||
| 544 | 'postmeta' => array(), |
||
| 545 | 'tax_input' => array(), |
||
| 546 | ); |
||
| 547 | |||
| 548 | $old_id = $post['post_id']; |
||
| 549 | self::populate_post($post, $item, $imported); |
||
| 550 | |||
| 551 | unset($item); |
||
| 552 | |||
| 553 | $post_id = false; |
||
| 554 | if ( $post['post_type'] == $form_action_type ) { |
||
| 555 | $action_control = FrmFormActionsController::get_form_actions( $post['post_excerpt'] ); |
||
| 556 | if ( $action_control && is_object( $action_control ) ) { |
||
| 557 | $post_id = $action_control->maybe_create_action( $post, $imported['form_status'] ); |
||
| 558 | } |
||
| 559 | unset($action_control); |
||
| 560 | } else if ( $post['post_type'] == 'frm_styles' ) { |
||
| 561 | // Properly encode post content before inserting the post |
||
| 562 | $post['post_content'] = FrmAppHelper::maybe_json_decode( $post['post_content'] ); |
||
| 563 | $post['post_content'] = FrmAppHelper::prepare_and_encode( $post['post_content'] ); |
||
| 564 | |||
| 565 | // Create/update post now |
||
| 566 | $post_id = wp_insert_post( $post ); |
||
| 567 | } else { |
||
| 568 | // Create/update post now |
||
| 569 | $post_id = wp_insert_post( $post ); |
||
| 570 | } |
||
| 571 | |||
| 572 | if ( ! is_numeric($post_id) ) { |
||
| 573 | continue; |
||
| 574 | } |
||
| 575 | |||
| 576 | self::update_postmeta($post, $post_id); |
||
| 577 | |||
| 578 | $this_type = 'posts'; |
||
| 579 | if ( isset( $post_types[ $post['post_type'] ] ) ) { |
||
| 580 | $this_type = $post_types[ $post['post_type'] ]; |
||
| 581 | } |
||
| 582 | |||
| 583 | if ( isset($post['ID']) && $post_id == $post['ID'] ) { |
||
| 584 | $imported['updated'][ $this_type ]++; |
||
| 585 | } else { |
||
| 586 | $imported['imported'][ $this_type ]++; |
||
| 587 | } |
||
| 588 | |||
| 589 | unset($post); |
||
| 590 | |||
| 591 | $imported['posts'][ (int) $old_id ] = $post_id; |
||
| 592 | } |
||
| 593 | |||
| 594 | self::maybe_update_stylesheet( $imported ); |
||
| 595 | |||
| 596 | return $imported; |
||
| 597 | } |
||
| 598 | |||
| 599 | private static function populate_post( &$post, $item, $imported ) { |
||
| 600 | if ( isset($item->attachment_url) ) { |
||
| 601 | $post['attachment_url'] = (string) $item->attachment_url; |
||
| 602 | } |
||
| 603 | |||
| 604 | if ( $post['post_type'] == FrmFormActionsController::$action_post_type && isset( $imported['forms'][ (int) $post['menu_order'] ] ) ) { |
||
| 605 | // update to new form id |
||
| 606 | $post['menu_order'] = $imported['forms'][ (int) $post['menu_order'] ]; |
||
| 607 | } |
||
| 608 | |||
| 609 | // Don't allow default styles to take over a site's default style |
||
| 610 | if ( 'frm_styles' == $post['post_type'] ) { |
||
| 611 | $post['menu_order'] = 0; |
||
| 612 | } |
||
| 613 | |||
| 614 | foreach ( $item->postmeta as $meta ) { |
||
| 615 | self::populate_postmeta($post, $meta, $imported); |
||
| 616 | unset($meta); |
||
| 617 | } |
||
| 618 | |||
| 619 | self::populate_taxonomies($post, $item); |
||
| 620 | |||
| 621 | self::maybe_editing_post($post); |
||
| 622 | } |
||
| 623 | |||
| 624 | private static function populate_postmeta( &$post, $meta, $imported ) { |
||
| 625 | global $frm_duplicate_ids; |
||
| 626 | |||
| 627 | $m = array( |
||
| 628 | 'key' => (string) $meta->meta_key, |
||
| 629 | 'value' => (string) $meta->meta_value, |
||
| 630 | ); |
||
| 631 | |||
| 632 | //switch old form and field ids to new ones |
||
| 633 | if ( $m['key'] == 'frm_form_id' && isset($imported['forms'][ (int) $m['value'] ]) ) { |
||
| 634 | $m['value'] = $imported['forms'][ (int) $m['value'] ]; |
||
| 635 | } else { |
||
| 636 | $m['value'] = FrmAppHelper::maybe_json_decode($m['value']); |
||
| 637 | |||
| 638 | if ( ! empty($frm_duplicate_ids) ) { |
||
| 639 | |||
| 640 | if ( $m['key'] == 'frm_dyncontent' ) { |
||
| 641 | $m['value'] = FrmFieldsHelper::switch_field_ids($m['value']); |
||
| 642 | } else if ( $m['key'] == 'frm_options' ) { |
||
| 643 | |||
| 644 | foreach ( array( 'date_field_id', 'edate_field_id' ) as $setting_name ) { |
||
| 645 | if ( isset( $m['value'][ $setting_name ] ) && is_numeric( $m['value'][ $setting_name ] ) && isset( $frm_duplicate_ids[ $m['value'][ $setting_name ] ] ) ) { |
||
| 646 | $m['value'][ $setting_name ] = $frm_duplicate_ids[ $m['value'][ $setting_name ] ]; |
||
| 647 | } |
||
| 648 | } |
||
| 649 | |||
| 650 | $check_dup_array = array(); |
||
| 651 | if ( isset( $m['value']['order_by'] ) && ! empty( $m['value']['order_by'] ) ) { |
||
| 652 | if ( is_numeric( $m['value']['order_by'] ) && isset( $frm_duplicate_ids[ $m['value']['order_by'] ] ) ) { |
||
| 653 | $m['value']['order_by'] = $frm_duplicate_ids[ $m['value']['order_by'] ]; |
||
| 654 | } else if ( is_array( $m['value']['order_by'] ) ) { |
||
| 655 | $check_dup_array[] = 'order_by'; |
||
| 656 | } |
||
| 657 | } |
||
| 658 | |||
| 659 | if ( isset( $m['value']['where'] ) && ! empty( $m['value']['where'] ) ) { |
||
| 660 | $check_dup_array[] = 'where'; |
||
| 661 | } |
||
| 662 | |||
| 663 | foreach ( $check_dup_array as $check_k ) { |
||
| 664 | foreach ( (array) $m['value'][ $check_k ] as $mk => $mv ) { |
||
| 665 | if ( isset( $frm_duplicate_ids[ $mv ] ) ) { |
||
| 666 | $m['value'][ $check_k ][ $mk ] = $frm_duplicate_ids[ $mv ]; |
||
| 667 | } |
||
| 668 | unset($mk, $mv); |
||
| 669 | } |
||
| 670 | } |
||
| 671 | } |
||
| 672 | } |
||
| 673 | } |
||
| 674 | |||
| 675 | if ( ! is_array($m['value']) ) { |
||
| 676 | $m['value'] = FrmAppHelper::maybe_json_decode($m['value']); |
||
| 677 | } |
||
| 678 | |||
| 679 | $post['postmeta'][ (string) $meta->meta_key ] = $m['value']; |
||
| 680 | } |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Add terms to post |
||
| 684 | * @param array $post by reference |
||
| 685 | * @param object $item The XML object data |
||
| 686 | */ |
||
| 687 | private static function populate_taxonomies( &$post, $item ) { |
||
| 688 | foreach ( $item->category as $c ) { |
||
| 689 | $att = $c->attributes(); |
||
| 690 | if ( ! isset( $att['nicename'] ) ) { |
||
| 691 | continue; |
||
| 692 | } |
||
| 693 | |||
| 694 | $taxonomy = (string) $att['domain']; |
||
| 695 | if ( is_taxonomy_hierarchical($taxonomy) ) { |
||
| 696 | $name = (string) $att['nicename']; |
||
| 697 | $h_term = get_term_by('slug', $name, $taxonomy); |
||
| 698 | if ( $h_term ) { |
||
| 699 | $name = $h_term->term_id; |
||
| 700 | } |
||
| 701 | unset($h_term); |
||
| 702 | } else { |
||
| 703 | $name = (string) $c; |
||
| 704 | } |
||
| 705 | |||
| 706 | if ( ! isset( $post['tax_input'][ $taxonomy ] ) ) { |
||
| 707 | $post['tax_input'][ $taxonomy ] = array(); |
||
| 708 | } |
||
| 709 | |||
| 710 | $post['tax_input'][ $taxonomy ][] = $name; |
||
| 711 | unset($name); |
||
| 712 | } |
||
| 713 | } |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Edit post if the key and created time match |
||
| 717 | */ |
||
| 718 | private static function maybe_editing_post( &$post ) { |
||
| 719 | $match_by = array( |
||
| 720 | 'post_type' => $post['post_type'], |
||
| 721 | 'name' => $post['post_name'], |
||
| 722 | 'post_status' => $post['post_status'], |
||
| 723 | 'posts_per_page' => 1, |
||
| 724 | ); |
||
| 725 | |||
| 726 | if ( in_array( $post['post_status'], array( 'trash', 'draft' ) ) ) { |
||
| 727 | $match_by['include'] = $post['post_id']; |
||
| 728 | unset($match_by['name']); |
||
| 729 | } |
||
| 730 | |||
| 731 | $editing = get_posts($match_by); |
||
| 732 | |||
| 733 | if ( ! empty($editing) && current($editing)->post_date == $post['post_date'] ) { |
||
| 734 | // set the id of the post to edit |
||
| 735 | $post['ID'] = current($editing)->ID; |
||
| 736 | } |
||
| 737 | } |
||
| 738 | |||
| 739 | private static function update_postmeta( &$post, $post_id ) { |
||
| 740 | foreach ( $post['postmeta'] as $k => $v ) { |
||
| 741 | if ( '_edit_last' == $k ) { |
||
| 742 | $v = FrmAppHelper::get_user_id_param($v); |
||
| 743 | } else if ( '_thumbnail_id' == $k && FrmAppHelper::pro_is_installed() ) { |
||
| 744 | //change the attachment ID |
||
| 745 | $v = FrmProXMLHelper::get_file_id($v); |
||
| 746 | } |
||
| 747 | |||
| 748 | update_post_meta($post_id, $k, $v); |
||
| 749 | |||
| 750 | unset($k, $v); |
||
| 751 | } |
||
| 752 | } |
||
| 753 | |||
| 754 | private static function maybe_update_stylesheet( $imported ) { |
||
| 755 | $new_styles = isset( $imported['imported']['styles'] ) && ! empty( $imported['imported']['styles'] ); |
||
| 756 | $updated_styles = isset( $imported['updated']['styles'] ) && ! empty( $imported['updated']['styles'] ); |
||
| 757 | if ( $new_styles || $updated_styles ) { |
||
| 758 | if ( is_admin() && function_exists( 'get_filesystem_method' ) ) { |
||
| 759 | $frm_style = new FrmStyle(); |
||
| 760 | $frm_style->update( 'default' ); |
||
| 761 | } |
||
| 762 | foreach ( $imported['forms'] as $form_id ) { |
||
| 763 | self::update_custom_style_setting_after_import( $form_id ); |
||
| 764 | } |
||
| 765 | } |
||
| 766 | } |
||
| 767 | |||
| 768 | /** |
||
| 769 | * @param string $message |
||
| 770 | */ |
||
| 771 | public static function parse_message( $result, &$message, &$errors ) { |
||
| 772 | if ( is_wp_error($result) ) { |
||
| 773 | $errors[] = $result->get_error_message(); |
||
| 774 | } else if ( ! $result ) { |
||
| 775 | return; |
||
| 776 | } |
||
| 777 | |||
| 778 | if ( ! is_array($result) ) { |
||
| 779 | $message = is_string( $result ) ? $result : print_r( $result, 1 ); |
||
| 780 | return; |
||
| 781 | } |
||
| 782 | |||
| 783 | $t_strings = array( |
||
| 784 | 'imported' => __( 'Imported', 'formidable' ), |
||
| 785 | 'updated' => __( 'Updated', 'formidable' ), |
||
| 786 | ); |
||
| 787 | |||
| 788 | $message = '<ul>'; |
||
| 789 | foreach ( $result as $type => $results ) { |
||
| 790 | if ( ! isset( $t_strings[ $type ] ) ) { |
||
| 791 | // only print imported and updated |
||
| 792 | continue; |
||
| 793 | } |
||
| 794 | |||
| 795 | $s_message = array(); |
||
| 796 | foreach ( $results as $k => $m ) { |
||
| 797 | self::item_count_message($m, $k, $s_message); |
||
| 798 | unset($k, $m); |
||
| 799 | } |
||
| 800 | |||
| 801 | if ( ! empty($s_message) ) { |
||
| 802 | $message .= '<li><strong>' . $t_strings[ $type ] . ':</strong> '; |
||
| 803 | $message .= implode(', ', $s_message); |
||
| 804 | $message .= '</li>'; |
||
| 805 | } |
||
| 806 | } |
||
| 807 | |||
| 808 | if ( $message == '<ul>' ) { |
||
| 809 | $message = ''; |
||
| 810 | $errors[] = __( 'Nothing was imported or updated', 'formidable' ); |
||
| 811 | } else { |
||
| 812 | $message .= '</ul>'; |
||
| 813 | } |
||
| 814 | } |
||
| 815 | |||
| 816 | public static function item_count_message( $m, $type, &$s_message ) { |
||
| 817 | if ( ! $m ) { |
||
| 818 | return; |
||
| 819 | } |
||
| 820 | |||
| 821 | $strings = array( |
||
| 822 | 'forms' => sprintf( _n( '%1$s Form', '%1$s Forms', $m, 'formidable' ), $m ), |
||
| 823 | 'fields' => sprintf( _n( '%1$s Field', '%1$s Fields', $m, 'formidable' ), $m ), |
||
| 824 | 'items' => sprintf( _n( '%1$s Entry', '%1$s Entries', $m, 'formidable' ), $m ), |
||
| 825 | 'views' => sprintf( _n( '%1$s View', '%1$s Views', $m, 'formidable' ), $m ), |
||
| 826 | 'posts' => sprintf( _n( '%1$s Post', '%1$s Posts', $m, 'formidable' ), $m ), |
||
| 827 | 'styles' => sprintf( _n( '%1$s Style', '%1$s Styles', $m, 'formidable' ), $m ), |
||
| 828 | 'terms' => sprintf( _n( '%1$s Term', '%1$s Terms', $m, 'formidable' ), $m ), |
||
| 829 | 'actions' => sprintf( _n( '%1$s Form Action', '%1$s Form Actions', $m, 'formidable' ), $m ), |
||
| 830 | ); |
||
| 831 | |||
| 832 | $s_message[] = isset( $strings[ $type ] ) ? $strings[ $type ] : ' ' . $m . ' ' . ucfirst( $type ); |
||
| 833 | } |
||
| 834 | |||
| 835 | /** |
||
| 836 | * Prepare the form options for export |
||
| 837 | * |
||
| 838 | * @since 2.0.19 |
||
| 839 | * @param string $options |
||
| 840 | * @return string |
||
| 841 | */ |
||
| 842 | public static function prepare_form_options_for_export( $options ) { |
||
| 843 | $options = maybe_unserialize( $options ); |
||
| 844 | // Change custom_style to the post_name instead of ID |
||
| 845 | if ( isset( $options['custom_style'] ) && 1 !== $options['custom_style'] ) { |
||
| 846 | global $wpdb; |
||
| 847 | $table = $wpdb->prefix . 'posts'; |
||
| 848 | $where = array( 'ID' => $options['custom_style'] ); |
||
| 849 | $select = 'post_name'; |
||
| 850 | |||
| 851 | $style_name = FrmDb::get_var( $table, $where, $select ); |
||
| 852 | |||
| 853 | if ( $style_name ) { |
||
| 854 | $options['custom_style'] = $style_name; |
||
| 855 | } else { |
||
| 856 | $options['custom_style'] = 1; |
||
| 857 | } |
||
| 858 | } |
||
| 859 | $options = serialize( $options ); |
||
| 860 | return self::cdata( $options ); |
||
| 861 | } |
||
| 862 | |||
| 863 | public static function cdata( $str ) { |
||
| 864 | $str = maybe_unserialize($str); |
||
| 865 | if ( is_array($str) ) { |
||
| 866 | $str = json_encode($str); |
||
| 867 | } else if ( seems_utf8( $str ) == false ) { |
||
| 868 | $str = utf8_encode( $str ); |
||
| 869 | } |
||
| 870 | |||
| 871 | if ( is_numeric($str) ) { |
||
| 872 | return $str; |
||
| 873 | } |
||
| 874 | |||
| 875 | self::remove_invalid_characters_from_xml( $str ); |
||
| 876 | |||
| 877 | // $str = ent2ncr(esc_html($str)); |
||
| 878 | $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>'; |
||
| 879 | |||
| 880 | return $str; |
||
| 881 | } |
||
| 882 | |||
| 883 | /** |
||
| 884 | * Remove <US> character (unit separator) from exported strings |
||
| 885 | * |
||
| 886 | * @since 2.0.22 |
||
| 887 | * @param string $str |
||
| 888 | */ |
||
| 889 | private static function remove_invalid_characters_from_xml( &$str ) { |
||
| 890 | // Remove <US> character |
||
| 891 | $str = str_replace( '\x1F', '', $str ); |
||
| 892 | } |
||
| 893 | |||
| 894 | public static function migrate_form_settings_to_actions( $form_options, $form_id, &$imported = array(), $switch = false ) { |
||
| 895 | // Get post type |
||
| 896 | $post_type = FrmFormActionsController::$action_post_type; |
||
| 897 | |||
| 898 | // Set up imported index, if not set up yet |
||
| 899 | if ( ! isset( $imported['imported']['actions'] ) ) { |
||
| 900 | $imported['imported']['actions'] = 0; |
||
| 901 | } |
||
| 902 | |||
| 903 | // Migrate post settings to action |
||
| 904 | self::migrate_post_settings_to_action( $form_options, $form_id, $post_type, $imported, $switch ); |
||
| 905 | |||
| 906 | // Migrate email settings to action |
||
| 907 | self::migrate_email_settings_to_action( $form_options, $form_id, $post_type, $imported, $switch ); |
||
| 908 | } |
||
| 909 | |||
| 910 | /** |
||
| 911 | * Migrate post settings to form action |
||
| 912 | * |
||
| 913 | * @param string $post_type |
||
| 914 | */ |
||
| 915 | private static function migrate_post_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) { |
||
| 916 | if ( ! isset($form_options['create_post']) || ! $form_options['create_post'] ) { |
||
| 917 | return; |
||
| 918 | } |
||
| 919 | |||
| 920 | $new_action = array( |
||
| 921 | 'post_type' => $post_type, |
||
| 922 | 'post_excerpt' => 'wppost', |
||
| 923 | 'post_title' => __( 'Create Posts', 'formidable' ), |
||
| 924 | 'menu_order' => $form_id, |
||
| 925 | 'post_status' => 'publish', |
||
| 926 | 'post_content' => array(), |
||
| 927 | 'post_name' => $form_id . '_wppost_1', |
||
| 928 | ); |
||
| 929 | |||
| 930 | $post_settings = array( |
||
| 931 | 'post_type', 'post_category', 'post_content', |
||
| 932 | 'post_excerpt', 'post_title', 'post_name', 'post_date', |
||
| 933 | 'post_status', 'post_custom_fields', 'post_password', |
||
| 934 | ); |
||
| 935 | |||
| 936 | foreach ( $post_settings as $post_setting ) { |
||
| 937 | if ( isset( $form_options[ $post_setting ] ) ) { |
||
| 938 | $new_action['post_content'][ $post_setting ] = $form_options[ $post_setting ]; |
||
| 939 | } |
||
| 940 | unset($post_setting); |
||
| 941 | } |
||
| 942 | |||
| 943 | $new_action['event'] = array( 'create', 'update' ); |
||
| 944 | |||
| 945 | if ( $switch ) { |
||
| 946 | // Fields with string or int saved |
||
| 947 | $basic_fields = array( 'post_title', 'post_content', 'post_excerpt', 'post_password', 'post_date', 'post_status' ); |
||
| 948 | |||
| 949 | // Fields with arrays saved |
||
| 950 | $array_fields = array( 'post_category', 'post_custom_fields' ); |
||
| 951 | |||
| 952 | $new_action['post_content'] = self::switch_action_field_ids( $new_action['post_content'], $basic_fields, $array_fields ); |
||
| 953 | } |
||
| 954 | $new_action['post_content'] = json_encode($new_action['post_content']); |
||
| 955 | |||
| 956 | $exists = get_posts( array( |
||
| 957 | 'name' => $new_action['post_name'], |
||
| 958 | 'post_type' => $new_action['post_type'], |
||
| 959 | 'post_status' => $new_action['post_status'], |
||
| 960 | 'numberposts' => 1, |
||
| 961 | ) ); |
||
| 962 | |||
| 963 | if ( ! $exists ) { |
||
| 964 | // this isn't an email, but we need to use a class that will always be included |
||
| 965 | FrmAppHelper::save_json_post( $new_action ); |
||
| 966 | $imported['imported']['actions']++; |
||
| 967 | } |
||
| 968 | } |
||
| 969 | |||
| 970 | /** |
||
| 971 | * Switch old field IDs for new field IDs in emails and post |
||
| 972 | * |
||
| 973 | * @since 2.0 |
||
| 974 | * @param array $post_content - check for old field IDs |
||
| 975 | * @param array $basic_fields - fields with string or int saved |
||
| 976 | * @param array $array_fields - fields with arrays saved |
||
| 977 | * |
||
| 978 | * @return string $post_content - new field IDs |
||
| 979 | */ |
||
| 980 | private static function switch_action_field_ids( $post_content, $basic_fields, $array_fields = array() ) { |
||
| 981 | global $frm_duplicate_ids; |
||
| 982 | |||
| 983 | // If there aren't IDs that were switched, end now |
||
| 984 | if ( ! $frm_duplicate_ids ) { |
||
| 985 | return; |
||
| 986 | } |
||
| 987 | |||
| 988 | // Get old IDs |
||
| 989 | $old = array_keys( $frm_duplicate_ids ); |
||
| 990 | |||
| 991 | // Get new IDs |
||
| 992 | $new = array_values( $frm_duplicate_ids ); |
||
| 993 | |||
| 994 | // Do a str_replace with each item to set the new IDs |
||
| 995 | foreach ( $post_content as $key => $setting ) { |
||
| 996 | if ( ! is_array( $setting ) && in_array( $key, $basic_fields ) ) { |
||
| 997 | // Replace old IDs with new IDs |
||
| 998 | $post_content[ $key ] = str_replace( $old, $new, $setting ); |
||
| 999 | } else if ( is_array( $setting ) && in_array( $key, $array_fields ) ) { |
||
| 1000 | foreach ( $setting as $k => $val ) { |
||
| 1001 | // Replace old IDs with new IDs |
||
| 1002 | $post_content[ $key ][ $k ] = str_replace( $old, $new, $val ); |
||
| 1003 | } |
||
| 1004 | } |
||
| 1005 | unset( $key, $setting ); |
||
| 1006 | } |
||
| 1007 | return $post_content; |
||
| 1008 | } |
||
| 1009 | |||
| 1010 | private static function migrate_email_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) { |
||
| 1011 | // No old notifications or autoresponders to carry over |
||
| 1012 | if ( ! isset( $form_options['auto_responder'] ) && ! isset( $form_options['notification'] ) && ! isset( $form_options['email_to'] ) ) { |
||
| 1013 | return; |
||
| 1014 | } |
||
| 1015 | |||
| 1016 | // Initialize notifications array |
||
| 1017 | $notifications = array(); |
||
| 1018 | |||
| 1019 | // Migrate regular notifications |
||
| 1020 | self::migrate_notifications_to_action( $form_options, $form_id, $notifications ); |
||
| 1021 | |||
| 1022 | // Migrate autoresponders |
||
| 1023 | self::migrate_autoresponder_to_action( $form_options, $form_id, $notifications ); |
||
| 1024 | |||
| 1025 | if ( empty( $notifications ) ) { |
||
| 1026 | return; |
||
| 1027 | } |
||
| 1028 | |||
| 1029 | foreach ( $notifications as $new_notification ) { |
||
| 1030 | $new_notification['post_type'] = $post_type; |
||
| 1031 | $new_notification['post_excerpt'] = 'email'; |
||
| 1032 | $new_notification['post_title'] = __( 'Email Notification', 'formidable' ); |
||
| 1033 | $new_notification['menu_order'] = $form_id; |
||
| 1034 | $new_notification['post_status'] = 'publish'; |
||
| 1035 | |||
| 1036 | // Switch field IDs and keys, if needed |
||
| 1037 | if ( $switch ) { |
||
| 1038 | |||
| 1039 | // Switch field IDs in email conditional logic |
||
| 1040 | self::switch_email_contition_field_ids( $new_notification['post_content'] ); |
||
| 1041 | |||
| 1042 | // Switch all other field IDs in email |
||
| 1043 | $new_notification['post_content'] = FrmFieldsHelper::switch_field_ids( $new_notification['post_content'] ); |
||
| 1044 | } |
||
| 1045 | $new_notification['post_content'] = FrmAppHelper::prepare_and_encode( $new_notification['post_content'] ); |
||
| 1046 | |||
| 1047 | $exists = get_posts( array( |
||
| 1048 | 'name' => $new_notification['post_name'], |
||
| 1049 | 'post_type' => $new_notification['post_type'], |
||
| 1050 | 'post_status' => $new_notification['post_status'], |
||
| 1051 | 'numberposts' => 1, |
||
| 1052 | ) ); |
||
| 1053 | |||
| 1054 | if ( empty($exists) ) { |
||
| 1055 | FrmAppHelper::save_json_post( $new_notification ); |
||
| 1056 | $imported['imported']['actions']++; |
||
| 1057 | } |
||
| 1058 | unset($new_notification); |
||
| 1059 | } |
||
| 1060 | } |
||
| 1061 | |||
| 1062 | private static function migrate_notifications_to_action( $form_options, $form_id, &$notifications ) { |
||
| 1063 | if ( ! isset( $form_options['notification'] ) && isset( $form_options['email_to'] ) && ! empty( $form_options['email_to'] ) ) { |
||
| 1064 | // add old settings into notification array |
||
| 1065 | $form_options['notification'] = array( 0 => $form_options ); |
||
| 1066 | } else if ( isset( $form_options['notification']['email_to'] ) ) { |
||
| 1067 | // make sure it's in the correct format |
||
| 1068 | $form_options['notification'] = array( 0 => $form_options['notification'] ); |
||
| 1069 | } |
||
| 1070 | |||
| 1071 | if ( isset( $form_options['notification'] ) && is_array($form_options['notification']) ) { |
||
| 1072 | foreach ( $form_options['notification'] as $email_key => $notification ) { |
||
| 1073 | |||
| 1074 | $atts = array( 'email_to' => '', 'reply_to' => '', 'reply_to_name' => '', 'event' => '', 'form_id' => $form_id, 'email_key' => $email_key ); |
||
| 1075 | |||
| 1076 | // Format the email data |
||
| 1077 | self::format_email_data( $atts, $notification ); |
||
| 1078 | |||
| 1079 | if ( isset( $notification['twilio'] ) && $notification['twilio'] ) { |
||
| 1080 | do_action( 'frm_create_twilio_action', $atts, $notification ); |
||
| 1081 | } |
||
| 1082 | |||
| 1083 | // Setup the new notification |
||
| 1084 | $new_notification = array(); |
||
| 1085 | self::setup_new_notification( $new_notification, $notification, $atts ); |
||
| 1086 | |||
| 1087 | $notifications[] = $new_notification; |
||
| 1088 | } |
||
| 1089 | } |
||
| 1090 | } |
||
| 1091 | |||
| 1092 | private static function format_email_data( &$atts, $notification ) { |
||
| 1093 | // Format email_to |
||
| 1094 | self::format_email_to_data( $atts, $notification ); |
||
| 1095 | |||
| 1096 | // Format the reply to email and name |
||
| 1097 | $reply_fields = array( 'reply_to' => '', 'reply_to_name' => '' ); |
||
| 1098 | foreach ( $reply_fields as $f => $val ) { |
||
| 1099 | if ( isset( $notification[ $f ] ) ) { |
||
| 1100 | $atts[ $f ] = $notification[ $f ]; |
||
| 1101 | if ( 'custom' == $notification[ $f ] ) { |
||
| 1102 | $atts[ $f ] = $notification[ 'cust_' . $f ]; |
||
| 1103 | } else if ( is_numeric( $atts[ $f ] ) && ! empty( $atts[ $f ] ) ) { |
||
| 1104 | $atts[ $f ] = '[' . $atts[ $f ] . ']'; |
||
| 1105 | } |
||
| 1106 | } |
||
| 1107 | unset( $f, $val ); |
||
| 1108 | } |
||
| 1109 | |||
| 1110 | // Format event |
||
| 1111 | $atts['event'] = array( 'create' ); |
||
| 1112 | if ( isset( $notification['update_email'] ) && 1 == $notification['update_email'] ) { |
||
| 1113 | $atts['event'][] = 'update'; |
||
| 1114 | } else if ( isset($notification['update_email']) && 2 == $notification['update_email'] ) { |
||
| 1115 | $atts['event'] = array( 'update' ); |
||
| 1116 | } |
||
| 1117 | } |
||
| 1118 | |||
| 1119 | private static function format_email_to_data( &$atts, $notification ) { |
||
| 1120 | if ( isset( $notification['email_to'] ) ) { |
||
| 1121 | $atts['email_to'] = preg_split( '/ (,|;) /', $notification['email_to'] ); |
||
| 1122 | } else { |
||
| 1123 | $atts['email_to'] = array(); |
||
| 1124 | } |
||
| 1125 | |||
| 1126 | if ( isset( $notification['also_email_to'] ) ) { |
||
| 1127 | $email_fields = (array) $notification['also_email_to']; |
||
| 1128 | $atts['email_to'] = array_merge( $email_fields, $atts['email_to'] ); |
||
| 1129 | unset( $email_fields ); |
||
| 1130 | } |
||
| 1131 | |||
| 1132 | foreach ( $atts['email_to'] as $key => $email_field ) { |
||
| 1133 | |||
| 1134 | if ( is_numeric( $email_field ) ) { |
||
| 1135 | $atts['email_to'][ $key ] = '[' . $email_field . ']'; |
||
| 1136 | } |
||
| 1137 | |||
| 1138 | if ( strpos( $email_field, '|') ) { |
||
| 1139 | $email_opt = explode( '|', $email_field ); |
||
| 1140 | if ( isset( $email_opt[0] ) ) { |
||
| 1141 | $atts['email_to'][ $key ] = '[' . $email_opt[0] . ' show=' . $email_opt[1] . ']'; |
||
| 1142 | } |
||
| 1143 | unset( $email_opt ); |
||
| 1144 | } |
||
| 1145 | } |
||
| 1146 | $atts['email_to'] = implode(', ', $atts['email_to']); |
||
| 1147 | } |
||
| 1148 | |||
| 1149 | private static function setup_new_notification( &$new_notification, $notification, $atts ) { |
||
| 1150 | // Set up new notification |
||
| 1151 | $new_notification = array( |
||
| 1152 | 'post_content' => array( |
||
| 1153 | 'email_to' => $atts['email_to'], |
||
| 1154 | 'event' => $atts['event'], |
||
| 1155 | ), |
||
| 1156 | 'post_name' => $atts['form_id'] . '_email_' . $atts['email_key'], |
||
| 1157 | ); |
||
| 1158 | |||
| 1159 | // Add more fields to the new notification |
||
| 1160 | $add_fields = array( 'email_message', 'email_subject', 'plain_text', 'inc_user_info', 'conditions' ); |
||
| 1161 | foreach ( $add_fields as $add_field ) { |
||
| 1162 | if ( isset( $notification[ $add_field ] ) ) { |
||
| 1163 | $new_notification['post_content'][ $add_field ] = $notification[ $add_field ]; |
||
| 1164 | } else if ( in_array( $add_field, array( 'plain_text', 'inc_user_info' ) ) ) { |
||
| 1165 | $new_notification['post_content'][ $add_field ] = 0; |
||
| 1166 | } else { |
||
| 1167 | $new_notification['post_content'][ $add_field ] = ''; |
||
| 1168 | } |
||
| 1169 | unset( $add_field ); |
||
| 1170 | } |
||
| 1171 | |||
| 1172 | // Set reply to |
||
| 1173 | $new_notification['post_content']['reply_to'] = $atts['reply_to']; |
||
| 1174 | |||
| 1175 | // Set from |
||
| 1176 | if ( ! empty( $atts['reply_to'] ) || ! empty( $atts['reply_to_name'] ) ) { |
||
| 1177 | $new_notification['post_content']['from'] = ( empty( $atts['reply_to_name'] ) ? '[sitename]' : $atts['reply_to_name'] ) . ' <' . ( empty( $atts['reply_to'] ) ? '[admin_email]' : $atts['reply_to'] ) . '>'; |
||
| 1178 | } |
||
| 1179 | } |
||
| 1180 | |||
| 1181 | /** |
||
| 1182 | * Switch field IDs in pre-2.0 email conditional logic |
||
| 1183 | * |
||
| 1184 | * @param $post_content array, pass by reference |
||
| 1185 | */ |
||
| 1186 | private static function switch_email_contition_field_ids( &$post_content ) { |
||
| 1187 | // Switch field IDs in conditional logic |
||
| 1188 | if ( isset( $post_content['conditions'] ) && is_array( $post_content['conditions'] ) ) { |
||
| 1189 | foreach ( $post_content['conditions'] as $email_key => $val ) { |
||
| 1190 | if ( is_numeric( $email_key ) ) { |
||
| 1191 | $post_content['conditions'][ $email_key ] = self::switch_action_field_ids( $val, array( 'hide_field' ) ); |
||
| 1192 | } |
||
| 1193 | unset( $email_key, $val); |
||
| 1194 | } |
||
| 1195 | } |
||
| 1196 | } |
||
| 1197 | |||
| 1198 | private static function migrate_autoresponder_to_action( $form_options, $form_id, &$notifications ) { |
||
| 1199 | if ( isset($form_options['auto_responder']) && $form_options['auto_responder'] && isset($form_options['ar_email_message']) && $form_options['ar_email_message'] ) { |
||
| 1200 | // migrate autoresponder |
||
| 1201 | |||
| 1202 | $email_field = isset($form_options['ar_email_to']) ? $form_options['ar_email_to'] : 0; |
||
| 1203 | if ( strpos($email_field, '|') ) { |
||
| 1204 | // data from entries field |
||
| 1205 | $email_field = explode('|', $email_field); |
||
| 1206 | if ( isset($email_field[1]) ) { |
||
| 1207 | $email_field = $email_field[1]; |
||
| 1208 | } |
||
| 1209 | } |
||
| 1210 | if ( is_numeric($email_field) && ! empty($email_field) ) { |
||
| 1211 | $email_field = '[' . $email_field . ']'; |
||
| 1212 | } |
||
| 1213 | |||
| 1214 | $notification = $form_options; |
||
| 1215 | $new_notification2 = array( |
||
| 1216 | 'post_content' => array( |
||
| 1217 | 'email_message' => $notification['ar_email_message'], |
||
| 1218 | 'email_subject' => isset($notification['ar_email_subject']) ? $notification['ar_email_subject'] : '', |
||
| 1219 | 'email_to' => $email_field, |
||
| 1220 | 'plain_text' => isset($notification['ar_plain_text']) ? $notification['ar_plain_text'] : 0, |
||
| 1221 | 'inc_user_info' => 0, |
||
| 1222 | ), |
||
| 1223 | 'post_name' => $form_id . '_email_' . count( $notifications ), |
||
| 1224 | ); |
||
| 1225 | |||
| 1226 | $reply_to = isset($notification['ar_reply_to']) ? $notification['ar_reply_to'] : ''; |
||
| 1227 | $reply_to_name = isset($notification['ar_reply_to_name']) ? $notification['ar_reply_to_name'] : ''; |
||
| 1228 | |||
| 1229 | if ( ! empty( $reply_to ) ) { |
||
| 1230 | $new_notification2['post_content']['reply_to'] = $reply_to; |
||
| 1231 | } |
||
| 1232 | |||
| 1233 | if ( ! empty( $reply_to ) || ! empty( $reply_to_name ) ) { |
||
| 1234 | $new_notification2['post_content']['from'] = ( empty( $reply_to_name ) ? '[sitename]' : $reply_to_name ) . ' <' . ( empty( $reply_to ) ? '[admin_email]' : $reply_to ) . '>'; |
||
| 1235 | } |
||
| 1236 | |||
| 1237 | $notifications[] = $new_notification2; |
||
| 1238 | unset( $new_notification2 ); |
||
| 1239 | } |
||
| 1240 | } |
||
| 1243 |