| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Settings class for browser language preference detection |
| 5 |
* |
| 6 |
* @since 1.8 |
| 7 |
*/ |
| 8 |
class PLL_Settings_Browser extends PLL_Settings_Module { |
| 9 |
|
| 10 |
/** |
| 11 |
* Constructor |
| 12 |
* |
| 13 |
* @since 1.8 |
| 14 |
* |
| 15 |
* @param object $polylang polylang object |
| 16 |
*/ |
| 17 |
public function __construct( &$polylang ) { |
| 18 |
$this->options = &$polylang->options; |
| 19 |
parent::__construct( |
| 20 |
$polylang, |
| 21 |
array( |
| 22 |
'module' => 'browser', |
| 23 |
'title' => __( 'Detect browser language', 'polylang' ), |
| 24 |
'description' => __( 'When the front page is visited, set the language according to the browser preference', 'polylang' ), |
| 25 |
'active_option' => $this->is_available() ? 'browser' : false, |
| 26 |
) |
| 27 |
); |
| 28 |
|
| 29 |
if ( ! class_exists( 'PLL_Xdata_Domain', true ) ) { |
| 30 |
add_action( 'admin_print_footer_scripts', array( $this, 'print_js' ) ); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Tells if the option is available |
| 36 |
* |
| 37 |
* @since 2.0 |
| 38 |
* |
| 39 |
* @return bool |
| 40 |
*/ |
| 41 |
protected function is_available() { |
| 42 |
return ( 3 > $this->options['force_lang'] ) || class_exists( 'PLL_Xdata_Domain', true ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Tells if the module is active |
| 47 |
* |
| 48 |
* @since 1.8 |
| 49 |
* |
| 50 |
* @return bool |
| 51 |
*/ |
| 52 |
public function is_active() { |
| 53 |
return $this->is_available() ? parent::is_active() : false; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Displays the javascript to handle dynamically the change in url modifications |
| 58 |
* as the preferred browser language is not used when the language is set from different domains |
| 59 |
* |
| 60 |
* @since 1.8 |
| 61 |
*/ |
| 62 |
public function print_js() { |
| 63 |
wp_enqueue_script( 'jquery' ); |
| 64 |
|
| 65 |
if ( parent::is_active() && 3 > $this->options['force_lang'] ) { |
| 66 |
$func = 'removeClass( "inactive" ).addClass( "active" )'; |
| 67 |
$link = sprintf( '<span class="deactivate">%s</span>', $this->action_links['deactivate'] ); |
| 68 |
} |
| 69 |
else { |
| 70 |
$func = 'removeClass( "active" ).addClass( "inactive" )'; |
| 71 |
$link = sprintf( '<span class="activate">%s</span>', $this->action_links['activate'] ); |
| 72 |
} |
| 73 |
|
| 74 |
$deactivated = sprintf( '<span class="deactivated">%s</span>', $this->action_links['deactivated'] ); |
| 75 |
|
| 76 |
?> |
| 77 |
<script type='text/javascript'> |
| 78 |
//<![CDATA[ |
| 79 |
( function( $ ){ |
| 80 |
$( "input[name='force_lang']" ).change( function() { |
| 81 |
var value = $( this ).val(); |
| 82 |
if ( 3 > value ) { |
| 83 |
$( "#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 ?>' ); |
| 84 |
} |
| 85 |
else { |
| 86 |
$( "#pll-module-browser" ).removeClass( "active" ).addClass( "inactive" ).children( "td" ).children( ".row-actions" ).html( '<?php echo $deactivated; // phpcs:ignore WordPress.Security.EscapeOutput ?>' ); |
| 87 |
} |
| 88 |
} ); |
| 89 |
} )( jQuery ); |
| 90 |
// ]]> |
| 91 |
</script> |
| 92 |
<?php |
| 93 |
} |
| 94 |
} |
| 95 |
|