PluginProbe
Polylang / 2.9
Polylang v2.9
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 2.9, at admin/admin-notices.php

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