Skip to content

Commit

Permalink
Merge pull request #7160 from Automattic/add/progress-validation-cli
Browse files Browse the repository at this point in the history
Add progress validation command
  • Loading branch information
m1r0 authored Nov 6, 2023
2 parents 3c01456 + f93f87f commit 4de7eca
Show file tree
Hide file tree
Showing 11 changed files with 942 additions and 26 deletions.
13 changes: 4 additions & 9 deletions includes/class-sensei-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* @package sensei
*/

defined( 'ABSPATH' ) || exit;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* CLI class.
Expand All @@ -17,21 +19,14 @@ class Sensei_CLI {
* Class constructor.
*/
public function __construct() {
$this->load();
$this->register();
}

/**
* Load the command files.
*/
private function load() {
require_once dirname( __FILE__ ) . '/cli/class-sensei-db-seed-command.php';
}

/**
* Register the CLI commands.
*/
private function register() {
WP_CLI::add_command( 'sensei db seed', Sensei_DB_Seed_Command::class );
WP_CLI::add_command( 'sensei validate progress', Sensei_Validate_Progress_Command::class );
}
}
4 changes: 1 addition & 3 deletions includes/class-sensei-course.php
Original file line number Diff line number Diff line change
Expand Up @@ -2319,9 +2319,7 @@ public function load_user_courses_content( $user = false ) {
* Returns a list of all courses
*
* @since 1.8.0
* @return array $courses{
* @type $course WP_Post
* }
* @return WP_Post[]
*/
public static function get_all_courses() {

Expand Down
4 changes: 3 additions & 1 deletion includes/cli/class-sensei-db-seed-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* @package sensei
*/

defined( 'ABSPATH' ) || exit;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* WP-CLI command that helps with seeding the database.
Expand Down
66 changes: 66 additions & 0 deletions includes/cli/class-sensei-validate-progress-command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Sensei_Validate_Progress_Command class file.
*
* @package sensei
*/

use Sensei\Internal\Migration\Validations\Progress_Validation;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* WP-CLI command that validates the progress data.
*
* @since $$next-version$$
*/
class Sensei_Validate_Progress_Command {
/**
* Seed the database.
*
* @since $$next-version$$
*
* @param array $args Command arguments.
* @param array $assoc_args Command arguments with names.
*/
public function __invoke( array $args = [], array $assoc_args = [] ) {
$progress_validation = new Progress_Validation();

$progress_validation->run();

if ( ! $progress_validation->has_errors() ) {
WP_CLI::success( 'Progress data is valid.' );
return;
}

$this->output_validation_errors( $progress_validation );

WP_CLI::error( 'Progress data is not valid.' );
}

/**
* Output the validation errors.
*
* @since $$next-version$$
*
* @param Progress_Validation $progress_validation Progress validation.
*/
private function output_validation_errors( Progress_Validation $progress_validation ) {
foreach ( $progress_validation->get_errors() as $error ) {
WP_CLI::warning( $error->get_message() );

if ( $error->has_data() ) {
$error_data = $error->get_data();
$error_data = is_array( $error_data[0] ) ? $error_data : [ $error_data ];

WP_CLI\Utils\format_items(
'table',
$error_data,
array_keys( $error_data[0] )
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private function get_comments_and_meta( int $after_comment_id, bool $dry_run ):
// At the moment we don't care about post meta for course progress.
if ( 'sensei_lesson_status' === $progress_comment->comment_type ) {
// Map the post ID to the comment ID. Is used later to map post meta to the comment ID.
$post_ids[ $progress_comment->comment_post_ID ] = $progress_comment->comment_ID;
$post_ids[ $progress_comment->comment_post_ID ][] = $progress_comment->comment_ID;
}
}

Expand Down Expand Up @@ -188,8 +188,10 @@ private function get_comments_and_meta( int $after_comment_id, bool $dry_run ):
$mapped_meta[ $comment_id ]['status'] = $comment_status;
}
} else {
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
$mapped_meta[ $comment_id ][ $meta->meta_key ] = $meta->meta_value;
foreach ( $post_ids[ $meta->post_id ] as $comment_id ) {
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
$mapped_meta[ $comment_id ][ $meta->meta_key ] = $meta->meta_value;
}
}
}

Expand Down
Loading

0 comments on commit 4de7eca

Please sign in to comment.