PluginProbe
Polylang / 3.0
Polylang v3.0
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / admin / admin-notices.php

admin-notices.php in Polylang 3.0, at admin/admin-notices.php

271 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * A class to manage admin notices
8 * displayed only to admin, based on 'manage_options' capability
9 * and only on dashboard, plugins and Polylang admin pages
10 *
11 * @since 2.3.9
12 * @since 2.7 Dismissed notices are stored in an option instead of a user meta
13 */
14 class PLL_Admin_Notices {
15 /**
16 * Stores the plugin options.
17 *
18 * @var array
19 */
20 protected $options;
21
22 /**
23 * Stores custom notices.
24 *
25 * @var string[]
26 */
27 private static $notices = array();
28
29 /**
30 * Constructor
31 * Setup actions
32 *
33 * @since 2.3.9
34 *
35 * @param object $polylang
36 */
37 public function __construct( $polylang ) {
38 $this->options = &$polylang->options;
39
40 add_action( 'admin_init', array( $this, 'hide_notice' ) );
41 add_action( 'admin_notices', array( $this, 'display_notices' ) );
42 }
43
44 /**
45 * Add a custom notice
46 *
47 * @since 2.3.9
48 *
49 * @param string $name Notice name
50 * @param string $html Content of the notice
51 * @return void
52 */
53 public static function add_notice( $name, $html ) {
54 self::$notices[ $name ] = $html;
55 }
56
57 /**
58 * Get custom notices.
59 *
60 * @since 2.3.9
61 *
62 * @return string[]
63 */
64 public static function get_notices() {
65 return self::$notices;
66 }
67
68 /**
69 * Has a notice been dismissed?
70 *
71 * @since 2.3.9
72 *
73 * @param string $notice Notice name
74 * @return bool
75 */
76 public static function is_dismissed( $notice ) {
77 $dismissed = get_option( 'pll_dismissed_notices', array() );
78
79 // Handle legacy user meta
80 $dismissed_meta = get_user_meta( get_current_user_id(), 'pll_dismissed_notices', true );
81 if ( is_array( $dismissed_meta ) ) {
82 if ( array_diff( $dismissed_meta, $dismissed ) ) {
83 $dismissed = array_merge( $dismissed, $dismissed_meta );
84 update_option( 'pll_dismissed_notices', $dismissed );
85 }
86 if ( ! is_multisite() ) {
87 // Don't delete on multisite to avoid the notices to appear in other sites.
88 delete_user_meta( get_current_user_id(), 'pll_dismissed_notices' );
89 }
90 }
91
92 return in_array( $notice, $dismissed );
93 }
94
95 /**
96 * Should we display notices on this screen?
97 *
98 * @since 2.3.9
99 *
100 * @param string $notice The notice name.
101 * @return bool
102 */
103 protected function can_display_notice( $notice ) {
104 $screen = get_current_screen();
105
106 if ( empty( $screen ) ) {
107 return false;
108 }
109
110 $screen_id = sanitize_title( __( 'Languages', 'polylang' ) );
111
112 /**
113 * Filter admin notices which can be displayed
114 *
115 * @since 2.7.0
116 *
117 * @param bool $display Whether the notice should be displayed or not.
118 * @param string $notice The notice name.
119 */
120 return apply_filters(
121 'pll_can_display_notice',
122 in_array(
123 $screen->id,
124 array(
125 'dashboard',
126 'plugins',
127 'toplevel_page_mlang',
128 $screen_id . '_page_mlang_strings',
129 $screen_id . '_page_mlang_settings',
130 )
131 ),
132 $notice
133 );
134 }
135
136 /**
137 * Stores a dismissed notice in database
138 *
139 * @since 2.3.9
140 *
141 * @param string $notice
142 * @return void
143 */
144 public static function dismiss( $notice ) {
145 $dismissed = get_option( 'pll_dismissed_notices', array() );
146
147 if ( ! in_array( $notice, $dismissed ) ) {
148 $dismissed[] = $notice;
149 update_option( 'pll_dismissed_notices', array_unique( $dismissed ) );
150 }
151 }
152
153 /**
154 * Handle a click on the dismiss button
155 *
156 * @since 2.3.9
157 *
158 * @return void
159 */
160 public function hide_notice() {
161 if ( isset( $_GET['pll-hide-notice'], $_GET['_pll_notice_nonce'] ) ) {
162 $notice = sanitize_key( $_GET['pll-hide-notice'] );
163 check_admin_referer( $notice, '_pll_notice_nonce' );
164 self::dismiss( $notice );
165 wp_safe_redirect( remove_query_arg( array( 'pll-hide-notice', '_pll_notice_nonce' ), wp_get_referer() ) );
166 exit;
167 }
168 }
169
170 /**
171 * Displays notices
172 *
173 * @since 2.3.9
174 *
175 * @return void
176 */
177 public function display_notices() {
178 if ( current_user_can( 'manage_options' ) ) {
179 // Core notices
180 if ( defined( 'WOOCOMMERCE_VERSION' ) && ! defined( 'PLLWC_VERSION' ) && $this->can_display_notice( 'pllwc' ) && ! $this->is_dismissed( 'pllwc' ) ) {
181 $this->pllwc_notice();
182 }
183
184 if ( ! defined( 'POLYLANG_PRO' ) && $this->can_display_notice( 'review' ) && ! $this->is_dismissed( 'review' ) && ! empty( $this->options['first_activation'] ) && time() > $this->options['first_activation'] + 15 * DAY_IN_SECONDS ) {
185 $this->review_notice();
186 }
187
188 // Custom notices
189 foreach ( $this->get_notices() as $notice => $html ) {
190 if ( $this->can_display_notice( $notice ) && ! $this->is_dismissed( $notice ) ) {
191 ?>
192 <div class="pll-notice notice notice-info">
193 <?php
194 $this->dismiss_button( $notice );
195 echo wp_kses_post( $html );
196 ?>
197 </div>
198 <?php
199 }
200 }
201 }
202 }
203
204 /**
205 * Displays a dismiss button
206 *
207 * @since 2.3.9
208 *
209 * @param string $name Notice name
210 * @return void
211 */
212 public function dismiss_button( $name ) {
213 printf(
214 '<a class="notice-dismiss" href="%s"><span class="screen-reader-text">%s</span></a>',
215 esc_url( wp_nonce_url( add_query_arg( 'pll-hide-notice', $name ), $name, '_pll_notice_nonce' ) ),
216 /* translators: accessibility text */
217 esc_html__( 'Dismiss this notice.', 'polylang' )
218 );
219 }
220
221 /**
222 * Displays a notice if WooCommerce is activated without Polylang for WooCommerce
223 *
224 * @since 2.3.9
225 *
226 * @return void
227 */
228 private function pllwc_notice() {
229 ?>
230 <div class="pll-notice notice notice-warning">
231 <?php $this->dismiss_button( 'pllwc' ); ?>
232 <p>
233 <?php
234 printf(
235 /* translators: %1$s is link start tag, %2$s is link end tag. */
236 esc_html__( 'We have noticed that you are using Polylang with WooCommerce. To ensure compatibility, we recommend you use %1$sPolylang for WooCommerce%2$s.', 'polylang' ),
237 '<a href="https://polylang.pro/downloads/polylang-for-woocommerce/">',
238 '</a>'
239 );
240 ?>
241 </p>
242 </div>
243 <?php
244 }
245
246 /**
247 * Displays a notice asking for a review
248 *
249 * @since 2.3.9
250 *
251 * @return void
252 */
253 private function review_notice() {
254 ?>
255 <div class="pll-notice notice notice-info">
256 <?php $this->dismiss_button( 'review' ); ?>
257 <p>
258 <?php
259 printf(
260 /* translators: %1$s is link start tag, %2$s is link end tag. */
261 esc_html__( 'We have noticed that you have been using Polylang for some time. We hope you love it, and we would really appreciate it if you would %1$sgive us a 5 stars rating%2$s.', 'polylang' ),
262 '<a href="https://wordpress.org/support/plugin/polylang/reviews/?rate=5#new-post">',
263 '</a>'
264 );
265 ?>
266 </p>
267 </div>
268 <?php
269 }
270 }
271