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

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