wpml-localization.class.php
4.94 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
<?php
class WPML_Localization {
/**
* @var \wpdb
*/
private $wpdb;
/**
* WPML_Localization constructor.
*
* @param wpdb $wpdb
*/
public function __construct( wpdb $wpdb ) {
$this->wpdb = $wpdb;
}
public function get_theme_localization_stats( $theme_localization_domains = array() ) {
if ( empty( $theme_localization_domains ) || ! is_array( $theme_localization_domains ) ) {
$theme_localization_domains = icl_get_sub_setting( 'st', 'theme_localization_domains' );
}
return $this->get_domain_stats( $theme_localization_domains, 'theme' );
}
public function get_domain_stats( $localization_domains, $default, $no_wordpress = false, $count_in_progress_as_completed = false ) {
$results = array();
if ( $localization_domains ) {
$domains = array();
foreach ( (array) $localization_domains as $domain ) {
if ( ! ( $no_wordpress && 'WordPress' === $domain ) ) {
$domains[] = $domain ? $domain : $default;
}
}
if ( ! empty( $domains ) ) {
$sql = "SELECT context, status, COUNT(id) AS c
FROM {$this->wpdb->prefix}icl_strings
WHERE context IN (" . wpml_prepare_in( $domains ) . ")
GROUP BY context, status";
$results = $this->wpdb->get_results( $sql );
}
}
return $this->results_to_array( $results, $count_in_progress_as_completed );
}
public function get_localization_stats( $component_type ) {
$localization_data = $this->get_localization_data( $component_type );
$results = array();
$all_domains = array();
foreach ( $localization_data as $component => $localization_domains ) {
$all_domains = array_merge( $all_domains, array_keys( $localization_domains ) );
}
$all_results = $this->get_domain_stats( $all_domains, $component_type, true );
foreach ( $localization_data as $component => $localization_domains ) {
$domains = array_keys( $localization_domains );
foreach ( $domains as $domain ) {
if ( array_key_exists( $domain, $all_results ) ) {
$results[ $component ][ $domain ] = $all_results[ $domain ];
}
}
}
return $results;
}
private function get_localization_data( $component_type ) {
$localization_data = apply_filters( 'wpml_sub_setting', array(), 'st', 'plugin' === $component_type ? 'plugin_localization_domains' : 'theme_localization_domains' );
if ( ! is_array( current( $localization_data ) ) ) {
if ( 'plugin' === $component_type ) {
return array();
}
$localization_data = array();
foreach ( wp_get_themes() as $theme_folder => $theme ) {
if ( $theme->get( 'TextDomain' ) ) {
$localization_data[ $theme_folder ] = array( $theme->get( 'TextDomain' ) => 0 );
}
}
}
return $localization_data;
}
public function get_wrong_plugin_localization_stats() {
$results = $this->wpdb->get_results(
"
SELECT context, status, COUNT(id) AS c
FROM {$this->wpdb->prefix}icl_strings
WHERE context LIKE ('plugin %')
GROUP BY context, status
"
);
return $this->results_to_array( $results );
}
public function get_wrong_theme_localization_stats() {
$results = $this->wpdb->get_results(
"
SELECT context, status, COUNT(id) AS c
FROM {$this->wpdb->prefix}icl_strings
WHERE context LIKE ('theme %')
GROUP BY context, status
"
);
$results = $this->results_to_array( $results );
$theme_path = TEMPLATEPATH;
$old_theme_context = 'theme ' . basename( $theme_path );
unset( $results[ $old_theme_context ] );
return $results;
}
public function does_theme_require_rescan() {
$theme_path = TEMPLATEPATH;
$old_theme_context = 'theme ' . basename( $theme_path );
/** @var string $sql */
$sql = $this->wpdb->prepare(
"
SELECT COUNT(id) AS c
FROM {$this->wpdb->prefix}icl_strings
WHERE context = %s",
$old_theme_context
);
$result = $this->wpdb->get_var( $sql );
return $result ? true : false;
}
public function get_most_popular_domain( $plugin ) {
$plugin_localization_domains = icl_get_sub_setting( 'st', 'plugin_localization_domains' );
$most_popular = '';
$most_count = 0;
foreach ( $plugin_localization_domains[ $plugin ] as $name => $count ) {
if ( $name == 'WordPress' || $name == 'default' ) {
continue;
}
if ( $count > $most_count ) {
$most_popular = $name;
$most_count = $count;
}
}
return $most_popular;
}
private function results_to_array( $results, $count_in_progress_as_completed = false ) {
$stats = array();
foreach ( $results as $r ) {
if ( ! isset( $stats[ $r->context ]['complete'] ) ) {
$stats[ $r->context ]['complete'] = 0;
}
if ( ! isset( $stats[ $r->context ]['incomplete'] ) ) {
$stats[ $r->context ]['incomplete'] = 0;
}
if (
$r->status == ICL_TM_COMPLETE ||
( $count_in_progress_as_completed && $r->status == ICL_TM_IN_PROGRESS )
) {
$stats[ $r->context ]['complete'] += $r->c;
} else {
$stats[ $r->context ]['incomplete'] += $r->c;
}
}
return $stats;
}
}