PLL_Settings_Url

A class to manage URL modifications settings.

Defined (1)

The class is defined in the following location(s).

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