namespace WP_CLI;
-interface Fetcher {
+abstract class Fetcher {
- // Returns the item if found; otherwise returns false
- function get( $id );
+ protected $msg;
- // Returns the item if found; otherwise calls WP_CLI::error()
- function get_check( $id );
+ /**
+ * @param string $msg The message to display when an item is not found
+ */
+ function __construct( $msg ) {
+ $this->msg = $msg;
+ }
- // Returns the list of found items
- function get_many( $ids );
+ /**
+ * @param string $arg The raw CLI argument
+ * @return mixed|false The item if found; false otherwise
+ */
+ abstract public function get( $arg );
+
+ /**
+ * Like get(), but calls WP_CLI::error() instead of returning false.
+ *
+ * @param string $arg The raw CLI argument
+ */
+ public function get_check( $arg ) {
+ $item = $this->get( $arg );
+
+ if ( ! $item ) {
+ \WP_CLI::error( sprintf( $this->msg, $arg ) );
+ }
+
+ return $item;
+ }
+
+ /**
+ * @param array The raw CLI arguments
+ * @return array The list of found items
+ */
+ public function get_many( $args ) {
+ $items = array();
+
+ foreach ( $args as $arg ) {
+ $item = $this->get( $arg );
+
+ if ( $item ) {
+ $items[] = $item;
+ } else {
+ \WP_CLI::warning( sprintf( $this->msg, $arg ) );
+ }
+ }
+
+ return $items;
+ }
}
namespace WP_CLI;
-class FetcherPlugin implements Fetcher {
+class FetcherPlugin extends Fetcher {
- /**
- * @param string $name The plugin slug
- * @return string|false The plugin filename
- */
public function get( $name ) {
$plugins = get_plugins( '/' . $name );
}
}
- return $file;
- }
-
- public function get_check( $name ) {
- $file = $this->get( $name );
-
- if ( ! $file ) {
- \WP_CLI::error( "The '$name' plugin could not be found." );
- }
-
- return $file;
- }
-
- public function get_many( $args ) {
- $plugins = array();
-
- foreach ( $args as $name ) {
- $file = $this->get( $name );
- if ( $file ) {
- $plugins[] = (object) array(
- 'name' => $name,
- 'file' => $file
- );
- } else {
- \WP_CLI::warning( "The '$name' plugin could not be found." );
- }
- }
-
- return $plugins;
+ return (object) array(
+ 'name' => $name,
+ 'file' => $file
+ );
}
}
namespace WP_CLI;
-class FetcherUser implements Fetcher {
+class FetcherUser extends Fetcher {
public function get( $id_or_login ) {
if ( is_numeric( $id_or_login ) )
return $user;
}
-
- public function get_check( $id_or_login ) {
- $user = $this->get( $id_or_login );
-
- if ( ! $user ) {
- \WP_CLI::error( "Invalid user ID or login: $id_or_login" );
- }
-
- return $user;
- }
-
- public function get_many( $ids_or_logins ) {
- $users = array();
-
- foreach ( $ids_or_logins as $id_or_login ) {
- $user = $this->get( $id_or_login );
- if ( $user ) {
- $users[] = $user;
- } else {
- \WP_CLI::warning( "Invalid user ID or login: $id_or_login" );
- }
- }
-
- return $users;
- }
}
parent::__construct();
- $this->fetcher = new \WP_CLI\FetcherPlugin;
+ $this->fetcher = new \WP_CLI\FetcherPlugin( "The '%s' plugin could not be found." );
}
protected function get_upgrader_class( $force ) {
}
protected function status_single( $args ) {
- $file = $this->fetcher->get_check( $args[0] );
+ $plugin = $this->fetcher->get_check( $args[0] );
+ $file = $plugin->file;
$details = $this->get_details( $file );
* wp plugin get bbpress --format=json
*/
public function get( $args, $assoc_args ) {
- $file = $this->get_check( $args[0] );
+ $plugin = $this->fetcher->get_check( $args[0] );
+ $file = $plugin->file;
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $file, false, false );
);
public function __construct() {
- $this->fetcher = new \WP_CLI\FetcherUser;
+ $this->fetcher = new \WP_CLI\FetcherUser( "Invalid user ID or login: '%s'" );
}
/**