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