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
get-form-data.php
81 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 Get_Form_Data { |
| 12 | |
| 13 | /** |
| 14 | * Get post ID from the current request and validate user acess to this post |
| 15 | * |
| 16 | * @return false|int [type] [description] |
| 17 | */ |
| 18 | public function get_post_id_from_request() { |
| 19 | |
| 20 | /** |
| 21 | * Nonce verifies earlier |
| 22 | * |
| 23 | * @see \Jet_Form_Builder\Form_Actions\Base_Form_Action::do_action |
| 24 | */ |
| 25 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 26 | $post_id = ! empty( $_GET['post'] ) ? absint( $_GET['post'] ) : false; |
| 27 | |
| 28 | if ( ! $post_id ) { |
| 29 | wp_die( 'Form ID not found in the request', 'Error!' ); |
| 30 | } |
| 31 | |
| 32 | if ( ! $this->check_user_access( $post_id ) ) { |
| 33 | wp_die( 'You haven`t access to this form', 'Error!' ); |
| 34 | } |
| 35 | |
| 36 | return $post_id; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Returns from data by ID |
| 41 | * |
| 42 | * @param [type] $form_id [description] |
| 43 | * |
| 44 | * @return [type] [description] |
| 45 | */ |
| 46 | public function get_from_data( $form_id ) { |
| 47 | $post = get_post( $form_id ); |
| 48 | $meta_value = $this->parse_meta_value( |
| 49 | get_post_meta( $form_id ), |
| 50 | array( '_edit_lock' ) |
| 51 | ); |
| 52 | |
| 53 | $for_encoding = $this->parse_form_data( |
| 54 | array( |
| 55 | 'post_title' => $post->post_title, |
| 56 | 'post_content' => $post->post_content, |
| 57 | 'meta_input' => $meta_value, |
| 58 | ) |
| 59 | ); |
| 60 | |
| 61 | return array( $post, $for_encoding ); |
| 62 | } |
| 63 | |
| 64 | public function parse_form_data( $data ) { |
| 65 | return $data; |
| 66 | } |
| 67 | |
| 68 | private function parse_meta_value( $meta, $exclude = array() ) { |
| 69 | $response = array(); |
| 70 | |
| 71 | foreach ( $meta as $key => $value ) { |
| 72 | if ( in_array( $key, $exclude, true ) ) { |
| 73 | continue; |
| 74 | } |
| 75 | $response[ $key ] = $value[0]; |
| 76 | } |
| 77 | |
| 78 | return $response; |
| 79 | } |
| 80 | } |
| 81 |