Skip to content

Commit 2cb621f

Browse files
committed
Register WP-CLI payment command in cli_init
Extract WP-CLI command registration into a private cli_init() method and hook it to the 'cli_init' action using add_action( 'cli_init', $this->cli_init(... ) ). The 'pay payment status' command behavior is preserved but initialization is deferred to the proper CLI init hook for cleaner startup and better timing.
1 parent 3466dbd commit 2cb621f

1 file changed

Lines changed: 37 additions & 31 deletions

File tree

src/Payments/PaymentsModule.php

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,44 +59,50 @@ public function __construct( Plugin $plugin ) {
5959
// Payment Status Checker.
6060
$this->status_checker = new StatusChecker();
6161

62-
// CLI.
63-
if ( defined( 'WP_CLI' ) && WP_CLI ) {
64-
WP_CLI::add_command(
65-
'pay payment status',
66-
function ( $args ): void {
67-
foreach ( $args as $id ) {
68-
$payment = get_pronamic_payment( $id );
69-
70-
if ( null === $payment ) {
71-
WP_CLI::error(
72-
\sprintf(
73-
'Cannot find payment based on ID %s.',
74-
$id
75-
)
76-
);
77-
}
78-
79-
WP_CLI::log(
80-
\sprintf(
81-
'Check the status (current: %s) of payment with ID %s…',
82-
$payment->get_status(),
83-
$id
84-
)
85-
);
86-
87-
Plugin::update_payment( $payment, false );
62+
\add_action( 'cli_init', $this->cli_init( ... ) );
63+
}
8864

89-
WP_CLI::log(
65+
/**
66+
* CLI init.
67+
*
68+
* @return void
69+
*/
70+
private function cli_init() {
71+
WP_CLI::add_command(
72+
'pay payment status',
73+
function ( $args ): void {
74+
foreach ( $args as $id ) {
75+
$payment = get_pronamic_payment( $id );
76+
77+
if ( null === $payment ) {
78+
WP_CLI::error(
9079
\sprintf(
91-
'Checked the status (current: %s) of payment with ID %s.',
92-
$payment->get_status(),
80+
'Cannot find payment based on ID %s.',
9381
$id
9482
)
9583
);
9684
}
85+
86+
WP_CLI::log(
87+
\sprintf(
88+
'Check the status (current: %s) of payment with ID %s…',
89+
$payment->get_status(),
90+
$id
91+
)
92+
);
93+
94+
Plugin::update_payment( $payment, false );
95+
96+
WP_CLI::log(
97+
\sprintf(
98+
'Checked the status (current: %s) of payment with ID %s.',
99+
$payment->get_status(),
100+
$id
101+
)
102+
);
97103
}
98-
);
99-
}
104+
}
105+
);
100106
}
101107

102108
/**

0 commit comments

Comments
 (0)