This repository was archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers.php
More file actions
228 lines (178 loc) · 6.53 KB
/
helpers.php
File metadata and controls
228 lines (178 loc) · 6.53 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
<?php
namespace WPGHPL;
/* HELPERS FOR PRESENTING MARKUP */
function format_issues($issues, $body = false)
{
if (empty($issues)) {
return;
}
#load necessary scripts if we're toggling
if (strtolower($body) === 'toggle') {
wp_enqueue_script('toggle');
}
$return = '<div class="issues-list">';
foreach ($issues as $issue) {
$return .= format_issue($issue, $body);
}
$return .= '</div>';
return $return;
}
function format_issue($issue, $body)
{
$return = '<div class="issue">';
$return .= '<h3 class="issue__title">'.htmlentities($issue['title']).' #'.$issue['number'].'</h3>';
$return .= build_labels($issue['labels']);
$return .= '<ul class="issue__details">';
$return .= '<li>State: '.$issue['state'].'</li>';
$return .= (!empty($issue['closed_at'])) ? '<li>Closed: '.wpghpl_formatdate($issue['closed_at']).'</li>' : null;
$return .= (!empty($issue['assignee'])) ? '<li>Assigned to: '.$issue['assignee']['login'].'</li>' : null;
$return .= (!empty($issue['milestone'])) ? '<li><span class="issue__details__milestone '.(($issue['milestone']['closed_at']) ? 'issue__details__milestone--closed' : null).'">Milestone: '.$issue['milestone']['title'].(($issue['milestone']['description']) ? ': '.$issue['milestone']['description'] : null).'</span></li>' : null;
$return .= '</ul>';
if ($body === strtolower('toggle') && !empty($issue['body'])) {
$return .= '<div class="issue__toggle-wrap">';
$return .= '<div class="issue__body">'.convert_markdown($issue['body'], true).'</div>';
$return .= '<a href="#" form-toggle-btn>↓ More</a>';
$return .= '</div>';
} elseif ($body) {
$return .= '<div class="issue__body">'.convert_markdown($issue['body'], true).'</div>';
}
$return .= '</div><!-- .issue -->';
return $return;
}
use League\CommonMark\CommonMarkConverter;
function convert_markdown($string, $linebreaks = null)
{
$newstring = $string;
if ($linebreaks) {
$newstring = nl2br($newstring);
}
$converter = new CommonMarkConverter();
$newstring = $converter->convertToHtml($newstring);
return $newstring;
}
function build_labels($labels)
{
$return = '';
foreach ($labels as $label) {
$return .= build_label($label);
}
return $return;
}
function build_label($label)
{
$return = '';
$return .= '<span class="issue__label" ';
$return .= 'style="background-color:#'.$label['color'].';';
$return .= (needs_white_text($label['color'])) ? ' color:#FFF;' : null;
$return .= '" '; #closing the style attribute
$return .= '>';
$return .= $label['name'];
$return .= '</span>';
return $return;
}
/**
* Checks if a color is dark enough to warrant white text over it.
*
* @param $colorhex (string) A hex color value
*
* @return (bool) TRUE if white text should be used
*/
function needs_white_text($hexcolor)
{
$r = hexdec(substr($hexcolor, 0, 2));
$g = hexdec(substr($hexcolor, 2, 2));
$b = hexdec(substr($hexcolor, 4, 2));
$yiq = (($r * 299) + ($g * 587) + ($b * 114)) / 1000;
return ($yiq >= 128) ? false : true;
}
function build_search_form($placeholder = null)
{
$value = (!empty($_GET['gh_searchterm'])) ? $_GET['gh_searchterm'] : null;
$return = '';
$return .= '<form class="issue-searchform" method="GET" action="'.get_the_permalink().'">';
$return .= '<input type="text" name="gh_searchterm" value="'.$value.'" placeholder="'.$placeholder.'" /> ';
$return .= '<input type="submit" value="Search" /></form>';
return $return;
}
function format_milestones($milestones)
{
if (empty($milestones)) {
return;
}
$return = '<div class="milestones-list">
';
foreach ($milestones as $milestone) {
$return .= '<h3 class="milestone__title">'.$milestone['title'].'</h3>
';
$return .= '<ul class="milestone__details">
';
// $return .= '<li>State: '.$milestone['state'].'</li>';
$return .= ($milestone['due_on']) ? '<li>Due: '.wpghpl_formatdate($milestone['due_on']).'</li>' : null;
$return .= '<li>Open issues: '.$milestone['open_issues'].' Closed issues: '.$milestone['closed_issues'].'</li>';
$return .= '<li>Issues:<br/>'.print_milestone_issues($milestone['issues']).'</li>';
$return .= '
</ul>';
}
$return .= '
</div>';
return $return;
}
function print_milestone_issues($issues)
{
if (!$issues) {
return;
}
$return = '<ul class="milestone__issues-list">
';
foreach ($issues as $issue) {
$class = ($issue['state'] == 'open') ? 'open' : 'closed';
$return .= '<li class="milestone__issue-item '.$class.'">'.htmlentities($issue['title']).' ';
$return .= build_labels($issue['labels']);
$return .= '</li>
';
}
$return .= '</ul>
';
return $return;
}
function prepend_page_count($string, $Github, $on_page_count)
{
$start = ($Github->per_page) * ($Github->page - 1) + 1;
$end = $start + $on_page_count - 1;
$count = "<div class='gh-result-count'>Showing: {$start} - {$end}</div>";
$newstring = $count.$string;
return $newstring;
}
/*
* Append previous or next page links to returned string (if appropriate)
* @param $string (str) the input string
* @param $Client (Github) an instance of the Github API class to check for pagination
* @return string
*/
function append_page_links($string, $Client)
{
$query = (!empty($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : null;
#remove previous page query param
if (!empty($_GET['gh_page'])) {
$query = str_replace('gh_page='.$_GET['gh_page'], '', $query);
}
$page_url = get_the_permalink();
if ($query) {
$page_url .= '?'.$query;
}
$punc = ($query) ? '&' : '?';
$prev_url = $page_url.$punc.'gh_page='.($Client->page - 1);
$next_url = $page_url.$punc.'gh_page='.($Client->page + 1);
#add either next or prev link
$pagination = '';
$pagination .= ($Client->has_prev_page) ? '<a class="gh-pagination__link gh-pagination__link--prev" href="'.$prev_url.'">← Previous</a>' : null;
$pagination .= ($Client->has_next_page) ? '<a class="gh-pagination__link gh-pagination__link--next" href="'.$next_url.'">Next →</a>' : null;
# wrap either/both with div
$newstring = ($pagination) ? $string.'<div class="gh-pagination">'.$pagination.'</div>' : $string;
return $newstring;
}
function wpghpl_formatdate($data_str, $format = null)
{
$format = ($format) ? $format : 'F j, Y';
return date_i18n($format, strtotime($data_str));
}