| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Polylang |
| 4 |
Plugin URI: http://polylang.wordpress.com/ |
| 5 |
Version: 1.3 |
| 6 |
Author: Frédéric Demarle |
| 7 |
Description: Adds multilingual capability to WordPress |
| 8 |
Text Domain: polylang |
| 9 |
Domain Path: /languages |
| 10 |
*/ |
| 11 |
|
| 12 |
/* |
| 13 |
* Copyright 2011-2013 Frédéric Demarle |
| 14 |
* |
| 15 |
* This program is free software; you can redistribute it and/or modify |
| 16 |
* it under the terms of the GNU General Public License as published by |
| 17 |
* the Free Software Foundation; either version 2 of the License, or |
| 18 |
* (at your option) any later version. |
| 19 |
* |
| 20 |
* This program is distributed in the hope that it will be useful, |
| 21 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 |
* GNU General Public License for more details. |
| 24 |
* |
| 25 |
* You should have received a copy of the GNU General Public License |
| 26 |
* along with this program; if not, write to the Free Software |
| 27 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 28 |
* MA 02110-1301, USA. |
| 29 |
* |
| 30 |
*/ |
| 31 |
|
| 32 |
define('POLYLANG_VERSION', '1.3'); |
| 33 |
define('PLL_MIN_WP_VERSION', '3.1'); |
| 34 |
|
| 35 |
define('POLYLANG_BASENAME', plugin_basename(__FILE__)); // plugin name as known by WP |
| 36 |
|
| 37 |
define('POLYLANG_DIR', dirname(__FILE__)); // our directory |
| 38 |
define('PLL_INC', POLYLANG_DIR . '/include'); |
| 39 |
define('PLL_FRONT_INC', POLYLANG_DIR . '/frontend'); |
| 40 |
define('PLL_ADMIN_INC', POLYLANG_DIR . '/admin'); |
| 41 |
|
| 42 |
// default directory to store user data such as custom flags |
| 43 |
if (!defined('PLL_LOCAL_DIR')) |
| 44 |
define('PLL_LOCAL_DIR', WP_CONTENT_DIR . '/polylang'); |
| 45 |
|
| 46 |
// includes local config file if exists |
| 47 |
if (file_exists(PLL_LOCAL_DIR . '/pll-config.php')) |
| 48 |
include_once(PLL_LOCAL_DIR . '/pll-config.php'); |
| 49 |
|
| 50 |
// our url. Don't use WP_PLUGIN_URL http://wordpress.org/support/topic/ssl-doesnt-work-properly |
| 51 |
define('POLYLANG_URL', plugins_url('/' . basename(POLYLANG_DIR))); |
| 52 |
|
| 53 |
// default url to access user data such as custom flags |
| 54 |
if (!defined('PLL_LOCAL_URL')) |
| 55 |
define('PLL_LOCAL_URL', content_url('/polylang')); |
| 56 |
|
| 57 |
// cookie name. no cookie will be used if set to false |
| 58 |
if (!defined('PLL_COOKIE')) |
| 59 |
define('PLL_COOKIE', 'pll_language'); |
| 60 |
|
| 61 |
// backward compatibility WP < 3.6 |
| 62 |
// the search form js is no more needed in WP 3.6+ except if the search form is hardcoded elsewhere than in searchform.php |
| 63 |
if (!defined('PLL_SEARCH_FORM_JS') && !version_compare($GLOBALS['wp_version'], '3.6', '<')) |
| 64 |
define('PLL_SEARCH_FORM_JS', false); |
| 65 |
|
| 66 |
/* |
| 67 |
* controls the plugin, as well as activation, and deactivation |
| 68 |
* |
| 69 |
* @since 0.1 |
| 70 |
*/ |
| 71 |
class Polylang { |
| 72 |
|
| 73 |
/* |
| 74 |
* constructor |
| 75 |
* |
| 76 |
* @since 0.1 |
| 77 |
*/ |
| 78 |
public function __construct() { |
| 79 |
// manages plugin activation and deactivation |
| 80 |
register_activation_hook( __FILE__, array(&$this, 'activate')); |
| 81 |
register_deactivation_hook( __FILE__, array(&$this, 'deactivate')); |
| 82 |
|
| 83 |
// stopping here if we are going to deactivate the plugin (avoids breaking rewrite rules) |
| 84 |
if (isset($_GET['action'], $_GET['plugin']) && 'deactivate' == $_GET['action'] && plugin_basename(__FILE__) == $_GET['plugin']) |
| 85 |
return; |
| 86 |
|
| 87 |
// avoid loading polylang admin for frontend ajax requests if 'pll_load_front' is set (thanks to g100g) |
| 88 |
if (!defined('PLL_AJAX_ON_FRONT')) |
| 89 |
define('PLL_AJAX_ON_FRONT', isset($_REQUEST['pll_load_front'])); |
| 90 |
|
| 91 |
if (!defined('PLL_ADMIN')) |
| 92 |
define('PLL_ADMIN', defined('DOING_CRON') || (is_admin() && !PLL_AJAX_ON_FRONT)); |
| 93 |
|
| 94 |
if (!defined('PLL_SETTINGS')) |
| 95 |
define('PLL_SETTINGS', is_admin() && isset($_GET['page']) && $_GET['page'] == 'mlang'); |
| 96 |
|
| 97 |
// blog creation on multisite |
| 98 |
add_action('wpmu_new_blog', array(&$this, 'wpmu_new_blog')); |
| 99 |
|
| 100 |
// FIXME maybe not available on every installations but widely used by WP plugins |
| 101 |
spl_autoload_register(array(&$this, 'autoload')); // autoload classes |
| 102 |
|
| 103 |
// override load text domain waiting for the language to be defined |
| 104 |
// here for plugins which load text domain as soon as loaded :( |
| 105 |
if (!defined('PLL_OLT') || PLL_OLT) |
| 106 |
new PLL_OLT_Manager(); |
| 107 |
|
| 108 |
// plugin initialization |
| 109 |
// take no action before all plugins are loaded |
| 110 |
add_action('plugins_loaded', array(&$this, 'init'), 1); |
| 111 |
|
| 112 |
// loads the API |
| 113 |
require_once(PLL_INC.'/api.php'); |
| 114 |
|
| 115 |
// WPML API |
| 116 |
if (!defined('PLL_WPML_COMPAT') || PLL_WPML_COMPAT) |
| 117 |
require_once (PLL_INC.'/wpml-compat.php'); |
| 118 |
|
| 119 |
// extra code for compatibility with some plugins |
| 120 |
if (!defined('PLL_PLUGINS_COMPAT') || PLL_PLUGINS_COMPAT) |
| 121 |
new PLL_Plugins_Compat(); |
| 122 |
} |
| 123 |
|
| 124 |
/* |
| 125 |
* activation or deactivation for all blogs |
| 126 |
* |
| 127 |
* @since 1.2 |
| 128 |
* |
| 129 |
* @param string $what either 'activate' or 'deactivate' |
| 130 |
*/ |
| 131 |
protected function do_for_all_blogs($what) { |
| 132 |
// network |
| 133 |
if (is_multisite() && isset($_GET['networkwide']) && ($_GET['networkwide'] == 1)) { |
| 134 |
global $wpdb; |
| 135 |
|
| 136 |
foreach ($wpdb->get_col("SELECT blog_id FROM $wpdb->blogs") as $blog_id) { |
| 137 |
switch_to_blog($blog_id); |
| 138 |
$what == 'activate' ? $this->_activate() : $this->_deactivate(); |
| 139 |
} |
| 140 |
restore_current_blog(); |
| 141 |
} |
| 142 |
|
| 143 |
// single blog |
| 144 |
else |
| 145 |
$what == 'activate' ? $this->_activate() : $this->_deactivate(); |
| 146 |
} |
| 147 |
|
| 148 |
/* |
| 149 |
* plugin activation for multisite |
| 150 |
* |
| 151 |
* @since 0.1 |
| 152 |
*/ |
| 153 |
public function activate() { |
| 154 |
global $wp_version; |
| 155 |
load_plugin_textdomain('polylang', false, basename(POLYLANG_DIR).'/languages'); // plugin i18n |
| 156 |
|
| 157 |
if (version_compare($wp_version, PLL_MIN_WP_VERSION , '<')) |
| 158 |
die (sprintf('<p style = "font-family: sans-serif; font-size: 12px; color: #333; margin: -5px">%s</p>', |
| 159 |
sprintf(__('You are using WordPress %s. Polylang requires at least WordPress %s.', 'polylang'), |
| 160 |
esc_html($wp_version), |
| 161 |
PLL_MIN_WP_VERSION |
| 162 |
) |
| 163 |
)); |
| 164 |
|
| 165 |
$this->do_for_all_blogs('activate'); |
| 166 |
} |
| 167 |
|
| 168 |
/* |
| 169 |
* plugin activation |
| 170 |
* |
| 171 |
* @since 0.5 |
| 172 |
*/ |
| 173 |
protected function _activate() { |
| 174 |
if ($options = get_option('polylang')) { |
| 175 |
// plugin upgrade |
| 176 |
if (version_compare($options['version'], POLYLANG_VERSION, '<')) { |
| 177 |
$upgrade = new PLL_Upgrade($options); |
| 178 |
$upgrade->upgrade_at_activation(); |
| 179 |
} |
| 180 |
} |
| 181 |
// defines default values for options in case this is the first installation |
| 182 |
else { |
| 183 |
$options = array( |
| 184 |
'browser' => 1, // default language for the front page is set by browser preference |
| 185 |
'rewrite' => 1, // remove /language/ in permalinks (was the opposite before 0.7.2) |
| 186 |
'hide_default' => 0, // do not remove URL language information for default language |
| 187 |
'force_lang' => 0, // do not add URL language information when useless |
| 188 |
'redirect_lang' => 0, // do not redirect the language page to the homepage |
| 189 |
'media_support' => 1, // support languages and translation for media by default |
| 190 |
'sync' => array(), // synchronisation is disabled by default (was the opposite before 1.2) |
| 191 |
'post_types' => array_values(get_post_types(array('_builtin' => false, 'show_ui => true'))), |
| 192 |
'taxonomies' => array_values(get_taxonomies(array('_builtin' => false, 'show_ui => true'))), |
| 193 |
'domains' => array(), |
| 194 |
'version' => POLYLANG_VERSION, |
| 195 |
); |
| 196 |
|
| 197 |
update_option('polylang', $options); |
| 198 |
} |
| 199 |
|
| 200 |
// add our rewrite rules |
| 201 |
$this->get_links_model(new PLL_Admin_Model($options), $options); |
| 202 |
flush_rewrite_rules(); |
| 203 |
} |
| 204 |
|
| 205 |
/* |
| 206 |
* plugin deactivation for multisite |
| 207 |
* |
| 208 |
* @since 0.1 |
| 209 |
*/ |
| 210 |
public function deactivate() { |
| 211 |
$this->do_for_all_blogs('deactivate'); |
| 212 |
} |
| 213 |
|
| 214 |
/* |
| 215 |
* plugin deactivation |
| 216 |
* |
| 217 |
* @since 0.5 |
| 218 |
*/ |
| 219 |
protected function _deactivate() { |
| 220 |
flush_rewrite_rules(); |
| 221 |
} |
| 222 |
|
| 223 |
/* |
| 224 |
* blog creation on multisite (to set default options) |
| 225 |
* |
| 226 |
* @since 0.9.4 |
| 227 |
* |
| 228 |
* @param int $blog_id |
| 229 |
*/ |
| 230 |
public function wpmu_new_blog($blog_id) { |
| 231 |
switch_to_blog($blog_id); |
| 232 |
$this->_activate(); |
| 233 |
restore_current_blog(); |
| 234 |
} |
| 235 |
|
| 236 |
/* |
| 237 |
* autoload classes |
| 238 |
* |
| 239 |
* @since 1.2 |
| 240 |
* |
| 241 |
* @param string $class |
| 242 |
*/ |
| 243 |
public function autoload($class) { |
| 244 |
$class = str_replace('_', '-', strtolower(substr($class, 4))); |
| 245 |
foreach (array(PLL_INC, PLL_FRONT_INC, PLL_ADMIN_INC) as $path) |
| 246 |
if (file_exists($file = "$path/$class.php")) |
| 247 |
require_once($file); |
| 248 |
} |
| 249 |
|
| 250 |
/* |
| 251 |
* Polylang initialization |
| 252 |
* setups models and separate admin and frontend |
| 253 |
* |
| 254 |
* @since 1.2 |
| 255 |
*/ |
| 256 |
public function init() { |
| 257 |
global $polylang; |
| 258 |
|
| 259 |
$options = get_option('polylang'); |
| 260 |
|
| 261 |
// plugin upgrade |
| 262 |
if ($options && version_compare($options['version'], POLYLANG_VERSION, '<')) { |
| 263 |
$upgrade = new PLL_Upgrade($options); |
| 264 |
if (!$upgrade->upgrade()) // if the version is too old |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
$model = PLL_SETTINGS ? new PLL_Admin_Model($options) : new PLL_Model($options); |
| 269 |
$links_model = $this->get_links_model($model); |
| 270 |
|
| 271 |
if (PLL_ADMIN) { |
| 272 |
$polylang = new PLL_Admin($links_model); |
| 273 |
$polylang->init(); |
| 274 |
} |
| 275 |
// do nothing on frontend if no language is defined |
| 276 |
elseif ($model->get_languages_list()) { |
| 277 |
$polylang = new PLL_Frontend($links_model); |
| 278 |
$polylang->init(); |
| 279 |
} |
| 280 |
else |
| 281 |
do_action('pll_no_language_defined'); // to load overriden textdomains |
| 282 |
|
| 283 |
// load wpml-config.xml |
| 284 |
if (!defined('PLL_WPML_COMPAT') || PLL_WPML_COMPAT) |
| 285 |
new PLL_WPML_Config; |
| 286 |
} |
| 287 |
|
| 288 |
/* |
| 289 |
* setup the links model based on options |
| 290 |
* |
| 291 |
* @since 1.2 |
| 292 |
* |
| 293 |
* @param object $model instance of PLL_Model |
| 294 |
* @return object implementing "links_model interface" |
| 295 |
*/ |
| 296 |
protected function get_links_model(&$model) { |
| 297 |
if (get_option('permalink_structure')) { |
| 298 |
if (2 == $model->options['force_lang']) |
| 299 |
return new PLL_Links_Subdomain($model); |
| 300 |
elseif (3 == $model->options['force_lang']) |
| 301 |
return new PLL_Links_Domain($model); |
| 302 |
else |
| 303 |
return new PLL_Links_Directory($model); |
| 304 |
} |
| 305 |
return new PLL_Links_Default($model); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
new Polylang(); |
| 310 |
|