null, 'notices' => null, ); /** * Dismiss action link is available or not. * * @var bool */ private $dismiss = true; /** * */ public function __construct( $args ) { parent::__construct( $args ); add_action( 'admin_notices', array( $this, 'admin_notices' ) ); add_action( 'wp_ajax_stateless_notice_dismiss', array( $this, 'dismiss_notices' ) ); add_action( 'wp_ajax_stateless_enable_notice_button_action', array( $this, 'stateless_enable_notice_button_action' ) ); } /** * Add new message for admin notices * * @param string $message * @param string $type Values: 'error', 'message', 'warning' * @author peshkov@UD */ public function add( $message, $type = 'error' ) { switch( $type ) { case 'error': $this->errors[] = $message; break; case 'message': case 'warning': case 'notice': if(!is_array($message)){ $message = array( 'title' => sprintf( __( '%s has the following notice:', $this->domain ), $this->name ), 'message' => $message, 'button' => null, ); } if(empty($message['key'])){ $message['key'] = md5( $message['title'] ); } $this->notices[] = $message; break; } } /** * Add footer link to specific ( errors|messages|wanrnings ) block * * @author peshkov@UD */ public function add_action_link( $link, $type = 'error' ) { switch( $type ) { case 'error': $this->action_links[ 'errors' ][] = $link; break; case 'message': case 'warning': case 'notice': $this->action_links[ 'notices' ][] = $link; break; } } /** * Determine if errors exist * * @author peshkov@UD */ public function has_errors() { return !empty( $this->errors ) ? true : false; } /** * Renders admin notes in case there are errors or notices on bootstrap init * * @author peshkov@UD */ public function admin_notices() { global $wp_version; wp_enqueue_style("stateless-error-style", ud_get_stateless_media()->path('static/styles/error-notice.css')); //enqueue dismiss js for ajax requests $script_path = \UsabilityDynamics\WP\Utility::path( 'static/scripts/ud-dismiss.js', 'url' ); wp_enqueue_script( "sateless-error-notice-js", ud_get_stateless_media()->path( 'static/scripts/error-notice.js', 'url' ), array( 'jquery' ) ); wp_enqueue_script( "ud-dismiss", $script_path, array( 'jquery' ) ); wp_localize_script( "ud-dismiss", "_ud_vars", array( "ajaxurl" => admin_url( 'admin-ajax.php' ), ) ); //** Don't show the message if the user has no enough permissions. */ if ( ! function_exists( 'wp_get_current_user' ) ) { require_once( ABSPATH . 'wp-includes/pluggable.php' ); } if( empty( $this->args['type'] ) || ( $this->args['type'] == 'plugin' && !current_user_can( 'activate_plugins' ) ) || ( $this->args['type'] == 'theme' && !current_user_can( 'switch_themes' ) ) ) { return; } //** Don't show the message if on a multisite and the user isn't a super user. */ if ( is_multisite() && ! is_super_admin() ) { return; } //** Ignore messages on TGM Plugin Activation page */ if( \UsabilityDynamics\WP\TGM_Plugin_Activation::get_instance()->is_tgmpa_page() ) { return; } $errors = apply_filters( 'ud:errors:admin_notices', $this->errors, $this->args ); $notices = apply_filters( 'stateless:notices:admin_notices', $this->notices, $this->args ); //** Errors Block */ if( !empty( $errors ) && is_array( $errors ) ) { $message = '