1: <?php
2:
3: if ( ! defined( 'ABSPATH' ) ) {
4: exit;
5: }
6:
7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18: class Sensei_Language_Pack_Manager {
19:
20: 21: 22: 23: 24:
25: protected static $repo = 'https://github.com/woothemes/sensei-language-packs/raw/';
26:
27: 28: 29:
30: public function __construct() {
31: add_action( 'update_option_WPLANG', array( $this, 'updated_language_option' ), 10, 2 );
32: add_filter( 'admin_init', array( $this, 'language_package_actions' ), 10 );
33: }
34:
35: 36: 37: 38: 39: 40: 41:
42: public static function get_package_uri( $locale ) {
43: return self::$repo . Sensei()->version . '/packages/' . $locale . '.zip';
44: }
45:
46: 47: 48: 49: 50: 51: 52:
53: protected static function get_settings_uri( $action ) {
54: return wp_nonce_url( admin_url( 'admin.php?page=woothemes-sensei-settings&action=' . $action ), 'language_pack', '_sensei_language_nonce' );
55: }
56:
57: 58: 59: 60: 61:
62: public static function get_install_uri() {
63: return self::get_settings_uri( 'language_pack_install' );
64: }
65:
66: 67: 68: 69: 70:
71: public static function get_dismiss_uri() {
72: return self::get_settings_uri( 'dismiss_language_pack_notice' );
73: }
74:
75: 76: 77: 78: 79: 80:
81: public function updated_language_option( $old, $new ) {
82: self::has_language_pack_available( $new );
83: }
84:
85: 86: 87: 88: 89: 90: 91:
92: public static function has_language_pack_available( $locale = null ) {
93:
94: if ( is_null( $locale ) ) {
95:
96: $locale = get_locale();
97:
98: }
99:
100: if ( 'en_US' === $locale ) {
101:
102: return false;
103:
104: }
105:
106: if ( 'yes' === get_option( 'sensei_needs_language_pack_install' ) ) {
107:
108: return true;
109:
110: }
111:
112: if( isset( $_GET['translation_updated'] ) && 5 == $_GET['translation_updated'] ){
113:
114: return false;
115:
116: }
117:
118: $version = get_option( 'woothemes_sensei_language_pack_version', array( '0', $locale ) );
119:
120: if ( ! is_array( $version ) || version_compare( $version[0], Sensei()->version, '<' ) || $version[1] !== $locale ) {
121: if ( self::check_if_language_pack_exists( $locale ) ) {
122: update_option( 'sensei_needs_language_pack_install', 'yes' );
123:
124: return true;
125: } else {
126:
127: self::update_language_pack_version( $locale );
128: }
129: }
130:
131: return false;
132:
133: }
134:
135: 136: 137: 138: 139:
140: public static function check_if_language_pack_exists( $locale ) {
141: $response = wp_safe_remote_get( self::get_package_uri( $locale ), array( 'timeout' => 60 ) );
142:
143: if ( ! is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 ) {
144: return true;
145: } else {
146: return false;
147: }
148: }
149:
150: 151: 152: 153: 154:
155: public static function update_language_pack_version( $locale ) {
156:
157: update_option( 'woothemes_sensei_language_pack_version', array( Sensei()->version, $locale ) );
158:
159:
160: update_option( 'sensei_needs_language_pack_install', 'no' );
161: }
162:
163: 164: 165:
166: public function language_package_actions() {
167: if (
168: is_admin()
169: && current_user_can( 'update_plugins' )
170: && isset( $_GET['page'] )
171: && 'woothemes-sensei-settings' === $_GET['page']
172: && isset( $_GET['action'] )
173: ) {
174:
175: if ( 'language_pack_install' === $_GET['action'] ) {
176: $this->language_pack_install();
177: }
178:
179: if ( 'dismiss_language_pack_notice' ) {
180: $this->dismiss_language_pack_notice();
181: }
182: }
183: }
184:
185: 186: 187:
188: protected function language_pack_install() {
189: $url = wp_nonce_url( admin_url( 'admin.php?page=woothemes-sensei-settings&action=language_pack_install' ), 'language_install' );
190: $settings_url = admin_url( 'admin.php?page=woothemes-sensei-settings' );
191: $locale = get_locale();
192:
193: if ( ! isset( $_REQUEST['_sensei_language_nonce'] ) && wp_verify_nonce( $_REQUEST['_sensei_language_nonce'], 'language_pack' ) ) {
194: wp_redirect( add_query_arg( array( 'translation_updated' => 2 ), $settings_url ) );
195: exit;
196: }
197:
198: if ( 'en_US' === $locale || ! self::check_if_language_pack_exists( $locale ) ) {
199: wp_redirect( add_query_arg( array( 'translation_updated' => 3 ), $settings_url ) );
200: exit;
201: }
202:
203: if ( false === ( $creds = request_filesystem_credentials( $url, '', false, false, null ) ) ) {
204: wp_redirect( add_query_arg( array( 'translation_updated' => 4 ), $settings_url ) );
205: exit;
206: }
207:
208: if ( ! WP_Filesystem( $creds ) ) {
209: request_filesystem_credentials( $url, '', true, false, null );
210:
211: wp_redirect( add_query_arg( array( 'translation_updated' => 4 ), $settings_url ) );
212: exit;
213: }
214:
215:
216: $response = wp_safe_remote_get( self::get_package_uri( $locale ), array( 'timeout' => 60 ) );
217: if ( ! is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 ) {
218: global $wp_filesystem;
219:
220: $upload_dir = wp_upload_dir();
221: $file = trailingslashit( $upload_dir['path'] ) . $locale . '.zip';
222:
223:
224: if ( ! $wp_filesystem->put_contents( $file, $response['body'], FS_CHMOD_FILE ) ) {
225: wp_redirect( add_query_arg( array( 'translation_updated' => 4 ), $settings_url ) );
226: exit;
227: }
228:
229:
230: $dir = trailingslashit( WP_LANG_DIR ) . 'plugins/';
231: $unzip = unzip_file( $file, $dir );
232: if ( true !== $unzip ) {
233: wp_redirect( add_query_arg( array( 'translation_updated' => 4 ), $settings_url ) );
234: exit;
235: }
236:
237:
238: $wp_filesystem->delete( $file );
239:
240:
241: self::update_language_pack_version( $locale );
242:
243:
244: wp_redirect( add_query_arg( array( 'translation_updated' => 1 ), $settings_url ) );
245: exit;
246: } else {
247:
248: wp_redirect( add_query_arg( array( 'translation_updated' => 5 ), $settings_url ) );
249: exit;
250: }
251: }
252:
253: 254: 255:
256: protected function dismiss_language_pack_notice() {
257: if ( ! isset( $_REQUEST['_sensei_language_nonce'] ) && wp_verify_nonce( $_REQUEST['_sensei_language_nonce'], 'language_pack' ) ) {
258: wp_die( __( 'Cheatin’ huh?', 'woothemes-sensei' ) );
259: }
260:
261:
262: self::update_language_pack_version( get_locale() );
263: }
264:
265: 266: 267:
268: public static function messages() {
269: if ( empty( $_GET['translation_updated'] ) ) {
270: return;
271: }
272:
273: switch ( $_GET['translation_updated'] ) {
274: case 2 :
275: echo '<div class="error"><p>' . __( 'Failed to install/update the translation:', 'woothemes-sensei' ) . ' ' . __( 'Seems you don\'t have permission to do this!', 'woothemes-sensei' ) . '</p></div>';
276: break;
277: case 3 :
278: echo '<div class="error"><p>' . __( 'Failed to install/update the translation:', 'woothemes-sensei' ) . ' ' . __( 'There is no translation available for your language!', 'woothemes-sensei' ) . '</p></div>';
279: break;
280: case 4 :
281: echo '<div class="error"><p>' . __( 'Failed to install/update the translation:', 'woothemes-sensei' ) . ' ' . sprintf( __( 'An authentication error occurred while updating the translation. Please try again or configure your %sUpgrade Constants%s.', 'woothemes-sensei' ), '<a href="http://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants">', '</a>' ) . '</p></div>';
282: break;
283: case 5 :
284: echo '<div class="error"><p>' . __( 'Failed to install/update the translation:', 'woothemes-sensei' ) . ' ' . __( 'Sorry but there is no translation available for your language =/', 'woothemes-sensei' ) . '</p></div>';
285: break;
286:
287: default :
288: echo '<div class="updated"><p>' . __( 'Translations installed/updated successfully!', 'woothemes-sensei' ) . '</p></div>';
289: break;
290: }
291: }
292: }
293:
294: new Sensei_Language_Pack_Manager();
295: