1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193:
<?php
class Inbound_Translation_Updater {
static $old_version;
static $enable;
static $translations_path;
static $response;
public function __construct() {
global $inbound_settings;
self::$old_version = get_option('inbound_translation_version' , 0);
self::$enable = (isset($inbound_settings['translations']['toggle-translations-updater'])) ? $inbound_settings['translations']['toggle-translations-updater'] : 'on';
self::$translations_path = self::get_language_path();
if ( version_compare(self::$old_version , INBOUND_PRO_TRANSLATIONS_VERSION) === -1 && self::$enable == 'on' ) {
add_action('admin_notices', array( __CLASS__ , 'throw_notice' ) );
add_filter( 'admin_footer' , array( __CLASS__ , 'add_js' ) );
add_action( 'wp_ajax_inbound_update_translations', array( __CLASS__ , 'ajax_update_translations' ) );
}
}
public static function throw_notice() {
$bases = array('dashboard','toplevel_page_inbound-pro');
$screen = get_current_screen();
if (!in_array($screen->base, $bases)) {
return;
}
?>
<div class="updated" id="inbound_translation_notification">
<p><?php _e( sprintf( 'A new translation package is available for Inbound Pro. %s' , '<a href="#" id="inbound-install-translations">'.__( 'Please click to download (this may take a moment). ' , 'inbound-pro' ).' <i class="fa fa-download" aria-hidden="true"></i></a><span class="spinner" id="inbound-spinner"></span> ') , 'inbound-pro'); ?></p>
</div>
<?php
}
public static function get_language_path() {
if (!is_dir( INBOUND_PRO_UPLOADS_PATH . 'assets/images/' )) {
wp_mkdir_p( INBOUND_PRO_UPLOADS_PATH . 'assets/lang' );
}
return INBOUND_PRO_UPLOADS_PATH . 'assets/lang/';
}
public static function add_js() {
?>
<script type="text/javascript">
var InboundTranslationsUpdater = (function () {
var construct = {
init : function () {
InboundTranslationsUpdater.addListeners();
},
addListeners : function () {
jQuery('body').on('click', '#inbound-install-translations', function () {
jQuery('#inbound-install-translations').hide();
jQuery('#inbound-spinner').css('float','none');
jQuery('#inbound-spinner').css('margin-top','-6px');
jQuery('#inbound-spinner').css('visibility','initial');
InboundTranslationsUpdater.updateTranslations();
});
},
updateTranslations : function () {
jQuery.ajax({
type: "POST",
url: ajaxurl,
data: {
action: 'inbound_update_translations'
},
dataType: 'html',
timeout: 360000,
success: function (response) {
jQuery('#inbound_translation_notification').remove();
},
error: function (request, status, err) {
alert(err);
}
});
}
}
return construct;
})();
jQuery(document).ready(function () {
InboundTranslationsUpdater.init();
});
</script>
<?php
}
public static function ajax_update_translations() {
self::get_translations_zip();
self::install_transations();
}
public static function get_translations_zip() {
self::$response = wp_remote_get(
'https://github.com/inboundnow/translations/raw/master/translations.zip' ,
array(
'timeout' => 500,
'redirection' => 5,
'decompress' => false
)
);
}
public static function install_transations() {
if (empty(self::$response['body'])) {
echo json_encode(self::$response);
exit;
}
include_once( ABSPATH . '/wp-admin/includes/class-pclzip.php');
$temp_file = tempnam('/tmp', 'TEMPPLUGIN' );
$handle = fopen($temp_file, "w");
fwrite($handle, self::$response['body']);
fclose($handle);
$archive = new PclZip($temp_file);
$result = $archive->extract( PCLZIP_OPT_PATH, self::$translations_path , PCLZIP_OPT_REPLACE_NEWER );
if ($result == 0) {
echo '{pclzip error}';
exit;
}
unlink($temp_file);
update_option('inbound_translation_version' , INBOUND_PRO_TRANSLATIONS_VERSION , true);
echo 'success';exit;
}
}
new Inbound_Translation_Updater;