Skip to content

Commit

Permalink
tagging v0.0.3 for release
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-weiner committed Aug 4, 2020
1 parent 2581007 commit 31693fb
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 31 deletions.
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Donate Link: https://paypal.me/michaelw13 <br>
Tags: github-api, github-list, github-table, wordpress-table <br>
Requires at least: 5.0 <br>
Tested up to: 5.5 <br>
Stable tag: 0.0.2 <br>
Stable tag: 0.0.3 <br>
Requires PHP: 7.0.0 <br>
License: GPLv2 or later <br>
License URI: https://www.gnu.org/licenses/gpl-2.0.html <br>
Expand All @@ -18,9 +18,9 @@ The display will default to show all of the publicly available repositories for

### Shortcode Attributes
This shortcode comes with a number of different shortcode modifiers to give you greater control over what is displayed and how it is displayed. All of the possible shortcode modifiers are listed and described below.
* `num` - A string that contains only an integer value to specify how many repositories you would like to display in the table. The default value will use the Github API default value of `30` when the parameter is left blank or when it is not included at all. ***Note: The Github API currently has a maximum of 100 repositories per page, so even if you enter a number of 100 only the first 100 repositories will be displayed.***
* `order` - A string that allows you to choose in what order the repositories are displayed. The possible choices are `asc` and `desc`. The default value will use the Github API default value of `asc` when sorting by `full_name` (default) and `desc` when sorting by anything else.
* `sort` - A string allowing you to specify in what way the repositories being displayed are sorted. Options include `created`, `updated`, `pushed`, and `full_name`. The default value will use the Github API default value of `full_name` when the parameter is left blank or when it is not included at all.
* `num` - A string that contains only an integer value to specify how many repositories you would like to display in the table. The default value of `30` is used when the parameter is left blank or when it is not included at all. This follows the default value set by the Github API. ***Note: The Github API currently has a maximum of 100 repositories per page, so even if you enter a number of 100 only the first 100 repositories will be displayed.***
* `order` - A string that allows you to choose in what order the repositories are displayed. The possible choices are `asc` and `desc`. The default value of `asc` is used when the `sort` parameter is `full_name` (see details on the `sort` parameter below for these scenarios) and `desc` is used for all other scenarios. This follows the default value set by the Github API.
* `sort` - A string allowing you to specify in what way the repositories being displayed are sorted. Options include `created`, `updated`, `pushed`, and `full_name`. The default value of `full_name` is used when the parameter is left blank or when it is not included at all. This follows the default value set by the Github API.
* `user` - A string that takes in the Github username of the user that you would like to display repositories for. The default value of `mike-weiner` is used when the parameter is left blank or when it is not included at all.

You can find more about the Github API and its parameters here: https://developer.github.com/v3/repos/#list-repositories-for-a-user
Expand All @@ -46,6 +46,12 @@ Then navigate to the page you would like to display the table on. Begin editing

You can find your Github username within a Github URL or by selecting your profile picture in the upper right-hand corner of Github's website and copy the name listed under: "Signed in as". ***Do not include any symbols with the Github user name.***

### Why is/are my table(s) not updating on every page refresh?

This plugin makes use of a transient, a form of cache. This means all of your Github repository tables only update every 15 minutes on a page refresh. This keep API calls down and decreases the time it takes for your page to load.

Don't worry! Your table will update! Give it 15 minutes, come back, refresh the page, and you should see the any changes made!

### Why are the repositories being displayed not from the correct user?

Navigate to the page where the table is being displayed. Begin editing the page and examine the `[gitlist user=""]` shortcode on the page. Double check that a Github username is entered within the double quotes after `user=` in the shortcode. Please check that this is the correct user name.
Expand All @@ -56,12 +62,21 @@ Navigate to the page where the table is being displayed. Begin editing the page

## Changelog

### 0.0.3
* Released on August 3, 2020
* Added: Transient cache for all Github display tables you have across your site. Each table will update on the first page refresh every 15 minutes
* Added: A new FAQ question has been added covering the transient cache feature
* Updated: Updated README shortcode attribute descriptions
* Fixed: Fixed a typo in changelog for v0.0.2
* Edited: github-repo-listing.php
* Edited: README.md

