Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require via CLI #28

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/WP/CLI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace WebDevStudios\Required_Plugins\WP\CLI;

class CLI {
public function run() : void {
$this->register_hooks();
}

private function register_hooks() : void {
add_action( 'cli_init', [ $this, 'add_command' ] );
}

public function add_command() : void {
\WP_CLI::add_command( 'require', [ $this, 'run_command' ], [
'shortdesc' => __( "Require a plugin.", 'wds-required-plugins' ),
] );
}

public function run_command( array $args, array $assoc_args ) : void {
$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}" ) );
}

\WP_CLI::log( $file );
}

private function resolve_plugin_file( string $file ) : string {
if ( file_exists( $file ) ) {
return realpath( $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();
}

init();
14 changes: 9 additions & 5 deletions wds-required-plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -30,15 +34,15 @@
* @subpackage Project
* @since Unknown
*/
class WDS_Required_Plugins {
class Plugin {

/**
* Instance of this class.
*
* @author Justin Sternberg
* @since Unknown
*
* @var WDS_Required_Plugins object
* @var Plugin object
*/
public static $instance = null;

Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -657,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();
Expand Down Expand Up @@ -722,4 +726,4 @@ private function get_required_header() {
}

// Init.
WDS_Required_Plugins::init();
Plugin::init();