PluginProbe
OPcache Manager / 3.3.0
OPcache Manager v3.3.0
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 2.0.0 2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.14.0 2.2.0 2.3.0 2.3.1 2.3.2 2.4.0 2.5.0 2.6.0 All 36 releases
opcache-manager / includes / system / class-nag.php

class-nag.php in OPcache Manager 3.3.0, at includes/system/class-nag.php

151 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Nag handling
4 *
5 * Handles all nag operations.
6 * Please, use these features with respect for users.
7 * Don't hijack the admin dashboard!
8 *
9 * @package System
10 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
11 * @since 1.0.0
12 */
13
14 namespace OPcacheManager\System;
15
16 /**
17 * Define the nag functionality.
18 *
19 * Handles all nag operations. Note this nag feature respects the
20 * DISABLE_NAG_NOTICES unofficial "standard".
21 *
22 * @package System
23 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
24 * @since 1.0.0
25 */
26 class Nag {
27
28 /**
29 * The nags list.
30 *
31 * @since 1.0.0
32 * @access private
33 * @var array $nags The nags list.
34 */
35 private static $nags = [];
36
37 /**
38 * Indicates whether nags are allowed or not.
39 *
40 * @since 1.0.0
41 * @access private
42 * @var boolean $allowed Is nags allowed?
43 */
44 private static $allowed = true;
45
46 /**
47 * Verify if nags are allowed and if yes, load the nags.
48 *
49 * @since 1.0.0
50 */
51 public static function init() {
52 if ( defined( 'DISABLE_NAG_NOTICES' ) ) {
53 self::$allowed = ! DISABLE_NAG_NOTICES;
54 }
55 if ( self::$allowed ) {
56 self::$allowed = Option::network_get( 'display_nag' );
57 }
58 if ( self::$allowed ) {
59 self::$nags = Option::network_get( 'nags' );
60 }
61 }
62
63 /**
64 * Adds a Nag.
65 *
66 * @param string $id Id of the nag.
67 * @param string $type Type of the nag ('error', 'warning', 'success' or 'info').
68 * @param string $value Value to display.
69 * @since 1.0.0
70 */
71 public static function add( $id, $type, $value ) {
72 self::$nags[ $id ] = [
73 'type' => $type,
74 'value' => $value,
75 ];
76 Option::network_set( 'nags', self::$nags );
77 }
78
79 /**
80 * Deletes a Nag.
81 *
82 * @param string $id Id of the nag.
83 * @since 1.0.0
84 */
85 public static function delete( $id ) {
86 if ( array_key_exists( $id, self::$nags ) ) {
87 unset( self::$nags[ $id ] );
88 Option::network_set( 'nags', self::$nags );
89 }
90 }
91
92 /**
93 * Initializes the class and set its properties.
94 *
95 * @since 1.0.0
96 */
97 public function __construct() {
98 }
99
100 /**
101 * Show all available notices.
102 *
103 * @since 1.0.0
104 */
105 public function display() {
106 if ( self::$allowed ) {
107 foreach ( self::$nags as $key => $nag ) {
108 $nonce_action = sanitize_key( $key );
109 $nonce_name = str_replace( [ '-', '_' ], '', $nonce_action );
110 $nonce = wp_nonce_field( $nonce_action, $nonce_name, false, false );
111 $div_id = 'opcm-' . $nonce_name;
112 $div_class = 'notice notice-' . $nag['type'] . ' is-dismissible';
113 $text = wp_kses(
114 $nag['value'],
115 [
116 'a' => [
117 'href' => true,
118 ],
119 'br' => true,
120 'em' => true,
121 'strong' => true,
122 ]
123 );
124 $html = '<div id="' . $div_id . '" class="' . $div_class . '">' . $nonce . '<p>' . $text . '</p></div>';
125 $js = '<script>jQuery(document).ready(function($){$("#' . $div_id . '").on("click", ".notice-dismiss", function(event){$.post(ajaxurl,{action: "hide_opcm_nag",' . $nonce_name . ': $("#' . $nonce_name . '").val()});});});</script>';
126 // phpcs:ignore
127 print( $html . $js );
128 }
129 }
130 }
131
132 /**
133 * Ajax handler for updating the displaying status of notices.
134 *
135 * @since 1.0.0
136 */
137 public static function hide_callback() {
138 foreach ( self::$nags as $key => $nag ) {
139 $nonce_action = sanitize_key( $key );
140 $nonce_name = str_replace( [ '-', '_' ], '', $nonce_action );
141 if ( false !== check_ajax_referer( $nonce_action, $nonce_name, false ) ) {
142 self::delete( $key );
143 wp_die( 200 );
144 }
145 }
146 wp_die( 409 );
147 }
148 }
149
150 Nag::init();
151