wpml-st-word-count-package-records.php
3.43 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
<?php
class WPML_ST_Word_Count_Package_Records {
/** @var wpdb */
private $wpdb;
public function __construct( wpdb $wpdb ) {
$this->wpdb = $wpdb;
}
/** @return array */
public function get_all_package_ids() {
return array_map(
'intval',
$this->wpdb->get_col( "SELECT ID FROM {$this->wpdb->prefix}icl_string_packages" )
);
}
/** @return array */
public function get_packages_ids_without_word_count() {
return array_map(
'intval',
$this->wpdb->get_col(
"SELECT ID FROM {$this->wpdb->prefix}icl_string_packages WHERE word_count IS NULL"
)
);
}
/** @return array */
public function get_word_counts( $post_id ) {
return $this->wpdb->get_col(
$this->wpdb->prepare(
"SELECT word_count FROM {$this->wpdb->prefix}icl_string_packages WHERE post_id = %d",
$post_id
)
);
}
/**
* @param int $package_id
* @param string $word_count
*/
public function set_word_count( $package_id, $word_count ) {
$this->wpdb->update(
$this->wpdb->prefix . 'icl_string_packages',
array( 'word_count' => $word_count ),
array( 'ID' => $package_id )
);
}
/**
* @param int $package_id
*
* @return null|string
*/
public function get_word_count( $package_id ) {
return $this->wpdb->get_var(
$this->wpdb->prepare(
"SELECT word_count FROM {$this->wpdb->prefix}icl_string_packages WHERE ID = %d",
$package_id
)
);
}
public function reset_all( array $package_kinds ) {
if ( ! $package_kinds ) {
return;
}
$query = "UPDATE {$this->wpdb->prefix}icl_string_packages SET word_count = NULL
WHERE kind_slug IN(" . wpml_prepare_in( $package_kinds ) . ')';
$this->wpdb->query( $query );
}
/**
* @param array $kinds
*
* @return array
*/
public function get_ids_from_kind_slugs( array $kinds ) {
if ( ! $kinds ) {
return array();
}
$query = "SELECT ID FROM {$this->wpdb->prefix}icl_string_packages
WHERE kind_slug IN(" . wpml_prepare_in( $kinds ) . ')';
return array_map( 'intval', $this->wpdb->get_col( $query ) );
}
/**
* @param array $post_types
*
* @return array
*/
public function get_ids_from_post_types( array $post_types ) {
if ( ! $post_types ) {
return array();
}
$query = "SELECT sp.ID FROM {$this->wpdb->prefix}icl_string_packages AS sp
LEFT JOIN {$this->wpdb->posts} AS p
ON p.ID = sp.post_id
WHERE p.post_type IN(" . wpml_prepare_in( $post_types ) . ')';
return array_map( 'intval', $this->wpdb->get_col( $query ) );
}
/**
* @param string $kind_slug
*
* @return int
*/
public function count_items_by_kind_not_part_of_posts( $kind_slug ) {
$query = "SELECT COUNT(*) FROM {$this->wpdb->prefix}icl_string_packages
WHERE kind_slug = %s AND post_id IS NULL";
return (int) $this->wpdb->get_var( $this->wpdb->prepare( $query, $kind_slug ) );
}
/**
* @param string $kind_slug
*
* @return int
*/
public function count_word_counts_by_kind( $kind_slug ) {
$query = "SELECT COUNT(*) FROM {$this->wpdb->prefix}icl_string_packages
WHERE kind_slug = %s AND word_count IS NOT NULL
AND post_id IS NULL";
return (int) $this->wpdb->get_var( $this->wpdb->prepare( $query, $kind_slug ) );
}
/**
* @param string $kind_slug
*
* @return array
*/
public function get_word_counts_by_kind( $kind_slug ) {
$query = "SELECT word_count FROM {$this->wpdb->prefix}icl_string_packages
WHERE kind_slug = %s";
return $this->wpdb->get_col( $this->wpdb->prepare( $query, $kind_slug ) );
}
}