PluginProbe
Bogo / 3.8
Bogo v3.8
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.8, at admin/admin.php

527 lines 13.6 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 require_once BOGO_PLUGIN_DIR . '/admin/includes/language-packs.php';
8 require_once BOGO_PLUGIN_DIR . '/admin/includes/terms-translation.php';
9
10 add_action( 'admin_init', 'bogo_upgrade', 10, 0 );
11
12 function bogo_upgrade() {
13 $old_ver = bogo_get_prop( 'version' );
14 $new_ver = BOGO_VERSION;
15
16 if ( $old_ver != $new_ver ) {
17 require_once BOGO_PLUGIN_DIR . '/admin/includes/upgrade.php';
18 do_action( 'bogo_upgrade', $new_ver, $old_ver );
19 bogo_set_prop( 'version', $new_ver );
20 }
21 }
22
23 add_action( 'admin_enqueue_scripts', 'bogo_admin_enqueue_scripts', 10, 1 );
24
25 function bogo_admin_enqueue_scripts( $hook_suffix ) {
26 wp_enqueue_style( 'bogo-admin',
27 plugins_url( 'admin/includes/css/admin.css', BOGO_PLUGIN_BASENAME ),
28 array(), BOGO_VERSION, 'all'
29 );
30
31 if ( is_rtl() ) {
32 wp_enqueue_style( 'bogo-admin-rtl',
33 plugins_url( 'admin/includes/css/admin-rtl.css', BOGO_PLUGIN_BASENAME ),
34 array(), BOGO_VERSION, 'all'
35 );
36 }
37
38 wp_enqueue_script( 'bogo-admin',
39 plugins_url( 'admin/includes/js/admin.js', BOGO_PLUGIN_BASENAME ),
40 array( 'jquery' ), BOGO_VERSION, true
41 );
42
43 $available_languages = array();
44
45 foreach ( bogo_available_languages() as $locale => $language ) {
46 $native_name = bogo_get_language_native_name( $locale );
47
48 if ( bogo_locale_is_alone( $locale ) ) {
49 $native_name = bogo_get_short_name( $native_name );
50 }
51
52 $available_languages[$locale] = array(
53 'name' => $language,
54 'nativename' => trim( $native_name ),
55 'country' => bogo_get_country_code( $locale ),
56 'tags' => array_unique( array_filter(
57 array(
58 bogo_language_tag( $locale ),
59 bogo_lang_slug( $locale ),
60 )
61 ) ),
62 );
63 }
64
65 $local_args = array(
66 'l10n' => array(
67 /* translators: accessibility text */
68 'targetBlank' => __( '(opens in a new window)', 'bogo' ),
69 'saveAlert' => __( "The changes you made will be lost if you navigate away from this page.", 'bogo' ),
70 ),
71 'apiSettings' => array(
72 'root' => esc_url_raw( rest_url( 'bogo/v1' ) ),
73 'namespace' => 'bogo/v1',
74 'nonce' => ( wp_installing() && ! is_multisite() )
75 ? '' : wp_create_nonce( 'wp_rest' ),
76 ),
77 'availableLanguages' => $available_languages,
78 'defaultLocale' => bogo_get_default_locale(),
79 'pagenow' => trim( $_GET['page'] ?? '' ),
80 'currentPost' => array(),
81 'localizablePostTypes' => bogo_localizable_post_types(),
82 'showFlags' => apply_filters( 'bogo_use_flags', true ),
83 );
84
85 if ( in_array( $hook_suffix, array( 'post.php', 'post-new.php' ) ) ) {
86 if ( $screen = get_current_screen() and $screen->is_block_editor() ) {
87 wp_enqueue_script( 'bogo-block-editor' );
88 }
89
90 $user_locale = bogo_get_user_locale();
91
92 $current_post = array(
93 'locale' => $user_locale,
94 'lang' => bogo_lang_slug( $user_locale ),
95 'translations' => array(),
96 );
97
98 if ( $post = get_post() ) {
99 $current_post['postId'] = $post->ID;
100 $post_type_object = get_post_type_object( $post->post_type );
101 $edit_post_cap = $post_type_object->cap->edit_post;
102
103 if ( $locale = get_post_meta( $post->ID, '_locale', true ) ) {
104 $current_post['locale'] = $locale;
105 $current_post['lang'] = bogo_lang_slug( $locale );
106 }
107
108 $available_locales = bogo_available_locales( array(
109 'exclude' => array( $current_post['locale'] ),
110 ) );
111
112 foreach ( $available_locales as $locale ) {
113 $current_post['translations'][$locale] = array();
114
115 $translation = bogo_get_post_translation( $post->ID, $locale );
116
117 if ( $translation ) {
118 $current_post['translations'][$locale] = array(
119 'postId' => $translation->ID,
120 'postTitle' => $translation->post_title,
121 'editLink' => current_user_can( $edit_post_cap, $translation->ID )
122 ? get_edit_post_link( $translation, 'raw' )
123 : '',
124 );
125 }
126 }
127 }
128
129 $local_args['currentPost'] = $current_post;
130 }
131
132 wp_localize_script( 'bogo-admin', 'bogo', $local_args );
133 }
134
135 add_action( 'admin_menu', 'bogo_admin_menu', 10, 0 );
136
137 function bogo_admin_menu() {
138 add_menu_page(
139 __( 'Languages', 'bogo' ),
140 __( 'Languages', 'bogo' ),
141 'bogo_manage_language_packs',
142 'bogo',
143 'bogo_tools_page',
144 'dashicons-translation',
145 73 // between Users (70) and Tools (75)
146 );
147
148 $tools = add_submenu_page(
149 'bogo',
150 __( 'Language Packs', 'bogo' ),
151 __( 'Language Packs', 'bogo' ),
152 'bogo_manage_language_packs',
153 'bogo',
154 'bogo_tools_page'
155 );
156
157 add_action( 'load-' . $tools, 'bogo_load_tools_page', 10, 0 );
158
159 $available_locales = bogo_available_locales( array(
160 'current_user_can_access' => true,
161 'exclude' => array( bogo_get_default_locale() ),
162 ) );
163
164 if ( 0 < count( $available_locales ) ) {
165 $texts = add_submenu_page(
166 'bogo',
167 __( 'Terms Translation', 'bogo' ),
168 __( 'Terms Translation', 'bogo' ),
169 'bogo_edit_terms_translation',
170 'bogo-texts',
171 'bogo_texts_page'
172 );
173
174 add_action( 'load-' . $texts, 'bogo_load_texts_page', 10, 0 );
175 }
176 }
177
178 add_filter(
179 'set_screen_option_bogo_texts_per_page',
180 'bogo_set_screen_options',
181 10, 3
182 );
183
184 function bogo_set_screen_options( $result, $option, $value ) {
185 $bogo_screens = array(
186 'bogo_texts_per_page',
187 );
188
189 if ( in_array( $option, $bogo_screens ) ) {
190 $result = $value;
191 }
192
193 return $result;
194 }
195
196 function bogo_load_tools_page() {
197 require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
198
199 $action = $_GET['action'] ?? '';
200 $locale = $_GET['locale'] ?? null;
201
202 if ( 'activate' == $action ) {
203 check_admin_referer( 'bogo-tools' );
204
205 if ( ! current_user_can( 'bogo_manage_language_packs' ) ) {
206 wp_die( __( "You are not allowed to manage translations.", 'bogo' ) );
207 }
208
209 if ( 'en_US' == $locale ) {
210 bogo_set_prop( 'enus_deactivated', false );
211
212 $redirect_to = add_query_arg(
213 array( 'message' => 'enus_activated' ),
214 menu_page_url( 'bogo', false )
215 );
216 } else {
217 if ( wp_download_language_pack( $locale ) ) {
218 $redirect_to = add_query_arg(
219 array( 'locale' => $locale, 'message' => 'install_success' ),
220 menu_page_url( 'bogo', false )
221 );
222 } else {
223 $redirect_to = add_query_arg(
224 array( 'locale' => $locale, 'message' => 'install_failed' ),
225 menu_page_url( 'bogo', false )
226 );
227 }
228 }
229
230 wp_safe_redirect( $redirect_to );
231 exit();
232 }
233
234 if ( 'deactivate' == $action ) {
235 check_admin_referer( 'bogo-tools' );
236
237 if ( ! current_user_can( 'bogo_manage_language_packs' ) ) {
238 wp_die( __( "You are not allowed to manage translations.", 'bogo' ) );
239 }
240
241 if ( 'en_US' == $locale ) {
242 bogo_set_prop( 'enus_deactivated', true );
243
244 $redirect_to = add_query_arg(
245 array( 'message' => 'enus_deactivated' ),
246 menu_page_url( 'bogo', false )
247 );
248 } else {
249 if ( bogo_delete_language_pack( $locale ) ) {
250 $redirect_to = add_query_arg(
251 array( 'locale' => $locale, 'message' => 'delete_success' ),
252 menu_page_url( 'bogo', false )
253 );
254 } else {
255 $redirect_to = add_query_arg(
256 array( 'locale' => $locale, 'message' => 'delete_failed' ),
257 menu_page_url( 'bogo', false )
258 );
259 }
260 }
261
262 wp_safe_redirect( $redirect_to );
263 exit();
264 }
265
266 if ( 'promote' == $action ) {
267 check_admin_referer( 'bogo-tools' );
268
269 if ( ! current_user_can( 'bogo_manage_language_packs' ) ) {
270 wp_die( __( "You are not allowed to manage translations.", 'bogo' ) );
271 }
272
273 if ( 'en_US' == $locale
274 or ! bogo_is_available_locale( $locale ) ) {
275 $locale = '';
276 }
277
278 if ( update_option( 'WPLANG', $locale ) ) {
279 $redirect_to = add_query_arg(
280 array( 'locale' => $locale, 'message' => 'promote_success' ),
281 menu_page_url( 'bogo', false )
282 );
283 } else {
284 $redirect_to = add_query_arg(
285 array( 'locale' => $locale, 'message' => 'promote_failed' ),
286 menu_page_url( 'bogo', false )
287 );
288 }
289
290 wp_safe_redirect( $redirect_to );
291 exit();
292 }
293
294 if ( 'translate' == $action ) {
295 check_admin_referer( 'bogo-tools' );
296
297 if ( ! current_user_can( 'bogo_edit_terms_translation', $locale ) ) {
298 wp_die( __( "You are not allowed to edit translations.", 'bogo' ) );
299 }
300
301 $is_active = ( 'en_US' == $locale )
302 ? ! bogo_is_enus_deactivated()
303 : bogo_is_available_locale( $locale );
304
305 if ( ! bogo_is_default_locale( $locale )
306 and $is_active ) {
307 $redirect_to = add_query_arg(
308 array( 'locale' => $locale ),
309 menu_page_url( 'bogo-texts', false ) );
310
311 wp_safe_redirect( $redirect_to );
312 exit();
313 }
314 }
315
316 $current_screen = get_current_screen();
317
318 add_filter( 'manage_' . $current_screen->id . '_columns',
319 array( 'Bogo_Language_Packs_List_Table', 'define_columns' ),
320 10, 1
321 );
322 }
323
324 function bogo_tools_page() {
325 $list_table = new Bogo_Language_Packs_List_Table();
326 $list_table->prepare_items();
327
328 ?>
329 <div class="wrap">
330
331 <h1 class="wp-heading-inline"><?php
332 echo esc_html( __( 'Language Packs', 'bogo' ) );
333 ?></h1>
334
335 <?php
336 if ( ! empty( $_REQUEST['s'] ) ) {
337 echo sprintf(
338 '<span class="subtitle">%s</span>',
339 sprintf(
340 /* translators: %s: search query */
341 __( 'Search results for &#8220;%s&#8221;', 'bogo' ),
342 esc_html( $_REQUEST['s'] )
343 )
344 );
345 }
346 ?>
347
348 <hr class="wp-header-end">
349
350 <?php bogo_admin_notice(); ?>
351
352 <?php $list_table->views(); ?>
353
354 <form method="get" action="" id="bogo-language-packs">
355 <input type="hidden" name="page" value="<?php echo esc_attr( $_REQUEST['page'] ); ?>" />
356 <?php $list_table->search_box( __( 'Search Language', 'bogo' ), 'bogo-language-packs' ); ?>
357 <?php $list_table->display(); ?>
358 </form>
359
360 </div>
361 <?php
362 }
363
364 function bogo_load_texts_page() {
365 $action = $_POST['action'] ?? '';
366
367 if ( 'save' == $action ) {
368 check_admin_referer( 'bogo-edit-text-translation' );
369
370 if ( ! current_user_can( 'bogo_edit_terms_translation' ) ) {
371 wp_die( __( "You are not allowed to edit translations.", 'bogo' ) );
372 }
373
374 $locale = $_POST['locale'] ?? null;
375
376 if ( ! bogo_is_available_locale( $locale ) ) {
377 return;
378 }
379
380 if ( ! current_user_can( 'bogo_access_locale', $locale ) ) {
381 wp_die( __( "You are not allowed to edit terms in this locale.", 'bogo' ) );
382 }
383
384 $entries = array();
385
386 foreach ( (array) bogo_terms_translation( $locale ) as $item ) {
387 $translation = $item['translated'];
388
389 $cap = $item['cap'] ?? 'bogo_edit_terms_translation';
390
391 if ( isset( $_POST[$item['name']] )
392 and current_user_can( $cap ) ) {
393 $translation = $_POST[$item['name']];
394 }
395
396 $entries[] = array(
397 'singular' => $item['name'],
398 'translations' => array( $translation ),
399 'context' => preg_replace( '/:.*$/', '', $item['name'] ),
400 );
401 }
402
403 if ( Bogo_POMO::export( $locale, $entries ) ) {
404 $message = 'translation_saved';
405 } else {
406 $message = 'translation_failed';
407 }
408
409 $redirect_to = add_query_arg(
410 array(
411 'locale' => $locale,
412 'message' => $message,
413 'paged' => absint( $_POST['paged'] ?? 1 ),
414 ),
415 menu_page_url( 'bogo-texts', false )
416 );
417
418 wp_safe_redirect( $redirect_to );
419 exit();
420
421 } else {
422 $current_screen = get_current_screen();
423
424 add_filter( 'manage_' . $current_screen->id . '_columns',
425 array( 'Bogo_Terms_Translation_List_Table', 'define_columns' ),
426 10, 1
427 );
428
429 add_screen_option( 'per_page', array(
430 'default' => 20,
431 'option' => 'bogo_texts_per_page',
432 ) );
433 }
434 }
435
436 function bogo_texts_page() {
437 $list_table = new Bogo_Terms_Translation_List_Table();
438 $list_table->prepare_items();
439
440 ?>
441 <div class="wrap">
442
443 <h1 class="wp-heading-inline"><?php
444 echo esc_html( __( 'Terms Translation', 'bogo' ) );
445 ?></h1>
446
447 <?php
448 if ( ! empty( $_REQUEST['s'] ) ) {
449 echo sprintf(
450 '<span class="subtitle">%s</span>',
451 sprintf(
452 /* translators: %s: search query */
453 __( 'Search results for &#8220;%s&#8221;', 'bogo' ),
454 esc_html( $_REQUEST['s'] )
455 )
456 );
457 }
458 ?>
459
460 <hr class="wp-header-end">
461
462 <?php bogo_admin_notice(); ?>
463
464 <form action="" method="get">
465 <input type="hidden" name="page" value="<?php echo esc_attr( $_REQUEST['page'] ?? '' ); ?>" />
466 <input type="hidden" name="locale" value="<?php echo esc_attr( $_REQUEST['locale'] ?? '' ); ?>" />
467 <?php
468 $list_table->search_box(
469 __( 'Search Translation', 'bogo' ), 'bogo-terms-translation'
470 );
471 ?>
472 </form>
473
474 <form action="" method="post" id="bogo-terms-translation">
475 <input type="hidden" name="action" value="save" />
476 <input type="hidden" name="paged" value="<?php echo absint( $_GET['paged'] ?? '' ); ?>" />
477 <?php
478 wp_nonce_field( 'bogo-edit-text-translation' );
479 $list_table->display();
480 ?>
481 </form>
482 </div>
483 <?php
484 }
485
486 function bogo_admin_notice( $reason = '' ) {
487 if ( empty( $reason ) and isset( $_GET['message'] ) ) {
488 $reason = $_GET['message'];
489 }
490
491 if ( 'install_success' == $reason ) {
492 $message = __( "Translation installed successfully.", 'bogo' );
493 } elseif ( 'install_failed' == $reason ) {
494 $message = __( "Translation install failed.", 'bogo' );
495 } elseif ( 'promote_success' == $reason ) {
496 $message = __( "Site language set successfully.", 'bogo' );
497 } elseif ( 'promote_failed' == $reason ) {
498 $message = __( "Setting site language failed.", 'bogo' );
499 } elseif ( 'delete_success' == $reason ) {
500 $message = __( "Translation uninstalled successfully.", 'bogo' );
501 } elseif ( 'delete_failed' == $reason ) {
502 $message = __( "Translation uninstall failed.", 'bogo' );
503 } elseif ( 'enus_deactivated' == $reason ) {
504 $message = __( "English (United States) deactivated.", 'bogo' );
505 } elseif ( 'enus_activated' == $reason ) {
506 $message = __( "English (United States) activated.", 'bogo' );
507 } elseif ( 'translation_saved' == $reason ) {
508 $message = __( "Translation saved.", 'bogo' );
509 } elseif ( 'translation_failed' == $reason ) {
510 $message = __( "Saving translation failed.", 'bogo' );
511 } else {
512 return false;
513 }
514
515 if ( '_failed' == substr( $reason, -7 ) ) {
516 echo sprintf(
517 '<div class="error notice notice-error is-dismissible"><p>%s</p></div>',
518 esc_html( $message )
519 );
520 } else {
521 echo sprintf(
522 '<div class="updated notice notice-success is-dismissible"><p>%s</p></div>',
523 esc_html( $message )
524 );
525 }
526 }
527