. * * Commands act as the user given with the global --user flag and pass the same * capability checks as the sheet UI, e.g. wp rafgrid bulk ... --user=admin. */ class CLI extends \WP_CLI_Command { /** * Stop unless --user points to someone allowed to use the editor. */ private static function require_user() { if ( ! Access::can_use() ) { \WP_CLI::error( 'Run this command as a user who can use Rafgrid, for example: wp rafgrid types --user=admin' ); } } /** * List editable post types. */ public function types() { self::require_user(); foreach ( Post_Types::editable() as $slug => $label ) { \WP_CLI::line( $slug . "\t" . wp_strip_all_tags( $label ) ); } } /** * Bulk-edit items. * * ## OPTIONS * * --type= * --column= * --mode= * [--value=] * [--ids=<1,2,3>] * * @param array $args Args. * @param array $assoc Assoc args. */ public function bulk( $args, $assoc ) { self::require_user(); $ids = array(); if ( ! empty( $assoc['ids'] ) ) { $ids = array_map( 'absint', explode( ',', $assoc['ids'] ) ); } else { $ids = Saving::filtered_ids( sanitize_key( $assoc['type'] ?? 'post' ), array(), 5000 ); } $res = Saving::bulk( $ids, sanitize_text_field( $assoc['column'] ?? '' ), sanitize_key( $assoc['mode'] ?? 'set' ), $assoc['value'] ?? '' ); \WP_CLI::success( 'updated=' . $res['updated'] . ' failed=' . $res['failed'] . ' history=' . $res['history_id'] ); } /** * Export a list to CSV on stdout. * * ## OPTIONS * * [--type=] * * @param array $args Args. * @param array $assoc Assoc args. */ public function export( $args, $assoc ) { self::require_user(); \WP_CLI::line( Export::to_csv( sanitize_key( $assoc['type'] ?? 'post' ), array( 'per_page' => 500 ) ) ); } } \WP_CLI::add_command( 'rafgrid', __NAMESPACE__ . '\\CLI' );