WC_Settings_Payment_Gateways::suppress_admin_notices
Suppress WP admin notices on the WooCommerce Payments settings page.
Method of the class: WC_Settings_Payment_Gateways{}
No Hooks.
Returns
null. Nothing (null).
Usage
$WC_Settings_Payment_Gateways = new WC_Settings_Payment_Gateways(); $WC_Settings_Payment_Gateways->suppress_admin_notices();
WC_Settings_Payment_Gateways::suppress_admin_notices() WC Settings Payment Gateways::suppress admin notices code WC 10.7.0
public function suppress_admin_notices() {
global $wp_filter, $current_tab, $current_section;
$screen = get_current_screen();
if ( ! $screen instanceof WP_Screen || 'woocommerce_page_wc-settings' !== $screen->id ) {
return;
}
// We only want to suppress notices on the main WooCommerce Payments settings page and Reactified sections.
if ( self::TAB_NAME !== $current_tab ) {
return;
}
if ( ! $this->should_render_react_section( $current_section ) ) {
return;
}
// Generic admin notices are definitely not needed.
remove_all_actions( 'all_admin_notices' );
// WooCommerce uses the 'admin_notices' hook for its own notices.
// We will only allow WooCommerce core notices to be displayed.
$wp_admin_notices_hook = $wp_filter['admin_notices'] ?? null;
if ( ! $wp_admin_notices_hook || ! $wp_admin_notices_hook->has_filters() ) {
// Nothing to do if there are no actions hooked into `admin_notices`.
return;
}
$wc_admin_notices = WC_Admin_Notices::get_notices();
if ( empty( $wc_admin_notices ) ) {
// If there are no WooCommerce core notices, we can remove all actions hooked into `admin_notices`.
remove_all_actions( 'admin_notices' );
return;
}
// Go through the callbacks hooked into `admin_notices` and
// remove any that are NOT from the WooCommerce core (i.e. from the `WC_Admin_Notices` class).
foreach ( $wp_admin_notices_hook->callbacks as $priority => $callbacks ) {
if ( ! is_array( $callbacks ) ) {
continue;
}
foreach ( $callbacks as $callback ) {
// Ignore malformed callbacks.
if ( ! is_array( $callback ) ) {
continue;
}
// WooCommerce doesn't use closures to handle notices.
// WooCommerce core notices are handled by `WC_Admin_Notices` class methods.
// Remove plain functions or closures.
if ( ! is_array( $callback['function'] ) ) {
remove_action( 'admin_notices', $callback['function'], $priority );
continue;
}
$class_or_object = $callback['function'][0] ?? null;
// We need to allow Automattic\WooCommerce\Internal\Admin\Loader methods callbacks
// because they are used to wrap notices.
// @see Automattic\WooCommerce\Internal\Admin\Loader::inject_before_notices().
// @see Automattic\WooCommerce\Internal\Admin\Loader::inject_after_notices().
if (
(
// We have a class name.
is_string( $class_or_object ) &&
! ( WC_Admin_Notices::class === $class_or_object || Loader::class === $class_or_object )
) ||
(
// We have a class instance.
is_object( $class_or_object ) &&
! ( $class_or_object instanceof WC_Admin_Notices || $class_or_object instanceof Loader )
)
) {
remove_action( 'admin_notices', $callback['function'], $priority );
}
}
}
}