PluginProbe
Polylang / 1.3
Polylang v1.3
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 / admin / admin.php

admin.php in Polylang 1.3, at admin/admin.php

323 lines 11.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * admin side controller
5 *
6 * @since 1.2
7 */
8 class PLL_Admin extends PLL_Base {
9 public $pref_lang;
10 public $settings_page, $filters, $filters_columns, $filters_post, $filters_term, $nav_menu, $sync, $filters_media;
11
12 /*
13 * loads the polylang text domain
14 * setups filters and action needed on all admin pages and on plugins page
15 * loads the settings pages or the filters base on the request
16 * manages the admin language filter and the "admin preferred language"
17 *
18 * @since 1.2
19 *
20 * @param object $links_model
21 */
22 public function __construct(&$links_model) {
23 parent::__construct($links_model);
24
25 // plugin i18n, only needed for backend
26 load_plugin_textdomain('polylang', false, basename(POLYLANG_DIR).'/languages');
27
28 // adds the link to the languages panel in the wordpress admin menu
29 add_action('admin_menu', array(&$this, 'add_menus'));
30
31 // setup js scripts and css styles
32 add_action('admin_enqueue_scripts', array(&$this, 'admin_enqueue_scripts'));
33
34 // adds a 'settings' link in the plugins table
35 add_filter('plugin_action_links_' . POLYLANG_BASENAME, array(&$this, 'plugin_action_links'));
36 add_action('in_plugin_update_message-' . POLYLANG_BASENAME, array(&$this, 'plugin_update_message'), 10, 2);
37 }
38
39 /*
40 * loads the polylang text domain
41 * setups filters and action needed on all admin pages and on plugins page
42 * loads the settings pages or the filters base on the request
43 * manages the admin language filter and the "admin preferred language"
44 *
45 * @since 1.2
46 *
47 * @param object $links_model
48 */
49 public function init() {
50 if (PLL_SETTINGS)
51 $this->settings_page = new PLL_Settings($this->model);
52
53 if (!$this->model->get_languages_list())
54 return;
55
56 $this->links = new PLL_Links($this->links_model);
57
58 // filter admin language for users
59 // we must not call user info before WordPress defines user roles in wp-settings.php
60 add_filter('setup_theme', array(&$this, 'init_user'));
61 add_filter('locale', array(&$this, 'get_locale'));
62
63 // adds the languages in admin bar
64 // FIXME: OK for WP 3.2 and newer (the admin bar is not displayed on admin side for WP 3.1)
65 add_action('admin_bar_menu', array(&$this, 'admin_bar_menu'), 100); // 100 determines the position
66
67 // setup filters for admin pages
68 if (!PLL_SETTINGS)
69 add_action('wp_loaded', array(&$this, 'add_filters'));
70 }
71
72 /*
73 * adds the link to the languages panel in the wordpress admin menu
74 *
75 * @since 0.1
76 */
77 public function add_menus() {
78 add_submenu_page('options-general.php', $title = __('Languages', 'polylang'), $title, 'manage_options', 'mlang',
79 PLL_SETTINGS ? array($this->settings_page, 'languages_page') : create_function('', ''));
80 }
81
82 /*
83 * setup js scripts & css styles (only on the relevant pages)
84 *
85 * @since 0.6
86 */
87 public function admin_enqueue_scripts() {
88 $screen = get_current_screen();
89 $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
90
91 // for each script:
92 // 0 => the pages on which to load the script
93 // 1 => the scripts it needs to work
94 // 2 => 1 if loaded even if languages have not been defined yet, 0 otherwise
95 $scripts = array(
96 'admin' => array( array('settings_page_mlang'), array('jquery', 'wp-ajax-response', 'postbox'), 1 ),
97 'post' => array( array('post', 'media', 'async-upload', 'edit'), array('jquery', 'wp-ajax-response', 'inline-edit-post'), 0 ),
98 'term' => array( array('edit-tags'), array('jquery', 'wp-ajax-response', 'inline-edit-tax'), 0 ),
99 'user' => array( array('profile', 'user-edit'), array('jquery'), 0 ),
100 );
101
102 foreach ($scripts as $script => $v)
103 if (in_array($screen->base, $v[0]) && ($v[2] || $this->model->get_languages_list()))
104 wp_enqueue_script('pll_'.$script, POLYLANG_URL .'/js/'.$script.$suffix.'.js', $v[1], POLYLANG_VERSION);
105
106 wp_enqueue_style('polylang_admin', POLYLANG_URL .'/css/admin'.$suffix.'.css', array(), POLYLANG_VERSION);
107
108 // backward compatibility WP < 3.8
109 // don't load this for old versions
110 if (version_compare($GLOBALS['wp_version'], '3.8alpha' , '>='))
111 wp_enqueue_style('polylang_admin_mobi', POLYLANG_URL .'/css/admin-mobi'.$suffix.'.css', array(), POLYLANG_VERSION);
112 }
113
114 /*
115 * adds a 'settings' link in the plugins table
116 *
117 * @since 0.1
118 *
119 * @param array $links list of links associated to the plugin
120 * @return array modified list of links
121 */
122 public function plugin_action_links($links) {
123 array_unshift($links, '<a href="admin.php?page=mlang">' . __('Settings', 'polylang') . '</a>');
124 return $links;
125 }
126
127 /*
128 * adds the upgrade notice in plugins table
129 *
130 * @since 1.1.6
131 *
132 * @param array $plugin_data not used
133 * @param object $r plugin update data
134 */
135 function plugin_update_message($plugin_data, $r) {
136 if (isset($r->upgrade_notice))
137 printf('<p style="margin: 3px 0 0 0; border-top: 1px solid #ddd; padding-top: 3px">%s</p>', $r->upgrade_notice);
138 }
139
140 /*
141 * defines the backend language and the admin language filter based on user preferences
142 *
143 * @since 1.2.3
144 */
145 public function init_user() {
146 // admin language filter
147 if (!defined('DOING_AJAX') && !empty($_GET['lang']) && !is_numeric($_GET['lang']))
148 update_user_meta(get_current_user_id(), 'pll_filter_content', ($lang = $this->model->get_language($_GET['lang'])) ? $lang->slug : '');
149
150 // set preferred language for use in filters
151 $this->pref_lang = $this->model->get_language(($lg = get_user_meta(get_current_user_id(), 'pll_filter_content', true)) ? $lg : $this->options['default_lang']);
152 $this->pref_lang = apply_filters('pll_admin_preferred_language', $this->pref_lang);
153
154 // inform that the admin language has been set
155 // only if the admin language is one of the Polylang defined language
156 if ($curlang = $this->model->get_language(get_locale())) {
157 $GLOBALS['text_direction'] = $curlang->is_rtl ? 'rtl' : 'ltr'; // force text direction according to language setting
158 do_action('pll_language_defined', $curlang->slug, $curlang);
159 }
160 else
161 do_action('pll_no_language_defined'); // to load overriden textdomains
162 }
163
164 /*
165 * get the locale based on user preference
166 *
167 * @since 0.4
168 *
169 * @param string $locale
170 * @return string modified locale
171 */
172 public function get_locale($locale) {
173 return ($loc = get_user_meta(get_current_user_id(), 'user_lang', 'true')) ? $loc : $locale;
174 }
175
176 /*
177 * setup filters for admin pages
178 *
179 * @since 1.2
180 */
181 public function add_filters() {
182 $this->filters = new PLL_Admin_Filters($this->links_model, $this->pref_lang);
183 $this->filters_columns = new PLL_Admin_Filters_Columns($this->model);
184 $this->filters_post = new PLL_Admin_Filters_Post($this->model, $this->pref_lang);
185 $this->filters_term = new PLL_Admin_Filters_Term($this->model, $this->pref_lang);
186 $this->nav_menu = new PLL_Admin_Nav_Menu($this->model);
187 $this->sync = new PLL_Admin_Sync($this->model);
188
189 if ($this->options['media_support'])
190 $this->filters_media = new PLL_Admin_Filters_Media($this->model, $this->pref_lang);
191 }
192
193 /*
194 * adds the languages list in admin bar for the admin languages filter
195 *
196 * @since 0.9
197 *
198 * @param object $wp_admin_bar
199 */
200 public function admin_bar_menu($wp_admin_bar) {
201 $url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
202
203 $all_item = (object) array(
204 'slug' => 'all',
205 'name' => __('Show all languages', 'polylang'),
206 'flag' => '<span class="ab-icon"></span>'
207 );
208
209 // $_GET['lang'] is numeric when editing a language, not when selecting a new language in the filter
210 $selected = !empty($_GET['lang']) && !is_numeric($_GET['lang']) && ($lang = $this->model->get_language($_GET['lang'])) ? $lang->slug :
211 (($lg = get_user_meta(get_current_user_id(), 'pll_filter_content', true)) ? $lg : 'all');
212
213 $selected = ('all' == $selected) ? $all_item : $this->model->get_language($selected);
214
215 $wp_admin_bar->add_menu(array(
216 'id' => 'languages',
217 'title' => $selected->flag . '<span class="ab-label">'. esc_html($selected->name) . '</span>',
218 'meta' => array('title' => __('Filters content by language', 'polylang')),
219 ));
220
221 foreach (array_merge(array($all_item), $this->model->get_languages_list()) as $lang) {
222 if ($selected->slug == $lang->slug)
223 continue;
224
225 $img = empty($lang->flag) ? '' : (false !== strpos($lang->flag, 'img') ? $lang->flag . '&nbsp;' : $lang->flag);
226
227 $wp_admin_bar->add_menu(array(
228 'parent' => 'languages',
229 'id' => $lang->slug,
230 'title' => $lang->flag . esc_html($lang->name),
231 'href' => esc_url(add_query_arg('lang', $lang->slug, remove_query_arg('paged',$url))),
232 ));
233 }
234 }
235
236 /*
237 * downloads mofiles from http://svn.automattic.com/wordpress-i18n/
238 * FIXME is it the best class for this?
239 * FIXME use language packs API coming with WP 3.7 instead
240 *
241 * @since 0.6
242 *
243 * @param string $locale locale to download
244 * @param bool $upgrade optional true if this is an upgrade, false if this is the first download, defaults to false
245 * @return bool true on success, false otherwise
246 */
247 static public function download_mo($locale, $upgrade = false) {
248 global $wp_version;
249 $mofile = WP_LANG_DIR."/$locale.mo";
250
251 // does file exists ?
252 if ((file_exists($mofile) && !$upgrade) || $locale == 'en_US')
253 return true;
254
255 // does language directory exists ?
256 if (!is_dir(WP_LANG_DIR)) {
257 if (!@mkdir(WP_LANG_DIR))
258 return false;
259 }
260
261 // will first look in tags/ (most languages) then in branches/ (only Greek ?)
262 $base = 'http://svn.automattic.com/wordpress-i18n/'.$locale;
263 $bases = array($base.'/tags/', $base.'/branches/');
264
265 foreach ($bases as $base) {
266 // get all the versions available in the subdirectory
267 $resp = wp_remote_get($base);
268 if (is_wp_error($resp) || 200 != $resp['response']['code'])
269 continue;
270
271 preg_match_all('#>([0-9\.]+)\/#', $resp['body'], $matches);
272 if (empty($matches[1]))
273 continue;
274
275 rsort($matches[1]); // sort from newest to oldest
276 $versions = $matches[1];
277
278 $newest = $upgrade ? $upgrade : $wp_version;
279 foreach ($versions as $key=>$version) {
280 // will not try to download a too recent mofile
281 if (version_compare($version, $newest, '>'))
282 unset($versions[$key]);
283 // will not download an older version if we are upgrading
284 if ($upgrade && version_compare($version, $wp_version, '<='))
285 unset($versions[$key]);
286 }
287
288 $versions = array_splice($versions, 0, 5); // reduce the number of versions to test to 5
289 $args = array('timeout' => 30, 'stream' => true);
290
291 // try to download the file
292 foreach ($versions as $version) {
293 $resp = wp_remote_get($base."$version/messages/$locale.mo", $args + array('filename' => $mofile));
294 if (is_wp_error($resp) || 200 != $resp['response']['code']) {
295 unlink($mofile); // otherwise we download a gzipped 404 page
296 continue;
297 }
298 // try to download ms and continents-cities files if exist (will not return false if failed)
299 // with new files introduced in WP 3.4
300 foreach (array('ms', 'continents-cities', 'admin', 'admin-network') as $file) {
301 $resp = wp_remote_get($base."$version/messages/$file-$locale.mo", $args + array('filename' => WP_LANG_DIR."/$file-$locale.mo"));
302 if (is_wp_error($resp) || 200 != $resp['response']['code'])
303 unlink(WP_LANG_DIR."/$file-$locale.mo");
304 }
305 // try to download theme files if exist (will not return false if failed)
306 // FIXME not updated when the theme is updated outside a core update
307 foreach (array('twentyten', 'twentyeleven', 'twentytwelve', 'twentythirteen', 'twentyfourteen') as $theme) {
308 if (!is_dir($theme_dir = get_theme_root()."/$theme/languages"))
309 continue; // the theme is not present
310
311 $resp = wp_remote_get($base."$version/messages/$theme/$locale.mo", $args + array('filename' => "$theme_dir/$locale.mo"));
312 if (is_wp_error($resp) || 200 != $resp['response']['code'])
313 unlink("$theme_dir/$locale.mo");
314 }
315 return true;
316 }
317 }
318 // we did not succeeded to download a file :(
319 return false;
320 }
321
322 }
323