class-wpml-inactive-content.php
4.57 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
184
<?php
class WPML_Inactive_Content {
/** @var wpdb $wpdb */
private $wpdb;
/** @var string $current_language */
private $current_language;
/** @var array $content_types */
private $content_types;
/** @var array $inactive */
private $inactive;
public function __construct( wpdb $wpdb, $current_language ) {
$this->wpdb = $wpdb;
$this->current_language = $current_language;
}
/** @return bool */
public function has_entries() {
return (bool) $this->get_inactive();
}
/** @return array */
public function get_content_types() {
foreach ( $this->get_inactive() as $types ) {
foreach ( $types as $type => $slugs ) {
foreach ( $slugs as $slug => $count ) {
$this->content_types[ $type ][ $slug ] = $this->get_label( $type, $slug );
}
}
}
return $this->content_types;
}
/** @return array */
public function get_languages() {
return array_keys( $this->get_inactive() );
}
/** @return array */
public function get_language_counts_rows() {
$counts = array();
foreach ( $this->get_languages() as $language ) {
foreach ( $this->get_content_types() as $type => $slugs ) {
foreach ( $slugs as $slug => $label ) {
$counts[ $language ][] = $this->count( $language, $type, $slug );
}
}
}
return $counts;
}
/**
* @param string $lang
* @param string $type
* @param string $slug
*
* @return int
*/
private function count( $lang, $type, $slug ) {
if ( isset( $this->inactive[ $lang ][ $type ][ $slug ] ) ) {
return (int) $this->inactive[ $lang ][ $type ][ $slug ];
}
return 0;
}
/**
* @param $langName
*
* @return string
*/
public function getLangCode( $langName ) {
return \WPML\Element\API\Languages::getCodeByName($langName);
}
/** @return array */
private function get_inactive() {
if ( null === $this->inactive ) {
$this->inactive = array();
$post_query = $this->wpdb->prepare(
"
SELECT COUNT(posts.ID) AS c, posts.post_type, languages_translations.name AS language
FROM {$this->wpdb->prefix}icl_translations translations
JOIN {$this->wpdb->posts} posts
ON translations.element_id = posts.ID AND translations.element_type LIKE %s
JOIN {$this->wpdb->prefix}icl_languages languages
ON translations.language_code = languages.code AND languages.active = 0
JOIN {$this->wpdb->prefix}icl_languages_translations languages_translations
ON languages_translations.language_code = languages.code
AND languages_translations.display_language_code = %s
GROUP BY posts.post_type, translations.language_code
",
array( wpml_like_escape( 'post_' ) . '%', $this->current_language )
);
$post_results = $this->wpdb->get_results( $post_query );
if ( $post_results ) {
foreach ( $post_results as $r ) {
$this->inactive[ $r->language ]['post'][ $r->post_type ] = $r->c;
}
}
$tax_query = $this->wpdb->prepare(
"
SELECT COUNT(posts.term_taxonomy_id) AS c, posts.taxonomy, languages_translations.name AS language
FROM {$this->wpdb->prefix}icl_translations translations
JOIN {$this->wpdb->term_taxonomy} posts
ON translations.element_id = posts.term_taxonomy_id
JOIN {$this->wpdb->prefix}icl_languages languages
ON translations.language_code = languages.code AND languages.active = 0
JOIN {$this->wpdb->prefix}icl_languages_translations languages_translations
ON languages_translations.language_code = languages.code
AND languages_translations.display_language_code = %s
WHERE translations.element_type LIKE %s AND translations.element_type NOT LIKE %s
GROUP BY posts.taxonomy, translations.language_code
",
[
$this->current_language,
wpml_like_escape( 'tax_' ) . '%',
'%'. wpml_like_escape('tax_translation_priority') . '%'
]
);
$tax_results = $this->wpdb->get_results( $tax_query );
if ( $tax_results ) {
foreach ( $tax_results as $r ) {
if ( ! $this->is_only_default_category( $r ) ) {
$this->inactive[ $r->language ]['taxonomy'][ $r->taxonomy ] = $r->c;
}
}
}
}
return $this->inactive;
}
/**
* @param stdClass $r
*
* @return bool
*/
private function is_only_default_category( $r ) {
return $r->taxonomy === 'category' && $r->c == 1;
}
/**
* @param string $type
* @param string $slug
*
* @return null|string
*/
private function get_label( $type, $slug ) {
if ( 'post' === $type ) {
$type_object = get_post_type_object( $slug );
} else {
$type_object = get_taxonomy( $slug );
}
if ( isset( $type_object->label ) ) {
return $type_object->label;
}
return null;
}
}