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

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