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

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