The WP_CLI
class has a runcommand
method that launches a new child process to run a specified WP-CLI command. According to the docs, you can use it like this:
$options = array(
'return' => true, // Return 'STDOUT'; use 'all' for full object.
'parse' => 'json', // Parse captured STDOUT to JSON array.
'launch' => false, // Reuse the current process.
'exit_error' => true, // Halt script execution on error.
);
$plugins = WP_CLI::runcommand( 'plugin list --format=json', $options );