get_option( $id, '' ), 'id' => $id, ) ); register_setting( self::PAGE_NAME, $id, 'sanitize_text_field' ); $id = self::SETTINGS_PREFIX . 'type'; add_settings_field( $id, __( 'Message type', 'admin-user-message' ), array( static::$class, 'select_render' ), self::PAGE_NAME, 'default', array( 'content' => get_option( $id, '' ), 'id' => $id, ) ); register_setting( self::PAGE_NAME, $id, 'sanitize_text_field' ); $id = self::SETTINGS_PREFIX . 'content'; add_settings_field( $id, __( 'Message content', 'admin-user-message' ), array( static::$class, 'wysiwyg_render' ), self::PAGE_NAME, 'default', array( 'content' => get_option( $id, '' ), 'id' => $id, ) ); register_setting( self::PAGE_NAME, $id, 'wp_kses_post' ); $id = self::SETTINGS_PREFIX . 'exclude'; add_settings_field( $id, __( 'Exclude message for roles', 'admin-user-message' ), array( static::$class, 'multi_select_render' ), self::PAGE_NAME, 'default', array( 'content' => get_option( $id, '' ), 'id' => $id, ) ); register_setting( self::PAGE_NAME, $id, array( self::$class, 'sanitize_multi_select' ) ); $id = self::SETTINGS_PREFIX . 'dismiss'; add_settings_field( $id, __( 'Allow message to be dismissed', 'admin-user-message' ), array( static::$class, 'checkbox_render' ), self::PAGE_NAME, 'default', array( 'content' => get_option( $id, '' ), 'id' => $id, ) ); register_setting( self::PAGE_NAME, $id, 'sanitize_text_field' ); $id = self::SETTINGS_PREFIX . 'reset'; add_settings_field( $id, __( 'Reset dismiss for everyone. (Force appearance of message)', 'admin-user-message' ), array( static::$class, 'checkbox_render' ), self::PAGE_NAME, 'default', array( 'content' => get_option( $id, '' ), 'id' => $id, ) ); register_setting( self::PAGE_NAME, $id, array( self::$class, 'increment_msg_id' ) ); } /** * Render text area settings field * * @param $args */ public static function wysiwyg_render( $args ) { printf( wp_editor( $args['content'], $args['id'], array( 'media_buttons' => false, 'textarea_rows' => 4, 'teeny' => true, ) ) ); } /** * Render a checkbox settings * * @param $args */ public static function checkbox_render( $args ) { printf( '', $args['id'], checked( 'on', $args['content'], false ) ); } /** * Render a select * * @param $args */ public static function select_render( $args ) { $types = array( 'updated' => esc_html__( 'Informative', 'admin-user-message' ), 'error' => esc_html__( 'Important', 'admin-user-message' ), 'update-nag' => esc_html__( 'Update', 'admin-user-message' ), ); printf( ''; } /** * Render a multiselect * * @param $args */ public static function multi_select_render( $args ) { global $wp_roles; printf( ''; } /** * Sanitize multi select options * * @param $data * * @return array|string */ public static function sanitize_multi_select( $data ) { if ( is_array( $data ) ) { $data = array_map( 'sanitize_text_field', $data ); } else { $data = sanitize_text_field( $data ); } return $data; } /** * Increment the message id so everyone will see it next time they reload * * @param $data */ public static function increment_msg_id( $data ) { if ( ! is_null( $data ) ) { $id = get_option( self::SETTINGS_PREFIX . 'id', 1 ); $id++; update_option( self::SETTINGS_PREFIX . 'id', $id ); } } /** * Add new admin menu to settings page */ public static function register_settings_page() { add_options_page( __( 'Admin User Message', 'admin-user-message' ), __( 'Admin User Message', 'admin-user-message' ), apply_filters( 'admin_user_message_cap', 'manage_options' ), self::PAGE_NAME, array( self::$class, 'page_callback' ) ); } /** * Page function */ public static function page_callback() { ?>

get( $token ); $msg_id = get_option( self::SETTINGS_PREFIX . 'id', 1 ); if ( isset( $session[ 'admin-user-message-dismiss-' . $msg_id ] ) ) { return true; } } } /** * Validate roles * * @return bool */ private static function can_show_message_for_role() { $user = wp_get_current_user(); if ( empty( $user ) ) { return false; } $exclude_for_roles = get_option( self::SETTINGS_PREFIX . 'exclude', array() ); // No role was selected so we can show the message if ( empty( $exclude_for_roles ) ) { return true; } foreach ( (array) $user->roles as $role ) { if ( ! in_array( $role, $exclude_for_roles ) ) { return true; } } return false; } /** * Will dismiss message for current session only */ public static function dismiss_message() { check_ajax_referer( 'admin_user_message_nonce', 'admin_user_message_nonce' ); $token = wp_get_session_token(); if ( $token ) { $manager = WP_Session_Tokens::get_instance( get_current_user_id() ); $session = $manager->get( $token ); add_filter( 'attach_session_information', '__return_empty_array' ); $manager->update( $token, array_merge( $session, array( 'admin-user-message-dismiss-' . get_option( self::SETTINGS_PREFIX . 'id', 1 ) => true ) ) ); } wp_send_json_success(); } } add_action( 'plugins_loaded', array( 'Admin_User_Message', 'setup' ) );