notice.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * Notices admin class.
  4. *
  5. * Handles retrieving whether a particular notice has been dismissed or not,
  6. * as well as marking a notice as dismissed.
  7. *
  8. * @since 6.0.0
  9. *
  10. * @package MonsterInsights
  11. * @subpackage Notices
  12. * @author Chris Christoff
  13. */
  14. // Exit if accessed directly
  15. if ( ! defined( 'ABSPATH' ) ) {
  16. exit;
  17. }
  18. final class MonsterInsights_Notice_Admin {
  19. /**
  20. * Holds all dismissed notices
  21. *
  22. * @access public
  23. * @since 6.0.0
  24. * @var array $notices Array of dismissed notices.
  25. */
  26. public $notices;
  27. /**
  28. * Primary class constructor.
  29. *
  30. * @access public
  31. * @since 6.0.0
  32. */
  33. public function __construct() {
  34. // Populate $notices
  35. $this->notices = get_option( 'monsterinsights_notices' );
  36. if ( ! is_array( $this->notices ) ) {
  37. $this->notices = array();
  38. }
  39. }
  40. /**
  41. * Checks if a given notice has been dismissed or not
  42. *
  43. * @access public
  44. * @since 6.0.0
  45. *
  46. * @param string $notice Programmatic Notice Name
  47. * @return bool Notice Dismissed
  48. */
  49. public function is_dismissed( $notice ) {
  50. if ( ! isset( $this->notices[ $notice ] ) ) {
  51. return false;
  52. }
  53. return true;
  54. }
  55. /**
  56. * Marks the given notice as dismissed
  57. *
  58. * @access public
  59. * @since 6.0.0
  60. *
  61. * @param string $notice Programmatic Notice Name
  62. * @return null
  63. */
  64. public function dismiss( $notice ) {
  65. $this->notices[ $notice ] = true;
  66. update_option( 'monsterinsights_notices', $this->notices );
  67. }
  68. /**
  69. * Marks a notice as not dismissed
  70. *
  71. * @access public
  72. * @since 6.0.0
  73. *
  74. * @param string $notice Programmatic Notice Name
  75. * @return null
  76. */
  77. public function undismiss( $notice ) {
  78. unset( $this->notices[ $notice ] );
  79. update_option( 'monsterinsights_notices', $this->notices );
  80. }
  81. /**
  82. * Displays an inline notice with some MonsterInsights styling.
  83. *
  84. * @access public
  85. * @since 6.0.0
  86. *
  87. * @param string $notice Programmatic Notice Name
  88. * @param string $title Title
  89. * @param string $message Message
  90. * @param string $type Message Type (updated|warning|error) - green, yellow/orange and red respectively.
  91. * @param string $button_text Button Text (optional)
  92. * @param string $button_url Button URL (optional)
  93. * @param bool $is_dismissible User can Dismiss Message (default: false)
  94. */
  95. public function display_inline_notice( $name, $title, $message, $type = 'success', $is_dismissible = false, $args = array() ) {
  96. /* Available/Planned $args options
  97. * $args = array(
  98. * 'primary' => array(
  99. * 'text' => '',
  100. * 'url' => '',
  101. * 'target' => '',
  102. * 'class' => 'button button-primary',
  103. * ),
  104. * 'secondary' => array(
  105. * 'text' => '',
  106. * 'url' => '',
  107. * 'target' => '',
  108. * 'class' => 'button button-secondary',
  109. * ),
  110. * 'skip_message_escape' => true // note: This param is for internal use only. Do not use as a developer.
  111. * );
  112. */
  113. // Check if the notice is dismissible, and if so has been dismissed.
  114. if ( $is_dismissible && $this->is_dismissed( $name ) ) {
  115. // Nothing to show here, return!
  116. return '';
  117. }
  118. $dismissible = ( $is_dismissible ) ? ' is-dismissible': '';
  119. // Display inline notice
  120. ob_start();
  121. ?>
  122. <div class="monsterinsights-notice <?php echo 'monsterinsights-' . esc_attr( $type ) . '-notice' . $dismissible; ?>" data-notice="<?php echo esc_attr( $name ); ?>">
  123. <div class="monsterinsights-notice-icon <?php echo 'monsterinsights-' . esc_attr( $type ) . '-notice-icon'?>">
  124. </div>
  125. <div class="monsterinsights-notice-text <?php echo 'monsterinsights-' . esc_attr( $type ) . '-notice-text'?>">
  126. <?php
  127. // Title
  128. if ( ! empty ( $title ) ) {
  129. ?>
  130. <p class="monsterinsights-notice-title"><?php echo esc_html( $title ); ?></p>
  131. <?php
  132. }
  133. // Message
  134. if ( ! empty( $message ) ) {
  135. if ( empty( $args['skip_message_escape'] ) ) {
  136. ?>
  137. <p class="monsterinsights-notice-message"><?php echo esc_html( $message ); ?></p>
  138. <?php
  139. } else {
  140. ?>
  141. <p class="monsterinsights-notice-message"><?php echo $message; ?></p>
  142. <?php
  143. }
  144. }
  145. // Primary Button
  146. if ( ! empty( $args['primary']['text'] ) ) {
  147. $text = '';
  148. if ( ! empty( $args['primary']['text'] ) ) {
  149. $text = $args['primary']['text'];
  150. }
  151. $url = '#';
  152. if ( ! empty( $args['primary']['url'] ) ) {
  153. $url = $args['primary']['url'];
  154. }
  155. $target = '';
  156. if ( ! empty( $args['primary']['target'] ) && $args['primary']['target'] === 'blank') {
  157. $target = ' target="_blank" rel="noopener noreferrer"';
  158. }
  159. $class = 'button button-primary';
  160. if ( ! empty( $args['primary']['class'] ) ) {
  161. $class = ' class="'. $args['primary']['class'] . '"';
  162. }
  163. ?>
  164. <a href="<?php echo esc_attr( $url ); ?>"<?php echo $target; ?><?php echo $class;?>><?php echo esc_html( $text ); ?></a>
  165. <?php
  166. }
  167. // Secondary Button
  168. if ( ! empty( $args['secondary']['text'] ) ) {
  169. $text = '';
  170. if ( ! empty( $args['secondary']['text'] ) ) {
  171. $text = $args['secondary']['text'];
  172. }
  173. $url = '#';
  174. if ( ! empty( $args['secondary']['url'] ) ) {
  175. $url = $args['secondary']['url'];
  176. }
  177. $target = '';
  178. if ( ! empty( $args['secondary']['target'] ) && $args['secondary']['target'] === 'blank') {
  179. $target = ' target="_blank" rel="noopener noreferrer"';
  180. }
  181. $class = 'button button-secondary';
  182. if ( ! empty( $args['secondary']['class'] ) ) {
  183. $class = ' class="'. $args['secondary']['class'] . '"';
  184. }
  185. ?>
  186. <a href="<?php echo esc_attr( $url ); ?>"<?php echo $target; ?><?php echo $class;?>><?php echo esc_html( $text ); ?></a>
  187. <?php
  188. }
  189. // Dismiss Button
  190. if ( $is_dismissible ) {
  191. ?>
  192. <button type="button" class="notice-dismiss<?php echo $dismissible; ?>">
  193. <span class="screen-reader-text">
  194. <?php esc_html_e( 'Dismiss this notice', 'google-analytics-for-wordpress' ); ?>
  195. </span>
  196. </button>
  197. <?php
  198. }
  199. ?>
  200. </div>
  201. </div>
  202. <?php
  203. return ob_get_clean();
  204. }
  205. }