admin
10 years ago
emails
10 years ago
fields
10 years ago
templates
10 years ago
class-db.php
10 years ago
class-fields.php
10 years ago
class-form.php
10 years ago
class-frontend.php
10 years ago
class-install.php
10 years ago
class-logging.php
10 years ago
class-preview.php
10 years ago
class-process.php
10 years ago
class-smart-tags.php
10 years ago
class-templates.php
10 years ago
class-widget.php
10 years ago
functions.php
10 years ago
class-form.php
430 lines
| 1 | <?php |
| 2 | /** |
| 3 | * All the form goodness and basics. |
| 4 | * |
| 5 | * Contains a bunch of helper methods as well. |
| 6 | * |
| 7 | * @package WPForms |
| 8 | * @author WPForms |
| 9 | * @since 1.0.0 |
| 10 | * @license GPL-2.0+ |
| 11 | * @copyright Copyright (c) 2016, WPForms LLC |
| 12 | */ |
| 13 | class WPForms_Form_Handler { |
| 14 | |
| 15 | /** |
| 16 | * Primary class constructor. |
| 17 | * |
| 18 | * @since 1.0.0 |
| 19 | */ |
| 20 | public function __construct() { |
| 21 | |
| 22 | // Register wpforms custom post type |
| 23 | $this->register_cpt(); |
| 24 | |
| 25 | // Add wpforms to new-content admin bar menu |
| 26 | add_action( 'admin_bar_menu', array( $this, 'admin_bar' ), 99 ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Registers the custom post type to be used for forms. |
| 31 | * |
| 32 | * @since 1.0.0 |
| 33 | */ |
| 34 | public function register_cpt() { |
| 35 | |
| 36 | // Custom post type arguments, which can be filtered if needed |
| 37 | $args = apply_filters( 'wpforms_post_type_args', |
| 38 | array( |
| 39 | 'labels' => array(), |
| 40 | 'public' => false, |
| 41 | 'exclude_from_search' => true, |
| 42 | 'show_ui' => false, |
| 43 | 'show_in_admin_bar' => false, |
| 44 | 'rewrite' => false, |
| 45 | 'query_var' => false, |
| 46 | 'supports' => array( 'title' ), |
| 47 | ) |
| 48 | ); |
| 49 | |
| 50 | // Register the post type |
| 51 | register_post_type( 'wpforms', $args ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Adds "WPForm" item to new-content admin bar menu item. |
| 56 | * |
| 57 | * @since 1.1.7.2 |
| 58 | * @param object $wp_admin_bar |
| 59 | */ |
| 60 | public function admin_bar( $wp_admin_bar ) { |
| 61 | |
| 62 | if ( !is_admin_bar_showing() || !current_user_can( apply_filters( 'wpforms_manage_cap', 'manage_options' ) ) ) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | $args = array( |
| 67 | 'id' => 'wpform', |
| 68 | 'title' => 'WPForm', |
| 69 | 'href' => admin_url( 'admin.php?page=wpforms-builder' ), |
| 70 | 'parent' => 'new-content', |
| 71 | ); |
| 72 | $wp_admin_bar->add_node( $args ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Fetches forms |
| 77 | * |
| 78 | * @since 1.0.0 |
| 79 | * @param int $id |
| 80 | * @param array $args |
| 81 | */ |
| 82 | public function get( $id = '', $args = array() ) { |
| 83 | |
| 84 | $args = apply_filters( 'wpforms_get_form_args', $args ); |
| 85 | |
| 86 | if ( false === $id ) { |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | if ( !empty( $id ) ) { |
| 91 | // @todo add $id array support |
| 92 | // If ID is provided, we get a single form |
| 93 | $forms = get_post( absint( $id ) ); |
| 94 | |
| 95 | if ( !empty( $args['content_only'] ) && !empty( $forms ) ) { |
| 96 | $forms = wpforms_decode( $forms->post_content ); |
| 97 | } |
| 98 | |
| 99 | } else { |
| 100 | // No ID provided, get multiple forms |
| 101 | $forms = get_posts( |
| 102 | wp_parse_args( |
| 103 | $args, |
| 104 | array( |
| 105 | 'post_type' => 'wpforms', |
| 106 | 'orderby' => 'id', |
| 107 | 'order' => 'ASC', // old->new |
| 108 | 'no_found_rows' => true, |
| 109 | 'nopaging' => true, |
| 110 | // 'cache_results' => false, |
| 111 | )) |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | if ( empty( $forms ) ) { |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | return $forms; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Deletes forms |
| 124 | * |
| 125 | * @since 1.0.0 |
| 126 | * @param array $ids |
| 127 | * @return boolean |
| 128 | */ |
| 129 | public function delete( $ids = array() ) { |
| 130 | |
| 131 | // Check for permissions |
| 132 | if ( !current_user_can( apply_filters( 'wpforms_manage_cap', 'manage_options' ) ) ) |
| 133 | return false; |
| 134 | |
| 135 | if ( ! is_array( $ids ) ) { |
| 136 | $ids = array( $ids ); |
| 137 | } |
| 138 | |
| 139 | $ids = array_map( 'absint', $ids ); |
| 140 | |
| 141 | foreach ( $ids as $id ) { |
| 142 | |
| 143 | $form = wp_delete_post( $id, true ); |
| 144 | |
| 145 | if ( class_exists( 'WPForms_Entry_Handler' ) ) { |
| 146 | $entries = wpforms()->entry->delete_by( 'form_id', $id ); |
| 147 | } |
| 148 | |
| 149 | if ( ! $form || ! $entries ) { |
| 150 | return false; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Add new form. |
| 159 | * |
| 160 | * @since 1.0.0 |
| 161 | * @param string $title |
| 162 | * @param array $args |
| 163 | * @return mixed |
| 164 | */ |
| 165 | public function add( $title = '', $args = array(), $data = array() ) { |
| 166 | |
| 167 | // Check for permissions |
| 168 | if ( !current_user_can( apply_filters( 'wpforms_manage_cap', 'manage_options' ) ) ) |
| 169 | return false; |
| 170 | |
| 171 | // Must have a title |
| 172 | if ( empty( $title ) ) |
| 173 | return false; |
| 174 | |
| 175 | $args = apply_filters( 'wpforms_create_form_args' , $args, $data ); |
| 176 | |
| 177 | $form_content = array( |
| 178 | 'field_id' => '0', |
| 179 | 'settings' => array( |
| 180 | 'form_title' => sanitize_text_field( $title ), |
| 181 | 'form_desc' => '', |
| 182 | ), |
| 183 | ); |
| 184 | |
| 185 | // Merge args and create the form |
| 186 | $form = wp_parse_args( |
| 187 | $args, |
| 188 | array( |
| 189 | 'post_title' => esc_html( $title ), |
| 190 | 'post_status' => 'publish', |
| 191 | 'post_type' => 'wpforms', |
| 192 | 'post_content' => wp_slash( json_encode( $form_content ) ), |
| 193 | ) |
| 194 | ); |
| 195 | $form_id = wp_insert_post( $form ); |
| 196 | |
| 197 | do_action( 'wpforms_create_form', $form_id, $form, $data ); |
| 198 | |
| 199 | return $form_id; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Updates form |
| 204 | * |
| 205 | * @since 1.0.0 |
| 206 | * @param string $title |
| 207 | * @param array $args |
| 208 | * @return mixed |
| 209 | */ |
| 210 | public function update( $form_id = '', $data = array(), $args = array() ) { |
| 211 | |
| 212 | // Check for permissions |
| 213 | if ( !current_user_can( apply_filters( 'wpforms_manage_cap', 'manage_options' ) ) ) |
| 214 | return false; |
| 215 | |
| 216 | if ( empty( $data ) ) |
| 217 | return false; |
| 218 | |
| 219 | if ( empty( $form_id ) ) { |
| 220 | $form_id = $data['id']; |
| 221 | } |
| 222 | |
| 223 | if ( !empty( $data['settings']['form_title'] ) ) { |
| 224 | $title = $data['settings']['form_title']; |
| 225 | } else { |
| 226 | $title = get_the_title( $form_id ); |
| 227 | } |
| 228 | |
| 229 | if ( !empty( $data['settings']['form_desc'] ) ) { |
| 230 | $desc = $data['settings']['form_desc']; |
| 231 | } else { |
| 232 | $desc = ''; |
| 233 | } |
| 234 | |
| 235 | $data['field_id'] = !empty( $data['field_id'] ) ? absint( $data['field_id'] ) : '0'; |
| 236 | |
| 237 | // @todo - This needs testing |
| 238 | if ( !empty( $args['merge'] ) ) { |
| 239 | $previous = $this->get( $form_id, array( 'content_only' => true ) ); |
| 240 | $data = wp_parse_args( $data, $previous ); |
| 241 | } |
| 242 | |
| 243 | // Perserve form meta. |
| 244 | $meta = $this->get_meta( $form_id ); |
| 245 | if ( $meta ) { |
| 246 | $data['meta'] = $meta; |
| 247 | } |
| 248 | |
| 249 | // Preserve field meta. |
| 250 | if ( isset( $data['fields'] ) ) { |
| 251 | foreach ( $data['fields'] as $i => $field_data ) { |
| 252 | if ( isset( $field_data['id'] ) ) { |
| 253 | $field_meta = $this->get_field_meta( $form_id, $field_data['id'] ); |
| 254 | if ( $field_meta ) { |
| 255 | $data['fields'][ $i ]['meta'] = $field_meta; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | // Sanitize - don't allow tags for users who do not have appropriate cap |
| 262 | if ( !current_user_can( 'unfiltered_html' ) ) { |
| 263 | array_walk_recursive( $data, 'wp_strip_all_tags' ); |
| 264 | } |
| 265 | |
| 266 | $form = array( |
| 267 | 'ID' => $form_id, |
| 268 | 'post_title' => esc_html( $title ), |
| 269 | 'post_excerpt' => $desc, |
| 270 | 'post_content' => wp_slash( json_encode( $data ) ), |
| 271 | ); |
| 272 | $form = apply_filters( 'wpforms_save_form_args', $form, $data, $args ); |
| 273 | $form_id = wp_update_post( $form ); |
| 274 | |
| 275 | do_action( 'wpforms_save_form', $form_id, $form ); |
| 276 | |
| 277 | return $form_id; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Duplicate forms. |
| 282 | * |
| 283 | * @since 1.1.4 |
| 284 | * @param array $ids |
| 285 | * @return boolean |
| 286 | */ |
| 287 | public function duplicate( $ids = array() ) { |
| 288 | |
| 289 | // Check for permissions |
| 290 | if ( !current_user_can( apply_filters( 'wpforms_manage_cap', 'manage_options' ) ) ) |
| 291 | return false; |
| 292 | |
| 293 | if ( ! is_array( $ids ) ) { |
| 294 | $ids = array( $ids ); |
| 295 | } |
| 296 | |
| 297 | $ids = array_map( 'absint', $ids ); |
| 298 | |
| 299 | foreach ( $ids as $id ) { |
| 300 | |
| 301 | // Get original entry |
| 302 | $form = get_post( $id ); |
| 303 | |
| 304 | // Confirm form exists |
| 305 | if ( ! $form || empty( $form ) ) { |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | // Get the form data |
| 310 | $new_form_data = wpforms_decode( $form->post_content ); |
| 311 | |
| 312 | // Remove form ID from title if present |
| 313 | $new_form_data['settings']['form_title'] = str_replace('(ID #' . absint( $id ) . ')', '', $new_form_data['settings']['form_title'] ); |
| 314 | |
| 315 | // Create the duplicate form |
| 316 | $new_form = array( |
| 317 | 'post_author' => $form->post_author, |
| 318 | 'post_content' => wp_slash( json_encode( $new_form_data ) ), |
| 319 | 'post_excerpt' => $form->post_excerpt, |
| 320 | 'post_status' => $form->post_status, |
| 321 | 'post_title' => $new_form_data['settings']['form_title'], |
| 322 | 'post_type' => $form->post_type, |
| 323 | ); |
| 324 | $new_form_id = wp_insert_post( $new_form ); |
| 325 | |
| 326 | if ( ! $new_form_id || is_wp_error( $new_form_id ) ) { |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | // Set new form title |
| 331 | $new_form_data['settings']['form_title'] .= ' (ID #' . absint( $new_form_id ) . ')'; |
| 332 | |
| 333 | // Set new form ID |
| 334 | $new_form_data['id'] = absint( $new_form_id ); |
| 335 | |
| 336 | // Update new duplicate form |
| 337 | $new_form_id = $this->update( $new_form_id, $new_form_data ); |
| 338 | |
| 339 | if ( ! $new_form_id || is_wp_error( $new_form_id ) ) { |
| 340 | return false; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Get the next available field ID and increment by one. |
| 349 | * |
| 350 | * @since 1.0.0 |
| 351 | * @param int $form_id |
| 352 | * @return mixed int or false |
| 353 | */ |
| 354 | public function next_field_id( $form_id ) { |
| 355 | |
| 356 | // Check for permissions |
| 357 | if ( !current_user_can( apply_filters( 'wpforms_manage_cap', 'manage_options' ) ) ) |
| 358 | return false; |
| 359 | |
| 360 | if ( empty( $form_id ) ) |
| 361 | return false; |
| 362 | |
| 363 | $form = $this->get( $form_id, array( 'content_only' => true ) ); |
| 364 | |
| 365 | if ( !empty( $form['field_id'] ) ) { |
| 366 | $field_id = absint( $form['field_id'] ); |
| 367 | $form['field_id']++; |
| 368 | } else { |
| 369 | $field_id = '0'; |
| 370 | $form['field_id'] = '1'; |
| 371 | } |
| 372 | |
| 373 | $this->update( $form_id, $form ); |
| 374 | |
| 375 | return $field_id; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Get private meta information for a form. |
| 380 | * |
| 381 | * @since 1.0.0 |
| 382 | */ |
| 383 | public function get_meta( $form_id, $field = '' ) { |
| 384 | |
| 385 | if ( empty( $form_id ) ) |
| 386 | return false; |
| 387 | |
| 388 | $data = $this->get( $form_id, array( 'content_only' => true ) ); |
| 389 | |
| 390 | if ( isset( $data['meta'] ) ) { |
| 391 | if ( empty( $field ) ) { |
| 392 | return $data['meta']; |
| 393 | } elseif ( isset( $data['meta'][$field] ) ) { |
| 394 | return $data['meta'][$field]; |
| 395 | } |
| 396 | } |
| 397 | return false; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Get private meta information for a form field. |
| 402 | * |
| 403 | * @since 1.0.0 |
| 404 | */ |
| 405 | public function get_field( $form_id, $field_id = '' ) { |
| 406 | |
| 407 | if ( empty( $form_id ) ) { |
| 408 | return false; |
| 409 | } |
| 410 | |
| 411 | $data = $this->get( $form_id, array( 'content_only' => true ) ); |
| 412 | |
| 413 | return isset( $data['fields'][ $field_id ] ) ? $data['fields'][ $field_id ] : false; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Get private meta information for a form field. |
| 418 | * |
| 419 | * @since 1.0.0 |
| 420 | */ |
| 421 | public function get_field_meta( $form_id, $field = '' ) { |
| 422 | |
| 423 | $field = $this->get_field( $form_id, $field ); |
| 424 | if ( ! $field ) { |
| 425 | return false; |
| 426 | } |
| 427 | |
| 428 | return isset( $field['meta'] ) ? $field['meta'] : false; |
| 429 | } |
| 430 | } |