暫無描述

Notices.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace NSL;
  3. use NSL\Persistent\Persistent;
  4. use WP_Error;
  5. class Notices {
  6. private static $notices;
  7. private static $instance;
  8. public static function init() {
  9. if (self::$instance === null) {
  10. self::$instance = new self;
  11. }
  12. }
  13. private function __construct() {
  14. if (is_admin() || (isset($_GET['nsl-notice']) && $_GET['nsl-notice'] == 1)) {
  15. add_action('init', array(
  16. $this,
  17. 'load'
  18. ), 11);
  19. if (basename($_SERVER['PHP_SELF']) !== 'options-general.php' || empty($_GET['page']) || $_GET['page'] !== 'nextend-social-login') {
  20. add_action('admin_notices', array(
  21. $this,
  22. 'admin_notices'
  23. ));
  24. }
  25. add_action('admin_print_footer_scripts', array(
  26. $this,
  27. 'notices_fallback'
  28. ));
  29. add_action('wp_print_footer_scripts', array(
  30. $this,
  31. 'notices_fallback'
  32. ));
  33. }
  34. }
  35. public function load() {
  36. self::$notices = maybe_unserialize(self::get());
  37. if (!is_array(self::$notices)) {
  38. self::$notices = array();
  39. }
  40. }
  41. private static function add($type, $message) {
  42. if (!isset(self::$notices[$type])) {
  43. self::$notices[$type] = array();
  44. }
  45. if (!in_array($message, self::$notices[$type])) {
  46. self::$notices[$type][] = $message;
  47. }
  48. self::set();
  49. }
  50. /**
  51. * @param $message string|WP_Error
  52. */
  53. public static function addError($message) {
  54. if (is_wp_error($message)) {
  55. foreach ($message->get_error_messages() as $m) {
  56. self::add('error', $m);
  57. }
  58. } else {
  59. self::add('error', $message);
  60. }
  61. }
  62. public static function getErrors() {
  63. if (isset(self::$notices['error'])) {
  64. $errors = self::$notices['error'];
  65. unset(self::$notices['error']);
  66. self::set();
  67. return $errors;
  68. }
  69. return false;
  70. }
  71. public static function addSuccess($message) {
  72. self::add('success', $message);
  73. }
  74. public static function displayNotices() {
  75. $html = self::getHTML();
  76. if (!empty($html)) {
  77. echo '<div class="nsl-admin-notices">' . $html . '</div>';
  78. }
  79. }
  80. public function admin_notices() {
  81. echo self::getHTML();
  82. }
  83. /**
  84. * Displays the non-displayed notices in lightbox as a fallback
  85. */
  86. public function notices_fallback() {
  87. $html = self::getHTML();
  88. if (!empty($html)) {
  89. ?>
  90. <div id="nsl-notices-fallback" onclick="this.parentNode.removeChild(this);">
  91. <?php echo $html; ?>
  92. <style>
  93. #nsl-notices-fallback {
  94. position: fixed;
  95. right: 10px;
  96. top: 10px;
  97. z-index: 10000;
  98. }
  99. .admin-bar #nsl-notices-fallback {
  100. top: 42px;
  101. }
  102. #nsl-notices-fallback > div {
  103. position: relative;
  104. background: #fff;
  105. border-left: 4px solid #fff;
  106. box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
  107. margin: 5px 15px 2px;
  108. padding: 1px 20px;
  109. }
  110. #nsl-notices-fallback > div.error {
  111. display: block;
  112. border-left-color: #dc3232;
  113. }
  114. #nsl-notices-fallback > div.updated {
  115. display: block;
  116. border-left-color: #46b450;
  117. }
  118. #nsl-notices-fallback p {
  119. margin: .5em 0;
  120. padding: 2px;
  121. }
  122. #nsl-notices-fallback > div:after {
  123. position: absolute;
  124. right: 5px;
  125. top: 5px;
  126. content: '\00d7';
  127. display: block;
  128. height: 16px;
  129. width: 16px;
  130. line-height: 16px;
  131. text-align: center;
  132. font-size: 20px;
  133. cursor: pointer;
  134. }
  135. </style>
  136. </div>
  137. <?php
  138. }
  139. }
  140. private static function getHTML() {
  141. $html = '';
  142. if (isset(self::$notices['success'])) {
  143. foreach (self::$notices['success'] AS $message) {
  144. $html .= '<div class="updated"><p>' . $message . '</p></div>';
  145. }
  146. }
  147. if (isset(self::$notices['error'])) {
  148. foreach (self::$notices['error'] AS $message) {
  149. $html .= '<div class="error"><p>' . $message . '</p></div>';
  150. }
  151. }
  152. self::clear();
  153. return $html;
  154. }
  155. private static function get() {
  156. return Persistent::get('notices');
  157. }
  158. private static function set() {
  159. Persistent::set('notices', self::$notices);
  160. }
  161. public static function clear() {
  162. Persistent::delete('notices');
  163. self::$notices = array();
  164. }
  165. public static function hasErrors() {
  166. if (isset(self::$notices['error'])) {
  167. return true;
  168. }
  169. return false;
  170. }
  171. }