From 63f1f1d7610fac4435b503122169225ca26acb46 Mon Sep 17 00:00:00 2001 From: Aubrey Portwood Date: Fri, 29 Jan 2021 12:09:30 -0700 Subject: [PATCH 1/3] Progress. --- src/WP/CLI.php | 25 +++++++++++++++++++++++++ wds-required-plugins.php | 12 ++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 src/WP/CLI.php diff --git a/src/WP/CLI.php b/src/WP/CLI.php new file mode 100644 index 0000000..9b3829a --- /dev/null +++ b/src/WP/CLI.php @@ -0,0 +1,25 @@ +register_hooks(); + } + + private function register_hooks() : void { + add_action( 'cli_init', [ $this, 'add_command' ] ); + } + + public function add_command() : void { + add_command( 'require', [ $this, 'run_command' ], [ + 'shortdesc' => __( "Require a plugin.", 'wds-required-plugins' ), + ] ); + } + + public function run_command( array $args, array $assoc_args ) : void { + WP_CLI::log( 'out' ); + } +} + +new CLI()->run(); diff --git a/wds-required-plugins.php b/wds-required-plugins.php index 6896def..984b73c 100644 --- a/wds-required-plugins.php +++ b/wds-required-plugins.php @@ -17,11 +17,15 @@ * Required: true */ +namespace \WebDevStudios\Required_Plugins; + // Exit if accessed directly. if ( ! defined( 'ABSPATH' ) ) { exit; } +require_once 'src/WP/CLI.php'; + /** * Required plugins class * @@ -30,7 +34,7 @@ * @subpackage Project * @since Unknown */ -class WDS_Required_Plugins { +class Plugin { /** * Instance of this class. @@ -38,7 +42,7 @@ class WDS_Required_Plugins { * @author Justin Sternberg * @since Unknown * - * @var WDS_Required_Plugins object + * @var Plugin object */ public static $instance = null; @@ -91,7 +95,7 @@ class WDS_Required_Plugins { * @since 0.1.0 * @author Justin Sternberg * - * @return WDS_Required_Plugins A single instance of this class. + * @return Plugin A single instance of this class. */ public static function init() { if ( null === self::$instance ) { @@ -722,4 +726,4 @@ private function get_required_header() { } // Init. -WDS_Required_Plugins::init(); +Plugin::init(); From ad1a9ac222334e1ad7d467db7852b9676bda8b35 Mon Sep 17 00:00:00 2001 From: Aubrey Portwood Date: Fri, 29 Jan 2021 12:53:53 -0700 Subject: [PATCH 2/3] Scaffold CLI. --- src/WP/CLI.php | 28 ++++++++++++++++++++++++---- wds-required-plugins.php | 4 ++-- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/WP/CLI.php b/src/WP/CLI.php index 9b3829a..4eedc49 100644 --- a/src/WP/CLI.php +++ b/src/WP/CLI.php @@ -1,6 +1,6 @@ __( "Require a plugin.", 'wds-required-plugins' ), ] ); } public function run_command( array $args, array $assoc_args ) : void { - WP_CLI::log( 'out' ); + $this->require( $this->resolve_plugin_file( $args[0] ?? '' ) ); } + + private function require( string $file ) : void { + if ( ! file_exists( $file ) ) { + \WP_CLI::error( sprintf( __( "Could not location plugin: %s", 'wds-required-plugins' ), "{$file}" ) ); + } + } + + private function resolve_plugin_file( string $file ) : string { + if ( file_exists( $file ) ) { + return $file; // What they gave us is there. + } + + // Try and use from working directory, maybe it's relative. + return getcwd() . "/{$file}"; + } +} + +function init() { + $cli = new CLI(); + $cli->run(); } -new CLI()->run(); +init(); diff --git a/wds-required-plugins.php b/wds-required-plugins.php index 984b73c..548d690 100644 --- a/wds-required-plugins.php +++ b/wds-required-plugins.php @@ -17,7 +17,7 @@ * Required: true */ -namespace \WebDevStudios\Required_Plugins; +namespace WebDevStudios\Required_Plugins; // Exit if accessed directly. if ( ! defined( 'ABSPATH' ) ) { @@ -661,7 +661,7 @@ public function get_header_required_plugins() { $all_plugins = apply_filters( 'all_plugins', get_plugins() ); if ( empty( $all_plugins ) ) { - return; + return []; } $required_header = $this->get_required_header(); From 10b601c133361a0b32487f06d07cfdadd3e87703 Mon Sep 17 00:00:00 2001 From: Aubrey Portwood Date: Fri, 29 Jan 2021 14:10:19 -0700 Subject: [PATCH 3/3] Return the real path even when it returns false. --- src/WP/CLI.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/WP/CLI.php b/src/WP/CLI.php index 4eedc49..2c6d75f 100644 --- a/src/WP/CLI.php +++ b/src/WP/CLI.php @@ -25,11 +25,13 @@ private function require( string $file ) : void { if ( ! file_exists( $file ) ) { \WP_CLI::error( sprintf( __( "Could not location plugin: %s", 'wds-required-plugins' ), "{$file}" ) ); } + + \WP_CLI::log( $file ); } private function resolve_plugin_file( string $file ) : string { if ( file_exists( $file ) ) { - return $file; // What they gave us is there. + return realpath( $file ) ?: ''; // What they gave us is there. } // Try and use from working directory, maybe it's relative.