Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jetpack comments via chrome when logged in #38554

Merged
merged 5 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: bugfix

Jetpack Comments: fix replying to comments in chrome when logged in to both wordpress.com and the jetpack site
56 changes: 54 additions & 2 deletions projects/plugins/jetpack/modules/comments/comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,12 +620,14 @@ public function pre_comment_on_post() {
// Bail if missing the Jetpack token.
if ( ! isset( $post_array['sig'] ) || ! isset( $post_array['token_key'] ) ) {
unset( $_POST['hc_post_as'] );

return;
}

if ( empty( $post_array['jetpack_comments_nonce'] ) || ! wp_verify_nonce( $post_array['jetpack_comments_nonce'], "jetpack_comments_nonce-{$post_array['comment_post_ID']}" ) ) {
wp_die( esc_html__( 'Nonce verification failed.', 'jetpack' ), 400 );
if ( ! isset( $_GET['only_once'] ) ) {
self::retry_submit_comment_form_locally();
}
wp_die( esc_html__( 'Nonce verification failed.', 'jetpack' ), 400 );
}

if ( str_contains( $post_array['hc_avatar'], '.gravatar.com' ) ) {
Expand Down Expand Up @@ -655,6 +657,56 @@ public function pre_comment_on_post() {
}
}

/**
* Handle Jetpack Comments POST requests: process the comment form, then client-side POST the results to the self-hosted blog
*
* This function exists because when we submit the form via the jetpack.wordpress.com iframe
* in Chrome the request comes in to Jetpack but for some reason the request doesn't have access to cookies yet.
* By submitting the form again locally with the same data the process works as expected.
*
* @return never
*/
public function retry_submit_comment_form_locally() {
// We are not doing any validation here since all the validation will be done again by pre_comment_on_post().
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$comment_data = stripslashes_deep( $_POST );
?>
<!DOCTYPE html>
<html>
<head>
<link rel="preload" as="image" href="https://jetpack.wordpress.com/wp-admin/images/spinner.gif"> <!-- Preload the spinner image -->
<meta charset="utf-8">
<title><?php echo esc_html__( 'Submitting Comment', 'jetpack' ); ?></title>
<style type="text/css">
body {
display: table;
width: 100%;
height: 60%;
position: absolute;
top: 0;
left: 0;
overflow: hidden;
color: #333;
}
</style>
</head>
<body>
<img src="https://jetpack.wordpress.com/wp-admin/images/spinner.gif" >
<form id="jetpack-remote-comment-post-form" action="<?php echo esc_url( get_site_url() ); ?>/wp-comments-post.php?for=jetpack&only_once=true" method="POST">
<?php foreach ( $comment_data as $key => $val ) : ?>
<input type="hidden" name="<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( $val ); ?>" />
<?php endforeach; ?>
</form>

<script type="text/javascript">
document.getElementById("jetpack-remote-comment-post-form").submit();
</script>
</body>
</html>
<?php
exit;
}

/** Capabilities **********************************************************/

/**
Expand Down
Loading