| 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 |
// additionnal filters and actions |
| 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 andd 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 text direction if the user set its own language |
| 28 |
function admin_init_base() { |
| 29 |
global $wpdb, $wp_locale; |
| 30 |
$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 |
| 31 |
WHERE tt.taxonomy = 'language' AND tt.description = %s LIMIT 1", get_locale())); // no function exists to get term by description |
| 32 |
if ($lang_id) |
| 33 |
$wp_locale->text_direction = get_metadata('term', $lang_id, '_rtl', true) ? 'rtl' : 'ltr'; |
| 34 |
} |
| 35 |
|
| 36 |
// adds the link to the languages panel in the wordpress admin menu |
| 37 |
function add_menus() { |
| 38 |
add_submenu_page('options-general.php', __('Languages', 'polylang'), __('Languages', 'polylang'), 'manage_options', 'mlang', array(&$this, 'languages_page')); |
| 39 |
|
| 40 |
// adds the about box the languages admin panel |
| 41 |
// test of $_GET['tab'] avoids displaying the automatically generated screen options on other tabs |
| 42 |
if (PLL_DISPLAY_ABOUT && isset($_GET['page']) && $_GET['page'] == 'mlang' && (!isset($_GET['tab']) || $_GET['tab'] == 'lang')) |
| 43 |
add_meta_box('pll_about_box', __('About Polylang', 'polylang'), array(&$this,'about'), 'settings_page_mlang', 'normal', 'high'); |
| 44 |
} |
| 45 |
|
| 46 |
// setup js scripts & css styles (only on the relevant pages) |
| 47 |
function admin_enqueue_scripts() { |
| 48 |
$screen = get_current_screen(); |
| 49 |
|
| 50 |
// FIXME keep the script in header to be sure it is loaded before post.js otherwise a non filtered tag cloud appears in tag cloud metabox |
| 51 |
if ($screen->base == 'settings_page_mlang' || $screen->base == 'post' || $screen->base == 'edit-tags') |
| 52 |
wp_enqueue_script('polylang_admin', POLYLANG_URL .'/js/admin.js', array('jquery', 'wp-ajax-response'), POLYLANG_VERSION); |
| 53 |
|
| 54 |
if ($screen->base == 'settings_page_mlang' || $screen->base == 'post' || $screen->base == 'edit-tags' || $screen->base == 'edit') |
| 55 |
wp_enqueue_style('polylang_admin', POLYLANG_URL .'/css/admin.css', array(), POLYLANG_VERSION); |
| 56 |
|
| 57 |
if ($screen->base == 'settings_page_mlang') { |
| 58 |
wp_enqueue_script('postbox'); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// downloads mofiles |
| 63 |
function download_mo($locale, $upgrade = false) { |
| 64 |
global $wp_version; |
| 65 |
$mofile = WP_LANG_DIR."/$locale.mo"; |
| 66 |
|
| 67 |
// does file exists ? |
| 68 |
if ((file_exists($mofile) && !$upgrade) || $locale == 'en_US') |
| 69 |
return true; |
| 70 |
|
| 71 |
// does language directory exists ? |
| 72 |
if (!is_dir(WP_LANG_DIR)) { |
| 73 |
if (!@mkdir(WP_LANG_DIR)) |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
// will first look in tags/ (most languages) then in branches/ (only Greek ?) |
| 78 |
$base = 'http://svn.automattic.com/wordpress-i18n/'.$locale; |
| 79 |
$bases = array($base.'/tags/', $base.'/branches/'); |
| 80 |
|
| 81 |
foreach ($bases as $base) { |
| 82 |
// get all the versions available in the subdirectory |
| 83 |
$resp = wp_remote_get($base); |
| 84 |
if (is_wp_error($resp) || 200 != $resp['response']['code']) |
| 85 |
continue; |
| 86 |
|
| 87 |
preg_match_all('#>([0-9\.]+)\/#', $resp['body'], $matches); |
| 88 |
if (empty($matches[1])) |
| 89 |
continue; |
| 90 |
|
| 91 |
rsort($matches[1]); // sort from newest to oldest |
| 92 |
$versions = $matches[1]; |
| 93 |
|
| 94 |
$newest = $upgrade ? $upgrade : $wp_version; |
| 95 |
foreach ($versions as $key=>$version) { |
| 96 |
// will not try to download a too recent mofile |
| 97 |
if (version_compare($version, $newest, '>')) |
| 98 |
unset($versions[$key]); |
| 99 |
// will not download an older version if we are upgrading |
| 100 |
if ($upgrade && version_compare($version, $wp_version, '<=')) |
| 101 |
unset($versions[$key]); |
| 102 |
} |
| 103 |
|
| 104 |
$versions = array_splice($versions, 0, 5); // reduce the number of versions to test to 5 |
| 105 |
$args = array('timeout' => 30, 'stream' => true); |
| 106 |
|
| 107 |
// try to download the file |
| 108 |
foreach ($versions as $version) { |
| 109 |
$resp = wp_remote_get($base."$version/messages/$locale.mo", $args + array('filename' => $mofile)); |
| 110 |
if (is_wp_error($resp) || 200 != $resp['response']['code']) |
| 111 |
continue; |
| 112 |
|
| 113 |
// try to download ms and continents-cities files if exist (will not return false if failed) |
| 114 |
// with new files introduced in WP 3.4 |
| 115 |
foreach (array("ms", "continent-cities", "admin", "admin-network") as $file) |
| 116 |
wp_remote_get($base."$version/messages/$file-$locale.mo", $args + array('filename' => WP_LANG_DIR."/$file-$locale.mo")); |
| 117 |
|
| 118 |
// try to download theme files if exist (will not return false if failed) |
| 119 |
// FIXME not updated when the theme is updated outside a core update |
| 120 |
foreach (array("twentyten", "twentyeleven", "twentytwelve") as $theme) |
| 121 |
wp_remote_get($base."$version/messages/$theme/$locale.mo", $args + array('filename' => get_theme_root()."/$theme/languages/$locale.mo")); |
| 122 |
|
| 123 |
return true; |
| 124 |
} |
| 125 |
} |
| 126 |
// we did not succeeded to download a file :( |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
// returns options available for the language switcher (menu or widget) |
| 131 |
// FIXME do not include the dropdown in menu yet since I need to work on js |
| 132 |
function get_switcher_options($type = 'widget', $key ='string') { |
| 133 |
$options = array ( |
| 134 |
'show_names' => array('string' => __('Displays language names', 'polylang'), 'default' => 1), |
| 135 |
'show_flags' => array('string' => __('Displays flags', 'polylang'), 'default' => 0), |
| 136 |
'force_home' => array('string' => __('Forces link to front page', 'polylang'), 'default' => 0), |
| 137 |
'hide_current' => array('string' => __('Hides the current language', 'polylang'), 'default' => 0), |
| 138 |
); |
| 139 |
$menu_options = array('switcher' => array('string' => __('Displays a language switcher at the end of the menu', 'polylang'), 'default' => 0)); |
| 140 |
$widget_options = array('dropdown' => array('string' => __('Displays as dropdown', 'polylang'), 'default' => 0)); |
| 141 |
$options = ($type == 'menu') ? array_merge($menu_options, $options) : array_merge($options, $widget_options); |
| 142 |
return array_map(create_function('$v', "return \$v['$key'];"), $options); |
| 143 |
} |
| 144 |
|
| 145 |
// register strings for translation making sure it is not duplicate |
| 146 |
function register_string($name, $string) { |
| 147 |
$to_register = array('name'=> $name, 'string' => $string); |
| 148 |
if (!in_array($to_register, $this->strings)) |
| 149 |
$this->strings[] = $to_register; |
| 150 |
} |
| 151 |
} // class Polylang_Admin_Base |
| 152 |
|
| 153 |
?> |
| 154 |
|