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

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