From ed5987d9438c8fb7b340445bba8c96fb4b8d36af Mon Sep 17 00:00:00 2001 From: Adam Wood <1017872+adamwoodnz@users.noreply.github.com> Date: Wed, 27 Nov 2024 17:08:51 +1300 Subject: [PATCH 1/3] Add CLI command for converting a single URL --- .gitignore | 1 + wp-content/mu-plugins/pub/wporg-learn-cli.php | 85 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100755 wp-content/mu-plugins/pub/wporg-learn-cli.php diff --git a/.gitignore b/.gitignore index 1c33c5813..0943e3ccd 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,7 @@ !/wp-content/mu-plugins/pub/class-validator.php !/wp-content/mu-plugins/pub/locale-switcher !/wp-content/mu-plugins/pub/locale-switcher/.eslintrc.js +!/wp-content/mu-plugins/pub/wporg-learn-cli.php !/wp-content/plugins !/wp-content/plugins/wporg-learn !/wp-content/plugins/sensei-pro diff --git a/wp-content/mu-plugins/pub/wporg-learn-cli.php b/wp-content/mu-plugins/pub/wporg-learn-cli.php new file mode 100755 index 000000000..697fce380 --- /dev/null +++ b/wp-content/mu-plugins/pub/wporg-learn-cli.php @@ -0,0 +1,85 @@ + + * : The URL of the tutorial post to convert + * + * [--live] + * : Actually perform the conversion (default is dry-run) + * + * ## EXAMPLES + * + * wp wporg-learn-tutorial-to-lesson convert https://learn.wordpress.org/tutorial/slug + * wp wporg-learn-tutorial-to-lesson convert https://learn.wordpress.org/tutorial/slug --live + * + * @param array $args Command arguments. + * @param array $assoc_args Associative arguments. + */ + public function convert( $args, $assoc_args ) { + $url = $args[0]; + $is_dry_run = ! isset( $assoc_args['live'] ); + + // Get post ID from URL + $post_id = url_to_postid( $url ); + + if ( ! $post_id ) { + WP_CLI::error( "No post found for URL: $url" ); + return; + } + + $post = get_post( $post_id ); + + if ( ! $post ) { + WP_CLI::error( "Could not retrieve post with ID: $post_id" ); + return; + } + + if ( 'wporg_workshop' !== $post->post_type ) { + WP_CLI::error( "Post is not a tutorial (Post ID: $post_id)" ); + return; + } + + if ( 'lesson' === $post->post_type ) { + WP_CLI::error( "Post is already a lesson (Post ID: $post_id)" ); + return; + } + + if ( $is_dry_run ) { + WP_CLI::line( sprintf( + "Dry run for:\nURL: %s\nTitle: %s\nPost ID: %d\nPost Type: %s\n---------------", + $url, + $post->post_title, + $post_id, + $post->post_type + ) ); + return; + } + + // Update the post type + $updated = wp_update_post( array( + 'ID' => $post_id, + 'post_type' => 'lesson', + ) ); + + if ( is_wp_error( $updated ) ) { + WP_CLI::error( 'Failed to update post type: ' . $updated->get_error_message() ); + return; + } + + WP_CLI::success( "Successfully converted tutorial to lesson (Post ID: $post_id)" ); + } +} + +WP_CLI::add_command( 'wporg-learn-tutorial-to-lesson', 'WPORG_Learn_Tutorial_To_Lesson_Command' ); From 8bd62288aedb4367f27cc4ab6f2b02bfaca63537 Mon Sep 17 00:00:00 2001 From: Adam Wood <1017872+adamwoodnz@users.noreply.github.com> Date: Wed, 27 Nov 2024 17:26:41 +1300 Subject: [PATCH 2/3] Add ability for command to process a list of urls in a file --- wp-content/mu-plugins/pub/wporg-learn-cli.php | 54 ++++++++++++++----- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/wp-content/mu-plugins/pub/wporg-learn-cli.php b/wp-content/mu-plugins/pub/wporg-learn-cli.php index 697fce380..93c84b213 100755 --- a/wp-content/mu-plugins/pub/wporg-learn-cli.php +++ b/wp-content/mu-plugins/pub/wporg-learn-cli.php @@ -9,50 +9,78 @@ */ class WPORG_Learn_Tutorial_To_Lesson_Command extends WP_CLI_Command { /** - * Converts a tutorial post to a lesson based on the provided URL. + * Converts tutorials to lessons based on the provided URL or file. * * ## OPTIONS * - * - * : The URL of the tutorial post to convert + * + * : The URL of the tutorial post to convert, or path to a file containing URLs (one per line) * * [--live] * : Actually perform the conversion (default is dry-run) * + * [--file] + * : Indicates that the source is a file containing URLs + * * ## EXAMPLES * * wp wporg-learn-tutorial-to-lesson convert https://learn.wordpress.org/tutorial/slug - * wp wporg-learn-tutorial-to-lesson convert https://learn.wordpress.org/tutorial/slug --live - * - * @param array $args Command arguments. - * @param array $assoc_args Associative arguments. + * wp wporg-learn-tutorial-to-lesson convert urls.txt --file + * wp wporg-learn-tutorial-to-lesson convert urls.txt --file --live */ public function convert( $args, $assoc_args ) { - $url = $args[0]; + $source = $args[0]; $is_dry_run = ! isset( $assoc_args['live'] ); + $is_file = isset( $assoc_args['file'] ); + + $urls = array(); + if ( $is_file ) { + if ( ! file_exists( $source ) ) { + WP_CLI::error( "File not found: $source" ); + return; + } + $urls = array_filter( explode( "\n", file_get_contents( $source ) ) ); + } else { + $urls = array( $source ); + } + + foreach ( $urls as $url ) { + $url = trim( $url ); + if ( empty( $url ) ) { + continue; + } + // Process each URL + $this->process_url( $url, $is_dry_run ); + } + } + + /** + * Process a single URL. + */ + private function process_url( $url, $is_dry_run ) { // Get post ID from URL $post_id = url_to_postid( $url ); if ( ! $post_id ) { - WP_CLI::error( "No post found for URL: $url" ); + WP_CLI::warning( "No post found for URL: $url" ); return; } $post = get_post( $post_id ); if ( ! $post ) { - WP_CLI::error( "Could not retrieve post with ID: $post_id" ); + WP_CLI::warning( "Could not retrieve post with ID: $post_id" ); return; } if ( 'wporg_workshop' !== $post->post_type ) { - WP_CLI::error( "Post is not a tutorial (Post ID: $post_id)" ); + WP_CLI::warning( "Post is not a tutorial (Post ID: $post_id)" ); return; } if ( 'lesson' === $post->post_type ) { - WP_CLI::error( "Post is already a lesson (Post ID: $post_id)" ); + WP_CLI::warning( "Post is already a lesson (Post ID: $post_id)" ); return; } @@ -74,7 +102,7 @@ public function convert( $args, $assoc_args ) { ) ); if ( is_wp_error( $updated ) ) { - WP_CLI::error( 'Failed to update post type: ' . $updated->get_error_message() ); + WP_CLI::warning( 'Failed to update post type: ' . $updated->get_error_message() ); return; } From e496d8f481cba7ab9532c3597ab9fa662173896c Mon Sep 17 00:00:00 2001 From: Adam Wood <1017872+adamwoodnz@users.noreply.github.com> Date: Thu, 28 Nov 2024 09:22:34 +1300 Subject: [PATCH 3/3] Simplify command semantics --- wp-content/mu-plugins/pub/wporg-learn-cli.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/wp-content/mu-plugins/pub/wporg-learn-cli.php b/wp-content/mu-plugins/pub/wporg-learn-cli.php index 93c84b213..753ca4ccf 100755 --- a/wp-content/mu-plugins/pub/wporg-learn-cli.php +++ b/wp-content/mu-plugins/pub/wporg-learn-cli.php @@ -24,11 +24,11 @@ class WPORG_Learn_Tutorial_To_Lesson_Command extends WP_CLI_Command { * * ## EXAMPLES * - * wp wporg-learn-tutorial-to-lesson convert https://learn.wordpress.org/tutorial/slug - * wp wporg-learn-tutorial-to-lesson convert urls.txt --file - * wp wporg-learn-tutorial-to-lesson convert urls.txt --file --live + * wp wporg-learn convert-tutorial-to-lesson https://learn.wordpress.org/tutorial/slug + * wp wporg-learn convert-tutorial-to-lesson urls.txt --file + * wp wporg-learn convert-tutorial-to-lesson urls.txt --file --live */ - public function convert( $args, $assoc_args ) { + public function __invoke( $args, $assoc_args ) { $source = $args[0]; $is_dry_run = ! isset( $assoc_args['live'] ); $is_file = isset( $assoc_args['file'] ); @@ -110,4 +110,4 @@ private function process_url( $url, $is_dry_run ) { } } -WP_CLI::add_command( 'wporg-learn-tutorial-to-lesson', 'WPORG_Learn_Tutorial_To_Lesson_Command' ); +WP_CLI::add_command( 'wporg-learn convert-tutorial-to-lesson', 'WPORG_Learn_Tutorial_To_Lesson_Command' );