icl-admin-notifier.php 28.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
<?php
/**
 * @package wpml-core
 */

/**
 * Admin Notifier Class
 *
 * Manages Admin Notices
 */

add_action( 'init', array( 'ICL_AdminNotifier', 'init' ) );

if ( ! class_exists( 'ICL_AdminNotifier' ) ) {
	class ICL_AdminNotifier {
		public static function init() {
			if ( is_admin() ) {
				add_action( 'wp_ajax_icl-hide-admin-message', array( __CLASS__, 'process_hide_message' ) );
				add_action( 'wp_ajax_icl-show-admin-message', array( __CLASS__, 'process_show_message' ) );
				if ( ! defined( 'DOING_AJAX' ) ) {
					add_action( 'admin_enqueue_scripts', array( __CLASS__, 'add_script' ) );

					add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) );
				}

				add_filter( 'troubleshooting_js_data', array( __CLASS__, 'troubleshooting_js_data' ) );
				add_action( 'wpml_troubleshooting_cleanup', array( __CLASS__, 'troubleshooting' ) );
				add_action( 'wp_ajax_icl_restore_notifications', array( __CLASS__, 'process_restore_notifications' ) );
				add_action( 'wp_ajax_icl_remove_notifications', array( __CLASS__, 'process_remove_notifications' ) );
			}
		}

		public static function add_script() {
			$handle = 'icl-admin-notifier';
			wp_register_script(
				$handle,
				ICL_PLUGIN_URL . '/res/js/icl-admin-notifier.js',
				array( 'jquery' ),
				ICL_SITEPRESS_VERSION,
				true
			);
			wp_localize_script(
				$handle,
				'icl_admin_notifier_strings',
				array(
					'iclHideAdminMessageNonce' => wp_create_nonce( 'icl_hide_admin_message' ),
					'iclShowAdminMessageNonce' => wp_create_nonce( 'icl_show_admin_message' ),
				)
			);
			wp_enqueue_script( $handle );
		}

		/**
		 * @param string $message
		 * @param string $type
		 */
		public static function add_instant_message( $message, $type = '' ) {
			$messages                       = self::get_messages();
			$messages['instant_messages'][] = array(
				'text' => $message,
				'type' => $type,
			);
			self::save_messages( $messages );
		}

		/**
		 * @param int|string $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 array<mixed> $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;
					}

					if ( isset( $message_user_data['hidden'] ) ) {
						$args['hidden'] = $message_user_data['hidden'] ? false : $args['hidden'];
					}
				}
			}

			$id       = $id ? $id : md5( (string) 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( (string) wp_json_encode( $message ) );

			if ( isset( $messages['messages'][ $id ] ) ) {
				$existing_message_md5 = md5( (string) 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 process_hide_message() {
			self::validate_request(
				'icl_hide_admin_message',
				function() {
					$fallback_message = self::hide_message();
					echo wp_json_encode(
						array(
							'errors'  => 0,
							'message' => __( 'Done', 'sitepress' ),
							'text'    => $fallback_message,
						)
					);
					die();
				}
			);
		}

		public static function hide_message() {

			$message_id = self::get_message_id();
			// phpcs:ignore WordPress.Security.NonceVerification
			$dismiss    = isset( $_POST['dismiss'] ) ? $_POST['dismiss'] : false;
			if ( ! self::message_id_exists( $message_id ) ) {
				return '';
			}

			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'] ) {
					return self::display_message( $message_id, $message['fallback_text'], $message['fallback_type'], $message['fallback_classes'], false, false, true, true );
				}
			}

			return '';
		}

		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 process_show_message() {
			self::validate_request(
				'icl_show_admin_message',
				function() {
					$message = self::show_message();
					echo wp_json_encode(
						array(
							'errors'  => 0,
							'message' => __( 'Done', 'sitepress' ),
							'text'    => $message,
						)
					);
					die();
				}
			);
		}

		public static function show_message() {
			$message_id = self::get_message_id();
			if ( ! self::message_id_exists( $message_id ) ) {
				return '';
			}

			self::set_message_display( $message_id, true, 'hide', 'hidden', 'hide_per_user' );

			$messages = self::get_messages();
			$message  = $messages['messages'][ $message_id ];
			if ( $message ) {
				return 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 );
			}

			return '';
		}

		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 ) {
				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 ) {
				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|string $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 .= '<div class="' . $class . '" id="icl-id-' . $id . '"';

			if ( $hide ) {
				$result .= ' data-hide-text="' . __( 'Hide', 'sitepress' ) . '" ';
			}
			$result .= '>';

			$result .= '<p>' . self::sanitize_and_format_message( $message ) . '</p>';
			if ( $hide ) {
				$result .= ' <span class="icl-admin-message-hide notice-dismiss"><span class="screen-reader-text">' . __( 'Hide this notice.', 'sitepress' ) . '</span></span>';
			}

			if ( $dismiss ) {
				$result .= ' <span class="icl-admin-message-dismiss notice-dismiss">';
				$result .= '<span class="screen-reader-text"><input class="icl-admin-message-dismiss-check" type="checkbox" value="1" />';
				$result .= __( 'Dismiss this notice.', 'sitepress' );
				$result .= '</span></span>';
			}

			$result .= '</div>';

			if ( ! $echo ) {
				echo $result;
			}

			return $result;
		}

		public static function display_instant_message( $message, $type = 'information', $class = false, $return = false, $fadeout = false ) {
			$classes = array();
			if ( ! $class && $type ) {
				$classes[] = $type;
			}
			$classes[] = 'instant-message';
			$classes[] = 'message';
			$classes[] = 'message-' . $type;

			foreach ( $classes as $class ) {
				$classes[] = 'icl-admin-' . $class;
			}

			if ( $fadeout ) {
				$classes[] = 'js-icl-fadeout';
			}

			$classes = array_unique( $classes );

			if ( in_array( 'error', $classes ) ) {
				$key = array_search( 'error', $classes );
				if ( $key !== false ) {
					unset( $classes[ $key ] );
				}
			}

			$result  = '<div class="' . implode( ' ', $classes ) . '">';
			$result .= self::sanitize_and_format_message( $message );
			$result .= '</div>';

			if ( ! $return ) {
				echo $result;
			}

			return $result;
		}

		/**
		 * @param array<mixed> $args
		 *
		 * @return mixed
		 */
		private static function sanitize_message_args( $args ) {
			if ( isset( $args['msg'] ) ) {
				$args['text'] = $args['msg'];
				unset( $args['msg'] );
			}

			if ( isset( $args['fallback'] ) ) {
				$args['fallback_message'] = $args['fallback'];
				unset( $args['fallback'] );
			}

			if ( isset( $args['message_fallback'] ) ) {
				$args['fallback_message'] = $args['message_fallback'];
				unset( $args['message_fallback'] );
			}

			if ( isset( $args['type_fallback'] ) ) {
				$args['fallback_type'] = $args['type_fallback'];
				unset( $args['type_fallback'] );

				return $args;
			}

			if ( ! isset( $args['classes'] ) ) {
				$args['classes'] = array();
			} elseif ( ! is_array( $args['classes'] ) ) {
				$args['classes'] = (array) $args['classes'];
			}

			if ( ! isset( $args['limit_to_page'] ) ) {
				$args['limit_to_page'] = array();
			} elseif ( ! is_array( $args['limit_to_page'] ) ) {
				$args['limit_to_page'] = (array) $args['limit_to_page'];
			}

			return $args;
		}

		static function troubleshooting_js_data( $data ) {
			$data['nonce']['icl_restore_notifications'] = wp_create_nonce( 'icl_restore_notifications' );
			$data['nonce']['icl_remove_notifications']  = wp_create_nonce( 'icl_remove_notifications' );

			return $data;
		}

		static function has_hidden_messages() {
			$messages           = self::get_messages();
			$no_hidden_messages = true;
			foreach ( $messages as $group => $message_group ) {
				foreach ( $message_group as $id => $msg ) {
					if ( ( isset( $msg['hidden'] ) && $msg['hidden'] ) || ( isset( $msg['dismissed'] ) && $msg['dismissed'] ) ) {
						$no_hidden_messages = false;
					} else {
						$current_user_id   = get_current_user_id();
						$message_user_data = isset( $msg['users'][ $current_user_id ] ) ? $msg['users'][ $current_user_id ] : false;
						if ( $message_user_data && $msg['dismiss_per_user'] && isset( $message_user_data['dismissed'] ) && $message_user_data['dismissed'] ) {
							$no_hidden_messages = false;
						} elseif ( $message_user_data && $msg['dismiss'] && isset( $msg['dismissed'] ) && $msg['dismissed'] ) {
							$no_hidden_messages = false;
						}

						if ( $no_hidden_messages ) {
							if ( isset( $msg['hide_per_user'] ) && $msg['hide_per_user'] && isset( $message_user_data['hidden'] ) && $message_user_data['hidden'] ) {
								$no_hidden_messages = ! $msg['fallback_text'];
							} elseif ( $msg['hide'] && isset( $msg['hidden'] ) && $msg['hidden'] ) {
								$no_hidden_messages = ! $msg['fallback_text'];
							}
						}
					}

					if ( ! $no_hidden_messages ) {
						return true;
					}
				}
			}

			return false;
		}

		static function troubleshooting() {
			?>
					<h4><?php _e( 'Messages and notifications', 'sitepress' ); ?></h4>
			<?php
			if ( self::has_hidden_messages() ) {

				?>
							<p>
								<input id="icl_restore_notifications" type="button" class="button-secondary" value="<?php echo __( 'Restore messages and notification', 'sitepress' ); ?>"/>
								<br/>
								<br/>
								<input id="icl_restore_notifications_all_users" name="icl_restore_notifications_all_users" type="checkbox" value="1"/><label for="icl_restore_notifications_all_users"><?php echo __( 'Apply to all users', 'sitepress' ); ?></label>
								<br/>
								<br/>
								<small style="margin-left:10px;"><?php echo __( 'Restore dismissed and hidden messages and notifications.', 'sitepress' ); ?></small>
							</p>
				<?php
			}
			?>
					<p>
						<input id="icl_remove_notifications" type="button" class="button-secondary" value="<?php echo __( 'Remove all messages and notifications', 'sitepress' ); ?>"/>
						<br/>
						<small style="margin-left:10px;"><?php echo __( 'Remove all messages and notifications, for all users.', 'sitepress' ); ?></small>
					</p>
			<?php
		}

		public static function process_remove_notifications() {
			self::validate_request(
				'icl_remove_notifications',
				function() {
					self::remove_notifications();
				}
			);
		}

		public static function remove_notifications() {
			self::save_messages( array() );
			echo wp_json_encode(
				array(
					'errors'  => 0,
					'message' => __( 'Done', 'sitepress' ),
					'cont'    => 0,
					'reload'  => 1,
				)
			);
			die();
		}

		public static function process_restore_notifications() {
			self::validate_request(
				'icl_restore_notifications',
				function() {
					self::restore_notifications();
				}
			);
		}

		public static function restore_notifications() {
			$all_users = $_POST['all_users'];

			$messages = self::get_messages();
			$dirty    = 0;
			foreach ( $messages as $group => $message_group ) {
				foreach ( $message_group as $id => $msg ) {
					if ( $msg['hidden'] ) {
						$msg['hidden'] = false;
						$dirty ++;
					}
					if ( $msg['dismissed'] ) {
						$msg['dismissed'] = false;
						$dirty ++;
					}

					$current_user_id = get_current_user_id();
					foreach ( $msg['users'] as $user_id => $message_user_data ) {
						if ( $current_user_id == $user_id || $all_users ) {
							if ( $message_user_data['hidden'] ) {
								$message_user_data['hidden'] = false;
								$dirty ++;
							}
							if ( $message_user_data['dismissed'] ) {
								$message_user_data['dismissed'] = false;
								$dirty ++;
							}
						}
						$msg['users'][ $user_id ] = $message_user_data;
					}

					$message_group[ $id ] = $msg;
				}
				$messages[ $group ] = $message_group;
			}

			if ( $dirty ) {
				self::save_messages( $messages );
			}

			echo wp_json_encode(
				array(
					'errors'  => 0,
					'message' => __( 'Done', 'sitepress' ),
					'cont'    => $dirty,
					'reload'  => 1,
				)
			);
			die();
		}

		private static function validate_request( $nonce_name, $cb ) {
			$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';

			if ( ! current_user_can( 'manage_options' ) ) {
				wp_send_json_error( esc_html__( 'Unauthorized', 'sitepress' ), 401 );
				return;
			}

			if ( ! wp_verify_nonce( $nonce, $nonce_name ) ) {
				wp_send_json_error( esc_html__( 'Invalid request!', 'sitepress' ), 400 );
				return;
			}

			$cb();
		}

		/** Deprecated methods */

		/**
		 * @param int|string $message_id
		 *
		 * @return bool
		 * @deprecated deprecated @since version 3.2. Use ICL_AdminNotifier::remove_message()
		 *
		 */
		public static function removeMessage( $message_id ) {
			return self::remove_message( $message_id );
		}

		/**
		 * @deprecated deprecated @since version 3.2
		 */
		public static function hideMessage() {
			self::hide_message();
		}

		/**
		 * @deprecated deprecated @since version 3.2
		 *
		 * @param string $message
		 * @param string $type
		 */
		public static function addInstantMessage( $message, $type = '' ) {
			self::add_instant_message( $message, $type );
		}

		/**
		 * @deprecated deprecated @since version 3.2
		 */
		public static function addScript() {
			self::add_script();
		}

		/**
		 * @deprecated deprecated @since version 3.2
		 *
		 * @param string      $id               An unique identifier for the message
		 * @param string      $msg              The actual message
		 * @param 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
		 * @param bool        $hide             (optional) Enable the toggle link to permanently hide the notice
		 * @param bool        $fallback_message (optional) A message to show when the notice gets hidden
		 * @param bool        $fallback_type    (optional) The message type to use in the fallback message (@see $type)
		 * @param 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)
		 * @param bool        $admin_notice     (optional) Hook the rendering to the 'admin_notice' action
		 */
		public static function addMessage( $id, $msg, $type = '', $hide = true, $fallback_message = false, $fallback_type = false, $group = false, $admin_notice = false ) {
			$args = array(
				'id'               => $id,
				'msg'              => $msg,
				'type'             => $type,
				'hide'             => $hide,
				'message_fallback' => $fallback_message,
				'fallback_type'    => $fallback_type,
				'group'            => $group,
				'admin_notice'     => $admin_notice,
			);

			self::add_message( $args );
		}

		/**
		 * @param string $message
		 * @param string $type
		 * @param bool   $class
		 * @param bool   $return
		 *
		 * @return string
		 * @deprecated deprecated @since version 3.2. Use ICL_AdminNotifier::display_instant_message()
		 *
		 */
		public static function displayInstantMessage( $message, $type = 'information', $class = false, $return = false ) {
			return self::display_instant_message( $message, $type, $class, $return );
		}

		/**
		 * @param string $message
		 *
		 * @return string
		 */
		public static function sanitize_and_format_message( $message ) {
			// return preg_replace( '/`(.*?)`/s', '<pre>$1</pre>', 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 = '<pre>' . esc_html( $match_to_sanitize ) . '</pre>';
				}
				unset( $match_to_sanitize );

				$sanitized_message = str_replace( $matches[0], $matches_to_sanitize, $sanitized_message );
			}

			return stripslashes( $sanitized_message );
		}
	}
}