| 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-wpcom-vip.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' ); |
| 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 |
* defines constants |
| 95 |
* may be overriden by a plugin if set before plugins_loaded, 1 |
| 96 |
* |
| 97 |
* @since 1.6 |
| 98 |
*/ |
| 99 |
static public function define_constants() { |
| 100 |
// cookie name. no cookie will be used if set to false |
| 101 |
if ( ! defined( 'PLL_COOKIE' ) ) { |
| 102 |
define( 'PLL_COOKIE', 'pll_language' ); |
| 103 |
} |
| 104 |
|
| 105 |
// avoid loading polylang admin for frontend ajax requests |
| 106 |
// special test for plupload which does not use jquery ajax and thus does not pass our ajax prefilter |
| 107 |
// special test for customize_save done in frontend but for which we want to load the admin |
| 108 |
if ( ! defined( 'PLL_AJAX_ON_FRONT' ) ) { |
| 109 |
$in = isset( $_REQUEST['action'] ) && in_array( $_REQUEST['action'], array( 'upload-attachment', 'customize_save' ) ); |
| 110 |
define( 'PLL_AJAX_ON_FRONT', defined( 'DOING_AJAX' ) && DOING_AJAX && empty( $_REQUEST['pll_ajax_backend'] ) && ! $in ); |
| 111 |
} |
| 112 |
|
| 113 |
// admin |
| 114 |
if ( ! defined( 'PLL_ADMIN' ) ) { |
| 115 |
define( 'PLL_ADMIN', defined( 'DOING_CRON' ) || ( is_admin() && ! PLL_AJAX_ON_FRONT ) ); |
| 116 |
} |
| 117 |
|
| 118 |
// settings page whatever the tab |
| 119 |
if ( ! defined( 'PLL_SETTINGS' ) ) { |
| 120 |
define( 'PLL_SETTINGS', is_admin() && ( ( isset( $_GET['page'] ) && 0 === strpos( $_GET['page'], 'mlang' ) ) || ! empty( $_REQUEST['pll_ajax_settings'] ) ) ); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Polylang initialization |
| 126 |
* setups models and separate admin and frontend |
| 127 |
* |
| 128 |
* @since 1.2 |
| 129 |
*/ |
| 130 |
public function init() { |
| 131 |
global $polylang; |
| 132 |
|
| 133 |
self::define_constants(); |
| 134 |
$options = get_option( 'polylang' ); |
| 135 |
|
| 136 |
// plugin upgrade |
| 137 |
if ( $options && version_compare( $options['version'], POLYLANG_VERSION, '<' ) ) { |
| 138 |
$upgrade = new PLL_Upgrade( $options ); |
| 139 |
if ( ! $upgrade->upgrade() ) { // if the version is too old |
| 140 |
return; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
// Make sure that this filter is *always* added before PLL_Model::get_languages_list() is called for the first time |
| 145 |
add_filter( 'pll_languages_list', array( 'PLL_Static_Pages', 'pll_languages_list' ), 2, 2 ); // before PLL_Links_Model |
| 146 |
|
| 147 |
/** |
| 148 |
* Filter the model class to use |
| 149 |
* /!\ this filter is fired *before* the $polylang object is available |
| 150 |
* |
| 151 |
* @since 1.5 |
| 152 |
* |
| 153 |
* @param string $class either PLL_Model or PLL_Admin_Model |
| 154 |
*/ |
| 155 |
$class = apply_filters( 'pll_model', PLL_SETTINGS ? 'PLL_Admin_Model' : 'PLL_Model' ); |
| 156 |
$model = new $class( $options ); |
| 157 |
$links_model = $model->get_links_model(); |
| 158 |
|
| 159 |
if ( PLL_SETTINGS ) { |
| 160 |
$polylang = new PLL_Settings( $links_model ); |
| 161 |
} |
| 162 |
elseif ( PLL_ADMIN ) { |
| 163 |
$polylang = new PLL_Admin( $links_model ); |
| 164 |
} |
| 165 |
// do nothing on frontend if no language is defined |
| 166 |
elseif ( $model->get_languages_list() && empty( $_GET['deactivate-polylang'] ) ) { |
| 167 |
$polylang = new PLL_Frontend( $links_model ); |
| 168 |
} |
| 169 |
|
| 170 |
if ( ! $model->get_languages_list() ) { |
| 171 |
/** |
| 172 |
* Fires when no language has been defined yet |
| 173 |
* Used to load overriden textdomains |
| 174 |
* |
| 175 |
* @since 1.2 |
| 176 |
*/ |
| 177 |
do_action( 'pll_no_language_defined' ); |
| 178 |
} |
| 179 |
|
| 180 |
if ( ! empty( $polylang ) ) { |
| 181 |
/** |
| 182 |
* Fires after the $polylang object is created and before the API is loaded |
| 183 |
* |
| 184 |
* @since 2.0 |
| 185 |
* |
| 186 |
* @param object $polylang |
| 187 |
*/ |
| 188 |
do_action_ref_array( 'pll_pre_init', array( &$polylang ) ); |
| 189 |
|
| 190 |
require_once PLL_INC.'/api.php'; // loads the API |
| 191 |
|
| 192 |
if ( ! defined( 'PLL_WPML_COMPAT' ) || PLL_WPML_COMPAT ) { |
| 193 |
PLL_WPML_Compat::instance(); // WPML API |
| 194 |
PLL_WPML_Config::instance(); // wpml-config.xml |
| 195 |
} |
| 196 |
|
| 197 |
$polylang->init(); |
| 198 |
|
| 199 |
/** |
| 200 |
* Fires after the $polylang object and the API is loaded |
| 201 |
* |
| 202 |
* @since 1.7 |
| 203 |
* |
| 204 |
* @param object $polylang |
| 205 |
*/ |
| 206 |
do_action_ref_array( 'pll_init', array( &$polylang ) ); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
new Polylang(); |
| 212 |
|