types
2 years ago
base-form-action.php
2 years ago
form-actions-manager.php
2 years ago
get-form-data.php
2 years ago
import-form-trait.php
2 years ago
import-form-trait.php
63 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Form_Actions; |
| 5 | |
| 6 | // If this file is called directly, abort. |
| 7 | if ( ! defined( 'WPINC' ) ) { |
| 8 | die; |
| 9 | } |
| 10 | |
| 11 | trait Import_Form_Trait { |
| 12 | |
| 13 | /** |
| 14 | * Import form by data |
| 15 | * |
| 16 | * @param array $form_data [description] |
| 17 | * |
| 18 | * @return [type] [description] |
| 19 | */ |
| 20 | public function import_form( $form_data = array() ) { |
| 21 | |
| 22 | $form_data = wp_parse_args( |
| 23 | $form_data, |
| 24 | array( |
| 25 | 'post_title' => 'New form', |
| 26 | 'post_status' => 'publish', |
| 27 | 'post_content' => '', |
| 28 | ) |
| 29 | ); |
| 30 | |
| 31 | $form_data['post_content'] = wp_slash( $form_data['post_content'] ); |
| 32 | |
| 33 | if ( isset( $form_data['meta_input'] ) && is_array( $form_data['meta_input'] ) ) { |
| 34 | foreach ( $form_data['meta_input'] as &$meta_value ) { |
| 35 | $unserialized = maybe_unserialize( $meta_value ); |
| 36 | |
| 37 | if ( $unserialized !== $meta_value ) { |
| 38 | $meta_value = $unserialized; |
| 39 | continue; |
| 40 | } |
| 41 | $meta_value = wp_slash( $meta_value ); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | $post_id = wp_insert_post( |
| 46 | array_merge( |
| 47 | $form_data, |
| 48 | array( |
| 49 | 'post_type' => $this->post_type(), |
| 50 | ) |
| 51 | ) |
| 52 | ); |
| 53 | |
| 54 | if ( is_wp_error( $post_id ) ) { |
| 55 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 56 | wp_die( $post_id->get_error_message(), 'Error' ); |
| 57 | } |
| 58 | |
| 59 | return $post_id; |
| 60 | } |
| 61 | |
| 62 | } |
| 63 |