| @@ -1,25 +1,22 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | - exit; // Don't access directly | |
| 4 | + exit; // don't access directly | |
| 8 | 5 | }; |
| 9 | 6 | |
| 10 | -// Default directory to store user data such as custom flags | |
| 7 | +// default directory to store user data such as custom flags | |
| 11 | 8 | if ( ! defined( 'PLL_LOCAL_DIR' ) ) { |
| 12 | 9 | define( 'PLL_LOCAL_DIR', WP_CONTENT_DIR . '/polylang' ); |
| 13 | 10 | } |
| 14 | 11 | |
| 15 | -// Includes local config file if exists | |
| 12 | +// includes local config file if exists | |
| 16 | 13 | if ( file_exists( PLL_LOCAL_DIR . '/pll-config.php' ) ) { |
| 17 | - include_once PLL_LOCAL_DIR . '/pll-config.php'; | |
| 14 | + include_once( PLL_LOCAL_DIR . '/pll-config.php' ); | |
| 18 | 15 | } |
| 19 | 16 | |
| 20 | 17 | /** |
| 21 | - * Controls the plugin, as well as activation, and deactivation | |
| 18 | + * controls the plugin, as well as activation, and deactivation | |
| 22 | 19 | * |
| 23 | 20 | * @since 0.1 |
| 24 | 21 | */ |
| 25 | 22 | class Polylang { |
| @@ -24,131 +21,114 @@ | ||
| 24 | 21 | */ |
| 25 | 22 | class Polylang { |
| 26 | 23 | |
| 27 | 24 | /** |
| 28 | - * Constructor | |
| 25 | + * constructor | |
| 29 | 26 | * |
| 30 | 27 | * @since 0.1 |
| 31 | 28 | */ |
| 32 | 29 | public function __construct() { |
| 33 | - require_once __DIR__ . '/functions.php'; // VIP functions | |
| 30 | + require_once( PLL_INC . '/functions-wpcom-vip.php' ); // VIP functions | |
| 31 | + spl_autoload_register( array( $this, 'autoload' ) ); // autoload classes | |
| 34 | 32 | |
| 35 | - // register an action when plugin is activating. | |
| 36 | - register_activation_hook( POLYLANG_BASENAME, array( 'PLL_Wizard', 'start_wizard' ) ); | |
| 37 | - | |
| 38 | 33 | $install = new PLL_Install( POLYLANG_BASENAME ); |
| 39 | 34 | |
| 40 | - // Stopping here if we are going to deactivate the plugin ( avoids breaking rewrite rules ) | |
| 41 | - if ( $install->is_deactivation() || ! $install->can_activate() ) { | |
| 35 | + // stopping here if we are going to deactivate the plugin ( avoids breaking rewrite rules ) | |
| 36 | + if ( $install->is_deactivation() ) { | |
| 42 | 37 | return; |
| 43 | 38 | } |
| 44 | 39 | |
| 45 | - // Plugin initialization | |
| 46 | - // Take no action before all plugins are loaded | |
| 40 | + // plugin initialization | |
| 41 | + // take no action before all plugins are loaded | |
| 47 | 42 | add_action( 'plugins_loaded', array( $this, 'init' ), 1 ); |
| 48 | 43 | |
| 49 | - // Override load text domain waiting for the language to be defined | |
| 50 | - // Here for plugins which load text domain as soon as loaded :( | |
| 44 | + // override load text domain waiting for the language to be defined | |
| 45 | + // here for plugins which load text domain as soon as loaded :( | |
| 51 | 46 | if ( ! defined( 'PLL_OLT' ) || PLL_OLT ) { |
| 52 | 47 | PLL_OLT_Manager::instance(); |
| 53 | 48 | } |
| 54 | 49 | |
| 55 | - /* | |
| 56 | - * Loads the compatibility with some plugins and themes. | |
| 57 | - * Loaded as soon as possible as we may need to act before other plugins are loaded. | |
| 58 | - */ | |
| 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 | |
| 59 | 52 | if ( ! defined( 'PLL_PLUGINS_COMPAT' ) || PLL_PLUGINS_COMPAT ) { |
| 60 | - PLL_Integrations::instance(); | |
| 53 | + PLL_Plugins_Compat::instance(); | |
| 61 | 54 | } |
| 62 | 55 | } |
| 63 | 56 | |
| 64 | 57 | /** |
| 65 | - * Tells whether the current request is an ajax request on frontend or not | |
| 58 | + * autoload classes | |
| 66 | 59 | * |
| 67 | - * @since 2.2 | |
| 60 | + * @since 1.2 | |
| 68 | 61 | * |
| 69 | - * @return bool | |
| 62 | + * @param string $class | |
| 70 | 63 | */ |
| 71 | - public static function is_ajax_on_front() { | |
| 72 | - // Special test for plupload which does not use jquery ajax and thus does not pass our ajax prefilter | |
| 73 | - // Special test for customize_save done in frontend but for which we want to load the admin | |
| 74 | - $in = isset( $_REQUEST['action'] ) && in_array( sanitize_key( $_REQUEST['action'] ), array( 'upload-attachment', 'customize_save' ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 75 | - $is_ajax_on_front = wp_doing_ajax() && empty( $_REQUEST['pll_ajax_backend'] ) && ! $in; // phpcs:ignore WordPress.Security.NonceVerification | |
| 64 | + public function autoload( $class ) { | |
| 65 | + // not a Polylang class | |
| 66 | + if ( 0 !== strncmp( 'PLL_', $class, 4 ) ) { | |
| 67 | + return; | |
| 68 | + } | |
| 76 | 69 | |
| 77 | - /** | |
| 78 | - * Filters whether the current request is an ajax request on front. | |
| 79 | - * | |
| 80 | - * @since 2.3 | |
| 81 | - * | |
| 82 | - * @param bool $is_ajax_on_front Whether the current request is an ajax request on front. | |
| 83 | - */ | |
| 84 | - return apply_filters( 'pll_is_ajax_on_front', $is_ajax_on_front ); | |
| 85 | - } | |
| 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 ) ); | |
| 86 | 73 | |
| 87 | - /** | |
| 88 | - * Is the current request a REST API request? | |
| 89 | - * Inspired by WP::parse_request() | |
| 90 | - * Needed because at this point, the constant REST_REQUEST is not defined yet | |
| 91 | - * | |
| 92 | - * @since 2.4.1 | |
| 93 | - * | |
| 94 | - * @return bool | |
| 95 | - */ | |
| 96 | - public static function is_rest_request() { | |
| 97 | - // Handle pretty permalinks. | |
| 98 | - $home_path = trim( wp_parse_url( home_url(), PHP_URL_PATH ), '/' ); | |
| 99 | - $home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) ); | |
| 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 | + ); | |
| 100 | 84 | |
| 101 | - $req_uri = trim( wp_parse_url( pll_get_requested_url(), PHP_URL_PATH ), '/' ); | |
| 102 | - $req_uri = preg_replace( $home_path_regex, '', $req_uri ); | |
| 103 | - $req_uri = trim( $req_uri, '/' ); | |
| 104 | - $req_uri = str_replace( 'index.php', '', $req_uri ); | |
| 105 | - $req_uri = trim( $req_uri, '/' ); | |
| 106 | - | |
| 107 | - // And also test rest_route query string parameter is not empty for plain permalinks. | |
| 108 | - $query_string = array(); | |
| 109 | - wp_parse_str( wp_parse_url( pll_get_requested_url(), PHP_URL_QUERY ), $query_string ); | |
| 110 | - $rest_route = isset( $query_string['rest_route'] ) ? trim( $query_string['rest_route'], '/' ) : false; | |
| 111 | - | |
| 112 | - return 0 === strpos( $req_uri, rest_get_url_prefix() . '/' ) || ! empty( $rest_route ); | |
| 85 | + foreach ( $dirs as $dir ) { | |
| 86 | + if ( file_exists( $file = "$dir/$class.php" ) ) { | |
| 87 | + require_once( $file ); | |
| 88 | + return; | |
| 89 | + } | |
| 90 | + } | |
| 113 | 91 | } |
| 114 | 92 | |
| 115 | 93 | /** |
| 116 | - * Tells if we are in the wizard process. | |
| 94 | + * defines constants | |
| 95 | + * may be overriden by a plugin if set before plugins_loaded, 1 | |
| 117 | 96 | * |
| 118 | - * @since 2.7 | |
| 119 | - * | |
| 120 | - * @return bool | |
| 97 | + * @since 1.6 | |
| 121 | 98 | */ |
| 122 | - public static function is_wizard() { | |
| 123 | - return isset( $_GET['page'] ) && ! empty( $_GET['page'] ) && 'mlang_wizard' === sanitize_key( $_GET['page'] ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 124 | - } | |
| 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 | + } | |
| 125 | 104 | |
| 126 | - /** | |
| 127 | - * Defines constants | |
| 128 | - * May be overridden by a plugin if set before plugins_loaded, 1 | |
| 129 | - * | |
| 130 | - * @since 1.6 | |
| 131 | - */ | |
| 132 | - public static function define_constants() { | |
| 133 | - // Cookie name. no cookie will be used if set to false | |
| 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 | |
| 134 | 111 | if ( ! defined( 'PLL_COOKIE' ) ) { |
| 135 | 112 | define( 'PLL_COOKIE', 'pll_language' ); |
| 136 | 113 | } |
| 137 | 114 | |
| 138 | - // Backward compatibility with Polylang < 2.3 | |
| 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 | |
| 139 | 118 | if ( ! defined( 'PLL_AJAX_ON_FRONT' ) ) { |
| 140 | - define( 'PLL_AJAX_ON_FRONT', self::is_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 ); | |
| 141 | 121 | } |
| 142 | 122 | |
| 143 | - // Admin | |
| 123 | + // admin | |
| 144 | 124 | if ( ! defined( 'PLL_ADMIN' ) ) { |
| 145 | - define( 'PLL_ADMIN', wp_doing_cron() || ( defined( 'WP_CLI' ) && WP_CLI ) || ( is_admin() && ! PLL_AJAX_ON_FRONT ) ); | |
| 125 | + define( 'PLL_ADMIN', defined( 'DOING_CRON' ) || ( is_admin() && ! PLL_AJAX_ON_FRONT ) ); | |
| 146 | 126 | } |
| 147 | 127 | |
| 148 | - // Settings page whatever the tab except for the wizard which needs to be an admin process. | |
| 128 | + // settings page whatever the tab | |
| 149 | 129 | if ( ! defined( 'PLL_SETTINGS' ) ) { |
| 150 | - define( 'PLL_SETTINGS', is_admin() && ( ( isset( $_GET['page'] ) && 0 === strpos( sanitize_key( $_GET['page'] ), 'mlang' ) && ! self::is_wizard() ) || ! empty( $_REQUEST['pll_ajax_settings'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 130 | + define( 'PLL_SETTINGS', is_admin() && ( ( isset( $_GET['page'] ) && 'mlang' == $_GET['page'] ) || ! empty( $_REQUEST['pll_ajax_settings'] ) ) ); | |
| 151 | 131 | } |
| 152 | 132 | } |
| 153 | 133 | |
| 154 | 134 | /** |
| @@ -162,24 +142,16 @@ | ||
| 162 | 142 | |
| 163 | 143 | self::define_constants(); |
| 164 | 144 | $options = get_option( 'polylang' ); |
| 165 | 145 | |
| 166 | - // Plugin upgrade | |
| 146 | + // plugin upgrade | |
| 167 | 147 | if ( $options && version_compare( $options['version'], POLYLANG_VERSION, '<' ) ) { |
| 168 | 148 | $upgrade = new PLL_Upgrade( $options ); |
| 169 | - if ( ! $upgrade->upgrade() ) { // If the version is too old | |
| 149 | + if ( ! $upgrade->upgrade() ) { // if the version is too old | |
| 170 | 150 | return; |
| 171 | 151 | } |
| 172 | 152 | } |
| 173 | 153 | |
| 174 | - // In some edge cases, it's possible that no options were found in the database. Load default options as we need some. | |
| 175 | - if ( ! $options ) { | |
| 176 | - $options = PLL_Install::get_default_options(); | |
| 177 | - } | |
| 178 | - | |
| 179 | - // Make sure that this filter is *always* added before PLL_Model::get_languages_list() is called for the first time | |
| 180 | - add_filter( 'pll_languages_list', array( 'PLL_Static_Pages', 'pll_languages_list' ), 2, 2 ); // Before PLL_Links_Model | |
| 181 | - | |
| 182 | 154 | /** |
| 183 | 155 | * Filter the model class to use |
| 184 | 156 | * /!\ this filter is fired *before* the $polylang object is available |
| 185 | 157 | * |
| @@ -186,16 +158,29 @@ | ||
| 186 | 158 | * @since 1.5 |
| 187 | 159 | * |
| 188 | 160 | * @param string $class either PLL_Model or PLL_Admin_Model |
| 189 | 161 | */ |
| 190 | - $class = apply_filters( 'pll_model', PLL_SETTINGS || self::is_wizard() ? 'PLL_Admin_Model' : 'PLL_Model' ); | |
| 162 | + $class = apply_filters( 'pll_model', PLL_SETTINGS ? 'PLL_Admin_Model' : 'PLL_Model' ); | |
| 191 | 163 | $model = new $class( $options ); |
| 192 | 164 | $links_model = $model->get_links_model(); |
| 193 | 165 | |
| 166 | + add_filter( 'pll_languages_list', array( 'PLL_Static_Pages', 'pll_languages_list' ), 2, 2 ); // before PLL_Links_Model | |
| 167 | + | |
| 168 | + if ( PLL_SETTINGS ) { | |
| 169 | + $polylang = new PLL_Settings( $links_model ); | |
| 170 | + } | |
| 171 | + elseif ( PLL_ADMIN ) { | |
| 172 | + $polylang = new PLL_Admin( $links_model ); | |
| 173 | + } | |
| 174 | + // do nothing on frontend if no language is defined | |
| 175 | + elseif ( $model->get_languages_list() && empty( $_GET['deactivate-polylang'] ) ) { | |
| 176 | + $polylang = new PLL_Frontend( $links_model ); | |
| 177 | + } | |
| 178 | + | |
| 194 | 179 | if ( ! $model->get_languages_list() ) { |
| 195 | 180 | /** |
| 196 | 181 | * Fires when no language has been defined yet |
| 197 | - * Used to load overridden textdomains | |
| 182 | + * Used to load overriden textdomains | |
| 198 | 183 | * |
| 199 | 184 | * @since 1.2 |
| 200 | 185 | */ |
| 201 | 186 | do_action( 'pll_no_language_defined' ); |
| @@ -200,32 +185,9 @@ | ||
| 200 | 185 | */ |
| 201 | 186 | do_action( 'pll_no_language_defined' ); |
| 202 | 187 | } |
| 203 | 188 | |
| 204 | - $class = ''; | |
| 205 | - | |
| 206 | - if ( PLL_SETTINGS ) { | |
| 207 | - $class = 'PLL_Settings'; | |
| 208 | - } elseif ( PLL_ADMIN ) { | |
| 209 | - $class = 'PLL_Admin'; | |
| 210 | - } elseif ( self::is_rest_request() ) { | |
| 211 | - $class = 'PLL_REST_Request'; | |
| 212 | - } elseif ( $model->get_languages_list() ) { | |
| 213 | - $class = 'PLL_Frontend'; | |
| 214 | - } | |
| 215 | - | |
| 216 | - /** | |
| 217 | - * Filters the class to use to instantiate the $polylang object | |
| 218 | - * | |
| 219 | - * @since 2.6 | |
| 220 | - * | |
| 221 | - * @param string $class A class name. | |
| 222 | - */ | |
| 223 | - $class = apply_filters( 'pll_context', $class ); | |
| 224 | - | |
| 225 | - if ( ! empty( $class ) ) { | |
| 226 | - $polylang = new $class( $links_model ); | |
| 227 | - | |
| 189 | + if ( ! empty( $polylang ) ) { | |
| 228 | 190 | /** |
| 229 | 191 | * Fires after the $polylang object is created and before the API is loaded |
| 230 | 192 | * |
| 231 | 193 | * @since 2.0 |
| @@ -233,13 +195,13 @@ | ||
| 233 | 195 | * @param object $polylang |
| 234 | 196 | */ |
| 235 | 197 | do_action_ref_array( 'pll_pre_init', array( &$polylang ) ); |
| 236 | 198 | |
| 237 | - require_once __DIR__ . '/api.php'; // Loads the API | |
| 199 | + require_once( PLL_INC.'/api.php' ); // loads the API | |
| 238 | 200 | |
| 239 | - // Loads the modules. | |
| 240 | - foreach ( glob( POLYLANG_DIR . '/modules/*/load.php', GLOB_NOSORT ) as $load_script ) { | |
| 241 | - require_once $load_script; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable | |
| 201 | + if ( ! defined( 'PLL_WPML_COMPAT' ) || PLL_WPML_COMPAT ) { | |
| 202 | + PLL_WPML_Compat::instance(); // WPML API | |
| 203 | + PLL_WPML_Config::instance(); // wpml-config.xml | |
| 242 | 204 | } |
| 243 | 205 | |
| 244 | 206 | $polylang->init(); |
| 245 | 207 | |
| @@ -253,4 +215,6 @@ | ||
| 253 | 215 | do_action_ref_array( 'pll_init', array( &$polylang ) ); |
| 254 | 216 | } |
| 255 | 217 | } |
| 256 | 218 | } |
| 219 | + | |
| 220 | +new Polylang(); | |