| @@ -1,9 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * @package Polylang | |
| 4 | + */ | |
| 2 | 5 | |
| 3 | 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | 7 | exit; // Don't access directly |
| 5 | -}; | |
| 8 | +} | |
| 6 | 9 | |
| 7 | 10 | // Default directory to store user data such as custom flags |
| 8 | 11 | if ( ! defined( 'PLL_LOCAL_DIR' ) ) { |
| 9 | 12 | define( 'PLL_LOCAL_DIR', WP_CONTENT_DIR . '/polylang' ); |
| @@ -9,9 +12,9 @@ | ||
| 9 | 12 | define( 'PLL_LOCAL_DIR', WP_CONTENT_DIR . '/polylang' ); |
| 10 | 13 | } |
| 11 | 14 | |
| 12 | 15 | // Includes local config file if exists |
| 13 | -if ( file_exists( PLL_LOCAL_DIR . '/pll-config.php' ) ) { | |
| 16 | +if ( is_readable( PLL_LOCAL_DIR . '/pll-config.php' ) ) { | |
| 14 | 17 | include_once PLL_LOCAL_DIR . '/pll-config.php'; |
| 15 | 18 | } |
| 16 | 19 | |
| 17 | 20 | /** |
| @@ -17,8 +20,10 @@ | ||
| 17 | 20 | /** |
| 18 | 21 | * Controls the plugin, as well as activation, and deactivation |
| 19 | 22 | * |
| 20 | 23 | * @since 0.1 |
| 24 | + * | |
| 25 | + * @template TPLLClass of PLL_Base | |
| 21 | 26 | */ |
| 22 | 27 | class Polylang { |
| 23 | 28 | |
| 24 | 29 | /** |
| @@ -26,10 +31,9 @@ | ||
| 26 | 31 | * |
| 27 | 32 | * @since 0.1 |
| 28 | 33 | */ |
| 29 | 34 | public function __construct() { |
| 30 | - require_once PLL_INC . '/functions.php'; // VIP functions | |
| 31 | - spl_autoload_register( array( $this, 'autoload' ) ); // Autoload classes | |
| 35 | + require_once __DIR__ . '/functions.php'; // VIP functions | |
| 32 | 36 | |
| 33 | 37 | // register an action when plugin is activating. |
| 34 | 38 | register_activation_hook( POLYLANG_BASENAME, array( 'PLL_Wizard', 'start_wizard' ) ); |
| 35 | 39 | |
| @@ -49,66 +53,18 @@ | ||
| 49 | 53 | if ( ! defined( 'PLL_OLT' ) || PLL_OLT ) { |
| 50 | 54 | PLL_OLT_Manager::instance(); |
| 51 | 55 | } |
| 52 | 56 | |
| 53 | - // Extra code for compatibility with some plugins | |
| 54 | - // Loaded as soon as possible as we may need to act before other plugins are loaded | |
| 57 | + /* | |
| 58 | + * Loads the compatibility with some plugins and themes. | |
| 59 | + * Loaded as soon as possible as we may need to act before other plugins are loaded. | |
| 60 | + */ | |
| 55 | 61 | if ( ! defined( 'PLL_PLUGINS_COMPAT' ) || PLL_PLUGINS_COMPAT ) { |
| 56 | - PLL_Plugins_Compat::instance(); | |
| 62 | + PLL_Integrations::instance(); | |
| 57 | 63 | } |
| 58 | 64 | } |
| 59 | 65 | |
| 60 | 66 | /** |
| 61 | - * Autoload classes | |
| 62 | - * | |
| 63 | - * @since 1.2 | |
| 64 | - * | |
| 65 | - * @param string $class | |
| 66 | - */ | |
| 67 | - public function autoload( $class ) { | |
| 68 | - // Not a Polylang class | |
| 69 | - if ( 0 !== strncmp( 'PLL_', $class, 4 ) ) { | |
| 70 | - return; | |
| 71 | - } | |
| 72 | - | |
| 73 | - $class = str_replace( '_', '-', strtolower( substr( $class, 4 ) ) ); | |
| 74 | - $dirs = array(); | |
| 75 | - $parts = explode( '-', $class ); | |
| 76 | - $parts = array_values( array_diff( $parts, array( 'frontend', 'admin', 'settings', 'advanced' ) ) ); | |
| 77 | - if ( isset( $parts[0] ) ) { | |
| 78 | - $dirs[] = PLL_MODULES_INC . "/{$parts[0]}"; | |
| 79 | - if ( isset( $parts[1] ) ) { | |
| 80 | - $dirs[] = PLL_MODULES_INC . "/{$parts[0]}-{$parts[1]}"; | |
| 81 | - if ( isset( $parts[2] ) && in_array( $parts[1], array( 'post', 'term' ) ) ) { | |
| 82 | - $dirs[] = PLL_MODULES_INC . "/{$parts[0]}-{$parts[2]}"; | |
| 83 | - } | |
| 84 | - } | |
| 85 | - } | |
| 86 | - | |
| 87 | - $dirs = array_merge( | |
| 88 | - array( | |
| 89 | - PLL_FRONT_INC, | |
| 90 | - PLL_MODULES_INC, | |
| 91 | - ), | |
| 92 | - $dirs, | |
| 93 | - array( | |
| 94 | - PLL_MODULES_INC . '/plugins', | |
| 95 | - PLL_INSTALL_INC, | |
| 96 | - PLL_ADMIN_INC, | |
| 97 | - PLL_SETTINGS_INC, | |
| 98 | - PLL_INC, | |
| 99 | - ) | |
| 100 | - ); | |
| 101 | - | |
| 102 | - foreach ( $dirs as $dir ) { | |
| 103 | - if ( file_exists( $file = "$dir/$class.php" ) ) { | |
| 104 | - require_once $file; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable | |
| 105 | - return; | |
| 106 | - } | |
| 107 | - } | |
| 108 | - } | |
| 109 | - | |
| 110 | - /** | |
| 111 | 67 | * Tells whether the current request is an ajax request on frontend or not |
| 112 | 68 | * |
| 113 | 69 | * @since 2.2 |
| 114 | 70 | * |
| @@ -140,13 +96,13 @@ | ||
| 140 | 96 | * @return bool |
| 141 | 97 | */ |
| 142 | 98 | public static function is_rest_request() { |
| 143 | 99 | // Handle pretty permalinks. |
| 144 | - $home_path = trim( wp_parse_url( home_url(), PHP_URL_PATH ), '/' ); | |
| 100 | + $home_path = trim( (string) wp_parse_url( home_url(), PHP_URL_PATH ), '/' ); | |
| 145 | 101 | $home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) ); |
| 146 | 102 | |
| 147 | - $req_uri = trim( wp_parse_url( pll_get_requested_url(), PHP_URL_PATH ), '/' ); | |
| 148 | - $req_uri = preg_replace( $home_path_regex, '', $req_uri ); | |
| 103 | + $req_uri = trim( (string) wp_parse_url( pll_get_requested_url(), PHP_URL_PATH ), '/' ); | |
| 104 | + $req_uri = (string) preg_replace( $home_path_regex, '', $req_uri ); | |
| 149 | 105 | $req_uri = trim( $req_uri, '/' ); |
| 150 | 106 | $req_uri = str_replace( 'index.php', '', $req_uri ); |
| 151 | 107 | $req_uri = trim( $req_uri, '/' ); |
| 152 | 108 | |
| @@ -151,9 +107,9 @@ | ||
| 151 | 107 | $req_uri = trim( $req_uri, '/' ); |
| 152 | 108 | |
| 153 | 109 | // And also test rest_route query string parameter is not empty for plain permalinks. |
| 154 | 110 | $query_string = array(); |
| 155 | - wp_parse_str( wp_parse_url( pll_get_requested_url(), PHP_URL_QUERY ), $query_string ); | |
| 111 | + wp_parse_str( (string) wp_parse_url( pll_get_requested_url(), PHP_URL_QUERY ), $query_string ); | |
| 156 | 112 | $rest_route = isset( $query_string['rest_route'] ) ? trim( $query_string['rest_route'], '/' ) : false; |
| 157 | 113 | |
| 158 | 114 | return 0 === strpos( $req_uri, rest_get_url_prefix() . '/' ) || ! empty( $rest_route ); |
| 159 | 115 | } |
| @@ -173,8 +129,10 @@ | ||
| 173 | 129 | * Defines constants |
| 174 | 130 | * May be overridden by a plugin if set before plugins_loaded, 1 |
| 175 | 131 | * |
| 176 | 132 | * @since 1.6 |
| 133 | + * | |
| 134 | + * @return void | |
| 177 | 135 | */ |
| 178 | 136 | public static function define_constants() { |
| 179 | 137 | // Cookie name. no cookie will be used if set to false |
| 180 | 138 | if ( ! defined( 'PLL_COOKIE' ) ) { |
| @@ -201,12 +159,12 @@ | ||
| 201 | 159 | * Polylang initialization |
| 202 | 160 | * setups models and separate admin and frontend |
| 203 | 161 | * |
| 204 | 162 | * @since 1.2 |
| 163 | + * | |
| 164 | + * @return void | |
| 205 | 165 | */ |
| 206 | 166 | public function init() { |
| 207 | - global $polylang; | |
| 208 | - | |
| 209 | 167 | self::define_constants(); |
| 210 | 168 | $options = get_option( 'polylang' ); |
| 211 | 169 | |
| 212 | 170 | // Plugin upgrade |
| @@ -221,11 +179,8 @@ | ||
| 221 | 179 | if ( ! $options ) { |
| 222 | 180 | $options = PLL_Install::get_default_options(); |
| 223 | 181 | } |
| 224 | 182 | |
| 225 | - // Make sure that this filter is *always* added before PLL_Model::get_languages_list() is called for the first time | |
| 226 | - add_filter( 'pll_languages_list', array( 'PLL_Static_Pages', 'pll_languages_list' ), 2, 2 ); // Before PLL_Links_Model | |
| 227 | - | |
| 228 | 183 | /** |
| 229 | 184 | * Filter the model class to use |
| 230 | 185 | * /!\ this filter is fired *before* the $polylang object is available |
| 231 | 186 | * |
| @@ -233,12 +188,12 @@ | ||
| 233 | 188 | * |
| 234 | 189 | * @param string $class either PLL_Model or PLL_Admin_Model |
| 235 | 190 | */ |
| 236 | 191 | $class = apply_filters( 'pll_model', PLL_SETTINGS || self::is_wizard() ? 'PLL_Admin_Model' : 'PLL_Model' ); |
| 192 | + /** @var PLL_Model $model */ | |
| 237 | 193 | $model = new $class( $options ); |
| 238 | - $links_model = $model->get_links_model(); | |
| 239 | 194 | |
| 240 | - if ( ! $model->get_languages_list() ) { | |
| 195 | + if ( ! $model->has_languages() ) { | |
| 241 | 196 | /** |
| 242 | 197 | * Fires when no language has been defined yet |
| 243 | 198 | * Used to load overridden textdomains |
| 244 | 199 | * |
| @@ -254,9 +209,9 @@ | ||
| 254 | 209 | } elseif ( PLL_ADMIN ) { |
| 255 | 210 | $class = 'PLL_Admin'; |
| 256 | 211 | } elseif ( self::is_rest_request() ) { |
| 257 | 212 | $class = 'PLL_REST_Request'; |
| 258 | - } elseif ( $model->get_languages_list() && empty( $_GET['deactivate-polylang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 213 | + } elseif ( $model->has_languages() ) { | |
| 259 | 214 | $class = 'PLL_Frontend'; |
| 260 | 215 | } |
| 261 | 216 | |
| 262 | 217 | /** |
| @@ -268,35 +223,76 @@ | ||
| 268 | 223 | */ |
| 269 | 224 | $class = apply_filters( 'pll_context', $class ); |
| 270 | 225 | |
| 271 | 226 | if ( ! empty( $class ) ) { |
| 272 | - $polylang = new $class( $links_model ); | |
| 227 | + /** @phpstan-var class-string<TPLLClass> $class */ | |
| 228 | + $this->init_context( $class, $model ); | |
| 229 | + } | |
| 230 | + } | |
| 273 | 231 | |
| 274 | - /** | |
| 275 | - * Fires after the $polylang object is created and before the API is loaded | |
| 276 | - * | |
| 277 | - * @since 2.0 | |
| 278 | - * | |
| 279 | - * @param object $polylang | |
| 280 | - */ | |
| 281 | - do_action_ref_array( 'pll_pre_init', array( &$polylang ) ); | |
| 232 | + /** | |
| 233 | + * Polylang initialization. | |
| 234 | + * Setups the Polylang Context, loads the modules and init Polylang. | |
| 235 | + * | |
| 236 | + * @since 3.6 | |
| 237 | + * | |
| 238 | + * @param string $class The class name. | |
| 239 | + * @param PLL_Model $model Instance of PLL_Model. | |
| 240 | + * @return PLL_Base | |
| 241 | + * | |
| 242 | + * @phpstan-param class-string<TPLLClass> $class | |
| 243 | + * @phpstan-return TPLLClass | |
| 244 | + */ | |
| 245 | + public function init_context( string $class, PLL_Model $model ): PLL_Base { | |
| 246 | + global $polylang; | |
| 282 | 247 | |
| 283 | - require_once PLL_INC . '/api.php'; // Loads the API | |
| 248 | + $links_model = $model->get_links_model(); | |
| 249 | + $polylang = new $class( $links_model ); | |
| 284 | 250 | |
| 285 | - if ( ! defined( 'PLL_WPML_COMPAT' ) || PLL_WPML_COMPAT ) { | |
| 286 | - PLL_WPML_Compat::instance(); // WPML API | |
| 287 | - PLL_WPML_Config::instance(); // wpml-config.xml | |
| 251 | + /** | |
| 252 | + * Fires after Polylang's model init. | |
| 253 | + * This is the best place to register a custom table (see `PLL_Model`'s constructor). | |
| 254 | + * /!\ This hook is fired *before* the $polylang object is available. | |
| 255 | + * /!\ The languages are also not available yet. | |
| 256 | + * | |
| 257 | + * @since 3.4 | |
| 258 | + * | |
| 259 | + * @param PLL_Model $model Polylang model. | |
| 260 | + */ | |
| 261 | + do_action( 'pll_model_init', $model ); | |
| 262 | + | |
| 263 | + $model->maybe_create_language_terms(); | |
| 264 | + | |
| 265 | + /** | |
| 266 | + * Fires after the $polylang object is created and before the API is loaded | |
| 267 | + * | |
| 268 | + * @since 2.0 | |
| 269 | + * | |
| 270 | + * @param object $polylang | |
| 271 | + */ | |
| 272 | + do_action_ref_array( 'pll_pre_init', array( &$polylang ) ); | |
| 273 | + | |
| 274 | + // Loads the API | |
| 275 | + require_once POLYLANG_DIR . '/include/api.php'; | |
| 276 | + | |
| 277 | + // Loads the modules. | |
| 278 | + $load_scripts = glob( POLYLANG_DIR . '/modules/*/load.php', GLOB_NOSORT ); | |
| 279 | + if ( is_array( $load_scripts ) ) { | |
| 280 | + foreach ( $load_scripts as $load_script ) { | |
| 281 | + require_once $load_script; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable | |
| 288 | 282 | } |
| 283 | + } | |
| 289 | 284 | |
| 290 | - $polylang->init(); | |
| 285 | + $polylang->init(); | |
| 291 | 286 | |
| 292 | - /** | |
| 293 | - * Fires after the $polylang object and the API is loaded | |
| 294 | - * | |
| 295 | - * @since 1.7 | |
| 296 | - * | |
| 297 | - * @param object $polylang | |
| 298 | - */ | |
| 299 | - do_action_ref_array( 'pll_init', array( &$polylang ) ); | |
| 300 | - } | |
| 287 | + /** | |
| 288 | + * Fires after the $polylang object and the API is loaded | |
| 289 | + * | |
| 290 | + * @since 1.7 | |
| 291 | + * | |
| 292 | + * @param object $polylang | |
| 293 | + */ | |
| 294 | + do_action_ref_array( 'pll_init', array( &$polylang ) ); | |
| 295 | + | |
| 296 | + return $polylang; | |
| 301 | 297 | } |
| 302 | 298 | } |