| @@ -4,9 +4,9 @@ | ||
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | 7 | exit; // Don't access directly |
| 8 | -}; | |
| 8 | +} | |
| 9 | 9 | |
| 10 | 10 | // Default directory to store user data such as custom flags |
| 11 | 11 | if ( ! defined( 'PLL_LOCAL_DIR' ) ) { |
| 12 | 12 | define( 'PLL_LOCAL_DIR', WP_CONTENT_DIR . '/polylang' ); |
| @@ -12,9 +12,9 @@ | ||
| 12 | 12 | define( 'PLL_LOCAL_DIR', WP_CONTENT_DIR . '/polylang' ); |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | // Includes local config file if exists |
| 16 | -if ( file_exists( PLL_LOCAL_DIR . '/pll-config.php' ) ) { | |
| 16 | +if ( is_readable( PLL_LOCAL_DIR . '/pll-config.php' ) ) { | |
| 17 | 17 | include_once PLL_LOCAL_DIR . '/pll-config.php'; |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | /** |
| @@ -20,8 +20,10 @@ | ||
| 20 | 20 | /** |
| 21 | 21 | * Controls the plugin, as well as activation, and deactivation |
| 22 | 22 | * |
| 23 | 23 | * @since 0.1 |
| 24 | + * | |
| 25 | + * @template TPLLClass of PLL_Base | |
| 24 | 26 | */ |
| 25 | 27 | class Polylang { |
| 26 | 28 | |
| 27 | 29 | /** |
| @@ -94,13 +96,13 @@ | ||
| 94 | 96 | * @return bool |
| 95 | 97 | */ |
| 96 | 98 | public static function is_rest_request() { |
| 97 | 99 | // Handle pretty permalinks. |
| 98 | - $home_path = trim( wp_parse_url( home_url(), PHP_URL_PATH ), '/' ); | |
| 100 | + $home_path = trim( (string) wp_parse_url( home_url(), PHP_URL_PATH ), '/' ); | |
| 99 | 101 | $home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) ); |
| 100 | 102 | |
| 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( (string) wp_parse_url( pll_get_requested_url(), PHP_URL_PATH ), '/' ); | |
| 104 | + $req_uri = (string) preg_replace( $home_path_regex, '', $req_uri ); | |
| 103 | 105 | $req_uri = trim( $req_uri, '/' ); |
| 104 | 106 | $req_uri = str_replace( 'index.php', '', $req_uri ); |
| 105 | 107 | $req_uri = trim( $req_uri, '/' ); |
| 106 | 108 | |
| @@ -105,9 +107,9 @@ | ||
| 105 | 107 | $req_uri = trim( $req_uri, '/' ); |
| 106 | 108 | |
| 107 | 109 | // And also test rest_route query string parameter is not empty for plain permalinks. |
| 108 | 110 | $query_string = array(); |
| 109 | - 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 ); | |
| 110 | 112 | $rest_route = isset( $query_string['rest_route'] ) ? trim( $query_string['rest_route'], '/' ) : false; |
| 111 | 113 | |
| 112 | 114 | return 0 === strpos( $req_uri, rest_get_url_prefix() . '/' ) || ! empty( $rest_route ); |
| 113 | 115 | } |
| @@ -161,10 +163,8 @@ | ||
| 161 | 163 | * |
| 162 | 164 | * @return void |
| 163 | 165 | */ |
| 164 | 166 | public function init() { |
| 165 | - global $polylang; | |
| 166 | - | |
| 167 | 167 | self::define_constants(); |
| 168 | 168 | $options = get_option( 'polylang' ); |
| 169 | 169 | |
| 170 | 170 | // Plugin upgrade |
| @@ -179,11 +179,8 @@ | ||
| 179 | 179 | if ( ! $options ) { |
| 180 | 180 | $options = PLL_Install::get_default_options(); |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | - // Make sure that this filter is *always* added before PLL_Model::get_languages_list() is called for the first time | |
| 184 | - add_filter( 'pll_languages_list', array( 'PLL_Static_Pages', 'pll_languages_list' ), 2, 2 ); // Before PLL_Links_Model | |
| 185 | - | |
| 186 | 183 | /** |
| 187 | 184 | * Filter the model class to use |
| 188 | 185 | * /!\ this filter is fired *before* the $polylang object is available |
| 189 | 186 | * |
| @@ -191,12 +188,12 @@ | ||
| 191 | 188 | * |
| 192 | 189 | * @param string $class either PLL_Model or PLL_Admin_Model |
| 193 | 190 | */ |
| 194 | 191 | $class = apply_filters( 'pll_model', PLL_SETTINGS || self::is_wizard() ? 'PLL_Admin_Model' : 'PLL_Model' ); |
| 192 | + /** @var PLL_Model $model */ | |
| 195 | 193 | $model = new $class( $options ); |
| 196 | - $links_model = $model->get_links_model(); | |
| 197 | 194 | |
| 198 | - if ( ! $model->get_languages_list() ) { | |
| 195 | + if ( ! $model->has_languages() ) { | |
| 199 | 196 | /** |
| 200 | 197 | * Fires when no language has been defined yet |
| 201 | 198 | * Used to load overridden textdomains |
| 202 | 199 | * |
| @@ -212,9 +209,9 @@ | ||
| 212 | 209 | } elseif ( PLL_ADMIN ) { |
| 213 | 210 | $class = 'PLL_Admin'; |
| 214 | 211 | } elseif ( self::is_rest_request() ) { |
| 215 | 212 | $class = 'PLL_REST_Request'; |
| 216 | - } elseif ( $model->get_languages_list() ) { | |
| 213 | + } elseif ( $model->has_languages() ) { | |
| 217 | 214 | $class = 'PLL_Frontend'; |
| 218 | 215 | } |
| 219 | 216 | |
| 220 | 217 | /** |
| @@ -226,38 +223,76 @@ | ||
| 226 | 223 | */ |
| 227 | 224 | $class = apply_filters( 'pll_context', $class ); |
| 228 | 225 | |
| 229 | 226 | if ( ! empty( $class ) ) { |
| 230 | - $polylang = new $class( $links_model ); | |
| 227 | + /** @phpstan-var class-string<TPLLClass> $class */ | |
| 228 | + $this->init_context( $class, $model ); | |
| 229 | + } | |
| 230 | + } | |
| 231 | 231 | |
| 232 | - /** | |
| 233 | - * Fires after the $polylang object is created and before the API is loaded | |
| 234 | - * | |
| 235 | - * @since 2.0 | |
| 236 | - * | |
| 237 | - * @param object $polylang | |
| 238 | - */ | |
| 239 | - 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; | |
| 240 | 247 | |
| 241 | - require_once __DIR__ . '/api.php'; // Loads the API | |
| 248 | + $links_model = $model->get_links_model(); | |
| 249 | + $polylang = new $class( $links_model ); | |
| 242 | 250 | |
| 243 | - // Loads the modules. | |
| 244 | - $load_scripts = glob( POLYLANG_DIR . '/modules/*/load.php', GLOB_NOSORT ); | |
| 245 | - if ( is_array( $load_scripts ) ) { | |
| 246 | - foreach ( $load_scripts as $load_script ) { | |
| 247 | - require_once $load_script; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable | |
| 248 | - } | |
| 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 | |
| 249 | 282 | } |
| 283 | + } | |
| 250 | 284 | |
| 251 | - $polylang->init(); | |
| 285 | + $polylang->init(); | |
| 252 | 286 | |
| 253 | - /** | |
| 254 | - * Fires after the $polylang object and the API is loaded | |
| 255 | - * | |
| 256 | - * @since 1.7 | |
| 257 | - * | |
| 258 | - * @param object $polylang | |
| 259 | - */ | |
| 260 | - do_action_ref_array( 'pll_init', array( &$polylang ) ); | |
| 261 | - } | |
| 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; | |
| 262 | 297 | } |
| 263 | 298 | } |