wpml-st-word-count-string-records.php
2.86 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
<?php
class WPML_ST_Word_Count_String_Records {
const CACHE_GROUP = __CLASS__;
/** @var wpdb */
private $wpdb;
public function __construct( wpdb $wpdb ) {
$this->wpdb = $wpdb;
}
/** @return int */
public function get_total_words() {
return (int) $this->wpdb->get_var( "SELECT SUM(word_count) FROM {$this->wpdb->prefix}icl_strings" );
}
/** @return array */
public function get_all_values_without_word_count() {
$query = "
SELECT id, value FROM {$this->wpdb->prefix}icl_strings
WHERE word_count IS NULL
";
return $this->wpdb->get_results( $query );
}
/**
* @param string $lang
* @param null|string $package_id
*
* @return int
*/
public function get_words_to_translate_per_lang( $lang, $package_id = null ) {
$key = $lang . ':' . $package_id;
$found = false;
$words = WPML_Non_Persistent_Cache::get( $key, self::CACHE_GROUP, $found );
if ( ! $found ) {
$query = "
SELECT SUM(word_count) FROM {$this->wpdb->prefix}icl_strings AS s
LEFT JOIN {$this->wpdb->prefix}icl_string_translations AS st
ON st.string_id = s.id AND st.language = %s
WHERE (st.status <> %d OR st.status IS NULL)
";
$prepare_args = [
$lang,
ICL_STRING_TRANSLATION_COMPLETE,
];
if ( $package_id ) {
$query .= ' AND s.string_package_id = %d';
$prepare_args[] = $package_id;
}
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$words = (int) $this->wpdb->get_var( $this->wpdb->prepare( $query, $prepare_args ) );
WPML_Non_Persistent_Cache::set( $key, $words, self::CACHE_GROUP );
}
return $words;
}
/**
* @param int $string_id
*
* @return stdClass
*/
public function get_value_and_language( $string_id ) {
return $this->wpdb->get_row(
$this->wpdb->prepare(
"SELECT value, language FROM {$this->wpdb->prefix}icl_strings WHERE id = %d",
$string_id
)
);
}
/**
* @param int $string_id
* @param int $word_count
*/
public function set_word_count( $string_id, $word_count ) {
$this->wpdb->update(
$this->wpdb->prefix . 'icl_strings',
array( 'word_count' => $word_count ),
array( 'id' => $string_id )
);
}
/**
* @param int $string_id
*
* @return int
*/
public function get_word_count( $string_id ) {
return (int) $this->wpdb->get_var(
$this->wpdb->prepare(
"SELECT word_count FROM {$this->wpdb->prefix}icl_strings WHERE ID = %d",
$string_id
)
);
}
public function reset_all() {
$this->wpdb->query( "UPDATE {$this->wpdb->prefix}icl_strings SET word_count = NULL" );
}
/**
* @param array $package_ids
*
* @return array
*/
public function get_ids_from_package_ids( array $package_ids ) {
if ( ! $package_ids ) {
return array();
}
$query = "SELECT id FROM {$this->wpdb->prefix}icl_strings
WHERE string_package_id IN(" . wpml_prepare_in( $package_ids ) . ')';
return array_map( 'intval', $this->wpdb->get_col( $query ) );
}
}