| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class Give_i18n |
| 5 |
*/ |
| 6 |
class Give_i18n_Banner { |
| 7 |
|
| 8 |
/** |
| 9 |
* Your translation site's URL. |
| 10 |
* |
| 11 |
* @var string |
| 12 |
*/ |
| 13 |
private $glotpress_url; |
| 14 |
|
| 15 |
/** |
| 16 |
* Hook where you want to show the promo box. |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
private $hook; |
| 21 |
|
| 22 |
/** |
| 23 |
* Will contain the site's locale. |
| 24 |
* |
| 25 |
* @access private |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
private $locale; |
| 29 |
|
| 30 |
/** |
| 31 |
* Will contain the locale's name, obtained from your translation site. |
| 32 |
* |
| 33 |
* @access private |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
private $locale_name; |
| 37 |
|
| 38 |
/** |
| 39 |
* Will contain the percentage translated for the plugin translation project in the locale. |
| 40 |
* |
| 41 |
* @access private |
| 42 |
* @var int |
| 43 |
*/ |
| 44 |
private $percent_translated; |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* Indicates whether there's a translation available at all. |
| 49 |
* |
| 50 |
* @access private |
| 51 |
* @var bool |
| 52 |
*/ |
| 53 |
private $translation_exists; |
| 54 |
|
| 55 |
/** |
| 56 |
* Indicates whether the translation's loaded. |
| 57 |
* |
| 58 |
* @access private |
| 59 |
* @var bool |
| 60 |
*/ |
| 61 |
private $translation_loaded; |
| 62 |
|
| 63 |
/** |
| 64 |
* Give_i18n constructor. |
| 65 |
* |
| 66 |
* @param $args |
| 67 |
*/ |
| 68 |
public function __construct( $args ) { |
| 69 |
|
| 70 |
// Only for admins. |
| 71 |
if ( ! is_admin() ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
foreach ( $args as $key => $arg ) { |
| 76 |
$this->$key = $arg; |
| 77 |
} |
| 78 |
|
| 79 |
add_action( 'admin_init', array( $this, 'init' ) ); |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Initialize i18n banner. |
| 85 |
*/ |
| 86 |
function init() { |
| 87 |
|
| 88 |
// First get user's locale (4.7+). |
| 89 |
$this->locale = function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale(); |
| 90 |
|
| 91 |
// This plugin is en_US native. |
| 92 |
if ( 'en_US' === $this->locale ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
if ( |
| 97 |
! $this->hide_promo() |
| 98 |
&& ( ! empty( $_GET['post_type'] ) && 'give_forms' === $_GET['post_type'] ) |
| 99 |
&& ( ! empty( $_GET['page'] ) && 'give-settings' === $_GET['page'] ) |
| 100 |
) { |
| 101 |
add_action( $this->hook, array( $this, 'promo' ) ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
/** |
| 107 |
* Check whether the promo should be hidden or not. |
| 108 |
* |
| 109 |
* @access private |
| 110 |
* |
| 111 |
* @return bool |
| 112 |
*/ |
| 113 |
private function hide_promo() { |
| 114 |
$hide_promo = Give_Cache::get( 'give_i18n_give_promo_hide', true ); |
| 115 |
if ( ! $hide_promo ) { |
| 116 |
if ( filter_input( INPUT_GET, 'remove_i18n_promo', FILTER_VALIDATE_INT ) === 1 ) { |
| 117 |
// No expiration time, so this would normally not expire, but it wouldn't be copied to other sites etc. |
| 118 |
Give_Cache::set( 'give_i18n_give_promo_hide', true, null, true ); |
| 119 |
$hide_promo = true; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return $hide_promo; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Generates a promo message. |
| 128 |
* |
| 129 |
* @access private |
| 130 |
* |
| 131 |
* @return bool|string $message |
| 132 |
*/ |
| 133 |
private function promo_message() { |
| 134 |
$message = false; |
| 135 |
|
| 136 |
// Using a translation less than 90% complete. |
| 137 |
if ( $this->translation_exists && $this->translation_loaded && $this->percent_translated < 90 ) { |
| 138 |
$message = __( 'As you can see, there is a translation of this plugin in %1$s. This translation is currently %3$d%% complete. We need your help to make it complete and to fix any errors. Please register at %4$s to help %5$s to %1$s!', 'give' ); |
| 139 |
} elseif ( ! $this->translation_loaded && $this->translation_exists ) { |
| 140 |
$message = __( 'You\'re using WordPress in %1$s. While %2$s has been %3$d%% translated to %1$s, it has not been shipped with the plugin yet. You can help! Register at %4$s to help complete the translation to %1$s!', 'give' ); |
| 141 |
} elseif ( ! $this->translation_exists ) { |
| 142 |
$message = __( 'You\'re using WordPress in a language we don\'t support yet. We\'d love for %2$s to be translated in that language too, but unfortunately, it isn\'t right now. You can change that! Register at %4$s to help translate it!', 'give' ); |
| 143 |
} |
| 144 |
|
| 145 |
// Links. |
| 146 |
$registration_link = sprintf( '<a href="%1$s" target="_blank">%2$s</a>', 'https://wordpress.org/support/register.php', esc_html__( 'WordPress.org', 'give' ) ); |
| 147 |
$translations_link = sprintf( '<a href="%1$s" target="_blank">%2$s</a>', 'https://translate.wordpress.org/projects/wp-plugins/give', esc_html__( 'complete the translation', 'give' ) ); |
| 148 |
|
| 149 |
// Message. |
| 150 |
$message = sprintf( $message, $this->locale_name, 'Give', $this->percent_translated, $registration_link, $translations_link ); |
| 151 |
|
| 152 |
return $message; |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Outputs a promo box |
| 158 |
*/ |
| 159 |
public function promo() { |
| 160 |
|
| 161 |
$this->translation_details(); |
| 162 |
$message = $this->promo_message(); |
| 163 |
|
| 164 |
if ( $message ) { |
| 165 |
$this->print_css(); |
| 166 |
|
| 167 |
ob_start(); |
| 168 |
?> |
| 169 |
<div id="give-i18n-notice" class="give-addon-alert updated give-notice" style="display: none"> |
| 170 |
|
| 171 |
<a href="https://wordpress.org/support/register.php" class="alignleft give-i18n-icon" style="margin:0" target="_blank"><span class="dashicons dashicons-translation" |
| 172 |
style="font-size: 110px; text-decoration: none;"></span></a> |
| 173 |
|
| 174 |
<div class="give-i18n-notice-content"> |
| 175 |
<a href="<?php echo esc_url( add_query_arg( array( 'remove_i18n_promo' => '1' ) ) ); ?>" class="dismiss"><span class="dashicons dashicons-dismiss"></span></a> |
| 176 |
|
| 177 |
<h2 style="margin: 10px 0;"><?php printf( esc_html__( 'Help Translate GiveWP to %s', 'give' ), $this->locale_name ); ?></h2> |
| 178 |
<p><?php echo $message; ?></p> |
| 179 |
<p> |
| 180 |
<a href="https://wordpress.org/support/register.php" target="_blank"><?php _e( 'Register now »', 'give' ); ?></a> |
| 181 |
</p> |
| 182 |
</div> |
| 183 |
</div> |
| 184 |
<?php |
| 185 |
|
| 186 |
$notice_html = ob_get_clean(); |
| 187 |
|
| 188 |
// Register notice. |
| 189 |
Give()->notices->register_notice( |
| 190 |
array( |
| 191 |
'id' => 'give-i18n-notice', |
| 192 |
'type' => 'updated', |
| 193 |
'description_html' => $notice_html, |
| 194 |
'show' => true, |
| 195 |
) |
| 196 |
); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
/** |
| 202 |
* Output notice css |
| 203 |
* |
| 204 |
* @since 1.8.16 |
| 205 |
* @access private |
| 206 |
*/ |
| 207 |
private function print_css() { |
| 208 |
?> |
| 209 |
<style> |
| 210 |
/* Banner specific styles */ |
| 211 |
div.give-addon-alert.updated { |
| 212 |
padding: 10px 20px; |
| 213 |
position: relative; |
| 214 |
border-color: #69B868; |
| 215 |
overflow: hidden; |
| 216 |
} |
| 217 |
|
| 218 |
div.give-addon-alert a { |
| 219 |
color: #69B868; |
| 220 |
} |
| 221 |
|
| 222 |
#give-i18n-notice > .give-i18n-icon { |
| 223 |
overflow: hidden; |
| 224 |
} |
| 225 |
|
| 226 |
#give-i18n-notice > .give-i18n-icon .dashicons { |
| 227 |
width: 110px; |
| 228 |
height: 110px; |
| 229 |
} |
| 230 |
|
| 231 |
#give-i18n-notice > .give-i18n-icon:focus { |
| 232 |
box-shadow: none; |
| 233 |
} |
| 234 |
|
| 235 |
.give-i18n-notice-content { |
| 236 |
margin: 0 30px 0 125px; |
| 237 |
} |
| 238 |
|
| 239 |
div.give-addon-alert .dismiss { |
| 240 |
position: absolute; |
| 241 |
right: 20px; |
| 242 |
height: 100%; |
| 243 |
top: 50%; |
| 244 |
margin-top: -10px; |
| 245 |
outline: none; |
| 246 |
box-shadow: none; |
| 247 |
text-decoration: none; |
| 248 |
color: #AAA; |
| 249 |
} |
| 250 |
|
| 251 |
div.give-addon-alert .dismiss:hover { |
| 252 |
color: #333; |
| 253 |
} |
| 254 |
|
| 255 |
/* RTL Styles for banner */ |
| 256 |
body.rtl .give-i18n-notice-content { |
| 257 |
margin: 0 125px 0 30px; |
| 258 |
} |
| 259 |
|
| 260 |
body.rtl div.give-addon-alert .dismiss { |
| 261 |
left: 20px; |
| 262 |
right: auto; |
| 263 |
} |
| 264 |
|
| 265 |
</style> |
| 266 |
<?php |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Try to find the transient for the translation set or retrieve them. |
| 271 |
* |
| 272 |
* @access private |
| 273 |
* |
| 274 |
* @return object|null |
| 275 |
*/ |
| 276 |
private function find_or_initialize_translation_details() { |
| 277 |
|
| 278 |
$set = Give_Cache::get( "give_i18n_give_{$this->locale}", true ); |
| 279 |
|
| 280 |
if ( ! $set ) { |
| 281 |
$set = $this->retrieve_translation_details(); |
| 282 |
Give_Cache::set( "give_i18n_give_{$this->locale}", $set, DAY_IN_SECONDS, true ); |
| 283 |
} |
| 284 |
|
| 285 |
return $set; |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* Try to get translation details from cache, otherwise retrieve them, then parse them. |
| 290 |
* |
| 291 |
* @access private |
| 292 |
*/ |
| 293 |
private function translation_details() { |
| 294 |
$set = $this->find_or_initialize_translation_details(); |
| 295 |
|
| 296 |
$this->translation_exists = ! is_null( $set ); |
| 297 |
$this->translation_loaded = is_textdomain_loaded( 'give' ); |
| 298 |
|
| 299 |
$this->parse_translation_set( $set ); |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Retrieve the translation details from Give Translate. |
| 304 |
* |
| 305 |
* @access private |
| 306 |
* |
| 307 |
* @return object|null |
| 308 |
*/ |
| 309 |
private function retrieve_translation_details() { |
| 310 |
|
| 311 |
$api_url = trailingslashit( $this->glotpress_url ); |
| 312 |
|
| 313 |
$resp = wp_remote_get( $api_url ); |
| 314 |
|
| 315 |
if ( is_wp_error( $resp ) || wp_remote_retrieve_response_code( $resp ) === '404' ) { |
| 316 |
return null; |
| 317 |
} |
| 318 |
|
| 319 |
$body = wp_remote_retrieve_body( $resp ); |
| 320 |
unset( $resp ); |
| 321 |
|
| 322 |
if ( $body ) { |
| 323 |
$body = json_decode( $body ); |
| 324 |
|
| 325 |
foreach ( $body->translation_sets as $set ) { |
| 326 |
if ( ! property_exists( $set, 'wp_locale' ) ) { |
| 327 |
continue; |
| 328 |
} |
| 329 |
|
| 330 |
if ( $this->locale == $set->wp_locale ) { |
| 331 |
return $set; |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
return null; |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Set the needed private variables based on the results from Give Translate. |
| 341 |
* |
| 342 |
* @param object $set The translation set |
| 343 |
* |
| 344 |
* @access private |
| 345 |
*/ |
| 346 |
private function parse_translation_set( $set ) { |
| 347 |
if ( $this->translation_exists && is_object( $set ) ) { |
| 348 |
$this->locale_name = $set->name; |
| 349 |
$this->percent_translated = $set->percent_translated; |
| 350 |
} else { |
| 351 |
$this->locale_name = ''; |
| 352 |
$this->percent_translated = ''; |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
$give_i18n = new Give_i18n_Banner( |
| 358 |
array( |
| 359 |
'hook' => 'admin_notices', |
| 360 |
'glotpress_url' => 'https://translate.wordpress.org/api/projects/wp-plugins/give/stable/', |
| 361 |
) |
| 362 |
); |
| 363 |
|