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

Add mechanism to report comment content modifications via sync #6929

Merged
merged 12 commits into from
Apr 6, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions sync/class.jetpack-sync-module-comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function init_listeners( $callable ) {
add_action( 'untrash_post_comments', $callable );
add_action( 'comment_approved_to_unapproved', $callable );
add_action( 'comment_unapproved_to_approved', $callable );
add_action( 'jetpack_modified_comment_contents', $callable, 10, 2 );
add_filter( 'wp_update_comment_data', array( $this, 'handle_comment_contents_modification' ), 10, 3 );

// even though it's messy, we implement these hooks because
// the edit_comment hook doesn't include the data
Expand All @@ -40,6 +42,35 @@ public function init_listeners( $callable ) {
$this->init_meta_whitelist_handler( 'comment', array( $this, 'filter_meta' ) );
}

public function handle_comment_contents_modification( $new_comment, $old_comment, $new_comment_with_slashes ) {
$content_fields = array(
'comment_author',
'comment_author_email',
'comment_author_url',
'comment_content',
);
$changes = array();
foreach ( $content_fields as $field ) {
if ( $new_comment_with_slashes[$field] != $old_comment[$field] ) {
$changes[$field] = array( $new_comment[$field], $old_comment[$field] );
}
}

if ( ! empty( $changes ) ) {
/**
* Signals to the sync listener that this comment's contents were modified and a sync action
* reflecting the change(s) to the content should be sent
*
* @since 4.8.2
*
* @param int $new_comment['comment_ID'] ID of comment whose content was modified
* @param mixed $changes Array of changed comment fields with before and after values
*/
do_action( 'jetpack_modified_comment_contents', $new_comment['comment_ID'], $changes );
}
return $new_comment;
}

public function init_full_sync_listeners( $callable ) {
add_action( 'jetpack_full_sync_comments', $callable ); // also send comments meta
}
Expand Down
135 changes: 135 additions & 0 deletions tests/php/sync/test_class.jetpack-sync-comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,141 @@ public function test_update_comment() {
$this->assertEquals( "foo bar baz", $remote_comment->comment_content );
}

public function test_unapprove_comment_does_not_trigger_content_modified_event() {
$this->comment->comment_approved = 0;
wp_update_comment( (array) $this->comment );
$this->sender->do_sync();

$event = $this->server_event_storage->get_most_recent_event( 'jetpack_modified_comment_contents' );
$this->assertFalse( (bool) $event );
}

public function test_modify_comment_content() {
global $wp_version;
if ( version_compare( $wp_version, 4.7, '<' ) ) {
$this->markTestSkipped( 'WP 4.7 and up supports required wp_update_comment_data filter' );
return;
}

$comment = clone $this->comment;
$comment->comment_content = "Heeeeeeere's Johnny!";
$expected_variable = array(
'comment_content' => array(
$comment->comment_content,
$this->comment->comment_content,
),
);
$this->modify_comment_helper( $comment, $expected_variable );
}

public function test_modify_comment_author() {
global $wp_version;
if ( version_compare( $wp_version, 4.7, '<' ) ) {
$this->markTestSkipped( 'WP 4.7 and up supports required wp_update_comment_data filter' );
return;
}

$comment = clone $this->comment;
$comment->comment_author = "jollycoder";
$expected_variable = array(
'comment_author' => array(
$comment->comment_author,
$this->comment->comment_author,
),
);
$this->modify_comment_helper( $comment, $expected_variable );
}

public function test_modify_comment_author_url() {
global $wp_version;
if ( version_compare( $wp_version, 4.7, '<' ) ) {
$this->markTestSkipped( 'WP 4.7 and up supports required wp_update_comment_data filter' );
return;
}

$comment = clone $this->comment;
$comment->comment_author_url = "http://jollycoder.xyz";
$expected_variable = array(
'comment_author_url' => array(
$comment->comment_author_url,
$this->comment->comment_author_url,
),
);
$this->modify_comment_helper( $comment, $expected_variable );
}

public function test_modify_comment_author_email() {
global $wp_version;
if ( version_compare( $wp_version, 4.7, '<' ) ) {
$this->markTestSkipped( 'WP 4.7 and up supports required wp_update_comment_data filter' );
return;
}

$comment = clone $this->comment;
$comment->comment_author_email = "i_prefer_to_remain_anonymous_thanks@example.com";;
$expected_variable = array(
'comment_author_email' => array(
$comment->comment_author_email,
$this->comment->comment_author_email,
),
);
$this->modify_comment_helper( $comment, $expected_variable );
}

public function test_modify_comment_multiple_attributes() {
global $wp_version;
if ( version_compare( $wp_version, 4.7, '<' ) ) {
$this->markTestSkipped( 'WP 4.7 and up supports required wp_update_comment_data filter' );
return;
}

$comment = clone $this->comment;
$comment->comment_author_email = "i_prefer_to_remain_anonymous_thanks@example.com";
$comment->comment_author_url = "http://jollycoder.xyz";
$comment->comment_author = "jollycoder";
$expected_variable = array(
'comment_author_email' => array(
$comment->comment_author_email,
$this->comment->comment_author_email,
),
'comment_author_url' => array(
$comment->comment_author_url,
$this->comment->comment_author_url,
),
'comment_author' => array(
$comment->comment_author,
$this->comment->comment_author,
),
);
$this->modify_comment_helper( $comment, $expected_variable );
}

/*
* Updates comment, checks that event args match expected, checks event is not duplicated
*/
private function modify_comment_helper( $comment, $expected_variable ) {
$expected = array(
$comment->comment_ID,
$expected_variable,
);

wp_update_comment( (array) $comment );
$this->sender->do_sync();

$event = $this->server_event_storage->get_most_recent_event( 'jetpack_modified_comment_contents' );
$this->assertTrue( (bool) $event );
$this->assertEquals( $expected, $event->args );

$this->server_event_storage->reset();

//Confirm that 'modified_comment_contents' action is not set after updating comment with same data
wp_update_comment( (array) $comment );
$this->sender->do_sync();

$event = $this->server_event_storage->get_most_recent_event( 'jetpack_modified_comment_contents' );
$this->assertFalse( (bool) $event );
}

public function test_unapprove_comment() {

$this->assertEquals( 1, $this->server_replica_storage->comment_count( 'approve' ) );
Expand Down