-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWS-Sync-with-Github.php
More file actions
296 lines (238 loc) · 11.9 KB
/
WS-Sync-with-Github.php
File metadata and controls
296 lines (238 loc) · 11.9 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/*
Plugin Name: WS Sync with Github
Plugin URI: https://github.com/WebShells/WS-Sync-with-Github
Description: Display GitHub repository issues, commits, and pull requests using shortcodes.
Version: 1.0
Author: WebShells ( WebShells Services Co. )
Author URI: https://www.wshells.ws
Text Domain: WS-Sync-with-Github
*/
function time_since_creation($created_at) {
$current_time = time();
$time_diff = $current_time - strtotime($created_at);
if ($time_diff < 60 * 60 * 24) { // Less than 1 day
return 'Today';
} elseif ($time_diff < 60 * 60 * 24 * 30) { // Less than 30 days
$days_ago = floor($time_diff / (60 * 60 * 24));
return $days_ago . ' ' . ($days_ago > 1 ? 'days' : 'day') . ' ago';
} else { // More than 30 days
$months_ago = floor($time_diff / (60 * 60 * 24 * 30));
return $months_ago . ' ' . ($months_ago > 1 ? 'months' : 'month') . ' ago';
}
}
function gitsync_issues_shortcode($atts) {
$atts = shortcode_atts(array(
'token' => '',
'repository' => '',
'owner' => '',
), $atts, 'gitsync_issues');
// Make API request for open issues using cURL
$issues_api_url = "https://api.github.com/repos/{$atts['owner']}/{$atts['repository']}/issues?state=open";
$headers = array(
'Authorization: token ' . $atts['token'],
'User-Agent: Your-User-Agent', // Add your own User-Agent here
);
// Initialize cURL session for issues
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $issues_api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Execute cURL session for issues
$issues_response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close cURL session for issues
curl_close($ch);
// Check if the API request for issues was successful
if ($http_code !== 200) {
return '<p>Error retrieving issues data from GitHub API.</p>';
}
// Check if the response is valid JSON
$issues_data = json_decode($issues_response, true);
// Display the data
$output = ''; // Initialize output variable
if (!empty($issues_data)) {
// Sort issues by created_at date in descending order (newest to oldest)
usort($issues_data, function ($a, $b) {
return strtotime($b['created_at']) - strtotime($a['created_at']);
});
// Get the latest 10 issues
$last_10_issues = array_slice($issues_data, 0, 10);
// Display issues
$output .= '<ul>';
foreach ($last_10_issues as $issue) {
// Check if the issue is not a pull request
if (!isset($issue['pull_request'])) {
// Generate list items for each issue
$output .= '<li>';
// Make the author image clickable and link to the user's GitHub profile
$output .= '<a href="' . esc_url($issue['user']['html_url']) . '" target="_blank">';
$output .= '<img src="' . esc_url($issue['user']['avatar_url']) . '" alt="' . esc_attr($issue['user']['login']) . '" width="24" height="24" style="vertical-align: middle; margin-right: 8px;" />';
$output .= '</a>';
// Issue Number (Clickable)
$output .= '<a href="' . esc_url($issue['html_url']) . '" target="_blank">Issue #' . esc_html($issue['number']) . '</a>';
// Issue Title
$output .= ' - ' . esc_html($issue['title']);
// Calculate the time since the issue was created
$time_since_creation = time_since_creation($issue['created_at']);
$output .= ' - Since (' . $time_since_creation . ')';
// Make the author name clickable and link to the user's GitHub profile
$output .= ' - <a href="' . esc_url($issue['user']['html_url']) . '" target="_blank">' . esc_html($issue['user']['login']) . '</a>';
$output .= '</li>';
}
}
$output .= '</ul>';
} else {
$output .= '<p>No open issues found.</p>';
}
return $output;
}
add_shortcode('gitsync_issues', 'gitsync_issues_shortcode');
function gitsync_commits_shortcode($atts) {
$atts = shortcode_atts(array(
'token' => '',
'repository' => '',
'owner' => '',
), $atts, 'gitsync_commits');
// Make API request for open commits using cURL
$commits_api_url = "https://api.github.com/repos/{$atts['owner']}/{$atts['repository']}/commits?per_page=10&sort=author-date&direction=desc";
$headers = array(
'Authorization: token ' . $atts['token'],
'User-Agent: Your-User-Agent', // Add your own User-Agent here
);
// Initialize cURL session for commits
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $commits_api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Execute cURL session for commits
$commits_response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close cURL session for commits
curl_close($ch);
// Check if the API request for commits was successful
if ($http_code !== 200) {
return '<p>Error retrieving commits data from GitHub API.</p>';
}
// Check if the response is valid JSON
$commits_data = json_decode($commits_response, true);
// Display the data
$output = ''; // Initialize output variable
if (!empty($commits_data)) {
// Display commits
$output .= '<ul>';
foreach ($commits_data as $commit) {
if (
isset($commit['author']['html_url']) &&
isset($commit['author']['avatar_url']) &&
isset($commit['commit']['author']['name']) &&
isset($commit['html_url']) &&
isset($commit['commit']['message']) &&
isset($commit['commit']['author']['date'])
) {
// Check if the commit message contains "Translate-URL:", and if so, exclude the exact line that contains it
$commit_message = explode("\n", $commit['commit']['message']);
$filtered_message = '';
$found_translate_url = false;
foreach ($commit_message as $line) {
if (strpos($line, 'Translate-URL:') !== false) {
$found_translate_url = true;
} else {
// Remove line breaks within the commit message
$filtered_message .= str_replace("\n", "", $line) . " ";
}
}
// Generate list items for each commit
$output .= '<li>';
// Make the author image clickable and link to the user's GitHub profile
$output .= '<a href="' . esc_url($commit['author']['html_url']) . '" target="_blank">';
$output .= '<img src="' . esc_url($commit['author']['avatar_url']) . '" alt="' . esc_attr($commit['commit']['author']['name']) . '" width="24" height="24" style="vertical-align: middle; margin-right: 8px;" />';
$output .= '</a>';
// Commit Number (Clickable)
$commit_number = substr($commit['sha'], 0, 7); // Extract the first 7 characters
$output .= '<a href="' . esc_url($commit['html_url']) . '" target="_blank">Commit #' . esc_html($commit_number) . '</a>';
// Commit Message (remove any line breaks within the commit message)
$output .= ' - ' . esc_html($filtered_message);
// Calculate the time since the commit was created
$time_since_creation = time_since_creation($commit['commit']['author']['date']);
$output .= ' - Since (' . $time_since_creation . ')';
// Make the author name clickable and link to the user's GitHub profile
$output .= ' - <a href="' . esc_url($commit['author']['html_url']) . '" target="_blank">' . esc_html($commit['commit']['author']['name']) . '</a>';
$output .= '</li>';
} else {
// Handle incomplete commit data
$output .= '<li>Weblate translation commit.</li>';
}
}
$output .= '</ul>';
} else {
$output .= '<p>No new commits found.</p>';
}
return $output;
}
add_shortcode('gitsync_commits', 'gitsync_commits_shortcode');
function gitsync_pull_requests_shortcode($atts) {
$atts = shortcode_atts(array(
'token' => '',
'repository' => '',
'owner' => '',
), $atts, 'gitsync_pull_requests');
// Make API request for open pull requests using cURL
$pulls_api_url = "https://api.github.com/repos/{$atts['owner']}/{$atts['repository']}/pulls?state=open";
$headers = array(
'Authorization: token ' . $atts['token'],
'User-Agent: Your-User-Agent', // Add your own User-Agent here
);
// Initialize cURL session for pull requests
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $pulls_api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Execute cURL session for pull requests
$pulls_response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close cURL session for pull requests
curl_close($ch);
// Check if the API request for pull requests was successful
if ($http_code !== 200) {
return '<p>Error retrieving pull requests data from GitHub API.</p>';
}
// Check if the response is valid JSON
$pulls_data = json_decode($pulls_response, true);
// Display the data
$output = ''; // Initialize output variable
if (!empty($pulls_data)) {
// Sort pull requests by created_at date in descending order (newest to oldest)
usort($pulls_data, function ($a, $b) {
return strtotime($b['created_at']) - strtotime($a['created_at']);
});
// Get the latest 10 pull requests
$last_10_pulls = array_slice($pulls_data, 0, 10);
// Display pull requests
$output .= '<ul>';
foreach ($last_10_pulls as $pull) {
// Generate list items for each pull request
$output .= '<li>';
// Make the author image clickable and link to the user's GitHub profile
$output .= '<a href="' . esc_url($pull['user']['html_url']) . '" target="_blank">';
$output .= '<img src="' . esc_url($pull['user']['avatar_url']) . '" alt="' . esc_attr($pull['user']['login']) . '" width="24" height="24" style="vertical-align: middle; margin-right: 8px;" />';
$output .= '</a>';
// Pull Request Number (Clickable)
$output .= '<a href="' . esc_url($pull['html_url']) . '" target="_blank">PR #' . esc_html($pull['number']) . '</a>';
// Pull Request Title
$output .= ' - ' . esc_html($pull['title']);
// Calculate the time since the pull request was created
$time_since_creation = time_since_creation($pull['created_at']);
$output .= ' - Since (' . $time_since_creation . ')';
// Make the author name clickable and link to the user's GitHub profile
$output .= ' - <a href="' . esc_url($pull['user']['html_url']) . '" target="_blank">' . esc_html($pull['user']['login']) . '</a>';
$output .= '</li>';
}
$output .= '</ul>';
} else {
$output .= '<p>No open pull requests found.</p>';
}
return $output;
}
add_shortcode('gitsync_pull_requests', 'gitsync_pull_requests_shortcode');
?>