### 0.0.2
* Released on August 3, 2020
* Added: Shortcode attribute `num` allows the user to specify how many repositories should be displayed. (Note: Current limit from Github API is 100)
* Added: Shortcode attribute `order` allows the user to specify if the repository table should be displayed in `asc` or `desc` order
* Added: Shortcode attribute `sort` allows the user to specify if repositories should be sorted by `created` (date), `updated` (date), `pushed` (date) or by `full_name` (repo name)
* Modifed: Shortcode attribute `git-user` has been renamed to `user` for clarity
* Modified: Shortcode attribute `git-user` has been renamed to `user` for clarity
* Edited: github-repo-listing.php
* Edited: README.md

Expand All @@ -71,6 +86,9 @@ Navigate to the page where the table is being displayed. Begin editing the page

## Upgrade Notice

### 0.0.3
This update introduces a 15 minute cache for displaying updates on your Github display table. This means your Github table will update on the first page refresh after 15 minutes. This was accomplished through using WordPress transients and will help to keep your page load times down.

### 0.0.2
This release gives the user more control of how the repositories are displayed in the table and how they are sorted via new `order` and `sort` attributes for the `[gitlist]` shortcode. Other shortcodes such as `num` and `user` have been added or modified. Please see the README for information about all of the new options for these shortcode attributes available to you!

Expand Down
104 changes: 78 additions & 26 deletions github-repo-listing.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description: Display a table with information about all of the public repositories for a specific Github user.
* Author: Michael Weiner
* Author URI: https://michaelweiner.org/
* Version: 0.0.2
* Version: 0.0.3
* License: GPL2+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
Expand All @@ -23,18 +23,47 @@ function mw_custom_github_listing_styles() {
}
add_action( 'wp_enqueue_scripts', 'mw_custom_github_listing_styles' );

