PluginProbe
Polylang / 1.1
Polylang v1.1
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 1.1, at include/admin-base.php

220 lines 9.0 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 if (!$this->get_languages_list())
30 return;
31
32 // set text direction if the user set its own language
33 global $wpdb, $wp_locale;
34 $lang_id = $wpdb->get_var($wpdb->prepare("
35 SELECT t.term_id FROM $wpdb->terms AS t
36 INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
37 WHERE tt.taxonomy = 'language' AND tt.description = %s LIMIT 1",
38 get_locale()
39 )); // no function exists to get term by description
40 if ($lang_id)
41 $wp_locale->text_direction = get_metadata('term', $lang_id, '_rtl', true) ? 'rtl' : 'ltr';
42
43 // set user meta when choosing to filter content by language
44 // $_GET[lang] is used in ajax 'tag suggest' and is numeric when editing a language
45 if (!defined('DOING_AJAX') && !empty($_GET['lang']) && !is_numeric($_GET['lang']))
46 update_user_meta(get_current_user_id(), 'pll_filter_content', ($lang = $this->get_language($_GET['lang'])) ? $lang->slug : '');
47
48 // adds the languages in admin bar
49 // FIXME: OK for WP 3.2 and newer (the admin bar is not displayed on admin side for WP 3.1)
50 add_action('admin_bar_menu', array(&$this, 'admin_bar_menu'), 100); // 100 determines the position
51 }
52
53 // adds the link to the languages panel in the wordpress admin menu
54 function add_menus() {
55 add_submenu_page('options-general.php', $title = __('Languages', 'polylang'), $title, 'manage_options', 'mlang', array(&$this, 'languages_page'));
56 }
57
58 // setup js scripts & css styles (only on the relevant pages)
59 function admin_enqueue_scripts() {
60 $screen = get_current_screen();
61 $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
62
63 // for each script:
64 // 0 => the pages on which to load the script
65 // 1 => the scripts it needs to work
66 // 2 => 1 if loaded even if languages have not been defined yet, 0 otherwise
67 $scripts = array(
68 'admin' => array( array('settings_page_mlang'), array('jquery', 'wp-ajax-response', 'postbox'), 1 ),
69 'post' => array( array('post', 'media', 'async-upload', 'edit'), array('jquery', 'wp-ajax-response'), 0 ),
70 'term' => array( array('edit-tags'), array('jquery', 'wp-ajax-response'), 0 ),
71 'user' => array( array('profile', 'user-edit'), array('jquery'), 0 ),
72 );
73
74 foreach ($scripts as $script => $v)
75 if (in_array($screen->base, $v[0]) && ($v[2] || $this->get_languages_list()))
76 wp_enqueue_script('pll_'.$script, POLYLANG_URL .'/js/'.$script.$suffix.'.js', $v[1], POLYLANG_VERSION);
77
78 if (in_array($screen->base, array('settings_page_mlang', 'post', 'edit-tags', 'edit', 'upload', 'media')))
79 wp_enqueue_style('polylang_admin', POLYLANG_URL .'/css/admin'.$suffix.'.css', array(), POLYLANG_VERSION);
80 }
81
82 // downloads mofiles
83 function download_mo($locale, $upgrade = false) {
84 global $wp_version;
85 $mofile = WP_LANG_DIR."/$locale.mo";
86
87 // does file exists ?
88 if ((file_exists($mofile) && !$upgrade) || $locale == 'en_US')
89 return true;
90
91 // does language directory exists ?
92 if (!is_dir(WP_LANG_DIR)) {
93 if (!@mkdir(WP_LANG_DIR))
94 return false;
95 }
96
97 // will first look in tags/ (most languages) then in branches/ (only Greek ?)
98 $base = 'http://svn.automattic.com/wordpress-i18n/'.$locale;
99 $bases = array($base.'/tags/', $base.'/branches/');
100
101 foreach ($bases as $base) {
102 // get all the versions available in the subdirectory
103 $resp = wp_remote_get($base);
104 if (is_wp_error($resp) || 200 != $resp['response']['code'])
105 continue;
106
107 preg_match_all('#>([0-9\.]+)\/#', $resp['body'], $matches);
108 if (empty($matches[1]))
109 continue;
110
111 rsort($matches[1]); // sort from newest to oldest
112 $versions = $matches[1];
113
114 $newest = $upgrade ? $upgrade : $wp_version;
115 foreach ($versions as $key=>$version) {
116 // will not try to download a too recent mofile
117 if (version_compare($version, $newest, '>'))
118 unset($versions[$key]);
119 // will not download an older version if we are upgrading
120 if ($upgrade && version_compare($version, $wp_version, '<='))
121 unset($versions[$key]);
122 }
123
124 $versions = array_splice($versions, 0, 5); // reduce the number of versions to test to 5
125 $args = array('timeout' => 30, 'stream' => true);
126
127 // try to download the file
128 foreach ($versions as $version) {
129 $resp = wp_remote_get($base."$version/messages/$locale.mo", $args + array('filename' => $mofile));
130 if (is_wp_error($resp) || 200 != $resp['response']['code']) {
131 unlink($mofile); // otherwise we download a gzipped 404 page
132 continue;
133 }
134 // try to download ms and continents-cities files if exist (will not return false if failed)
135 // with new files introduced in WP 3.4
136 foreach (array('ms', 'continents-cities', 'admin', 'admin-network') as $file) {
137 $resp = wp_remote_get($base."$version/messages/$file-$locale.mo", $args + array('filename' => WP_LANG_DIR."/$file-$locale.mo"));
138 if (is_wp_error($resp) || 200 != $resp['response']['code'])
139 unlink(WP_LANG_DIR."/$file-$locale.mo");
140 }
141 // try to download theme files if exist (will not return false if failed)
142 // FIXME not updated when the theme is updated outside a core update
143 foreach (array('twentyten', 'twentyeleven', 'twentytwelve', 'twentythirteen') as $theme) {
144 if (!is_dir($theme_dir = get_theme_root()."/$theme/languages"))
145 continue; // the theme is not present
146
147 $resp = wp_remote_get($base."$version/messages/$theme/$locale.mo", $args + array('filename' => "$theme_dir/$locale.mo"));
148 if (is_wp_error($resp) || 200 != $resp['response']['code'])
149 unlink("$theme_dir/$locale.mo");
150 }
151 return true;
152 }
153 }
154 // we did not succeeded to download a file :(
155 return false;
156 }
157
158 // returns options available for the language switcher (menu or widget)
159 function get_switcher_options($type = 'widget', $key ='string') {
160 $options = array(
161 'show_names' => array('string' => __('Displays language names', 'polylang'), 'default' => 1),
162 'show_flags' => array('string' => __('Displays flags', 'polylang'), 'default' => 0),
163 'force_home' => array('string' => __('Forces link to front page', 'polylang'), 'default' => 0),
164 'hide_current' => array('string' => __('Hides the current language', 'polylang'), 'default' => 0),
165 );
166
167 if ($type != 'menu')
168 $options['dropdown'] = array('string' => __('Displays as dropdown', 'polylang'), 'default' => 0);
169
170 return array_map(create_function('$v', "return \$v['$key'];"), $options);
171 }
172
173 // register strings for translation making sure it is not duplicate or empty
174 function register_string($name, $string, $context = 'polylang', $multiline = false) {
175 // backward compatibility with Polylang older than 1.1
176 if (is_bool($context)) {
177 $multiline = $context;
178 $context = 'polylang';
179 }
180
181 $to_register = compact('name', 'string', 'context', 'multiline');
182 if (!in_array($to_register, $this->strings) && $to_register['string'])
183 $this->strings[] = $to_register;
184 }
185
186 // adds the languages in admin bar
187 function admin_bar_menu($wp_admin_bar) {
188 $url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
189
190 // $_GET['lang'] is numeric when editing a language, not when selecting a new language in the filter
191 $selected = !empty($_GET['lang']) && !is_numeric($_GET['lang']) && ($lang = $this->get_language($_GET['lang'])) ? $lang->slug :
192 (($lg = get_user_meta(get_current_user_id(), 'pll_filter_content', true)) ? $lg : 'all');
193
194 $all_item = array((object) array('slug' => 'all', 'name' => __('Show all languages', 'polylang')));
195
196 $wp_admin_bar->add_menu(array(
197 'id' => 'languages',
198 'title' => __('Languages', 'polylang'),
199 'meta' => array('title' => __('Filters content by language', 'polylang')),
200 ));
201
202 foreach (array_merge($all_item, $this->get_languages_list()) as $lang) {
203 $href = add_query_arg('lang', $lang->slug, $url);
204 $wp_admin_bar->add_menu(array(
205 'parent' => 'languages',
206 'id' => $lang->slug,
207 'title' => sprintf(
208 '<input name="language" type="radio" onclick="location.href=%s" value="%s" %s /> %s',
209 "'" . $href . "'", // onclick is needed for Chrome browser, thanks to RavanH for the bug report and fix
210 esc_attr($lang->slug),
211 $selected == $lang->slug ? 'checked="checked"' : '',
212 esc_html($lang->name)
213 ),
214 'href' => $href,
215 ));
216 }
217 }
218
219 } // class Polylang_Admin_Base
220