PluginProbe ʕ •ᴥ•ʔ
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.12.3
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.12.3
2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8 0.0.9 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.1.2 1.10.0 1.10.1 1.11.0 1.12.0 1.12.1 1.12.2 1.12.3 1.13.0 1.13.1 1.13.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.5.0 1.5.1 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.8.0 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.2 2.6.0
sureforms / inc / abilities / forms / get-form.php
sureforms / inc / abilities / forms Last commit date
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