From 36c3870633ef5bcbb859139db0c694dd12e682b3 Mon Sep 17 00:00:00 2001 From: carl-alberto Date: Thu, 7 Nov 2024 17:41:15 +0800 Subject: [PATCH 1/4] Added sample wp cli controller --- src/Controller/ExampleWpCliController.php | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/Controller/ExampleWpCliController.php 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 ); + } +} From 90651378fc5ba266c945857f2c208c251d86a3f8 Mon Sep 17 00:00:00 2001 From: carl-alberto Date: Thu, 7 Nov 2024 17:41:55 +0800 Subject: [PATCH 2/4] Added provision for wp cli in boot.php --- boot.php | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) 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() ) { From 22fd7beaa19294e0911923152b2fc12681a3c246 Mon Sep 17 00:00:00 2001 From: carl-alberto Date: Thu, 7 Nov 2024 17:44:10 +0800 Subject: [PATCH 3/4] Added wp cli helper function --- src/Function/WP_CLI.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/Function/WP_CLI.php 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 @@ + Date: Thu, 7 Nov 2024 17:53:34 +0800 Subject: [PATCH 4/4] Added instructions on how to add wp cli commands --- README.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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.