Skip to content

Instantly share code, notes, and snippets.

@kagg-design
Created November 8, 2023 11:29
Show Gist options
  • Save kagg-design/c37234288afc315640678f941cfa5d89 to your computer and use it in GitHub Desktop.
Save kagg-design/c37234288afc315640678f941cfa5d89 to your computer and use it in GitHub Desktop.
<?php
/**
* Error-handler to be used as a mu-plugin.
*
* @package kagg/compatibility
*/
// phpcs:disable Generic.Commenting.DocComment.MissingShort
/** @noinspection PhpIllegalPsrClassPathInspection */
/** @noinspection AutoloadingIssuesInspection */
// phpcs:enable Generic.Commenting.DocComment.MissingShort
namespace KAGG\Compatibility;
/**
* Class ErrorHandler
*/
class ErrorHandler {
/**
* Directories where can deprecation error occurs.
*
* @var string[]
*/
private $dirs;
/**
* Init class.
*
* @return void
*/
public function init() {
$this->dirs = [
ABSPATH . WPINC . '/', // WordPress wp-includes.
ABSPATH . 'wp-admin/', // WordPress wp-admin.
'/vendor/rmccue/requests/', // Requests library used in WP-CLI.
'/vendor/woocommerce/action-scheduler/', // Action Scheduler.
'/plugins/woocommerce/', // WooCommerce.
'/plugins/backwpup/', // BackWPup.
'/plugins/business-reviews-bundle/', // Business review bundle.
'/plugins/cloudflare/', // Cloudflare.
'/plugins/easy-digital-downloads/', // Easy Digital Downloads.
'/plugins/google-site-kit/', // Google Site Kit.
'/plugins/gravityforms/', // Gravity Forms.
'/plugins/mailpoet/', // MailPoet.
'/plugins/seo-by-rank-math/', // Rank Math SEO.
'/plugins/sitepress-multilingual-cms/', // WPML.
'/plugins/wp-google-places-review-slider/', // Google places review slider.
'/plugins/wp-job-openings/', // Job openings.
'/plugins/wp-seo-multilingual/', // WPML SEO.
'/plugins/wp-super-cache/', // WP Super Cache.
'/themes/Divi/', // Divi Theme.
];
$this->dirs = array_map(
static function ( $dir ) {
return str_replace( DIRECTORY_SEPARATOR, '/', $dir );
},
$this->dirs
);
$this->init_hooks();
}
/**
* Init class hooks.
*
* @return void
*/
private function init_hooks() {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler
set_error_handler( [ $this, 'error_handler' ] );
add_action( 'admin_head', [ $this, 'admin_head' ] );
}
/**
* Error handler.
*
* @param int $level Error level.
* @param string $message Error message.
* @param string $file File produced an error.
* @param int $line Line number.
*
* @return bool
* @noinspection PhpUnusedParameterInspection
*/
public function error_handler( int $level, string $message, string $file, int $line ): bool {
if ( E_DEPRECATED !== $level ) {
// Use standard error handler.
return false;
}
$file = str_replace( DIRECTORY_SEPARATOR, '/', $file );
foreach ( $this->dirs as $dir ) {
if ( str_contains( $file, $dir ) ) {
// Suppress deprecated errors from this directory.
return true;
}
}
// Use standard error handler.
return false;
}
/**
* Clear error caused by xdebug with PHP 8.1.
* This error leads to adding .php-error class (2em margin-top) to the #adminmenuwrap.
*
* @return void
*/
public function admin_head() {
$error_get_last = error_get_last();
if ( ! isset( $error_get_last['file'] ) ) {
return;
}
if ( 'xdebug://debug-eval' === $error_get_last['file'] ) {
// phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.error_clear_lastFound
error_clear_last();
}
}
}
( new ErrorHandler() )->init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment