port from gitlab to gitea (WIP)

Done fix wording and gettext domain references
ToDo port API calls
This commit is contained in:
root 2023-09-23 14:36:46 +02:00
parent 590f35c0a0
commit 18f7d56fcd

View File

@ -1,9 +1,9 @@
<?php <?php
/* /*
Plugin Name: Gitlab REST API Shortcodes Plugin Name: Gitea REST API Shortcodes
Plugin URI: http://tk-sls.de/ref/gitlab-list-commits Plugin URI: http://tk-sls.de/ref/gitea-list-commits
Description: Summary: Embed list of most recent commits to a Gitlab project from a Gitlab instance's public REST API. Example: Add [gitlab-list-commits url=https://tk-sls.de/gitlab 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". 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".
Author: Tilman Kranz Author: Tilman Kranz
Version: 1.4 Version: 1.4
Author URI: https://tk-sls.de Author URI: https://tk-sls.de
@ -14,11 +14,11 @@ class LinuxfooGitlab {
} }
static function load_textdomain() { static function load_textdomain() {
load_plugin_textdomain('linuxfoo-gitlab', false, dirname(plugin_basename(__FILE__)).'/lang'); load_plugin_textdomain('linuxfoo-gitea', false, dirname(plugin_basename(__FILE__)).'/lang');
} }
static function css() { static function css() {
wp_enqueue_style('linuxfoo_gitlab_css', plugins_url('styles.css', __FILE__ ), '', '1.4' ); wp_enqueue_style('linuxfoo_gitea_css', plugins_url('styles.css', __FILE__ ), '', '1.4' );
} }
static function error ( $msg ) { static function error ( $msg ) {
@ -31,7 +31,7 @@ class LinuxfooGitlab {
static function project_header($atts, $project) { static function project_header($atts, $project) {
return return
'<span class="project_header">'. '<span class="project_header">'.
__('Gitlab project', 'linuxfoo-gitlab').' '. __('Gitea project', 'linuxfoo-gitea').' '.
'<a href="'.$project->web_url.'">'.$project->name.'</a>'. '<a href="'.$project->web_url.'">'.$project->name.'</a>'.
'</span>'; '</span>';
} }
@ -42,17 +42,17 @@ class LinuxfooGitlab {
$unit = $m[2]; $unit = $m[2];
if($unit=='days') { if($unit=='days') {
$unit = __('days', 'linuxfoo-gitlab'); $unit = __('days', 'linuxfoo-gitea');
} }
elseif($unit=='months') { elseif($unit=='months') {
$unit = __('months', 'linuxfoo-gitlab'); $unit = __('months', 'linuxfoo-gitea');
} }
elseif($unit=='years') { elseif($unit=='years') {
$unit = __('years', 'linuxfoo-gitlab'); $unit = __('years', 'linuxfoo-gitea');
} }
/* translators: %d: count, %s unit of time interval*/ /* translators: %d: count, %s unit of time interval*/
return sprintf(__('since %d %s ago', 'linuxfoo-gitlab'), $num, $unit); return sprintf(__('since %d %s ago', 'linuxfoo-gitea'), $num, $unit);
} }
static function format_commit($atts, $commit) { static function format_commit($atts, $commit) {
@ -62,7 +62,7 @@ class LinuxfooGitlab {
if($atts['author']==null || $atts['author']!='none') if($atts['author']==null || $atts['author']!='none')
$result .= $result .=
__('by', 'linuxfoo-gitlab').' '.$commit->author_name; __('by', 'linuxfoo-gitea').' '.$commit->author_name;
$result .= $result .=
': '. ': '.
@ -76,11 +76,11 @@ class LinuxfooGitlab {
return return
( (
($commits_count==0) ($commits_count==0)
? __('No commits', 'linuxfoo-gitlab') ? __('No commits', 'linuxfoo-gitea')
/* translators: %d: maximum number of commits displayed */ /* translators: %d: maximum number of commits displayed */
: sprintf(__('Last %d commits', 'linuxfoo-gitlab'), $commits_count) : sprintf(__('Last %d commits', 'linuxfoo-gitea'), $commits_count)
). ).
(is_null($branch) ? '' : ' '.__('in branch', 'linuxfoo-gitlab').' "'.$branch.'"'). (is_null($branch) ? '' : ' '.__('in branch', 'linuxfoo-gitea').' "'.$branch.'"').
(is_null($atts['since']) ? '' : ' '.self::format_since($atts['since'])).':'; (is_null($atts['since']) ? '' : ' '.self::format_since($atts['since'])).':';
} }
@ -91,11 +91,11 @@ class LinuxfooGitlab {
return ''; return '';
} }
elseif(!is_null($atts['commits']) && $atts['commits']!='all') { elseif(!is_null($atts['commits']) && $atts['commits']!='all') {
return self::error(__('Invalid value for parameter "commits".', 'linuxfoo-gitlab')); return self::error(__('Invalid value for parameter "commits".', 'linuxfoo-gitea'));
} }
elseif(!is_null($atts['since'])) { elseif(!is_null($atts['since'])) {
if(!preg_match('/^[1-9][0-9]*\s+(days|months|years)$/', $atts['since'])) { if(!preg_match('/^[1-9][0-9]*\s+(days|months|years)$/', $atts['since'])) {
return self::error(__('Invalid value for parameter "since".', 'linuxfoo-gitlab')); return self::error(__('Invalid value for parameter "since".', 'linuxfoo-gitea'));
} }
else { else {
$since = date(DATE_ISO8601, strtotime('-'.$atts['since'])); $since = date(DATE_ISO8601, strtotime('-'.$atts['since']));
@ -125,13 +125,13 @@ class LinuxfooGitlab {
$commits_json = file_get_contents($commits_url); $commits_json = file_get_contents($commits_url);
if(is_null($commits_json)) { if(is_null($commits_json)) {
$out .= self::error(__('Commits URL not reachable.', 'linuxfoo-gitlab')); $out .= self::error(__('Commits URL not reachable.', 'linuxfoo-gitea'));
} }
else { else {
$commits = json_decode($commits_json); $commits = json_decode($commits_json);
if(is_null($commits)) { if(is_null($commits)) {
$out .= self::error(__('Commits information not readable.', 'linuxfoo-gitlab')); $out .= self::error(__('Commits information not readable.', 'linuxfoo-gitea'));
} }
else { else {
$commits_count = count($commits); $commits_count = count($commits);
@ -165,39 +165,39 @@ class LinuxfooGitlab {
$out .= ''; $out .= '';
} }
elseif(!preg_match('/^(all|latest)$/', $atts['releases'])) { elseif(!preg_match('/^(all|latest)$/', $atts['releases'])) {
$out .= self::error(__('Invalid value for parameter "releases".', 'linuxfoo-gitlab')); $out .= self::error(__('Invalid value for parameter "releases".', 'linuxfoo-gitea'));
} }
else { else {
$releases_url = $project_url.'/releases?'; $releases_url = $project_url.'/releases?';
$releases_json = file_get_contents($releases_url); $releases_json = file_get_contents($releases_url);
if(is_null($releases_json)) { if(is_null($releases_json)) {
$out .= self::error(__('Releases URL not reachable.', 'linuxfoo-gitlab')); $out .= self::error(__('Releases URL not reachable.', 'linuxfoo-gitea'));
} }
else { else {
$releases = json_decode($releases_json); $releases = json_decode($releases_json);
if(is_null($releases)) { if(is_null($releases)) {
$out .= self::error(__('Releases information not readable.', 'linuxfoo-gitlab')); $out .= self::error(__('Releases information not readable.', 'linuxfoo-gitea'));
} }
else { else {
if(count($releases)==0) { if(count($releases)==0) {
$out .= $out .=
'<span class="releases">'. '<span class="releases">'.
__('This project has currently no releases.', 'linuxfoo-gitlab'). __('This project has currently no releases.', 'linuxfoo-gitea').
'</span>'; '</span>';
} }
elseif($atts['releases']=='latest') { elseif($atts['releases']=='latest') {
$out .= $out .=
'<span class="releases">'. '<span class="releases">'.
__('Latest release', 'linuxfoo-gitlab').': '. __('Latest release', 'linuxfoo-gitea').': '.
'<a href="'.$releases[0]->_links->self.'">'.$releases[0]->name.'</a>'. '<a href="'.$releases[0]->_links->self.'">'.$releases[0]->name.'</a>'.
'</span>'; '</span>';
} }
elseif($atts['releases']=='all') { elseif($atts['releases']=='all') {
$out .= $out .=
'<span class="releases">'. '<span class="releases">'.
__('Releases', 'linuxfoo-gitlab').': '. __('Releases', 'linuxfoo-gitea').': '.
'</span>'. '</span>'.
'<span class="releases" role="list">'; '<span class="releases" role="list">';
@ -224,29 +224,29 @@ class LinuxfooGitlab {
static function show_project($atts, $content, $tag) { static function show_project($atts, $content, $tag) {
global $post; global $post;
$out = '<span class="linuxfoo_gitlab">'; $out = '<span class="linuxfoo_gitea">';
if( if(
is_null($atts['url']) || is_null($atts['url']) ||
is_null($atts['project_id']) is_null($atts['project_id'])
) { ) {
$out .= self::error(__('Required parameter missing.', 'linuxfoo-gitlab')); $out .= self::error(__('Required parameter missing.', 'linuxfoo-gitea'));
} }
elseif(!preg_match('/^[1-9][0-9]*$/', $atts['project_id'])) { elseif(!preg_match('/^[1-9][0-9]*$/', $atts['project_id'])) {
$out .= self::error(__('Invalid value for parameter "project_id".', 'linuxfoo-gitlab')); $out .= self::error(__('Invalid value for parameter "project_id".', 'linuxfoo-gitea'));
} }
elseif(!filter_var($atts['url'], FILTER_VALIDATE_URL)) { elseif(!filter_var($atts['url'], FILTER_VALIDATE_URL)) {
$out .= self::error(__('Invalid value for parameter "url".', 'linuxfoo-gitlab')); $out .= self::error(__('Invalid value for parameter "url".', 'linuxfoo-gitea'));
} }
elseif(!preg_match('/^[1-9][0-9]*$/', $atts['project_id'])) { elseif(!preg_match('/^[1-9][0-9]*$/', $atts['project_id'])) {
$out .= self::error(__('Invalid value for parameter "project_id".', 'linuxfoo-gitlab')); $out .= self::error(__('Invalid value for parameter "project_id".', 'linuxfoo-gitea'));
} }
else { else {
$project_url = $atts['url'].'/api/v4/projects/'.$atts['project_id']; $project_url = $atts['url'].'/api/v4/projects/'.$atts['project_id'];
$project_json = file_get_contents($project_url); $project_json = file_get_contents($project_url);
if(is_null($project_json)) { if(is_null($project_json)) {
$out .= self::error(__('Project URL not reachable.', 'linuxfoo-gitlab')); $out .= self::error(__('Project URL not reachable.', 'linuxfoo-gitea'));
} }
else { else {
$project = json_decode($project_json); $project = json_decode($project_json);
@ -268,7 +268,7 @@ class LinuxfooGitlab {
} }
} }
add_action('plugins_loaded', 'LinuxfooGitlab::load_textdomain'); add_action('plugins_loaded', 'LinuxfooGitea::load_textdomain');
add_action('wp_enqueue_scripts', 'LinuxfooGitlab::css' ); add_action('wp_enqueue_scripts', 'LinuxfooGitea::css' );
add_shortcode('gitlab-list-commits', 'LinuxfooGitlab::list_commits'); add_shortcode('gitlab-list-commits', 'LinuxfooGitea::list_commits');
add_shortcode('gitlab-show-project', 'LinuxfooGitlab::show_project'); add_shortcode('gitlab-show-project', 'LinuxfooGitea::show_project');