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

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