add a CLI command for dumping tracker data to terminal

This commit is contained in:
Rua Haszard 2021-05-31 14:55:29 +12:00
parent 84f8fcd016
commit bcadf9d264
3 changed files with 42 additions and 1 deletions

View File

@ -28,6 +28,7 @@ class WC_CLI {
require_once dirname( __FILE__ ) . '/cli/class-wc-cli-rest-command.php';
require_once dirname( __FILE__ ) . '/cli/class-wc-cli-tool-command.php';
require_once dirname( __FILE__ ) . '/cli/class-wc-cli-update-command.php';
require_once dirname( __FILE__ ) . '/cli/class-wc-cli-tracker-command.php';
}
/**
@ -37,6 +38,7 @@ class WC_CLI {
WP_CLI::add_hook( 'after_wp_load', 'WC_CLI_Runner::after_wp_load' );
WP_CLI::add_hook( 'after_wp_load', 'WC_CLI_Tool_Command::register_commands' );
WP_CLI::add_hook( 'after_wp_load', 'WC_CLI_Update_Command::register_commands' );
WP_CLI::add_hook( 'after_wp_load', 'WC_CLI_Tracker_Command::register_commands' );
}
}

View File

@ -113,7 +113,7 @@ class WC_Tracker {
*
* @return array
*/
private static function get_tracking_data() {
public static function get_tracking_data() {
$data = array();
// General site info.

View File

@ -0,0 +1,39 @@
<?php
/**
* WC_CLI_Tracker_Command class file.
*
* @package WooCommerce\CLI
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Allows access to tracker snapshot for transparency and debugging.
*
* @since x.x.x
* @package WooCommerce
*/
class WC_CLI_Tracker_Command {
/**
* Registers a command for showing WooCommerce Tracker snapshot data.
*/
public static function register_commands() {
WP_CLI::add_command( 'wc tracker-snapshot', array( 'WC_CLI_Tracker_Command', 'show_tracker_snapshot' ) );
}
/**
* Dump tracker snapshot data to screen.
*/
public static function show_tracker_snapshot() {
$snapshot_data = WC_Tracker::get_tracking_data();
// Using print_r as a convenient way to dump the snapshot data to screen in a readable form.
WP_CLI::log( print_r( $snapshot_data, true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
// Could improve this, e.g. flatten keys (join nested fields with `-`) and render as CSV or json.
WP_CLI::success( 'Printed tracker data.' );
}
}