wpml-admin-post-actions.class.php
8.07 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
use WPML\API\Sanitize;
/**
* Class WPML_Admin_Post_Actions
*
* @package wpml-core
* @subpackage post-translation
*/
class WPML_Admin_Post_Actions extends WPML_Post_Translation {
/**
* @depecated since 4.6.5 You should use constants from WPML\Media\Option
*/
const DUPLICATE_MEDIA_META_KEY = \WPML\Media\Option::DUPLICATE_MEDIA_KEY;
const DUPLICATE_FEATURED_META_KEY = \WPML\Media\Option::DUPLICATE_FEATURED_KEY;
const DUPLICATE_MEDIA_GLOBAL_KEY = 'duplicate_media';
const DUPLICATE_FEATURED_GLOBAL_KEY = 'duplicate_media';
private $http_referer;
public function init() {
parent::init ();
if ( $this->is_setup_complete() ) {
add_action ( 'delete_post', array( $this, 'delete_post_actions' ) );
add_action ( 'wp_trash_post', array( $this, 'trashed_post_actions' ) );
add_action ( 'untrashed_post', array( $this, 'untrashed_post_actions' ) );
}
}
/**
* @param int $post_id
* @param string $post_status
*
* @return null|int
*/
function get_save_post_trid( $post_id, $post_status ) {
$trid = $this->get_element_trid( $post_id );
if ( ! ( $this->is_inner_post_insertion() && $this->is_editing_different_post( $post_id ) ) ) {
$trid = $trid ? $trid : filter_var( isset( $_POST['icl_trid'] ) ? $_POST['icl_trid'] : '', FILTER_SANITIZE_NUMBER_INT );
$trid = $trid ? $trid : filter_var( isset( $_GET['trid'] ) ? $_GET['trid'] : '', FILTER_SANITIZE_NUMBER_INT );
$trid = $trid ? $trid : $this->get_trid_from_referer();
}
$trid = apply_filters( 'wpml_save_post_trid_value', $trid, $post_status );
return $trid;
}
/**
* @param int $post_id
* @param WP_Post $post
*/
public function save_post_actions( $post_id, $post ) {
global $sitepress;
$this->defer_term_counting();
if ( ! $post ) {
$post = get_post( $post_id );
}
// exceptions
$http_referer = $this->get_http_referer();
if ( ! $this->has_save_post_action( $post ) && ! $http_referer->is_rest_request_called_from_post_edit_page() ) {
return;
}
if ( WPML_WordPress_Actions::is_bulk_trash( $post_id ) ||
WPML_WordPress_Actions::is_bulk_untrash( $post_id ) ||
$this->has_invalid_language_details_on_heartbeat()
) {
return;
}
$default_language = $sitepress->get_default_language();
$post_vars = $this->get_post_vars( $post );
if ( isset( $post_vars['action'] ) && $post_vars['action'] === 'post-quickpress-publish' ) {
$language_code = $default_language;
} else {
if( isset( $post_vars['post_ID'] ) ){
$post_id = $post_vars['post_ID'];
}
$language_code = $this->get_save_post_lang( $post_id, $sitepress );
}
if ( $this->is_inline_action( $post_vars ) && ! ( $language_code = $this->get_element_lang_code(
$post_id
) )
) {
return;
}
if ( isset( $post_vars['icl_translation_of'] ) && is_numeric( $post_vars['icl_translation_of'] ) ) {
$translation_of_data_prepared = $this->wpdb->prepare(
"SELECT trid, language_code
FROM {$this->wpdb->prefix}icl_translations
WHERE element_id=%d
AND element_type=%s
LIMIT 1",
$post_vars['icl_translation_of'],
'post_' . $post->post_type
);
list( $trid, $source_language ) = $this->wpdb->get_row( $translation_of_data_prepared, 'ARRAY_N' );
}
if ( isset( $post_vars['icl_translation_of'] ) && $post_vars['icl_translation_of'] == 'none' ) {
$trid = null;
$source_language = $language_code;
} else {
$trid = isset( $trid ) && $trid ? $trid : $this->get_save_post_trid( $post_id, $post->post_status );
// after getting the right trid set the source language from it by referring to the root translation
// of this trid, in case no proper source language has been set yet
$source_language = isset( $source_language )
? $source_language : $this->get_save_post_source_lang( $trid, $language_code, $default_language );
}
if ( isset( $post_vars['icl_tn_note'] ) ) {
update_post_meta( $post_id, '_icl_translator_note', $post_vars['icl_tn_note'] );
}
$this->after_save_post( $trid, $post_vars, $language_code, $source_language );
if ( 'attachment' !== $post->post_type ) {
$this->save_media_options( $post_id, $source_language );
}
}
/**
* @param int $post_id
* @param string|null $source_language
*/
private function save_media_options( $post_id, $source_language ) {
if ( $this->has_post_media_options_metabox() ) {
$original_post_id = isset( $_POST['icl_translation_of'] )
? filter_var( $_POST['icl_translation_of'], FILTER_SANITIZE_NUMBER_INT ) : $post_id;
$duplicate_media = isset( $_POST['wpml_duplicate_media'] )
? filter_var( $_POST['wpml_duplicate_media'], FILTER_SANITIZE_NUMBER_INT ) : false;
$duplicate_featured = isset( $_POST['wpml_duplicate_featured'] )
? filter_var( $_POST['wpml_duplicate_featured'], FILTER_SANITIZE_NUMBER_INT ) : false;
\WPML\Media\Option::setDuplicateMediaForIndividualPost( $post_id, (bool) $duplicate_media );
\WPML\Media\Option::setDuplicateFeaturedForIndividualPost( $post_id, (bool) $duplicate_featured );
}
}
private function has_post_media_options_metabox() {
return array_key_exists( WPML_Meta_Boxes_Post_Edit_HTML::FLAG_HAS_MEDIA_OPTIONS, $_POST );
}
private function has_invalid_language_details_on_heartbeat() {
if ( ! WPML_WordPress_Actions::is_heartbeat() ) {
return false;
}
if ( isset( $_POST['data']['icl_post_language'], $_POST['data']['icl_trid'] ) ) {
$_POST['icl_post_language'] = Sanitize::string( $_POST['data']['icl_post_language'] );
$_POST['icl_trid'] = filter_var( $_POST['data']['icl_trid'], FILTER_SANITIZE_NUMBER_INT );
return false;
}
return true;
}
/**
* @param integer $post_id
* @param SitePress $sitepress
*
* @return null|string
*/
public function get_save_post_lang( $post_id, $sitepress ) {
$language_code = null;
if ( isset( $_POST['post_ID'] ) && (int) $_POST['post_ID'] === (int) $post_id ) {
$language_code = filter_var(
( isset( $_POST['icl_post_language'] ) ? $_POST['icl_post_language'] : '' ),
FILTER_SANITIZE_FULL_SPECIAL_CHARS );
}
$language_code = $language_code
? $language_code
: filter_input(
INPUT_GET,
'lang',
FILTER_SANITIZE_FULL_SPECIAL_CHARS
);
return $language_code ? $language_code : parent::get_save_post_lang( $post_id, $sitepress );
}
/**
* @param array $post_vars
* @return bool
*/
private function is_inline_action( $post_vars ) {
return isset( $post_vars[ 'action' ] )
&& $post_vars[ 'action' ] == 'inline-save'
|| isset( $_GET[ 'bulk_edit' ] )
|| isset( $_GET[ 'doing_wp_cron' ] )
|| ( isset( $_GET[ 'action' ] )
&& $_GET[ 'action' ] == 'untrash' );
}
/**
* @param int $trid
* @param string $language_code
* @param string $default_language
*
* @return null|string
*/
protected function get_save_post_source_lang( $trid, $language_code, $default_language ) {
$source_language = filter_input ( INPUT_GET, 'source_lang', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$source_language = $source_language ? $source_language : $this->get_source_language_from_referer();
$source_language = $source_language ? $source_language : SitePress::get_source_language_by_trid ( $trid );
$source_language = $source_language === 'all' ? $default_language : $source_language;
$source_language = $source_language !== $language_code ? $source_language : null;
return $source_language;
}
/**
* Gets the source_language $_GET parameter from the HTTP_REFERER
*
* @return string|bool
*/
private function get_source_language_from_referer() {
if ( ! isset( $_SERVER['HTTP_REFERER'] ) ) {
return false;
}
$referer = $_SERVER['HTTP_REFERER'];
$query = (string) wpml_parse_url( $referer, PHP_URL_QUERY );
parse_str( $query, $query_parts );
return isset( $query_parts['source_lang'] ) ? $query_parts['source_lang'] : false;
}
public function get_trid_from_referer() {
$http_referer = $this->get_http_referer();
return $http_referer->get_trid();
}
protected function get_http_referer() {
if ( ! $this->http_referer ) {
$factory = new WPML_URL_HTTP_Referer_Factory();
$this->http_referer = $factory->create();
}
return $this->http_referer;
}
}