PluginProbe
Polylang / 2.8.2
Polylang v2.8.2
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / settings / settings-url.php

settings-url.php in Polylang 2.8.2, at settings/settings-url.php

315 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Constructor
21 *
22 * @since 1.8
23 *
24 * @param object $polylang
25 */
26 public function __construct( &$polylang ) {
27 parent::__construct(
28 $polylang,
29 array(
30 'module' => 'url',
31 'title' => __( 'URL modifications', 'polylang' ),
32 'description' => __( 'Decide how your URLs will look like.', 'polylang' ),
33 'configure' => true,
34 )
35 );
36
37 $this->links_model = &$polylang->links_model;
38 $this->page_on_front = &$polylang->static_pages->page_on_front;
39 }
40
41 /**
42 * Displays the fieldset to choose how the language is set
43 *
44 * @since 1.8
45 */
46 protected function force_lang() {
47 ?>
48 <label>
49 <?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 );
55 ?>
56 </label>
57 <p class="description"><?php esc_html_e( 'Posts, pages, categories and tags URLs will not be modified.', 'polylang' ); ?></p>
58 <label>
59 <?php
60 printf(
61 '<input name="force_lang" type="radio" value="1" %s/> %s',
62 checked( $this->options['force_lang'], 1, false ),
63 ( $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' ) )
64 );
65 ?>
66 </label>
67 <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>
68 <label>
69 <?php
70 printf(
71 '<input name="force_lang" type="radio" value="2" %s %s/> %s',
72 disabled( $this->links_model->using_permalinks, false, false ),
73 checked( $this->options['force_lang'], 2, false ),
74 esc_html__( 'The language is set from the subdomain name in pretty permalinks', 'polylang' )
75 );
76 ?>
77 </label>
78 <p class="description"><?php echo esc_html__( 'Example:', 'polylang' ) . ' <code>' . esc_html( str_replace( array( '://', 'www.' ), array( '://en.', '' ), home_url( 'my-post/' ) ) ) . '</code>'; ?></p>
79 <label>
80 <?php
81 printf(
82 '<input name="force_lang" type="radio" value="3" %s %s/> %s',
83 disabled( $this->links_model->using_permalinks, false, false ),
84 checked( $this->options['force_lang'], 3, false ),
85 esc_html__( 'The language is set from different domains', 'polylang' )
86 );
87 ?>
88 </label>
89 <table id="pll-domains-table" class="form-table" <?php echo 3 == $this->options['force_lang'] ? '' : 'style="display: none;"'; ?>>
90 <?php
91 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 : '' );
93 printf(
94 '<tr><td><label for="pll-domain[%1$s]">%2$s</label></td>' .
95 '<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 esc_attr( $lg->slug ),
97 esc_attr( $lg->name ),
98 esc_url( $url )
99 );
100 }
101 ?>
102 </table>
103 <?php
104 }
105
106 /**
107 * Displays the fieldset to choose to hide the default language information in url
108 *
109 * @since 1.8
110 */
111 protected function hide_default() {
112 ?>
113 <label>
114 <?php
115 printf(
116 '<input name="hide_default" type="checkbox" value="1" %s /> %s',
117 checked( $this->options['hide_default'], 1, false ),
118 esc_html__( 'Hide URL language information for default language', 'polylang' )
119 );
120 ?>
121 </label>
122 <?php
123 }
124
125 /**
126 * Displays the fieldset to choose to hide /language/ in url
127 *
128 * @since 1.8
129 */
130 protected function rewrite() {
131 ?>
132 <label>
133 <?php
134 printf(
135 '<input name="rewrite" type="radio" value="1" %s %s/> %s',
136 disabled( $this->links_model->using_permalinks, false, false ),
137 checked( $this->options['rewrite'], 1, false ),
138 esc_html__( 'Remove /language/ in pretty permalinks', 'polylang' )
139 );
140 ?>
141 </label>
142 <p class="description"><?php echo esc_html__( 'Example:', 'polylang' ) . ' <code>' . esc_html( home_url( 'en/' ) ) . '</code>'; ?></p>
143 <label>
144 <?php
145 printf(
146 '<input name="rewrite" type="radio" value="0" %s %s/> %s',
147 disabled( $this->links_model->using_permalinks, false, false ),
148 checked( $this->options['rewrite'], 0, false ),
149 esc_html__( 'Keep /language/ in pretty permalinks', 'polylang' )
150 );
151 ?>
152 </label>
153 <p class="description"><?php echo esc_html__( 'Example:', 'polylang' ) . ' <code>' . esc_html( home_url( 'language/en/' ) ) . '</code>'; ?></p>
154 <?php
155 }
156
157 /**
158 * Displays the fieldset to choose to redirect the home page to language page
159 *
160 * @since 1.8
161 */
162 protected function redirect_lang() {
163 ?>
164 <label>
165 <?php
166 printf(
167 '<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' )
170 );
171 ?>
172 </label>
173 <p class="description">
174 <?php
175 // That's nice to display the right home urls but don't forget that the page on front may have no language yet
176 $lang = $this->model->post->get_language( $this->page_on_front );
177 $lang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] );
178 printf(
179 /* translators: %1$s example url when the option is active. %2$s example url when the option is not active */
180 esc_html__( 'Example: %1$s instead of %2$s', 'polylang' ),
181 '<code>' . esc_html( $this->links_model->home_url( $lang ) ) . '</code>',
182 '<code>' . esc_html( _get_page_link( $this->page_on_front ) ) . '</code>'
183 );
184 ?>
185 </p>
186 <?php
187 }
188
189 /**
190 * Displays the settings
191 *
192 * @since 1.8
193 */
194 public function form() {
195 ?>
196 <div class="pll-settings-url-col">
197 <fieldset class="pll-col-left pll-url" id="pll-force-lang">
198 <?php $this->force_lang(); ?>
199 </fieldset>
200 </div>
201
202 <div class="pll-settings-url-col">
203 <fieldset class="pll-col-right pll-url" id="pll-hide-default" <?php echo 3 > $this->options['force_lang'] ? '' : 'style="display: none;"'; ?>>
204 <?php $this->hide_default(); ?>
205 </fieldset>
206 <?php
207 if ( $this->links_model->using_permalinks ) {
208 ?>
209 <fieldset class="pll-col-right pll-url" id="pll-rewrite" <?php echo 2 > $this->options['force_lang'] ? '' : 'style="display: none;"'; ?>>
210 <?php $this->rewrite(); ?>
211 </fieldset>
212 <?php
213 }
214
215 if ( $this->page_on_front ) {
216 ?>
217 <fieldset class="pll-col-right pll-url" id="pll-redirect-lang" <?php echo 2 > $this->options['force_lang'] ? '' : 'style="display: none;"'; ?>>
218 <?php $this->redirect_lang(); ?>
219 </fieldset>
220 <?php
221 }
222 ?>
223 </div>
224 <?php
225 }
226
227 /**
228 * Sanitizes the settings before saving
229 *
230 * @since 1.8
231 *
232 * @param array $options
233 */
234 protected function update( $options ) {
235 $newoptions = array();
236
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 }
313 }
314 }
315