PluginProbe
Gutenberg / 18.9.0
Gutenberg v18.9.0
24.0.0 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 All 403 releases
gutenberg / build / block-library / blocks / form.php

form.php in Gutenberg 18.9.0, at build/block-library/blocks/form.php

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