exit_code = (int) $exit_code; parent::__construct( 'pawa_cli_halt:' . $this->exit_code, $this->exit_code ); } } /** * Shared I/O for `wp pawa …`. * * @since 1.0.0 */ class Pawa_Cli_IO { /** * When true, halt() throws Pawa_Cli_Halt instead of exiting (PHP tests). * * @var bool */ public static $capture = false; /** * Last JSON payload emitted (tests). * * @var array|null */ public static $last_payload = null; /** * Last halt code (tests). * * @var int */ public static $last_exit = 0; /** * Captured stdout when $capture is true. * * @var string */ public static $stdout = ''; /** * Reset capture buffers (PHP tests). */ public static function reset_capture() { self::$capture = false; self::$last_payload = null; self::$last_exit = 0; self::$stdout = ''; } /** * @param array $assoc_args Assoc args. * @return string table|json|csv|yaml|count|human */ public static function format( $assoc_args ) { $assoc_args = is_array( $assoc_args ) ? $assoc_args : array(); $format = isset( $assoc_args['format'] ) ? strtolower( (string) $assoc_args['format'] ) : ''; if ( '' === $format ) { return 'human'; } if ( in_array( $format, array( 'json', 'table', 'csv', 'yaml', 'count', 'human' ), true ) ) { return $format; } return 'human'; } /** * @param array $assoc_args Assoc args. * @return bool */ public static function wants_json( $assoc_args ) { return 'json' === self::format( $assoc_args ); } /** * @param array $assoc_args Assoc args. * @return bool */ public static function is_yes( $assoc_args ) { $assoc_args = is_array( $assoc_args ) ? $assoc_args : array(); return ! empty( $assoc_args['yes'] ); } /** * Read --json= or --json-file=. Empty array when neither is set. * * @param array $assoc_args Assoc args. * @return array|WP_Error */ public static function read_json( $assoc_args ) { $assoc_args = is_array( $assoc_args ) ? $assoc_args : array(); $raw = null; if ( isset( $assoc_args['json-file'] ) && '' !== (string) $assoc_args['json-file'] ) { $path = (string) $assoc_args['json-file']; if ( ! is_readable( $path ) ) { $cwd = function_exists( 'getcwd' ) ? getcwd() : ''; $try = $cwd ? $cwd . DIRECTORY_SEPARATOR . $path : $path; if ( is_readable( $try ) ) { $path = $try; } } if ( ! is_readable( $path ) ) { return new WP_Error( 'json_file_unreadable', sprintf( 'JSON file is not readable: %s', $path ) ); } $raw = file_get_contents( $path ); if ( false === $raw ) { return new WP_Error( 'json_file_unreadable', 'Failed to read JSON file.' ); } } elseif ( array_key_exists( 'json', $assoc_args ) ) { $json = $assoc_args['json']; if ( true === $json || '' === $json ) { return new WP_Error( 'invalid_json', 'Pass --json=\'{...}\' or --json-file=. The --json flag requires a JSON string.' ); } $raw = (string) $json; } if ( null === $raw ) { return array(); } $raw = trim( (string) $raw ); if ( '' === $raw ) { return new WP_Error( 'invalid_json', 'JSON input is empty.' ); } $decoded = json_decode( $raw, true ); if ( JSON_ERROR_NONE !== json_last_error() || ! is_array( $decoded ) ) { $msg = function_exists( 'json_last_error_msg' ) ? json_last_error_msg() : 'JSON parse error'; return new WP_Error( 'invalid_json', 'Invalid JSON input: ' . $msg ); } return $decoded; } /** * Require JSON object input. * * @param array $assoc_args Assoc args. * @return array|WP_Error */ public static function require_json( $assoc_args ) { $decoded = self::read_json( $assoc_args ); if ( is_wp_error( $decoded ) ) { return $decoded; } if ( array() === $decoded ) { return new WP_Error( 'missing_json', 'This command requires --json=\'{...}\' or --json-file=.' ); } return $decoded; } /** * Emit one JSON object and halt. * * @param bool $success Success flag. * @param mixed $data Data payload. * @param array $meta Optional meta (lists). * @param int $exit Exit code. */ public static function send_data( $success, $data, $meta = array(), $exit = 0 ) { $payload = array( 'success' => (bool) $success, 'data' => $data, ); if ( is_array( $meta ) && $meta ) { $payload['meta'] = $meta; } self::emit_json( $payload ); self::halt( (int) $exit ); } /** * Emit an error envelope and halt with a non-zero code. * * @param string $code Machine code. * @param string $message Human message. * @param int $exit Exit code. * @param array $extra Extra error keys. */ public static function send_error( $code, $message, $exit = 1, $extra = array() ) { $error = array( 'code' => (string) $code, 'message' => (string) $message, ); if ( is_array( $extra ) ) { foreach ( $extra as $key => $value ) { if ( ! isset( $error[ $key ] ) ) { $error[ $key ] = $value; } } } self::emit_json( array( 'success' => false, 'error' => $error, ) ); self::halt( $exit > 0 ? (int) $exit : 1 ); } /** * @param array $payload Payload. */ public static function emit_json( $payload ) { $payload = self::redact( $payload ); $flags = 0; if ( defined( 'JSON_UNESCAPED_SLASHES' ) ) { $flags |= JSON_UNESCAPED_SLASHES; } if ( defined( 'JSON_UNESCAPED_UNICODE' ) ) { $flags |= JSON_UNESCAPED_UNICODE; } if ( defined( 'JSON_PRETTY_PRINT' ) ) { $flags |= JSON_PRETTY_PRINT; } $json = wp_json_encode( $payload, $flags ); if ( ! is_string( $json ) || '' === $json ) { $json = '{"success":false,"error":{"code":"json_encode_failed","message":"Failed to encode JSON output."}}'; } self::$last_payload = $payload; self::emit( $json . "\n" ); } /** * @param string $text Text to write to stdout. */ public static function emit( $text ) { if ( self::$capture ) { self::$stdout .= (string) $text; return; } echo $text; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CLI stdout. } /** * Halt the process. Never returns in production WP-CLI. * * @param int $code Exit code. */ public static function halt( $code ) { $code = (int) $code; self::$last_exit = $code; if ( self::$capture ) { throw new Pawa_Cli_Halt( (int) $code ); } if ( class_exists( 'WP_CLI' ) && method_exists( 'WP_CLI', 'halt' ) ) { WP_CLI::halt( $code ); } exit( $code ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** * Recursively redact secrets using Workflow Automation's shared redactor when available. * * @param mixed $data Payload. * @return mixed */ public static function redact( $data ) { return pawa_redact_sensitive_fields( $data ); } /** * Check the user a command acts as. * * Workflow Automation never picks or signs in a user itself: the user is * the one WP-CLI's own global `--user` flag signed in, and a command that * changes automations or runs them refuses to go on without one, or when * that user lacks the capability. * * @param string $capability Capability the command needs (default: the * capability that manages automations). * @return true|WP_Error */ public static function require_user( $capability = '' ) { $capability = '' !== (string) $capability ? (string) $capability : pawa_manage_capability(); if ( get_current_user_id() <= 0 ) { return new WP_Error( 'pawa_cli_user_required', 'This command acts as a WordPress user. Run it with WP-CLI\'s --user flag set to an administrator, for example --user=admin.' ); } if ( ! current_user_can( $capability ) ) { return new WP_Error( 'pawa_cli_forbidden', sprintf( 'The user given with --user does not have the "%s" capability this command needs.', $capability ) ); } return true; } /** * Print a list as a table (or count) for human output. * * @param array $items Rows (list of assoc arrays). * @param string[] $fields Field keys. * @param string $format table|count|csv|yaml. */ public static function print_items( $items, $fields, $format = 'table' ) { $items = is_array( $items ) ? $items : array(); $fields = is_array( $fields ) ? $fields : array(); if ( 'count' === $format ) { self::line( (string) count( $items ) ); return; } if ( class_exists( '\WP_CLI\Formatter' ) ) { $assoc_args = array( 'format' => $format ); $formatter = new \WP_CLI\Formatter( $assoc_args, $fields ); $formatter->display_items( $items ); return; } if ( 'csv' === $format ) { self::line( implode( ',', $fields ) ); foreach ( $items as $row ) { $line = array(); foreach ( $fields as $field ) { $val = isset( $row[ $field ] ) ? $row[ $field ] : ''; $line[] = self::csv_field( $val ); } self::line( implode( ',', $line ) ); } return; } self::line( implode( "\t", $fields ) ); foreach ( $items as $row ) { $line = array(); foreach ( $fields as $field ) { $val = isset( $row[ $field ] ) ? $row[ $field ] : ''; $line[] = is_scalar( $val ) || null === $val ? (string) $val : wp_json_encode( $val ); } self::line( implode( "\t", $line ) ); } } /** * Human log line (never used in JSON mode by callers). * * @param string $text Text. */ public static function line( $text ) { if ( class_exists( 'WP_CLI' ) && method_exists( 'WP_CLI', 'log' ) ) { WP_CLI::log( (string) $text ); return; } self::emit( (string) $text . "\n" ); } /** * @param mixed $value Cell. * @return string */ private static function csv_field( $value ) { $str = is_scalar( $value ) || null === $value ? (string) $value : wp_json_encode( $value ); if ( false !== strpos( $str, ',' ) || false !== strpos( $str, '"' ) || false !== strpos( $str, "\n" ) ) { return '"' . str_replace( '"', '""', $str ) . '"'; } return $str; } }