PluginProbe
Polylang / 1.4
Polylang v1.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, at polylang.php

315 lines 9.6 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
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');
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->links_model = $this->get_links_model(new PLL_Admin_Model($options), $options);
205 flush_rewrite_rules();
206 }
207
208 /*
209 * plugin deactivation for multisite
210 *
211 * @since 0.1
212 */
213 public function deactivate() {
214 $this->do_for_all_blogs('deactivate');
215 }
216
217 /*
218 * plugin deactivation
219 *
220 * @since 0.5
221 */
222 protected function _deactivate() {
223 flush_rewrite_rules();
224 }
225
226 /*
227 * blog creation on multisite (to set default options)
228 *
229 * @since 0.9.4
230 *
231 * @param int $blog_id
232 */
233 public function wpmu_new_blog($blog_id) {
234 switch_to_blog($blog_id);
235 $this->_activate();
236 restore_current_blog();
237 }
238
239 /*
240 * autoload classes
241 *
242 * @since 1.2
243 *
244 * @param string $class
245 */
246 public function autoload($class) {
247 $class = str_replace('_', '-', strtolower(substr($class, 4)));
248 foreach (array(PLL_INC, PLL_FRONT_INC, PLL_ADMIN_INC) as $path)
249 if (file_exists($file = "$path/$class.php")) {
250 require_once($file);
251 break;
252 }
253 }
254
255 /*
256 * Polylang initialization
257 * setups models and separate admin and frontend
258 *
259 * @since 1.2
260 */
261 public function init() {
262 global $polylang;
263
264 $options = get_option('polylang');
265
266 // plugin upgrade
267 if ($options && version_compare($options['version'], POLYLANG_VERSION, '<')) {
268 $upgrade = new PLL_Upgrade($options);
269 if (!$upgrade->upgrade()) // if the version is too old
270 return;
271 }
272
273 $model = PLL_SETTINGS ? new PLL_Admin_Model($options) : new PLL_Model($options);
274 $links_model = $this->get_links_model($model);
275
276 if (PLL_ADMIN) {
277 $polylang = new PLL_Admin($links_model);
278 $polylang->init();
279 }
280 // do nothing on frontend if no language is defined
281 elseif ($model->get_languages_list()) {
282 $polylang = new PLL_Frontend($links_model);
283 $polylang->init();
284 }
285 else
286 do_action('pll_no_language_defined'); // to load overriden textdomains
287
288 // load wpml-config.xml
289 if (!defined('PLL_WPML_COMPAT') || PLL_WPML_COMPAT)
290 new PLL_WPML_Config;
291 }
292
293 /*
294 * setup the links model based on options
295 *
296 * @since 1.2
297 *
298 * @param object $model instance of PLL_Model
299 * @return object implementing "links_model interface"
300 */
301 protected function get_links_model(&$model) {
302 if (get_option('permalink_structure')) {
303 if (2 == $model->options['force_lang'])
304 return new PLL_Links_Subdomain($model);
305 elseif (3 == $model->options['force_lang'])
306 return new PLL_Links_Domain($model);
307 else
308 return new PLL_Links_Directory($model);
309 }
310 return new PLL_Links_Default($model);
311 }
312 }
313
314 new Polylang();
315