| @@ -16,13 +16,20 @@ | ||
| 16 | 16 | */ |
| 17 | 17 | public $priority = 10; |
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | - * Constructor | |
| 20 | + * The page id of the static front page. | |
| 21 | 21 | * |
| 22 | + * @var int|null | |
| 23 | + */ | |
| 24 | + protected $page_on_front; | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * Constructor. | |
| 28 | + * | |
| 22 | 29 | * @since 1.8 |
| 23 | 30 | * |
| 24 | - * @param object $polylang | |
| 31 | + * @param object $polylang The Polylang object. | |
| 25 | 32 | */ |
| 26 | 33 | public function __construct( &$polylang ) { |
| 27 | 34 | parent::__construct( |
| 28 | 35 | $polylang, |
| @@ -29,13 +36,11 @@ | ||
| 29 | 36 | array( |
| 30 | 37 | 'module' => 'url', |
| 31 | 38 | 'title' => __( 'URL modifications', 'polylang' ), |
| 32 | 39 | 'description' => __( 'Decide how your URLs will look like.', 'polylang' ), |
| 33 | - 'configure' => true, | |
| 34 | 40 | ) |
| 35 | 41 | ); |
| 36 | 42 | |
| 37 | - $this->links_model = &$polylang->links_model; | |
| 38 | 43 | $this->page_on_front = &$polylang->static_pages->page_on_front; |
| 39 | 44 | } |
| 40 | 45 | |
| 41 | 46 | /** |
| @@ -41,21 +46,32 @@ | ||
| 41 | 46 | /** |
| 42 | 47 | * Displays the fieldset to choose how the language is set |
| 43 | 48 | * |
| 44 | 49 | * @since 1.8 |
| 50 | + * | |
| 51 | + * @return void | |
| 45 | 52 | */ |
| 46 | 53 | protected function force_lang() { |
| 47 | - ?> | |
| 48 | - <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> | |
| 49 | 67 | <?php |
| 50 | - printf( | |
| 51 | - '<input name="force_lang" type="radio" value="0" %s /> %s', | |
| 52 | - checked( $this->options['force_lang'], 0, false ), | |
| 53 | - esc_html__( 'The language is set from content', 'polylang' ) | |
| 54 | - ); | |
| 68 | + } else { | |
| 55 | 69 | ?> |
| 56 | - </label> | |
| 57 | - <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 | + ?> | |
| 58 | 74 | <label> |
| 59 | 75 | <?php |
| 60 | 76 | printf( |
| 61 | 77 | '<input name="force_lang" type="radio" value="1" %s/> %s', |
| @@ -88,9 +104,9 @@ | ||
| 88 | 104 | </label> |
| 89 | 105 | <table id="pll-domains-table" class="form-table" <?php echo 3 == $this->options['force_lang'] ? '' : 'style="display: none;"'; ?>> |
| 90 | 106 | <?php |
| 91 | 107 | foreach ( $this->model->get_languages_list() as $lg ) { |
| 92 | - $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 : '' ); | |
| 93 | 109 | printf( |
| 94 | 110 | '<tr><td><label for="pll-domain[%1$s]">%2$s</label></td>' . |
| 95 | 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>', |
| 96 | 112 | esc_attr( $lg->slug ), |
| @@ -106,8 +122,10 @@ | ||
| 106 | 122 | /** |
| 107 | 123 | * Displays the fieldset to choose to hide the default language information in url |
| 108 | 124 | * |
| 109 | 125 | * @since 1.8 |
| 126 | + * | |
| 127 | + * @return void | |
| 110 | 128 | */ |
| 111 | 129 | protected function hide_default() { |
| 112 | 130 | ?> |
| 113 | 131 | <label> |
| @@ -113,9 +131,9 @@ | ||
| 113 | 131 | <label> |
| 114 | 132 | <?php |
| 115 | 133 | printf( |
| 116 | 134 | '<input name="hide_default" type="checkbox" value="1" %s /> %s', |
| 117 | - checked( $this->options['hide_default'], 1, false ), | |
| 135 | + checked( $this->options['hide_default'], true, false ), | |
| 118 | 136 | esc_html__( 'Hide URL language information for default language', 'polylang' ) |
| 119 | 137 | ); |
| 120 | 138 | ?> |
| 121 | 139 | </label> |
| @@ -125,8 +143,10 @@ | ||
| 125 | 143 | /** |
| 126 | 144 | * Displays the fieldset to choose to hide /language/ in url |
| 127 | 145 | * |
| 128 | 146 | * @since 1.8 |
| 147 | + * | |
| 148 | + * @return void | |
| 129 | 149 | */ |
| 130 | 150 | protected function rewrite() { |
| 131 | 151 | ?> |
| 132 | 152 | <label> |
| @@ -133,10 +153,14 @@ | ||
| 133 | 153 | <?php |
| 134 | 154 | printf( |
| 135 | 155 | '<input name="rewrite" type="radio" value="1" %s %s/> %s', |
| 136 | 156 | disabled( $this->links_model->using_permalinks, false, false ), |
| 137 | - checked( $this->options['rewrite'], 1, false ), | |
| 138 | - 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 | + ) | |
| 139 | 163 | ); |
| 140 | 164 | ?> |
| 141 | 165 | </label> |
| 142 | 166 | <p class="description"><?php echo esc_html__( 'Example:', 'polylang' ) . ' <code>' . esc_html( home_url( 'en/' ) ) . '</code>'; ?></p> |
| @@ -144,10 +168,14 @@ | ||
| 144 | 168 | <?php |
| 145 | 169 | printf( |
| 146 | 170 | '<input name="rewrite" type="radio" value="0" %s %s/> %s', |
| 147 | 171 | disabled( $this->links_model->using_permalinks, false, false ), |
| 148 | - checked( $this->options['rewrite'], 0, false ), | |
| 149 | - 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 | + ) | |
| 150 | 178 | ); |
| 151 | 179 | ?> |
| 152 | 180 | </label> |
| 153 | 181 | <p class="description"><?php echo esc_html__( 'Example:', 'polylang' ) . ' <code>' . esc_html( home_url( 'language/en/' ) ) . '</code>'; ?></p> |
| @@ -157,8 +185,10 @@ | ||
| 157 | 185 | /** |
| 158 | 186 | * Displays the fieldset to choose to redirect the home page to language page |
| 159 | 187 | * |
| 160 | 188 | * @since 1.8 |
| 189 | + * | |
| 190 | + * @return void | |
| 161 | 191 | */ |
| 162 | 192 | protected function redirect_lang() { |
| 163 | 193 | ?> |
| 164 | 194 | <label> |
| @@ -164,10 +194,10 @@ | ||
| 164 | 194 | <label> |
| 165 | 195 | <?php |
| 166 | 196 | printf( |
| 167 | 197 | '<input name="redirect_lang" type="checkbox" value="1" %s/> %s', |
| 168 | - checked( $this->options['redirect_lang'], 1, false ), | |
| 169 | - 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' ) | |
| 170 | 200 | ); |
| 171 | 201 | ?> |
| 172 | 202 | </label> |
| 173 | 203 | <p class="description"> |
| @@ -173,9 +203,10 @@ | ||
| 173 | 203 | <p class="description"> |
| 174 | 204 | <?php |
| 175 | 205 | // That's nice to display the right home urls but don't forget that the page on front may have no language yet |
| 176 | 206 | $lang = $this->model->post->get_language( $this->page_on_front ); |
| 177 | - $lang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] ); | |
| 207 | + /** @var PLL_Language $lang */ | |
| 208 | + $lang = $lang ?: $this->model->get_default_language(); | |
| 178 | 209 | printf( |
| 179 | 210 | /* translators: %1$s example url when the option is active. %2$s example url when the option is not active */ |
| 180 | 211 | esc_html__( 'Example: %1$s instead of %2$s', 'polylang' ), |
| 181 | 212 | '<code>' . esc_html( $this->links_model->home_url( $lang ) ) . '</code>', |
| @@ -189,8 +220,10 @@ | ||
| 189 | 220 | /** |
| 190 | 221 | * Displays the settings |
| 191 | 222 | * |
| 192 | 223 | * @since 1.8 |
| 224 | + * | |
| 225 | + * @return void | |
| 193 | 226 | */ |
| 194 | 227 | public function form() { |
| 195 | 228 | ?> |
| 196 | 229 | <div class="pll-settings-url-col"> |
| @@ -224,91 +257,23 @@ | ||
| 224 | 257 | <?php |
| 225 | 258 | } |
| 226 | 259 | |
| 227 | 260 | /** |
| 228 | - * Sanitizes the settings before saving | |
| 261 | + * Prepares the received data before saving. | |
| 229 | 262 | * |
| 230 | - * @since 1.8 | |
| 263 | + * @since 3.7 | |
| 231 | 264 | * |
| 232 | - * @param array $options | |
| 265 | + * @param array $options Raw values to save. | |
| 266 | + * @return array | |
| 233 | 267 | */ |
| 234 | - protected function update( $options ) { | |
| 235 | - $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 | + ); | |
| 236 | 276 | |
| 237 | - foreach ( array( 'force_lang', 'rewrite' ) as $key ) { | |
| 238 | - $newoptions[ $key ] = isset( $options[ $key ] ) ? (int) $options[ $key ] : 0; | |
| 239 | - } | |
| 240 | - | |
| 241 | - if ( 3 == $options['force_lang'] && isset( $options['domains'] ) && is_array( $options['domains'] ) ) { | |
| 242 | - foreach ( $options['domains'] as $key => $domain ) { | |
| 243 | - if ( empty( $domain ) ) { | |
| 244 | - $lang = $this->model->get_language( $key ); | |
| 245 | - add_settings_error( | |
| 246 | - 'general', | |
| 247 | - 'pll_invalid_domain', | |
| 248 | - esc_html( | |
| 249 | - sprintf( | |
| 250 | - /* translators: %s is a native language name */ | |
| 251 | - __( 'Please enter a valid URL for %s.', 'polylang' ), | |
| 252 | - $lang->name | |
| 253 | - ) | |
| 254 | - ) | |
| 255 | - ); | |
| 256 | - } | |
| 257 | - else { | |
| 258 | - $newoptions['domains'][ $key ] = esc_url_raw( trim( $domain ) ); | |
| 259 | - } | |
| 260 | - } | |
| 261 | - } | |
| 262 | - | |
| 263 | - foreach ( array( 'hide_default', 'redirect_lang' ) as $key ) { | |
| 264 | - $newoptions[ $key ] = isset( $options[ $key ] ) ? 1 : 0; | |
| 265 | - } | |
| 266 | - | |
| 267 | - if ( 3 == $options['force_lang'] ) { | |
| 268 | - if ( ! class_exists( 'PLL_Xdata_Domain', true ) ) { | |
| 269 | - $newoptions['browser'] = 0; | |
| 270 | - } | |
| 271 | - $newoptions['hide_default'] = 0; | |
| 272 | - } | |
| 273 | - | |
| 274 | - // Check if domains exist | |
| 275 | - if ( $newoptions['force_lang'] > 1 ) { | |
| 276 | - $this->check_domains( $newoptions ); | |
| 277 | - } | |
| 278 | - | |
| 279 | - return $newoptions; // Take care to return only validated options | |
| 280 | - } | |
| 281 | - | |
| 282 | - /** | |
| 283 | - * Check if subdomains or domains are accessible | |
| 284 | - * | |
| 285 | - * @since 1.8 | |
| 286 | - * | |
| 287 | - * @param array $options new set of options to test | |
| 288 | - */ | |
| 289 | - protected function check_domains( $options ) { | |
| 290 | - $options = array_merge( $this->options, $options ); | |
| 291 | - $model = new PLL_Model( $options ); | |
| 292 | - $links_model = $model->get_links_model(); | |
| 293 | - foreach ( $this->model->get_languages_list() as $lang ) { | |
| 294 | - $url = add_query_arg( 'deactivate-polylang', 1, $links_model->home_url( $lang ) ); | |
| 295 | - // Don't redefine vip_safe_wp_remote_get() as it has not the same signature as wp_remote_get() | |
| 296 | - $response = function_exists( 'vip_safe_wp_remote_get' ) ? vip_safe_wp_remote_get( esc_url_raw( $url ) ) : wp_remote_get( esc_url_raw( $url ) ); | |
| 297 | - $response_code = wp_remote_retrieve_response_code( $response ); | |
| 298 | - | |
| 299 | - if ( 200 != $response_code ) { | |
| 300 | - add_settings_error( | |
| 301 | - 'general', | |
| 302 | - 'pll_invalid_domain', | |
| 303 | - esc_html( | |
| 304 | - sprintf( | |
| 305 | - /* translators: %s is an url */ | |
| 306 | - __( 'Polylang was unable to access the %s URL. Please check that the URL is valid.', 'polylang' ), | |
| 307 | - $url | |
| 308 | - ) | |
| 309 | - ) | |
| 310 | - ); | |
| 311 | - } | |
| 312 | - } | |
| 277 | + return array_intersect_key( array_merge( $defaults, $options ), $defaults ); // Take care to return only validated options | |
| 313 | 278 | } |
| 314 | 279 | } |