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
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
[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.
33 changes: 23 additions & 10 deletions boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) {
Expand Down
43 changes: 43 additions & 0 deletions src/Controller/ExampleWpCliController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Example code for adding WP Cli commands in your site.
*
* @package MyApp\Core\Controller
*/
namespace MyApp\Core\Controller;

use WPMVC\Core\Controller;
use WP_CLI;

/**
* Example class for adding WP CLI commands.
*/
class ExampleWpCliController extends Controller {

/**
* Setup your WP cli commands
*
* @return void
*/
public function set_up() {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
// Register the command properly with the 'clisample' namespace
WP_CLI::add_command( 'clisample get-user-count', array( $this, 'get_row_count' ) );
}
}

/**
* Sample function to get the row count of the wp_users table.
*
* @return void
*/
public function get_row_count() {
global $wpdb;
$result = $wpdb->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 );
}
}
17 changes: 17 additions & 0 deletions src/Function/WP_CLI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Adds WP CLI helper functions.
*
* @package MyApp\Core
*/

namespace WPMVC\Core;

/**
* Returns if WP CLI is loaded and available.
*
* @return bool
*/
function is_wp_cli(): bool {
return defined( 'WP_CLI' ) && WP_CLI && class_exists( 'WP_CLI' );
}