2021-11-27 03:20:48 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2023-09-23 14:36:46 +02:00
|
|
|
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_id=42 commits="all" since="3 month" max=5 releases="latest"] for a list of at most 5 commits to that project that were made since at most 3 months ago, followed by alink 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".
|
2021-11-27 03:20:48 +01:00
|
|
|
Author: Tilman Kranz
|
2021-11-27 20:04:17 +01:00
|
|
|
Version: 1.4
|
2021-11-27 03:20:48 +01:00
|
|
|
Author URI: https://tk-sls.de
|
|
|
|
*/
|
|
|
|
|
|
|
|
class LinuxfooGitlab {
|
2022-08-28 13:02:39 +02:00
|
|
|
public function __construct() {
|
|
|
|
}
|
2021-11-27 03:20:48 +01:00
|
|
|
|
2021-11-27 12:03:15 +01:00
|
|
|
static function load_textdomain() {
|
2023-09-23 14:36:46 +02:00
|
|
|
load_plugin_textdomain('linuxfoo-gitea', false, dirname(plugin_basename(__FILE__)).'/lang');
|
2021-11-27 12:03:15 +01:00
|
|
|
}
|
|
|
|
|
2021-11-27 03:20:48 +01:00
|
|
|
static function css() {
|
2023-09-23 14:36:46 +02:00
|
|
|
wp_enqueue_style('linuxfoo_gitea_css', plugins_url('styles.css', __FILE__ ), '', '1.4' );
|
2021-11-27 03:20:48 +01:00
|
|
|
}
|
|
|
|
|
2022-08-28 13:02:39 +02:00
|
|
|
static function error ( $msg ) {
|
2021-11-27 03:20:48 +01:00
|
|
|
return
|
2021-11-27 20:04:17 +01:00
|
|
|
'<span class="error">'.
|
|
|
|
$msg.
|
|
|
|
'</span>';
|
2021-11-27 03:20:48 +01:00
|
|
|
}
|
|
|
|
|
2021-11-30 01:48:11 +01:00
|
|
|
static function project_header($atts, $project) {
|
|
|
|
return
|
|
|
|
'<span class="project_header">'.
|
2023-09-23 14:36:46 +02:00
|
|
|
__('Gitea project', 'linuxfoo-gitea').' '.
|
2023-09-23 15:26:08 +02:00
|
|
|
'<a href="'.$project->html_url.'">'.$project->name.'</a>'.
|
2021-11-30 01:48:11 +01:00
|
|
|
'</span>';
|
|
|
|
}
|
|
|
|
|
2021-11-27 12:03:15 +01:00
|
|
|
static function format_since($since) {
|
|
|
|
preg_match('/^([0-9]+)\s+(.*)$/', $since, $m);
|
|
|
|
$num = $m[1];
|
|
|
|
$unit = $m[2];
|
|
|
|
|
|
|
|
if($unit=='days') {
|
2023-09-23 14:36:46 +02:00
|
|
|
$unit = __('days', 'linuxfoo-gitea');
|
2021-11-27 12:03:15 +01:00
|
|
|
}
|
|
|
|
elseif($unit=='months') {
|
2023-09-23 14:36:46 +02:00
|
|
|
$unit = __('months', 'linuxfoo-gitea');
|
2021-11-27 12:03:15 +01:00
|
|
|
}
|
|
|
|
elseif($unit=='years') {
|
2023-09-23 14:36:46 +02:00
|
|
|
$unit = __('years', 'linuxfoo-gitea');
|
2021-11-27 12:03:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* translators: %d: count, %s unit of time interval*/
|
2023-09-23 14:36:46 +02:00
|
|
|
return sprintf(__('since %d %s ago', 'linuxfoo-gitea'), $num, $unit);
|
2021-11-27 12:03:15 +01:00
|
|
|
}
|
|
|
|
|
2021-11-28 00:47:07 +01:00
|
|
|
static function format_commit($atts, $commit) {
|
|
|
|
preg_match('/^....-..-../', $commit->committed_date, $m);
|
|
|
|
|
2022-01-01 08:08:04 +01:00
|
|
|
$result = $m[0].' ';
|
2021-11-28 00:47:07 +01:00
|
|
|
|
2022-01-01 08:08:04 +01:00
|
|
|
if($atts['author']==null || $atts['author']!='none')
|
|
|
|
$result .=
|
2023-09-23 15:26:08 +02:00
|
|
|
__('by', 'linuxfoo-gitea').' '.$commit->commit->author->name;
|
2022-01-01 08:08:04 +01:00
|
|
|
|
|
|
|
$result .=
|
2021-11-28 00:47:07 +01:00
|
|
|
': '.
|
2023-09-23 15:26:08 +02:00
|
|
|
'<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);
|
2022-01-01 08:08:04 +01:00
|
|
|
|
|
|
|
return $result;
|
2021-11-28 00:47:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static function format_commit_stats($atts, $commits_count, $branch) {
|
2021-11-29 05:50:47 +01:00
|
|
|
return
|
|
|
|
(
|
|
|
|
($commits_count==0)
|
2023-09-23 14:36:46 +02:00
|
|
|
? __('No commits', 'linuxfoo-gitea')
|
2021-11-29 05:50:47 +01:00
|
|
|
/* translators: %d: maximum number of commits displayed */
|
2023-09-23 14:36:46 +02:00
|
|
|
: sprintf(__('Last %d commits', 'linuxfoo-gitea'), $commits_count)
|
2021-11-29 05:50:47 +01:00
|
|
|
).
|
2023-09-23 14:36:46 +02:00
|
|
|
(is_null($branch) ? '' : ' '.__('in branch', 'linuxfoo-gitea').' "'.$branch.'"').
|
2021-11-29 05:50:47 +01:00
|
|
|
(is_null($atts['since']) ? '' : ' '.self::format_since($atts['since'])).':';
|
2021-11-28 00:47:07 +01:00
|
|
|
}
|
|
|
|
|
2022-08-28 13:02:39 +02:00
|
|
|
static function commits_list($atts, $project_url) {
|
2021-11-27 20:04:17 +01:00
|
|
|
$since = null;
|
2021-11-27 16:50:41 +01:00
|
|
|
|
2021-11-27 20:04:17 +01:00
|
|
|
if($atts['commits']=='none') {
|
|
|
|
return '';
|
2021-11-27 03:20:48 +01:00
|
|
|
}
|
2021-11-27 20:04:17 +01:00
|
|
|
elseif(!is_null($atts['commits']) && $atts['commits']!='all') {
|
2023-09-23 14:36:46 +02:00
|
|
|
return self::error(__('Invalid value for parameter "commits".', 'linuxfoo-gitea'));
|
2021-11-27 12:03:15 +01:00
|
|
|
}
|
2021-11-27 03:20:48 +01:00
|
|
|
|
2021-11-27 20:04:17 +01:00
|
|
|
$out = '';
|
2021-11-27 03:20:48 +01:00
|
|
|
|
2023-09-23 15:26:08 +02:00
|
|
|
$commits_url = $project_url.'/commits?stat=false&verification=false&files=false';
|
2021-11-27 03:20:48 +01:00
|
|
|
|
|
|
|
if(!is_null($atts['ref_name'])) {
|
2023-09-23 15:26:08 +02:00
|
|
|
$commits_url .= '&sha='.$atts['ref_name'];
|
2021-11-27 03:20:48 +01:00
|
|
|
$branch = $atts['ref_name'];
|
|
|
|
}
|
2023-09-23 15:26:08 +02:00
|
|
|
elseif(!is_null($project->default_branch)) {
|
|
|
|
$commits_url .= '&sha='.$project->default_branch;
|
2021-11-27 03:20:48 +01:00
|
|
|
$branch = $project->default_branch;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$branch = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$commits_json = file_get_contents($commits_url);
|
|
|
|
|
2021-11-27 16:22:29 +01:00
|
|
|
if(is_null($commits_json)) {
|
2023-09-23 14:36:46 +02:00
|
|
|
$out .= self::error(__('Commits URL not reachable.', 'linuxfoo-gitea'));
|
2021-11-27 03:20:48 +01:00
|
|
|
}
|
2021-11-27 20:04:17 +01:00
|
|
|
else {
|
|
|
|
$commits = json_decode($commits_json);
|
2021-11-27 03:20:48 +01:00
|
|
|
|
2021-11-27 20:04:17 +01:00
|
|
|
if(is_null($commits)) {
|
2023-09-23 14:36:46 +02:00
|
|
|
$out .= self::error(__('Commits information not readable.', 'linuxfoo-gitea'));
|
2021-11-27 20:04:17 +01:00
|
|
|
}
|
|
|
|
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);
|
2021-11-27 03:20:48 +01:00
|
|
|
|
2021-11-28 00:47:07 +01:00
|
|
|
$out .=
|
|
|
|
'<span class="stats">'.
|
|
|
|
self::format_commit_stats($atts, $commits_count, $branch).
|
|
|
|
'</span>';
|
2021-11-27 20:04:17 +01:00
|
|
|
|
2021-11-28 00:47:07 +01:00
|
|
|
if($commits_count>0) {
|
2021-11-29 19:56:05 +01:00
|
|
|
$out .= '<span class="commits" role="list">';
|
2021-11-27 20:04:17 +01:00
|
|
|
|
2021-11-28 00:47:07 +01:00
|
|
|
foreach($commits as $commit) {
|
2021-11-29 19:56:05 +01:00
|
|
|
$out .= '<span class="commit">'.self::format_commit($atts, $commit).'</span>';
|
2021-11-27 20:04:17 +01:00
|
|
|
}
|
|
|
|
|
2021-11-29 19:56:05 +01:00
|
|
|
$out .= '</span>';
|
2021-11-27 20:04:17 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-27 03:20:48 +01:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:04:17 +01:00
|
|
|
return $out;
|
|
|
|
}
|
2021-11-27 03:20:48 +01:00
|
|
|
|
2022-08-28 13:02:39 +02:00
|
|
|
static function releases_list($atts, $project_url) {
|
2021-11-27 20:04:17 +01:00
|
|
|
$out = '';
|
2021-11-27 03:20:48 +01:00
|
|
|
|
2021-11-27 20:04:17 +01:00
|
|
|
if(is_null($atts['releases']) || $atts['releases']=='none') {
|
|
|
|
$out .= '';
|
|
|
|
}
|
|
|
|
elseif(!preg_match('/^(all|latest)$/', $atts['releases'])) {
|
2023-09-23 14:36:46 +02:00
|
|
|
$out .= self::error(__('Invalid value for parameter "releases".', 'linuxfoo-gitea'));
|
2021-11-27 20:04:17 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$releases_url = $project_url.'/releases?';
|
|
|
|
$releases_json = file_get_contents($releases_url);
|
2021-11-27 03:20:48 +01:00
|
|
|
|
2021-11-27 20:04:17 +01:00
|
|
|
if(is_null($releases_json)) {
|
2023-09-23 14:36:46 +02:00
|
|
|
$out .= self::error(__('Releases URL not reachable.', 'linuxfoo-gitea'));
|
2021-11-27 20:04:17 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$releases = json_decode($releases_json);
|
2021-11-27 03:20:48 +01:00
|
|
|
|
2021-11-27 20:04:17 +01:00
|
|
|
if(is_null($releases)) {
|
2023-09-23 14:36:46 +02:00
|
|
|
$out .= self::error(__('Releases information not readable.', 'linuxfoo-gitea'));
|
2021-11-27 20:04:17 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(count($releases)==0) {
|
|
|
|
$out .=
|
2021-11-29 19:56:05 +01:00
|
|
|
'<span class="releases">'.
|
2023-09-23 14:36:46 +02:00
|
|
|
__('This project has currently no releases.', 'linuxfoo-gitea').
|
2021-11-29 19:56:05 +01:00
|
|
|
'</span>';
|
2021-11-27 20:04:17 +01:00
|
|
|
}
|
|
|
|
elseif($atts['releases']=='latest') {
|
|
|
|
$out .=
|
2021-11-29 19:56:05 +01:00
|
|
|
'<span class="releases">'.
|
2023-09-23 14:36:46 +02:00
|
|
|
__('Latest release', 'linuxfoo-gitea').': '.
|
2023-09-23 15:26:08 +02:00
|
|
|
'<a href="'.$releases[0]->html_url.'">'.$releases[0]->name.'</a>'.
|
2021-11-29 19:56:05 +01:00
|
|
|
'</span>';
|
2021-11-27 20:04:17 +01:00
|
|
|
}
|
|
|
|
elseif($atts['releases']=='all') {
|
|
|
|
$out .=
|
2021-11-29 19:56:05 +01:00
|
|
|
'<span class="releases">'.
|
2023-09-23 14:36:46 +02:00
|
|
|
__('Releases', 'linuxfoo-gitea').': '.
|
2021-11-29 19:56:05 +01:00
|
|
|
'</span>'.
|
|
|
|
'<span class="releases" role="list">';
|
2021-11-27 20:04:17 +01:00
|
|
|
|
|
|
|
foreach($releases as $release) {
|
|
|
|
$out .=
|
2021-11-29 19:56:05 +01:00
|
|
|
'<span class="release">'.
|
2023-09-23 15:26:08 +02:00
|
|
|
'<a href="'.$release->html_url.'">'.$release->name.'</a>'.
|
2021-11-29 19:56:05 +01:00
|
|
|
'</span>';
|
2021-11-27 20:04:17 +01:00
|
|
|
}
|
|
|
|
|
2021-11-29 19:56:05 +01:00
|
|
|
$out .= '</span>';
|
2021-11-27 20:04:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-27 03:20:48 +01:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:04:17 +01:00
|
|
|
return $out;
|
|
|
|
}
|
2021-11-27 16:50:41 +01:00
|
|
|
|
2022-08-28 13:02:39 +02:00
|
|
|
static function list_commits($atts, $content, $tag) {
|
2021-11-27 20:04:17 +01:00
|
|
|
return self::show_project($atts, $content, $tag);
|
|
|
|
}
|
2021-11-27 16:50:41 +01:00
|
|
|
|
2022-08-28 13:02:39 +02:00
|
|
|
static function show_project($atts, $content, $tag) {
|
|
|
|
global $post;
|
2021-11-27 16:50:41 +01:00
|
|
|
|
2023-09-23 14:36:46 +02:00
|
|
|
$out = '<span class="linuxfoo_gitea">';
|
2021-11-27 16:50:41 +01:00
|
|
|
|
2021-11-27 20:04:17 +01:00
|
|
|
if(
|
|
|
|
is_null($atts['url']) ||
|
2023-09-23 15:26:08 +02:00
|
|
|
is_null($atts['project_owner']) ||
|
|
|
|
is_null($atts['project_repo'])
|
2021-11-27 20:04:17 +01:00
|
|
|
) {
|
2023-09-23 14:36:46 +02:00
|
|
|
$out .= self::error(__('Required parameter missing.', 'linuxfoo-gitea'));
|
2021-11-27 20:04:17 +01:00
|
|
|
}
|
|
|
|
elseif(!filter_var($atts['url'], FILTER_VALIDATE_URL)) {
|
2023-09-23 14:36:46 +02:00
|
|
|
$out .= self::error(__('Invalid value for parameter "url".', 'linuxfoo-gitea'));
|
2021-11-27 20:04:17 +01:00
|
|
|
}
|
|
|
|
else {
|
2023-09-23 15:26:08 +02:00
|
|
|
$project_url = $atts['url'].'/api/v1/repos/'.$atts['project_owner'].'/'.$atts['project_repo'];
|
2021-11-27 20:04:17 +01:00
|
|
|
$project_json = file_get_contents($project_url);
|
2021-11-27 16:50:41 +01:00
|
|
|
|
2021-11-27 20:04:17 +01:00
|
|
|
if(is_null($project_json)) {
|
2023-09-23 14:36:46 +02:00
|
|
|
$out .= self::error(__('Project URL not reachable.', 'linuxfoo-gitea'));
|
2021-11-27 16:50:41 +01:00
|
|
|
}
|
2021-11-27 20:04:17 +01:00
|
|
|
else {
|
|
|
|
$project = json_decode($project_json);
|
2021-11-27 16:50:41 +01:00
|
|
|
|
2021-11-27 20:04:17 +01:00
|
|
|
if(is_null($project)) {
|
|
|
|
$out .= self::error('Project information not readable.');
|
2021-11-27 16:50:41 +01:00
|
|
|
}
|
2021-11-27 20:04:17 +01:00
|
|
|
else {
|
2021-11-30 01:48:11 +01:00
|
|
|
$out .= self::project_header($atts, $project);
|
2021-11-27 20:04:17 +01:00
|
|
|
$out .= self::commits_list($atts, $project_url);
|
|
|
|
$out .= self::releases_list($atts, $project_url);
|
|
|
|
}
|
2021-11-27 16:50:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-29 19:56:05 +01:00
|
|
|
$out .= '</span>';
|
2021-11-27 03:20:48 +01:00
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-23 14:36:46 +02:00
|
|
|
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');
|