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

381 lines 10.9 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 'rest_api' => array(
40 'url' => trailingslashit( rest_url( 'bogo/v1' ) ),
41 'nonce' => wp_create_nonce( 'wp_rest' ) ) );
42
43 if ( 'nav-menus.php' == $hook_suffix ) {
44 $nav_menu_id = absint( get_user_option( 'nav_menu_recently_edited' ) );
45 $nav_menu_items = wp_get_nav_menu_items( $nav_menu_id );
46 $locales = array();
47
48 foreach ( (array) $nav_menu_items as $item ) {
49 $locales[$item->db_id] = $item->bogo_locales;
50 }
51
52 $local_args = array_merge( $local_args, array(
53 'availableLanguages' => bogo_available_languages( array(
54 'exclude_enus_if_inactive' => true,
55 'orderby' => 'value' ) ),
56 'locales' => $locales,
57 'selectorLegend' => __( 'Displayed on pages in', 'bogo' ),
58 'cbPrefix' => 'menu-item-bogo-locale' ) );
59 }
60
61 if ( 'options-general.php' == $hook_suffix ) {
62 $local_args = array_merge( $local_args, array(
63 'defaultLocale' => bogo_get_default_locale() ) );
64 }
65
66 if ( 'post.php' == $hook_suffix && ! empty( $GLOBALS['post'] ) ) {
67 $post = $GLOBALS['post'];
68 $local_args = array_merge( $local_args, array(
69 'post_id' => $post->ID ) );
70 }
71
72 wp_localize_script( 'bogo-admin', '_bogo', $local_args );
73 }
74
75 add_action( 'admin_menu', 'bogo_admin_menu' );
76
77 function bogo_admin_menu() {
78 $tools = add_management_page(
79 __( 'Bogo Tools', 'bogo' ), __( 'Bogo', 'bogo' ),
80 'update_core', 'bogo-tools', 'bogo_tools_page' );
81
82 add_action( 'load-' . $tools, 'bogo_load_tools_page' );
83 }
84
85 function bogo_load_tools_page() {
86 require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
87
88 $action = isset( $_GET['action'] ) ? $_GET['action'] : '';
89
90 if ( 'install_translation' == $action ) {
91 check_admin_referer( 'bogo-tools' );
92
93 if ( ! current_user_can( 'update_core' ) ) {
94 wp_die( __( 'You are not allowed to install translations.', 'bogo' ) );
95 }
96
97 $locale = isset( $_GET['locale'] ) ? $_GET['locale'] : null;
98
99 if ( wp_download_language_pack( $locale ) ) {
100 $redirect_to = add_query_arg(
101 array( 'locale' => $locale, 'message' => 'install_success' ),
102 menu_page_url( 'bogo-tools', false ) );
103 } else {
104 $redirect_to = add_query_arg(
105 array( 'locale' => $locale, 'message' => 'install_failed' ),
106 menu_page_url( 'bogo-tools', false ) );
107 }
108
109 wp_safe_redirect( $redirect_to );
110 exit();
111 }
112
113 if ( 'delete_translation' == $action ) {
114 check_admin_referer( 'bogo-tools' );
115
116 if ( ! current_user_can( 'update_core' ) ) {
117 wp_die( __( 'You are not allowed to delete translations.', 'bogo' ) );
118 }
119
120 $locale = isset( $_GET['locale'] ) ? $_GET['locale'] : null;
121
122 if ( bogo_delete_language_pack( $locale ) ) {
123 $redirect_to = add_query_arg(
124 array( 'locale' => $locale, 'message' => 'delete_success' ),
125 menu_page_url( 'bogo-tools', false ) );
126 } else {
127 $redirect_to = add_query_arg(
128 array( 'locale' => $locale, 'message' => 'delete_failed' ),
129 menu_page_url( 'bogo-tools', false ) );
130 }
131
132 wp_safe_redirect( $redirect_to );
133 exit();
134 }
135
136 if ( 'deactivate_enus' == $action ) {
137 check_admin_referer( 'bogo-tools' );
138
139 bogo_set_prop( 'enus_deactivated', true );
140
141 $redirect_to = add_query_arg(
142 array( 'message' => 'enus_deactivated' ),
143 menu_page_url( 'bogo-tools', false ) );
144
145 wp_safe_redirect( $redirect_to );
146 exit();
147 }
148
149 if ( 'activate_enus' == $action ) {
150 check_admin_referer( 'bogo-tools' );
151
152 bogo_set_prop( 'enus_deactivated', false );
153
154 $redirect_to = add_query_arg(
155 array( 'message' => 'enus_activated' ),
156 menu_page_url( 'bogo-tools', false ) );
157
158 wp_safe_redirect( $redirect_to );
159 exit();
160 }
161 }
162
163 function bogo_tools_page() {
164 $enus_deactivated = bogo_get_prop( 'enus_deactivated' );
165 $can_install = wp_can_install_language_pack();
166
167 $default_locale = bogo_get_default_locale();
168 $available_locales = bogo_available_locales();
169
170 ?>
171 <div class="wrap">
172
173 <h2><?php echo esc_html( __( 'Bogo Tools', 'bogo' ) ); ?></h2>
174
175 <?php bogo_admin_notice(); ?>
176
177 <h3 class="title"><?php echo esc_html( __( 'Available Languages', 'bogo' ) ); ?></h3>
178
179 <table id="bogo-languages-table" class="widefat">
180 <thead>
181 <tr><th></th><th><?php echo esc_html( __( 'Language', 'bogo' ) ); ?></th></tr>
182 </thead>
183 <tfoot>
184 <tr><th></th><th><?php echo esc_html( __( 'Language', 'bogo' ) ); ?></th></tr>
185 </tfoot>
186 <tbody id="translations">
187 <tr class="active"><th>1</th><td>
188 <strong><?php echo esc_html( bogo_get_language( $default_locale ) ); ?></strong>
189 [<?php echo esc_html( $default_locale ); ?>]
190 <div class="status"><span class="status"><?php echo esc_html( __( 'Site Default Language', 'bogo' ) ); ?></span></div>
191 </td></tr>
192
193 <?php
194 $count = 1;
195
196 foreach ( $available_locales as $locale ) {
197 if ( $locale == $default_locale ) {
198 continue;
199 }
200
201 $count += 1;
202 $class = 'active';
203
204 if ( 'en_US' == $locale ) {
205 $status = $enus_deactivated
206 ? esc_html( __( 'Inactive', 'bogo' ) )
207 : esc_html( __( 'Active', 'bogo' ) );
208 } else {
209 $status = esc_html( __( 'Installed', 'bogo' ) );
210 }
211
212 if ( 'en_US' == $locale ) {
213 if ( $enus_deactivated ) {
214 $activate_link = menu_page_url( 'bogo-tools', false );
215 $activate_link = add_query_arg(
216 array( 'action' => 'activate_enus' ),
217 $activate_link );
218 $activate_link = wp_nonce_url( $activate_link, 'bogo-tools' );
219 $activate_link = sprintf(
220 '<a href="%1$s" class="activate">%2$s</a>',
221 $activate_link,
222 esc_html( __( 'Activate', 'bogo' ) ) );
223
224 $status = sprintf(
225 '<div class="status"><span class="status">%s</span> | %s</div>',
226 $status, $activate_link );
227 $class = '';
228 } else {
229 $deactivate_link = menu_page_url( 'bogo-tools', false );
230 $deactivate_link = add_query_arg(
231 array( 'action' => 'deactivate_enus' ),
232 $deactivate_link );
233 $deactivate_link = wp_nonce_url( $deactivate_link, 'bogo-tools' );
234 $deactivate_link = sprintf(
235 '<a href="%1$s" class="deactivate">%2$s</a>',
236 $deactivate_link,
237 esc_html( __( 'Deactivate', 'bogo' ) ) );
238
239 $status = sprintf(
240 '<div class="status"><span class="status">%s</span> | %s</div>',
241 $status, $deactivate_link );
242 }
243 } elseif ( $can_install ) {
244 $delete_link = menu_page_url( 'bogo-tools', false );
245 $delete_link = add_query_arg(
246 array( 'action' => 'delete_translation', 'locale' => $locale ),
247 $delete_link );
248 $delete_link = wp_nonce_url( $delete_link, 'bogo-tools' );
249
250 if ( ! $delete_language = bogo_get_language( $locale ) ) {
251 $delete_language = $locale;
252 }
253
254 $delete_confirm = sprintf( __( "You are about to delete %s language pack.\n 'Cancel' to stop, 'OK' to delete.", 'bogo' ), $delete_language );
255
256 $delete_link = sprintf( '<a href="%1$s" class="delete" onclick="if (confirm(\'%3$s\')){return true;} return false;">%2$s</a>',
257 $delete_link,
258 esc_html( __( 'Delete', 'bogo' ) ),
259 esc_js( $delete_confirm ) );
260
261 $status = sprintf(
262 '<div class="status"><span class="status">%s</span> | %s</div>',
263 $status, $delete_link );
264 }
265 ?>
266 <tr class="<?php echo esc_attr( $class ); ?>">
267 <th><?php echo $count; ?></th>
268 <td>
269 <strong><?php echo esc_html( bogo_get_language( $locale ) ); ?></strong>
270 [<?php echo esc_html( $locale ); ?>]
271 <?php echo $status; ?>
272 </td>
273 </tr>
274 <?php
275 }
276
277 foreach ( wp_get_available_translations() as $locale => $translation ) {
278 if ( in_array( $locale, $available_locales ) ) {
279 continue;
280 }
281
282 $count += 1;
283
284 $install_link = '';
285
286 if ( $can_install ) {
287 $install_link = menu_page_url( 'bogo-tools', false );
288 $install_link = add_query_arg(
289 array( 'action' => 'install_translation', 'locale' => $locale ),
290 $install_link );
291 $install_link = wp_nonce_url( $install_link, 'bogo-tools' );
292 $install_link = sprintf( '<a href="%1$s" class="install">%2$s</a>',
293 $install_link, esc_html( __( 'Install', 'bogo' ) ) );
294 }
295 ?>
296 <tr><th><?php echo $count; ?></th><td>
297 <strong><?php echo esc_html( bogo_get_language( $locale ) ); ?></strong>
298 [<?php echo esc_html( $locale ); ?>]
299 <?php echo $install_link; ?>
300 </td></tr>
301 <?php
302 }
303 ?>
304
305 </tbody>
306 </table>
307
308 </div>
309 <?php
310 }
311
312 function bogo_admin_notice( $reason = '' ) {
313 if ( empty( $reason ) && isset( $_GET['message'] ) ) {
314 $reason = $_GET['message'];
315 }
316
317 if ( 'install_success' == $reason ) {
318 $message = __( "Translation installed successfully.", 'bogo' );
319 } elseif ( 'install_failed' == $reason ) {
320 $message = __( "Translation install failed.", 'bogo' );
321 } elseif ( 'delete_success' == $reason ) {
322 $message = __( "Translation uninstalled successfully.", 'bogo' );
323 } elseif ( 'delete_failed' == $reason ) {
324 $message = __( "Translation uninstall failed.", 'bogo' );
325 } elseif ( 'enus_deactivated' == $reason ) {
326 $message = __( "English (United States) deactivated.", 'bogo' );
327 } elseif ( 'enus_activated' == $reason ) {
328 $message = __( "English (United States) activated.", 'bogo' );
329 } else {
330 return false;
331 }
332
333 if ( 'install_failed' == $reason || 'delete_failed' == $reason ) {
334 echo sprintf(
335 '<div class="error notice notice-error is-dismissible"><p>%s</p></div>',
336 esc_html( $message ) );
337 } else {
338 echo sprintf(
339 '<div class="updated notice notice-success is-dismissible"><p>%s</p></div>',
340 esc_html( $message ) );
341 }
342 }
343
344 function bogo_delete_language_pack( $locale ) {
345 if ( 'en_US' == $locale
346 || ! bogo_is_available_locale( $locale )
347 || bogo_is_default_locale( $locale ) ) {
348 return false;
349 }
350
351 if ( ! is_dir( WP_LANG_DIR ) || ! $files = scandir( WP_LANG_DIR ) ) {
352 return false;
353 }
354
355 $target_files = array(
356 sprintf( '%s.mo', $locale ),
357 sprintf( '%s.po', $locale ),
358 sprintf( 'admin-%s.mo', $locale ),
359 sprintf( 'admin-%s.po', $locale ),
360 sprintf( 'admin-network-%s.mo', $locale ),
361 sprintf( 'admin-network-%s.po', $locale ),
362 sprintf( 'continents-cities-%s.mo', $locale ),
363 sprintf( 'continents-cities-%s.po', $locale ) );
364
365 foreach ( $files as $file ) {
366 if ( '.' === $file[0] || is_dir( $file ) ) {
367 continue;
368 }
369
370 if ( in_array( $file, $target_files ) ) {
371 $result = @unlink( path_join( WP_LANG_DIR, $file ) );
372
373 if ( ! $result ) {
374 return false;
375 }
376 }
377 }
378
379 return true;
380 }
381