Skip to content

Commit

Permalink
Merge pull request #458 from matiasbenedetto/add/wp-cli-integration
Browse files Browse the repository at this point in the history
Add wp-cli integration
  • Loading branch information
matiasbenedetto authored Sep 17, 2024
2 parents 22226a0 + 5b520e1 commit 0b2c2a3
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 0 deletions.
48 changes: 48 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 0,
"runtimeArgs": [
"-dxdebug.start_with_request=yes"
],
"env": {
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
},
{
"name": "Launch Built-in web server",
"type": "php",
"request": "launch",
"runtimeArgs": [
"-dxdebug.mode=debug",
"-dxdebug.start_with_request=yes",
"-S",
"localhost:0"
],
"program": "",
"cwd": "${workspaceRoot}",
"port": 9003,
"serverReadyAction": {
"pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
"uriFormat": "http://localhost:%s",
"action": "openExternally"
}
}
]
}
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,25 @@ add_filter( 'tc_skip_development_directories', '__return_true' );

To add more directories to the paths where other files are excluded then add them to the array through the `tc_common_dev_directories` filter.

### Usage with wp-cli

To use with [wp-cli](https://wp-cli.org/), ensure the theme check plugin is active and `wp-cli` is installed. The `theme-check` subcommand is added to `wp-cli` and can be used as follows:

`wp theme-check run [<theme>] [--format=<format>]`

On success, the command returns a formatted table of results from the theme check plugin.

#### Options
| Option | Accepts | Required | Default
| -- | -- | -- | -- |
| `theme` | The slug of the theme to check | No | Current theme slug
| `format` | `cli` or `json` | No | `cli`

#### Examples
`wp theme-check run`
`wp theme-check run twentytwentyfour`
`wp theme-check run --format=json`
`wp theme-check run twentytwentyfour --format=json`

## Contributors
Otto42, pross, The theme review team
4 changes: 4 additions & 0 deletions theme-check.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class ThemeCheckMain {
function __construct() {
add_action( 'admin_init', array( $this, 'tc_i18n' ) );
add_action( 'admin_menu', array( $this, 'themecheck_add_page' ) );

if ( defined( 'WP_CLI' ) && WP_CLI ) {
include 'wp-cli/class-theme-check-cli.php';
}
}

function tc_i18n() {
Expand Down
120 changes: 120 additions & 0 deletions wp-cli/class-theme-check-cli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

if ( defined( 'WP_CLI' ) && WP_CLI ) {
$parent_dir = dirname( __DIR__ );
require_once $parent_dir . '/checkbase.php';
require_once $parent_dir . '/main.php';
WP_CLI::add_command( 'theme-check', 'Theme_Check_Command' );
}
/**
* Run a theme check on the specified theme or the current theme.
*/
class Theme_Check_Command extends WP_CLI_Command {
/**
* Run a theme check on the specified theme or the current theme.
*
* ## OPTIONS
* [<theme>]
* : The slug of the theme to check. If not provided, checks the current theme.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - json
* ---
*
* ## EXAMPLES
* # Check the current active theme
* wp theme-check run
*
* # Check a specific theme
* wp theme-check run twentytwentyfour
*
* # Check the current active theme and output results as JSON
* wp theme-check run --format=json
*
* # Check a specific theme and output results as JSON
* wp theme-check run twentytwentyfour --format=json
*
* @param array $args Indexed array of positional arguments.
* @param array $assoc_args Associative array of options.
* @return void
*/
public function run( $args, $assoc_args ) {
$format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'table' );

// Get the current theme
$current_theme = wp_get_theme();
$current_theme_slug = $current_theme->get_stylesheet();

// Use the provided theme slug if available, otherwise use the current theme
$check_theme_slug = ! empty( $args[0] ) ? $args[0] : $current_theme_slug;

// Get the theme
$theme = wp_get_theme( $check_theme_slug );

if ( ! $theme->exists() ) {
WP_CLI::error( "Theme '{$check_theme_slug}' not found." );
}

// Run the checks
$success = run_themechecks_against_theme( $theme, $check_theme_slug );
$processed_messages = $this->process_themecheck_messages();

WP_CLI\Utils\format_items( $format, $processed_messages, array( 'type', 'value' ) );

// Set the exit code based on $success
WP_CLI::halt( $success ? 0 : 1 );
}

/**
* Process theme check messages.
*
* @return array Processed messages.
*/
private function process_themecheck_messages() {
global $themechecks;
$messages = array();

foreach ( $themechecks as $check ) {
if ( $check instanceof themecheck ) {
$error = $check->getError();
$error = (array) $error;
if ( ! empty( $error ) ) {
$messages = array_merge( $messages, $error );
}
}
}

$processed_messages = array_map(
function( $message ) {
if ( preg_match( '/<span[^>]*>(.*?)<\/span>(.*)/', $message, $matches ) ) {
$key = $matches[1];
$value = $matches[2];
} else {
$key = '';
$value = $message;
}

$key = wp_strip_all_tags( $key );
$key = html_entity_decode( $key, ENT_QUOTES, 'UTF-8' );
$key = rtrim( $key, ':' );

$value = wp_strip_all_tags( $value );
$value = html_entity_decode( $value, ENT_QUOTES, 'UTF-8' );
$value = ltrim( $value, ': ' );

return array(
'type' => trim( $key ),
'value' => trim( $value ),
);
},
$messages
);

return $processed_messages;
}
}

0 comments on commit 0b2c2a3

Please sign in to comment.