get-form-fields.php
88 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Rest_Api\Endpoints; |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks\Block_Helper; |
| 7 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 8 | use Jet_Form_Builder\Exceptions\Silence_Exception; |
| 9 | use Jet_Form_Builder\Form_Manager; |
| 10 | use Jet_Form_Builder\Request\Exceptions\Plain_Value_Exception; |
| 11 | use JFB_Components\Rest_Api\Rest_Api_Endpoint_Base; |
| 12 | use JFB_Modules\Block_Parsers\Field_Data_Parser; |
| 13 | |
| 14 | // If this file is called directly, abort. |
| 15 | if ( ! defined( 'WPINC' ) ) { |
| 16 | die; |
| 17 | } |
| 18 | |
| 19 | class Get_Form_Fields extends Rest_Api_Endpoint_Base { |
| 20 | |
| 21 | public static function get_rest_base() { |
| 22 | return '(?P<id>[\d]+)/fields'; |
| 23 | } |
| 24 | |
| 25 | public static function get_methods() { |
| 26 | return \WP_REST_Server::READABLE; |
| 27 | } |
| 28 | |
| 29 | public function get_common_args(): array { |
| 30 | return array( |
| 31 | 'id' => array( |
| 32 | 'type' => 'integer', |
| 33 | 'required' => true, |
| 34 | ), |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | public function check_permission(): bool { |
| 39 | return current_user_can( 'edit_jet_fb_forms' ); |
| 40 | } |
| 41 | |
| 42 | public function run_callback( \WP_REST_Request $request ) { |
| 43 | $form_id = $request->get_param( 'id' ); |
| 44 | $form = get_post( $form_id ); |
| 45 | |
| 46 | if ( absint( $form->post_author ) !== get_current_user_id() |
| 47 | && ! current_user_can( 'edit_post', $form->ID ) |
| 48 | ) { |
| 49 | return new \WP_REST_Response( |
| 50 | array( |
| 51 | 'message' => __( 'Not allowed', 'jet-form-builder' ), |
| 52 | ), |
| 53 | 403 |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | jet_fb_context()->set_parsers( |
| 58 | Block_Helper::get_blocks_by_post( $form ) |
| 59 | ); |
| 60 | |
| 61 | foreach ( jet_fb_context()->iterate_parsers() as $name => $parser ) { |
| 62 | if ( $parser->is_secure() ) { |
| 63 | continue; |
| 64 | } |
| 65 | $fields[] = array( |
| 66 | 'value' => $name, |
| 67 | 'label' => $parser->get_label(), |
| 68 | 'type' => $parser->get_type(), |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | if ( empty( $fields ) ) { |
| 73 | return new \WP_REST_Response( |
| 74 | array( |
| 75 | 'message' => __( 'Not founded fields', 'jet-form-builder' ), |
| 76 | ), |
| 77 | 204 |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | return new \WP_REST_Response( |
| 82 | array( |
| 83 | 'fields' => $fields, |
| 84 | ) |
| 85 | ); |
| 86 | } |
| 87 | } |
| 88 |