| @@ -18,18 +18,18 @@ | ||
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | 20 | * The page id of the static front page. |
| 21 | 21 | * |
| 22 | - * @var int | |
| 22 | + * @var int|null | |
| 23 | 23 | */ |
| 24 | 24 | protected $page_on_front; |
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | - * Constructor | |
| 27 | + * Constructor. | |
| 28 | 28 | * |
| 29 | 29 | * @since 1.8 |
| 30 | 30 | * |
| 31 | - * @param object $polylang | |
| 31 | + * @param object $polylang The Polylang object. | |
| 32 | 32 | */ |
| 33 | 33 | public function __construct( &$polylang ) { |
| 34 | 34 | parent::__construct( |
| 35 | 35 | $polylang, |
| @@ -36,9 +36,8 @@ | ||
| 36 | 36 | array( |
| 37 | 37 | 'module' => 'url', |
| 38 | 38 | 'title' => __( 'URL modifications', 'polylang' ), |
| 39 | 39 | 'description' => __( 'Decide how your URLs will look like.', 'polylang' ), |
| 40 | - 'configure' => true, | |
| 41 | 40 | ) |
| 42 | 41 | ); |
| 43 | 42 | |
| 44 | 43 | $this->page_on_front = &$polylang->static_pages->page_on_front; |
| @@ -51,19 +50,28 @@ | ||
| 51 | 50 | * |
| 52 | 51 | * @return void |
| 53 | 52 | */ |
| 54 | 53 | protected function force_lang() { |
| 55 | - ?> | |
| 56 | - <label> | |
| 54 | + if ( 'yes' === get_option( 'pll_language_from_content_available' ) ) { | |
| 55 | + ?> | |
| 56 | + <p class="description"><?php esc_html_e( 'Some themes or plugins may not be fully compatible with the language defined by the content or by domains.', 'polylang' ); ?></p> | |
| 57 | + <label> | |
| 58 | + <?php | |
| 59 | + printf( | |
| 60 | + '<input name="force_lang" type="radio" value="0" %s /> %s', | |
| 61 | + checked( $this->options['force_lang'], 0, false ), | |
| 62 | + esc_html__( 'The language is set from content', 'polylang' ) | |
| 63 | + ); | |
| 64 | + ?> | |
| 65 | + </label> | |
| 66 | + <p class="description"><?php esc_html_e( 'Posts, pages, categories and tags URLs will not be modified.', 'polylang' ); ?></p> | |
| 57 | 67 | <?php |
| 58 | - printf( | |
| 59 | - '<input name="force_lang" type="radio" value="0" %s /> %s', | |
| 60 | - checked( $this->options['force_lang'], 0, false ), | |
| 61 | - esc_html__( 'The language is set from content', 'polylang' ) | |
| 62 | - ); | |
| 68 | + } else { | |
| 63 | 69 | ?> |
| 64 | - </label> | |
| 65 | - <p class="description"><?php esc_html_e( 'Posts, pages, categories and tags URLs will not be modified.', 'polylang' ); ?></p> | |
| 70 | + <p class="description"><?php esc_html_e( 'Some themes or plugins may not be fully compatible with the language defined by domains.', 'polylang' ); ?></p> | |
| 71 | + <?php | |
| 72 | + } | |
| 73 | + ?> | |
| 66 | 74 | <label> |
| 67 | 75 | <?php |
| 68 | 76 | printf( |
| 69 | 77 | '<input name="force_lang" type="radio" value="1" %s/> %s', |
| @@ -96,9 +104,9 @@ | ||
| 96 | 104 | </label> |
| 97 | 105 | <table id="pll-domains-table" class="form-table" <?php echo 3 == $this->options['force_lang'] ? '' : 'style="display: none;"'; ?>> |
| 98 | 106 | <?php |
| 99 | 107 | foreach ( $this->model->get_languages_list() as $lg ) { |
| 100 | - $url = isset( $this->options['domains'][ $lg->slug ] ) ? $this->options['domains'][ $lg->slug ] : ( $lg->slug == $this->options['default_lang'] ? $this->links_model->home : '' ); | |
| 108 | + $url = $this->options['domains'][ $lg->slug ] ?? ( $lg->is_default ? $this->links_model->home : '' ); | |
| 101 | 109 | printf( |
| 102 | 110 | '<tr><td><label for="pll-domain[%1$s]">%2$s</label></td>' . |
| 103 | 111 | '<td><input name="domains[%1$s]" id="pll-domain[%1$s]" type="text" value="%3$s" class="regular-text code" aria-required="true" /></td></tr>', |
| 104 | 112 | esc_attr( $lg->slug ), |
| @@ -123,9 +131,9 @@ | ||
| 123 | 131 | <label> |
| 124 | 132 | <?php |
| 125 | 133 | printf( |
| 126 | 134 | '<input name="hide_default" type="checkbox" value="1" %s /> %s', |
| 127 | - checked( $this->options['hide_default'], 1, false ), | |
| 135 | + checked( $this->options['hide_default'], true, false ), | |
| 128 | 136 | esc_html__( 'Hide URL language information for default language', 'polylang' ) |
| 129 | 137 | ); |
| 130 | 138 | ?> |
| 131 | 139 | </label> |
| @@ -145,10 +153,14 @@ | ||
| 145 | 153 | <?php |
| 146 | 154 | printf( |
| 147 | 155 | '<input name="rewrite" type="radio" value="1" %s %s/> %s', |
| 148 | 156 | disabled( $this->links_model->using_permalinks, false, false ), |
| 149 | - checked( $this->options['rewrite'], 1, false ), | |
| 150 | - esc_html__( 'Remove /language/ in pretty permalinks', 'polylang' ) | |
| 157 | + checked( $this->options['rewrite'], true, false ), | |
| 158 | + sprintf( | |
| 159 | + /* translators: %s is a URL slug: `/language/`. */ | |
| 160 | + esc_html__( 'Remove %s in pretty permalinks', 'polylang' ), | |
| 161 | + '<code>/language/</code>' | |
| 162 | + ) | |
| 151 | 163 | ); |
| 152 | 164 | ?> |
| 153 | 165 | </label> |
| 154 | 166 | <p class="description"><?php echo esc_html__( 'Example:', 'polylang' ) . ' <code>' . esc_html( home_url( 'en/' ) ) . '</code>'; ?></p> |
| @@ -156,10 +168,14 @@ | ||
| 156 | 168 | <?php |
| 157 | 169 | printf( |
| 158 | 170 | '<input name="rewrite" type="radio" value="0" %s %s/> %s', |
| 159 | 171 | disabled( $this->links_model->using_permalinks, false, false ), |
| 160 | - checked( $this->options['rewrite'], 0, false ), | |
| 161 | - esc_html__( 'Keep /language/ in pretty permalinks', 'polylang' ) | |
| 172 | + checked( $this->options['rewrite'], false, false ), | |
| 173 | + sprintf( | |
| 174 | + /* translators: %s is a URL slug: `/language/`. */ | |
| 175 | + esc_html__( 'Keep %s in pretty permalinks', 'polylang' ), | |
| 176 | + '<code>/language/</code>' | |
| 177 | + ) | |
| 162 | 178 | ); |
| 163 | 179 | ?> |
| 164 | 180 | </label> |
| 165 | 181 | <p class="description"><?php echo esc_html__( 'Example:', 'polylang' ) . ' <code>' . esc_html( home_url( 'language/en/' ) ) . '</code>'; ?></p> |
| @@ -178,10 +194,10 @@ | ||
| 178 | 194 | <label> |
| 179 | 195 | <?php |
| 180 | 196 | printf( |
| 181 | 197 | '<input name="redirect_lang" type="checkbox" value="1" %s/> %s', |
| 182 | - checked( $this->options['redirect_lang'], 1, false ), | |
| 183 | - esc_html__( 'The front page url contains the language code instead of the page name or page id', 'polylang' ) | |
| 198 | + checked( $this->options['redirect_lang'], true, false ), | |
| 199 | + esc_html__( 'The front page URL contains the language code instead of the page name or page id', 'polylang' ) | |
| 184 | 200 | ); |
| 185 | 201 | ?> |
| 186 | 202 | </label> |
| 187 | 203 | <p class="description"> |
| @@ -187,9 +203,10 @@ | ||
| 187 | 203 | <p class="description"> |
| 188 | 204 | <?php |
| 189 | 205 | // That's nice to display the right home urls but don't forget that the page on front may have no language yet |
| 190 | 206 | $lang = $this->model->post->get_language( $this->page_on_front ); |
| 191 | - $lang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] ); | |
| 207 | + /** @var PLL_Language $lang */ | |
| 208 | + $lang = $lang ?: $this->model->get_default_language(); | |
| 192 | 209 | printf( |
| 193 | 210 | /* translators: %1$s example url when the option is active. %2$s example url when the option is not active */ |
| 194 | 211 | esc_html__( 'Example: %1$s instead of %2$s', 'polylang' ), |
| 195 | 212 | '<code>' . esc_html( $this->links_model->home_url( $lang ) ) . '</code>', |
| @@ -240,93 +257,23 @@ | ||
| 240 | 257 | <?php |
| 241 | 258 | } |
| 242 | 259 | |
| 243 | 260 | /** |
| 244 | - * Sanitizes the settings before saving | |
| 261 | + * Prepares the received data before saving. | |
| 245 | 262 | * |
| 246 | - * @since 1.8 | |
| 263 | + * @since 3.7 | |
| 247 | 264 | * |
| 248 | - * @param array $options | |
| 265 | + * @param array $options Raw values to save. | |
| 249 | 266 | * @return array |
| 250 | 267 | */ |
| 251 | - protected function update( $options ) { | |
| 252 | - $newoptions = array(); | |
| 268 | + protected function prepare_raw_data( array $options ): array { | |
| 269 | + $defaults = array( | |
| 270 | + 'force_lang' => 0, | |
| 271 | + 'domains' => array(), | |
| 272 | + 'hide_default' => 0, | |
| 273 | + 'rewrite' => 0, | |
| 274 | + 'redirect_lang' => 0, | |
| 275 | + ); | |
| 253 | 276 | |
| 254 | - foreach ( array( 'force_lang', 'rewrite' ) as $key ) { | |
| 255 | - $newoptions[ $key ] = isset( $options[ $key ] ) ? (int) $options[ $key ] : 0; | |
| 256 | - } | |
| 257 | - | |
| 258 | - if ( 3 == $options['force_lang'] && isset( $options['domains'] ) && is_array( $options['domains'] ) ) { | |
| 259 | - foreach ( $options['domains'] as $key => $domain ) { | |
| 260 | - if ( empty( $domain ) ) { | |
| 261 | - $lang = $this->model->get_language( $key ); | |
| 262 | - add_settings_error( | |
| 263 | - 'general', | |
| 264 | - 'pll_invalid_domain', | |
| 265 | - esc_html( | |
| 266 | - sprintf( | |
| 267 | - /* translators: %s is a native language name */ | |
| 268 | - __( 'Please enter a valid URL for %s.', 'polylang' ), | |
| 269 | - $lang->name | |
| 270 | - ) | |
| 271 | - ) | |
| 272 | - ); | |
| 273 | - } | |
| 274 | - else { | |
| 275 | - $newoptions['domains'][ $key ] = esc_url_raw( trim( $domain ) ); | |
| 276 | - } | |
| 277 | - } | |
| 278 | - } | |
| 279 | - | |
| 280 | - foreach ( array( 'hide_default', 'redirect_lang' ) as $key ) { | |
| 281 | - $newoptions[ $key ] = isset( $options[ $key ] ) ? 1 : 0; | |
| 282 | - } | |
| 283 | - | |
| 284 | - if ( 3 == $options['force_lang'] ) { | |
| 285 | - if ( ! class_exists( 'PLL_Xdata_Domain', true ) ) { | |
| 286 | - $newoptions['browser'] = 0; | |
| 287 | - } | |
| 288 | - $newoptions['hide_default'] = 0; | |
| 289 | - } | |
| 290 | - | |
| 291 | - // Check if domains exist | |
| 292 | - if ( $newoptions['force_lang'] > 1 ) { | |
| 293 | - $this->check_domains( $newoptions ); | |
| 294 | - } | |
| 295 | - | |
| 296 | - return $newoptions; // Take care to return only validated options | |
| 297 | - } | |
| 298 | - | |
| 299 | - /** | |
| 300 | - * Check if subdomains or domains are accessible | |
| 301 | - * | |
| 302 | - * @since 1.8 | |
| 303 | - * | |
| 304 | - * @param array $options new set of options to test | |
| 305 | - * @return void | |
| 306 | - */ | |
| 307 | - protected function check_domains( $options ) { | |
| 308 | - $options = array_merge( $this->options, $options ); | |
| 309 | - $model = new PLL_Model( $options ); | |
| 310 | - $links_model = $model->get_links_model(); | |
| 311 | - foreach ( $this->model->get_languages_list() as $lang ) { | |
| 312 | - $url = add_query_arg( 'deactivate-polylang', 1, $links_model->home_url( $lang ) ); | |
| 313 | - // Don't redefine vip_safe_wp_remote_get() as it has not the same signature as wp_remote_get() | |
| 314 | - $response = function_exists( 'vip_safe_wp_remote_get' ) ? vip_safe_wp_remote_get( esc_url_raw( $url ) ) : wp_remote_get( esc_url_raw( $url ) ); | |
| 315 | - $response_code = wp_remote_retrieve_response_code( $response ); | |
| 316 | - | |
| 317 | - if ( 200 != $response_code ) { | |
| 318 | - add_settings_error( | |
| 319 | - 'general', | |
| 320 | - 'pll_invalid_domain', | |
| 321 | - esc_html( | |
| 322 | - sprintf( | |
| 323 | - /* translators: %s is an url */ | |
| 324 | - __( 'Polylang was unable to access the %s URL. Please check that the URL is valid.', 'polylang' ), | |
| 325 | - $url | |
| 326 | - ) | |
| 327 | - ) | |
| 328 | - ); | |
| 329 | - } | |
| 330 | - } | |
| 277 | + return array_intersect_key( array_merge( $defaults, $options ), $defaults ); // Take care to return only validated options | |
| 331 | 278 | } |
| 332 | 279 | } |