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