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

323 lines 10.0 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.6.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-2015 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 // don't access directly
33 if (!function_exists('add_action'))
34 exit();
35
36 define('POLYLANG_VERSION', '1.6.3');
37 define('PLL_MIN_WP_VERSION', '3.5');
38
39 define('POLYLANG_BASENAME', plugin_basename(__FILE__)); // plugin name as known by WP
40
41 define('POLYLANG_DIR', dirname(__FILE__)); // our directory
42 define('PLL_INC', POLYLANG_DIR . '/include');
43 define('PLL_FRONT_INC', POLYLANG_DIR . '/frontend');
44 define('PLL_ADMIN_INC', POLYLANG_DIR . '/admin');
45
46 // default directory to store user data such as custom flags
47 if (!defined('PLL_LOCAL_DIR'))
48 define('PLL_LOCAL_DIR', WP_CONTENT_DIR . '/polylang');
49
50 // includes local config file if exists
51 if (file_exists(PLL_LOCAL_DIR . '/pll-config.php'))
52 include_once(PLL_LOCAL_DIR . '/pll-config.php');
53
54 // our url. Don't use WP_PLUGIN_URL http://wordpress.org/support/topic/ssl-doesnt-work-properly
55 define('POLYLANG_URL', plugins_url('', __FILE__));
56
57 // default url to access user data such as custom flags
58 if (!defined('PLL_LOCAL_URL'))
59 define('PLL_LOCAL_URL', content_url('/polylang'));
60
61 /*
62 * controls the plugin, as well as activation, and deactivation
63 *
64 * @since 0.1
65 */
66 class Polylang {
67
68 /*
69 * constructor
70 *
71 * @since 0.1
72 */
73 public function __construct() {
74 // FIXME maybe not available on every installations but widely used by WP plugins
75 spl_autoload_register(array(&$this, 'autoload')); // autoload classes
76
77 // manages plugin activation and deactivation
78 register_activation_hook( __FILE__, array(&$this, 'activate'));
79 register_deactivation_hook( __FILE__, array(&$this, 'deactivate'));
80
81 // stopping here if we are going to deactivate the plugin (avoids breaking rewrite rules)
82 if (isset($_GET['action'], $_GET['plugin']) && 'deactivate' == $_GET['action'] && plugin_basename(__FILE__) == $_GET['plugin'])
83 return;
84
85 // blog creation on multisite
86 add_action('wpmu_new_blog', array(&$this, 'wpmu_new_blog'), 5); // before WP attempts to send mails which can break on some PHP versions
87
88 // plugin initialization
89 // take no action before all plugins are loaded
90 add_action('plugins_loaded', array(&$this, 'init'), 1);
91
92 // override load text domain waiting for the language to be defined
93 // here for plugins which load text domain as soon as loaded :(
94 if (!defined('PLL_OLT') || PLL_OLT)
95 new PLL_OLT_Manager();
96
97 // loads the API
98 require_once(PLL_INC.'/api.php');
99
100 // WPML API
101 if (!defined('PLL_WPML_COMPAT') || PLL_WPML_COMPAT)
102 require_once (PLL_INC.'/wpml-compat.php');
103
104 // extra code for compatibility with some plugins
105 if (!defined('PLL_PLUGINS_COMPAT') || PLL_PLUGINS_COMPAT)
106 new PLL_Plugins_Compat();
107 }
108
109 /*
110 * activation or deactivation for all blogs
111 *
112 * @since 1.2
113 *
114 * @param string $what either 'activate' or 'deactivate'
115 */
116 protected function do_for_all_blogs($what) {
117 // network
118 if (is_multisite() && isset($_GET['networkwide']) && ($_GET['networkwide'] == 1)) {
119 global $wpdb;
120
121 foreach ($wpdb->get_col("SELECT blog_id FROM $wpdb->blogs") as $blog_id) {
122 switch_to_blog($blog_id);
123 $what == 'activate' ? $this->_activate() : $this->_deactivate();
124 }
125 restore_current_blog();
126 }
127
128 // single blog
129 else
130 $what == 'activate' ? $this->_activate() : $this->_deactivate();
131 }
132
133 /*
134 * plugin activation for multisite
135 *
136 * @since 0.1
137 */
138 public function activate() {
139 global $wp_version;
140 $this->define_constants();
141 load_plugin_textdomain('polylang', false, basename(POLYLANG_DIR).'/languages'); // plugin i18n
142
143 if (version_compare($wp_version, PLL_MIN_WP_VERSION , '<'))
144 die (sprintf('<p style = "font-family: sans-serif; font-size: 12px; color: #333; margin: -5px">%s</p>',
145 sprintf(__('You are using WordPress %s. Polylang requires at least WordPress %s.', 'polylang'),
146 esc_html($wp_version),
147 PLL_MIN_WP_VERSION
148 )
149 ));
150
151 $this->do_for_all_blogs('activate');
152 }
153
154 /*
155 * plugin activation
156 *
157 * @since 0.5
158 */
159 protected function _activate() {
160 global $polylang;
161
162 if ($options = get_option('polylang')) {
163 // plugin upgrade
164 if (version_compare($options['version'], POLYLANG_VERSION, '<')) {
165 $upgrade = new PLL_Upgrade($options);
166 $upgrade->upgrade_at_activation();
167 }
168 }
169 // defines default values for options in case this is the first installation
170 else {
171 $options = array(
172 'browser' => 1, // default language for the front page is set by browser preference
173 'rewrite' => 1, // remove /language/ in permalinks (was the opposite before 0.7.2)
174 'hide_default' => 0, // do not remove URL language information for default language
175 'force_lang' => 0, // do not add URL language information when useless
176 'redirect_lang' => 0, // do not redirect the language page to the homepage
177 'media_support' => 1, // support languages and translation for media by default
178 'sync' => array(), // synchronisation is disabled by default (was the opposite before 1.2)
179 'post_types' => array_values(get_post_types(array('_builtin' => false, 'show_ui => true'))),
180 'taxonomies' => array_values(get_taxonomies(array('_builtin' => false, 'show_ui => true'))),
181 'domains' => array(),
182 'version' => POLYLANG_VERSION,
183 );
184
185 update_option('polylang', $options);
186 }
187
188 // always provide a global $polylang object and add our rewrite rules if needed
189 $polylang = new StdClass();
190 $polylang->options = &$options;
191 $polylang->model = new PLL_Admin_Model($options);
192 $polylang->links_model = $polylang->model->get_links_model();
193 do_action('pll_init');
194 flush_rewrite_rules();
195 }
196
197 /*
198 * plugin deactivation for multisite
199 *
200 * @since 0.1
201 */
202 public function deactivate() {
203 $this->do_for_all_blogs('deactivate');
204 }
205
206 /*
207 * plugin deactivation
208 *
209 * @since 0.5
210 */
211 protected function _deactivate() {
212 flush_rewrite_rules();
213 }
214
215 /*
216 * blog creation on multisite (to set default options)
217 *
218 * @since 0.9.4
219 *
220 * @param int $blog_id
221 */
222 public function wpmu_new_blog($blog_id) {
223 switch_to_blog($blog_id);
224 $this->_activate();
225 restore_current_blog();
226 }
227
228 /*
229 * autoload classes
230 *
231 * @since 1.2
232 *
233 * @param string $class
234 */
235 public function autoload($class) {
236 $class = str_replace('_', '-', strtolower(substr($class, 4)));
237 foreach (array(PLL_INC, PLL_FRONT_INC, PLL_ADMIN_INC) as $path) {
238 if (file_exists($file = "$path/$class.php")) {
239 require_once($file);
240 break;
241 }
242 }
243 }
244
245 /*
246 * defines constants
247 * may be overriden by a plugin if set before plugins_loaded, 1
248 *
249 * @since 1.6
250 */
251 protected function define_constants() {
252 // cookie name. no cookie will be used if set to false
253 if (!defined('PLL_COOKIE'))
254 define('PLL_COOKIE', 'pll_language');
255
256 // backward compatibility WP < 3.6
257 // the search form js is no more needed in WP 3.6+ except if the search form is hardcoded elsewhere than in searchform.php
258 if (!defined('PLL_SEARCH_FORM_JS') && !version_compare($GLOBALS['wp_version'], '3.6', '<'))
259 define('PLL_SEARCH_FORM_JS', false);
260
261 // avoid loading polylang admin for frontend ajax requests
262 // special test for plupload which does not use jquery ajax and thus does not pass our ajax prefilter
263 // special test for customize_save done in frontend but for which we want to load the admin
264 if (!defined('PLL_AJAX_ON_FRONT')) {
265 $in = isset($_REQUEST['action']) && in_array($_REQUEST['action'], array('upload-attachment', 'customize_save'));
266 define('PLL_AJAX_ON_FRONT', defined('DOING_AJAX') && DOING_AJAX && empty($_REQUEST['pll_ajax_backend']) && !$in);
267 }
268
269 // admin
270 if (!defined('PLL_ADMIN'))
271 define('PLL_ADMIN', defined('DOING_CRON') || (is_admin() && !PLL_AJAX_ON_FRONT));
272
273 // settings page whatever the tab
274 if (!defined('PLL_SETTINGS'))
275 define('PLL_SETTINGS', is_admin() && isset($_GET['page']) && $_GET['page'] == 'mlang');
276 }
277
278 /*
279 * Polylang initialization
280 * setups models and separate admin and frontend
281 *
282 * @since 1.2
283 */
284 public function init() {
285 global $polylang;
286
287 $this->define_constants();
288 $options = get_option('polylang');
289
290 // plugin upgrade
291 if ($options && version_compare($options['version'], POLYLANG_VERSION, '<')) {
292 $upgrade = new PLL_Upgrade($options);
293 if (!$upgrade->upgrade()) // if the version is too old
294 return;
295 }
296
297 $class = apply_filters('pll_model', PLL_SETTINGS ? 'PLL_Admin_Model' : 'PLL_Model');
298 $model = new $class($options);
299 $links_model = $model->get_links_model();
300
301 if (PLL_ADMIN) {
302 $polylang = new PLL_Admin($links_model);
303 $polylang->init();
304 }
305 // do nothing on frontend if no language is defined
306 elseif ($model->get_languages_list()) {
307 $polylang = new PLL_Frontend($links_model);
308 $polylang->init();
309 }
310
311 do_action('pll_init');
312
313 if (!$model->get_languages_list())
314 do_action('pll_no_language_defined'); // to load overriden textdomains
315
316 // load wpml-config.xml
317 if (!defined('PLL_WPML_COMPAT') || PLL_WPML_COMPAT)
318 new PLL_WPML_Config;
319 }
320 }
321
322 new Polylang();
323