]> _ Git - physioassist-wordpress.git/blob
74ce7786c14785b54878352efa18d71a585520d4
[physioassist-wordpress.git] /
1 <?php
2
3 class WPML_Post_Edit_Terms_Hooks implements IWPML_Action {
4
5         const AFTER_POST_DATA_SANITIZED_ACTION = 'init';
6
7         /** @var IWPML_Current_Language $language */
8         private $language;
9
10         /** @var wpdb $wpdb */
11         private $wpdb;
12
13         public function __construct( IWPML_Current_Language $current_language, wpdb $wpdb ) {
14                 $this->language = $current_language;
15                 $this->wpdb     = $wpdb;
16         }
17
18         public function add_hooks() {
19                 add_action( self::AFTER_POST_DATA_SANITIZED_ACTION, array( $this, 'set_tags_input_with_ids' ) );
20         }
21
22         public function set_tags_input_with_ids() {
23                 $tag_names = $this->get_tags_from_tax_input();
24
25                 if ( $tag_names ) {
26                         $sql = "SELECT t.name, t.term_id FROM {$this->wpdb->terms} AS t
27                                 LEFT JOIN {$this->wpdb->term_taxonomy} AS tt
28                                         ON tt.term_id = t.term_id
29                                 LEFT JOIN {$this->wpdb->prefix}icl_translations AS tr
30                                         ON tr.element_id = tt.term_taxonomy_id AND tr.element_type = 'tax_post_tag'
31                                 WHERE tr.language_code = %s AND t.name IN(" . wpml_prepare_in( $tag_names ) . ")";
32
33                         $tags = $this->wpdb->get_results( $this->wpdb->prepare( $sql, $this->language->get_current_language() ) );
34
35                         foreach ( $tags as $tag ) {
36                                 $_POST['tags_input'][] = (int) $tag->term_id;
37                         }
38                 }
39         }
40
41         /**
42          * @return array
43          */
44         public function get_tags_from_tax_input() {
45                 if ( ! empty( $_POST['tax_input']['post_tag'] ) ) {
46                         $tags = $_POST['tax_input']['post_tag'];
47
48                         if ( ! is_array( $tags ) ) {
49                                 /**
50                                  * This code is following the logic from `edit_post()` in core
51                                  * where the terms name are converted into IDs.
52                                  *
53                                  * @see edit_post
54                                  */
55                                 $delimiter = _x( ',', 'tag delimiter' );
56                                 $tags      = explode( $delimiter, trim( $tags, " \n\t\r\0\x0B," ) );
57                         }
58
59                         return $tags;
60                 }
61
62                 return null;
63         }
64 }