1

To do this, I copied the repository WP CLI into Root WordPress

And I wrote the following PHP code to encode it and encountered the following errors

define('WP_CLI_ROOT', ABSPATH . 'wp-cli');
require_once WP_CLI_ROOT . '/php/wp-cli.php';
Warning: include_once(D:\Web\Sites\jupiterx/wp-cli/vendor/rmccue/requests/library/Requests.php): failed to open stream: No such file or directory in D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Bootstrap\IncludeFrameworkAutoloader.php on line 48

Warning: include_once(): Failed opening 'D:\Web\Sites\jupiterx/wp-cli/vendor/rmccue/requests/library/Requests.php' for inclusion (include_path='C:\xampp\php\PEAR') in D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Bootstrap\IncludeFrameworkAutoloader.php on line 48

Warning: include_once(D:\Web\Sites\jupiterx/wp-cli/vendor/wp-cli/mustangostang-spyc/Spyc.php): failed to open stream: No such file or directory in D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Bootstrap\IncludeFrameworkAutoloader.php on line 49

Warning: include_once(): Failed opening 'D:\Web\Sites\jupiterx/wp-cli/vendor/wp-cli/mustangostang-spyc/Spyc.php' for inclusion (include_path='C:\xampp\php\PEAR') in D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Bootstrap\IncludeFrameworkAutoloader.php on line 49

Warning: array_slice() expects parameter 1 to be array, null given in D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Runner.php on line 905

Warning: Invalid argument supplied for foreach() in D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Configurator.php on line 138

Warning: implode(): Invalid arguments passed in D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Runner.php on line 1024

Fatal error: Uncaught Error: Class 'cli\Colors' not found in D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Loggers\Regular.php:70 Stack trace: #0 D:\Web\Sites\jupiterx\wp-cli\php\class-wp-cli.php(881): WP_CLI\Loggers\Regular->error_multi_line(Array) #1 D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Runner.php(248): WP_CLI::error_multi_line(Array) #2 D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Runner.php(1083): WP_CLI\Runner::set_wp_root('D:\\Web\\Sites\\ju...') #3 D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Bootstrap\LaunchRunner.php(23): WP_CLI\Runner->start() #4 D:\Web\Sites\jupiterx\wp-cli\php\bootstrap.php(74): WP_CLI\Bootstrap\LaunchRunner->process(Object(WP_CLI\Bootstrap\BootstrapState)) #5 D:\Web\Sites\jupiterx\wp-cli\php\wp-cli.php(27): WP_CLI\bootstrap() #6 D:\Web\Sites\jupiterx\wp-content\plugins\tabanShahrSiteSaz\Plugin.php(72): require_once('D:\\Web\\Sites\\ju...') #7 D:\Web\Sites\jupiterx\wp-content\plugins\tabanShahrSiteSaz\app\core\SingletonPattern.php(26): TabanShahrSiteSaz\Plugin->includes() #8 D:\Web\Sites\jupiterx\wp-conten in D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Loggers\Regular.php on line 70

Thanks in advance for your help

  • Have you tried quoting the backslashes? E.g., where you have "D:\Web\Sites|jupiterx/wc-cli/vendor/" etc., change it to: "D:\\Web\\Sites\\jupiterx/wc-cli/vendor/" etc. Note that the forward slash (/) is OK, only the back-slash (\) needs quoting (\\). Your define for "ABSPATH" may be the culprit here. – UncaAlby Dec 11 '19 at 17:23
  • Second look -- there's a scroll-bar! Have you confirmed that the include files being requested actually do exist at those locations? "D:\Web\Sites\jupiterx\wp-cli\php\WP_CLI\Bootstrap\IncludeFrameworkAutoloader.php" needs to be exactly there spelled exactly in that manner. – UncaAlby Dec 11 '19 at 17:32
  • Thank you for your reply, Dear @UncaAlby, yes I tested the backslashes and unfortunately encountered this error. I'm not convinced of the method I used, in fact, my question is how to use WP CLI in PHP script – Mohammad Elmi Dec 11 '19 at 18:04
  • https://github.com/wp-cli/wp-cli/issues/1924 – leymannx Dec 11 '19 at 18:16
  • https://github.com/leymannx/wp-cli-launcher/blob/master/bin/wp-cli.php#L102 – leymannx Dec 11 '19 at 18:20
  • I think you need to include this boot-fs file if you really want to do it that way. – leymannx Dec 11 '19 at 18:36
  • Thank you for your reply, Dear @leymannx, I read this but did not get it The same error is displayed with include boot-fs.php, – Mohammad Elmi Dec 11 '19 at 19:49

1 Answers1

5

WP-CLI can only be run correctly under the PHP cli SAPI, not under a web server API like cgi, apache or similar. This has some technical reasons, but it is also directly related to your server's security.

WP-CLI is a console application

The first technical reason is that WP-CLI is a console application, and it assumes access to the console input (STDIN) and output (STDOUT) streams. Within the web server context that WordPress runs under, these don't exist. The "input" for a webserver is the URL and payload of a web request, and the output is the HTML or JSON that is sent back to the browser.

WP-CLI is a wrapper around WordPress

The second technical reason is that WP-CLI is basically a "hack" that "wraps" an entire WordPress installation and executes it as a subroutine within its own process. If you would try to use WordPress to launch WP-CLI, you'd have a chicken & egg problem, or a software interpretation of "Inception".

WP-CLI is a sysadmin tool

The reason why this is also directly relevant in terms of security is that WP-CLI is, in the broadest sense, a sysadmin tool. It will directly create, modify, delete files, do direct requests against the database, and more. All of its operations can bypass any security measures that might have been put in place at the application level by WordPress and its plugins. Making WP-CLI accessible to the public-facing frontend and letting it be triggered via web requests would turn it into a critical security vulnerability. No server-side administration tool should be exposed to the frontend in whatever way.

Hopefully, this explains why what you want to do cannot be done and why you would not want to if you could.