PluginProbe
Polylang / 2.2.2
Polylang v2.2.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.2.2, at settings/settings-url.php

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