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

Feature/add export command #52

Merged
merged 9 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
110 changes: 63 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,46 @@
[![Support Level](https://img.shields.io/badge/support-beta-blueviolet.svg)](#support-level) ![WordPress tested up to version](https://img.shields.io/badge/WordPress-v6.1%20tested-success.svg) [![GPLv2 License](https://img.shields.io/github/license/10up/block-catalog.svg)](https://github.com/10up/block-catalog/blob/develop/LICENSE.md)

## Table of Contents
* [Features](#features)
* [Requirements](#requirements)
* [Installation](#installation)
* [Getting Started](#getting-started)
* [WP CLI Commands](#wp-cli-commands)
* [FAQs](#frequently-asked-questions)
* [Support](#support-level)
* [Changelog](#changelog)
* [Contributing](#contributing)

- [Features](#features)
- [Requirements](#requirements)
- [Installation](#installation)
- [Getting Started](#getting-started)
- [WP CLI Commands](#wp-cli-commands)
- [FAQs](#frequently-asked-questions)
- [Support](#support-level)
- [Changelog](#changelog)
- [Contributing](#contributing)

## Features

* Find which blocks are used across your site.
* Fully Integrated with the WordPress Admin.
* Use filters to see Posts that use a specific block.
* Find Posts that use Reusable Blocks.
* Use the WP CLI to quickly find blocks from the command line.
* Use custom WordPress filters to extend the Block Catalog.
* Find block usage on a Multisite network.
- Find which blocks are used across your site.
- Fully Integrated with the WordPress Admin.
- Use filters to see Posts that use a specific block.
- Find Posts that use Reusable Blocks.
- Use the WP CLI to quickly find blocks from the command line.
- Use custom WordPress filters to extend the Block Catalog.
- Find block usage on a Multisite network.
- Export block catalog data to a CSV file via the WP CLI.

## Requirements

* PHP 7.4+
* [WordPress](http://wordpress.org) 5.7+
- PHP 7.4+
- [WordPress](http://wordpress.org) 5.7+

## Installation

* Block Catalog can be installed like any other plugin from the [WordPress.org plugin directory](https://wordpress.org/plugins/block-catalog).
* You can also install the plugin manually by [downloading a zip file](https://github.com/10up/block-catalog/releases/latest).
* To install the plugin using [composer](https://getcomposer.org) and [wpackagist](https://wpackagist.org/), add the following to your composer.json.
- Block Catalog can be installed like any other plugin from the [WordPress.org plugin directory](https://wordpress.org/plugins/block-catalog).
- You can also install the plugin manually by [downloading a zip file](https://github.com/10up/block-catalog/releases/latest).
- To install the plugin using [composer](https://getcomposer.org) and [wpackagist](https://wpackagist.org/), add the following to your composer.json.

```json
"wpackagist-plugin/block-catalog":"~1.3.1"
```

## Getting Started

On activation, the plugin will prompt you to index your content. You need to do this first before you will be able to see the various blocks used on your site. You can also go to *WP-Admin > Tools > Block Catalog* to do this yourself.
On activation, the plugin will prompt you to index your content. You need to do this first before you will be able to see the various blocks used on your site. You can also go to _WP-Admin > Tools > Block Catalog_ to do this yourself.

![Screenshot of Block Catalog Tools](.wordpress-org/screenshot-1.png)

Expand All @@ -64,67 +66,81 @@ You can also filter the listing to only show Posts that have a specific block.

The following WP CLI commands are supported by the Block Catalog plugin.

* `wp block-catalog index [--only=<only>] [--dry-run]`
Iterates through all posts and catalogs them one at a time.
- `wp block-catalog index [--only=<only>] [--dry-run]`
Iterates through all posts and catalogs them one at a time.

* [--reset]
- [--reset]
Deletes the previous index before starting.

* [--only=\<only\>]
- [--only=\<only\>]
Limits the command to the specified comma delimited post ids.

* [--network=\<network\>]
- [--network=\<network\>]
Indexes the entire network. Accepts a comma delimited list of child site ids.

* [--dry-run]
- [--dry-run]
Runs catalog without saving changes to the DB.

* `wp block-catalog find <blocks>... [--index] [--fields] [--format] [--post_type] [--posts_per_page] [--post_status] [--count=<count>] [--operator=<operator>]`
Finds the list of posts having the specified block(s)
- `wp block-catalog find <blocks>... [--index] [--fields] [--format] [--post_type] [--posts_per_page] [--post_status] [--count=<count>] [--operator=<operator>]`
Finds the list of posts having the specified block(s)

* \<blocks\>...
- \<blocks\>...
The block names to search for, eg:- core/embed

* [--index]
- [--index]
Whether to re-index before searching.

* [--fields=\<fields\>]
- [--fields=\<fields\>]
List of post fields to display.

* [--format=\<format\>]
- [--format=\<format\>]
Output format, default table.

* [--post_type=\<post_type\>]
- [--post_type=\<post_type\>]
Limit search to specified post types.

* [--posts_per_page=\<posts_per_page\>]
- [--posts_per_page=\<posts_per_page\>]
Number of posts to find per page, default 20

* [--post_status=\<post_status\>]
- [--post_status=\<post_status\>]
Post status of posts to search, default 'publish'

* [--count=\<count\>]
- [--count=\<count\>]
Prints total found posts, default true. When combined with `--network` prints an aggregate across the multisite.

* [--operator=\<operator\>]
- [--operator=\<operator\>]
The query operator to be used in the search clause. Default IN.

* [--network=\<network\>]
- [--network=\<network\>]
Searches across the entire network if on multisite. Accepts a comma delimited list of child site ids.

* `wp block-catalog delete-index`
Resets the Block Catalog by removing all catalog terms.
- `wp block-catalog delete-index`
Resets the Block Catalog by removing all catalog terms.

* [--network=\<network\>]
- [--network=\<network\>]
Deletes the indexes across the entire network. Accepts a comma delimited list of child site ids.

- `wp block-catalog post-blocks <post-id> [--index]`
Prints the list of blocks in the specified post.

* `wp block-catalog post-blocks <post-id> [--index]`
Prints the list of blocks in the specified post.

* \<post-id\>
- \<post-id\>
The post id to lookup blocks for.

- `wp block-catalog export [--output=<output>] [--post_type=<types>] [--posts_per_block=<number>] [--ignore_parent=<ignore_parent>]`
Exports the posts associated with the 'block_catalog' taxonomy to a CSV file.

- `[--output=<output>]`
Path to the CSV file. Defaults to `/tmp/block-catalog.csv`.

- `[--post_type=<types>]`
Comma-delimited list of post types. Optional.

- `[--posts_per_block=<number>]`
Number of posts per block, default to -1 (all). Optional.

- `[--ignore_parent=<ignore_parent>]`
Ignore top level blocks. Optional. Default true.

## Frequently Asked Questions

### 1) Why does the Plugin require indexing?
Expand Down
47 changes: 47 additions & 0 deletions includes/classes/CatalogCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,53 @@ function ( $term ) {
\WP_CLI\Utils\format_items( 'table', $block_items, [ 'ID', 'Block' ] );
}

/**
* Exports the posts associated with the 'block_catalog' taxonomy to a CSV file.
*
* ## OPTIONS
*
* [--output=<output>]
* : Path to the CSV file. Defaults to /tmp/block-catalog.csv
*
* [--post_type=<types>]
* : Comma-delimited list of post types. Optional.
*
* [--posts_per_block=<number>]
* : Number of posts per block, default to -1 (all). Optional.
*
* [--ignore_parent=<ignore_parent>]
* : Ignore top level blocks. Optional. Default true
*
* ## EXAMPLES
*
* wp block-catalog export --output=path/to/csv
*
* @when after_wp_load
*
* @param array $args Positional arguments.
* @param array $opts Optional arguments.
*/
public function export( $args = [], $opts = [] ) {
$output = isset( $opts['output'] ) ? $opts['output'] : '/tmp/block-catalog.csv';
$post_types = isset( $opts['post_type'] ) ? explode( ',', $opts['post_type'] ) : array();
$posts_per_block = isset( $opts['posts_per_block'] ) ? intval( $opts['posts_per_block'] ) : -1;

$opts['output'] = $output;
$opts['post_type'] = $post_types;
$opts['posts_per_block'] = $posts_per_block;

$exporter = new \BlockCatalog\CatalogExporter();
$result = $exporter->export( $output, $opts );

if ( is_wp_error( $result ) ) {
\WP_CLI::error( $result->get_error_message() );
} elseif ( ! $result['success'] ) {
\WP_CLI::error( $result['message'] );
} else {
\WP_CLI::success( $result['message'] );
}
}

/**
* Returns the list of post ids to migrate.
*
Expand Down
Loading
Loading