class-wpml-post-edit-terms-hooks.php
1.75 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
<?php
class WPML_Post_Edit_Terms_Hooks implements IWPML_Action {
const AFTER_POST_DATA_SANITIZED_ACTION = 'init';
/** @var IWPML_Current_Language $language */
private $language;
/** @var wpdb $wpdb */
private $wpdb;
public function __construct( IWPML_Current_Language $current_language, wpdb $wpdb ) {
$this->language = $current_language;
$this->wpdb = $wpdb;
}
public function add_hooks() {
add_action( self::AFTER_POST_DATA_SANITIZED_ACTION, array( $this, 'set_tags_input_with_ids' ) );
}
public function set_tags_input_with_ids() {
$tag_names = $this->get_tags_from_tax_input();
if ( $tag_names ) {
$sql = "SELECT t.name, t.term_id FROM {$this->wpdb->terms} AS t
LEFT JOIN {$this->wpdb->term_taxonomy} AS tt
ON tt.term_id = t.term_id
LEFT JOIN {$this->wpdb->prefix}icl_translations AS tr
ON tr.element_id = tt.term_taxonomy_id AND tr.element_type = 'tax_post_tag'
WHERE tr.language_code = %s AND t.name IN(" . wpml_prepare_in( $tag_names ) . ")";
$tags = $this->wpdb->get_results( $this->wpdb->prepare( $sql, $this->language->get_current_language() ) );
foreach ( $tags as $tag ) {
$_POST['tags_input'][] = (int) $tag->term_id;
}
}
}
/**
* @return array|null
*/
public function get_tags_from_tax_input() {
if ( ! empty( $_POST['tax_input']['post_tag'] ) ) {
$tags = $_POST['tax_input']['post_tag'];
if ( ! is_array( $tags ) ) {
/**
* This code is following the logic from `edit_post()` in core
* where the terms name are converted into IDs.
*
* @see edit_post
*/
$delimiter = _x( ',', 'tag delimiter' );
$tags = explode( $delimiter, trim( $tags, " \n\t\r\0\x0B," ) );
}
if ( $tags ) {
return array_map( 'trim', $tags );
}
}
return null;
}
}