Skip to content
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

Adds pagination and classic mode integration with Block Catalog #164

Merged
merged 2 commits into from
May 13, 2024
Merged
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
58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

Convert to Blocks is a WordPress plugin that transforms classic editor content to blocks on-the-fly. After installing Gutenberg or upgrading to WordPress 5.0+, your content will be displayed in "Classic Editor Blocks". While these blocks are completely functional and will display fine on the frontend of your website, they do not empower editors to fully make use of the block editing experience. In order to do so, your classic editor posts need to be converted to blocks. This plugin does that for you "on the fly". When an editor goes to edit a classic post, the content will be parsed into blocks. When the editor saves the post, the new structure will be saved into the database. This strategy reduces risk as you are only altering database values for content that needs to be changed.

When combined with the [Block Catalog Plugin](https://github.com/10up-block-catalog/block-catalog), Convert to Blocks can be used to bulk convert only classic editor content to blocks. This is especially useful for sites with a combination of classic and block editor content.

### Bulk migration of Classic Editor items to the Block Editor

The `wp convert-to-blocks start` [WP-CLI command](https://github.com/10up/convert-to-blocks/blob/4df0e970c51eee8d84e3edf3c6210dc10011d574/includes/ConvertToBlocks/MigrationCommand.php) that converts posts iteratively in the browser without requiring any manual input. One caveat worth mentioning is that Gutenberg is a CPU intensive application. You will want to keep your computer plugged in before doing this!
The `wp convert-to-blocks start` [WP-CLI command](https://github.com/10up/convert-to-blocks/blob/4df0e970c51eee8d84e3edf3c6210dc10011d574/includes/ConvertToBlocks/MigrationCommand.php) that converts posts iteratively in the browser without requiring any manual input. One caveat worth mentioning is that Gutenberg is a CPU intensive application. You will want to keep your computer plugged in before doing this!

![Demo of WP-CLI command](.wordpress-org/screenshot-1.gif "Example of a convert-to-blocks WP-CLI command bulk migration")

Expand All @@ -28,6 +30,60 @@ The `wp convert-to-blocks start` [WP-CLI command](https://github.com/10up/conver
2. Inside the repository directory, run `npm install` and then `npm run build`.
3. Inside the repository directory, run `composer install`.

## Command Line Options

```bash
NAME

wp convert-to-blocks start

DESCRIPTION

Starts a new Migration. The command prints the URL that must be opened in a browser to connect it to the WP CLI.

SYNOPSIS

wp convert-to-blocks start [--post_type=<post_type>] [--per_page=<per_page>] [--page=<page>] [--only=<only>] [--catalog] [--reset]

OPTIONS

[--post_type=<post_type>]
Optional comma delimited list of post types to migrate. Defaults to post,page

[--per_page=<per_page>]
Optional number of posts to migrate per batch. Defaults to no limit. Combine with --page to paginate.

[--page=<page>]
Optional page number to start migration from. Defaults to 1.

[--only=<only>]
Optional comma delimited list of post ids to migrate.

[--catalog]
Optional flag to only migrate classic editor tagged posts. Requires that Block
Catalog plugin is present and has been indexed.

[--reset]
Stops any currently running migration and resets the migration state.

NAME

wp convert-to-blocks stop

DESCRIPTION

Stops the currently running migration if active.

NAME

wp convert-to-blocks status

DESCRIPTION

Prints the status of the currently running migration.

```

## Frequently Asked Questions

### How Do I Know It's Working?
Expand Down
22 changes: 21 additions & 1 deletion includes/ConvertToBlocks/MigrationAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,28 @@ public function get_posts_to_update( $opts = [] ) {
$post_type = [ 'post', 'page' ];
}

$posts_per_page = $opts['per_page'] ?? -1;
$page = $opts['page'] ?? 1;

$query_params = [
'post_type' => $post_type,
'post_status' => 'publish',
'fields' => 'ids',
'posts_per_page' => -1,
'posts_per_page' => $posts_per_page,
'paged' => $page,
'ignore_sticky_posts' => true,
];

if ( ! empty( $opts['catalog'] ) ) {
$query_params['tax_query'] = [
[
'taxonomy' => BLOCK_CATALOG_TAXONOMY,
'field' => 'slug',
'terms' => [ 'core-classic' ],
],
];
}

/**
* Filter query parameters for the query to get the posts to be updated.
*
Expand All @@ -209,6 +223,12 @@ public function get_posts_to_update( $opts = [] ) {
$query = new \WP_Query( $query_params );
$posts = $query->posts;

if ( ! empty( $posts ) && defined( '\WP_CLI' ) && \WP_CLI ) {
$posts_per_page = $posts_per_page > 0 ? $posts_per_page : $query->found_posts;
$pages = ceil( $query->found_posts / $posts_per_page );
\WP_CLI::line( sprintf( 'Pagination: %d/%d of %d', $page, $pages, $query->found_posts ) );
}

return $posts;
}

Expand Down
39 changes: 39 additions & 0 deletions includes/ConvertToBlocks/MigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,47 @@ class MigrationCommand extends \WP_CLI_Command {
* [--post_type=<post_type>]
* : Optional comma delimited list of post types to migrate. Defaults to post,page
*
* [--per_page=<per_page>]
* : Optional number of posts to migrate per batch. Defaults to no limit. Combine with --page to paginate.
*
* [--page=<page>]
* : Optional page number to start migration from. Defaults to 1.
*
* [--only=<only>]
* : Optional comma delimited list of post ids to migrate.
*
* [--catalog]
* : Optional flag to only migrate classic editor tagged posts. Requires that Block
* Catalog plugin is present and has been indexed.
*
* [--reset]
* : Stops any currently running migration and resets the migration state.
*
* @param array $args The command args
* @param array $opts The command opts
*/
public function start( $args = [], $opts = [] ) {
$agent = new MigrationAgent();
$delay = 5; // 5 second delay between each tick

$opts['catalog'] = ! empty( $opts['catalog'] ) ? $opts['catalog'] : false;
$opts['catalog'] = filter_var( $opts['catalog'], FILTER_VALIDATE_BOOLEAN );

$opts['reset'] = ! empty( $opts['reset'] ) ? $opts['reset'] : false;
$opts['reset'] = filter_var( $opts['reset'], FILTER_VALIDATE_BOOLEAN );

$opts['per_page'] = ! empty( $opts['per_page'] ) ? intval( $opts['per_page'] ) : -1;
$opts['paged'] = ! empty( $opts['page'] ) ? intval( $opts['page'] ) : 1;

if ( $opts['reset'] ) {
$this->stop( $args, $opts );
}

// check if block-catalog plugin is installed
if ( $opts['catalog'] && ! defined( 'BLOCK_CATALOG_PLUGIN_VERSION' ) ) {
\WP_CLI::error( __( 'The Block Catalog plugin must be active and indexed to run in --catalog mode.', 'convert-to-blocks' ) );
}

if ( $agent->is_running() ) {
\WP_CLI::error( 'Please stop the currently running migration first.' );
}
Expand All @@ -47,6 +78,14 @@ public function start( $args = [], $opts = [] ) {
}

\WP_CLI::log( 'Migration started...' );

$options = [];
$options[] = 'Posts Per Page: ' . $opts['per_page'];
$options[] = 'Page: ' . ( ! empty( $opts['page'] ) ? $opts['page'] : 1 );
$options[] = 'Catalog: ' . ( $opts['catalog'] ? 'true' : 'false' );

\WP_CLI::log( 'Options: ' . implode( ', ', $options ) );

\WP_CLI::log( 'Please open the following URL in a browser to start the migration agent.' );
\WP_CLI::line( '' );
\WP_CLI::log( $result );
Expand Down
34 changes: 0 additions & 34 deletions tests/cypress/e2e/block.test.js

This file was deleted.

Loading