-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathwp-github-activity.php
More file actions
237 lines (219 loc) · 6.71 KB
/
wp-github-activity.php
File metadata and controls
237 lines (219 loc) · 6.71 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
<?php
/*
Plugin Name: WP GitHub Activity
Plugin URI: http://crowdfavorite.com/wordpress/plugins/
Description: Show your recent GitHub activity on your WordPress site via shortcode or sidebar widget.
Version: 1.0
Author: Crowd Favorite
Author URI: http://crowdfavorite.com
*/
class cf_github_activity {
public static $instance = null;
// has the CSS and JS already been output on this page load?
var $css_output = false;
var $js_output = false;
public static function instance() {
if (is_null(self::$instance)) {
self::$instance = new cf_github_activity;
}
return self::$instance;
}
}
function cf_github_activity($username, $excluded = array(), $count = 10, $include_css = true) {
$feed = fetch_feed('https://github.com/'.$username.'.atom');
if (is_wp_error($feed)) {
return '';
}
// disable the default CSS or JS using these filters
cf_github_activity::instance()->css_output = apply_filters('cf_github_activity_css_output', cf_github_activity::instance()->css_output);
cf_github_activity::instance()->js_output = apply_filters('cf_github_activity_js_output', cf_github_activity::instance()->js_output);
$html = '';
if ($include_css && !cf_github_activity::instance()->css_output) {
$html .= cf_github_activity_css();
cf_github_activity::instance()->css_output = true;
}
$i = 0;
foreach ($feed->get_items() as $item) {
if ($i == $count) {
break;
}
$content = $item->data['child']['http://www.w3.org/2005/Atom']['content'][0]['data'];
$skip = false;
if (!empty($excluded)) {
foreach ($excluded as $exclude) {
if (strpos($content, '<!-- '.$exclude.' -->') !== false) {
$skip = true;
break;
}
}
}
if (!$skip) {
$html .= '<div class="github-activity-item">'.$content.'</div>';
$i++;
}
}
if (!cf_github_activity::instance()->js_output) {
$html .= cf_github_activity_js_fix_urls();
cf_github_activity::instance()->js_output = true;
}
return $html;
}
function cf_github_activity_css() {
?>
<style>
.github-activity-item {
border-bottom: 1px solid #eee;
font-size: 13px;
padding: 10px 0;
}
.github-activity-item:first-of-type {
padding-top: 0;
}
.github-activity-item a {
white-space: nowrap;
}
.github-activity-item .gravatar {
display: none;
}
.github-activity-item img {
height: 20px;
margin-right: 3px;
width: 20px;
}
.github-activity-item .time {
color: #999;
font-size: 12px;
line-height: 1.2em;
}
.github-activity-item .commits {
font-size: 12px;
}
.github-activity-item .commits ul,
.github-activity-item .commits ul li {
list-style: none;
margin: 0;
padding: 0;
}
.github-activity-item .commits ul li {
line-height: 20px;
padding-left: 10px;
}
.github-activity-item .commits ul li.more {
font-size: 12px;
padding: 0;
}
.github-activity-item .message,
.github-activity-item .message blockquote {
display: inline;
}
.github-activity-item .message blockquote {
border: 0;
font-size: 13px;
font-style: normal;
margin: 0;
padding-left: 3px;
}
</style>
<?php
}
function cf_github_activity_js_fix_urls() {
ob_start();
?>
<script>
jQuery(function($) {
$('.github-activity-item a').each(function() {
var href = $(this).attr('href');
if (href.indexOf('https://') == -1) {
$(this).attr('href', 'https://github.com' + href);
}
});
});
</script>
<?php
return ob_get_clean();
}
function cf_github_activity_shortcode($args = array()) {
$args = shortcode_atts(array(
'username' => null,
'excluded' => null,
'count' => 10
), $args);
// if no username, get out.
if (empty($args['username'])) {
return '';
}
$excluded = (!empty($args['excluded']) ? explode(',', $args['excluded']) : array());
$excluded = array_unique(array_map('trim', $excluded));
return cf_github_activity($args['username'], $excluded, $args['count']);
}
add_shortcode('github_activity', 'cf_github_activity_shortcode');
class CF_GitHub_Activity_Widget extends WP_Widget {
function __construct() {
$title = __('GitHub Activity', 'github-activity');
$desc = __('Show recent GitHub Activity.', 'github-activity');
// widget actual processes
parent::__construct(
'github-activity-widget',
$title,
array(
'classname' => 'github-activity-widget',
'description' => $desc
)
);
}
function form($instance) {
$defaults = array(
'title' => __('GitHub Activity', 'github-activity'),
'username' => '',
'excluded' => '',
'count' => 10,
);
foreach ($defaults as $k => $v) {
if (!isset($instance[$k])) {
$instance[$k] = $v;
}
}
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'github-activity'); ?></label>
<input type="text" name="<?php echo $this->get_field_name('title'); ?>" id="<?php echo $this->get_field_id('title'); ?>" value="<?php echo esc_attr($instance['title']); ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id('username'); ?>"><?php _e('Username', 'github-activity'); ?></label>
<input type="text" name="<?php echo $this->get_field_name('username'); ?>" id="<?php echo $this->get_field_id('username'); ?>" value="<?php echo esc_attr($instance['username']); ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('How Many Items?', 'github-activity'); ?></label>
<input type="text" size="3" name="<?php echo $this->get_field_name('count'); ?>" id="<?php echo $this->get_field_id('count'); ?>" value="<?php echo esc_attr($instance['count']); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('excluded'); ?>"><?php _e('Activity Types to Exclude', 'github-activity'); ?></label>
<input type="text" name="<?php echo $this->get_field_name('excluded'); ?>" id="<?php echo $this->get_field_id('excluded'); ?>" value="<?php echo esc_attr($instance['excluded']); ?>" />
</p>
<p class="help"><?php _e('Comma separated - example: "issue_comment, watch". Types are the leading HTML comment in the output HTML.', 'github-activity'); ?></p>
<?php
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['username'] = strip_tags($new_instance['username']);
if (!($count = intval($new_instance['count']))) {
$count = 1;
}
$instance['count'] = $count;
$instance['excluded'] = strip_tags($new_instance['excluded']);
return $instance;
}
function widget($args, $instance) {
extract($args);
$excluded = (!empty($instance['excluded']) ? explode(',', $instance['excluded']) : array());
$excluded = array_unique(array_map('trim', $excluded));
echo $before_widget.$before_title.$instance['title'].$after_title;
echo cf_github_activity($instance['username'], $excluded, $instance['count']);
echo $after_widget;
}
static function register() {
register_widget('CF_GitHub_Activity_Widget');
}
}
add_action('widgets_init', array('CF_GitHub_Activity_Widget', 'register'));