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