create-form.php
1 month ago
delete-form.php
5 months ago
duplicate-form.php
5 months ago
form-field-schema.php
2 months ago
form-metadata.php
1 month ago
get-form-stats.php
5 months ago
get-form.php
5 months ago
get-shortcode.php
5 months ago
list-forms.php
2 months ago
update-form.php
1 month ago
get-form.php
192 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Get Form Ability. |
| 4 | * |
| 5 | * @package sureforms |
| 6 | * @since 2.5.2 |
| 7 | */ |
| 8 | |
| 9 | namespace SRFM\Inc\Abilities\Forms; |
| 10 | |
| 11 | use SRFM\Inc\Abilities\Abstract_Ability; |
| 12 | use SRFM\Inc\Helper; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; // Exit if accessed directly. |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Get_Form ability class. |
| 20 | * |
| 21 | * Retrieves detailed information about a specific SureForms form, |
| 22 | * including its field structure parsed from Gutenberg blocks. |
| 23 | * |
| 24 | * @since 2.5.2 |
| 25 | */ |
| 26 | class Get_Form extends Abstract_Ability { |
| 27 | /** |
| 28 | * Constructor. |
| 29 | * |
| 30 | * @since 2.5.2 |
| 31 | */ |
| 32 | public function __construct() { |
| 33 | $this->id = 'sureforms/get-form'; |
| 34 | $this->label = __( 'Get SureForms Form Details', 'sureforms' ); |
| 35 | $this->description = __( 'Retrieve detailed information about a specific SureForms form including its fields, settings, and shortcode.', 'sureforms' ); |
| 36 | $this->capability = 'manage_options'; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * {@inheritDoc} |
| 41 | */ |
| 42 | public function get_annotations() { |
| 43 | return [ |
| 44 | 'readonly' => true, |
| 45 | 'destructive' => false, |
| 46 | 'idempotent' => true, |
| 47 | 'priority' => 1.0, |
| 48 | 'openWorldHint' => false, |
| 49 | ]; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * {@inheritDoc} |
| 54 | */ |
| 55 | public function get_input_schema() { |
| 56 | return [ |
| 57 | 'type' => 'object', |
| 58 | 'additionalProperties' => false, |
| 59 | 'properties' => [ |
| 60 | 'form_id' => [ |
| 61 | 'type' => 'integer', |
| 62 | 'description' => __( 'The ID of the form to retrieve.', 'sureforms' ), |
| 63 | ], |
| 64 | ], |
| 65 | 'required' => [ 'form_id' ], |
| 66 | ]; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * {@inheritDoc} |
| 71 | */ |
| 72 | public function get_output_schema() { |
| 73 | return [ |
| 74 | 'type' => 'object', |
| 75 | 'properties' => [ |
| 76 | 'form_id' => [ 'type' => 'integer' ], |
| 77 | 'title' => [ 'type' => 'string' ], |
| 78 | 'status' => [ 'type' => 'string' ], |
| 79 | 'fields' => [ |
| 80 | 'type' => 'array', |
| 81 | 'items' => [ |
| 82 | 'type' => 'object', |
| 83 | 'properties' => [ |
| 84 | 'type' => [ 'type' => 'string' ], |
| 85 | 'label' => [ 'type' => 'string' ], |
| 86 | 'slug' => [ 'type' => 'string' ], |
| 87 | 'required' => [ 'type' => 'boolean' ], |
| 88 | ], |
| 89 | ], |
| 90 | ], |
| 91 | 'settings' => [ 'type' => 'object' ], |
| 92 | 'shortcode' => [ 'type' => 'string' ], |
| 93 | ], |
| 94 | ]; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Execute the get-form ability. |
| 99 | * |
| 100 | * @param array<string,mixed> $input Validated input data. |
| 101 | * @return array<string,mixed>|\WP_Error |
| 102 | */ |
| 103 | public function execute( $input ) { |
| 104 | $form_id = Helper::get_integer_value( $input['form_id'] ?? 0 ); |
| 105 | $post = get_post( $form_id ); |
| 106 | |
| 107 | if ( ! $post || SRFM_FORMS_POST_TYPE !== $post->post_type ) { |
| 108 | return new \WP_Error( |
| 109 | 'srfm_form_not_found', |
| 110 | __( 'Form not found.', 'sureforms' ), |
| 111 | [ 'status' => 404 ] |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | // Parse blocks to extract field structure. |
| 116 | $blocks = parse_blocks( $post->post_content ); |
| 117 | $fields = $this->extract_fields_from_blocks( $blocks ); |
| 118 | |
| 119 | // Gather form settings from post meta. |
| 120 | $settings = [ |
| 121 | 'submit_button_text' => Helper::get_meta_value( $form_id, '_srfm_submit_button_text' ), |
| 122 | 'use_label_as_placeholder' => Helper::get_meta_value( $form_id, '_srfm_use_label_as_placeholder' ), |
| 123 | 'form_container_width' => Helper::get_meta_value( $form_id, '_srfm_form_container_width' ), |
| 124 | 'instant_form' => Helper::get_meta_value( $form_id, '_srfm_instant_form' ), |
| 125 | 'submit_alignment' => Helper::get_meta_value( $form_id, '_srfm_submit_alignment' ), |
| 126 | 'form_recaptcha' => Helper::get_meta_value( $form_id, '_srfm_form_recaptcha' ), |
| 127 | ]; |
| 128 | |
| 129 | return [ |
| 130 | 'form_id' => $form_id, |
| 131 | 'title' => $post->post_title, |
| 132 | 'status' => $post->post_status, |
| 133 | 'fields' => $fields, |
| 134 | 'settings' => $settings, |
| 135 | 'shortcode' => sprintf( '[sureforms id="%d"]', $form_id ), |
| 136 | ]; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Extract field information from parsed Gutenberg blocks. |
| 141 | * |
| 142 | * @param array<mixed> $blocks Parsed blocks array. |
| 143 | * @since 2.5.2 |
| 144 | * @return array<array<string,mixed>> |
| 145 | */ |
| 146 | private function extract_fields_from_blocks( $blocks ) { |
| 147 | $fields = []; |
| 148 | |
| 149 | foreach ( $blocks as $block ) { |
| 150 | if ( ! is_array( $block ) ) { |
| 151 | continue; |
| 152 | } |
| 153 | |
| 154 | $block_name = Helper::get_string_value( $block['blockName'] ?? '' ); |
| 155 | |
| 156 | if ( empty( $block_name ) ) { |
| 157 | continue; |
| 158 | } |
| 159 | |
| 160 | $inner_blocks = Helper::get_array_value( $block['innerBlocks'] ?? [] ); |
| 161 | |
| 162 | // Only process srfm/* blocks. |
| 163 | if ( 0 !== strpos( $block_name, 'srfm/' ) ) { |
| 164 | // Check inner blocks for nested structures. |
| 165 | if ( ! empty( $inner_blocks ) ) { |
| 166 | $fields = array_merge( $fields, $this->extract_fields_from_blocks( $inner_blocks ) ); |
| 167 | } |
| 168 | continue; |
| 169 | } |
| 170 | |
| 171 | // Skip the form wrapper block itself. |
| 172 | if ( 'srfm/form' === $block_name ) { |
| 173 | if ( ! empty( $inner_blocks ) ) { |
| 174 | $fields = array_merge( $fields, $this->extract_fields_from_blocks( $inner_blocks ) ); |
| 175 | } |
| 176 | continue; |
| 177 | } |
| 178 | |
| 179 | $attrs = Helper::get_array_value( $block['attrs'] ?? [] ); |
| 180 | |
| 181 | $fields[] = [ |
| 182 | 'type' => str_replace( 'srfm/', '', $block_name ), |
| 183 | 'label' => $attrs['label'] ?? '', |
| 184 | 'slug' => $attrs['slug'] ?? '', |
| 185 | 'required' => ! empty( $attrs['required'] ), |
| 186 | ]; |
| 187 | } |
| 188 | |
| 189 | return $fields; |
| 190 | } |
| 191 | } |
| 192 |