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