PluginProbe
Polylang / 1.1.5
Polylang v1.1.5
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.5, at include/admin-base.php

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