-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tutorial to lesson migration command #3041
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
if ( ! defined( 'WP_CLI' ) ) { | ||
return; | ||
} | ||
|
||
/** | ||
* Converts a tutorial post to a lesson post type. | ||
*/ | ||
class WPORG_Learn_Tutorial_To_Lesson_Command extends WP_CLI_Command { | ||
/** | ||
* Converts tutorials to lessons based on the provided URL or file. | ||
* | ||
* ## OPTIONS | ||
* | ||
* <source> | ||
* : 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 urls.txt --file | ||
* wp wporg-learn-tutorial-to-lesson convert urls.txt --file --live | ||
*/ | ||
public function convert( $args, $assoc_args ) { | ||
$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 ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. some consideration should be made for memory expensive operations when looping through an indeterminate number of items |
||
$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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL |
||
|
||
if ( ! $post_id ) { | ||
WP_CLI::warning( "No post found for URL: $url" ); | ||
return; | ||
} | ||
|
||
$post = get_post( $post_id ); | ||
|
||
if ( ! $post ) { | ||
WP_CLI::warning( "Could not retrieve post with ID: $post_id" ); | ||
return; | ||
} | ||
|
||
if ( 'wporg_workshop' !== $post->post_type ) { | ||
WP_CLI::warning( "Post is not a tutorial (Post ID: $post_id)" ); | ||
return; | ||
} | ||
|
||
if ( 'lesson' === $post->post_type ) { | ||
WP_CLI::warning( "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::warning( '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' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in most cases having a
dry-run
variable is how it's normally done, but this is a minor nitpick and not a blocker.