diff --git a/includes/formatters/comments.php b/includes/formatters/comments.php new file mode 100644 index 0000000..09ea2ea --- /dev/null +++ b/includes/formatters/comments.php @@ -0,0 +1,59 @@ +comments"; + $comments = new Query( $comments_query ); + + while ( $comments->valid() ) { + $original_comment = (array) $comments->current(); + $modified_comment = (array) $comments->current(); + + foreach ( $formatters['comments'] as $column => $formatter ) { + $modified_comment = apply_filters( 'wp_hammer_run_formatter_filter_comments_' . $column, $modified_comment, $formatter ); + } + + $modified = array_diff( $modified_comment, $original_comment ); + + if ( count( $modified ) ) { + \WP_CLI::line( "Making change to comment {$original_comment[ 'comment_ID' ]} to contain " . wp_json_encode( $modified ) ); + $wpdb->update( + "$wpdb->comments", + $modified, + array( 'comment_ID' => $original_comment['comment_ID'] ), + '%s', + '%d' + ); + } + $comments->next(); + } +} + +/** + * @param array $comment Represents a row in wp_comments table + * @param string $formatter String for email, with other columns substituted with __COLUMN_NAME__ + * + * @return mixed + */ +function comment_author_email( $comment, $formatter ) { + preg_match_all( '/__([a-zA-Z0-9-_]*)__/', $formatter, $matches ); + if ( is_array( $matches ) && 2 === count( $matches ) ) { + foreach ( $matches[1] as $match ) { + if ( isset( $comment[ $match ] ) ) { + $formatter = str_replace( "__{$match}__", $comment[ $match ], $formatter ); + } + } + $comment['comment_author_email'] = $formatter; + } + return $comment; +} + +add_filter( 'wp_hammer_run_formatter_filter_comments_comment_author_email', __NAMESPACE__ . '\comment_author_email', null, 2 ); +add_action( 'wp_hammer_run_formatter_comments', __NAMESPACE__ . '\comments' ); diff --git a/tests/test-settings.php b/tests/test-settings.php index 9cef131..18b5399 100644 --- a/tests/test-settings.php +++ b/tests/test-settings.php @@ -15,9 +15,10 @@ public function testDryRun() { * Check Formatters */ public function testFormats() { - $this->assertEquals( 4, count( $this->settings->formats ), 'Valid Format Count' ); + $this->assertEquals( 5, count( $this->settings->formats ), 'Valid Format Count' ); $this->assertEquals( 'posts.post_author=auto', $this->settings->formats[0], 'Valid table.column=type parse'); $this->assertEquals( 'users.user_email=ivan+__ID__@kruchkoff.com', $this->settings->formats[2], 'Valid users.user_email=email@format parse' ); + $this->assertEquals( 'comments.comment_author_email=ivan+__comment_ID__@kruchkoff.com', $this->settings->formats[4], 'Valid comments.comment_author_email=email@format parse' ); } /** diff --git a/tests/wp-hammer-testcase.php b/tests/wp-hammer-testcase.php index a75db7b..4a6da14 100644 --- a/tests/wp-hammer-testcase.php +++ b/tests/wp-hammer-testcase.php @@ -58,11 +58,25 @@ public function setUp() { $this->author1_page2 = wp_insert_post( $page ); + $comment = array( + 'comment_post_ID' => $this->author1_post1, + 'comment_author_email' => 'commenter_1@example.org', + ); + + $this->comment_1 = $this->factory()->comment->create( $comment ); + + $comment = array( + 'comment_post_ID' => $this->author1_post2, + 'comment_author_email' => 'commenter_2@example.org', + ); + + $this->comment_2 = $this->factory()->comment->create( $comment ); + $args = array( "-l", "users=5,posts=100.post_date", "-f", - "posts.post_author=auto,users.user_pass=auto,users.user_email=ivan+__ID__@kruchkoff.com,posts.post_title=ipsum", + "posts.post_author=auto,users.user_pass=auto,users.user_email=ivan+__ID__@kruchkoff.com,posts.post_title=ipsum,comments.comment_author_email=ivan+__comment_ID__@kruchkoff.com", ); $assoc_args = array(); $this->settings = new WP_CLI\Hammer\Settings();