| 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 PLL_Settings $polylang The Polylang object. |
| 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 |
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> |
| 67 |
<?php |
| 68 |
} else { |
| 69 |
?> |
| 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 |
?> |
| 74 |
<label> |
| 75 |
<?php |
| 76 |
printf( |
| 77 |
'<input name="force_lang" type="radio" value="1" %s/> %s', |
| 78 |
checked( $this->options['force_lang'], 1, false ), |
| 79 |
( $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' ) ) |
| 80 |
); |
| 81 |
?> |
| 82 |
</label> |
| 83 |
<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> |
| 84 |
<label> |
| 85 |
<?php |
| 86 |
printf( |
| 87 |
'<input name="force_lang" type="radio" value="2" %s %s/> %s', |
| 88 |
disabled( $this->links_model->using_permalinks, false, false ), |
| 89 |
checked( $this->options['force_lang'], 2, false ), |
| 90 |
esc_html__( 'The language is set from the subdomain name in pretty permalinks', 'polylang' ) |
| 91 |
); |
| 92 |
?> |
| 93 |
</label> |
| 94 |
<p class="description"><?php echo esc_html__( 'Example:', 'polylang' ) . ' <code>' . esc_html( str_replace( array( '://', 'www.' ), array( '://en.', '' ), home_url( 'my-post/' ) ) ) . '</code>'; ?></p> |
| 95 |
<label> |
| 96 |
<?php |
| 97 |
printf( |
| 98 |
'<input name="force_lang" type="radio" value="3" %s %s/> %s', |
| 99 |
disabled( $this->links_model->using_permalinks, false, false ), |
| 100 |
checked( $this->options['force_lang'], 3, false ), |
| 101 |
esc_html__( 'The language is set from different domains', 'polylang' ) |
| 102 |
); |
| 103 |
?> |
| 104 |
</label> |
| 105 |
<table id="pll-domains-table" class="form-table" <?php echo 3 == $this->options['force_lang'] ? '' : 'style="display: none;"'; ?>> |
| 106 |
<?php |
| 107 |
foreach ( $this->model->get_languages_list() as $lg ) { |
| 108 |
$url = $this->options['domains'][ $lg->slug ] ?? ( $lg->is_default ? $this->links_model->home : '' ); |
| 109 |
printf( |
| 110 |
'<tr><td><label for="pll-domain[%1$s]">%2$s</label></td>' . |
| 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>', |
| 112 |
esc_attr( $lg->slug ), |
| 113 |
esc_attr( $lg->name ), |
| 114 |
esc_url( $url ) |
| 115 |
); |
| 116 |
} |
| 117 |
?> |
| 118 |
</table> |
| 119 |
<?php |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Displays the fieldset to choose to hide the default language information in url |
| 124 |
* |
| 125 |
* @since 1.8 |
| 126 |
* |
| 127 |
* @return void |
| 128 |
*/ |
| 129 |
protected function hide_default() { |
| 130 |
?> |
| 131 |
<label> |
| 132 |
<?php |
| 133 |
printf( |
| 134 |
'<input name="hide_default" type="checkbox" value="1" %s /> %s', |
| 135 |
checked( $this->options['hide_default'], true, false ), |
| 136 |
esc_html__( 'Hide URL language information for default language', 'polylang' ) |
| 137 |
); |
| 138 |
?> |
| 139 |
</label> |
| 140 |
<?php |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Displays the fieldset to choose to hide /language/ in url |
| 145 |
* |
| 146 |
* @since 1.8 |
| 147 |
* |
| 148 |
* @return void |
| 149 |
*/ |
| 150 |
protected function rewrite() { |
| 151 |
?> |
| 152 |
<label> |
| 153 |
<?php |
| 154 |
printf( |
| 155 |
'<input name="rewrite" type="radio" value="1" %s %s/> %s', |
| 156 |
disabled( $this->links_model->using_permalinks, false, false ), |
| 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 |
) |
| 163 |
); |
| 164 |
?> |
| 165 |
</label> |
| 166 |
<p class="description"><?php echo esc_html__( 'Example:', 'polylang' ) . ' <code>' . esc_html( home_url( 'en/' ) ) . '</code>'; ?></p> |
| 167 |
<label> |
| 168 |
<?php |
| 169 |
printf( |
| 170 |
'<input name="rewrite" type="radio" value="0" %s %s/> %s', |
| 171 |
disabled( $this->links_model->using_permalinks, false, false ), |
| 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 |
) |
| 178 |
); |
| 179 |
?> |
| 180 |
</label> |
| 181 |
<p class="description"><?php echo esc_html__( 'Example:', 'polylang' ) . ' <code>' . esc_html( home_url( 'language/en/' ) ) . '</code>'; ?></p> |
| 182 |
<?php |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Displays the fieldset to choose to redirect the home page to language page |
| 187 |
* |
| 188 |
* @since 1.8 |
| 189 |
* |
| 190 |
* @return void |
| 191 |
*/ |
| 192 |
protected function redirect_lang() { |
| 193 |
?> |
| 194 |
<label> |
| 195 |
<?php |
| 196 |
printf( |
| 197 |
'<input name="redirect_lang" type="checkbox" value="1" %s/> %s', |
| 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' ) |
| 200 |
); |
| 201 |
?> |
| 202 |
</label> |
| 203 |
<p class="description"> |
| 204 |
<?php |
| 205 |
// That's nice to display the right home urls but don't forget that the page on front may have no language yet |
| 206 |
$lang = $this->model->post->get_language( $this->page_on_front ); |
| 207 |
/** @var PLL_Language $lang */ |
| 208 |
$lang = $lang ?: $this->model->get_default_language(); |
| 209 |
printf( |
| 210 |
/* translators: %1$s example url when the option is active. %2$s example url when the option is not active */ |
| 211 |
esc_html__( 'Example: %1$s instead of %2$s', 'polylang' ), |
| 212 |
'<code>' . esc_html( $this->links_model->home_url( $lang ) ) . '</code>', |
| 213 |
'<code>' . esc_html( _get_page_link( $this->page_on_front ) ) . '</code>' |
| 214 |
); |
| 215 |
?> |
| 216 |
</p> |
| 217 |
<?php |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Displays the settings |
| 222 |
* |
| 223 |
* @since 1.8 |
| 224 |
* |
| 225 |
* @return void |
| 226 |
*/ |
| 227 |
public function form() { |
| 228 |
?> |
| 229 |
<div class="pll-settings-url-col"> |
| 230 |
<fieldset class="pll-col-left pll-url" id="pll-force-lang"> |
| 231 |
<?php $this->force_lang(); ?> |
| 232 |
</fieldset> |
| 233 |
</div> |
| 234 |
|
| 235 |
<div class="pll-settings-url-col"> |
| 236 |
<fieldset class="pll-col-right pll-url" id="pll-hide-default" <?php echo 3 > $this->options['force_lang'] ? '' : 'style="display: none;"'; ?>> |
| 237 |
<?php $this->hide_default(); ?> |
| 238 |
</fieldset> |
| 239 |
<?php |
| 240 |
if ( $this->links_model->using_permalinks ) { |
| 241 |
?> |
| 242 |
<fieldset class="pll-col-right pll-url" id="pll-rewrite" <?php echo 2 > $this->options['force_lang'] ? '' : 'style="display: none;"'; ?>> |
| 243 |
<?php $this->rewrite(); ?> |
| 244 |
</fieldset> |
| 245 |
<?php |
| 246 |
} |
| 247 |
|
| 248 |
if ( $this->page_on_front ) { |
| 249 |
?> |
| 250 |
<fieldset class="pll-col-right pll-url" id="pll-redirect-lang" <?php echo 2 > $this->options['force_lang'] ? '' : 'style="display: none;"'; ?>> |
| 251 |
<?php $this->redirect_lang(); ?> |
| 252 |
</fieldset> |
| 253 |
<?php |
| 254 |
} |
| 255 |
?> |
| 256 |
</div> |
| 257 |
<?php |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Prepares the received data before saving. |
| 262 |
* |
| 263 |
* @since 3.7 |
| 264 |
* |
| 265 |
* @param array $options Raw values to save. |
| 266 |
* @return array |
| 267 |
*/ |
| 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 |
); |
| 276 |
|
| 277 |
return array_intersect_key( array_merge( $defaults, $options ), $defaults ); // Take care to return only validated options |
| 278 |
} |
| 279 |
} |
| 280 |
|