AlkantarClanX12
| Current Path : /www/capitalgmcbuickregina_830/public/wp-content/plugins/leadbox/cli/ |
| Current File : /www/capitalgmcbuickregina_830/public/wp-content/plugins/leadbox/cli/lead-log-command.php |
<?php if (!defined('ABSPATH')) exit;
/**
* LBT-1468 — WP-CLI command to read the lead-dispatch text log from the terminal
* (handy for the tester during the `both` parity window, no DB/UI access needed).
*
* wp leadbox lead-log tail
* wp leadbox lead-log tail --lines=200 --failures
*
* Registered in Leadbox.php under `if (defined('WP_CLI') && WP_CLI)`.
*/
final class LB_CLI_Lead_Log_Command
{
/**
* Prints the tail of the lead-dispatch log.
*
* ## OPTIONS
*
* [--lines=<n>]
* : How many trailing lines to show. Default 50.
*
* [--failures]
* : Only show failed dispatches (FAIL lines).
*
* ## EXAMPLES
*
* wp leadbox lead-log tail
* wp leadbox lead-log tail --lines=200 --failures
*
* @when after_wp_load
*/
public function tail($args, $assoc_args)
{
$lines = isset($assoc_args['lines']) ? max(1, (int) $assoc_args['lines']) : 50;
$only_fail = isset($assoc_args['failures']);
$default_path = (defined('WP_CONTENT_DIR') ? WP_CONTENT_DIR : '.') . '/' . LB_Lead_Dispatch_Log::FILENAME;
$path = apply_filters('leadbox_dispatch_log_path', $default_path);
if (!file_exists($path)) {
WP_CLI::warning('No dispatch log yet at: ' . $path);
WP_CLI::log('It is written only when lead_intake_target is "both" or "leadbox_os".');
return;
}
$all = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($all === false) {
WP_CLI::error('Could not read the dispatch log at: ' . $path);
}
if ($only_fail) {
$all = array_values(array_filter($all, function ($line) {
return strpos($line, '] FAIL ') !== false;
}));
}
$slice = array_slice($all, -$lines);
foreach ($slice as $line) {
WP_CLI::log($line);
}
WP_CLI::log(sprintf('— %d line(s)%s from %s', count($slice), $only_fail ? ' (failures only)' : '', $path));
}
}