| @@ -1,220 +1,222 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -/** | |
| 4 | - * The Form Manager Class | |
| 5 | - * | |
| 6 | - * @since 1.1.0 | |
| 7 | - */ | |
| 8 | -class WeForms_Form_Manager { | |
| 9 | - | |
| 10 | - /** | |
| 11 | - * Get all the forms | |
| 12 | - * | |
| 13 | - * @return array | |
| 14 | - */ | |
| 15 | - public function all() { | |
| 16 | - return $this->get_forms( [ 'posts_per_page' => -1 ] ); | |
| 17 | - } | |
| 18 | - | |
| 19 | - /** | |
| 20 | - * Get forms | |
| 21 | - * | |
| 22 | - * @param array $args | |
| 23 | - * | |
| 24 | - * @return array | |
| 25 | - */ | |
| 26 | - public function get_forms( $args = [] ) { | |
| 27 | - $forms_array = [ | |
| 28 | - 'forms' => [], | |
| 29 | - 'meta' => [ | |
| 30 | - 'total' => 0, | |
| 31 | - 'pages' => 0, | |
| 32 | - ], | |
| 33 | - ]; | |
| 34 | - $defaults = [ | |
| 35 | - 'post_type' => 'wpuf_contact_form', | |
| 36 | - 'post_status' => [ 'publish', 'draft', 'pending' ], | |
| 37 | - ]; | |
| 38 | - | |
| 39 | - $args = wp_parse_args( $args, $defaults ); | |
| 40 | - | |
| 41 | - $query = new WP_Query( $args ); | |
| 42 | - $forms = $query->get_posts(); | |
| 43 | - | |
| 44 | - if ( $forms ) { | |
| 45 | - foreach ( $forms as $form ) { | |
| 46 | - $forms_array['forms'][] = new WeForms_Form( $form ); | |
| 47 | - } | |
| 48 | - } | |
| 49 | - | |
| 50 | - $forms_array['meta']['total'] = (int) $query->found_posts; | |
| 51 | - $forms_array['meta']['pages'] = (int) $query->max_num_pages; | |
| 52 | - | |
| 53 | - return $forms_array; | |
| 54 | - } | |
| 55 | - | |
| 56 | - /** | |
| 57 | - * Get a single form | |
| 58 | - * | |
| 59 | - * @param int|WP_Post $form | |
| 60 | - * | |
| 61 | - * @return \WeForms_Form | |
| 62 | - */ | |
| 63 | - public function get( $form ) { | |
| 64 | - return new WeForms_Form( $form ); | |
| 65 | - } | |
| 66 | - | |
| 67 | - /** | |
| 68 | - * Create a form | |
| 69 | - * | |
| 70 | - * @param string $form_name | |
| 71 | - * @param array $fields | |
| 72 | - * | |
| 73 | - * @return int|WP_Error | |
| 74 | - */ | |
| 75 | - public function create( $form_name, $fields = [] ) { | |
| 76 | - $form_id = wp_insert_post( [ | |
| 77 | - 'post_title' => $form_name, | |
| 78 | - 'post_type' => 'wpuf_contact_form', | |
| 79 | - 'post_status' => 'publish', | |
| 80 | - ] ); | |
| 81 | - | |
| 82 | - if ( is_wp_error( $form_id ) ) { | |
| 83 | - return $form_id; | |
| 84 | - } | |
| 85 | - | |
| 86 | - if ( $fields ) { | |
| 87 | - foreach ( $fields as $order => $field ) { | |
| 88 | - $args = [ | |
| 89 | - 'post_type' => 'wpuf_input', | |
| 90 | - 'post_parent' => $form_id, | |
| 91 | - 'post_status' => 'publish', | |
| 92 | - 'post_content' => maybe_serialize( wp_unslash( $field ) ), | |
| 93 | - 'menu_order' => $order, | |
| 94 | - ]; | |
| 95 | - | |
| 96 | - wp_insert_post( $args ); | |
| 97 | - } | |
| 98 | - } | |
| 99 | - | |
| 100 | - return $form_id; | |
| 101 | - } | |
| 102 | - | |
| 103 | - /** | |
| 104 | - * Delete a form with it's input fields | |
| 105 | - * | |
| 106 | - * @param int $form_id | |
| 107 | - * @param bool $force | |
| 108 | - * | |
| 109 | - * @return void | |
| 110 | - */ | |
| 111 | - public function delete( $form_id, $force = true ) { | |
| 112 | - global $wpdb; | |
| 113 | - | |
| 114 | - wp_delete_post( $form_id, $force ); | |
| 115 | - | |
| 116 | - // delete form inputs as WP doesn't know the relationship | |
| 117 | - $wpdb->delete( $wpdb->posts, | |
| 118 | - [ | |
| 119 | - 'post_parent' => $form_id, | |
| 120 | - 'post_type' => 'wpuf_input', | |
| 121 | - ] | |
| 122 | - ); | |
| 123 | - } | |
| 124 | - | |
| 125 | - /** | |
| 126 | - * Save and existing form | |
| 127 | - * | |
| 128 | - * @since 1.1.0 | |
| 129 | - * | |
| 130 | - * @param array $data Contains form_fields, form_settings, form_settings_key data | |
| 131 | - * | |
| 132 | - * @return bool | |
| 133 | - */ | |
| 134 | - public function save( $data ) { | |
| 135 | - $saved_wpuf_inputs = []; | |
| 136 | - | |
| 137 | - wp_update_post( [ 'ID' => $data['form_id'], 'post_status' => 'publish', 'post_title' => $data['post_title'] ] ); | |
| 138 | - | |
| 139 | - $existing_wpuf_input_ids = get_children( [ | |
| 140 | - 'post_parent' => $data['form_id'], | |
| 141 | - 'post_status' => 'publish', | |
| 142 | - 'post_type' => 'wpuf_input', | |
| 143 | - 'numberposts' => '-1', | |
| 144 | - 'orderby' => 'menu_order', | |
| 145 | - 'order' => 'ASC', | |
| 146 | - 'fields' => 'ids', | |
| 147 | - ] ); | |
| 148 | - | |
| 149 | - $new_wpuf_input_ids = []; | |
| 150 | - | |
| 151 | - if ( !empty( $data['form_fields'] ) ) { | |
| 152 | - foreach ( $data['form_fields'] as $order => $field ) { | |
| 153 | - if ( !empty( $field['is_new'] ) ) { | |
| 154 | - unset( $field['is_new'] ); | |
| 155 | - unset( $field['id'] ); | |
| 156 | - | |
| 157 | - $field_id = 0; | |
| 158 | - } else { | |
| 159 | - $field_id = $field['id']; | |
| 160 | - } | |
| 161 | - | |
| 162 | - $field_id = weforms_insert_form_field( $data['form_id'], $field, $field_id, $order ); | |
| 163 | - | |
| 164 | - $new_wpuf_input_ids[] = $field_id; | |
| 165 | - | |
| 166 | - $field['id'] = $field_id; | |
| 167 | - | |
| 168 | - $saved_wpuf_inputs[] = $field; | |
| 169 | - } | |
| 170 | - } | |
| 171 | - | |
| 172 | - $inputs_to_delete = array_diff( $existing_wpuf_input_ids, $new_wpuf_input_ids ); | |
| 173 | - | |
| 174 | - if ( !empty( $inputs_to_delete ) ) { | |
| 175 | - foreach ( $inputs_to_delete as $delete_id ) { | |
| 176 | - wp_delete_post( $delete_id, true ); | |
| 177 | - } | |
| 178 | - } | |
| 179 | - | |
| 180 | - update_post_meta( $data['form_id'], $data['form_settings_key'], $data['form_settings'] ); | |
| 181 | - update_post_meta( $data['form_id'], 'notifications', $data['notifications'] ); | |
| 182 | - update_post_meta( $data['form_id'], 'integrations', $data['integrations'] ); | |
| 183 | - update_post_meta( $data['form_id'], '_weforms_version', WEFORMS_VERSION ); | |
| 184 | - | |
| 185 | - do_action( 'weforms_after_save_form', $data['form_id'], $data['form_fields'], $data['form_settings'] ); | |
| 186 | - | |
| 187 | - return $saved_wpuf_inputs; | |
| 188 | - } | |
| 189 | - | |
| 190 | - /** | |
| 191 | - * API to duplicate a form | |
| 192 | - * | |
| 193 | - * @param int $_form_id | |
| 194 | - * | |
| 195 | - * @return int New duplicated form id | |
| 196 | - */ | |
| 197 | - public function duplicate( $_form_id ) { | |
| 198 | - $form = $this->get( $_form_id ); | |
| 199 | - | |
| 200 | - if ( empty( $form ) ) { | |
| 201 | - return; | |
| 202 | - } | |
| 203 | - | |
| 204 | - $form_id = $this->create( $form->name, $form->get_fields() ); | |
| 205 | - | |
| 206 | - $data = [ | |
| 207 | - 'form_id' => absint( $form_id ), | |
| 208 | - 'post_title' => sanitize_text_field( $form->name ) . ' (#' . $form_id . ')', | |
| 209 | - 'form_fields' => $this->get( $form_id )->get_fields(), // already imported just proxy | |
| 210 | - 'form_settings' => $form->get_settings(), | |
| 211 | - 'form_settings_key' => 'wpuf_form_settings', | |
| 212 | - 'notifications' => $form->get_notifications(), | |
| 213 | - 'integrations' => $form->get_integrations(), | |
| 214 | - ]; | |
| 215 | - | |
| 216 | - $form_fields = $this->save( $data ); | |
| 217 | - | |
| 218 | - return $this->get( $form_id ); | |
| 219 | - } | |
| 220 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * The Form Manager Class | |
| 5 | + * | |
| 6 | + * @since 1.1.0 | |
| 7 | + */ | |
| 8 | +class WeForms_Form_Manager { | |
| 9 | + | |
| 10 | + /** | |
| 11 | + * Get all the forms | |
| 12 | + * | |
| 13 | + * @return array | |
| 14 | + */ | |
| 15 | + public function all() { | |
| 16 | + return $this->get_forms( array( 'posts_per_page' => -1 ) ); | |
| 17 | + } | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * Get forms | |
| 21 | + * | |
| 22 | + * @param array $args | |
| 23 | + * | |
| 24 | + * @return array | |
| 25 | + */ | |
| 26 | + public function get_forms( $args = array() ) { | |
| 27 | + $forms_array = array( | |
| 28 | + 'forms' => array(), | |
| 29 | + 'meta' => array( | |
| 30 | + 'total' => 0, | |
| 31 | + 'pages' => 0 | |
| 32 | + ) | |
| 33 | + ); | |
| 34 | + $defaults = array( | |
| 35 | + 'post_type' => 'wpuf_contact_form', | |
| 36 | + 'post_status' => array( 'publish', 'draft', 'pending' ) | |
| 37 | + ); | |
| 38 | + | |
| 39 | + $args = wp_parse_args( $args, $defaults ); | |
| 40 | + | |
| 41 | + $query = new WP_Query( $args ); | |
| 42 | + $forms = $query->get_posts(); | |
| 43 | + | |
| 44 | + if ( $forms ) { | |
| 45 | + foreach ($forms as $form) { | |
| 46 | + $forms_array['forms'][] = new WeForms_Form( $form ); | |
| 47 | + } | |
| 48 | + } | |
| 49 | + | |
| 50 | + $forms_array['meta']['total'] = (int) $query->found_posts; | |
| 51 | + $forms_array['meta']['pages'] = (int) $query->max_num_pages; | |
| 52 | + | |
| 53 | + return $forms_array; | |
| 54 | + } | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * Get a single form | |
| 58 | + * | |
| 59 | + * @param integer|WP_Post $form | |
| 60 | + * | |
| 61 | + * @return \WeForms_Form | |
| 62 | + */ | |
| 63 | + public function get( $form ) { | |
| 64 | + return new WeForms_Form( $form ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Create a form | |
| 69 | + * | |
| 70 | + * @param string $form_name | |
| 71 | + * @param array $fields | |
| 72 | + * | |
| 73 | + * @return integer|WP_Error | |
| 74 | + */ | |
| 75 | + public function create( $form_name, $fields = array() ) { | |
| 76 | + $form_id = wp_insert_post( array( | |
| 77 | + 'post_title' => $form_name, | |
| 78 | + 'post_type' => 'wpuf_contact_form', | |
| 79 | + 'post_status' => 'publish' | |
| 80 | + ) ); | |
| 81 | + | |
| 82 | + if ( is_wp_error( $form_id ) ) { | |
| 83 | + return $form_id; | |
| 84 | + } | |
| 85 | + | |
| 86 | + if ( $fields ) { | |
| 87 | + foreach ($fields as $order => $field) { | |
| 88 | + $args = array( | |
| 89 | + 'post_type' => 'wpuf_input', | |
| 90 | + 'post_parent' => $form_id, | |
| 91 | + 'post_status' => 'publish', | |
| 92 | + 'post_content' => maybe_serialize( wp_unslash( $field ) ), | |
| 93 | + 'menu_order' => $order | |
| 94 | + ); | |
| 95 | + | |
| 96 | + wp_insert_post( $args ); | |
| 97 | + } | |
| 98 | + } | |
| 99 | + | |
| 100 | + return $form_id; | |
| 101 | + } | |
| 102 | + | |
| 103 | + /** | |
| 104 | + * Delete a form with it's input fields | |
| 105 | + * | |
| 106 | + * @param integer $form_id | |
| 107 | + * @param boolean $force | |
| 108 | + * | |
| 109 | + * @return void | |
| 110 | + */ | |
| 111 | + public function delete( $form_id, $force = true ) { | |
| 112 | + global $wpdb; | |
| 113 | + | |
| 114 | + wp_delete_post( $form_id, $force ); | |
| 115 | + | |
| 116 | + // delete form inputs as WP doesn't know the relationship | |
| 117 | + $wpdb->delete( $wpdb->posts, | |
| 118 | + array( | |
| 119 | + 'post_parent' => $form_id, | |
| 120 | + 'post_type' => 'wpuf_input' | |
| 121 | + ) | |
| 122 | + ); | |
| 123 | + } | |
| 124 | + | |
| 125 | + /** | |
| 126 | + * Save and existing form | |
| 127 | + * | |
| 128 | + * @since 1.1.0 | |
| 129 | + * | |
| 130 | + * @param array $data Contains form_fields, form_settings, form_settings_key data | |
| 131 | + * | |
| 132 | + * @return boolean | |
| 133 | + */ | |
| 134 | + public function save( $data ) { | |
| 135 | + $saved_wpuf_inputs = array(); | |
| 136 | + | |
| 137 | + wp_update_post( array( 'ID' => $data['form_id'], 'post_status' => 'publish', 'post_title' => $data['post_title'] ) ); | |
| 138 | + | |
| 139 | + $existing_wpuf_input_ids = get_children( array( | |
| 140 | + 'post_parent' => $data['form_id'], | |
| 141 | + 'post_status' => 'publish', | |
| 142 | + 'post_type' => 'wpuf_input', | |
| 143 | + 'numberposts' => '-1', | |
| 144 | + 'orderby' => 'menu_order', | |
| 145 | + 'order' => 'ASC', | |
| 146 | + 'fields' => 'ids' | |
| 147 | + ) ); | |
| 148 | + | |
| 149 | + $new_wpuf_input_ids = array(); | |
| 150 | + | |
| 151 | + if ( ! empty( $data['form_fields'] ) ) { | |
| 152 | + | |
| 153 | + foreach ( $data['form_fields'] as $order => $field ) { | |
| 154 | + if ( ! empty( $field['is_new'] ) ) { | |
| 155 | + unset( $field['is_new'] ); | |
| 156 | + unset( $field['id'] ); | |
| 157 | + | |
| 158 | + $field_id = 0; | |
| 159 | + | |
| 160 | + } else { | |
| 161 | + $field_id = $field['id']; | |
| 162 | + } | |
| 163 | + | |
| 164 | + $field_id = weforms_insert_form_field( $data['form_id'], $field, $field_id, $order ); | |
| 165 | + | |
| 166 | + $new_wpuf_input_ids[] = $field_id; | |
| 167 | + | |
| 168 | + $field['id'] = $field_id; | |
| 169 | + | |
| 170 | + $saved_wpuf_inputs[] = $field; | |
| 171 | + } | |
| 172 | + | |
| 173 | + } | |
| 174 | + | |
| 175 | + $inputs_to_delete = array_diff( $existing_wpuf_input_ids, $new_wpuf_input_ids ); | |
| 176 | + | |
| 177 | + if ( ! empty( $inputs_to_delete ) ) { | |
| 178 | + foreach ( $inputs_to_delete as $delete_id ) { | |
| 179 | + wp_delete_post( $delete_id , true ); | |
| 180 | + } | |
| 181 | + } | |
| 182 | + | |
| 183 | + update_post_meta( $data['form_id'], $data['form_settings_key'], $data['form_settings'] ); | |
| 184 | + update_post_meta( $data['form_id'], 'notifications', $data['notifications'] ); | |
| 185 | + update_post_meta( $data['form_id'], 'integrations', $data['integrations'] ); | |
| 186 | + update_post_meta( $data['form_id'], '_weforms_version', WEFORMS_VERSION ); | |
| 187 | + | |
| 188 | + return $saved_wpuf_inputs; | |
| 189 | + } | |
| 190 | + | |
| 191 | + /** | |
| 192 | + * API to duplicate a form | |
| 193 | + * | |
| 194 | + * @param int $_form_id | |
| 195 | + * | |
| 196 | + * @return int New duplicated form id | |
| 197 | + */ | |
| 198 | + function duplicate( $_form_id ) { | |
| 199 | + | |
| 200 | + $form = $this->get( $_form_id ); | |
| 201 | + | |
| 202 | + if ( empty( $form ) ) { | |
| 203 | + return; | |
| 204 | + } | |
| 205 | + | |
| 206 | + $form_id = $this->create( $form->name, $form->get_fields()); | |
| 207 | + | |
| 208 | + $data = array( | |
| 209 | + 'form_id' => absint( $form_id ), | |
| 210 | + 'post_title' => sanitize_text_field( $form->name ) . ' (#' . $form_id . ')', | |
| 211 | + 'form_fields' => $this->get( $form_id )->get_fields(), // already imported just proxy | |
| 212 | + 'form_settings' => $form->get_settings(), | |
| 213 | + 'form_settings_key' => 'wpuf_form_settings', | |
| 214 | + 'notifications' => $form->get_notifications(), | |
| 215 | + 'integrations' => $form->get_integrations() | |
| 216 | + ); | |
| 217 | + | |
| 218 | + $form_fields = $this->save( $data ); | |
| 219 | + | |
| 220 | + return $this->get( $form_id ); | |
| 221 | + } | |
| 222 | +} | |