class-wpml-xmlrpc.php
4.4 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* @author OnTheGo Systems
*/
class WPML_XMLRPC extends WPML_SP_User {
private $xmlrpc_call_methods_for_save_post;
/**
* WPML_XMLRPC constructor.
*
* @param SitePress $sitepress
*/
public function __construct( SitePress $sitepress ) {
parent::__construct( $sitepress );
$this->xmlrpc_call_methods_for_save_post = array( 'wp.newPost', 'wp.editPost', 'wp.newPage', 'wp.editPage' );
}
public function init_hooks() {
add_action( 'xmlrpc_call_success_mw_newPost', array( $this, 'meta_weblog_xmlrpc_post_update_action' ), 10, 2 );
add_action( 'xmlrpc_call_success_mw_editPost', array( $this, 'meta_weblog_xmlrpc_post_update_action' ), 10, 2 );
add_action( 'xmlrpc_call', array( $this, 'xmlrpc_call' ) );
add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) );
}
function get_languages( $args ) {
list( $blog_id, $username, $password ) = $args;
if ( ! $this->sitepress->get_wp_api()->get_wp_xmlrpc_server()->login( $username, $password ) ) {
return $this->sitepress->get_wp_api()->get_wp_xmlrpc_server()->error;
}
if ( ! defined( 'WP_ADMIN' ) ) {
define( 'WP_ADMIN', true ); // hack - allow to force display language
}
return $this->sitepress->get_active_languages( true );
}
public function get_post_trid( $args ) {
list( $blog_id, $username, $password, $element_id ) = $args;
if ( ! $this->sitepress->get_wp_api()->get_wp_xmlrpc_server()->login( $username, $password ) ) {
return $this->sitepress->get_wp_api()->get_wp_xmlrpc_server()->error;
}
$post_element = new WPML_Post_Element( $element_id, $this->sitepress );
return $post_element->get_trid();
}
/**
* @param int $post_ID
* @param array $args
*
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
*/
public function meta_weblog_xmlrpc_post_update_action( $post_ID, $args ) {
$custom_fields = array();
if ( array_key_exists( 'custom_fields', $args[3] ) ) {
foreach ( $args[3]['custom_fields'] as $cf ) {
$custom_fields[ $cf['key'] ] = $cf['value'];
}
}
$post_language_code = $this->sitepress->get_default_language();
$trid = false;
if ( array_key_exists( '_wpml_language', $custom_fields ) ) {
$post_language_code = $custom_fields['_wpml_language'];
}
if ( array_key_exists( '_wpml_trid', $custom_fields ) ) {
$trid = $custom_fields['_wpml_trid'];
}
$post_type = 'post';
if ( array_key_exists( 'post_type', $args[3] ) ) {
$post_type = $args[3]['post_type'];
}
$this->set_post_language( $post_ID, $post_type, $post_language_code, $trid );
}
public function save_post_action( $pidd, $post ) {
$post_language_code = get_post_meta( $pidd, '_wpml_language', true );
$post_language_code = $post_language_code ? $post_language_code : $this->sitepress->get_default_language();
$trid = get_post_meta( $pidd, '_wpml_trid', true );
$trid = $trid ? $trid : false;
$this->set_post_language( $pidd, $post->post_type, $post_language_code, $trid );
}
/**
* @param int $post_ID
* @param string $post_type
* @param string $post_language_code
* @param int|bool $trid
*
* @throws \InvalidArgumentException
* @throws \UnexpectedValueException
*/
private function set_post_language( $post_ID, $post_type, $post_language_code, $trid = false ) {
if ( $post_language_code && $this->sitepress->is_translated_post_type( $post_type ) ) {
$wpml_translations = new WPML_Translations( $this->sitepress );
$post_element = new WPML_Post_Element( $post_ID, $this->sitepress );
if ( $post_language_code ) {
$wpml_translations->set_language_code( $post_element, $post_language_code );
}
if ( $trid ) {
$wpml_translations->set_trid( $post_element, $trid );
}
}
}
public function xmlrpc_call( $action ) {
if ( in_array( $action, $this->xmlrpc_call_methods_for_save_post, true ) ) {
add_action( 'save_post', array( $this, 'save_post_action' ), 10, 2 );
}
}
public function xmlrpc_methods( $methods ) {
/**
* Parameters:
* - int blog_id
* - string username
* - string password
* - int post_id
* Returns:
* - struct
* - int trid
*/
$methods['wpml.get_post_trid'] = array( $this, 'get_post_trid' );
/**
* Parameters:
* - int blog_id
* - string username
* - string password
* Returns:
* - struct
* - array active languages
*/
$methods['wpml.get_languages'] = array( $this, 'get_languages' );
return apply_filters( 'wpml_xmlrpc_methods', $methods );
}
}