$message, 'type' => $type ); self::save_messages( $messages ); } /** * @param $message_id * * @return bool|array */ public static function get_message( $message_id ) { $messages = self::get_messages(); return isset( $messages['messages'][ $message_id ] ) ? $messages['messages'][ $message_id ] : false; } public static function message_id_exists( $message_id ) { $message = self::get_message( $message_id ); return $message !== false; } private static function get_messages() { $messages = get_option( 'icl_admin_messages' ); if ( ! ( isset( $messages ) && $messages != false ) ) { return array( 'messages' => array(), 'instant_messages' => array() ); } if ( ! isset( $messages['messages'] ) || ! isset( $messages['instant_messages'] ) ) { $messages = array( 'messages' => array(), 'instant_messages' => array() ); } return (array) $messages; } private static function save_messages( $messages ) { if ( isset( $messages ) ) { update_option( 'icl_admin_messages', (array) $messages ); } self::get_messages(); } /** * @param $args * Args attributes: * string id - An unique identifier for the message * string msg - The actual message * string type (optional) - Any string: it will be used as css class fro the message container. A typical value is 'error', but the following strings can be also used: icl-admin-message-information, icl-admin-message-warning * array classes (optional) - Display the notice only on specific url(s) * bool hide (optional) - Enable the toggle link to permanently hide the notice * bool hide_per_user (optional) - Enable the toggle link per user basis (overrides hide option) * bool dismiss (optional) - Enable the dismiss option * bool dismiss_per_user (optional) - Enable the dismiss option per user basis (overrides dismiss option) * bool|string fallback_text (optional) - A message to show when the notice gets hidden * bool|string fallback_type (optional) - The message type to use in the fallback message (@see $type) * array fallback_classes (optional) - The message type to use in the fallback message (@see $type) * bool|string group (optional) - A way to group messages: when displaying messages stored with this method, it's possible to filter them by group (@see ICL_AdminNotifier::displayMessages) * bool admin_notice (optional) - Hook the rendering to the 'admin_notice' action * string|array limit_to_page (optional) - Display the notice only on specific page(s) */ public static function add_message( $args ) { $defaults = array( 'type' => '', 'classes' => array(), 'hide' => false, 'hide_per_user' => false, 'dismiss' => false, 'dismiss_per_user' => false, 'fallback_text' => false, 'fallback_type' => false, 'fallback_classes' => array(), 'group' => false, 'admin_notice' => false, 'hidden' => false, 'dismissed' => false, 'limit_to_page' => false, 'show_once' => false, 'capability' => '', ); $args = self::sanitize_message_args( $args ); $args = array_merge( $defaults, $args ); $id = $args['id']; //Check if existing message has been set as dismissed or hidden if ( self::message_id_exists( $id ) ) { $temp_msg = self::get_message( $id ); if ( $temp_msg ) { $current_user_id = get_current_user_id(); $message_user_data = isset( $temp_msg['users'][ $current_user_id ] ) ? $temp_msg['users'][ $current_user_id ] : false; if ( self::is_user_dismissed( $temp_msg ) || self::is_globally_dismissed( $temp_msg ) || self::is_globally_hidden( $temp_msg ) ) { return; } $args['hidden'] = $message_user_data['hidden'] ? false : $args['hidden']; } } $id = $id ? $id : md5( wp_json_encode( $args ) ); $messages = self::get_messages(); $message = array( 'id' => $id, 'text' => $args['text'], 'type' => $args['type'], 'classes' => $args['classes'], 'hide' => $args['hide'], 'hide_per_user' => $args['hide_per_user'], 'dismiss' => $args['dismiss'], 'dismiss_per_user' => $args['dismiss_per_user'], 'fallback_text' => $args['fallback_text'], 'fallback_type' => $args['fallback_type'], 'fallback_classes' => $args['classes'], 'group' => $args['group'], 'admin_notice' => $args['admin_notice'], 'hidden' => false, 'dismissed' => false, 'limit_to_page' => $args['limit_to_page'], 'show_once' => $args['show_once'], 'capability' => $args['capability'], ); $message_md5 = md5( wp_json_encode( $message ) ); if ( isset( $messages['messages'][ $id ] ) ) { $existing_message_md5 = md5( wp_json_encode( $messages['messages'][ $id ] ) ); if ( $message_md5 != $existing_message_md5 ) { unset( $messages['messages'][ $id ] ); } } if ( ! isset( $messages['messages'][ $id ] ) ) { $messages['messages'][ $id ] = $message; self::save_messages( $messages ); } } public static function is_user_dismissed( $message_data ) { $current_user_id = get_current_user_id(); $message_user_data = isset( $message_data['users'][ $current_user_id ] ) ? $message_data['users'][ $current_user_id ] : false; return ! empty( $message_data['dismiss_per_user'] ) && ! empty( $message_user_data['dismissed'] ); } public static function is_globally_dismissed( $message_data ) { return ! empty( $message_data['dismiss'] ) && $message_data['dismissed']; } public static function is_globally_hidden( $message_data ) { return ! empty( $message_data['hide'] ) && $message_data['hidden']; } public static function hide_message() { $message_id = self::get_message_id(); $dismiss = isset( $_POST['dismiss'] ) ? $_POST['dismiss'] : false; if ( ! self::message_id_exists( $message_id ) ) { exit; } self::set_message_display( $message_id, false, 'hide', 'hidden', 'hide_per_user' ); if ( $dismiss ) { self::set_message_display( $message_id, false, 'dismiss', 'dismissed', 'dismiss_per_user' ); } else { $messages = self::get_messages(); $message = $messages['messages'][ $message_id ]; if ( $message && isset( $message['fallback_text'] ) && $message['fallback_text'] ) { echo self::display_message( $message_id, $message['fallback_text'], $message['fallback_type'], $message['fallback_classes'], false, false, true, true ); } } exit; } public static function get_message_id() { $message_id = ''; if ( isset( $_POST['icl-admin-message-id'] ) ) { $message_id = filter_var( $_POST['icl-admin-message-id'], FILTER_SANITIZE_FULL_SPECIAL_CHARS ); } $message_id = $message_id ? $message_id : ''; $message_id = preg_replace( '/^icl-id-/', '', $message_id ); return $message_id; } public static function show_message() { $message_id = self::get_message_id(); if ( ! self::message_id_exists( $message_id ) ) { exit; } self::set_message_display( $message_id, true, 'hide', 'hidden', 'hide_per_user' ); $messages = self::get_messages(); $message = $messages['messages'][ $message_id ]; if ( $message ) { echo self::display_message( $message_id, $message['text'], $message['type'], $message['classes'], $message['hide'] || $message['hide_per_user'], $message['dismiss'] || $message['dismiss_per_user'], true, true ); } exit; } public static function engage_message() { $message_id = self::get_message_id(); if ( ! self::message_id_exists( $message_id ) ) { exit; } self::set_message_display( $message_id, true, 'dismiss', 'dismissed', 'dismiss_per_user' ); } private static function set_message_display( $message_id, $show, $action, $action_past, $action_user ) { if ( $message_id === null ) { return; } $messages = self::get_messages(); if ( ! isset( $messages['messages'][ $message_id ] ) ) { return; } $message = $messages['messages'][ $message_id ]; $current_user_id = get_current_user_id(); if ( $message[ $action_user ] && $current_user_id ) { $message['users'][ $current_user_id ][ $action_past ] = ! $show; } elseif ( $message[ $action ] ) { $message[ $action_past ] = ! $show; } $messages['messages'][ $message_id ] = $message; self::save_messages( $messages ); } public static function remove_message( $message_id ) { if ( $message_id === null || ! isset( $message_id ) ) { return false; } $messages = self::get_messages(); if ( ! isset( $messages['messages'][ $message_id ] ) ) { return false; } unset( $messages['messages'][ $message_id ] ); self::save_messages( $messages ); return false; } public static function remove_message_group( $message_group ) { if ( $message_group === null || ! isset( $message_group ) ) { return; } $all_messages = self::get_messages(); if ( ! isset( $all_messages['messages'] ) ) { return; } $messages = $all_messages['messages']; $ids_to_remove = array(); foreach ( $messages as $id => $message_data ) { if ( isset( $message_data['group'] ) && $message_data['group'] == $message_group ) { $ids_to_remove[] = $id; } } foreach ( $ids_to_remove as $id_to_remove ) { self::remove_message( $id_to_remove ); } } public static function display_messages( $group = false ) { if ( is_admin() ) { $messages = self::get_messages(); foreach ( $messages['messages'] as $id => $msg ) { if ( ! $group || ( isset( $msg['group'] ) && $msg['group'] == $group ) ) { if ( isset( $msg['admin_notice'] ) && ! $msg['admin_notice'] ) { if ( ! isset( $msg['capability'] ) || ( $msg['capability'] == '' ) || current_user_can( $msg['capability'] ) ) { if ( array_key_exists( 'limit_to_page', $msg ) ) { foreach( $msg['limit_to_page'] as $page ) { if ( array_key_exists( 'page', $_GET ) && $_GET['page'] === $page ) { self::display_message( $id, $msg['text'], $msg['type'], $msg['classes'], $msg['hide'] || $msg['hide_per_user'], $msg['dismiss'] || $msg['dismiss_per_user'], true ); } } } else { self::display_message( $id, $msg['text'], $msg['type'], $msg['classes'], $msg['hide'] || $msg['hide_per_user'], $msg['dismiss'] || $msg['dismiss_per_user'], true ); } } } } } foreach ( $messages['instant_messages'] as $msg ) { self::display_instant_message( $msg['text'], $msg['type'] ); } // delete instant messages $messages['instant_messages'] = array(); self::save_messages( $messages ); } } /** * @deprecated deprecated @since version 3.2. Use ICL_AdminNotifier::display_message() * * @param bool $group */ public static function displayMessages( $group = false ) { self::display_messages( $group ); } public static function admin_notices() { $messages = self::get_messages(); if ( isset( $messages['messages'] ) ) { foreach ( $messages['messages'] as $id => $msg ) { if ( isset( $msg['limit_to_page'] ) && $msg['limit_to_page'] ) { if ( ! is_array( $msg['limit_to_page'] ) ) { $msg['limit_to_page'] = (array) $msg['limit_to_page']; } if ( ! isset( $_REQUEST['page'] ) || ! in_array( $_REQUEST['page'], $msg['limit_to_page'] ) ) { continue; } } if ( $msg['admin_notice'] ) { $current_user_id = get_current_user_id(); $display = true; $display_fallback = false; $message_user_data = isset( $msg['users'][ $current_user_id ] ) ? $msg['users'][ $current_user_id ] : false; if ( $msg['dismiss_per_user'] && isset( $message_user_data['dismissed'] ) && $message_user_data['dismissed'] ) { $display = false; } elseif ( $msg['dismiss'] && isset( $msg['dismissed'] ) && $msg['dismissed'] ) { $display = false; } if ( $display ) { if ( $msg['hide_per_user'] && isset( $message_user_data['hidden'] ) && $message_user_data['hidden'] ) { $display = false; $display_fallback = $msg['fallback_text']; } elseif ( $msg['hide'] && isset( $msg['hidden'] ) && $msg['hidden'] ) { $display = false; $display_fallback = $msg['fallback_text']; } } $msg['classes'] = isset( $msg['classes'] ) ? $msg['classes'] : array(); $msg['fallback_classes'] = isset( $msg['fallback_classes'] ) ? $msg['fallback_classes'] : array(); if ( $display ) { self::display_message( $id, $msg['text'], $msg['type'], $msg['classes'], $msg['hide'] || $msg['hide_per_user'], $msg['dismiss'] || $msg['dismiss_per_user'], true ); if ( $msg['show_once'] && ! $display_fallback ) { self::remove_message( $msg['id'] ); } } elseif ( $display_fallback ) { self::display_message( $id, $msg['fallback_text'], $msg['fallback_type'], $msg['fallback_classes'], false, false, true ); if ( $msg['show_once'] ) { self::remove_message( $msg['id'] ); } } } } } } /** * @param string $id * @param string $message * @param string $type * @param string|array $classes * @param bool $hide * @param bool $dismiss * @param bool $admin_notice * @param bool $echo * * @return string */ private static function display_message( $id, $message, $type = '', $classes = array(), $hide = true, $dismiss = false, $admin_notice = false, $echo = false ) { $result = ''; $temp_classes = array(); if ( strpos( $type, 'icl-admin-message' ) ) { $type = str_replace( 'icl-admin-message-', '', $type ); } if ( $admin_notice ) { $temp_classes[] = 'icl-admin-message'; } $temp_types = explode( ' ', $type ); $temp_types = array_unique( $temp_types ); foreach ( $temp_types as $temp_type ) { if ( $admin_notice ) { $temp_classes[] = 'icl-admin-message-' . $temp_type; } $temp_classes[] = $temp_type; } if ( $classes ) { if ( ! is_array( $classes ) ) { $classes = explode( ' ', $classes ); } foreach ( $classes as $class ) { $temp_classes[] = $class; } } if ( $hide OR $dismiss ) { $temp_classes[] = 'otgs-is-dismissible'; } $temp_classes = array_unique( $temp_classes ); $class = implode( ' ', $temp_classes ); $result .= '
$1', stripslashes( $message ) ); $backticks_pattern = '|`(.*)`|U'; preg_match_all( $backticks_pattern, $message, $matches ); $sanitized_message = $message; if ( 2 === count( $matches ) ) { $matches_to_sanitize = $matches[1]; foreach ( $matches_to_sanitize as &$match_to_sanitize ) { $match_to_sanitize = '
' . esc_html( $match_to_sanitize ) . ''; } unset( $match_to_sanitize ); $sanitized_message = str_replace( $matches[0], $matches_to_sanitize, $sanitized_message ); } return stripslashes( $sanitized_message ); } } }