From 01765171f0a852adc6d59db9cc6925a52bf903d4 Mon Sep 17 00:00:00 2001 From: Debian Date: Mon, 23 Jun 2025 20:53:01 +0000 Subject: [PATCH 1/6] check if author is on the post, otherwise use generic values --- disqus/rest-api/class-disqus-rest-api.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/disqus/rest-api/class-disqus-rest-api.php b/disqus/rest-api/class-disqus-rest-api.php index a45a454..656f6ad 100644 --- a/disqus/rest-api/class-disqus-rest-api.php +++ b/disqus/rest-api/class-disqus-rest-api.php @@ -732,7 +732,7 @@ private function validate_disqus_post_data( $post ) { */ private function comment_data_from_post( $post ) { $thread = array_key_exists( 'threadData', $post ) ? $post['threadData'] : $post['thread']; - $author = $post['author']; + $author = isset( $post['author'] ) ? $post['author'] : null; $wp_post_id = null; @@ -782,12 +782,16 @@ private function comment_data_from_post( $post ) { // Email is a special permission for Disqus API applications and won't be present // if the user has not set the permission for their API application. $author_email = null; - if ( isset( $author['email'] ) ) { - $author_email = $author['email']; - } elseif ( $author['isAnonymous'] ) { - $author_email = 'anonymized-' . md5( $author['name'] ) . '@disqus.com'; - } else { - $author_email = 'user-' . $author['id'] . '@disqus.com'; + if ( $author ) { + if ( isset( $author['email'] ) ) { + $author_email = $author['email']; + } elseif ( isset( $author['isAnonymous'] ) && $author['isAnonymous'] ) { + $author_email = 'anonymized-' . md5( $author['name'] ) . '@disqus.com'; + } elseif ( isset( $author['id'] ) ) { + $author_email = 'user-' . $author['id'] . '@disqus.com'; + } + } else { + $author_email = 'anonymoususer@disqus.com'; } // Translate the comment approval state. @@ -804,10 +808,10 @@ private function comment_data_from_post( $post ) { return array( 'comment_post_ID' => (int) $wp_post_id, - 'comment_author' => $author['name'], + 'comment_author' => $author && isset( $author['name'] ) ? $author['name'] : 'Anonymous', 'comment_author_email' => $author_email, 'comment_author_IP' => $post['ipAddress'], - 'comment_author_url' => isset( $author['url'] ) ? $author['url'] : '', + 'comment_author_url' => $author && isset( $author['url'] ) ? $author['url'] : '', 'comment_content' => $post['raw_message'], 'comment_date' => $post['createdAt'], 'comment_date_gmt' => $post['createdAt'], From 19b530880654cee8a513ee794780a63f39594984 Mon Sep 17 00:00:00 2001 From: Debian Date: Mon, 23 Jun 2025 21:02:24 +0000 Subject: [PATCH 2/6] update author_email fallback --- disqus/rest-api/class-disqus-rest-api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/disqus/rest-api/class-disqus-rest-api.php b/disqus/rest-api/class-disqus-rest-api.php index 656f6ad..8d51292 100644 --- a/disqus/rest-api/class-disqus-rest-api.php +++ b/disqus/rest-api/class-disqus-rest-api.php @@ -791,7 +791,7 @@ private function comment_data_from_post( $post ) { $author_email = 'user-' . $author['id'] . '@disqus.com'; } } else { - $author_email = 'anonymoususer@disqus.com'; + $author_email = md5( 'no_author_data' ) . '@disqus.com'; } // Translate the comment approval state. From 6ada5972aa7e136117942efdeedd4acbd84368ff Mon Sep 17 00:00:00 2001 From: Debian Date: Mon, 23 Jun 2025 21:08:28 +0000 Subject: [PATCH 3/6] formatting --- disqus/rest-api/class-disqus-rest-api.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/disqus/rest-api/class-disqus-rest-api.php b/disqus/rest-api/class-disqus-rest-api.php index 8d51292..03afac7 100644 --- a/disqus/rest-api/class-disqus-rest-api.php +++ b/disqus/rest-api/class-disqus-rest-api.php @@ -783,14 +783,14 @@ private function comment_data_from_post( $post ) { // if the user has not set the permission for their API application. $author_email = null; if ( $author ) { - if ( isset( $author['email'] ) ) { - $author_email = $author['email']; - } elseif ( isset( $author['isAnonymous'] ) && $author['isAnonymous'] ) { - $author_email = 'anonymized-' . md5( $author['name'] ) . '@disqus.com'; - } elseif ( isset( $author['id'] ) ) { - $author_email = 'user-' . $author['id'] . '@disqus.com'; - } - } else { + if ( isset( $author['email'] ) ) { + $author_email = $author['email']; + } elseif ( isset( $author['isAnonymous'] ) && $author['isAnonymous'] ) { + $author_email = 'anonymized-' . md5( $author['name'] ) . '@disqus.com'; + } elseif ( isset( $author['id'] ) ) { + $author_email = 'user-' . $author['id'] . '@disqus.com'; + } + } else { $author_email = md5( 'no_author_data' ) . '@disqus.com'; } From 47c48cbedf0bc9067d85a342b397ce703616678c Mon Sep 17 00:00:00 2001 From: Debian Date: Mon, 23 Jun 2025 21:27:33 +0000 Subject: [PATCH 4/6] add tests --- .../test-rest-api-comment-data-from-post.php | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 tests/test-rest-api-comment-data-from-post.php diff --git a/tests/test-rest-api-comment-data-from-post.php b/tests/test-rest-api-comment-data-from-post.php new file mode 100644 index 0000000..282a135 --- /dev/null +++ b/tests/test-rest-api-comment-data-from-post.php @@ -0,0 +1,94 @@ +disqus_rest_api = new Disqus_Rest_Api( null, null ); + } + + private function call_comment_data_from_post( $post ) { + $class = new ReflectionClass( 'Disqus_Rest_Api' ); + $method = $class->getMethod( 'comment_data_from_post' ); + $method->setAccessible( true ); + return $method->invokeArgs( $this->disqus_rest_api, array( $post ) ); + } + + public function test_email_from_author_email() { + $post = [ + 'thread' => [ 'id' => 1, 'identifiers' => [ '1' ] ], + 'author' => [ 'email' => 'foo@example.com', 'name' => 'Foo' ], + 'parent' => null, + 'ipAddress' => '127.0.0.1', + 'raw_message' => 'Test', + 'createdAt' => '2020-01-01 00:00:00', + 'isApproved' => true, + 'isDeleted' => false, + 'isSpam' => false, + 'id' => 123, + 'forum' => get_option( 'disqus_forum_url' ), + ]; + $data = $this->call_comment_data_from_post( $post ); + $this->assertEquals( 'foo@example.com', $data['comment_author_email'] ); + } + + public function test_email_from_anonymous_author() { + $post = [ + 'thread' => [ 'id' => 1, 'identifiers' => [ '1' ] ], + 'author' => [ 'isAnonymous' => true, 'name' => 'Anon' ], + 'parent' => null, + 'ipAddress' => '127.0.0.1', + 'raw_message' => 'Test', + 'createdAt' => '2020-01-01 00:00:00', + 'isApproved' => true, + 'isDeleted' => false, + 'isSpam' => false, + 'id' => 123, + 'forum' => get_option( 'disqus_forum_url' ), + ]; + $data = $this->call_comment_data_from_post( $post ); + $this->assertStringStartsWith( 'anonymized-', $data['comment_author_email'] ); + $this->assertStringEndsWith( '@disqus.com', $data['comment_author_email'] ); + } + + public function test_email_from_author_id() { + $post = [ + 'thread' => [ 'id' => 1, 'identifiers' => [ '1' ] ], + 'author' => [ 'id' => 42 ], + 'parent' => null, + 'ipAddress' => '127.0.0.1', + 'raw_message' => 'Test', + 'createdAt' => '2020-01-01 00:00:00', + 'isApproved' => true, + 'isDeleted' => false, + 'isSpam' => false, + 'id' => 123, + 'forum' => get_option( 'disqus_forum_url' ), + ]; + $data = $this->call_comment_data_from_post( $post ); + $this->assertEquals( 'user-42@disqus.com', $data['comment_author_email'] ); + } + + public function test_email_when_no_author() { + $post = [ + 'thread' => [ 'id' => 1, 'identifiers' => [ '1' ] ], + 'author' => null, + 'parent' => null, + 'ipAddress' => '127.0.0.1', + 'raw_message' => 'Test', + 'createdAt' => '2020-01-01 00:00:00', + 'isApproved' => true, + 'isDeleted' => false, + 'isSpam' => false, + 'id' => 123, + 'forum' => get_option( 'disqus_forum_url' ), + ]; + $data = $this->call_comment_data_from_post( $post ); + $this->assertStringEndsWith( '@disqus.com', $data['comment_author_email'] ); + $this->assertNotEmpty( $data['comment_author_email'] ); + } +} From 823e840f24ef03d31f225e4764735d119ee3076c Mon Sep 17 00:00:00 2001 From: Debian Date: Tue, 24 Jun 2025 18:08:43 +0000 Subject: [PATCH 5/6] address feedback --- disqus/rest-api/class-disqus-rest-api.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/disqus/rest-api/class-disqus-rest-api.php b/disqus/rest-api/class-disqus-rest-api.php index 03afac7..248a6be 100644 --- a/disqus/rest-api/class-disqus-rest-api.php +++ b/disqus/rest-api/class-disqus-rest-api.php @@ -790,8 +790,6 @@ private function comment_data_from_post( $post ) { } elseif ( isset( $author['id'] ) ) { $author_email = 'user-' . $author['id'] . '@disqus.com'; } - } else { - $author_email = md5( 'no_author_data' ) . '@disqus.com'; } // Translate the comment approval state. From 3f16873f42aaa560b94fcc7f1440b5ed3f5b95f5 Mon Sep 17 00:00:00 2001 From: Debian Date: Tue, 24 Jun 2025 18:15:58 +0000 Subject: [PATCH 6/6] update test --- tests/test-rest-api-comment-data-from-post.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test-rest-api-comment-data-from-post.php b/tests/test-rest-api-comment-data-from-post.php index 282a135..0cf6103 100644 --- a/tests/test-rest-api-comment-data-from-post.php +++ b/tests/test-rest-api-comment-data-from-post.php @@ -88,7 +88,6 @@ public function test_email_when_no_author() { 'forum' => get_option( 'disqus_forum_url' ), ]; $data = $this->call_comment_data_from_post( $post ); - $this->assertStringEndsWith( '@disqus.com', $data['comment_author_email'] ); - $this->assertNotEmpty( $data['comment_author_email'] ); + $this->assertNull( $data['comment_author_email'] ); } }