Browse Source

„snippet.php“ hinzufügen

master
Victor Giers 4 years ago
parent
commit
c774ab6873
1 changed files with 86 additions and 0 deletions
  1. 86
    0
      snippet.php

+ 86
- 0
snippet.php View File

@@ -0,0 +1,86 @@
<?php
/**
* Snippet from the "page.php" of a Wordpress-Theme which displays the public repositories of a Gitea-installation from another server
***/

if(is_page(362)){ //better put it directly on the site which should list the repositories
$entriesPerPage = 15;
$entries = [];
$pageNo = get_query_var('paged', 1); //wordpress "GETs" the page number as parameter from a url formated like yourwp/yourpage/?paged=1 and kindly redirects to yourwp/yourpage/1/
if($pageNo == 0) $pageNo = 1; //wordpress makes page number 1 become page number 0.. skip 0 later and reset 0 to 1 here.

//get how many repos there are overall (listed by Gitea in the http-header as entry [11], 'X-Total-Count'
$headers = get_headers("https://giers10.uber.space/api/v1/repos/search");
$entryCount = str_replace('X-Total-Count: ','',$headers[11]);

//the api call
$json = file_get_contents('https://giers10.uber.space/api/v1/repos/search?private=false&sort=updated&order=desc&limit=' . $entriesPerPage . '&page=' . $pageNo);
$data = json_decode($json,true);
foreach ($data['data'] as $entry){
array_push($entries,$entry);
}

//this function is for a nice display of when the repository was created and last updated like on Gitea.
function time_elapsed_string($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);

$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;

$string = array(
'y' => 'Jahr',
'm' => 'Monat',
'w' => 'Woche',
'd' => 'Tag',
'h' => 'Stunde',
'i' => 'Minute',
's' => 'Sekunde',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
//you won't need $pc ('plural character') if you i.e. just need to add an "s" because you deal with the English language
$pc = (($v == 'Jahr' || $v == 'Monat' || $v == 'Tag') ? 'en' : 'n');
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? $pc : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . '' : 'Gerade eben';
}

//list the repositories
for($i = 0; $i < count($entries); $i++){
echo("<div class='repository'><p><a href='" . $entries[$i]['html_url'] . "' target='_blank'><h2 class='repoheadline'>" . $entries[$i]['name'] . "</h2></a></p>" . "\n");
echo("<span class='repotime'>Zuletzt bearbeitet vor " . time_elapsed_string($entries[$i]['updated_at']) . " &#xb7; Erstellt vor " . time_elapsed_string($entries[$i]['created_at']) . "</span><br />\n");
if($entries[$i]['description'] != '') echo($entries[$i]['description'] . "<br />\n");
if($entries[$i]['website'] != '') echo("<a href='" . $entries[$i]['website'] . "' target='_blank'>Link zur Website</a><br />\n");
echo("</div>\n");
}
?>

<!-- create paginator -->
<div class="paging">
<?php
if($pageNo > 1){
?><a class="prev page-numbers" href="https://victorgiers.com/wp/content/open-source/?paged=<?=$pageNo-1 ?>/">« Zurück</a><?php
}
for($i = 1; $i < $entryCount / $entriesPerPage +1; $i++){
if($i == $pageNo){
?><span aria-current="page" class="page-numbers current"><?=$pageNo ?></span><?php
} else {
?><a class="page-numbers" href="https://victorgiers.com/wp/content/open-source/?paged=<?=$i ?>/"><?=$i ?></a><?php
}
}
if($pageNo < $entryCount / $entriesPerPage){
?><a class="next page-numbers" href="https://victorgiers.com/wp/content/open-source/?paged=<?=$pageNo+1 ?>/">Weiter »</a><?php
}
?>
</div>
<?php
}
?>

Loading…
Cancel
Save