| @@ -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 | } |
| @@ -127,8 +129,10 @@ | ||
| 127 | 129 | * Defines constants |
| 128 | 130 | * May be overridden by a plugin if set before plugins_loaded, 1 |
| 129 | 131 | * |
| 130 | 132 | * @since 1.6 |
| 133 | + * | |
| 134 | + * @return void | |
| 131 | 135 | */ |
| 132 | 136 | public static function define_constants() { |
| 133 | 137 | // Cookie name. no cookie will be used if set to false |
| 134 | 138 | if ( ! defined( 'PLL_COOKIE' ) ) { |
| @@ -155,12 +159,12 @@ | ||
| 155 | 159 | * Polylang initialization |
| 156 | 160 | * setups models and separate admin and frontend |
| 157 | 161 | * |
| 158 | 162 | * @since 1.2 |
| 163 | + * | |
| 164 | + * @return void | |
| 159 | 165 | */ |
| 160 | 166 | public function init() { |
| 161 | - global $polylang; | |
| 162 | - | |
| 163 | 167 | self::define_constants(); |
| 164 | 168 | $options = get_option( 'polylang' ); |
| 165 | 169 | |
| 166 | 170 | // Plugin upgrade |
| @@ -175,11 +179,8 @@ | ||
| 175 | 179 | if ( ! $options ) { |
| 176 | 180 | $options = PLL_Install::get_default_options(); |
| 177 | 181 | } |
| 178 | 182 | |
| 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 | 183 | /** |
| 183 | 184 | * Filter the model class to use |
| 184 | 185 | * /!\ this filter is fired *before* the $polylang object is available |
| 185 | 186 | * |
| @@ -187,12 +188,12 @@ | ||
| 187 | 188 | * |
| 188 | 189 | * @param string $class either PLL_Model or PLL_Admin_Model |
| 189 | 190 | */ |
| 190 | 191 | $class = apply_filters( 'pll_model', PLL_SETTINGS || self::is_wizard() ? 'PLL_Admin_Model' : 'PLL_Model' ); |
| 192 | + /** @var PLL_Model $model */ | |
| 191 | 193 | $model = new $class( $options ); |
| 192 | - $links_model = $model->get_links_model(); | |
| 193 | 194 | |
| 194 | - if ( ! $model->get_languages_list() ) { | |
| 195 | + if ( ! $model->has_languages() ) { | |
| 195 | 196 | /** |
| 196 | 197 | * Fires when no language has been defined yet |
| 197 | 198 | * Used to load overridden textdomains |
| 198 | 199 | * |
| @@ -208,9 +209,9 @@ | ||
| 208 | 209 | } elseif ( PLL_ADMIN ) { |
| 209 | 210 | $class = 'PLL_Admin'; |
| 210 | 211 | } elseif ( self::is_rest_request() ) { |
| 211 | 212 | $class = 'PLL_REST_Request'; |
| 212 | - } elseif ( $model->get_languages_list() ) { | |
| 213 | + } elseif ( $model->has_languages() ) { | |
| 213 | 214 | $class = 'PLL_Frontend'; |
| 214 | 215 | } |
| 215 | 216 | |
| 216 | 217 | /** |
| @@ -222,35 +223,76 @@ | ||
| 222 | 223 | */ |
| 223 | 224 | $class = apply_filters( 'pll_context', $class ); |
| 224 | 225 | |
| 225 | 226 | if ( ! empty( $class ) ) { |
| 226 | - $polylang = new $class( $links_model ); | |
| 227 | + /** @phpstan-var class-string<TPLLClass> $class */ | |
| 228 | + $this->init_context( $class, $model ); | |
| 229 | + } | |
| 230 | + } | |
| 227 | 231 | |
| 228 | - /** | |
| 229 | - * Fires after the $polylang object is created and before the API is loaded | |
| 230 | - * | |
| 231 | - * @since 2.0 | |
| 232 | - * | |
| 233 | - * @param object $polylang | |
| 234 | - */ | |
| 235 | - 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; | |
| 236 | 247 | |
| 237 | - require_once __DIR__ . '/api.php'; // Loads the API | |
| 248 | + $links_model = $model->get_links_model(); | |
| 249 | + $polylang = new $class( $links_model ); | |
| 238 | 250 | |
| 239 | - // Loads the modules. | |
| 240 | - foreach ( glob( POLYLANG_DIR . '/modules/*/load.php', GLOB_NOSORT ) as $load_script ) { | |
| 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 ) { | |
| 241 | 281 | require_once $load_script; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable |
| 242 | 282 | } |
| 283 | + } | |
| 243 | 284 | |
| 244 | - $polylang->init(); | |
| 285 | + $polylang->init(); | |
| 245 | 286 | |
| 246 | - /** | |
| 247 | - * Fires after the $polylang object and the API is loaded | |
| 248 | - * | |
| 249 | - * @since 1.7 | |
| 250 | - * | |
| 251 | - * @param object $polylang | |
| 252 | - */ | |
| 253 | - do_action_ref_array( 'pll_init', array( &$polylang ) ); | |
| 254 | - } | |
| 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; | |
| 255 | 297 | } |
| 256 | 298 | } |