PluginProbe
Polylang / 0.9
Polylang v0.9
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / include / admin-base.php

admin-base.php in Polylang 0.9, at include/admin-base.php

202 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // functions common to all admin panels
4 class Polylang_Admin_Base extends Polylang_Base {
5 function __construct() {
6 parent::__construct();
7
8 // filter admin language for users
9 add_filter('locale', array(&$this, 'get_locale'));
10
11 // set user preferences
12 add_action('admin_init', array(&$this, 'admin_init_base'));
13
14 // adds the link to the languages panel in the wordpress admin menu
15 add_action('admin_menu', array(&$this, 'add_menus'));
16
17 // setup js scripts and css styles
18 add_action('admin_enqueue_scripts', array(&$this, 'admin_enqueue_scripts'));
19 }
20
21 // returns the locale based on user preference
22 function get_locale($locale) {
23 // get_current_user_id uses wp_get_current_user which may not be available the first time(s) get_locale is called
24 return function_exists('wp_get_current_user') && ($loc = get_user_meta(get_current_user_id(), 'user_lang', 'true')) ? $loc : $locale;
25 }
26
27 // set user preferences
28 function admin_init_base() {
29 // set text direction if the user set its own language
30 global $wpdb, $wp_locale;
31 $lang_id = $wpdb->get_var($wpdb->prepare("SELECT t.term_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
32 WHERE tt.taxonomy = 'language' AND tt.description = %s LIMIT 1", get_locale())); // no function exists to get term by description
33 if ($lang_id)
34 $wp_locale->text_direction = get_metadata('term', $lang_id, '_rtl', true) ? 'rtl' : 'ltr';
35
36 // set user meta when choosing to filter content by language
37 if (isset($_GET['lang']) && $_GET['lang'])
38 update_user_meta(get_current_user_id(), 'pll_filter_content', $_GET['lang'] == 'all' ? '' : $_GET['lang']);
39
40 if (!$this->get_languages_list())
41 return;
42
43 // adds the languages in admin bar
44 // FIXME: OK for WP 3.2 and newer (the admin bar is not displayed on admin side for WP 3.1)
45 add_action('admin_bar_menu', array(&$this, 'admin_bar_menu'), 100); // 100 determines the position
46 }
47
48 // adds the link to the languages panel in the wordpress admin menu
49 function add_menus() {
50 add_submenu_page('options-general.php', __('Languages', 'polylang'), __('Languages', 'polylang'), 'manage_options', 'mlang', array(&$this, 'languages_page'));
51
52 // adds the about box the languages admin panel
53 // test of $_GET['tab'] avoids displaying the automatically generated screen options on other tabs
54 if (PLL_DISPLAY_ABOUT && isset($_GET['page']) && $_GET['page'] == 'mlang' && (!isset($_GET['tab']) || $_GET['tab'] == 'lang'))
55 add_meta_box('pll_about_box', __('About Polylang', 'polylang'), array(&$this,'about'), 'settings_page_mlang', 'normal', 'high');
56 }
57
58 // setup js scripts & css styles (only on the relevant pages)
59 function admin_enqueue_scripts() {
60 $screen = get_current_screen();
61
62 $scripts = array(
63 'admin' => array( array('settings_page_mlang'), array('jquery', 'wp-ajax-response', 'postbox') ),
64 'post' => array( array('post', 'media', 'async-upload', 'edit'), array('jquery', 'wp-ajax-response') ),
65 'term' => array( array('edit-tags'), array('jquery', 'wp-ajax-response') ),
66 'user' => array( array('profile', 'user-edit'), array('jquery') ),
67 );
68
69 foreach ($scripts as $script => $v)
70 if (in_array($screen->base, $v[0]))
71 wp_enqueue_script('pll_'.$script, POLYLANG_URL .'/js/'.$script.'.js', $v[1], POLYLANG_VERSION);
72
73 if (in_array($screen->base, array('settings_page_mlang', 'post', 'edit-tags', 'edit', 'upload', 'media')))
74 wp_enqueue_style('polylang_admin', POLYLANG_URL .'/css/admin.css', array(), POLYLANG_VERSION);
75 }
76
77 // downloads mofiles
78 function download_mo($locale, $upgrade = false) {
79 global $wp_version;
80 $mofile = WP_LANG_DIR."/$locale.mo";
81
82 // does file exists ?
83 if ((file_exists($mofile) && !$upgrade) || $locale == 'en_US')
84 return true;
85
86 // does language directory exists ?
87 if (!is_dir(WP_LANG_DIR)) {
88 if (!@mkdir(WP_LANG_DIR))
89 return false;
90 }
91
92 // will first look in tags/ (most languages) then in branches/ (only Greek ?)
93 $base = 'http://svn.automattic.com/wordpress-i18n/'.$locale;
94 $bases = array($base.'/tags/', $base.'/branches/');
95
96 foreach ($bases as $base) {
97 // get all the versions available in the subdirectory
98 $resp = wp_remote_get($base);
99 if (is_wp_error($resp) || 200 != $resp['response']['code'])
100 continue;
101
102 preg_match_all('#>([0-9\.]+)\/#', $resp['body'], $matches);
103 if (empty($matches[1]))
104 continue;
105
106 rsort($matches[1]); // sort from newest to oldest
107 $versions = $matches[1];
108
109 $newest = $upgrade ? $upgrade : $wp_version;
110 foreach ($versions as $key=>$version) {
111 // will not try to download a too recent mofile
112 if (version_compare($version, $newest, '>'))
113 unset($versions[$key]);
114 // will not download an older version if we are upgrading
115 if ($upgrade && version_compare($version, $wp_version, '<='))
116 unset($versions[$key]);
117 }
118
119 $versions = array_splice($versions, 0, 5); // reduce the number of versions to test to 5
120 $args = array('timeout' => 30, 'stream' => true);
121
122 // try to download the file
123 foreach ($versions as $version) {
124 $resp = wp_remote_get($base."$version/messages/$locale.mo", $args + array('filename' => $mofile));
125 if (is_wp_error($resp) || 200 != $resp['response']['code'])
126 continue;
127
128 // try to download ms and continents-cities files if exist (will not return false if failed)
129 // with new files introduced in WP 3.4
130 foreach (array("ms", "continent-cities", "admin", "admin-network") as $file)
131 wp_remote_get($base."$version/messages/$file-$locale.mo", $args + array('filename' => WP_LANG_DIR."/$file-$locale.mo"));
132
133 // try to download theme files if exist (will not return false if failed)
134 // FIXME not updated when the theme is updated outside a core update
135 foreach (array("twentyten", "twentyeleven", "twentytwelve") as $theme)
136 wp_remote_get($base."$version/messages/$theme/$locale.mo", $args + array('filename' => get_theme_root()."/$theme/languages/$locale.mo"));
137
138 return true;
139 }
140 }
141 // we did not succeeded to download a file :(
142 return false;
143 }
144
145 // returns options available for the language switcher (menu or widget)
146 // FIXME do not include the dropdown in menu yet since I need to work on js
147 function get_switcher_options($type = 'widget', $key ='string') {
148 $options = array (
149 'show_names' => array('string' => __('Displays language names', 'polylang'), 'default' => 1),
150 'show_flags' => array('string' => __('Displays flags', 'polylang'), 'default' => 0),
151 'force_home' => array('string' => __('Forces link to front page', 'polylang'), 'default' => 0),
152 'hide_current' => array('string' => __('Hides the current language', 'polylang'), 'default' => 0),
153 );
154 $menu_options = array('switcher' => array('string' => __('Displays a language switcher at the end of the menu', 'polylang'), 'default' => 0));
155 $widget_options = array('dropdown' => array('string' => __('Displays as dropdown', 'polylang'), 'default' => 0));
156 $options = ($type == 'menu') ? array_merge($menu_options, $options) : array_merge($options, $widget_options);
157 return array_map(create_function('$v', "return \$v['$key'];"), $options);
158 }
159
160 // register strings for translation making sure it is not duplicate or empty
161 function register_string($name, $string, $multiline = false) {
162 $to_register = array('name'=> $name, 'string' => $string, 'multiline' => $multiline);
163 if (!in_array($to_register, $this->strings) && $to_register['string'])
164 $this->strings[] = $to_register;
165 }
166
167 // adds the languages in admin bar
168 function admin_bar_menu($wp_admin_bar) {
169 $url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
170
171 $selected = isset($_GET['lang']) && $_GET['lang'] ? $_GET['lang'] :
172 (($lg = get_user_meta(get_current_user_id(), 'pll_filter_content', true)) ? $lg : 'all');
173
174 $wp_admin_bar->add_menu(array(
175 'id' => 'languages',
176 'title' => __('Languages', 'polylang'),
177 'meta' => array('title' => __('Filters content by language', 'polylang')),
178 ));
179
180 $wp_admin_bar->add_menu(array(
181 'parent' => 'languages',
182 'id' => 'all',
183 'title' => sprintf('<input name="all" type="radio" value="1" %s /> %s',
184 $selected == 'all' ? 'checked="checked"' : '', __('Show all languages', 'polylang')),
185 'href' => add_query_arg('lang', 'all', $url),
186 ));
187
188 foreach ($this->get_languages_list() as $lang) {
189 $wp_admin_bar->add_menu(array(
190 'parent' => 'languages',
191 'id' => $lang->slug,
192 'title' => sprintf('<input name="%s" type="radio" value="1" %s /> %s',
193 $lang->slug, $selected == $lang->slug ? 'checked="checked"' : '', $lang->name),
194 'href' => add_query_arg('lang', $lang->slug, $url),
195 ));
196 }
197 }
198
199 } // class Polylang_Admin_Base
200
201 ?>
202