class-wpml-url-http-referer.php
2.33 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
<?php
class WPML_URL_HTTP_Referer {
private $rest;
public function __construct( WPML_Rest $rest ) {
$this->rest = $rest;
}
/**
* @param string $backup_url
*
* @return string
*/
public function get_url( $backup_url ) {
if ( ! isset( $_SERVER['HTTP_REFERER'] ) ) {
return $backup_url;
}
if ( WPML_Ajax::is_admin_ajax_request_called_from_frontend( $backup_url )
|| $this->is_rest_request_called_from_post_edit_page()
) {
return $_SERVER['HTTP_REFERER'];
}
return $backup_url;
}
/**
* @return bool|int
*/
public function get_trid() {
$referer_data = $request_uri_data = array();
if ( array_key_exists( 'HTTP_REFERER', $_SERVER ) ) {
$query = (string) wpml_parse_url( $_SERVER['HTTP_REFERER'], PHP_URL_QUERY );
parse_str( $query, $referer_data );
}
if ( array_key_exists( 'REQUEST_URI', $_SERVER ) ) {
$request_uri = (string) wpml_parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY );
parse_str( $request_uri, $request_uri_data );
}
/**
* trid from `HTTP_REFERER` should be return only if `REQUEST_URI` also has trid set.
*
* @link https://onthegosystems.myjetbrains.com/youtrack/issue/wpmltm-1351
*
* Or when it is a rest request called in the post edit page (Gutenberg)
* @link https://onthegosystems.myjetbrains.com/youtrack/issue/wpmlcore-5265
*/
return array_key_exists( 'trid', $referer_data ) && array_key_exists( 'trid', $request_uri_data )
|| ( array_key_exists( 'trid', $referer_data ) && $this->is_rest_request_called_from_post_edit_page() )
? (int) $referer_data['trid']
: false;
}
/**
* We need this in order to detect the language when adding
* translation from inside of a Gutenberg page while
* they don't provide a JS API which allows us to do it
*
* @link https://github.com/WordPress/gutenberg/issues/5958
* @link https://onthegosystems.myjetbrains.com/youtrack/issue/wpmlcore-5265
*
* @return bool
*/
public function is_rest_request_called_from_post_edit_page() {
return $this->rest->is_rest_request() && self::is_post_edit_page();
}
public static function is_post_edit_page() {
return isset( $_SERVER['HTTP_REFERER'] )
&& ( strpos( $_SERVER['HTTP_REFERER'], 'wp-admin/post.php' )
|| strpos( $_SERVER['HTTP_REFERER'], 'wp-admin/post-new.php' )
|| strpos( $_SERVER['HTTP_REFERER'], 'wp-admin/edit.php' ) );
}
}