class-wpml-xdomain-data-parser.php
3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
use WPML\API\Sanitize;
class WPML_XDomain_Data_Parser {
const SCRIPT_HANDLER = 'wpml-xdomain-data';
/**
* @var array $settings
*/
private $settings;
private $encryptor;
/**
* WPML_XDomain_Data_Parser constructor.
*
* @param array<string,mixed> $settings
* @param \WPML_Data_Encryptor $encryptor
*/
public function __construct( &$settings, $encryptor ) {
$this->settings = &$settings;
$this->encryptor = $encryptor;
}
public function init_hooks() {
if ( ! isset( $this->settings['xdomain_data'] ) || $this->settings['xdomain_data'] != WPML_XDOMAIN_DATA_OFF ) {
add_action( 'init', array( $this, 'init' ) );
add_filter( 'wpml_get_cross_domain_language_data', array( $this, 'get_xdomain_data' ) );
add_filter( 'wpml_get_cross_domain_language_data', array( $this, 'get_xdomain_data' ) );
}
}
public function init() {
add_action( 'wp_ajax_switching_language', array( $this, 'send_xdomain_language_data' ) );
add_action( 'wp_ajax_nopriv_switching_language', array( $this, 'send_xdomain_language_data' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts_action' ), 100 );
}
public function register_scripts_action() {
if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
$ls_parameters = WPML_Language_Switcher::parameters();
$js_xdomain_data = array(
'css_selector' => $ls_parameters['css_prefix'] . 'item',
'ajax_url' => admin_url( 'admin-ajax.php' ),
'current_lang' => apply_filters( 'wpml_current_language', '' ),
'_nonce' => wp_create_nonce( 'wp_ajax_switching_language' ),
);
wp_enqueue_script( self::SCRIPT_HANDLER, ICL_PLUGIN_URL . '/res/js/xdomain-data.js', array(), ICL_SITEPRESS_VERSION );
wp_localize_script( self::SCRIPT_HANDLER, 'wpml_xdomain_data', $js_xdomain_data );
}
}
public function set_up_xdomain_language_data() {
$ret = array();
$data = apply_filters( 'WPML_cross_domain_language_data', array() );
$data = apply_filters( 'wpml_cross_domain_language_data', $data );
if ( ! empty( $data ) ) {
$encoded_data = json_encode( $data );
$encoded_data = $this->encryptor->encrypt( $encoded_data );
$base64_encoded_data = base64_encode( $encoded_data );
$ret['xdomain_data'] = urlencode( $base64_encoded_data );
$ret['method'] = WPML_XDOMAIN_DATA_POST == $this->settings['xdomain_data'] ? 'post' : 'get';
}
return $ret;
}
public function send_xdomain_language_data() {
$nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( $_POST['_nonce'] ) : '';
if ( ! wp_verify_nonce( $nonce, 'wp_ajax_switching_language' ) ) {
wp_send_json_error( esc_html__( 'Invalid request!', 'sitepress' ), 400 );
return;
}
$data = $this->set_up_xdomain_language_data();
wp_send_json_success( $data );
}
public function get_xdomain_data() {
$xdomain_data = array();
if ( isset( $_GET['xdomain_data'] ) || isset( $_POST['xdomain_data'] ) ) {
$xdomain_data_request = false;
if ( WPML_XDOMAIN_DATA_GET == $this->settings['xdomain_data'] ) {
$xdomain_data_request = Sanitize::stringProp( 'xdomain_data', $_GET );
} elseif ( WPML_XDOMAIN_DATA_POST == $this->settings['xdomain_data'] ) {
$xdomain_data_request = urldecode( (string) Sanitize::stringProp( 'xdomain_data', $_POST ) );
}
if ( $xdomain_data_request ) {
$data = base64_decode( $xdomain_data_request );
$data = $this->encryptor->decrypt( $data );
$xdomain_data = (array) json_decode( $data, true );
}
}
return $xdomain_data;
}
}