This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathacknowledge-me.php
133 lines (111 loc) · 3.92 KB
/
acknowledge-me.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
<?php
/*
* Plugin Name: Acknowledge Me
* Description: Shows contributors to a Github repo
* Version: 0.2.3
* Author: Pods Framework Team
* Author URI: http://pods.io
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
/**
* A Community Building Tool By Pods
*
* Based on the code for the underscores.me site: https://github.com/Automattic/underscores.me/, which is copyright Automattic, hugobaeta, kovshenin and GPL licensed.
*
* This plugin copyright 2015 Pods Foundation LLC. Licensed under the terms of the GPL v2 or later.
*/
/**
* Output the contributors
*
* @since 0.0.1
*
* @param string $owner Owner (username or organization name) for repo
* @param string $repo Repo name
* @param bool|string $header_text Optional text to output before the contributors
* @param int $total Optional. Total number of contributors to show. Default is 100.
*
* @returns string Rendered output
*/
function acknowledge_me_display( $owner, $repo, $header_text = false, $total = 100 ) {
$contributors = acknowledge_me_get( $owner, $repo, $total );
if ( ! is_array( $contributors ) || empty( $contributors ) ) {
return;
}
$out[] =
'<section id="contribute">
<div class="wrap">';
if ( is_string( $header_text ) ) {
$out[] = sprintf( '<div id="header-text">%1s</div>', wp_kses_post( $header_text ) );
}
$out[] = '<ul id="team">';
foreach ( $contributors as $contributor ) {
$title = sprintf( '@%s with %d %s', esc_html( $contributor->login ), absint( $contributor->contributions ), esc_html( _n( 'contribution', 'contributions', $contributor->contributions ) ) );
$url = sprintf( 'http://github.com/%s', $contributor->login );
$avatar_url = add_query_arg( 's', 280, $contributor->avatar_url );
$avatar_url = add_query_arg( 'd', esc_url_raw( 'https://secure.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s=280' ), $avatar_url );
$out[] = sprintf( '<li class="contributor-%1s"><a title="%2s" href="%3s"><img class="avatar" src="%4s" /></a></li>', esc_attr( $contributor->login ), esc_attr( $title ), esc_url( $url ), esc_url( $avatar_url ) );
}
$out[] = '</ul><!-- #team -->
</div><!-- .wrap -->
</section><!-- #contribute -->';
if ( is_array( $out ) ) {
return implode( '', $out );
}
}
/**
* Get contributors
*
* @since 0.0.1
*
* @param string $owner Owner (username or organization name) for repo
* @param string $repo Repo name
* @param bool|string $header_text Optional text to output before the contributors
* @param int $total Optional. Total number of contributors to show. Default is 100.
*
* @return array|mixed
*/
function acknowledge_me_get( $owner, $repo, $total = 100 ) {
$transient_key = md5( implode( '_', array( __FUNCTION__, $owner, $repo, $total ) ) );
$contributors = get_transient( $transient_key );
if ( false !== $contributors ) {
return $contributors;
}
$url = "https://api.github.com/repos/{$owner}/{$repo}/contributors?per_page={$total}";
$response = wp_remote_get( $url );
if ( is_wp_error( $response ) ) {
return array();
}
$contributors = json_decode( wp_remote_retrieve_body( $response ) );
if ( ! is_array( $contributors ) ) {
return array();
}
set_transient( $transient_key, $contributors, HOUR_IN_SECONDS );
return (array) $contributors;
}
/**
* Adds a shortcode for this
*
* @since 0.0.1
*/
add_shortcode( 'acknowledge_me', 'acknowledge_me_shortcode' );
function acknowledge_me_shortcode( $atts ) {
$atts = shortcode_atts( array(
'owner' => 'pods-framework',
'repo' => 'pods',
'header_text' => '',
'total' => '100'
), $atts, 'acknowledge_me' );
return acknowledge_me_display( $atts[ 'owner' ], $atts[ 'repo' ], $atts[ 'header_text' ], $atts[ 'total'] );
}
/**
* Add CSS for this
*
* @since 0.0.1
*/
add_action( 'wp_enqueue_scripts', 'acknowledge_me_css' );
function acknowledge_me_css() {
if ( ! is_admin() ) {
wp_enqueue_style( 'acknowledge_me', plugins_url( 'acknowledge.css', __FILE__ ) );
}
}