| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | 4 | exit; // Don't access directly |
| 8 | 5 | }; |
| @@ -29,17 +26,15 @@ | ||
| 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.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 | 35 | // Stopping here if we are going to deactivate the plugin ( avoids breaking rewrite rules ) |
| 41 | - if ( $install->is_deactivation() || ! $install->can_activate() ) { | |
| 36 | + if ( $install->is_deactivation() ) { | |
| 42 | 37 | return; |
| 43 | 38 | } |
| 44 | 39 | |
| 45 | 40 | // Plugin initialization |
| @@ -51,18 +46,66 @@ | ||
| 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 | /** |
| 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 | + $dirs = array(); | |
| 72 | + $parts = explode( '-', $class ); | |
| 73 | + $parts = array_values( array_diff( $parts, array( 'frontend', 'admin', 'settings', 'advanced' ) ) ); | |
| 74 | + if ( isset( $parts[0] ) ) { | |
| 75 | + $dirs[] = PLL_MODULES_INC . "/{$parts[0]}"; | |
| 76 | + if ( isset( $parts[1] ) ) { | |
| 77 | + $dirs[] = PLL_MODULES_INC . "/{$parts[0]}-{$parts[1]}"; | |
| 78 | + if ( isset( $parts[2] ) && in_array( $parts[1], array( 'post', 'term' ) ) ) { | |
| 79 | + $dirs[] = PLL_MODULES_INC . "/{$parts[0]}-{$parts[2]}"; | |
| 80 | + } | |
| 81 | + } | |
| 82 | + } | |
| 83 | + | |
| 84 | + $dirs = array_merge( | |
| 85 | + array( | |
| 86 | + PLL_FRONT_INC, | |
| 87 | + PLL_MODULES_INC, | |
| 88 | + ), | |
| 89 | + $dirs, | |
| 90 | + array( | |
| 91 | + PLL_MODULES_INC . '/plugins', | |
| 92 | + PLL_INSTALL_INC, | |
| 93 | + PLL_ADMIN_INC, | |
| 94 | + PLL_SETTINGS_INC, | |
| 95 | + PLL_INC, | |
| 96 | + ) | |
| 97 | + ); | |
| 98 | + | |
| 99 | + foreach ( $dirs as $dir ) { | |
| 100 | + if ( file_exists( $file = "$dir/$class.php" ) ) { | |
| 101 | + require_once $file; | |
| 102 | + return; | |
| 103 | + } | |
| 104 | + } | |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 65 | 108 | * Tells whether the current request is an ajax request on frontend or not |
| 66 | 109 | * |
| 67 | 110 | * @since 2.2 |
| 68 | 111 | * |
| @@ -93,9 +136,8 @@ | ||
| 93 | 136 | * |
| 94 | 137 | * @return bool |
| 95 | 138 | */ |
| 96 | 139 | public static function is_rest_request() { |
| 97 | - // Handle pretty permalinks. | |
| 98 | 140 | $home_path = trim( wp_parse_url( home_url(), PHP_URL_PATH ), '/' ); |
| 99 | 141 | $home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) ); |
| 100 | 142 | |
| 101 | 143 | $req_uri = trim( wp_parse_url( pll_get_requested_url(), PHP_URL_PATH ), '/' ); |
| @@ -103,28 +145,12 @@ | ||
| 103 | 145 | $req_uri = trim( $req_uri, '/' ); |
| 104 | 146 | $req_uri = str_replace( 'index.php', '', $req_uri ); |
| 105 | 147 | $req_uri = trim( $req_uri, '/' ); |
| 106 | 148 | |
| 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 ); | |
| 149 | + return 0 === strpos( $req_uri, rest_get_url_prefix() . '/' ); | |
| 113 | 150 | } |
| 114 | 151 | |
| 115 | 152 | /** |
| 116 | - * Tells if we are in the wizard process. | |
| 117 | - * | |
| 118 | - * @since 2.7 | |
| 119 | - * | |
| 120 | - * @return bool | |
| 121 | - */ | |
| 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 | - } | |
| 125 | - | |
| 126 | - /** | |
| 127 | 153 | * Defines constants |
| 128 | 154 | * May be overridden by a plugin if set before plugins_loaded, 1 |
| 129 | 155 | * |
| 130 | 156 | * @since 1.6 |
| @@ -144,11 +170,11 @@ | ||
| 144 | 170 | if ( ! defined( 'PLL_ADMIN' ) ) { |
| 145 | 171 | define( 'PLL_ADMIN', wp_doing_cron() || ( defined( 'WP_CLI' ) && WP_CLI ) || ( is_admin() && ! PLL_AJAX_ON_FRONT ) ); |
| 146 | 172 | } |
| 147 | 173 | |
| 148 | - // Settings page whatever the tab except for the wizard which needs to be an admin process. | |
| 174 | + // Settings page whatever the tab | |
| 149 | 175 | 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 | |
| 176 | + define( 'PLL_SETTINGS', is_admin() && ( ( isset( $_GET['page'] ) && 0 === strpos( sanitize_key( $_GET['page'] ), 'mlang' ) ) || ! empty( $_REQUEST['pll_ajax_settings'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 151 | 177 | } |
| 152 | 178 | } |
| 153 | 179 | |
| 154 | 180 | /** |
| @@ -170,13 +196,8 @@ | ||
| 170 | 196 | return; |
| 171 | 197 | } |
| 172 | 198 | } |
| 173 | 199 | |
| 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 | 200 | // Make sure that this filter is *always* added before PLL_Model::get_languages_list() is called for the first time |
| 180 | 201 | add_filter( 'pll_languages_list', array( 'PLL_Static_Pages', 'pll_languages_list' ), 2, 2 ); // Before PLL_Links_Model |
| 181 | 202 | |
| 182 | 203 | /** |
| @@ -186,9 +207,9 @@ | ||
| 186 | 207 | * @since 1.5 |
| 187 | 208 | * |
| 188 | 209 | * @param string $class either PLL_Model or PLL_Admin_Model |
| 189 | 210 | */ |
| 190 | - $class = apply_filters( 'pll_model', PLL_SETTINGS || self::is_wizard() ? 'PLL_Admin_Model' : 'PLL_Model' ); | |
| 211 | + $class = apply_filters( 'pll_model', PLL_SETTINGS ? 'PLL_Admin_Model' : 'PLL_Model' ); | |
| 191 | 212 | $model = new $class( $options ); |
| 192 | 213 | $links_model = $model->get_links_model(); |
| 193 | 214 | |
| 194 | 215 | if ( ! $model->get_languages_list() ) { |
| @@ -233,13 +254,13 @@ | ||
| 233 | 254 | * @param object $polylang |
| 234 | 255 | */ |
| 235 | 256 | do_action_ref_array( 'pll_pre_init', array( &$polylang ) ); |
| 236 | 257 | |
| 237 | - require_once __DIR__ . '/api.php'; // Loads the API | |
| 258 | + require_once PLL_INC . '/api.php'; // Loads the API | |
| 238 | 259 | |
| 239 | - // Loads the modules. | |
| 240 | - foreach ( glob( POLYLANG_DIR . '/modules/*/load.php', GLOB_NOSORT ) as $load_script ) { // phpcs:ignore WordPressVIPMinimum.Variables.VariableAnalysis.UnusedVariable | |
| 241 | - require_once $load_script; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable | |
| 260 | + if ( ! defined( 'PLL_WPML_COMPAT' ) || PLL_WPML_COMPAT ) { | |
| 261 | + PLL_WPML_Compat::instance(); // WPML API | |
| 262 | + PLL_WPML_Config::instance(); // wpml-config.xml | |
| 242 | 263 | } |
| 243 | 264 | |
| 244 | 265 | $polylang->init(); |
| 245 | 266 | |