PluginProbe
Polylang / 3.7
Polylang v3.7
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-browser.php

settings-browser.php in Polylang 3.7, at settings/settings-browser.php

108 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * Settings class for browser language preference detection
8 *
9 * @since 1.8
10 */
11 class PLL_Settings_Browser extends PLL_Settings_Module {
12 /**
13 * Stores the display order priority.
14 *
15 * @var int
16 */
17 public $priority = 20;
18
19 /**
20 * Constructor
21 *
22 * @since 1.8
23 *
24 * @param object $polylang polylang object
25 */
26 public function __construct( &$polylang ) {
27 // Needed for `$this->is_available()`, which is used before calling the parent's constructor.
28 $this->options = &$polylang->options;
29
30 parent::__construct(
31 $polylang,
32 array(
33 'module' => 'browser',
34 'title' => __( 'Detect browser language', 'polylang' ),
35 'description' => __( 'When the front page is visited, redirects to itself in the browser preferred language. As this doesn\'t work if it is cached, Polylang will attempt to disable the front page cache for known cache plugins.', 'polylang' ),
36 'active_option' => $this->is_available() ? 'browser' : 'none',
37 )
38 );
39
40 if ( ! class_exists( 'PLL_Xdata_Domain', true ) ) {
41 add_action( 'admin_print_footer_scripts', array( $this, 'print_js' ) );
42 }
43 }
44
45 /**
46 * Tells if the option is available
47 *
48 * @since 2.0
49 *
50 * @return bool
51 */
52 protected function is_available() {
53 return ( 3 > $this->options['force_lang'] ) || class_exists( 'PLL_Xdata_Domain', true );
54 }
55
56 /**
57 * Tells if the module is active
58 *
59 * @since 1.8
60 *
61 * @return bool
62 */
63 public function is_active() {
64 return $this->is_available() ? parent::is_active() : false;
65 }
66
67 /**
68 * Displays the javascript to handle dynamically the change in url modifications
69 * as the preferred browser language is not used when the language is set from different domains
70 *
71 * @since 1.8
72 *
73 * @return void
74 */
75 public function print_js() {
76 wp_enqueue_script( 'jquery' );
77
78 if ( parent::is_active() && 3 > $this->options['force_lang'] ) {
79 $func = 'removeClass( "inactive" ).addClass( "active" )';
80 $link = sprintf( '<span class="deactivate">%s</span>', $this->action_links['deactivate'] );
81 }
82 else {
83 $func = 'removeClass( "active" ).addClass( "inactive" )';
84 $link = sprintf( '<span class="activate">%s</span>', $this->action_links['activate'] );
85 }
86
87 $deactivated = sprintf( '<span class="deactivated">%s</span>', $this->action_links['deactivated'] );
88
89 ?>
90 <script>
91 jQuery(
92 function( $ ){
93 $( "input[name='force_lang']" ).on( 'change', function() {
94 var value = $( this ).val();
95 if ( 3 > value ) {
96 $( "#pll-module-browser" ).<?php echo $func; // phpcs:ignore WordPress.Security.EscapeOutput ?>.children( "td" ).children( ".row-actions" ).html( '<?php echo $link; // phpcs:ignore WordPress.Security.EscapeOutput ?>' );
97 }
98 else {
99 $( "#pll-module-browser" ).removeClass( "active" ).addClass( "inactive" ).children( "td" ).children( ".row-actions" ).html( '<?php echo $deactivated; // phpcs:ignore WordPress.Security.EscapeOutput ?>' );
100 }
101 } );
102 }
103 );
104 </script>
105 <?php
106 }
107 }
108