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