-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwp-github-repos.php
198 lines (137 loc) · 5.97 KB
/
wp-github-repos.php
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
<?php
/**
Plugin Name: Github Repos
Plugin URI: https://alaa.blog
Description: Showing the latest Github Repos in the Sidebar
Author: Alaa Abdulridha
Version: 0.1
Author URI: https://alaa.blog
Text Domain: wp-github-repos
Domain Path: languages/
*/
class WP_Github_Repos {
const CACHE = 'github-repos-';
function __construct() {
load_plugin_textdomain( 'wp-github-repos', false, dirname(plugin_basename(__FILE__)) . '/languages' );
// Register style sheet.
add_action( 'wp_enqueue_scripts', array( $this, 'register_plugin_styles' ) );
}
/**
* Register and enqueue style sheet.
*/
public function register_plugin_styles() {
wp_register_style( 'github_api_style', plugins_url(). '/wp-github-repos/include/color.css' );
wp_enqueue_style( 'github_api_style' );
wp_register_script('color',plugin_dir_url( __FILE__ ) . 'include/color.js',false,'1.0',true);
wp_enqueue_script('color');
}
public function get_github_repos($user, $count = 5, $forked = false) {
$data = '';
$rowsno = 0;
$key = self::CACHE . "$user";
if (false === ( $repos = get_transient( $key ) ) ) {
if(!class_exists('Github_API')){
require dirname(__FILE__) . '/include/github-api.php';
}
$github_api = new Github_API();
$repos = $github_api->get_repos($user,'');
set_transient($key, $repos, 5 * HOUR_IN_SECONDS);
/* For Testing , To delete the cache
delete_transient($key);
delete_transient($data);
delete_transient($latest);
*/
}
$data = '<ul class = "github-repos">';
if(is_array($repos)){
foreach($repos as $latest) {
$rowsno ++;
if ($latest->fork == json_decode($forked))
{
$repo_name = wp_trim_words( $latest->name, 20, '...' );
$data .= '<li class = "github-repos">';
$data .= "<a href = '{$latest->html_url}'> " . $repo_name . '</a> ';
$data .= __(' ', 'wp-github-repos') . ' <pp class = "github-color"> ' . $latest->language . '</a> </pp>';
$data .= __('on', 'wp-github-repos') . ' ' . date("M d, Y ", strtotime($latest->updated_at));
$data .= '</li>';
}
if ($count == $rowsno)
{
break;
}
}
}
$data .= '</ul>';
return $data;
}
}
function myplugin_register_widgets() {
register_widget( 'WP_Github_Repos_Widget' );
}
// Register the widget
add_action( 'init', 'WP_Github_Repos' ); function WP_Github_Repos() { global $wp_github_repos; $wp_github_repos = new WP_Github_Repos(); }
add_action( 'widgets_init', 'myplugin_register_widgets' );
class WP_Github_Repos_Widget extends WP_Widget {
function __construct() {
$widget_ops = array( 'classname' => 'WP_Github_Repos_Widget', 'description' => __('Github Repos for a user', 'wp-github-repos'));
$control_ops = array('id_base' => 'wp-github-repos' );
parent::__construct( 'wp-github-repos', __('WP Github Repos', 'wp-github-repos'), $widget_ops, $control_ops );
}
function widget($args, $instance) {
extract( $args );
$defaults = array( 'title' => __('RECENT GITHUB REPOS', 'wp-github-repos'), 'user' => '', 'forked' => 'false');
$instance = wp_parse_args( (array) $instance, $defaults );
$title = $instance['title'];
$user = $instance['user'];
$forked = $instance['forked'];
$count = absint($instance['count']);
$widget_content = get_github_repos($user, $count, $forked);
if ($widget_content != '') {
echo $before_widget;
echo $before_title;
echo $title;
echo $after_title;
echo $widget_content;
echo $after_widget;
}
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['user'] = strip_tags($new_instance['user']);
$instance['forked'] = strip_tags($new_instance['forked']);
$instance['count'] = absint($new_instance['count']);
return $instance;
}
function form($instance) {
$defaults = array( 'title' => __('RECENT GITHUB REPOS', 'wp-github-repos'), 'user' => '', 'count' => 5, 'forked' =>'false');
$instance = wp_parse_args( (array) $instance, $defaults );
$title = esc_attr($instance['title']);
$user = $instance['user'];
$forked = $instance['forked'];
$count = absint($instance['count']);
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'wp-github-repos'); ?>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label>
</p>
<p>
<label for="<?php echo $this->get_field_id('user'); ?>"><?php _e('User:', 'wp-github-repos'); ?>
<input class="widefat" id="<?php echo $this->get_field_id('user'); ?>" name="<?php echo $this->get_field_name('user'); ?>" type="text" value="<?php echo $user; ?>" /></label>
</p>
<p>
<label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('No of Repos to show:', 'wp-github-repos'); ?>
<input class="widefat" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo $count; ?>" /></label>
</p>
<p>
<label for="<?php echo $this->get_field_id('forked'); ?>"><?php _e('Show forked repos ?(true or false):', 'wp-github-repos'); ?>
<input class="widefat" id="<?php echo $this->get_field_id('forked'); ?>" name="<?php echo $this->get_field_name('forked'); ?>" type="text" value="<?php echo $forked; ?>" /></label>
</p>
<?php
}
}
function get_github_repos($user, $count = 5, $forked) {
global $wp_github_repos;
return $wp_github_repos->get_github_repos($user, $count, $forked);
}
?>