Skip to content

Commit a50e78e

Browse files
authored
Forms: Fix forms source warning (#45501)
* Forms: fix source php warning * changelog * Check that the source is larger then 0. * use absint and wp_unslash * fix typo * minor * fix
1 parent bc39bd8 commit a50e78e

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fixed
3+
4+
Forms: fix PHP warning for when a site doesn't have the JWT token yet

projects/packages/forms/src/contact-form/class-contact-form.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,25 @@ public static function get_instance_from_jwt( $jwt_token ) {
256256
} catch ( \Exception $e ) {
257257
return null;
258258
}
259+
$source = $data['source'] ?? array();
260+
if ( empty( $source ) ) {
261+
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- check done by caller process_form_submission()
262+
$source_post_id = ! empty( $_POST['contact-form-id'] ) && is_numeric( $_POST['contact-form-id'] ) ? absint( wp_unslash( $_POST['contact-form-id'] ) ) : 0;
263+
$post = get_post( $source_post_id );
264+
if ( $post !== null && $source_post_id > 0 ) {
265+
// create a fallback source
266+
$source = array(
267+
'source_id' => $post->ID,
268+
'entry_title' => $post->post_title,
269+
'entry_page' => 1,
270+
'source_type' => 'single',
271+
'request_url' => get_permalink( $post ),
272+
);
273+
}
274+
}
259275

260276
$form = new self( $data['attributes'], $data['content'], empty( $data['attributes']['id'] ) );
261-
$form->source = Feedback_Source::from_serialized( $data['source'] );
277+
$form->source = Feedback_Source::from_serialized( $source );
262278
$form->hash = $data['hash'];
263279
$form->has_verified_jwt = true;
264280
return $form;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Significance: patch
2+
Type: bugfix
3+
Comment: This change has not been deployed to jetpack sites yet
4+
5+

0 commit comments

Comments
 (0)