PluginProbe
Bogo / 3.0.1
Bogo v3.0.1
3.9.3 trunk 1.0 1.1 2.0 2.0.1 2.1 2.1.1 2.1.2 2.1.3 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5 2.6 2.6.1 2.7 2.8 2.8.1 2.9 3.0 3.0.1 All 52 releases
bogo / admin / admin.php

admin.php in Bogo 3.0.1, at admin/admin.php

612 lines 18.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 require_once BOGO_PLUGIN_DIR . '/admin/includes/user.php';
4 require_once BOGO_PLUGIN_DIR . '/admin/includes/post.php';
5 require_once BOGO_PLUGIN_DIR . '/admin/includes/nav-menu.php';
6 require_once BOGO_PLUGIN_DIR . '/admin/includes/widgets.php';
7
8 add_action( 'admin_init', 'bogo_upgrade' );
9
10 function bogo_upgrade() {
11 $old_ver = bogo_get_prop( 'version' );
12 $new_ver = BOGO_VERSION;
13
14 if ( $old_ver != $new_ver ) {
15 require_once BOGO_PLUGIN_DIR . '/admin/includes/upgrade.php';
16 do_action( 'bogo_upgrade', $new_ver, $old_ver );
17 bogo_set_prop( 'version', $new_ver );
18 }
19 }
20
21 add_action( 'admin_enqueue_scripts', 'bogo_admin_enqueue_scripts' );
22
23 function bogo_admin_enqueue_scripts( $hook_suffix ) {
24 wp_enqueue_style( 'bogo-admin',
25 plugins_url( 'admin/includes/css/admin.css', BOGO_PLUGIN_BASENAME ),
26 array(), BOGO_VERSION, 'all' );
27
28 if ( is_rtl() ) {
29 wp_enqueue_style( 'bogo-admin-rtl',
30 plugins_url( 'admin/includes/css/admin-rtl.css', BOGO_PLUGIN_BASENAME ),
31 array(), BOGO_VERSION, 'all' );
32 }
33
34 wp_enqueue_script( 'bogo-admin',
35 plugins_url( 'admin/includes/js/admin.js', BOGO_PLUGIN_BASENAME ),
36 array( 'jquery' ), BOGO_VERSION, true );
37
38 $local_args = array(
39 'saveAlert' => __(
40 "The changes you made will be lost if you navigate away from this page.",
41 'bogo' ),
42 'rest_api' => array(
43 'url' => trailingslashit( rest_url( 'bogo/v1' ) ),
44 'nonce' => wp_create_nonce( 'wp_rest' ) ) );
45
46 if ( 'nav-menus.php' == $hook_suffix ) {
47 $nav_menu_id = absint( get_user_option( 'nav_menu_recently_edited' ) );
48 $nav_menu_items = wp_get_nav_menu_items( $nav_menu_id );
49 $locales = array();
50
51 foreach ( (array) $nav_menu_items as $item ) {
52 $locales[$item->db_id] = $item->bogo_locales;
53 }
54
55 $local_args = array_merge( $local_args, array(
56 'availableLanguages' => bogo_available_languages( array(
57 'exclude_enus_if_inactive' => true,
58 'orderby' => 'value' ) ),
59 'locales' => $locales,
60 'selectorLegend' => __( 'Displayed on pages in', 'bogo' ),
61 'cbPrefix' => 'menu-item-bogo-locale' ) );
62 }
63
64 if ( 'options-general.php' == $hook_suffix ) {
65 $local_args = array_merge( $local_args, array(
66 'defaultLocale' => bogo_get_default_locale() ) );
67 }
68
69 if ( 'post.php' == $hook_suffix && ! empty( $GLOBALS['post'] ) ) {
70 $post = $GLOBALS['post'];
71 $local_args = array_merge( $local_args, array(
72 'post_id' => $post->ID ) );
73 }
74
75 $local_args['pagenow'] = isset( $_GET['page'] ) ? trim( $_GET['page'] ) : '';
76
77 wp_localize_script( 'bogo-admin', '_bogo', $local_args );
78 }
79
80 add_action( 'admin_menu', 'bogo_admin_menu' );
81
82 function bogo_admin_menu() {
83 add_menu_page( __( 'Languages', 'bogo' ), __( 'Languages', 'bogo' ),
84 'bogo_manage_language_packs', 'bogo', 'bogo_tools_page',
85 'dashicons-translation', 73 ); // between Users (70) and Tools (75)
86
87 $tools = add_submenu_page( 'bogo',
88 __( 'Language Packs', 'bogo' ),
89 __( 'Language Packs', 'bogo' ),
90 'bogo_manage_language_packs', 'bogo', 'bogo_tools_page' );
91
92 add_action( 'load-' . $tools, 'bogo_load_tools_page' );
93
94 $texts = add_submenu_page( 'bogo',
95 __( 'Terms Translation', 'bogo' ),
96 __( 'Terms Translation', 'bogo' ),
97 'bogo_edit_terms_translation', 'bogo-texts', 'bogo_texts_page' );
98
99 add_action( 'load-' . $texts, 'bogo_load_texts_page' );
100 }
101
102 function bogo_load_tools_page() {
103 require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
104
105 $action = isset( $_GET['action'] ) ? $_GET['action'] : '';
106
107 if ( 'install_translation' == $action ) {
108 check_admin_referer( 'bogo-tools' );
109
110 if ( ! current_user_can( 'bogo_manage_language_packs' ) ) {
111 wp_die( __( "You are not allowed to install translations.", 'bogo' ) );
112 }
113
114 $locale = isset( $_GET['locale'] ) ? $_GET['locale'] : null;
115
116 if ( wp_download_language_pack( $locale ) ) {
117 $redirect_to = add_query_arg(
118 array( 'locale' => $locale, 'message' => 'install_success' ),
119 menu_page_url( 'bogo', false ) );
120 } else {
121 $redirect_to = add_query_arg(
122 array( 'locale' => $locale, 'message' => 'install_failed' ),
123 menu_page_url( 'bogo', false ) );
124 }
125
126 wp_safe_redirect( $redirect_to );
127 exit();
128 }
129
130 if ( 'set_site_lang' == $action ) {
131 check_admin_referer( 'bogo-tools' );
132
133 if ( ! current_user_can( 'bogo_manage_language_packs' ) ) {
134 wp_die( __( "You are not allowed to manage translations.", 'bogo' ) );
135 }
136
137 $locale = isset( $_GET['locale'] ) ? $_GET['locale'] : null;
138
139 if ( 'en_US' == $locale || ! bogo_is_available_locale( $locale ) ) {
140 $locale = '';
141 }
142
143 if ( update_option( 'WPLANG', $locale ) ) {
144 $redirect_to = add_query_arg(
145 array( 'locale' => $locale, 'message' => 'site_lang_success' ),
146 menu_page_url( 'bogo', false ) );
147 } else {
148 $redirect_to = add_query_arg(
149 array( 'locale' => $locale, 'message' => 'site_lang_failed' ),
150 menu_page_url( 'bogo', false ) );
151 }
152
153 wp_safe_redirect( $redirect_to );
154 exit();
155 }
156
157 if ( 'delete_translation' == $action ) {
158 check_admin_referer( 'bogo-tools' );
159
160 if ( ! current_user_can( 'bogo_manage_language_packs' ) ) {
161 wp_die( __( "You are not allowed to delete translations.", 'bogo' ) );
162 }
163
164 $locale = isset( $_GET['locale'] ) ? $_GET['locale'] : null;
165
166 if ( bogo_delete_language_pack( $locale ) ) {
167 $redirect_to = add_query_arg(
168 array( 'locale' => $locale, 'message' => 'delete_success' ),
169 menu_page_url( 'bogo', false ) );
170 } else {
171 $redirect_to = add_query_arg(
172 array( 'locale' => $locale, 'message' => 'delete_failed' ),
173 menu_page_url( 'bogo', false ) );
174 }
175
176 wp_safe_redirect( $redirect_to );
177 exit();
178 }
179
180 if ( 'deactivate_enus' == $action ) {
181 check_admin_referer( 'bogo-tools' );
182
183 bogo_set_prop( 'enus_deactivated', true );
184
185 $redirect_to = add_query_arg(
186 array( 'message' => 'enus_deactivated' ),
187 menu_page_url( 'bogo', false ) );
188
189 wp_safe_redirect( $redirect_to );
190 exit();
191 }
192
193 if ( 'activate_enus' == $action ) {
194 check_admin_referer( 'bogo-tools' );
195
196 bogo_set_prop( 'enus_deactivated', false );
197
198 $redirect_to = add_query_arg(
199 array( 'message' => 'enus_activated' ),
200 menu_page_url( 'bogo', false ) );
201
202 wp_safe_redirect( $redirect_to );
203 exit();
204 }
205 }
206
207 function bogo_load_texts_page() {
208 $action = isset( $_POST['action'] ) ? $_POST['action'] : '';
209
210 if ( 'save' == $action ) {
211 check_admin_referer( 'bogo-edit-text-translation' );
212
213 $locale = isset( $_POST['locale'] ) ? $_POST['locale'] : null;
214
215 if ( ! current_user_can( 'bogo_edit_terms_translation' ) ) {
216 wp_die( __( "You are not allowed to edit translations.", 'bogo' ) );
217 }
218
219 $entries = array();
220
221 foreach ( $_POST as $p_key => $p_val ) {
222 if ( in_array( $p_key, array( 'action', 'locale', 'submit' ) )
223 || substr( $p_key, 0, 1 ) == '_' ) {
224 continue;
225 }
226
227 $entries[] = array(
228 'singular' => $p_key,
229 'translations' => array( $p_val ),
230 'context' => preg_replace( '/:.*$/', '', $p_key ) );
231 }
232
233 if ( Bogo_POMO::export( $locale, $entries ) ) {
234 $message = 'translation_saved';
235 } else {
236 $message = 'translation_failed';
237 }
238
239 $redirect_to = add_query_arg(
240 array( 'locale' => $locale, 'message' => $message ),
241 menu_page_url( 'bogo-texts', false ) );
242
243 wp_safe_redirect( $redirect_to );
244 exit();
245 }
246 }
247
248 function bogo_tools_page() {
249 $enus_deactivated = bogo_is_enus_deactivated();
250 $can_install = wp_can_install_language_pack();
251
252 $default_locale = bogo_get_default_locale();
253 $available_locales = bogo_available_locales();
254
255 ?>
256 <div class="wrap">
257
258 <h1><?php echo esc_html( __( 'Language Packs', 'bogo' ) ); ?></h1>
259
260 <?php bogo_admin_notice(); ?>
261
262 <table id="bogo-languages-table" class="widefat">
263 <thead>
264 <tr><th><?php echo esc_html( __( 'Language', 'bogo' ) ); ?></th></tr>
265 </thead>
266 <tfoot>
267 <tr><th><?php echo esc_html( __( 'Language', 'bogo' ) ); ?></th></tr>
268 </tfoot>
269 <tbody id="translations">
270 <tr class="active" id="language-1"><td>
271 <strong><?php echo esc_html( bogo_get_language( $default_locale ) ); ?></strong>
272 [<?php echo esc_html( $default_locale ); ?>]
273 <div class="status"><?php echo esc_html( __( 'Site Language', 'bogo' ) ); ?></div>
274 </td></tr>
275
276 <?php
277 $count = 1;
278
279 foreach ( $available_locales as $locale ) {
280 if ( $locale == $default_locale ) {
281 continue;
282 }
283
284 $count += 1;
285 $class = 'active';
286
287 if ( ! $language = bogo_get_language( $locale ) ) {
288 $language = $locale;
289 }
290
291 if ( 'en_US' == $locale ) {
292 $status = $enus_deactivated
293 ? __( 'Inactive', 'bogo' ) : __( 'Active', 'bogo' );
294 } else {
295 $status = __( 'Installed', 'bogo' );
296 }
297
298 if ( 'en_US' == $locale ) {
299 if ( $enus_deactivated ) {
300 $activate_link = menu_page_url( 'bogo', false );
301 $activate_link = add_query_arg(
302 array( 'action' => 'activate_enus' ),
303 $activate_link );
304 $activate_link = wp_nonce_url( $activate_link, 'bogo-tools' );
305 $activate_link = sprintf(
306 '<a href="%1$s" class="activate" aria-label="%3$s">%2$s</a>',
307 $activate_link,
308 esc_html( __( 'Activate', 'bogo' ) ),
309 esc_attr(
310 sprintf( __( 'Activate %s language pack', 'bogo' ), $language ) ) );
311
312 $row_actions = array( $activate_link );
313
314 $class = '';
315 } else {
316 $sitelang_link = menu_page_url( 'bogo', false );
317 $sitelang_link = add_query_arg(
318 array( 'action' => 'set_site_lang', 'locale' => $locale ),
319 $sitelang_link );
320 $sitelang_link = wp_nonce_url( $sitelang_link, 'bogo-tools' );
321 $sitelang_link = sprintf(
322 '<a href="%1$s" class="sitelang" aria-label="%3$s">%2$s</a>',
323 $sitelang_link,
324 esc_html( __( 'Set as Site Language', 'bogo' ) ),
325 esc_attr(
326 sprintf( __( 'Set %s as Site Language', 'bogo' ), $language ) ) );
327
328 $deactivate_link = menu_page_url( 'bogo', false );
329 $deactivate_link = add_query_arg(
330 array( 'action' => 'deactivate_enus' ),
331 $deactivate_link );
332 $deactivate_link = wp_nonce_url( $deactivate_link, 'bogo-tools' );
333 $deactivate_link = sprintf(
334 '<a href="%1$s" class="deactivate" aria-label="%3$s">%2$s</a>',
335 $deactivate_link,
336 esc_html( __( 'Deactivate', 'bogo' ) ),
337 esc_attr( sprintf(
338 __( 'Deactivate %s language pack', 'bogo' ), $language ) ) );
339
340 $row_actions = array( $sitelang_link, $deactivate_link );
341 }
342 } elseif ( $can_install ) {
343 $sitelang_link = menu_page_url( 'bogo', false );
344 $sitelang_link = add_query_arg(
345 array( 'action' => 'set_site_lang', 'locale' => $locale ),
346 $sitelang_link );
347 $sitelang_link = wp_nonce_url( $sitelang_link, 'bogo-tools' );
348 $sitelang_link = sprintf(
349 '<a href="%1$s" class="sitelang" aria-label="%3$s">%2$s</a>',
350 $sitelang_link,
351 esc_html( __( 'Set as Site Language', 'bogo' ) ),
352 esc_attr(
353 sprintf( __( 'Set %s as Site Language', 'bogo' ), $language ) ) );
354
355 $delete_link = menu_page_url( 'bogo', false );
356 $delete_link = add_query_arg(
357 array( 'action' => 'delete_translation', 'locale' => $locale ),
358 $delete_link );
359 $delete_link = wp_nonce_url( $delete_link, 'bogo-tools' );
360
361 $delete_confirm = sprintf( __( "You are about to delete %s language pack.\n 'Cancel' to stop, 'OK' to delete.", 'bogo' ), $language );
362
363 $delete_link = sprintf( '<a href="%1$s" class="delete" onclick="if (confirm(\'%3$s\')){return true;} return false;" aria-label="%4$s">%2$s</a>',
364 $delete_link,
365 esc_html( __( 'Delete', 'bogo' ) ),
366 esc_js( $delete_confirm ),
367 esc_attr(
368 sprintf( __( 'Delete %s language pack', 'bogo' ), $language ) ) );
369
370 $row_actions = array( $sitelang_link, $delete_link );
371 }
372 ?>
373 <tr class="<?php echo esc_attr( $class ); ?>" id="language-<?php echo absint( $count ); ?>">
374 <td>
375 <strong><?php echo esc_html( bogo_get_language( $locale ) ); ?></strong>
376 [<?php echo esc_html( $locale ); ?>]
377 <div class="status"><?php echo esc_html( $status ); ?></div>
378 <div class="row-actions"><?php echo implode( ' | ', $row_actions ); ?></div>
379 </td>
380 </tr>
381 <?php
382 }
383
384 foreach ( wp_get_available_translations() as $locale => $translation ) {
385 if ( in_array( $locale, $available_locales ) ) {
386 continue;
387 }
388
389 $count += 1;
390
391 $install_link = '';
392
393 if ( $can_install ) {
394 $install_link = menu_page_url( 'bogo', false );
395 $install_link = add_query_arg(
396 array( 'action' => 'install_translation', 'locale' => $locale ),
397 $install_link );
398 $install_link = wp_nonce_url( $install_link, 'bogo-tools' );
399 $install_link = sprintf( '<a href="%1$s" class="install" aria-label="%3$s">%2$s</a>',
400 $install_link,
401 esc_html( __( 'Install', 'bogo' ) ),
402 esc_attr(
403 sprintf( __( 'Install %s language pack', 'bogo' ),
404 bogo_get_language( $locale ) ) ) );
405
406 $row_actions = array( $install_link );
407 }
408 ?>
409 <tr id="language-<?php echo absint( $count ); ?>"><td>
410 <strong><?php echo esc_html( bogo_get_language( $locale ) ); ?></strong>
411 [<?php echo esc_html( $locale ); ?>]
412 <div class="row-actions"><?php echo implode( ' | ', $row_actions ); ?></div>
413 </td></tr>
414 <?php
415 }
416 ?>
417
418 </tbody>
419 </table>
420
421 </div>
422 <?php
423 }
424
425 function bogo_texts_page() {
426 $locale_to_edit = isset( $_GET['locale'] ) ? $_GET['locale'] : '';
427
428 if ( ! Bogo_POMO::import( $locale_to_edit ) ) {
429 Bogo_POMO::reset();
430 }
431
432 ?>
433 <div class="wrap">
434
435 <h1><?php echo esc_html( __( 'Terms Translation', 'bogo' ) ); ?></h1>
436
437 <?php bogo_admin_notice(); ?>
438
439 <form action="" method="post" id="bogo-translatable-text-strings">
440 <input type="hidden" name="action" value="save" />
441 <?php wp_nonce_field( 'bogo-edit-text-translation' ); ?>
442
443 <p>
444 <select name="locale" id="select-locale">
445 <option value=""><?php echo esc_html( __( '-- Select Language to Edit --', 'bogo' ) ); ?></option>
446 <?php
447 $available_locales = bogo_available_locales(
448 array( 'current_user_can_access' => true ) );
449
450 foreach ( $available_locales as $locale ) {
451 echo sprintf( '<option value="%1$s"%3$s>%2$s</option>',
452 esc_attr( $locale ),
453 esc_html( bogo_get_language( $locale ) ),
454 $locale == $locale_to_edit ? ' selected="selected"' : '' );
455 }
456 ?>
457 </select>
458 </p>
459
460 <?php
461 if ( bogo_is_available_locale( $locale_to_edit ) ) :
462 $strings = array(
463 array(
464 'name' => 'blogname',
465 'original' => get_option( 'blogname' ),
466 'translated' => bogo_translate( 'blogname',
467 'blogname', get_option( 'blogname' ) ),
468 'context' => __( 'Site Title', 'bogo' ) ),
469 array(
470 'name' => 'blogdescription',
471 'original' => get_option( 'blogdescription' ),
472 'translated' => bogo_translate( 'blogdescription',
473 'blogdescription', get_option( 'blogdescription' ) ),
474 'context' => __( 'Tagline', 'bogo' ) ) );
475
476 remove_filter( 'get_term', 'bogo_get_term_filter' );
477
478 foreach ( (array) get_taxonomies( array(), 'objects' ) as $taxonomy ) {
479 $tax_labels = get_taxonomy_labels( $taxonomy );
480 $terms = get_terms( array(
481 'taxonomy' => $taxonomy->name,
482 'orderby' => 'slug',
483 'hide_empty' => false ) );
484
485 foreach ( (array) $terms as $term ) {
486 $name = sprintf( '%s:%d', $taxonomy->name, $term->term_id );
487 $strings[] = array(
488 'name' => $name,
489 'original' => $term->name,
490 'translated' => bogo_translate( $name,
491 $taxonomy->name, $term->name ),
492 'context' => $tax_labels->name );
493 }
494 }
495 ?>
496 <table class="wp-list-table widefat fixed striped" id="bogo-translation-table">
497 <thead>
498 <tr>
499 <th scope="col"><?php echo esc_html( __( 'Original', 'bogo' ) ); ?></th>
500 <th scope="col"><?php echo esc_html( __( 'Translation', 'bogo' ) ); ?></th>
501 <th scope="col" style="width: 20%;"><?php echo esc_html( __( 'Context', 'bogo' ) ); ?></th>
502 </tr>
503 </thead>
504 <tfoot>
505 <tr>
506 <th scope="col"><?php echo esc_html( __( 'Original', 'bogo' ) ); ?></th>
507 <th scope="col"><?php echo esc_html( __( 'Translation', 'bogo' ) ); ?></th>
508 <th scope="col"><?php echo esc_html( __( 'Context', 'bogo' ) ); ?></th>
509 </tr>
510 </tfoot>
511 <tbody>
512 <?php
513 foreach( $strings as $string ) :
514 ?>
515 <tr>
516 <td><?php echo esc_html( $string['original'] ); ?></td>
517 <td><input name="<?php echo esc_attr( $string['name'] ); ?>" type="text" id="<?php echo esc_attr( $string['name'] ); ?>" value="<?php echo esc_attr( $string['translated'] ); ?>" class="large-text" /></td>
518 <td><?php echo esc_html( $string['context'] ); ?></td>
519 </tr>
520 <?php
521 endforeach;
522 ?>
523 </tbody>
524 </table>
525
526 <?php submit_button(); ?>
527 <?php
528 endif;
529 ?>
530 </form>
531 </div>
532 <?php
533 }
534
535 function bogo_admin_notice( $reason = '' ) {
536 if ( empty( $reason ) && isset( $_GET['message'] ) ) {
537 $reason = $_GET['message'];
538 }
539
540 if ( 'install_success' == $reason ) {
541 $message = __( "Translation installed successfully.", 'bogo' );
542 } elseif ( 'install_failed' == $reason ) {
543 $message = __( "Translation install failed.", 'bogo' );
544 } elseif ( 'site_lang_success' == $reason ) {
545 $message = __( "Site language set successfully.", 'bogo' );
546 } elseif ( 'site_lang_failed' == $reason ) {
547 $message = __( "Setting site language failed.", 'bogo' );
548 } elseif ( 'delete_success' == $reason ) {
549 $message = __( "Translation uninstalled successfully.", 'bogo' );
550 } elseif ( 'delete_failed' == $reason ) {
551 $message = __( "Translation uninstall failed.", 'bogo' );
552 } elseif ( 'enus_deactivated' == $reason ) {
553 $message = __( "English (United States) deactivated.", 'bogo' );
554 } elseif ( 'enus_activated' == $reason ) {
555 $message = __( "English (United States) activated.", 'bogo' );
556 } elseif ( 'translation_saved' == $reason ) {
557 $message = __( "Translation saved.", 'bogo' );
558 } elseif ( 'translation_failed' == $reason ) {
559 $message = __( "Saving translation failed.", 'bogo' );
560 } else {
561 return false;
562 }
563
564 if ( '_failed' == substr( $reason, -7 ) ) {
565 echo sprintf(
566 '<div class="error notice notice-error is-dismissible"><p>%s</p></div>',
567 esc_html( $message ) );
568 } else {
569 echo sprintf(
570 '<div class="updated notice notice-success is-dismissible"><p>%s</p></div>',
571 esc_html( $message ) );
572 }
573 }
574
575 function bogo_delete_language_pack( $locale ) {
576 if ( 'en_US' == $locale
577 || ! bogo_is_available_locale( $locale )
578 || bogo_is_default_locale( $locale ) ) {
579 return false;
580 }
581
582 if ( ! is_dir( WP_LANG_DIR ) || ! $files = scandir( WP_LANG_DIR ) ) {
583 return false;
584 }
585
586 $target_files = array(
587 sprintf( '%s.mo', $locale ),
588 sprintf( '%s.po', $locale ),
589 sprintf( 'admin-%s.mo', $locale ),
590 sprintf( 'admin-%s.po', $locale ),
591 sprintf( 'admin-network-%s.mo', $locale ),
592 sprintf( 'admin-network-%s.po', $locale ),
593 sprintf( 'continents-cities-%s.mo', $locale ),
594 sprintf( 'continents-cities-%s.po', $locale ) );
595
596 foreach ( $files as $file ) {
597 if ( '.' === $file[0] || is_dir( $file ) ) {
598 continue;
599 }
600
601 if ( in_array( $file, $target_files ) ) {
602 $result = @unlink( path_join( WP_LANG_DIR, $file ) );
603
604 if ( ! $result ) {
605 return false;
606 }
607 }
608 }
609
610 return true;
611 }
612