io = $io; } /** * Prints the stored CSS, and nothing else. * * The output is byte for byte what is stored, with no newline added, * so it can be piped back into `set` unchanged. * * ## EXAMPLES * * # Back the CSS up to a file. * $ wp adminkeep css get > site.css * * @param array $args Positional arguments. * @param array $assoc_args Flags. * @return void */ public function get( $args, $assoc_args ) { // Not line(): its added newline would make a get piped back into set a change. $this->io->out( CustomCss::css() ); } /** * Replaces the stored CSS with a file's contents, or with standard input. * * The CSS is checked the way the Custom CSS screen checks it: * anything that looks like an HTML tag is refused. Empty input * clears the CSS; if CSS is already stored, clearing it asks for * confirmation first, unless --yes is given. * * ## OPTIONS * * [] * : Path to a CSS file. Leave it out, or pass -, to read standard input. * * [--yes] * : Skip the confirmation when clearing stored CSS. * * ## EXAMPLES * * $ wp adminkeep css set site.css * $ cat site.css | wp adminkeep css set * * # Clear the stored CSS without being asked to confirm. * $ printf '' | wp adminkeep css set --yes * * @param array $args Optional file path. * @param array $assoc_args Flags. * @return void */ public function set( $args, $assoc_args ) { $path = isset( $args[0] ) ? (string) $args[0] : '-'; if ( '-' === $path ) { $css = $this->io->stdin(); } else { if ( ! is_file( $path ) || ! is_readable( $path ) ) { $this->io->error( sprintf( 'Cannot read %s.', $path ) ); } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- A local file named on the command line. $css = (string) file_get_contents( $path ); } // Refused before the confirmation below, so a bad file never asks a question first. $valid = CustomCss::validate( $css ); if ( is_wp_error( $valid ) ) { $this->io->error( $valid->get_error_message() ); } $stored = CustomCss::css(); if ( '' === trim( $css ) && '' !== trim( $stored ) && empty( $assoc_args['yes'] ) ) { $this->io->confirm( sprintf( 'This clears %d bytes of stored CSS. Continue?', strlen( $stored ) ) ); } // The same writer as the screen and the REST route; it normalises line endings too. $css = CustomCss::save( $css ); if ( is_wp_error( $css ) ) { $this->io->error( $css->get_error_message() ); } $message = '' === trim( $css ) ? 'Custom CSS cleared.' : sprintf( 'Custom CSS saved (%d bytes).', strlen( $css ) ); if ( ! Settings::is_enabled( 'custom_css' ) ) { $message .= ' It is stored but not printed while the feature is off; turn it on with: wp adminkeep feature enable custom_css'; } $this->io->success( $message ); } }