Skip to content

Commit

Permalink
Merge branch 'trunk' into phpcs/ActionScheduler_WPCommentCleaner.php
Browse files Browse the repository at this point in the history
  • Loading branch information
crstauf committed Oct 29, 2024
2 parents 60d9812 + ef1021e commit 15e2c68
Show file tree
Hide file tree
Showing 17 changed files with 325 additions and 164 deletions.
21 changes: 14 additions & 7 deletions .github/workflows/pr-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ jobs:
matrix:
# We test against the earliest and latest PHP versions for each major supported version.
php: [ '7.0', '7.4', '8.0', '8.3' ]
wp: [ '6.4', '6.5', 'latest', 'nightly' ]
wp: [ '6.4', '6.5', '6.6', 'latest', 'nightly' ]
multisite: [ '0', '1' ]
exclude:
# WordPress 6.6+ requires PHP 7.2+
- php: 7.0
wp: 6.6
- php: 7.0
wp: latest
- php: 7.0
Expand Down Expand Up @@ -44,14 +46,19 @@ jobs:
php --version
composer --version
- name: Get cached composer directories
uses: actions/cache@v2
- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache dependencies
uses: actions/cache@v4
with:
path: ./vendor
key: ${{ runner.os }}-${{ hashFiles('./composer.lock') }}
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Setup and install composer
run: composer install
- name: Install dependencies
run: composer install --prefer-dist

