FS_Admin_Notice_Manager

The NextGEN Gallery FS Admin Notice Manager class.

Defined (1)

The class is defined in the following location(s).

/freemius/includes/managers/class-fs-admin-notice-manager.php  
  1. class FS_Admin_Notice_Manager { 
  2. /** 
  3. * @var string 
  4. */ 
  5. protected $_slug; 
  6. /** 
  7. * @var string 
  8. */ 
  9. protected $_title; 
  10. /** 
  11. * @var array[] 
  12. */ 
  13. private $_admin_messages = array(); 
  14. /** 
  15. * @var FS_Key_Value_Storage 
  16. */ 
  17. private $_sticky_storage; 
  18. /** 
  19. * @var FS_Plugin_Manager[] 
  20. */ 
  21. private static $_instances = array(); 
  22. /** 
  23. * @var FS_Logger 
  24. */ 
  25. protected $_logger; 
  26.  
  27. /** 
  28. * @param string $slug 
  29. * @param string $title 
  30. * @return FS_Admin_Notice_Manager 
  31. */ 
  32. static function instance( $slug, $title = '' ) { 
  33. if ( ! isset( self::$_instances[ $slug ] ) ) { 
  34. self::$_instances[ $slug ] = new FS_Admin_Notice_Manager( $slug, $title ); 
  35.  
  36. return self::$_instances[ $slug ]; 
  37.  
  38. protected function __construct( $slug, $title = '' ) { 
  39. $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $slug . '_data', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK ); 
  40.  
  41. $this->_slug = $slug; 
  42. $this->_title = ! empty( $title ) ? $title : ''; 
  43. $this->_sticky_storage = FS_Key_Value_Storage::instance( 'admin_notices', $this->_slug ); 
  44.  
  45. if ( is_admin() ) { 
  46. if ( 0 < count( $this->_sticky_storage ) ) { 
  47. // If there are sticky notices for the current slug, add a callback 
  48. // to the AJAX action that handles message dismiss. 
  49. add_action( "wp_ajax_fs_dismiss_notice_action_{$slug}", array( 
  50. &$this,  
  51. 'dismiss_notice_ajax_callback' 
  52. ) ); 
  53.  
  54. foreach ( $this->_sticky_storage as $id => $msg ) { 
  55. // Add admin notice. 
  56. $this->add( 
  57. $msg['message'],  
  58. $msg['title'],  
  59. $msg['type'],  
  60. true,  
  61. $msg['all'],  
  62. $msg['id'],  
  63. false 
  64. ); 
  65.  
  66. /** 
  67. * Remove sticky message by ID. 
  68. * @author Vova Feldman (@svovaf) 
  69. * @since 1.0.7 
  70. */ 
  71. function dismiss_notice_ajax_callback() { 
  72. $this->_sticky_storage->remove( $_POST['message_id'] ); 
  73. wp_die(); 
  74.  
  75. /** 
  76. * Rendered sticky message dismiss JavaScript. 
  77. * @author Vova Feldman (@svovaf) 
  78. * @since 1.0.7 
  79. */ 
  80. static function _add_sticky_dismiss_javascript() { 
  81. $params = array(); 
  82. fs_require_once_template( 'sticky-admin-notice-js.php', $params ); 
  83.  
  84. private static $_added_sticky_javascript = false; 
  85.  
  86. /** 
  87. * Hook to the admin_footer to add sticky message dismiss JavaScript handler. 
  88. * @author Vova Feldman (@svovaf) 
  89. * @since 1.0.7 
  90. */ 
  91. private static function has_sticky_messages() { 
  92. if ( ! self::$_added_sticky_javascript ) { 
  93. add_action( 'admin_footer', array( 'FS_Admin_Notice_Manager', '_add_sticky_dismiss_javascript' ) ); 
  94.  
  95. /** 
  96. * Handle admin_notices by printing the admin messages stacked in the queue. 
  97. * @author Vova Feldman (@svovaf) 
  98. * @since 1.0.4 
  99. */ 
  100. function _admin_notices_hook() { 
  101. $notice_type = 'admin_notices'; 
  102.  
  103. if ( function_exists( 'current_user_can' ) && 
  104. ! current_user_can( 'manage_options' ) 
  105. ) { 
  106. // Only show messages to admins. 
  107. return; 
  108.  
  109. if ( ! isset( $this->_admin_messages[ $notice_type ] ) || ! is_array( $this->_admin_messages[ $notice_type ] ) ) { 
  110. return; 
  111.  
  112. foreach ( $this->_admin_messages[ $notice_type ] as $id => $msg ) { 
  113. fs_require_template( 'admin-notice.php', $msg ); 
  114.  
  115. if ( $msg['sticky'] ) { 
  116. self::has_sticky_messages(); 
  117.  
  118. /** 
  119. * Handle all_admin_notices by printing the admin messages stacked in the queue. 
  120. * @author Vova Feldman (@svovaf) 
  121. * @since 1.0.4 
  122. */ 
  123. function _all_admin_notices_hook() { 
  124. $notice_type = 'all_admin_notices'; 
  125.  
  126. if ( ! isset( $this->_admin_messages[ $notice_type ] ) || ! is_array( $this->_admin_messages[ $notice_type ] ) ) { 
  127. return; 
  128.  
  129. foreach ( $this->_admin_messages[ $notice_type ] as $id => $msg ) { 
  130. fs_require_template( 'all-admin-notice.php', $msg ); 
  131.  
  132. /** 
  133. * Enqueue common stylesheet to style admin notice. 
  134. * @author Vova Feldman (@svovaf) 
  135. * @since 1.0.7 
  136. */ 
  137. function _enqueue_styles() { 
  138. fs_enqueue_local_style( 'fs_common', '/admin/common.css' ); 
  139.  
  140. /** 
  141. * Add admin message to admin messages queue, and hook to admin_notices / all_admin_notices if not yet hooked. 
  142. * @author Vova Feldman (@svovaf) 
  143. * @since 1.0.4 
  144. * @param string $message 
  145. * @param string $title 
  146. * @param string $type 
  147. * @param bool $is_sticky 
  148. * @param bool $all_admin 
  149. * @param string $id Message ID 
  150. * @param bool $store_if_sticky 
  151. * @uses add_action() 
  152. */ 
  153. function add( $message, $title = '', $type = 'success', $is_sticky = false, $all_admin = false, $id = '', $store_if_sticky = true ) { 
  154. $key = ( $all_admin ? 'all_admin_notices' : 'admin_notices' ); 
  155.  
  156. if ( ! isset( $this->_admin_messages[ $key ] ) ) { 
  157. $this->_admin_messages[ $key ] = array(); 
  158.  
  159. add_action( $key, array( &$this, "_{$key}_hook" ) ); 
  160. add_action( 'admin_enqueue_scripts', array( &$this, '_enqueue_styles' ) ); 
  161.  
  162.  
  163. if ( '' === $id ) { 
  164. $id = md5( $title . ' ' . $message . ' ' . $type ); 
  165.  
  166. $message_object = array( 
  167. 'message' => $message,  
  168. 'title' => $title,  
  169. 'type' => $type,  
  170. 'sticky' => $is_sticky,  
  171. 'id' => $id,  
  172. 'all' => $all_admin,  
  173. 'slug' => $this->_slug,  
  174. 'plugin' => $this->_title,  
  175. ); 
  176.  
  177. if ( $is_sticky && $store_if_sticky ) { 
  178. $this->_sticky_storage->{$id} = $message_object; 
  179.  
  180. $this->_admin_messages[ $key ][ $id ] = $message_object; 
  181.  
  182. /** 
  183. * @author Vova Feldman (@svovaf) 
  184. * @since 1.0.7 
  185. * @param string|string[] $ids 
  186. */ 
  187. function remove_sticky( $ids ) { 
  188. if ( ! is_array( $ids ) ) { 
  189. $ids = array( $ids ); 
  190.  
  191. foreach ( $ids as $id ) { 
  192. // Remove from sticky storage. 
  193. $this->_sticky_storage->remove( $id ); 
  194.  
  195. // Remove from current admin messages. 
  196. if ( isset( $this->_admin_messages['all_admin_notices'] ) && isset( $this->_admin_messages['all_admin_notices'][ $id ] ) ) { 
  197. unset( $this->_admin_messages['all_admin_notices'][ $id ] ); 
  198. if ( isset( $this->_admin_messages['admin_notices'] ) && isset( $this->_admin_messages['admin_notices'][ $id ] ) ) { 
  199. unset( $this->_admin_messages['admin_notices'][ $id ] ); 
  200.  
  201. /** 
  202. * Check if sticky message exists by id. 
  203. * @author Vova Feldman (@svovaf) 
  204. * @since 1.0.9 
  205. * @param $id 
  206. * @return bool 
  207. */ 
  208. function has_sticky( $id ) { 
  209. return isset( $this->_sticky_storage[ $id ] ); 
  210.  
  211. /** 
  212. * Adds sticky admin notification. 
  213. * @author Vova Feldman (@svovaf) 
  214. * @since 1.0.7 
  215. * @param string $message 
  216. * @param string $id Message ID 
  217. * @param string $title 
  218. * @param string $type 
  219. * @param bool $all_admin 
  220. */ 
  221. function add_sticky( $message, $id, $title = '', $type = 'success', $all_admin = false ) { 
  222. $message = fs_apply_filter( $this->_slug, "sticky_message_{$id}", $message ); 
  223. $title = fs_apply_filter( $this->_slug, "sticky_title_{$id}", $title ); 
  224.  
  225. $this->add( $message, $title, $type, true, $all_admin, $id ); 
  226.  
  227. /** 
  228. * Clear all sticky messages. 
  229. * @author Vova Feldman (@svovaf) 
  230. * @since 1.0.8 
  231. */ 
  232. function clear_all_sticky() { 
  233. $this->_sticky_storage->clear_all(); 
  234.  
  235. /** 
  236. * Add admin message to all admin messages queue, and hook to all_admin_notices if not yet hooked. 
  237. * @author Vova Feldman (@svovaf) 
  238. * @since 1.0.4 
  239. * @param string $message 
  240. * @param string $title 
  241. * @param string $type 
  242. * @param bool $is_sticky 
  243. * @param string $id Message ID 
  244. */ 
  245. function add_all( $message, $title = '', $type = 'success', $is_sticky = false, $id = '' ) { 
  246. $this->add( $message, $title, $type, $is_sticky, true, $id );