class-wpml-rest-extend-args.php
2.53 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
<?php
/**
* @author OnTheGo Systems
*/
class WPML_REST_Extend_Args implements IWPML_Action {
const REST_LANGUAGE_ARGUMENT = 'wpml_language';
/** @var \SitePress $sitepress */
private $sitepress;
/** @var string $current_language_backup */
private $current_language_backup;
public function __construct( SitePress $sitepress ) {
$this->sitepress = $sitepress;
}
function add_hooks() {
add_filter( 'rest_endpoints', array( $this, 'rest_endpoints' ) );
add_filter( 'rest_request_before_callbacks', array( $this, 'rest_request_before_callbacks' ), 10, 3 );
add_filter( 'rest_request_after_callbacks', array( $this, 'rest_request_after_callbacks' ) );
}
/**
* Adds the `wpml_language` argument (optional) to all REST calls with arguments.
*
* @param array $endpoints
*
* @return array
*/
public function rest_endpoints( array $endpoints ) {
$valid_language_codes = $this->get_active_language_codes();
foreach ( $endpoints as $route => &$endpoint ) {
foreach ( $endpoint as $key => &$data ) {
if ( is_numeric( $key ) ) {
$data['args'][ self::REST_LANGUAGE_ARGUMENT ] = array(
'type' => 'string',
'description' => "WPML's language code",
'required' => false,
'enum' => $valid_language_codes,
);
}
}
}
return $endpoints;
}
/**
* If `wpml_language` is provided, backups the current language, then switch to the provided one.
*
* @param \WP_REST_Response|array|mixed $response
* @param \WP_REST_Server|array|mixed $rest_server
* @param \WP_REST_Request $request
*
* @return mixed
*/
public function rest_request_before_callbacks( $response, $rest_server, $request ) {
$this->current_language_backup = null;
$current_language = $this->sitepress->get_current_language();
$rest_language = $request->get_param( self::REST_LANGUAGE_ARGUMENT );
if ( $rest_language && $rest_language !== $current_language ) {
$this->current_language_backup = $current_language;
$this->sitepress->switch_lang( $rest_language );
}
return $response;
}
/**
* Restore the backup language, if set.
*
* @param \WP_REST_Response|array|mixed $response
*
* @return mixed
*/
public function rest_request_after_callbacks( $response ) {
if ( $this->current_language_backup ) {
$this->sitepress->switch_lang( $this->current_language_backup );
}
return $response;
}
/**
* @return array
*/
private function get_active_language_codes() {
return array_keys( $this->sitepress->get_active_languages() );
}
}