- name: Setup PHPUnit
run: |
Expand Down
2 changes: 1 addition & 1 deletion action-scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Version: 3.8.2
* License: GPLv3
* Requires at least: 6.4
* Tested up to: 6.6
* Tested up to: 6.7
* Requires PHP: 7.0
*
* Copyright 2019 Automattic, Inc. (https://automattic.com/contact/)
Expand Down
16 changes: 4 additions & 12 deletions classes/WP_CLI/ActionScheduler_WPCLI_Scheduler_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ public function run( $args, $assoc_args ) {

$batches_completed = 0;
$actions_completed = 0;
$unlimited = $batches === 0;
if ( is_callable( [ ActionScheduler::store(), 'set_claim_filter' ] ) ) {
$unlimited = 0 === $batches;
if ( is_callable( array( ActionScheduler::store(), 'set_claim_filter' ) ) ) {
$exclude_groups = $this->parse_comma_separated_string( $exclude_groups );

if ( ! empty( $exclude_groups ) ) {
ActionScheduler::store()->set_claim_filter('exclude-groups', $exclude_groups );
ActionScheduler::store()->set_claim_filter( 'exclude-groups', $exclude_groups );
}
}

Expand Down Expand Up @@ -141,8 +141,6 @@ private function parse_comma_separated_string( $string ): array {
/**
* Print WP CLI message about how many actions are about to be processed.
*
* @author Jeremy Pry
*
* @param int $total Number of actions found.
*/
protected function print_total_actions( $total ) {
Expand All @@ -158,8 +156,6 @@ protected function print_total_actions( $total ) {
/**
* Print WP CLI message about how many batches of actions were processed.
*
* @author Jeremy Pry
*
* @param int $batches_completed Number of completed batches.
*/
protected function print_total_batches( $batches_completed ) {
Expand All @@ -175,11 +171,9 @@ protected function print_total_batches( $batches_completed ) {
/**
* Convert an exception into a WP CLI error.
*
* @author Jeremy Pry
*
* @param Exception $e The error object.
*
* @throws \WP_CLI\ExitException
* @throws \WP_CLI\ExitException Under some conditions WP CLI may throw an exception.
*/
protected function print_error( Exception $e ) {
WP_CLI::error(
Expand All @@ -194,8 +188,6 @@ protected function print_error( Exception $e ) {
/**
* Print a success message with the number of completed actions.
*
* @author Jeremy Pry
*
* @param int $actions_completed Number of completed actions.
*/
protected function print_success( $actions_completed ) {
Expand Down
34 changes: 27 additions & 7 deletions classes/WP_CLI/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,39 @@
*/
class ProgressBar {

/** @var integer */
/**
* Current number of ticks.
*
* @var integer
*/
protected $total_ticks;

/** @var integer */
/**
* Total number of ticks.
*
* @var integer
*/
protected $count;

/** @var integer */
/**
* Progress bar update interval.
*
* @var integer
*/
protected $interval;

/** @var string */
/**
* Progress bar message.
*
* @var string
*/
protected $message;

/** @var \cli\progress\Bar */
/**
* Instance.
*
* @var \cli\progress\Bar
*/
protected $progress_bar;

/**
Expand All @@ -38,7 +58,7 @@ class ProgressBar {
* @param string $message Text to display before the progress bar.
* @param integer $count Total number of ticks to be performed.
* @param integer $interval Optional. The interval in milliseconds between updates. Default 100.
*
*
* @throws \Exception When this is not run within WP CLI.
*/
public function __construct( $message, $count, $interval = 100 ) {
Expand All @@ -64,7 +84,7 @@ public function tick() {
$this->progress_bar->tick();
$this->total_ticks++;

do_action( 'action_scheduler/progress_tick', $this->total_ticks );
do_action( 'action_scheduler/progress_tick', $this->total_ticks ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
}

/**
Expand Down
87 changes: 49 additions & 38 deletions classes/data-stores/ActionScheduler_wpCommentLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
class ActionScheduler_wpCommentLogger extends ActionScheduler_Logger {
const AGENT = 'ActionScheduler';
const TYPE = 'action_log';
const TYPE = 'action_log';

/**
* Create log entry.
Expand All @@ -16,8 +16,8 @@ class ActionScheduler_wpCommentLogger extends ActionScheduler_Logger {
*
* @return string The log entry ID
*/
public function log( $action_id, $message, DateTime $date = NULL ) {
if ( empty($date) ) {
public function log( $action_id, $message, DateTime $date = null ) {
if ( empty( $date ) ) {
$date = as_get_datetime_object();
} else {
$date = as_get_datetime_object( clone $date );
Expand All @@ -35,18 +35,19 @@ public function log( $action_id, $message, DateTime $date = NULL ) {
*/
protected function create_wp_comment( $action_id, $message, DateTime $date ) {

$comment_date_gmt = $date->format('Y-m-d H:i:s');
$comment_date_gmt = $date->format( 'Y-m-d H:i:s' );
ActionScheduler_TimezoneHelper::set_local_timezone( $date );
$comment_data = array(
'comment_post_ID' => $action_id,
'comment_date' => $date->format('Y-m-d H:i:s'),
'comment_post_ID' => $action_id,
'comment_date' => $date->format( 'Y-m-d H:i:s' ),
'comment_date_gmt' => $comment_date_gmt,
'comment_author' => self::AGENT,
'comment_content' => $message,
'comment_agent' => self::AGENT,
'comment_type' => self::TYPE,
'comment_author' => self::AGENT,
'comment_content' => $message,
'comment_agent' => self::AGENT,
'comment_type' => self::TYPE,
);
return wp_insert_comment($comment_data);

return wp_insert_comment( $comment_data );
}

/**
Expand All @@ -58,7 +59,8 @@ protected function create_wp_comment( $action_id, $message, DateTime $date ) {
*/
public function get_entry( $entry_id ) {
$comment = $this->get_comment( $entry_id );
if ( empty($comment) || $comment->comment_type != self::TYPE ) {

if ( empty( $comment ) || self::TYPE !== $comment->comment_type ) {
return new ActionScheduler_NullLogEntry();
}

Expand All @@ -76,23 +78,30 @@ public function get_entry( $entry_id ) {
*/
public function get_logs( $action_id ) {
$status = 'all';
if ( get_post_status($action_id) == 'trash' ) {
$logs = array();

if ( get_post_status( $action_id ) === 'trash' ) {
$status = 'post-trashed';
}
$comments = get_comments(array(
'post_id' => $action_id,
'orderby' => 'comment_date_gmt',
'order' => 'ASC',
'type' => self::TYPE,
'status' => $status,
));
$logs = array();

$comments = get_comments(
array(
'post_id' => $action_id,
'orderby' => 'comment_date_gmt',
'order' => 'ASC',
'type' => self::TYPE,
'status' => $status,
)
);

foreach ( $comments as $c ) {
$entry = $this->get_entry( $c );
if ( !empty($entry) ) {

if ( ! empty( $entry ) ) {
$logs[] = $entry;
}
}

return $logs;
}

Expand All @@ -105,20 +114,18 @@ protected function get_comment( $comment_id ) {
return get_comment( $comment_id );
}



/**
* Filter comment queries.
*
* @param WP_Comment_Query $query Comment query object.
*/
public function filter_comment_queries( $query ) {
foreach ( array('ID', 'parent', 'post_author', 'post_name', 'post_parent', 'type', 'post_type', 'post_id', 'post_ID') as $key ) {
if ( !empty($query->query_vars[$key]) ) {
foreach ( array( 'ID', 'parent', 'post_author', 'post_name', 'post_parent', 'type', 'post_type', 'post_id', 'post_ID' ) as $key ) {
if ( ! empty( $query->query_vars[ $key ] ) ) {
return; // don't slow down queries that wouldn't include action_log comments anyway.
}
}
$query->query_vars['action_log_filter'] = TRUE;
$query->query_vars['action_log_filter'] = true;
add_filter( 'comments_clauses', array( $this, 'filter_comment_query_clauses' ), 10, 2 );
}

Expand All @@ -131,7 +138,7 @@ public function filter_comment_queries( $query ) {
* @return array
*/
public function filter_comment_query_clauses( $clauses, $query ) {
if ( !empty($query->query_vars['action_log_filter']) ) {
if ( ! empty( $query->query_vars['action_log_filter'] ) ) {
$clauses['where'] .= $this->get_where_clause();
}
return $clauses;
Expand Down Expand Up @@ -192,17 +199,21 @@ protected function get_comment_count() {
$stats = get_transient( 'as_comment_count' );

if ( ! $stats ) {
$stats = array();

$count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} WHERE comment_type NOT IN('order_note','action_log') GROUP BY comment_approved", ARRAY_A );

$total = 0;
$stats = array();
$approved = array( '0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed' );
$stats = array();
$count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} WHERE comment_type NOT IN('order_note','action_log') GROUP BY comment_approved", ARRAY_A );
$total = 0;
$stats = array();
$approved = array(
'0' => 'moderated',
'1' => 'approved',
'spam' => 'spam',
'trash' => 'trash',
'post-trashed' => 'post-trashed',
);

foreach ( (array) $count as $row ) {
// Don't count post-trashed toward totals.
if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) {
if ( 'post-trashed' !== $row['comment_approved'] && 'trash' !== $row['comment_approved'] ) {
$total += $row['num_comments'];
}
if ( isset( $approved[ $row['comment_approved'] ] ) ) {
Expand Down Expand Up @@ -258,14 +269,14 @@ public function init() {
* Defer comment counting.
*/
public function disable_comment_counting() {
wp_defer_comment_counting(true);
wp_defer_comment_counting( true );
}

/**
* Enable comment counting.
*/
public function enable_comment_counting() {
wp_defer_comment_counting(false);
wp_defer_comment_counting( false );
}

}
Loading

0 comments on commit 15e2c68

Please sign in to comment.