// Create function to be called upon [gitlist] shortcode
function mw_git_display_function( $attr ) {
# Create function to handle calls for Github API transient
# Used by mw_git_display_function($attr)
function mw_github_api_call_transient($mw_transient_id, $mw_api_url) {
# establish a new transient and assign it to the return value of trying to get it
$mw_transient = get_transient($mw_transient_id);

if (!empty($mw_transient)) { # if the transient exists, we just need to return it
return $mw_transient;
} else {
# go to Github, grab json data from Github, and decode it
$mw_github_data = wp_remote_get(esc_html($mw_api_url));

# break early if JSON request ends in error
if( is_wp_error($mw_github_data)) {
return ("<html><div class='mw-github-container'>We're sorry. There appeared to be an error. Please re-evaluate your shortcode.</div></<html>"); // return an empty array
}

# read decoded JSON data into an array
$mw_github_data_arr = json_decode(wp_remote_retrieve_body($mw_github_data), true);

# save API response for 15 minutes
set_transient($mw_transient_id, $mw_github_data_arr, 15 * MINUTE_IN_SECONDS);

return($mw_github_data_arr);
}
}

# Create function to be called upon [gitlist] shortcode
function mw_git_display_function($attr) {
// define function prefix
$mw_prefix = "mw-";

# array to store valid parameter values for 'order' attribute for Github API
# array to store valid parameter values for 'order' attribute of Github API
$mw_github_direction_params = array("asc", "desc");

# array to store valid parameter values for 'sort' attribute for Github API
# array to store valid parameter values for 'sort' attribute of Github API
$mw_github_sort_params = array("created", "updated", "pushed", "full_name");

# array to store shortcode parameters
$mw_git_display_function = shortcode_atts( array(
'num' => '',
'num' => '30',
'order' => '',
'sort' => '',
'user' => 'mike-weiner',
Expand All @@ -54,35 +83,47 @@ function mw_git_display_function( $attr ) {
# establish JSON request URL
$mw_github_api_url = "https://api.github.com/users/" . $mw_github_user_name . "/repos";

# check for valid order parameter
if (in_array(strtolower($mw_git_display_function['order']), $mw_github_direction_params)) {
$mw_github_api_url = $mw_github_api_url . "?direction=" . $mw_git_display_function['order'];
}

# check for valid sort parameter
if (in_array(strtolower($mw_git_display_function['sort']), $mw_github_sort_params)) {
$mw_github_api_url = $mw_github_api_url . "?sort=" . $mw_git_display_function['sort'];
}

# check for valid per_page parameter
if (is_numeric($mw_git_display_function['num'])) {
$mw_github_api_url = $mw_github_api_url . "?per_page=" . abs(intval($mw_git_display_function['num']));
# establish placeholder for sort param for use in transient id
$mw_url_sort_placeholder = $mw_git_display_function['sort'];
} else {
$mw_url_sort_placeholder = "full_name";
}

# go to Github, grab json data from Github, and decode it
$mw_github_data = $json = wp_remote_get(esc_html($mw_github_api_url));

# break early if JSON request ends in error
if( is_wp_error($mw_github_data)) {
return "<html><div class='mw-github-container'>We're sorry. There appeared to be an error. Please re-evaluate your shortcode.</div></<html>"; // Bail early
# check for valid order parameter
if (in_array(strtolower($mw_git_display_function['order']), $mw_github_direction_params)) {
$mw_github_api_url = $mw_github_api_url . "?direction=" . $mw_git_display_function['order'];

# establish placeholder for order param for use in transient id
$mw_url_order_placeholder = $mw_git_display_function['order'];

} else {
# set placeholder for order param based on default values specified by Github API
# see: https://developer.github.com/v3/repos/#parameters
if ($mw_url_sort_placeholder == "full_name") {
$mw_url_order_placeholder = "asc";
} else {
$mw_url_order_placeholder = "desc";
}
}

# create transient id for this particular shortcode call
$mw_github_transient_id = $mw_prefix . "git-list-" . esc_html($mw_git_display_function['user']) . "-" . esc_html($mw_url_sort_placeholder) . "-" . esc_html($mw_url_order_placeholder) . "-" . esc_html(abs(intval($mw_git_display_function['num'])));

# read decoded JSON data into an array
$mw_github_data_dict = json_decode(wp_remote_retrieve_body($mw_github_data), true);
# grab/create transient
$mw_github_transient = mw_github_api_call_transient($mw_github_transient_id, esc_html($mw_github_api_url));

# check if array is empty -> if so, return early
if (empty($mw_github_data_dict)) {
# check if transient is empty -> if so, return early
if (empty($mw_github_transient)) {
return "<html><div class='mw-github-container'>We could not find any public repositories for this user!</div></html>";
}

# check if transient is a String (JSON error ocurred) -> if so, return early
if (is_string($mw_github_transient)) {
return ($mw_github_transient);
}

# generate HTML
Expand All @@ -104,8 +145,17 @@ function mw_git_display_function( $attr ) {
# append table header row HTML to output
$mw_html_output = $mw_html_output . $mw_html_table_header_row;

# initialize a count variable for the num attribute of the shortcode
$mw_repo_count = 0;

# add row to HTML table data for every repo in github_data_dict
foreach ($mw_github_data_dict as $mw_repo){
foreach ($mw_github_transient as $mw_repo){

# check that we have not gone over the number of repos the user wants to list & that we are under 100
if (!($mw_repo_count < abs(intval($mw_git_display_function['num']))) && ($mw_repo_count < 100)) {
break;
}

$mw_html_table_row = "<tr class='mw-github-data-row'>"; # open HTML table row tag

foreach ($mw_html_th_cols as $mw_json_param => $mw_label) { # add every column being requested in html_th_cols
Expand All @@ -117,6 +167,8 @@ function mw_git_display_function( $attr ) {
}

$mw_html_output = $mw_html_output . $mw_html_table_row . "</tr>"; # close HTML table row tag

$mw_repo_count += 1; # increment repo count
}

# close remaining HTML tags
Expand Down

0 comments on commit 31693fb

Please sign in to comment.