Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.
Open
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
59 changes: 59 additions & 0 deletions includes/formatters/comments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace WP_CLI\Hammer\Formatters\Comments;
use WP_CLI\Iterators\Query;

/**
* Action to be run for manipulating comments table, all formatters are passed as a parameter.
* @param array $formatters All formatters for all tables
*/
function comments( $formatters ) {
global $wpdb;
$comments_query = "SELECT * FROM $wpdb->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' );
3 changes: 2 additions & 1 deletion tests/test-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}

/**
Expand Down
16 changes: 15 additions & 1 deletion tests/wp-hammer-testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down