/wp-content/plugins/the-events-calendar/common/vendor/freemius/includes/class-fs-admin-notices.php

https://github.com/livinglab/openlab · PHP · 321 lines · 144 code · 28 blank · 149 comment · 23 complexity · adadd160d12b74d495bbf6319e23d432 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Freemius
  4. * @copyright Copyright (c) 2015, Freemius, Inc.
  5. * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
  6. * @since 2.0.0
  7. */
  8. if ( ! defined( 'ABSPATH' ) ) {
  9. exit;
  10. }
  11. /**
  12. * WP Admin notices manager both for site level and network level.
  13. *
  14. * Class FS_Admin_Notices
  15. */
  16. class FS_Admin_Notices {
  17. /**
  18. * @since 1.2.2
  19. *
  20. * @var string
  21. */
  22. protected $_module_unique_affix;
  23. /**
  24. * @var string
  25. */
  26. protected $_id;
  27. /**
  28. * @var string
  29. */
  30. protected $_title;
  31. /**
  32. * @var FS_Admin_Notice_Manager
  33. */
  34. protected $_notices;
  35. /**
  36. * @var FS_Admin_Notice_Manager
  37. */
  38. protected $_network_notices;
  39. /**
  40. * @var int The ID of the blog that is associated with the current site level options.
  41. */
  42. private $_blog_id = 0;
  43. /**
  44. * @var bool
  45. */
  46. private $_is_multisite;
  47. /**
  48. * @var FS_Admin_Notices[]
  49. */
  50. private static $_instances = array();
  51. /**
  52. * @param string $id
  53. * @param string $title
  54. * @param string $module_unique_affix
  55. * @param bool $is_network_and_blog_admins Whether or not the message should be shown both on network and
  56. * blog admin pages.
  57. *
  58. * @return FS_Admin_Notices
  59. */
  60. static function instance( $id, $title = '', $module_unique_affix = '', $is_network_and_blog_admins = false ) {
  61. if ( ! isset( self::$_instances[ $id ] ) ) {
  62. self::$_instances[ $id ] = new FS_Admin_Notices( $id, $title, $module_unique_affix, $is_network_and_blog_admins );
  63. }
  64. return self::$_instances[ $id ];
  65. }
  66. /**
  67. * @param string $id
  68. * @param string $title
  69. * @param string $module_unique_affix
  70. * @param bool $is_network_and_blog_admins Whether or not the message should be shown both on network and
  71. * blog admin pages.
  72. */
  73. protected function __construct( $id, $title = '', $module_unique_affix = '', $is_network_and_blog_admins = false ) {
  74. $this->_id = $id;
  75. $this->_title = $title;
  76. $this->_module_unique_affix = $module_unique_affix;
  77. $this->_is_multisite = is_multisite();
  78. if ( $this->_is_multisite ) {
  79. $this->_blog_id = get_current_blog_id();
  80. $this->_network_notices = FS_Admin_Notice_Manager::instance(
  81. $id,
  82. $title,
  83. $module_unique_affix,
  84. $is_network_and_blog_admins,
  85. true
  86. );
  87. }
  88. $this->_notices = FS_Admin_Notice_Manager::instance(
  89. $id,
  90. $title,
  91. $module_unique_affix,
  92. false,
  93. $this->_blog_id
  94. );
  95. }
  96. /**
  97. * Add admin message to admin messages queue, and hook to admin_notices / all_admin_notices if not yet hooked.
  98. *
  99. * @author Vova Feldman (@svovaf)
  100. * @since 1.0.4
  101. *
  102. * @param string $message
  103. * @param string $title
  104. * @param string $type
  105. * @param bool $is_sticky
  106. * @param string $id Message ID
  107. * @param bool $store_if_sticky
  108. * @param int|null $network_level_or_blog_id
  109. *
  110. * @uses add_action()
  111. */
  112. function add(
  113. $message,
  114. $title = '',
  115. $type = 'success',
  116. $is_sticky = false,
  117. $id = '',
  118. $store_if_sticky = true,
  119. $network_level_or_blog_id = null
  120. ) {
  121. if ( $this->should_use_network_notices( $id, $network_level_or_blog_id ) ) {
  122. $notices = $this->_network_notices;
  123. } else {
  124. $notices = $this->get_site_notices( $network_level_or_blog_id );
  125. }
  126. $notices->add(
  127. $message,
  128. $title,
  129. $type,
  130. $is_sticky,
  131. $id,
  132. $store_if_sticky
  133. );
  134. }
  135. /**
  136. * @author Vova Feldman (@svovaf)
  137. * @since 1.0.7
  138. *
  139. * @param string|string[] $ids
  140. * @param int|null $network_level_or_blog_id
  141. */
  142. function remove_sticky( $ids, $network_level_or_blog_id = null ) {
  143. if ( ! is_array( $ids ) ) {
  144. $ids = array( $ids );
  145. }
  146. if ( $this->should_use_network_notices( $ids[0], $network_level_or_blog_id ) ) {
  147. $notices = $this->_network_notices;
  148. } else {
  149. $notices = $this->get_site_notices( $network_level_or_blog_id );
  150. }
  151. return $notices->remove_sticky( $ids );
  152. }
  153. /**
  154. * Check if sticky message exists by id.
  155. *
  156. * @author Vova Feldman (@svovaf)
  157. * @since 1.0.9
  158. *
  159. * @param string $id
  160. * @param int|null $network_level_or_blog_id
  161. *
  162. * @return bool
  163. */
  164. function has_sticky( $id, $network_level_or_blog_id = null ) {
  165. if ( $this->should_use_network_notices( $id, $network_level_or_blog_id ) ) {
  166. $notices = $this->_network_notices;
  167. } else {
  168. $notices = $this->get_site_notices( $network_level_or_blog_id );
  169. }
  170. return $notices->has_sticky( $id );
  171. }
  172. /**
  173. * Adds sticky admin notification.
  174. *
  175. * @author Vova Feldman (@svovaf)
  176. * @since 1.0.7
  177. *
  178. * @param string $message
  179. * @param string $id Message ID
  180. * @param string $title
  181. * @param string $type
  182. * @param int|null $network_level_or_blog_id
  183. * @param number|null $wp_user_id
  184. * @param string|null $plugin_title
  185. * @param bool $is_network_and_blog_admins Whether or not the message should be shown both on network and
  186. * blog admin pages.
  187. */
  188. function add_sticky(
  189. $message,
  190. $id,
  191. $title = '',
  192. $type = 'success',
  193. $network_level_or_blog_id = null,
  194. $wp_user_id = null,
  195. $plugin_title = null,
  196. $is_network_and_blog_admins = false
  197. ) {
  198. if ( $this->should_use_network_notices( $id, $network_level_or_blog_id ) ) {
  199. $notices = $this->_network_notices;
  200. } else {
  201. $notices = $this->get_site_notices( $network_level_or_blog_id );
  202. }
  203. $notices->add_sticky( $message, $id, $title, $type, $wp_user_id, $plugin_title, $is_network_and_blog_admins );
  204. }
  205. /**
  206. * Clear all sticky messages.
  207. *
  208. * @author Vova Feldman (@svovaf)
  209. * @since 2.0.0
  210. *
  211. * @param int|null $network_level_or_blog_id
  212. */
  213. function clear_all_sticky( $network_level_or_blog_id = null ) {
  214. if ( ! $this->_is_multisite ||
  215. false === $network_level_or_blog_id ||
  216. 0 == $network_level_or_blog_id ||
  217. is_null( $network_level_or_blog_id )
  218. ) {
  219. $notices = $this->get_site_notices( $network_level_or_blog_id );
  220. $notices->clear_all_sticky();
  221. }
  222. if ( $this->_is_multisite &&
  223. ( true === $network_level_or_blog_id || is_null( $network_level_or_blog_id ) )
  224. ) {
  225. $this->_network_notices->clear_all_sticky();
  226. }
  227. }
  228. /**
  229. * Add admin message to all admin messages queue, and hook to all_admin_notices if not yet hooked.
  230. *
  231. * @author Vova Feldman (@svovaf)
  232. * @since 1.0.4
  233. *
  234. * @param string $message
  235. * @param string $title
  236. * @param string $type
  237. * @param bool $is_sticky
  238. * @param string $id Message ID
  239. */
  240. function add_all( $message, $title = '', $type = 'success', $is_sticky = false, $id = '' ) {
  241. $this->add( $message, $title, $type, $is_sticky, true, $id );
  242. }
  243. #--------------------------------------------------------------------------------
  244. #region Helper Methods
  245. #--------------------------------------------------------------------------------
  246. /**
  247. * @author Vova Feldman (@svovaf)
  248. * @since 2.0.0
  249. *
  250. * @param int $blog_id
  251. *
  252. * @return FS_Admin_Notice_Manager
  253. */
  254. private function get_site_notices( $blog_id = 0 ) {
  255. if ( 0 == $blog_id || $blog_id == $this->_blog_id ) {
  256. return $this->_notices;
  257. }
  258. return FS_Admin_Notice_Manager::instance(
  259. $this->_id,
  260. $this->_title,
  261. $this->_module_unique_affix,
  262. false,
  263. $blog_id
  264. );
  265. }
  266. /**
  267. * Check if the network notices should be used.
  268. *
  269. * @author Vova Feldman (@svovaf)
  270. * @since 2.0.0
  271. *
  272. * @param string $id
  273. * @param null|bool|int $network_level_or_blog_id When an integer, use the given blog storage. When `true` use the multisite notices (if there's a network). When `false`, use the current context blog notices. When `null`, the decision which notices manager to use (MS vs. Current S) will be handled internally and determined based on the $id and the context admin (blog admin vs. network level admin).
  274. *
  275. * @return bool
  276. */
  277. private function should_use_network_notices( $id = '', $network_level_or_blog_id = null ) {
  278. if ( ! $this->_is_multisite ) {
  279. // Not a multisite environment.
  280. return false;
  281. }
  282. if ( is_numeric( $network_level_or_blog_id ) ) {
  283. // Explicitly asked to use a specified blog storage.
  284. return false;
  285. }
  286. if ( is_bool( $network_level_or_blog_id ) ) {
  287. // Explicitly specified whether should use the network or blog level storage.
  288. return $network_level_or_blog_id;
  289. }
  290. return fs_is_network_admin();
  291. }
  292. #endregion
  293. }