| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Don't access directly |
| 5 |
}; |
| 6 |
|
| 7 |
// Default directory to store user data such as custom flags |
| 8 |
if ( ! defined( 'PLL_LOCAL_DIR' ) ) { |
| 9 |
define( 'PLL_LOCAL_DIR', WP_CONTENT_DIR . '/polylang' ); |
| 10 |
} |
| 11 |
|
| 12 |
// Includes local config file if exists |
| 13 |
if ( file_exists( PLL_LOCAL_DIR . '/pll-config.php' ) ) { |
| 14 |
include_once PLL_LOCAL_DIR . '/pll-config.php'; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Controls the plugin, as well as activation, and deactivation |
| 19 |
* |
| 20 |
* @since 0.1 |
| 21 |
*/ |
| 22 |
class Polylang { |
| 23 |
|
| 24 |
/** |
| 25 |
* Constructor |
| 26 |
* |
| 27 |
* @since 0.1 |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
require_once PLL_INC . '/functions.php'; // VIP functions |
| 31 |
spl_autoload_register( array( $this, 'autoload' ) ); // Autoload classes |
| 32 |
|
| 33 |
$install = new PLL_Install( POLYLANG_BASENAME ); |
| 34 |
|
| 35 |
// Stopping here if we are going to deactivate the plugin ( avoids breaking rewrite rules ) |
| 36 |
if ( $install->is_deactivation() ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
// Plugin initialization |
| 41 |
// Take no action before all plugins are loaded |
| 42 |
add_action( 'plugins_loaded', array( $this, 'init' ), 1 ); |
| 43 |
|
| 44 |
// Override load text domain waiting for the language to be defined |
| 45 |
// Here for plugins which load text domain as soon as loaded :( |
| 46 |
if ( ! defined( 'PLL_OLT' ) || PLL_OLT ) { |
| 47 |
PLL_OLT_Manager::instance(); |
| 48 |
} |
| 49 |
|
| 50 |
// Extra code for compatibility with some plugins |
| 51 |
// Loaded as soon as possible as we may need to act before other plugins are loaded |
| 52 |
if ( ! defined( 'PLL_PLUGINS_COMPAT' ) || PLL_PLUGINS_COMPAT ) { |
| 53 |
PLL_Plugins_Compat::instance(); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Autoload classes |
| 59 |
* |
| 60 |
* @since 1.2 |
| 61 |
* |
| 62 |
* @param string $class |
| 63 |
*/ |
| 64 |
public function autoload( $class ) { |
| 65 |
// Not a Polylang class |
| 66 |
if ( 0 !== strncmp( 'PLL_', $class, 4 ) ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
$class = str_replace( '_', '-', strtolower( substr( $class, 4 ) ) ); |
| 71 |
$to_find = array( 'media', 'share', 'slug', 'slugs', 'sync', 'translate', 'wpml', 'xdata', 'rest' ); |
| 72 |
$dir = implode( '-', array_intersect( explode( '-', $class ), $to_find ) ); |
| 73 |
|
| 74 |
$dirs = array( |
| 75 |
PLL_FRONT_INC, |
| 76 |
PLL_MODULES_INC, |
| 77 |
PLL_MODULES_INC . "/$dir", |
| 78 |
PLL_MODULES_INC . '/plugins', |
| 79 |
PLL_INSTALL_INC, |
| 80 |
PLL_ADMIN_INC, |
| 81 |
PLL_SETTINGS_INC, |
| 82 |
PLL_INC, |
| 83 |
); |
| 84 |
|
| 85 |
foreach ( $dirs as $dir ) { |
| 86 |
if ( file_exists( $file = "$dir/$class.php" ) ) { |
| 87 |
require_once $file; |
| 88 |
return; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Tells whether the current request is an ajax request on frontend or not |
| 95 |
* |
| 96 |
* @since 2.2 |
| 97 |
* |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
static public function is_ajax_on_front() { |
| 101 |
// Special test for plupload which does not use jquery ajax and thus does not pass our ajax prefilter |
| 102 |
// Special test for customize_save done in frontend but for which we want to load the admin |
| 103 |
$in = isset( $_REQUEST['action'] ) && in_array( $_REQUEST['action'], array( 'upload-attachment', 'customize_save' ) ); |
| 104 |
$is_ajax_on_front = wp_doing_ajax() && empty( $_REQUEST['pll_ajax_backend'] ) && ! $in; |
| 105 |
|
| 106 |
/** |
| 107 |
* Filters whether the current request is an ajax request on front. |
| 108 |
* |
| 109 |
* @since 2.3 |
| 110 |
* |
| 111 |
* @param bool $is_ajax_on_front Whether the current request is an ajax request on front. |
| 112 |
*/ |
| 113 |
return apply_filters( 'pll_is_ajax_on_front', $is_ajax_on_front ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Defines constants |
| 118 |
* May be overridden by a plugin if set before plugins_loaded, 1 |
| 119 |
* |
| 120 |
* @since 1.6 |
| 121 |
*/ |
| 122 |
static public function define_constants() { |
| 123 |
// Cookie name. no cookie will be used if set to false |
| 124 |
if ( ! defined( 'PLL_COOKIE' ) ) { |
| 125 |
define( 'PLL_COOKIE', 'pll_language' ); |
| 126 |
} |
| 127 |
|
| 128 |
// Backward compatibility with Polylang < 2.3 |
| 129 |
if ( ! defined( 'PLL_AJAX_ON_FRONT' ) ) { |
| 130 |
define( 'PLL_AJAX_ON_FRONT', self::is_ajax_on_front() ); |
| 131 |
} |
| 132 |
|
| 133 |
// Admin |
| 134 |
if ( ! defined( 'PLL_ADMIN' ) ) { |
| 135 |
define( 'PLL_ADMIN', defined( 'DOING_CRON' ) || ( defined( 'WP_CLI' ) && WP_CLI ) || ( is_admin() && ! PLL_AJAX_ON_FRONT ) ); |
| 136 |
} |
| 137 |
|
| 138 |
// Settings page whatever the tab |
| 139 |
if ( ! defined( 'PLL_SETTINGS' ) ) { |
| 140 |
define( 'PLL_SETTINGS', is_admin() && ( ( isset( $_GET['page'] ) && 0 === strpos( $_GET['page'], 'mlang' ) ) || ! empty( $_REQUEST['pll_ajax_settings'] ) ) ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Should we activate Polylang on front |
| 146 |
* |
| 147 |
* @since 2.3.2 |
| 148 |
* |
| 149 |
* @param object $model |
| 150 |
* @return bool |
| 151 |
*/ |
| 152 |
protected function is_active_on_front( $model ) { |
| 153 |
// Do nothing on frontend if no language is defined |
| 154 |
// Or if it is a REST request and the compatibility class is not present |
| 155 |
return $model->get_languages_list() && |
| 156 |
empty( $_GET['deactivate-polylang'] ) && ( |
| 157 |
class_exists( 'PLL_REST_Translated_Object' ) || |
| 158 |
false === strpos( str_replace( 'index.php', '', $_SERVER['REQUEST_URI'] ), '/' . rest_get_url_prefix() . '/' ) |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Polylang initialization |
| 164 |
* setups models and separate admin and frontend |
| 165 |
* |
| 166 |
* @since 1.2 |
| 167 |
*/ |
| 168 |
public function init() { |
| 169 |
global $polylang; |
| 170 |
|
| 171 |
self::define_constants(); |
| 172 |
$options = get_option( 'polylang' ); |
| 173 |
|
| 174 |
// Plugin upgrade |
| 175 |
if ( $options && version_compare( $options['version'], POLYLANG_VERSION, '<' ) ) { |
| 176 |
$upgrade = new PLL_Upgrade( $options ); |
| 177 |
if ( ! $upgrade->upgrade() ) { // If the version is too old |
| 178 |
return; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
// Make sure that this filter is *always* added before PLL_Model::get_languages_list() is called for the first time |
| 183 |
add_filter( 'pll_languages_list', array( 'PLL_Static_Pages', 'pll_languages_list' ), 2, 2 ); // Before PLL_Links_Model |
| 184 |
|
| 185 |
/** |
| 186 |
* Filter the model class to use |
| 187 |
* /!\ this filter is fired *before* the $polylang object is available |
| 188 |
* |
| 189 |
* @since 1.5 |
| 190 |
* |
| 191 |
* @param string $class either PLL_Model or PLL_Admin_Model |
| 192 |
*/ |
| 193 |
$class = apply_filters( 'pll_model', PLL_SETTINGS ? 'PLL_Admin_Model' : 'PLL_Model' ); |
| 194 |
$model = new $class( $options ); |
| 195 |
$links_model = $model->get_links_model(); |
| 196 |
|
| 197 |
if ( PLL_SETTINGS ) { |
| 198 |
$polylang = new PLL_Settings( $links_model ); |
| 199 |
} elseif ( PLL_ADMIN ) { |
| 200 |
$polylang = new PLL_Admin( $links_model ); |
| 201 |
} elseif ( $this->is_active_on_front( $model ) ) { |
| 202 |
$polylang = new PLL_Frontend( $links_model ); |
| 203 |
} |
| 204 |
|
| 205 |
if ( ! $model->get_languages_list() ) { |
| 206 |
/** |
| 207 |
* Fires when no language has been defined yet |
| 208 |
* Used to load overridden textdomains |
| 209 |
* |
| 210 |
* @since 1.2 |
| 211 |
*/ |
| 212 |
do_action( 'pll_no_language_defined' ); |
| 213 |
} |
| 214 |
|
| 215 |
if ( ! empty( $polylang ) ) { |
| 216 |
/** |
| 217 |
* Fires after the $polylang object is created and before the API is loaded |
| 218 |
* |
| 219 |
* @since 2.0 |
| 220 |
* |
| 221 |
* @param object $polylang |
| 222 |
*/ |
| 223 |
do_action_ref_array( 'pll_pre_init', array( &$polylang ) ); |
| 224 |
|
| 225 |
require_once PLL_INC . '/api.php'; // Loads the API |
| 226 |
|
| 227 |
if ( ! defined( 'PLL_WPML_COMPAT' ) || PLL_WPML_COMPAT ) { |
| 228 |
PLL_WPML_Compat::instance(); // WPML API |
| 229 |
PLL_WPML_Config::instance(); // wpml-config.xml |
| 230 |
} |
| 231 |
|
| 232 |
$polylang->init(); |
| 233 |
|
| 234 |
/** |
| 235 |
* Fires after the $polylang object and the API is loaded |
| 236 |
* |
| 237 |
* @since 1.7 |
| 238 |
* |
| 239 |
* @param object $polylang |
| 240 |
*/ |
| 241 |
do_action_ref_array( 'pll_init', array( &$polylang ) ); |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
new Polylang(); |
| 247 |
|