@@ -29,6 +29,8 @@ class InstallWordPress extends Command
2929 */
3030 protected $ description = 'Installs WordPress with PHPdotenv for Moox Press. ' ;
3131
32+ protected string $ wpCliPath = 'wp ' ;
33+
3234 /**
3335 * Execute the console command.
3436 */
@@ -308,25 +310,47 @@ public function useOrInstallWpCli(): void
308310 $ this ->error ('Failed to move wp-cli.phar to ' .$ targetDir );
309311 exit (1 );
310312 } else {
313+ $ this ->wpCliPath = $ targetPath ;
311314 $ this ->info ('wp-cli installed successfully in ' .$ targetDir );
312315 }
313316 } else {
314- $ this -> info ( ' Moving wp-cli.phar to /usr/local/bin/wp... ' ) ;
315- $ moveProcess = new Process ([
316- ' mv ' ,
317- base_path ( ' wp-cli.phar ' ),
318- ' /usr/local/bin/wp ' ,
319- ]);
317+ $ home = getenv ( ' HOME ' ) ?: '' ;
318+
319+ if ( $ home === '' ) {
320+ $ this -> error ( ' Could not determine the home directory. ' );
321+ exit ( 1 );
322+ }
320323
321- $ moveProcess -> run () ;
324+ $ targetDir = $ home . ' /.local/bin ' ;
322325
323- if ($ moveProcess ->isSuccessful ()) {
324- $ this ->info ('wp-cli installed successfully. ' );
325- } else {
326- $ this ->error ('Failed to move wp-cli.phar to /usr/local/bin/wp. ' );
327- $ this ->line ($ moveProcess ->getErrorOutput ());
326+ if (! is_dir ($ targetDir ) && ! mkdir ($ targetDir , 0755 , true ) && ! is_dir ($ targetDir )) {
327+ $ this ->error ('Failed to create directory ' .$ targetDir );
328+ exit (1 );
329+ }
330+
331+ $ targetPath = $ targetDir .'/wp ' ;
332+
333+ $ this ->info ('Moving wp-cli.phar to ' .$ targetPath .'... ' );
334+
335+ if (! @rename (base_path ('wp-cli.phar ' ), $ targetPath )) {
336+ $ this ->error ('Failed to move wp-cli.phar to ' .$ targetPath );
337+ exit (1 );
338+ }
339+
340+ if (! @chmod ($ targetPath , 0755 )) {
341+ $ this ->error ('Failed to make ' .$ targetPath .' executable. ' );
328342 exit (1 );
329343 }
344+
345+ $ this ->wpCliPath = $ targetPath ;
346+
347+ $ this ->info ('wp-cli installed successfully. ' );
348+
349+ $ pathDirs = explode (PATH_SEPARATOR , (string ) getenv ('PATH ' ));
350+
351+ if (! in_array ($ targetDir , $ pathDirs , true )) {
352+ $ this ->warn ('Ensure ' .$ targetDir .' is in your PATH to use the "wp" command. ' );
353+ }
330354 }
331355 }
332356
@@ -353,7 +377,7 @@ public function wpInstall(): void
353377 warning ('Please make sure to save this password as it will not be shown again. ' );
354378
355379 $ command = [
356- ' wp ' ,
380+ $ this -> wpCliPath ,
357381 'core ' ,
358382 'install ' ,
359383 '--url= ' .$ siteUrl ,
@@ -390,7 +414,7 @@ protected function installAndActivateDefaultTheme(string $fullWpPath): void
390414 $ this ->info ('Ensuring a default theme is installed and activated... ' );
391415
392416 $ checkThemeProcess = new Process ([
393- ' wp ' ,
417+ $ this -> wpCliPath ,
394418 'theme ' ,
395419 'is-installed ' ,
396420 'twentytwentyfour ' ,
@@ -401,7 +425,7 @@ protected function installAndActivateDefaultTheme(string $fullWpPath): void
401425 $ this ->info ('Default theme twentytwentyfour is not installed. Installing it now... ' );
402426
403427 $ installThemeProcess = new Process ([
404- ' wp ' ,
428+ $ this -> wpCliPath ,
405429 'theme ' ,
406430 'install ' ,
407431 'twentytwentyfour ' ,
@@ -454,7 +478,7 @@ public function pressPluginInstall(): void
454478 File::copyDirectory ($ pluginSource , $ pluginDestination );
455479
456480 info ('Activating the Moox Press plugin... ' );
457- $ activateCommand = [' wp ' , 'plugin ' , 'activate ' , 'moox-press ' ];
481+ $ activateCommand = [$ this -> wpCliPath , 'plugin ' , 'activate ' , 'moox-press ' ];
458482 $ process = new Process ($ activateCommand , $ fullWpPath );
459483 $ process ->setTimeout (null );
460484 $ process ->run ();
0 commit comments