forked from tk-sls.de/linuxfoo-gitlab
258 lines
9.3 KiB
PHP
258 lines
9.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
Plugin Name: Gitea REST API Shortcodes
|
|
Plugin URI: http://tk-sls.de/ref/gitea-list-commits
|
|
Description: Summary: Embed list of most recent commits to a Gitea project from a Gitea instance's public REST API. Example: Add [gitea-list-commits url=https://tk-sls.de/git/ project_owner=tk-sls.de project_repo=linuxfoo-gitea commits="all" max=5 releases="latest"] for a list of at most the 5 latest commits to that project, followed by a link to the latest release of the project (if any). To disable the list of commits, set commits="none". To disable printing the author name of a commit, set author="none". To generate a list of all releases, set releases="all". To disable the list of releases, omit the "releases" attribute or set releases="none".
|
|
Author: Tilman Kranz
|
|
Version: 1.4
|
|
Author URI: https://tk-sls.de
|
|
*/
|
|
|
|
class LinuxfooGitea {
|
|
public function __construct() {
|
|
}
|
|
|
|
static function load_textdomain() {
|
|
load_plugin_textdomain('linuxfoo-gitea', false, dirname(plugin_basename(__FILE__)).'/lang');
|
|
}
|
|
|
|
static function css() {
|
|
wp_enqueue_style('linuxfoo_gitea_css', plugins_url('styles.css', __FILE__ ), '', '1.4' );
|
|
}
|
|
|
|
static function error ( $msg ) {
|
|
return
|
|
'<span class="error">'.
|
|
$msg.
|
|
'</span>';
|
|
}
|
|
|
|
static function project_header($atts, $project) {
|
|
return
|
|
'<span class="project_header">'.
|
|
__('Gitea project', 'linuxfoo-gitea').' '.
|
|
'<a href="'.$project->html_url.'">'.$project->name.'</a>'.
|
|
'</span>';
|
|
}
|
|
|
|
static function format_since($since) {
|
|
preg_match('/^([0-9]+)\s+(.*)$/', $since, $m);
|
|
$num = $m[1];
|
|
$unit = $m[2];
|
|
|
|
if($unit=='days') {
|
|
$unit = __('days', 'linuxfoo-gitea');
|
|
}
|
|
elseif($unit=='months') {
|
|
$unit = __('months', 'linuxfoo-gitea');
|
|
}
|
|
elseif($unit=='years') {
|
|
$unit = __('years', 'linuxfoo-gitea');
|
|
}
|
|
|
|
/* translators: %d: count, %s unit of time interval*/
|
|
return sprintf(__('since %d %s ago', 'linuxfoo-gitea'), $num, $unit);
|
|
}
|
|
|
|
static function format_commit($atts, $commit) {
|
|
preg_match('/^....-..-../', $commit->committed_date, $m);
|
|
|
|
$result = $m[0].' ';
|
|
|
|
if($atts['author']==null || $atts['author']!='none')
|
|
$result .=
|
|
__('by', 'linuxfoo-gitea').' '.$commit->commit->author->name;
|
|
|
|
$result .=
|
|
': '.
|
|
'<a href="'.$commit->html_url.'">'.substr($commit->sha, 0, 8).'</a> '.
|
|
htmlspecialchars(explode("\n", $commit->message)[0], ENT_NOQUOTES|ENT_HTML5|ENT_SUBSTITUTE, 'UTF-8', FALSE);
|
|
|
|
return $result;
|
|
}
|
|
|
|
static function format_commit_stats($atts, $commits_count, $branch) {
|
|
return
|
|
(
|
|
($commits_count==0)
|
|
? __('No commits', 'linuxfoo-gitea')
|
|
/* translators: %d: maximum number of commits displayed */
|
|
: sprintf(__('Last %d commits', 'linuxfoo-gitea'), $commits_count)
|
|
).
|
|
(is_null($branch) ? '' : ' '.__('in branch', 'linuxfoo-gitea').' "'.$branch.'"').
|
|
(is_null($atts['since']) ? '' : ' '.self::format_since($atts['since'])).':';
|
|
}
|
|
|
|
static function commits_list($atts, $project_url) {
|
|
$since = null;
|
|
|
|
if($atts['commits']=='none') {
|
|
return '';
|
|
}
|
|
elseif(!is_null($atts['commits']) && $atts['commits']!='all') {
|
|
return self::error(__('Invalid value for parameter "commits".', 'linuxfoo-gitea'));
|
|
}
|
|
|
|
$out = '';
|
|
|
|
$commits_url = $project_url.'/commits?stat=false&verification=false&files=false';
|
|
|
|
if(!is_null($atts['ref_name'])) {
|
|
$commits_url .= '&sha='.$atts['ref_name'];
|
|
$branch = $atts['ref_name'];
|
|
}
|
|
elseif(!is_null($project->default_branch)) {
|
|
$commits_url .= '&sha='.$project->default_branch;
|
|
$branch = $project->default_branch;
|
|
}
|
|
else {
|
|
$branch = null;
|
|
}
|
|
|
|
$commits_json = file_get_contents($commits_url);
|
|
|
|
if(is_null($commits_json)) {
|
|
$out .= self::error(__('Commits URL not reachable.', 'linuxfoo-gitea'));
|
|
}
|
|
else {
|
|
$commits = json_decode($commits_json);
|
|
|
|
if(is_null($commits)) {
|
|
$out .= self::error(__('Commits information not readable.', 'linuxfoo-gitea'));
|
|
}
|
|
else {
|
|
$commits_count = count($commits);
|
|
$commits_count = is_null($atts['max']) ? $commits_count : min($commits_count, $atts['max']);
|
|
$commits = array_slice($commits, 0, $commits_count);
|
|
|
|
$out .=
|
|
'<span class="stats">'.
|
|
self::format_commit_stats($atts, $commits_count, $branch).
|
|
'</span>';
|
|
|
|
if($commits_count>0) {
|
|
$out .= '<span class="commits" role="list">';
|
|
|
|
foreach($commits as $commit) {
|
|
$out .= '<span class="commit">'.self::format_commit($atts, $commit).'</span>';
|
|
}
|
|
|
|
$out .= '</span>';
|
|
}
|
|
}
|
|
}
|
|
|
|
return $out;
|
|
}
|
|
|
|
static function releases_list($atts, $project_url) {
|
|
$out = '';
|
|
|
|
if(is_null($atts['releases']) || $atts['releases']=='none') {
|
|
$out .= '';
|
|
}
|
|
elseif(!preg_match('/^(all|latest)$/', $atts['releases'])) {
|
|
$out .= self::error(__('Invalid value for parameter "releases".', 'linuxfoo-gitea'));
|
|
}
|
|
else {
|
|
$releases_url = $project_url.'/releases?';
|
|
$releases_json = file_get_contents($releases_url);
|
|
|
|
if(is_null($releases_json)) {
|
|
$out .= self::error(__('Releases URL not reachable.', 'linuxfoo-gitea'));
|
|
}
|
|
else {
|
|
$releases = json_decode($releases_json);
|
|
|
|
if(is_null($releases)) {
|
|
$out .= self::error(__('Releases information not readable.', 'linuxfoo-gitea'));
|
|
}
|
|
else {
|
|
if(count($releases)==0) {
|
|
$out .=
|
|
'<span class="releases">'.
|
|
__('This project has currently no releases.', 'linuxfoo-gitea').
|
|
'</span>';
|
|
}
|
|
elseif($atts['releases']=='latest') {
|
|
$out .=
|
|
'<span class="releases">'.
|
|
__('Latest release', 'linuxfoo-gitea').': '.
|
|
'<a href="'.$releases[0]->html_url.'">'.$releases[0]->name.'</a>'.
|
|
'</span>';
|
|
}
|
|
elseif($atts['releases']=='all') {
|
|
$out .=
|
|
'<span class="releases">'.
|
|
__('Releases', 'linuxfoo-gitea').': '.
|
|
'</span>'.
|
|
'<span class="releases" role="list">';
|
|
|
|
foreach($releases as $release) {
|
|
$out .=
|
|
'<span class="release">'.
|
|
'<a href="'.$release->html_url.'">'.$release->name.'</a>'.
|
|
'</span>';
|
|
}
|
|
|
|
$out .= '</span>';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $out;
|
|
}
|
|
|
|
static function list_commits($atts, $content, $tag) {
|
|
return self::show_project($atts, $content, $tag);
|
|
}
|
|
|
|
static function show_project($atts, $content, $tag) {
|
|
global $post;
|
|
|
|
$out = '<span class="linuxfoo_gitea">';
|
|
|
|
if(
|
|
is_null($atts['url']) ||
|
|
is_null($atts['project_owner']) ||
|
|
is_null($atts['project_repo'])
|
|
) {
|
|
$out .= self::error(__('Required parameter missing.', 'linuxfoo-gitea'));
|
|
}
|
|
elseif(!filter_var($atts['url'], FILTER_VALIDATE_URL)) {
|
|
$out .= self::error(__('Invalid value for parameter "url".', 'linuxfoo-gitea'));
|
|
}
|
|
else {
|
|
$project_url = $atts['url'].'/api/v1/repos/'.$atts['project_owner'].'/'.$atts['project_repo'];
|
|
$project_json = file_get_contents($project_url);
|
|
|
|
if(is_null($project_json)) {
|
|
$out .= self::error(__('Project URL not reachable.', 'linuxfoo-gitea'));
|
|
}
|
|
else {
|
|
$project = json_decode($project_json);
|
|
|
|
if(is_null($project)) {
|
|
$out .= self::error('Project information not readable.');
|
|
}
|
|
else {
|
|
$out .= self::project_header($atts, $project);
|
|
$out .= self::commits_list($atts, $project_url);
|
|
$out .= self::releases_list($atts, $project_url);
|
|
}
|
|
}
|
|
}
|
|
|
|
$out .= '</span>';
|
|
|
|
return $out;
|
|
}
|
|
}
|
|
|
|
add_action('plugins_loaded', 'LinuxfooGitea::load_textdomain');
|
|
add_action('wp_enqueue_scripts', 'LinuxfooGitea::css' );
|
|
add_shortcode('gitlab-list-commits', 'LinuxfooGitea::list_commits');
|
|
add_shortcode('gitlab-show-project', 'LinuxfooGitea::show_project');
|