diff --git a/disqus/rest-api/class-disqus-rest-api.php b/disqus/rest-api/class-disqus-rest-api.php index a45a454..248a6be 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,14 @@ 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'; + } } // Translate the comment approval state. @@ -804,10 +806,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'], 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..0cf6103 --- /dev/null +++ b/tests/test-rest-api-comment-data-from-post.php @@ -0,0 +1,93 @@ +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->assertNull( $data['comment_author_email'] ); + } +}