1212use Psr \Log \LogLevel ;
1313use WP_CLI ;
1414
15+ use function Mantle \Support \Helpers \collect ;
16+
1517// phpcs:disable WordPressVIPMinimum.Classes.RestrictedExtendClasses.wp_cli
1618
1719if ( ! class_exists ( 'WP_CLI_Command ' ) ) {
2426 * Cannot extend `WPCOM_VIP_CLI_Command` since this plugin can run
2527 * outside the context of a VIP site.
2628 */
27- class CLI extends \WP_CLI_Command {
29+ final class CLI {
30+ /**
31+ * Constructor.
32+ */
33+ public function __construct () {
34+ if ( ! defined ( 'WP_CLI ' ) || ! WP_CLI ) {
35+ return ;
36+ }
37+
38+ WP_CLI ::add_command ( 'ai-logger cleanup ' , [ $ this , 'cleanup ' ] );
39+ WP_CLI ::add_command ( 'ai-logger display-object ' , [ $ this , 'display_object ' ] );
40+ WP_CLI ::add_command ( 'ai-logger display ' , [ $ this , 'display ' ] );
41+ WP_CLI ::add_command ( 'ai-logger generate-for-object ' , [ $ this , 'generate_for_object ' ] );
42+ WP_CLI ::add_command ( 'ai-logger generate ' , [ $ this , 'generate ' ] );
43+ }
44+
45+ /**
46+ * Display the site-wide log.
47+ *
48+ * @synopsis [--count=<value>] [--offset=<value>] [--log-context=<value>] [--level=<value>] [--format=<value>]
49+ *
50+ * @param array $args Arguments for the command.
51+ * @param array $assoc_args Associated flags for the command.
52+ */
53+ public function display ( $ args , $ assoc_args ): int {
54+ $ assoc_args = \wp_parse_args (
55+ $ assoc_args ,
56+ [
57+ 'count ' => 50 ,
58+ 'offset ' => 0 ,
59+ 'format ' => 'table ' ,
60+ ]
61+ );
62+
63+ $ tax_query = [
64+ 'relation ' => 'AND ' ,
65+ ];
66+
67+ if ( ! empty ( $ assoc_args ['log-context ' ] ) ) {
68+ $ tax_query [] = [
69+ 'taxonomy ' => Post_Handler::TAXONOMY_LOG_CONTEXT ,
70+ 'field ' => 'slug ' ,
71+ 'terms ' => explode ( ', ' , $ assoc_args ['log-context ' ] ),
72+ ];
73+ }
74+
75+ if ( ! empty ( $ assoc_args ['level ' ] ) ) {
76+ $ tax_query [] = [
77+ 'taxonomy ' => Post_Handler::TAXONOMY_LOG_LEVEL ,
78+ 'field ' => 'slug ' ,
79+ 'terms ' => explode ( ', ' , $ assoc_args ['level ' ] ),
80+ ];
81+ }
82+
83+ $ logs = get_posts ( [ // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.get_posts_get_posts
84+ 'fields ' => 'ids ' ,
85+ 'offset ' => (int ) $ assoc_args ['offset ' ],
86+ 'order ' => 'DESC ' ,
87+ 'orderby ' => 'date ' ,
88+ 'post_type ' => Post_Handler::POST_TYPE ,
89+ 'posts_per_page ' => (int ) $ assoc_args ['count ' ],
90+ 'suppress_filters ' => false ,
91+ 'tax_query ' => $ tax_query , // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
92+ ] );
93+
94+ if ( empty ( $ logs ) ) {
95+ WP_CLI ::line ( 'No logs found. ' );
96+
97+ return 0 ;
98+ }
99+
100+ $ logs = collect ( $ logs )->map ( function ( int $ log_id ): ?array {
101+ $ record = get_post_meta ( $ log_id , '_logger_record ' , true );
102+
103+ if ( ! $ record ) {
104+ return null ;
105+ }
106+
107+ return [
108+ 'context ' => $ record ['context ' ]['context ' ] ?? '' ,
109+ 'message ' => $ record ['message ' ] ?? '' ,
110+ 'level_name ' => $ record ['level_name ' ] ?? '' ,
111+ 'datetime ' => isset ( $ record ['datetime ' ] ) ? $ record ['datetime ' ]->setTimezone ( wp_timezone () )->format ( 'm/d/Y H:i:s ' ) : null ,
112+ ];
113+ } )->filter ()->values ()->all ();
114+
115+ WP_CLI \Utils \format_items (
116+ $ assoc_args ['format ' ],
117+ array_map (
118+ fn ( array $ log ) => [
119+ 'level ' => $ log ['level_name ' ],
120+ 'message ' => $ log ['message ' ],
121+ 'context ' => $ log ['context ' ],
122+ 'timestamp ' => $ log ['datetime ' ],
123+ ],
124+ $ logs
125+ ),
126+ [
127+ 'level ' ,
128+ 'message ' ,
129+ 'context ' ,
130+ 'timestamp ' ,
131+ ]
132+ );
133+
134+ return 0 ;
135+ }
136+
28137 /**
29138 * Display the log for a post.
30139 *
@@ -42,7 +151,7 @@ class CLI extends \WP_CLI_Command {
42151 * @param array $args Arguments for the command.
43152 * @param array $assoc_args Associated flags for the command.
44153 */
45- public function display ( $ args , $ assoc_args ) {
154+ public function display_object ( $ args , $ assoc_args ) {
46155 [ $ object_type , $ object_id ] = $ args ;
47156
48157 $ assoc_args = \wp_parse_args (
0 commit comments