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

304 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 checked( $this->options['force_lang'], 0, false ),
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 will not be modified.', 'polylang' ); ?></p>
49 <label>
50 <?php
51 printf(
52 '<input name="force_lang" type="radio" value="1" %s/> %s',
53 checked( $this->options['force_lang'], 1, false ),
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 disabled( $this->links_model->using_permalinks, false, false ),
64 checked( $this->options['force_lang'], 2, false ),
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 disabled( $this->links_model->using_permalinks, false, false ),
75 checked( $this->options['force_lang'], 3, false ),
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 $url = isset( $this->options['domains'][ $lg->slug ] ) ? $this->options['domains'][ $lg->slug ] : ( $lg->slug == $this->options['default_lang'] ? $this->links_model->home : '' );
84 printf(
85 '<tr><td><label for="pll-domain[%1$s]">%2$s</label></td>' .
86 '<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>',
87 esc_attr( $lg->slug ),
88 esc_attr( $lg->name ),
89 esc_url( $url )
90 );
91 }
92 ?>
93 </table>
94 <?php
95 }
96
97 /**
98 * Displays the fieldset to choose to hide the default language information in url
99 *
100 * @since 1.8
101 */
102 protected function hide_default() {
103 ?>
104 <label>
105 <?php
106 printf(
107 '<input name="hide_default" type="checkbox" value="1" %s /> %s',
108 checked( $this->options['hide_default'], 1, false ),
109 esc_html__( 'Hide URL language information for default language', 'polylang' )
110 );
111 ?>
112 </label>
113 <?php
114 }
115
116 /**
117 * Displays the fieldset to choose to hide /language/ in url
118 *
119 * @since 1.8
120 */
121 protected function rewrite() {
122 ?>
123 <label>
124 <?php
125 printf(
126 '<input name="rewrite" type="radio" value="1" %s %s/> %s',
127 disabled( $this->links_model->using_permalinks, false, false ),
128 checked( $this->options['rewrite'], 1, false ),
129 esc_html__( 'Remove /language/ in pretty permalinks', 'polylang' )
130 );
131 ?>
132 </label>
133 <p class="description"><?php echo esc_html__( 'Example:', 'polylang' ) . ' <code>' . esc_html( home_url( 'en/' ) ) . '</code>'; ?></p>
134 <label>
135 <?php
136 printf(
137 '<input name="rewrite" type="radio" value="0" %s %s/> %s',
138 disabled( $this->links_model->using_permalinks, false, false ),
139 checked( $this->options['rewrite'], 0, false ),
140 esc_html__( 'Keep /language/ in pretty permalinks', 'polylang' )
141 );
142 ?>
143 </label>
144 <p class="description"><?php echo esc_html__( 'Example:', 'polylang' ) . ' <code>' . esc_html( home_url( 'language/en/' ) ) . '</code>'; ?></p>
145 <?php
146 }
147
148 /**
149 * Displays the fieldset to choose to redirect the home page to language page
150 *
151 * @since 1.8
152 */
153 protected function redirect_lang() {
154 ?>
155 <label>
156 <?php
157 printf(
158 '<input name="redirect_lang" type="checkbox" value="1" %s/> %s',
159 checked( $this->options['redirect_lang'], 1, false ),
160 esc_html__( 'The front page url contains the language code instead of the page name or page id', 'polylang' )
161 );
162 ?>
163 </label>
164 <p class="description">
165 <?php
166 // That's nice to display the right home urls but don't forget that the page on front may have no language yet
167 $lang = $this->model->post->get_language( $this->page_on_front );
168 $lang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] );
169 printf(
170 /* translators: %1$s example url when the option is active. %2$s example url when the option is not active */
171 esc_html__( 'Example: %1$s instead of %2$s', 'polylang' ),
172 '<code>' . esc_html( $this->links_model->home_url( $lang ) ) . '</code>',
173 '<code>' . esc_html( _get_page_link( $this->page_on_front ) ) . '</code>'
174 );
175 ?>
176 </p>
177 <?php
178 }
179
180 /**
181 * Displays the settings
182 *
183 * @since 1.8
184 */
185 public function form() {
186 ?>
187 <div class="pll-settings-url-col">
188 <fieldset class="pll-col-left pll-url" id="pll-force-lang">
189 <?php $this->force_lang(); ?>
190 </fieldset>
191 </div>
192
193 <div class="pll-settings-url-col">
194 <fieldset class="pll-col-right pll-url" id="pll-hide-default" <?php echo 3 > $this->options['force_lang'] ? '' : 'style="display: none;"'; ?>>
195 <?php $this->hide_default(); ?>
196 </fieldset>
197 <?php
198 if ( $this->links_model->using_permalinks ) {
199 ?>
200 <fieldset class="pll-col-right pll-url" id="pll-rewrite" <?php echo 2 > $this->options['force_lang'] ? '' : 'style="display: none;"'; ?>>
201 <?php $this->rewrite(); ?>
202 </fieldset>
203 <?php
204 }
205
206 if ( $this->page_on_front ) {
207 ?>
208 <fieldset class="pll-col-right pll-url" id="pll-redirect-lang" <?php echo 2 > $this->options['force_lang'] ? '' : 'style="display: none;"'; ?>>
209 <?php $this->redirect_lang(); ?>
210 </fieldset>
211 <?php
212 }
213 ?>
214 </div>
215 <?php
216 }
217
218 /**
219 * Sanitizes the settings before saving
220 *
221 * @since 1.8
222 *
223 * @param array $options
224 */
225 protected function update( $options ) {
226 foreach ( array( 'force_lang', 'rewrite' ) as $key ) {
227 $newoptions[ $key ] = isset( $options[ $key ] ) ? (int) $options[ $key ] : 0;
228 }
229
230 if ( 3 == $options['force_lang'] && isset( $options['domains'] ) && is_array( $options['domains'] ) ) {
231 foreach ( $options['domains'] as $key => $domain ) {
232 if ( empty( $domain ) ) {
233 $lang = $this->model->get_language( $key );
234 add_settings_error(
235 'general',
236 'pll_invalid_domain',
237 esc_html(
238 sprintf(
239 /* translators: %s is a native language name */
240 __( 'Please enter a valid URL for %s.', 'polylang' ),
241 $lang->name
242 )
243 )
244 );
245 }
246 else {
247 $newoptions['domains'][ $key ] = esc_url_raw( trim( $domain ) );
248 }
249 }
250 }
251
252 foreach ( array( 'hide_default', 'redirect_lang' ) as $key ) {
253 $newoptions[ $key ] = isset( $options[ $key ] ) ? 1 : 0;
254 }
255
256 if ( 3 == $options['force_lang'] ) {
257 if ( ! class_exists( 'PLL_Xdata_Domain', true ) ) {
258 $newoptions['browser'] = 0;
259 }
260 $newoptions['hide_default'] = 0;
261 }
262
263 // Check if domains exist
264 if ( $newoptions['force_lang'] > 1 ) {
265 $this->check_domains( $newoptions );
266 }
267
268 return $newoptions; // Take care to return only validated options
269 }
270
271 /**
272 * Check if subdomains or domains are accessible
273 *
274 * @since 1.8
275 *
276 * @param array $options new set of options to test
277 */
278 protected function check_domains( $options ) {
279 $options = array_merge( $this->options, $options );
280 $model = new PLL_Model( $options );
281 $links_model = $model->get_links_model();
282 foreach ( $this->model->get_languages_list() as $lang ) {
283 $url = add_query_arg( 'deactivate-polylang', 1, $links_model->home_url( $lang ) );
284 // Don't redefine vip_safe_wp_remote_get() as it has not the same signature as wp_remote_get()
285 $response = function_exists( 'vip_safe_wp_remote_get' ) ? vip_safe_wp_remote_get( esc_url_raw( $url ) ) : wp_remote_get( esc_url_raw( $url ) );
286 $response_code = wp_remote_retrieve_response_code( $response );
287
288 if ( 200 != $response_code ) {
289 add_settings_error(
290 'general',
291 'pll_invalid_domain',
292 esc_html(
293 sprintf(
294 /* translators: %s is an url */
295 __( 'Polylang was unable to access the %s URL. Please check that the URL is valid.', 'polylang' ),
296 $url
297 )
298 )
299 );
300 }
301 }
302 }
303 }
304