PluginProbe
Polylang / 1.4.4
Polylang v1.4.4
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 / polylang.php

polylang.php in Polylang 1.4.4, at polylang.php

317 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Polylang
4 Plugin URI: http://polylang.wordpress.com/
5 Version: 1.4.4
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-2014 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.4.4');
33 define('PLL_MIN_WP_VERSION', '3.5');
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
88 if (!defined('PLL_AJAX_ON_FRONT'))
89 define('PLL_AJAX_ON_FRONT', defined('DOING_AJAX') && DOING_AJAX && empty($_REQUEST['pll_ajax_backend']));
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 global $polylang;
175
176 if ($options = get_option('polylang')) {
177 // plugin upgrade
178 if (version_compare($options['version'], POLYLANG_VERSION, '<')) {
179 $upgrade = new PLL_Upgrade($options);
180 $upgrade->upgrade_at_activation();
181 }
182 }
183 // defines default values for options in case this is the first installation
184 else {
185 $options = array(
186 'browser' => 1, // default language for the front page is set by browser preference
187 'rewrite' => 1, // remove /language/ in permalinks (was the opposite before 0.7.2)
188 'hide_default' => 0, // do not remove URL language information for default language
189 'force_lang' => 0, // do not add URL language information when useless
190 'redirect_lang' => 0, // do not redirect the language page to the homepage
191 'media_support' => 1, // support languages and translation for media by default
192 'sync' => array(), // synchronisation is disabled by default (was the opposite before 1.2)
193 'post_types' => array_values(get_post_types(array('_builtin' => false, 'show_ui => true'))),
194 'taxonomies' => array_values(get_taxonomies(array('_builtin' => false, 'show_ui => true'))),
195 'domains' => array(),
196 'version' => POLYLANG_VERSION,
197 );
198
199 update_option('polylang', $options);
200 }
201
202 // always provide a global $polylang object and add our rewrite rules if needed
203 $polylang = new StdClass();
204 $polylang->options = &$options;
205 $polylang->model = new PLL_Admin_Model($options);
206 $polylang->links_model = $this->get_links_model($polylang->model, $options);
207 flush_rewrite_rules();
208 }
209
210 /*
211 * plugin deactivation for multisite
212 *
213 * @since 0.1
214 */
215 public function deactivate() {
216 $this->do_for_all_blogs('deactivate');
217 }
218
219 /*
220 * plugin deactivation
221 *
222 * @since 0.5
223 */
224 protected function _deactivate() {
225 flush_rewrite_rules();
226 }
227
228 /*
229 * blog creation on multisite (to set default options)
230 *
231 * @since 0.9.4
232 *
233 * @param int $blog_id
234 */
235 public function wpmu_new_blog($blog_id) {
236 switch_to_blog($blog_id);
237 $this->_activate();
238 restore_current_blog();
239 }
240
241 /*
242 * autoload classes
243 *
244 * @since 1.2
245 *
246 * @param string $class
247 */
248 public function autoload($class) {
249 $class = str_replace('_', '-', strtolower(substr($class, 4)));
250 foreach (array(PLL_INC, PLL_FRONT_INC, PLL_ADMIN_INC) as $path)
251 if (file_exists($file = "$path/$class.php")) {
252 require_once($file);
253 break;
254 }
255 }
256
257 /*
258 * Polylang initialization
259 * setups models and separate admin and frontend
260 *
261 * @since 1.2
262 */
263 public function init() {
264 global $polylang;
265
266 $options = get_option('polylang');
267
268 // plugin upgrade
269 if ($options && version_compare($options['version'], POLYLANG_VERSION, '<')) {
270 $upgrade = new PLL_Upgrade($options);
271 if (!$upgrade->upgrade()) // if the version is too old
272 return;
273 }
274
275 $model = PLL_SETTINGS ? new PLL_Admin_Model($options) : new PLL_Model($options);
276 $links_model = $this->get_links_model($model);
277
278 if (PLL_ADMIN) {
279 $polylang = new PLL_Admin($links_model);
280 $polylang->init();
281 }
282 // do nothing on frontend if no language is defined
283 elseif ($model->get_languages_list()) {
284 $polylang = new PLL_Frontend($links_model);
285 $polylang->init();
286 }
287 else
288 do_action('pll_no_language_defined'); // to load overriden textdomains
289
290 // load wpml-config.xml
291 if (!defined('PLL_WPML_COMPAT') || PLL_WPML_COMPAT)
292 new PLL_WPML_Config;
293 }
294
295 /*
296 * setup the links model based on options
297 *
298 * @since 1.2
299 *
300 * @param object $model instance of PLL_Model
301 * @return object implementing "links_model interface"
302 */
303 protected function get_links_model(&$model) {
304 if (get_option('permalink_structure')) {
305 if (2 == $model->options['force_lang'])
306 return new PLL_Links_Subdomain($model);
307 elseif (3 == $model->options['force_lang'])
308 return new PLL_Links_Domain($model);
309 else
310 return new PLL_Links_Directory($model);
311 }
312 return new PLL_Links_Default($model);
313 }
314 }
315
316 new Polylang();
317