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
/*
Plugin Name: Gitlab REST API Shortcodes
Plugin URI: http://tk-sls.de/ref/gitlab-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".
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".
Author: Tilman Kranz
Version: 1.4
Author URI: https://tk-sls.de
@ -14,11 +14,11 @@ class LinuxfooGitlab {
}
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() {
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 ) {
@ -31,7 +31,7 @@ class LinuxfooGitlab {
static function project_header($atts, $project) {
return
'<span class="project_header">'.
__('Gitlab project', 'linuxfoo-gitlab').' '.
__('Gitea project', 'linuxfoo-gitea').' '.
'<a href="'.$project->web_url.'">'.$project->name.'</a>'.
'</span>';
}
@ -42,17 +42,17 @@ class LinuxfooGitlab {
$unit = $m[2];
if($unit=='days') {
$unit = __('days', 'linuxfoo-gitlab');
$unit = __('days', 'linuxfoo-gitea');
}
elseif($unit=='months') {
$unit = __('months', 'linuxfoo-gitlab');
$unit = __('months', 'linuxfoo-gitea');
}
elseif($unit=='years') {
$unit = __('years', 'linuxfoo-gitlab');
$unit = __('years', 'linuxfoo-gitea');
}
/* 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) {
@ -62,7 +62,7 @@ class LinuxfooGitlab {
if($atts['author']==null || $atts['author']!='none')
$result .=
__('by', 'linuxfoo-gitlab').' '.$commit->author_name;
__('by', 'linuxfoo-gitea').' '.$commit->author_name;
$result .=
': '.
@ -76,11 +76,11 @@ class LinuxfooGitlab {
return
(
($commits_count==0)
? __('No commits', 'linuxfoo-gitlab')
? __('No commits', 'linuxfoo-gitea')
/* 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'])).':';
}
@ -91,11 +91,11 @@ class LinuxfooGitlab {
return '';
}
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'])) {
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 {
$since = date(DATE_ISO8601, strtotime('-'.$atts['since']));
@ -125,13 +125,13 @@ class LinuxfooGitlab {
$commits_json = file_get_contents($commits_url);
if(is_null($commits_json)) {
$out .= self::error(__('Commits URL not reachable.', 'linuxfoo-gitlab'));
$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-gitlab'));
$out .= self::error(__('Commits information not readable.', 'linuxfoo-gitea'));
}
else {
$commits_count = count($commits);
@ -165,39 +165,39 @@ class LinuxfooGitlab {
$out .= '';
}
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 {
$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-gitlab'));
$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-gitlab'));
$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-gitlab').
__('This project has currently no releases.', 'linuxfoo-gitea').
'</span>';
}
elseif($atts['releases']=='latest') {
$out .=
'<span class="releases">'.
__('Latest release', 'linuxfoo-gitlab').': '.
__('Latest release', 'linuxfoo-gitea').': '.
'<a href="'.$releases[0]->_links->self.'">'.$releases[0]->name.'</a>'.
'</span>';
}
elseif($atts['releases']=='all') {
$out .=
'<span class="releases">'.
__('Releases', 'linuxfoo-gitlab').': '.
__('Releases', 'linuxfoo-gitea').': '.
'</span>'.
'<span class="releases" role="list">';
@ -224,29 +224,29 @@ class LinuxfooGitlab {
static function show_project($atts, $content, $tag) {
global $post;
$out = '<span class="linuxfoo_gitlab">';
$out = '<span class="linuxfoo_gitea">';
if(
is_null($atts['url']) ||
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'])) {
$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)) {
$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'])) {
$out .= self::error(__('Invalid value for parameter "project_id".', 'linuxfoo-gitlab'));
$out .= self::error(__('Invalid value for parameter "project_id".', 'linuxfoo-gitea'));
}
else {
$project_url = $atts['url'].'/api/v4/projects/'.$atts['project_id'];
$project_json = file_get_contents($project_url);
if(is_null($project_json)) {
$out .= self::error(__('Project URL not reachable.', 'linuxfoo-gitlab'));
$out .= self::error(__('Project URL not reachable.', 'linuxfoo-gitea'));
}
else {
$project = json_decode($project_json);
@ -268,7 +268,7 @@ class LinuxfooGitlab {
}
}
add_action('plugins_loaded', 'LinuxfooGitlab::load_textdomain');
add_action('wp_enqueue_scripts', 'LinuxfooGitlab::css' );
add_shortcode('gitlab-list-commits', 'LinuxfooGitlab::list_commits');
add_shortcode('gitlab-show-project', 'LinuxfooGitlab::show_project');
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');