io = $io; } /** * Prints plugin-level settings. * * With a key, prints that value and nothing else. Without one, lists * every setting: key, value, type and the values it accepts. * * ## OPTIONS * * [] * : One setting to print on its own. * * [--format=] * : Output format for the list. * --- * default: table * options: * - table * - json * - csv * - yaml * --- * * ## EXAMPLES * * $ wp adminkeep setting get hide_unused * false * * $ wp adminkeep setting get --format=json * * @param array $args Optional key. * @param array $assoc_args Flags. * @return void */ public function get( $args, $assoc_args ) { $schema = PluginSettings::schema(); $values = PluginSettings::all(); if ( ! empty( $args ) ) { $key = (string) $args[0]; if ( ! isset( $schema[ $key ] ) ) { $this->io->error( sprintf( 'Unknown setting "%s". Keys: %s.', $key, implode( ', ', array_keys( $schema ) ) ) ); } $this->io->line( $this->as_text( $values[ $key ] ) ); return; } $format = isset( $assoc_args['format'] ) ? (string) $assoc_args['format'] : 'table'; $native = in_array( $format, array( 'json', 'yaml' ), true ); $rows = array(); foreach ( $schema as $key => $spec ) { $rows[] = array( 'key' => $key, 'value' => $native ? $values[ $key ] : $this->as_text( $values[ $key ] ), 'type' => $spec['type'], 'accepts' => $this->accepts( $spec ), ); } $this->io->items( $format, $rows, array( 'key', 'value', 'type', 'accepts' ) ); } /** * Changes one or more plugin-level settings. Everything not named is kept. * * Values: true/false for a switch, a whole number, or plain text — the * same as `wp adminkeep feature set`. Every pair is checked before * anything is written. * * ## OPTIONS * * ... * : One or more settings, as key=value pairs. `wp adminkeep setting get` lists the keys. * * ## EXAMPLES * * # Show only the features that are on, for every administrator. * $ wp adminkeep setting set hide_unused=true * * @param array $args key=value pairs. * @param array $assoc_args Flags. * @return void */ public function set( $args, $assoc_args ) { $schema = PluginSettings::schema(); if ( empty( $args ) ) { $this->io->error( sprintf( 'Give at least one key=value pair. Keys: %s.', implode( ', ', array_keys( $schema ) ) ) ); } $changes = array(); // Parse everything before writing anything. foreach ( $args as $pair ) { $split = ValueParser::split( $pair ); if ( is_wp_error( $split ) ) { $this->io->error( $split->get_error_message() ); } list( $key, $raw ) = $split; if ( ! isset( $schema[ $key ] ) ) { $this->io->error( sprintf( 'Unknown setting "%s". Keys: %s.', $key, implode( ', ', array_keys( $schema ) ) ) ); } $value = ValueParser::parse( $raw, $schema[ $key ] ); if ( is_wp_error( $value ) ) { $this->io->error( sprintf( '%s: %s', $key, $value->get_error_message() ) ); } $changes[ $key ] = $value; } PluginSettings::set( $changes ); $this->io->success( sprintf( 'Updated: %s.', implode( ', ', array_keys( $changes ) ) ) ); } /** * What a setting accepts, for the list. * * @param array $spec Schema entry. * @return string */ private function accepts( array $spec ) { switch ( $spec['type'] ) { case 'bool': return 'true or false'; case 'int': return 'a whole number'; default: return isset( $spec['choices'] ) ? implode( ', ', $spec['choices'] ) : 'text'; } } /** * A value as the terminal shows it. * * @param mixed $value Value. * @return string */ private function as_text( $value ) { if ( is_bool( $value ) ) { return $value ? 'true' : 'false'; } return is_array( $value ) ? implode( ',', $value ) : (string) $value; } }