diff --git a/README.md b/README.md index 85e0262..6b3f803 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,24 @@ The arguments used in the `get_taxonomy_args()` method are the same as the argum As per the "Registering a controller" section above, you must include and initialise your controller in the `boot.php` file. +## Creating custom WP CLI commands + +If you need some functionality that needs to be in the WP CLI, here are the steps to add it. + +### Step 1 - Add wp cli composer dependencies + +In the composer.json file inside `require`, add `"wp-cli/wp-cli": "dev-main"` then run `composer update`. + +### Step 2 - Include wp cli in the boot.php + +In your boot.php +- Add/Uncomment `use function MyApp\Core\is_wp_cli;` +- Add your WP CLI Controller at the top of the file file eg: `MyApp\Core\Controller\ExampleWpCliController;` +- Inside the conditional `if ( php_sapi_name() == 'cli' ) { } `, add the wp cli instances. This will make sure that those functions will run only within WP CLI. + +### Step 3 - +- Add your WP cli commands, see `ExampleWpCliController.php` + ## Contributing -[WPMVC](https://github.com/TheCodeCompany/wpmvc) is maintained by [The Code Company](https://thecode.co/), while we appreciate feedback and will endeavour to action requests for features/bug fixes this repository is not open to outside contribution at this time. You are, however, free to fork and use WPMVC in any way you see fit as per the ISC license. \ No newline at end of file +[WPMVC](https://github.com/TheCodeCompany/wpmvc) is maintained by [The Code Company](https://thecode.co/), while we appreciate feedback and will endeavour to action requests for features/bug fixes this repository is not open to outside contribution at this time. You are, however, free to fork and use WPMVC in any way you see fit as per the ISC license. diff --git a/boot.php b/boot.php index 0746b09..fc68258 100644 --- a/boot.php +++ b/boot.php @@ -13,22 +13,35 @@ use MyApp\Core\Controller\RegisterPostType\RegisterMovieController; use MyApp\Core\Controller\RegisterTaxonomy\RegisterGenreController; +// Uncomment this to enable WP CLI commands. Don't forget to include wp cli dependency in composer.json. +// use MyApp\Core\Controller\ExampleWpCliController; +// use function MyApp\Core\is_wp_cli; + if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) { require_once __DIR__ . '/vendor/autoload.php'; - define( 'MyApp\APP_NAME', 'example-corp' ); + define( 'MyApp\APP_NAME', 'tcc-importer' ); + + // Initialize controllers array + $controllers = array( + // Custom Post types. + new RegisterMovieController(), + + // Custom Taxonomies. + new RegisterGenreController(), + ); + + // Add the controllers that you need to run in WP CLI. This will make sure that the controllers are only loaded when WP CLI is available. + if ( php_sapi_name() == 'cli' ) { + // Uncomment this to enable WP CLI commands. Don't forget to include wp cli dependency in composer.json. + // $controllers[] = new ExampleWpCliController(); + } + // Instantiate the Application with the controllers array $my_app = new Application( APP_NAME, // The application name / slug. - __DIR__, // The application root directory. - [ // The application controllers - - // Custom Post types. - new RegisterMovieController(), - - // Custom Taxonomies. - new RegisterGenreController(), - ] + __DIR__, // The application root directory. + $controllers // The controllers array ); } elseif ( function_exists( 'is_local' ) && is_local() ) { diff --git a/src/Controller/ExampleWpCliController.php b/src/Controller/ExampleWpCliController.php new file mode 100644 index 0000000..32cb9ba --- /dev/null +++ b/src/Controller/ExampleWpCliController.php @@ -0,0 +1,43 @@ +get_results( 'SELECT COUNT(*) AS row_count FROM wp_users' ); + if ( ! $result ) { + WP_CLI::error( 'Failed to execute query.' ); + } + + WP_CLI::success( 'Row count: ' . $result[0]->row_count ); + } +} diff --git a/src/Function/WP_CLI.php b/src/Function/WP_CLI.php new file mode 100644 index 0000000..b68df1b --- /dev/null +++ b/src/Function/WP_CLI.php @@ -0,0 +1,17 @@ +