-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgp-project-contributors.php
More file actions
250 lines (195 loc) · 10.1 KB
/
gp-project-contributors.php
File metadata and controls
250 lines (195 loc) · 10.1 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
/*
Plugin Name: GP Project Contributors
Plugin URI: http://glot-o-matic.com/gp-project-contributors
Description: Create a dynamic list of contirbutors to your GlotPress project with a WordPress shortcode.
Version: 1.1
Author: Greg Ross
Author URI: http://toolstack.com
Tags: glotpress, glotpress plugin
License: GPLv2 or later
*/
class GP_Project_Contributors {
public $id = 'gp-project-contributors';
public function __construct() {
add_shortcode( 'gp-project-contributors', array( $this, 'gp_project_contributors' ) );
add_shortcode( 'gp-project-contributors-translators', array( $this, 'gp_project_contributors_translators' ) );
}
public function gp_project_contributors( $atts ) {
GLOBAL $wpdb, $gp_table_prefix;
$project_id = '%';
if( is_array( $atts ) ) {
$projects = null;
if( array_key_exists( 'name', $atts ) ) {
$project = GP::$project->find_one( array( 'name' => $atts['name'] ) );
$project_id = $project->id;
}
if( array_key_exists( 'slug', $atts ) ) {
$project = GP::$project->find_one( array( 'slug' => $atts['slug'] ) );
$project_id = $project->id;
}
if( array_key_exists( 'id', $atts ) ) {
$project_id = (int)$atts['id'];
}
}
wp_enqueue_style( 'dashicons' );
// They're a call so let's create it.
$gpl = new GP_Locales;
// Setup some variables to use later.
$return = '<style type="text/css">.gptl-twitter, .gptl-twitter:focus, .gptl-twitter:hover, .gptl-twitter:link, .gptl-twitter:visited, .gptl-twitter:active { color: #55acee; } .gptl-facebook, .gptl-facebook:focus, .gptl-facebook:hover, .gptl-facebook:link, .gptl-facebook:visited, .gptl-facebook:active { color: #3A5795; }</style><table style="border: 0px;">';
$names = array();
$links = array();
// Grab all of the approvers from the GlotPress permissions table and join it to the WordPress users table so we can get display names later.
$result = $wpdb->get_results( "SELECT * FROM {$gp_table_prefix}permissions INNER JOIN `{$wpdb->users}` on `{$gp_table_prefix}permissions`.`user_id` = `{$wpdb->users}`.`ID` WHERE `{$gp_table_prefix}permissions`.`action`='approve' AND `{$gp_table_prefix}permissions`.`object_id` LIKE '{$project_id}|%'" );
// Loop through all the results from the database and create a list of locales with all their approvers associated with them.
foreach( $result as $row ) {
$details = explode( '|', $row->object_id );
if( $details === FALSE || !isset( $details[1] ) ) { continue; }
$current = $gpl->locales[$details[1]];
$names[$current->english_name][] = $row->display_name;
$links[$row->display_name] = $row->user_url;
}
// Sort the locale list.
ksort( $names );
// Loop through all the locales to do the output.
foreach( $names as $key => $values ) {
// Sort the approvers names alphabetically.
ksort( $values );
foreach( $values as $keynumber => $display_name ) {
if( $links[$display_name] ) {
$nice_link = parse_url( $links[$display_name], PHP_URL_HOST );
$nice_link = str_ireplace( 'www.', '', $nice_link );
$nice_link = strtolower( $nice_link );
if( strstr( $display_name, $nice_link ) ) {
$nice_link = '';
} else {
switch( $nice_link ) {
case 'twitter.com':
$nice_link = ' <span class="dashicons dashicons-twitter gptl-twitter"></span>';
break;
case 'facebook.com':
$nice_link = ' <span class="dashicons dashicons-facebook gptl-facebook"></span>';
break;
default:
$nice_link = ' (' . htmlentities( $nice_link ) . ')';
break;
}
}
$values[$keynumber] = '<a href="' . htmlentities( $links[$display_name], ENT_QUOTES ) . '" target="_blank">' . htmlentities( $display_name, ENT_QUOTES ) . $nice_link . '</a>';
} else {
$values[$keynumber] = htmlentities( $display_name, ENT_QUOTES );
}
}
// Create the return string.
$return .= '<tr><td style="text-align: right; border: 0px; background: transparent; white-space: nowrap;">' . htmlentities( $key, ENT_QUOTES ) . ':</td><td style="border: 0px; background: transparent; padding-left:5px;">' . implode( ', ', $values ) . '</td></tr>' . PHP_EOL;
}
$return .= '</table>';
// Return the value.
return $return;
}
public function gp_project_contributors_translators( $atts ) {
GLOBAL $wpdb, $gp_table_prefix;
$project_id = '%';
if( is_array( $atts ) ) {
$projects = null;
if( array_key_exists( 'name', $atts ) ) {
$project = GP::$project->find_one( array( 'name' => $atts['name'] ) );
$project_id = $project->id;
}
if( array_key_exists( 'slug', $atts ) ) {
$project = GP::$project->find_one( array( 'slug' => $atts['slug'] ) );
$project_id = $project->id;
}
if( array_key_exists( 'id', $atts ) ) {
$project_id = (int)$atts['id'];
}
}
wp_enqueue_style( 'dashicons' );
// They're a call so let's create it.
$gpl = new GP_Locales;
// Setup some variables to use later.
$return = '<style type="text/css">.gptl-twitter, .gptl-twitter:focus, .gptl-twitter:hover, .gptl-twitter:link, .gptl-twitter:visited, .gptl-twitter:active { color: #55acee; } .gptl-facebook, .gptl-facebook:focus, .gptl-facebook:hover, .gptl-facebook:link, .gptl-facebook:visited, .gptl-facebook:active { color: #3A5795; }</style><table style="border: 0px;">';
$names = array();
$links = array();
$languageContributions = array();
// Grab all of the contributors from the GlotPress translations table and join it to the WordPress users table so we can get display names later.
$result = $wpdb->get_results( "SELECT {$wpdb->users}.*,
SUM(CASE {$gp_table_prefix}translations.status WHEN 'current' THEN 1 ELSE 0 END) current_contrib,
SUM(CASE {$gp_table_prefix}translations.status WHEN 'fuzzy' THEN 1 ELSE 0 END) fuzzy_contrib,
SUM(CASE {$gp_table_prefix}translations.status WHEN 'waiting' THEN 1 ELSE 0 END) waiting_contrib,
SUM(CASE {$gp_table_prefix}translations.status WHEN 'old' THEN 1 ELSE 0 END) old_contrib,
SUM(CASE {$gp_table_prefix}translations.status WHEN 'rejected' THEN 1 ELSE 0 END) rejected_contrib,
COUNT(DISTINCT {$gp_table_prefix}translations.id) total_contrib,
{$gp_table_prefix}translation_sets.*
FROM {$gp_table_prefix}translations
INNER JOIN {$gp_table_prefix}translation_sets ON {$gp_table_prefix}translations.translation_set_id = {$gp_table_prefix}translation_sets.id
INNER JOIN `{$wpdb->users}` on `{$gp_table_prefix}translations`.`user_id` = `{$wpdb->users}`.`ID`
INNER JOIN {$gp_table_prefix}originals ON {$gp_table_prefix}translations.original_id = {$gp_table_prefix}originals.id
WHERE `{$gp_table_prefix}originals`.`project_id` = {$project_id}
AND `{$gp_table_prefix}originals`.`status` = '+active'
GROUP BY {$wpdb->users}.user_login, {$gp_table_prefix}translation_sets.id" );
// Loop through all the results from the database and create a list of locales with all their approvers associated with them.
foreach( $result as $row ) {
$current = $gpl->locales[$row->locale];
$names[$current->english_name][] = $row->display_name;
$links[$row->display_name] = $row->user_url;
$contribs = new stdClass;
$contribs->current = $row->current_contrib;
$contribs->fuzzy = $row->fuzzy_contrib;
$contribs->waiting = $row->waiting_contrib;
$contribs->old = $row->old_contrib;
$contribs->rejected = $row->rejected_contrib;
$contribs->total = $row->total_contrib;
$contribs->tooltip = __( 'Current', 'gp-project-contributors' ) . ': ' . number_format_i18n( $contribs->current, 0 ) . PHP_EOL .
__( 'Fuzzy', 'gp-project-contributors' ) . ': ' . number_format_i18n( $contribs->fuzzy, 0 ) . PHP_EOL .
__( 'Waiting', 'gp-project-contributors' ) . ': ' . number_format_i18n( $contribs->waiting, 0 ) . PHP_EOL .
__( 'Old', 'gp-project-contributors' ) . ': ' . number_format_i18n( $contribs->old, 0 ) . PHP_EOL .
__( 'Rejected', 'gp-project-contributors' ) . ': ' . number_format_i18n( $contribs->rejected, 0 );
$languageContributions[$row->display_name][$current->english_name] = $contribs;
}
// Sort the locale list.
ksort( $names );
// Loop through all the locales to do the output.
foreach( $names as $key => $values ) {
// Sort the approvers names alphabetically.
ksort( $values );
foreach( $values as $keynumber => $display_name ) {
if( $links[$display_name] ) {
$nice_link = parse_url( $links[$display_name], PHP_URL_HOST );
$nice_link = str_ireplace( 'www.', '', $nice_link );
$nice_link = strtolower( $nice_link );
if( strstr( $display_name, $nice_link ) ) {
$nice_link = '';
} else {
switch( $nice_link ) {
case 'twitter.com':
$nice_link = ' <span class="dashicons dashicons-twitter gptl-twitter"></span>';
break;
case 'facebook.com':
$nice_link = ' <span class="dashicons dashicons-facebook gptl-facebook"></span>';
break;
default:
$nice_link = ' (' . htmlentities( $nice_link ) . ')';
break;
}
}
$values[$keynumber] = '<a href="' . htmlentities( $links[$display_name], ENT_QUOTES ) . '" target="_blank">' . htmlentities( $display_name, ENT_QUOTES ) . $nice_link . '</a>' . ' (' . number_format_i18n($languageContributions[$display_name][$key]->current, 0) . ')';
} else {
$values[$keynumber] = htmlentities( $display_name, ENT_QUOTES ) . ' <span title="' . htmlentities( $languageContributions[$display_name][$key]->tooltip ) .'">(' . number_format_i18n($languageContributions[$display_name][$key]->current, 0) . ')</span>';
}
}
// Create the return string.
$return .= '<tr><td style="text-align: right; border: 0px; background: transparent; white-space: nowrap;">' . htmlentities( $key, ENT_QUOTES ) . ':</td><td style="border: 0px; background: transparent; padding-left:5px;">' . implode( ', ', $values ) . '</td></tr>' . PHP_EOL;
}
$return .= '</table>';
// Return the value.
return $return;
}
}
// Add an action to WordPress's init hook to setup the plugin. Don't just setup the plugin here as the GlotPress plugin may not have loaded yet.
add_action( 'gp_init', 'gp_project_contributors_init' );
// This function creates the plugin.
function gp_project_contributors_init() {
GLOBAL $gp_project_contributors;
$gp_project_contributors = new GP_Project_Contributors;
}