class-wpml-tm-records.php
4.31 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
<?php
class WPML_TM_Records {
/** @var WPDB $wpdb */
public $wpdb;
/** @var array $cache */
private $cache = array(
'icl_translations' => array(),
'status' => array(),
);
private $preloaded_statuses = null;
/** @var WPML_Frontend_Post_Actions | WPML_Admin_Post_Actions $wpml_post_translations */
private $wpml_post_translations;
/** @var WPML_Term_Translation $wpml_term_translations */
private $wpml_term_translations;
public function __construct(
wpdb $wpdb,
WPML_Post_Translation $wpml_post_translations,
WPML_Term_Translation $wpml_term_translations
) {
$this->wpdb = $wpdb;
$this->wpml_post_translations = $wpml_post_translations;
$this->wpml_term_translations = $wpml_term_translations;
}
public function wpdb() {
return $this->wpdb;
}
public function get_post_translations() {
return $this->wpml_post_translations;
}
public function get_term_translations() {
return $this->wpml_term_translations;
}
/**
* @param int $translation_id
*
* @return WPML_TM_ICL_Translation_Status
*/
public function icl_translation_status_by_translation_id( $translation_id ) {
if ( ! isset( $this->cache['status'][ $translation_id ] ) ) {
$this->maybe_preload_translation_statuses();
$this->cache['status'][ $translation_id ] = new WPML_TM_ICL_Translation_Status( $this->wpdb, $this, $translation_id );
}
return $this->cache['status'][ $translation_id ];
}
private function maybe_preload_translation_statuses() {
if ( null === $this->preloaded_statuses ) {
$translation_ids = $this->wpml_post_translations->get_translations_ids();
if ( $translation_ids ) {
$translation_ids = implode( ',', $translation_ids );
$this->preloaded_statuses = $this->wpdb->get_results(
"SELECT *
FROM {$this->wpdb->prefix}icl_translation_status
WHERE translation_id in ({$translation_ids})"
);
} else {
$this->preloaded_statuses = array();
}
}
}
public function get_preloaded_translation_status( $translation_id, $rid ) {
$data = null;
if ( $this->preloaded_statuses ) {
foreach ( $this->preloaded_statuses as $status ) {
if ( $translation_id && $status->translation_id == $translation_id ) {
$data = $status;
break;
} elseif ( $rid && $status->translation_id == $rid ) {
$data = $status;
break;
}
}
}
return $data;
}
/**
* @param int $rid
*
* @return WPML_TM_ICL_Translation_Status
*/
public function icl_translation_status_by_rid( $rid ) {
return new WPML_TM_ICL_Translation_Status( $this->wpdb, $this, $rid, 'rid' );
}
/**
* @param int $job_id
*
* @return WPML_TM_ICL_Translate_Job
*/
public function icl_translate_job_by_job_id( $job_id ) {
return new WPML_TM_ICL_Translate_Job( $this, $job_id );
}
/**
* @param int $translation_id
*
* @return WPML_TM_ICL_Translations
*/
public function icl_translations_by_translation_id( $translation_id ) {
return new WPML_TM_ICL_Translations( $this, $translation_id );
}
/**
* @param int $element_id
* @param string $type_prefix
*
* @return WPML_TM_ICL_Translations
*/
public function icl_translations_by_element_id_and_type_prefix(
$element_id,
$type_prefix
) {
$key = md5( $element_id . $type_prefix );
if ( ! isset( $this->cache['icl_translations'][ $key ] ) ) {
$this->cache['icl_translations'][ $key ] = new WPML_TM_ICL_Translations(
$this,
array(
'element_id' => $element_id,
'type_prefix' => $type_prefix,
),
'id_type_prefix'
);
}
return $this->cache['icl_translations'][ $key ];
}
/**
* @param int $trid
* @param string $lang
*
* @return WPML_TM_ICL_Translations
*/
public function icl_translations_by_trid_and_lang( $trid, $lang ) {
$key = md5( $trid . $lang );
if ( ! isset( $this->cache['icl_translations'][ $key ] ) ) {
$this->cache['icl_translations'][ $key ] = new WPML_TM_ICL_Translations(
$this,
array(
'trid' => $trid,
'language_code' => $lang,
),
'trid_lang'
);
}
return $this->cache['icl_translations'][ $key ];
}
/**
* @param int $trid
*
* @return int[]
*/ public function get_element_ids_from_trid( $trid ) {
return $this->wpdb->get_col(
$this->wpdb->prepare(
"SELECT element_id
FROM {$this->wpdb->prefix}icl_translations
WHERE trid = %d",
$trid
)
);
}
}