diff --git a/app/Console/Commands/WordpressInstall.php b/app/Console/Commands/WordpressInstall.php new file mode 100644 index 0000000..c5931c4 --- /dev/null +++ b/app/Console/Commands/WordpressInstall.php @@ -0,0 +1,108 @@ + 'Enpress', + 'email' => 'admin@example.com', + 'username' => '', + 'password' => '' + ]; + + /** + * Create a new command instance. + * + * @return void + */ + public function __construct() + { + parent::__construct(); + } + + /** + * Create a new command instance. + * + * @return void + */ + private function getInput() : void + { + $input = collect($this->input->getArguments()) + ->only([ + 'title', + 'username', + 'password', + 'email' + ]); + + if (!isset($input['title']) || empty($input['title'])) { + $input['title'] = $this->ask('Website title:', 'Enpress'); + } + + if (!isset($input['username']) || empty($input['username'])) { + $input['username'] = $this->ask('Admin username:'); + } + + if (!isset($input['password']) || empty($input['password'])) { + $input['password'] = $this->secret('Admin password:'); + } + + // Any answers to missing data will override existing defaults + $this->siteDetails = $input->union($this->siteDetails); + } + + private function validate() : void + { + $requiredDetails = collect($this->siteDetails)->keys(); + + foreach ($requiredDetails as $detail) { + if (empty($this->siteDetails[$detail])) { + $this->error(Str::title(str_replace('_', ' ', $detail)). ' must not be blank'); + exit(1); + } + } + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + $this->getInput(); + $this->validate(); + + require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); + + $response = wp_install( + $this->siteDetails['title'], + $this->siteDetails['username'], + $this->siteDetails['email'], + true, + '', + wp_slash( $this->siteDetails['password'] ), + 'en_US' + ); + + return $response; + } +} diff --git a/artisan b/artisan index 8a6247b..98e0451 100755 --- a/artisan +++ b/artisan @@ -13,6 +13,12 @@ | */ +// If the wp:install command is run, indicate to Wordpress we're installing +// to prevent redirects +if (isset($argv[1]) && $argv[1] === 'wp:install') { + define('WP_INSTALLING', true); +} + require __DIR__. '/public/cms/wp-load.php'; $app = app(); diff --git a/bootstrap/app.php b/bootstrap/app.php index 0e37fd9..db4512b 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -29,6 +29,19 @@ $_SERVER['HTTPS'] = 'on'; } +/* +|-------------------------------------------------------------------------- +| Wordpress non-HTTP request fix +|-------------------------------------------------------------------------- +| +| Commands through artisan do not set this server header and Wordpress +| expects it to exist otherwise it crashes. +*/ +if ($app->runningInConsole()) { + $_SERVER['SERVER_PROTOCOL'] = null; + $_SERVER['SERVER_NAME'] = 'localhost'; +} + /* |-------------------------------------------------------------------------- | Bind Important Interfaces