Skip to content

Commit

Permalink
Fixes uncaught exceptions with WP-CLI obsolete versions - see #36
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Lannoy committed Oct 7, 2022
1 parent 3b1d321 commit 6ac91e0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ All notable changes to **DecaLog** are documented in this *changelog*.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and **DecaLog** adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.6.1] - Not Yet Released

### Changed
- [WP-CLI] A warning is now triggered when an outdated version of WP-CLI is detected.

### Fixed
- [WP-CLI] With outdated versions of WP-CLI, a PHP uncaught exception is thrown (thanks to [Jan Thiel](https://github.com/JanThiel)).

## [3.6.0] - 2022-10-06

### Added
Expand Down
23 changes: 14 additions & 9 deletions includes/plugin/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,24 @@ public function late_initialize() {
*/
public function wpcli_initialize() {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
$class = str_replace( 'WP_CLI\Loggers\\', 'Decalog\Listener\WP_CLI\\', get_class( \WP_CLI::get_logger() ) );
if ( class_exists( $class ) ) {
try {
$reflection = new \ReflectionClass( $class );
$instance = $reflection->newInstance();
\WP_CLI::set_logger( $instance );
} catch ( \Exception $e ) {
if ( method_exists( '\WP_CLI', 'set_logger') && method_exists( '\WP_CLI', 'get_logger') ) {
$class = str_replace( 'WP_CLI\Loggers\\', 'Decalog\Listener\WP_CLI\\', get_class( \WP_CLI::get_logger() ) );
if ( class_exists( $class ) ) {
try {
$reflection = new \ReflectionClass( $class );
$instance = $reflection->newInstance();
\WP_CLI::set_logger( $instance );
} catch ( \Exception $e ) {
$logger = Log::bootstrap( 'plugin', DECALOG_PRODUCT_SHORTNAME, DECALOG_VERSION );
$logger->critical( sprintf( 'Unable to instanciate `%s` class. DecaLog will not log following WP-CLI events.', $class ) );
}
} else {
$logger = Log::bootstrap( 'plugin', DECALOG_PRODUCT_SHORTNAME, DECALOG_VERSION );
$logger->critical( sprintf( 'Unable to instanciate `%s` class. DecaLog will not log following WP-CLI events.', $class ) );
$logger->critical( sprintf( 'Unable to find `%s` class. DecaLog will not log following WP-CLI events.', $class ) );
}
} else {
$logger = Log::bootstrap( 'plugin', DECALOG_PRODUCT_SHORTNAME, DECALOG_VERSION );
$logger->critical( sprintf( 'Unable to find `%s` class. DecaLog will not log following WP-CLI events.', $class ) );
$logger->warning( 'WP-CLI is outdated: DecaLog will not be able to report events triggered in interactive command-line session. Please, update WP-CLI!' );
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion init.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
define( 'DECALOG_PRODUCT_SHORTNAME', 'DecaLog' );
define( 'DECALOG_PRODUCT_ABBREVIATION', 'decalog' );
define( 'DECALOG_SLUG', 'decalog' );
define( 'DECALOG_VERSION', '3.6.0' );
define( 'DECALOG_VERSION', '3.6.1-dev0' );
define( 'DECALOG_API_VERSION', '3' );
define( 'DECALOG_MONOLOG_VERSION', '2.8.0' );
define( 'DECALOG_CODENAME', '"-"' );
Expand Down

0 comments on commit 6ac91e0

Please sign in to comment.