PluginProbe
Polylang / 2.4
Polylang v2.4
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.4, at settings/settings-url.php

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