PluginProbe
Gutenberg / 23.7.2
Gutenberg v23.7.2
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / scripts / block-library / form.php

form.php in Gutenberg 23.7.2, at build/scripts/block-library/form.php

198 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Server-side rendering of the `core/form` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Renders the `core/form` block on server.
10 *
11 * @param array $attributes The block attributes.
12 * @param string $content The saved content.
13 *
14 * @return string The content of the block being rendered.
15 */
16 function gutenberg_render_block_core_form( $attributes, $content ) {
17 wp_enqueue_script_module( '@wordpress/block-library/form/view' );
18
19 $processed_content = new WP_HTML_Tag_Processor( $content );
20 $processed_content->next_tag( 'form' );
21
22 // Get the action for this form.
23 $action = '';
24 $form_action = $attributes['action'] ?? null;
25 if ( is_string( $form_action ) ) {
26 $action = str_replace(
27 array( '{SITE_URL}', '{ADMIN_URL}' ),
28 array( site_url(), admin_url() ),
29 $form_action
30 );
31 }
32 $processed_content->set_attribute( 'action', esc_attr( $action ) );
33
34 // Add the method attribute. If it is not set, default to `post`.
35 $method = empty( $attributes['method'] ) ? 'post' : $attributes['method'];
36 $processed_content->set_attribute( 'method', $method );
37
38 $extra_fields = apply_filters( 'render_block_core_form_extra_fields', '', $attributes );
39
40 return str_replace(
41 '</form>',
42 $extra_fields . '</form>',
43 $processed_content->get_updated_html()
44 );
45 }
46
47 /**
48 * Adds extra fields to the form.
49 *
50 * If the form is a comment form, adds the post ID as a hidden field,
51 * to allow the comment to be associated with the post.
52 *
53 * @param string $extra_fields The extra fields.
54 * @param array $attributes The block attributes.
55 *
56 * @return string The extra fields.
57 */
58 function gutenberg_block_core_form_extra_fields_comment_form( $extra_fields, $attributes ) {
59 $form_action = $attributes['action'] ?? null;
60 if ( ! empty( $form_action ) && is_string( $form_action ) && str_ends_with( $form_action, '/wp-comments-post.php' ) ) {
61 $extra_fields .= '<input type="hidden" name="comment_post_ID" value="' . get_the_ID() . '" id="comment_post_ID">';
62 }
63 return $extra_fields;
64 }
65 add_filter( 'render_block_core_form_extra_fields', 'gutenberg_block_core_form_extra_fields_comment_form', 10, 2 );
66
67 /**
68 * Sends an email if the form is a contact form.
69 */
70 function gutenberg_block_core_form_send_email() {
71 check_ajax_referer( 'wp-block-form' );
72
73 // Get the POST data.
74 $params = wp_unslash( $_POST );
75 // Start building the email content.
76 $content = sprintf(
77 /* translators: %s: The request URI. */
78 __( 'Form submission from %1$s' ) . '</br>',
79 '<a href="' . esc_url( get_site_url( null, $params['_wp_http_referer'] ) ) . '">' . get_bloginfo( 'name' ) . '</a>'
80 );
81
82 $skip_fields = array( 'formAction', '_ajax_nonce', 'action', '_wp_http_referer' );
83 foreach ( $params as $key => $value ) {
84 if ( in_array( $key, $skip_fields, true ) ) {
85 continue;
86 }
87 $content .= sanitize_key( $key ) . ': ' . wp_kses_post( $value ) . '</br>';
88 }
89
90 // Filter the email content.
91 $content = apply_filters( 'render_block_core_form_email_content', $content, $params );
92
93 // Send the email.
94 $result = wp_mail(
95 str_replace( 'mailto:', '', $params['formAction'] ),
96 __( 'Form submission' ),
97 $content
98 );
99
100 if ( ! $result ) {
101 wp_send_json_error( $result );
102 }
103 wp_send_json_success( $result );
104 }
105 add_action( 'wp_ajax_wp_block_form_email_submit', 'gutenberg_block_core_form_send_email' );
106 add_action( 'wp_ajax_nopriv_wp_block_form_email_submit', 'gutenberg_block_core_form_send_email' );
107
108 /**
109 * Send the data export/remove request if the form is a privacy-request form.
110 */
111 function gutenberg_block_core_form_privacy_form() {
112 // Get the POST data.
113 $params = wp_unslash( $_POST );
114
115 // Bail early if not a form submission, or if the nonce is not valid.
116 if ( empty( $params['wp-action'] )
117 || 'wp_privacy_send_request' !== $params['wp-action']
118 || empty( $params['wp-privacy-request'] )
119 || '1' !== $params['wp-privacy-request']
120 || empty( $params['email'] )
121 ) {
122 return;
123 }
124
125 // Get the request types.
126 $request_types = _wp_privacy_action_request_types();
127 $requests_found = array();
128 foreach ( $request_types as $request_type ) {
129 if ( ! empty( $params[ $request_type ] ) ) {
130 $requests_found[] = $request_type;
131 }
132 }
133
134 // Bail early if no requests were found.
135 if ( empty( $requests_found ) ) {
136 return;
137 }
138
139 // Process the requests.
140 $actions_errored = array();
141 $actions_performed = array();
142 foreach ( $requests_found as $action_name ) {
143 // Get the request ID.
144 $request_id = wp_create_user_request( $params['email'], $action_name );
145
146 // Bail early if the request ID is invalid.
147 if ( is_wp_error( $request_id ) ) {
148 $actions_errored[] = $action_name;
149 continue;
150 }
151
152 // Send the request email.
153 wp_send_user_request( $request_id );
154 $actions_performed[] = $action_name;
155 }
156
157 /**
158 * Determine whether the core/form-submission-notification block should be shown.
159 *
160 * @param bool $show Whether to show the core/form-submission-notification block.
161 * @param array $attributes The block attributes.
162 *
163 * @return bool Whether to show the core/form-submission-notification block.
164 */
165 $show_notification = static function ( $show, $attributes ) use ( $actions_performed, $actions_errored ) {
166 switch ( $attributes['type'] ) {
167 case 'success':
168 return ! empty( $actions_performed ) && empty( $actions_errored );
169
170 case 'error':
171 return ! empty( $actions_errored );
172
173 default:
174 return $show;
175 }
176 };
177
178 // Add filter to show the core/form-submission-notification block.
179 add_filter( 'show_form_submission_notification_block', $show_notification, 10, 2 );
180 }
181 add_action( 'wp', 'gutenberg_block_core_form_privacy_form' );
182
183 /**
184 * Registers the `core/form` block on server.
185 */
186 function gutenberg_register_block_core_form() {
187 if ( ! gutenberg_is_experiment_enabled( 'gutenberg-form-blocks' ) ) {
188 return;
189 }
190 register_block_type_from_metadata(
191 __DIR__ . '/form',
192 array(
193 'render_callback' => 'gutenberg_render_block_core_form',
194 )
195 );
196 }
197 add_action( 'init', 'gutenberg_register_block_core_form', 20 );
198