class-evf-form-handler.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * form handler. |
| 5 | * |
| 6 | * Contains a bunch of helper methods as well. |
| 7 | * |
| 8 | * @package EverestForms |
| 9 | * @author WPEverest |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | class EVF_Form_Handler { |
| 13 | |
| 14 | /** |
| 15 | * Fetches forms |
| 16 | * |
| 17 | * @since 1.0.0 |
| 18 | * @param mixed $id |
| 19 | * @param array $args |
| 20 | * @return array|bool|null|WP_Post |
| 21 | */ |
| 22 | public function get( $id = '', $args = array() ) { |
| 23 | |
| 24 | $args = apply_filters( 'everest_forms_get_form_args', $args ); |
| 25 | |
| 26 | if ( false === $id ) { |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | if ( ! empty( $id ) ) { |
| 31 | // @todo add $id array support. |
| 32 | // If ID is provided, we get a single form. |
| 33 | $forms = get_post( absint( $id ) ); |
| 34 | |
| 35 | if ( ! empty( $args['content_only'] ) && ! empty( $forms ) && 'everest_form' === $forms->post_type ) { |
| 36 | $forms = evf_decode( $forms->post_content ); |
| 37 | } |
| 38 | } else { |
| 39 | // No ID provided, get multiple forms. |
| 40 | $defaults = array( |
| 41 | 'post_type' => 'everest_form', |
| 42 | 'orderby' => 'id', |
| 43 | 'order' => 'DESC', |
| 44 | 'no_found_rows' => true, |
| 45 | 'nopaging' => true, |
| 46 | ); |
| 47 | |
| 48 | $args = wp_parse_args( $args, $defaults ); |
| 49 | |
| 50 | $args['post_type'] = 'everest_form'; |
| 51 | |
| 52 | $forms = get_posts( $args ); |
| 53 | } |
| 54 | |
| 55 | if ( empty( $forms ) ) { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | return $forms; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Delete forms. |
| 64 | * |
| 65 | * @since 1.0.0 |
| 66 | * @param array $ids |
| 67 | * @return boolean |
| 68 | */ |
| 69 | public function delete( $ids = array() ) { |
| 70 | // Check for permissions. |
| 71 | if ( ! current_user_can( apply_filters( 'everest_forms_manage_cap', 'manage_options' ) ) ) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | if ( ! is_array( $ids ) ) { |
| 76 | $ids = array( $ids ); |
| 77 | } |
| 78 | |
| 79 | $ids = array_map( 'absint', $ids ); |
| 80 | |
| 81 | foreach ( $ids as $id ) { |
| 82 | $form = wp_delete_post( $id, true ); |
| 83 | |
| 84 | if ( class_exists( 'EVF_Entry_Handler' ) ) { |
| 85 | // Delete entry if exists. |
| 86 | } |
| 87 | |
| 88 | if ( ! $form ) { |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Create new form. |
| 98 | * |
| 99 | * @since 1.0.0 |
| 100 | * @param string $title |
| 101 | * @param array $args |
| 102 | * @param array $data |
| 103 | * @return mixed |
| 104 | */ |
| 105 | public static function create( $title = '', $template = 'blank', $args = array(), $data = array() ) { |
| 106 | if ( empty( $title ) || ! current_user_can( 'manage_everest_forms' ) ) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | $args = apply_filters( 'everest_forms_create_form_args', $args, $data ); |
| 111 | $form_content = array( |
| 112 | 'form_field_id' => '1', |
| 113 | 'settings' => array( |
| 114 | 'form_title' => sanitize_text_field( $title ), |
| 115 | 'form_desc' => '', |
| 116 | ), |
| 117 | ); |
| 118 | |
| 119 | // Check for template and format the form content. |
| 120 | if ( in_array( $template, array( 'contact' ), true ) ) { |
| 121 | include_once( dirname( __FILE__ ) . "/templates/{$template}.php" ); |
| 122 | $form_content = $form_template[ $template ]; |
| 123 | } |
| 124 | |
| 125 | // Create a form. |
| 126 | $form_id = wp_insert_post( array( |
| 127 | 'post_title' => esc_html( $title ), |
| 128 | 'post_status' => 'publish', |
| 129 | 'post_type' => 'everest_form', |
| 130 | 'post_content' => '{}', |
| 131 | ) ); |
| 132 | |
| 133 | if ( $form_id ) { |
| 134 | $form_data = wp_parse_args( $args, array( |
| 135 | 'ID' => $form_id, |
| 136 | 'post_title' => esc_html( $title ), |
| 137 | 'post_content' => evf_encode( array_merge( array( 'id' => $form_id ), $form_content ) ), |
| 138 | ) ); |
| 139 | |
| 140 | wp_update_post( $form_data ); |
| 141 | } |
| 142 | |
| 143 | do_action( 'everest_forms_create_form', $form_id, $form_data, $data ); |
| 144 | |
| 145 | return $form_id; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Updates form |
| 150 | * |
| 151 | * @since 1.0.0 |
| 152 | * @param string $form_id |
| 153 | * @param array $data |
| 154 | * @param array $args |
| 155 | * @return mixed |
| 156 | * @internal param string $title |
| 157 | */ |
| 158 | public function update( $form_id = '', $data = array(), $args = array() ) { |
| 159 | |
| 160 | // This filter breaks forms if they contain HTML |
| 161 | remove_filter( 'content_save_pre', 'balanceTags', 50 ); |
| 162 | |
| 163 | // Check for permissions |
| 164 | if ( ! current_user_can( apply_filters( 'everest_forms_manage_cap', 'manage_options' ) ) ) { |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | if ( empty( $data ) ) { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | if ( empty( $form_id ) ) { |
| 173 | $form_id = $data['form_id']; |
| 174 | } |
| 175 | |
| 176 | $data = wp_unslash( $data ); |
| 177 | |
| 178 | if ( ! empty( $data['settings']['form_title'] ) ) { |
| 179 | $title = $data['settings']['form_title']; |
| 180 | } else { |
| 181 | $title = get_the_title( $form_id ); |
| 182 | } |
| 183 | |
| 184 | if ( ! empty( $data['settings']['form_desc'] ) ) { |
| 185 | $desc = $data['settings']['form_desc']; |
| 186 | } else { |
| 187 | $desc = ''; |
| 188 | } |
| 189 | |
| 190 | $data['form_field_id'] = ! empty( $data['form_field_id'] ) ? absint( $data['form_field_id'] ) : '0'; |
| 191 | |
| 192 | |
| 193 | // Sanitize - don't allow tags for users who do not have appropriate cap |
| 194 | if ( ! current_user_can( 'unfiltered_html' ) ) { |
| 195 | array_walk_recursive( $data, 'wp_strip_all_tags' ); |
| 196 | } |
| 197 | |
| 198 | $form = array( |
| 199 | 'ID' => $form_id, |
| 200 | 'post_title' => esc_html( $title ), |
| 201 | 'post_excerpt' => $desc, |
| 202 | 'post_content' => evf_encode( $data ), |
| 203 | ); |
| 204 | $form = apply_filters( 'everest_forms_save_form_args', $form, $data, $args ); |
| 205 | $form_id = wp_update_post( $form ); |
| 206 | |
| 207 | do_action( 'everest_forms_save_form', $form_id, $form ); |
| 208 | |
| 209 | return $form_id; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Duplicate forms. |
| 214 | * |
| 215 | * @since 1.0.0 |
| 216 | * @param array $ids |
| 217 | * @return boolean |
| 218 | */ |
| 219 | public function duplicate( $ids = array() ) { |
| 220 | |
| 221 | // Check for permissions. |
| 222 | if ( ! current_user_can( apply_filters( 'everest_forms_manage_cap', 'manage_options' ) ) ) { |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | if ( ! is_array( $ids ) ) { |
| 227 | $ids = array( $ids ); |
| 228 | } |
| 229 | |
| 230 | $ids = array_map( 'absint', $ids ); |
| 231 | |
| 232 | foreach ( $ids as $id ) { |
| 233 | |
| 234 | // Get original entry. |
| 235 | $form = get_post( $id ); |
| 236 | |
| 237 | // Confirm form exists. |
| 238 | if ( ! $form || empty( $form ) ) { |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | // Get the form data. |
| 243 | $new_form_data = evf_decode( $form->post_content ); |
| 244 | |
| 245 | // Remove form ID from title if present. |
| 246 | $new_form_data['settings']['form_title'] = str_replace( '(ID #' . absint( $id ) . ')', '', $new_form_data['settings']['form_title'] ); |
| 247 | |
| 248 | // Create the duplicate form. |
| 249 | $new_form = array( |
| 250 | 'post_author' => $form->post_author, |
| 251 | 'post_content' => evf_encode( $new_form_data ), |
| 252 | 'post_excerpt' => $form->post_excerpt, |
| 253 | 'post_status' => $form->post_status, |
| 254 | 'post_title' => $new_form_data['settings']['form_title'], |
| 255 | 'post_type' => $form->post_type, |
| 256 | ); |
| 257 | $new_form_id = wp_insert_post( $new_form ); |
| 258 | |
| 259 | if ( ! $new_form_id || is_wp_error( $new_form_id ) ) { |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | // Set new form name. |
| 264 | $new_form_data['settings']['form_title'] .= ' (ID #' . absint( $new_form_id ) . ')'; |
| 265 | |
| 266 | // Set new form ID. |
| 267 | $new_form_data['id'] = absint( $new_form_id ); |
| 268 | |
| 269 | // Update new duplicate form. |
| 270 | $new_form_id = $this->update( $new_form_id, $new_form_data ); |
| 271 | |
| 272 | if ( ! $new_form_id || is_wp_error( $new_form_id ) ) { |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | return $new_form_id; |
| 277 | } |
| 278 | |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Get private meta information for a form. |
| 284 | * |
| 285 | * @since 1.1.0 |
| 286 | * |
| 287 | * @param string $form_id |
| 288 | * @param string $field |
| 289 | * |
| 290 | * @return bool |
| 291 | */ |
| 292 | public function get_meta( $form_id, $field = '' ) { |
| 293 | |
| 294 | if ( empty( $form_id ) ) { |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | $data = $this->get( $form_id, array( |
| 299 | 'content_only' => true, |
| 300 | ) ); |
| 301 | |
| 302 | if ( isset( $data['meta'] ) ) { |
| 303 | if ( empty( $field ) ) { |
| 304 | return $data['meta']; |
| 305 | } elseif ( isset( $data['meta'][ $field ] ) ) { |
| 306 | return $data['meta'][ $field ]; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Get the next available field ID and increment by one. |
| 315 | * |
| 316 | * @since 1.0.0 |
| 317 | * @param int $form_id |
| 318 | * @return mixed int or false |
| 319 | */ |
| 320 | public function field_unique_key( $form_id ) { |
| 321 | if ( ! current_user_can( apply_filters( 'everest_forms_manage_cap', 'manage_options' ) ) ) { |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | if ( empty( $form_id ) ) { |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | $form = $this->get( $form_id, array( |
| 331 | 'content_only' => true, |
| 332 | ) ); |
| 333 | |
| 334 | if ( ! empty( $form['form_field_id'] ) ) { |
| 335 | $form_field_id = absint( $form['form_field_id'] ); |
| 336 | $form['form_field_id'] ++; |
| 337 | } else { |
| 338 | $form_field_id = '0'; |
| 339 | $form['form_field_id'] = '1'; |
| 340 | } |
| 341 | |
| 342 | $this->update( $form_id, $form ); |
| 343 | |
| 344 | $field_id = evf_get_random_string() . '-' . $form_field_id; |
| 345 | |
| 346 | return $field_id; |
| 347 | } |
| 348 | |
| 349 | |
| 350 | /** |
| 351 | * Get private meta information for a form field. |
| 352 | * |
| 353 | * @since 1.0.0 |
| 354 | * @param string $form_id |
| 355 | * @param string $field_id |
| 356 | * @return bool |
| 357 | */ |
| 358 | public function get_field( $form_id, $field_id = '' ) { |
| 359 | |
| 360 | if ( empty( $form_id ) ) { |
| 361 | return false; |
| 362 | } |
| 363 | |
| 364 | $data = $this->get( $form_id, array( |
| 365 | 'content_only' => true, |
| 366 | ) ); |
| 367 | |
| 368 | return isset( $data['form_fields'][ $field_id ] ) ? $data['form_fields'][ $field_id ] : false; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Get private meta information for a form field. |
| 373 | * |
| 374 | * @since 1.0.0 |
| 375 | * |
| 376 | * @param string $form_id |
| 377 | * @param string $field |
| 378 | * |
| 379 | * @return bool |
| 380 | */ |
| 381 | public function get_field_meta( $form_id, $field = '' ) { |
| 382 | |
| 383 | $field = $this->get_field( $form_id, $field ); |
| 384 | if ( ! $field ) { |
| 385 | return false; |
| 386 | } |
| 387 | |
| 388 | return isset( $field['meta'] ) ? $field['meta'] : false; |
| 389 | } |
| 390 | } |
| 391 |