* The error handler to suppress error messages from vendor directories.
// phpcs:ignore Generic.Commenting.DocComment.MissingShort
/** @noinspection PhpUndefinedClassInspection */
* Directories from where errors should be suppressed.
* Previous error handler.
private $previous_error_handler;
* Error levels to suppress.
* Whether the error handler is handling an error.
private $handling = false;
* @param array $dirs Directories from where errors should be suppressed.
* @param int $levels Error levels to suppress.
public function __construct( array $dirs = [], int $levels = 0 ) {
* @noinspection PhpUndefinedConstantInspection
if ( defined( 'WPFORMS_DISABLE_ERROR_HANDLER' ) && WPFORMS_DISABLE_ERROR_HANDLER ) {
WPFORMS_PLUGIN_DIR . 'vendor/',
WPFORMS_PLUGIN_DIR . 'vendor_prefixed/',
WP_PLUGIN_DIR . '/wpforms-activecampaign/vendor/',
WP_PLUGIN_DIR . '/wpforms-authorize-net/vendor/',
WP_PLUGIN_DIR . '/wpforms-aweber/deprecated/',
WP_PLUGIN_DIR . '/wpforms-aweber/vendor/',
WP_PLUGIN_DIR . '/wpforms-calculations/vendor/',
WP_PLUGIN_DIR . '/wpforms-campaign-monitor/vendor/',
WP_PLUGIN_DIR . '/wpforms-captcha/vendor/',
WP_PLUGIN_DIR . '/wpforms-conversational-forms/vendor/',
WP_PLUGIN_DIR . '/wpforms-convertkit/vendor/',
WP_PLUGIN_DIR . '/wpforms-convertkit/vendor_prefixed/',
WP_PLUGIN_DIR . '/wpforms-coupons/vendor/',
WP_PLUGIN_DIR . '/wpforms-drip/vendor/',
WP_PLUGIN_DIR . '/wpforms-e2e-helpers/vendor/',
WP_PLUGIN_DIR . '/wpforms-entry-automation/vendor/',
WP_PLUGIN_DIR . '/wpforms-form-abandonment/vendor/',
WP_PLUGIN_DIR . '/wpforms-form-locker/vendor/',
WP_PLUGIN_DIR . '/wpforms-form-pages/vendor/',
WP_PLUGIN_DIR . '/wpforms-geolocation/vendor/',
WP_PLUGIN_DIR . '/wpforms-getresponse/vendor/',
WP_PLUGIN_DIR . '/wpforms-google-sheets/vendor/',
WP_PLUGIN_DIR . '/wpforms-hubspot/vendor/',
WP_PLUGIN_DIR . '/wpforms-lead-forms/vendor/',
WP_PLUGIN_DIR . '/wpforms-mailchimp/vendor/',
WP_PLUGIN_DIR . '/wpforms-mailerlite/vendor/',
WP_PLUGIN_DIR . '/wpforms-offline-forms/vendor/',
WP_PLUGIN_DIR . '/wpforms-paypal-commerce/vendor/',
WP_PLUGIN_DIR . '/wpforms-paypal-standard/vendor/',
WP_PLUGIN_DIR . '/wpforms-post-submissions/vendor/',
WP_PLUGIN_DIR . '/wpforms-salesforce/vendor/',
WP_PLUGIN_DIR . '/wpforms-salesforce/vendor_prefixed/',
WP_PLUGIN_DIR . '/wpforms-save-resume/vendor/',
WP_PLUGIN_DIR . '/wpforms-sendinblue/vendor/',
WP_PLUGIN_DIR . '/wpforms-signatures/vendor/',
WP_PLUGIN_DIR . '/wpforms-square/vendor/', // Backward compatibility.
WP_PLUGIN_DIR . '/wpforms-stripe/vendor/',
WP_PLUGIN_DIR . '/wpforms-surveys-polls/vendor/',
WP_PLUGIN_DIR . '/wpforms-user-journey/vendor/',
WP_PLUGIN_DIR . '/wpforms-user-registration/vendor/',
WP_PLUGIN_DIR . '/wpforms-webhooks/vendor/',
WP_PLUGIN_DIR . '/wpforms-zapier/vendor/',
* Allow modifying the list of dirs to suppress messages from.
* @param array $dirs The list of dirs to suppress messages from.
$this->dirs = (array) apply_filters( 'wpforms_error_handler_dirs', $this->dirs );
* Allow modifying the levels of messages to suppress.
* @param int $levels Error levels of messages to suppress.
$this->levels = (int) apply_filters(
'wpforms_error_handler_levels',
E_WARNING | E_NOTICE | E_USER_WARNING | E_USER_NOTICE | E_DEPRECATED | E_USER_DEPRECATED
protected function hooks() {
if ( $this->dirs && $this->levels ) {
$this->set_error_handler();
// Some plugins destroy an error handler chain. Set the error handler again upon loading them.
add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ], 1000 );
// Suppress the _load_textdomain_just_in_time() notices related the WPForms for WP 6.7+.
if ( version_compare( $GLOBALS['wp_version'], '6.7', '>=' ) ) {
add_action( 'doing_it_wrong_run', [ $this,'action_doing_it_wrong_run' ], 0, 3 );
add_action( 'doing_it_wrong_run', [ $this,'action_doing_it_wrong_run' ], 20, 3 );
add_filter( 'doing_it_wrong_trigger_error', [ $this, 'filter_doing_it_wrong_trigger_error' ], 10, 4 );
* Set error handler and save original.
public function set_error_handler() {
// To chain error handlers, we must not specify the second argument and catch all errors in our handler.
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler
$this->previous_error_handler = set_error_handler( [ $this, 'error_handler' ] );
* The 'plugins_loaded' hook.
public function plugins_loaded() {
// Constants of plugins that destroy an error handler chain.
'QM_VERSION', // Query Monitor.
'AUTOMATOR_PLUGIN_VERSION', // Uncanny Automator.
foreach ( $constants as $constant ) {
if ( defined( $constant ) ) {
// Set this error handler after loading a plugin to chain its error handler.
( new self( $this->dirs, $this->levels ) )->set_error_handler();
* @param int $level Error level.
* @param string $message Error message.
* @param string $file File produced an error.
* @param int $line Line number.
* @noinspection PhpTernaryExpressionCanBeReplacedWithConditionInspection
public function error_handler( int $level, string $message, string $file, int $line ): bool { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
// Prevent infinite recursion and fallback to standard error handler.
if ( ( $level & $this->levels ) === 0 ) {
// Not served error level, use fallback error handler.
// phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
return $this->fallback_error_handler( func_get_args() );
$normalized_file = str_replace( DIRECTORY_SEPARATOR, '/', $file );
foreach ( $this->dirs as $dir ) {
if ( strpos( $normalized_file, $dir ) !== false ) {
// Suppress deprecated errors from this directory.
// Not served directory, use fallback error handler.
// phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
return $this->fallback_error_handler( func_get_args() );
* Action for _doing_it_wrong() calls.
* @param string $function_name The function that was called.
* @param string $message A message explaining what has been done incorrectly.
* @param string $version The version of WordPress where the message was added.
* @noinspection PhpMissingParamTypeInspection
* @noinspection PhpUnusedParameterInspection
public function action_doing_it_wrong_run( $function_name, $message, $version ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed, WPForms.PHP.HooksMethod.InvalidPlaceForAddingHooks
$function_name = (string) $function_name;
$message = (string) $message;
if ( ! class_exists( 'QM_Collectors' ) || ! $this->is_just_in_time_for_wpforms_domain( $function_name, $message ) ) {
$qm_collector_doing_it_wrong = QM_Collectors::get( 'doing_it_wrong' );
$current_priority = $wp_filter['doing_it_wrong_run']->current_priority();
if ( $qm_collector_doing_it_wrong === null || $current_priority === false ) {
switch ( $current_priority ) {
remove_action( 'doing_it_wrong_run', [ $qm_collector_doing_it_wrong, 'action_doing_it_wrong_run' ] );
add_action( 'doing_it_wrong_run', [ $qm_collector_doing_it_wrong, 'action_doing_it_wrong_run' ], 10, 3 );
* Filter for _doing_it_wrong() calls.
* @param bool|mixed $trigger Whether to trigger the error for _doing_it_wrong() calls. Default true.
* @param string $function_name The function that was called.
* @param string $message A message explaining what has been done incorrectly.
* @param string $version The version of WordPress where the message was added.
* @noinspection PhpMissingParamTypeInspection
* @noinspection PhpUnusedParameterInspection
public function filter_doing_it_wrong_trigger_error( $trigger, $function_name, $message, $version ): bool { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
$trigger = (bool) $trigger;
$function_name = (string) $function_name;
$message = (string) $message;
return $this->is_just_in_time_for_wpforms_domain( $function_name, $message ) ? false : $trigger;
* @param string|mixed $translation Translated text.
* @param string|mixed $text Text to translate.
* @param string|mixed $domain Text domain. Unique identifier for retrieving translated strings.
* @noinspection PhpUnusedParameterInspection
public function filter_gettext( $translation, $text, $domain ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
_deprecated_function( __METHOD__, '1.9.3 of the WPForms plugin' );
* Fallback error handler.
* @param array $args Arguments.
* @noinspection PhpTernaryExpressionCanBeReplacedWithConditionInspection
private function fallback_error_handler( array $args ): bool {
$result = $this->previous_error_handler === null ?
// Use standard error handler.
(bool) call_user_func_array( $this->previous_error_handler, $args );
private function normalize_dirs() {
$this->dirs = array_filter(
static function ( $dir ) {
return str_replace( DIRECTORY_SEPARATOR, '/', trim( $dir ) );
* Whether it is the just_in_time_error for WPForms-related domains.
* @param string $function_name Function name.
* @param string $message Message.
protected function is_just_in_time_for_wpforms_domain( string $function_name, string $message ): bool {
return $function_name === '_load_textdomain_just_in_time' && strpos( $message, '<code>wpforms' ) !== false;