Skip to content
Open
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
5 changes: 5 additions & 0 deletions dokan-class.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ public function init_classes() {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
$ajax_services = $this->get_container()->get( 'ajax-service' );
}

// Boot the WP-CLI command registry only when running under WP-CLI.
if ( defined( 'WP_CLI' ) && WP_CLI ) {
$cli_services = $this->get_container()->get( 'cli-service' );
}
}

/**
Expand Down
46 changes: 46 additions & 0 deletions includes/CLI/Manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace WeDevs\Dokan\CLI;

/**
* Dokan WP-CLI command registry.
*
* Owns the `wp dokan` command namespace and exposes the `dokan_cli_commands`
* filter so Dokan and its extensions (e.g. Dokan Pro) can register commands
* under the same namespace from a single place.
*
* @since DOKAN_SINCE
*/
class Manager {

/**
* Bootstraps the CLI command registry.
*
* @since DOKAN_SINCE
*/
public function __construct() {
// Only register commands while running under WP-CLI.
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
return;
}

// Register late so every extension has contributed its commands to the filter first.
add_action( 'init', [ $this, 'register_commands' ], 99 );
}

/**
* Registers every Dokan WP-CLI command collected from the filter.
*
* @since DOKAN_SINCE
*
* @return void
*/
public function register_commands() {
// Extensions add their commands as [ 'dokan <command>' => HandlerClass::class ].
$commands = (array) apply_filters( 'dokan_cli_commands', [] );

foreach ( $commands as $name => $handler ) {
\WP_CLI::add_command( $name, $handler );
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
44 changes: 44 additions & 0 deletions includes/DependencyManagement/Providers/CliServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace WeDevs\Dokan\DependencyManagement\Providers;

use WeDevs\Dokan\DependencyManagement\BaseServiceProvider;
use WeDevs\Dokan\CLI\Manager;

/**
* Class CliServiceProvider
*
* Registers the WP-CLI command registry with the dependency container and tags
* it so it is only resolved while running under WP-CLI.
*
* @since DOKAN_SINCE
*/
class CliServiceProvider extends BaseServiceProvider {
/**
* Tags used to identify the service in the container.
*
* @var array
*/
protected $tags = [ 'cli-service' ];

/**
* List of services provided by this provider.
*
* @var array
*/
protected $services = [
Manager::class,
];

/**
* Register the CLI Manager in the container and add the corresponding tags.
*
* @return void
*/
public function register(): void {
$this->add_tags(
$this->getContainer()->addShared( Manager::class ),
$this->tags
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function boot(): void {
$this->getContainer()->addServiceProvider( new AdminSetupGuideServiceProvider() );
$this->getContainer()->addServiceProvider( new ModelServiceProvider() );
$this->getContainer()->addServiceProvider( new CaptchaServiceProvider() );
$this->getContainer()->addServiceProvider( new CliServiceProvider() );
}

/**
Expand Down
Loading