<?php
class yoast_i18n {
private $glotpress_logo;
private $glotpress_name;
private $glotpress_url;
private $hook;
private $locale;
private $locale_name;
private $percent_translated;
private $plugin_name;
private $project_slug;
private $register_url;
private $textdomain;
private $translation_exists;
private $translation_loaded;
public function __construct( $args ) {
if ( ! is_admin() ) {
return;
}
$this->locale = get_locale();
if ( 'en_US' === $this->locale ) {
return;
}
$this->init( $args );
if ( ! $this->hide_promo() ) {
add_action( $this->hook, array( $this, 'promo' ) );
}
}
private function init( $args ) {
foreach ( $args as $key => $arg ) {
$this->$key = $arg;
}
}
private function hide_promo() {
$hide_promo = get_transient( 'yoast_i18n_' . $this->project_slug . '_promo_hide' );
if ( ! $hide_promo ) {
if ( filter_input( INPUT_GET, 'remove_i18n_promo', FILTER_VALIDATE_INT ) === 1 ) {
set_transient( 'yoast_i18n_' . $this->project_slug . '_promo_hide', true );
$hide_promo = true;
}
}
return $hide_promo;
}
private function promo_message() {
$message = false;
if ( $this->translation_exists && $this->translation_loaded && $this->percent_translated < 90 ) {
$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 complete the translation to %1$s!', $this->textdomain );
} else if ( ! $this->translation_loaded && $this->translation_exists ) {
$message = __( 'You\'re using WordPress in %1$s. While %2$s has been translated to %1$s for %3$d%%, it\'s not been shipped with the plugin yet. You can help! Register at %4$s to help complete the translation to %1$s!', $this->textdomain );
} else if ( ! $this->translation_exists ) {
$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!', $this->textdomain );
}
$registration_link = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $this->register_url ), esc_html( $this->glotpress_name ) );
$message = sprintf( $message, esc_html( $this->locale_name ), esc_html( $this->plugin_name ), $this->percent_translated, $registration_link );
return $message;
}
public function promo() {
$this->translation_details();
$message = $this->promo_message();
if ( $message ) {
echo '<div id="i18n_promo_box" style="border:1px solid #ccc;background-color:#fff;padding:10px;max-width:650px;">';
echo '<a href="' . esc_url( add_query_arg( array( 'remove_i18n_promo' => '1' ) ) ) . '" style="color:#333;text-decoration:none;font-weight:bold;font-size:16px;border:1px solid #ccc;padding:1px 4px;" class="alignright">X</a>';
echo '<h2>' . sprintf( __( 'Translation of %s', $this->textdomain ), $this->plugin_name ) . '</h2>';
if ( isset( $this->glotpress_logo ) && '' != $this->glotpress_logo ) {
echo '<a href="' . $this->register_url . '"><img class="alignright" style="margin:15px 5px 5px 5px;width:200px;" src="' . $this->glotpress_logo . '" alt="' . $this->glotpress_name . '"/></a>';
}
echo '<p>' . $message . '</p>';
echo '<p><a href="' . $this->register_url . '">' . __( 'Register now »', $this->textdomain ) . '</a></p>';
echo '</div>';
}
}
private function find_or_initialize_translation_details() {
$set = get_transient( 'yoast_i18n_' . $this->project_slug . '_' . $this->locale );
if ( ! $set ) {
$set = $this->retrieve_translation_details();
set_transient( 'yoast_i18n_' . $this->project_slug . '_' . $this->locale, $set, DAY_IN_SECONDS );
}
return $set;
}
private function translation_details() {
$set = $this->find_or_initialize_translation_details();
$this->translation_exists = ! is_null( $set );
$this->translation_loaded = is_textdomain_loaded( $this->textdomain );
$this->parse_translation_set( $set );
}
private function retrieve_translation_details() {
$api_url = trailingslashit( $this->glotpress_url ) . 'api/projects/' . $this->project_slug;
$resp = wp_remote_get( $api_url );
$body = wp_remote_retrieve_body( $resp );
unset( $resp );
if ( $body ) {
$body = json_decode( $body );
foreach ( $body->translation_sets as $set ) {
if ( $this->locale == $set->wp_locale ) {
return $set;
}
}
}
return null;
}
private function parse_translation_set( $set ) {
if ( $this->translation_exists && is_object( $set ) ) {
$this->locale_name = $set->name;
$this->percent_translated = $set->percent_translated;
} else {
$this->locale_name = '';
$this->percent_translated = '';
}
}
}