PluginProbe
Polylang / 0.9.2
Polylang v0.9.2
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.2, at include/admin-base.php

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