admin-notices.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * ACF Admin Notices
  4. *
  5. * Functions and classes to manage admin notices.
  6. *
  7. * @date 10/1/19
  8. * @since 5.7.10
  9. */
  10. // Exit if accessed directly.
  11. if ( ! defined( 'ABSPATH' ) ) {
  12. exit;
  13. }
  14. // Register notices store.
  15. acf_register_store( 'notices' );
  16. /**
  17. * ACF_Admin_Notice
  18. *
  19. * Class used to create an admin notice.
  20. *
  21. * @date 10/1/19
  22. * @since 5.7.10
  23. */
  24. if ( ! class_exists( 'ACF_Admin_Notice' ) ) :
  25. class ACF_Admin_Notice extends ACF_Data {
  26. /** @var array Storage for data. */
  27. var $data = array(
  28. /** @type string Text displayed in notice. */
  29. 'text' => '',
  30. /** @type string The type of notice (warning, error, success, info). */
  31. 'type' => 'info',
  32. /** @type bool If the notice can be dismissed. */
  33. 'dismissible' => true,
  34. );
  35. /**
  36. * render
  37. *
  38. * Renders the notice HTML.
  39. *
  40. * @date 27/12/18
  41. * @since 5.8.0
  42. *
  43. * @param void
  44. * @return void
  45. */
  46. function render() {
  47. $notice_text = $this->get( 'text' );
  48. $notice_type = $this->get( 'type' );
  49. $is_dismissible = $this->get( 'dismissible' );
  50. printf(
  51. '<div class="acf-admin-notice notice notice-%s %s">%s</div>',
  52. esc_attr( $notice_type ),
  53. $is_dismissible ? 'is-dismissible' : '',
  54. acf_esc_html( wpautop( acf_punctify( $notice_text ) ) )
  55. );
  56. }
  57. }
  58. endif; // class_exists check
  59. /**
  60. * acf_new_admin_notice
  61. *
  62. * Instantiates and returns a new model.
  63. *
  64. * @date 23/12/18
  65. * @since 5.8.0
  66. *
  67. * @param array $data Optional data to set.
  68. * @return ACF_Admin_Notice
  69. */
  70. function acf_new_admin_notice( $data = false ) {
  71. // Create notice.
  72. $instance = new ACF_Admin_Notice( $data );
  73. // Register notice.
  74. acf_get_store( 'notices' )->set( $instance->cid, $instance );
  75. // Return notice.
  76. return $instance;
  77. }
  78. /**
  79. * acf_render_admin_notices
  80. *
  81. * Renders all admin notices HTML.
  82. *
  83. * @date 10/1/19
  84. * @since 5.7.10
  85. *
  86. * @param void
  87. * @return void
  88. */
  89. function acf_render_admin_notices() {
  90. // Get notices.
  91. $notices = acf_get_store( 'notices' )->get_data();
  92. // Loop over notices and render.
  93. if ( $notices ) {
  94. foreach ( $notices as $notice ) {
  95. $notice->render();
  96. }
  97. }
  98. }
  99. // Render notices during admin action.
  100. add_action( 'admin_notices', 'acf_render_admin_notices', 99 );
  101. /**
  102. * acf_add_admin_notice
  103. *
  104. * Creates and returns a new notice.
  105. *
  106. * @date 17/10/13
  107. * @since 5.0.0
  108. *
  109. * @param string $text The admin notice text.
  110. * @param string $class The type of notice (warning, error, success, info).
  111. * @param string $dismissable Is this notification dismissible (default true) (since 5.11.0)
  112. * @return ACF_Admin_Notice
  113. */
  114. function acf_add_admin_notice( $text = '', $type = 'info', $dismissible = true ) {
  115. return acf_new_admin_notice(
  116. array(
  117. 'text' => $text,
  118. 'type' => $type,
  119. 'dismissible' => $dismissible,
  120. )
  121. );
  122. }