767 lines
27 KiB
PHP
767 lines
27 KiB
PHP
<?php
|
||
/**
|
||
* وضعیت توسعه — Forgejo API، کش، webhook، RSS/JSON
|
||
*
|
||
* @package Hesabix2
|
||
*/
|
||
|
||
if (!defined('ABSPATH')) {
|
||
exit;
|
||
}
|
||
|
||
define('HESABIX2_FORGEJO_CACHE_TTL', 1800);
|
||
define('HESABIX2_FORGEJO_BACKUP_TTL', 7 * DAY_IN_SECONDS);
|
||
|
||
function hesabix2_forgejo_api_base() {
|
||
return apply_filters('hesabix2_forgejo_api_base', 'https://source.hesabix.ir/api/v1');
|
||
}
|
||
|
||
function hesabix2_forgejo_owner() {
|
||
return apply_filters('hesabix2_forgejo_owner', 'hesabix');
|
||
}
|
||
|
||
/**
|
||
* تم بصری هر مخزن
|
||
*
|
||
* @return array<string, array{theme: string, label_short: string}>
|
||
*/
|
||
function hesabix2_forgejo_repo_meta_map() {
|
||
return array(
|
||
'arc' => array('theme' => 'arc', 'label_short' => 'آرک'),
|
||
'arcWoc' => array('theme' => 'woc', 'label_short' => 'WOC'),
|
||
'HesabixChatPlugin' => array('theme' => 'chat', 'label_short' => 'چت'),
|
||
);
|
||
}
|
||
|
||
function hesabix2_forgejo_repo_meta($slug) {
|
||
$map = hesabix2_forgejo_repo_meta_map();
|
||
$def = isset($map[ $slug ]) ? $map[ $slug ] : array('theme' => 'default', 'label_short' => $slug);
|
||
return apply_filters('hesabix2_forgejo_repo_meta', $def, $slug);
|
||
}
|
||
|
||
function hesabix2_forgejo_repos() {
|
||
$repos = array(
|
||
array(
|
||
'slug' => 'arc',
|
||
'label' => __('حسابیکس آرک', 'hesabix2'),
|
||
'description' => __('پلتفرم اصلی حسابیکس — نسخهٔ فعلی محصول', 'hesabix2'),
|
||
),
|
||
array(
|
||
'slug' => 'arcWoc',
|
||
'label' => __('افزونه ArcWOC', 'hesabix2'),
|
||
'description' => __('اتصال فروشگاه ووکامرس به حسابیکس آرک', 'hesabix2'),
|
||
),
|
||
array(
|
||
'slug' => 'HesabixChatPlugin',
|
||
'label' => __('افزونه چت Hesabix', 'hesabix2'),
|
||
'description' => __('چت آنلاین و پشتیبانی وب برای وردپرس', 'hesabix2'),
|
||
),
|
||
);
|
||
return apply_filters('hesabix2_forgejo_repos', $repos);
|
||
}
|
||
|
||
function hesabix2_development_page_url() {
|
||
static $cached = null;
|
||
if ($cached !== null) {
|
||
return $cached;
|
||
}
|
||
$pages = get_pages(array(
|
||
'meta_key' => '_wp_page_template',
|
||
'meta_value' => 'page-development.php',
|
||
'number' => 1,
|
||
));
|
||
$cached = !empty($pages)
|
||
? get_permalink($pages[0]->ID)
|
||
: home_url('/development/');
|
||
return apply_filters('hesabix2_development_page_url', $cached);
|
||
}
|
||
|
||
function hesabix2_forgejo_issues_url($html_url) {
|
||
return trailingslashit((string) $html_url) . 'issues';
|
||
}
|
||
|
||
function hesabix2_forgejo_webhook_secret() {
|
||
$secret = get_option('hesabix2_forgejo_webhook_secret');
|
||
if (!$secret) {
|
||
$secret = wp_generate_password(32, false);
|
||
update_option('hesabix2_forgejo_webhook_secret', $secret);
|
||
}
|
||
return apply_filters('hesabix2_forgejo_webhook_secret', $secret);
|
||
}
|
||
|
||
function hesabix2_forgejo_webhook_url() {
|
||
return rest_url('hesabix2/v1/forgejo-webhook');
|
||
}
|
||
|
||
function hesabix2_forgejo_webhook_secret_header() {
|
||
return 'X-Hesabix2-Webhook-Secret';
|
||
}
|
||
|
||
function hesabix2_forgejo_feed_json_url() {
|
||
return rest_url('hesabix2/v1/development');
|
||
}
|
||
|
||
function hesabix2_forgejo_feed_rss_url() {
|
||
return rest_url('hesabix2/v1/development/feed');
|
||
}
|
||
|
||
function hesabix2_forgejo_api_get($path) {
|
||
$url = trailingslashit(hesabix2_forgejo_api_base()) . ltrim($path, '/');
|
||
$resp = wp_remote_get($url, array(
|
||
'timeout' => 15,
|
||
'headers' => array('Accept' => 'application/json'),
|
||
'user-agent' => 'Hesabix2-Theme/' . wp_get_theme()->get('Version'),
|
||
));
|
||
|
||
if (is_wp_error($resp)) {
|
||
return array('ok' => false, 'data' => null, 'error' => $resp->get_error_message());
|
||
}
|
||
|
||
$code = (int) wp_remote_retrieve_response_code($resp);
|
||
$body = wp_remote_retrieve_body($resp);
|
||
|
||
if ($code < 200 || $code >= 300) {
|
||
$msg = __('پاسخ نامعتبر از مخزن کد', 'hesabix2');
|
||
$decoded = json_decode($body, true);
|
||
if (is_array($decoded) && !empty($decoded['message'])) {
|
||
$msg = (string) $decoded['message'];
|
||
}
|
||
return array('ok' => false, 'data' => null, 'error' => $msg);
|
||
}
|
||
|
||
$data = json_decode($body, true);
|
||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||
return array('ok' => false, 'data' => null, 'error' => __('خطا در خواندن JSON', 'hesabix2'));
|
||
}
|
||
|
||
return array('ok' => true, 'data' => $data, 'error' => '');
|
||
}
|
||
|
||
function hesabix2_forgejo_flush_cache() {
|
||
foreach (hesabix2_forgejo_repos() as $repo) {
|
||
delete_transient('hesabix2_forgejo_' . $repo['slug']);
|
||
}
|
||
delete_transient('hesabix2_forgejo_timeline');
|
||
delete_transient('hesabix2_forgejo_feed_payload');
|
||
}
|
||
|
||
function hesabix2_forgejo_save_backup($slug, array $bundle) {
|
||
$bundle['backup_saved_at'] = time();
|
||
set_transient('hesabix2_forgejo_backup_' . $slug, $bundle, HESABIX2_FORGEJO_BACKUP_TTL);
|
||
}
|
||
|
||
function hesabix2_forgejo_load_backup($slug) {
|
||
$backup = get_transient('hesabix2_forgejo_backup_' . $slug);
|
||
if (!is_array($backup)) {
|
||
return null;
|
||
}
|
||
$backup['stale'] = true;
|
||
$backup['stale_from'] = !empty($backup['backup_saved_at'])
|
||
? (int) $backup['backup_saved_at']
|
||
: 0;
|
||
return $backup;
|
||
}
|
||
|
||
function hesabix2_forgejo_should_refresh() {
|
||
return current_user_can('manage_options')
|
||
&& isset($_GET['hesabix2_refresh_forgejo'])
|
||
&& '1' === (string) $_GET['hesabix2_refresh_forgejo'];
|
||
}
|
||
|
||
function hesabix2_forgejo_format_date($iso) {
|
||
if (!$iso) {
|
||
return '';
|
||
}
|
||
$ts = strtotime($iso);
|
||
if (!$ts) {
|
||
return '';
|
||
}
|
||
return date_i18n(get_option('date_format') . ' ' . get_option('time_format'), $ts);
|
||
}
|
||
|
||
function hesabix2_forgejo_relative_days($iso) {
|
||
$ts = strtotime($iso);
|
||
if (!$ts) {
|
||
return null;
|
||
}
|
||
return (int) floor((time() - $ts) / DAY_IN_SECONDS);
|
||
}
|
||
|
||
function hesabix2_forgejo_relative_label($iso) {
|
||
$days = hesabix2_forgejo_relative_days($iso);
|
||
if ($days === null) {
|
||
return '';
|
||
}
|
||
if ($days === 0) {
|
||
return __('امروز', 'hesabix2');
|
||
}
|
||
if ($days === 1) {
|
||
return __('دیروز', 'hesabix2');
|
||
}
|
||
if ($days < 7) {
|
||
return sprintf(__('%d روز پیش', 'hesabix2'), $days);
|
||
}
|
||
if ($days < 30) {
|
||
$weeks = (int) floor($days / 7);
|
||
return sprintf(_n('%d هفته پیش', '%d هفته پیش', $weeks, 'hesabix2'), $weeks);
|
||
}
|
||
return hesabix2_forgejo_format_date($iso);
|
||
}
|
||
|
||
function hesabix2_forgejo_hero_summary(array $projects) {
|
||
$active = 0;
|
||
$latest_ts = 0;
|
||
$latest_iso = '';
|
||
|
||
foreach ($projects as $proj) {
|
||
if (!empty($proj['status']) && 'active' === $proj['status']) {
|
||
$active++;
|
||
}
|
||
$iso = !empty($proj['last_commit_at']) ? $proj['last_commit_at'] : '';
|
||
if ($iso) {
|
||
$ts = strtotime($iso);
|
||
if ($ts && $ts > $latest_ts) {
|
||
$latest_ts = $ts;
|
||
$latest_iso = $iso;
|
||
}
|
||
}
|
||
}
|
||
|
||
return array(
|
||
'project_count' => count($projects),
|
||
'active_count' => $active,
|
||
'latest_activity_label' => $latest_iso ? hesabix2_forgejo_relative_label($latest_iso) : '',
|
||
);
|
||
}
|
||
|
||
function hesabix2_forgejo_activity_status($iso) {
|
||
$days = hesabix2_forgejo_relative_days($iso);
|
||
if ($days === null) {
|
||
return 'quiet';
|
||
}
|
||
return ($days <= 7) ? 'active' : 'quiet';
|
||
}
|
||
|
||
function hesabix2_forgejo_status_label($status) {
|
||
if ('active' === $status) {
|
||
return __('فعال', 'hesabix2');
|
||
}
|
||
if ('error' === $status) {
|
||
return __('نامشخص', 'hesabix2');
|
||
}
|
||
return __('کمفعال', 'hesabix2');
|
||
}
|
||
|
||
/**
|
||
* Markdown ساده → HTML امن
|
||
*/
|
||
function hesabix2_forgejo_render_markdown($text) {
|
||
if (!$text) {
|
||
return '';
|
||
}
|
||
if (preg_match('/<(?:p|ul|ol|h[1-6]|div|br)\b/i', $text)) {
|
||
return wp_kses_post(wpautop($text));
|
||
}
|
||
|
||
$allowed = wp_kses_allowed_html('post');
|
||
$text = esc_html($text);
|
||
$text = preg_replace('/^### (.+)$/m', '<h4>$1</h4>', $text);
|
||
$text = preg_replace('/^## (.+)$/m', '<h3>$1</h3>', $text);
|
||
$text = preg_replace('/^# (.+)$/m', '<h2>$1</h2>', $text);
|
||
$text = preg_replace('/\*\*(.+?)\*\*/s', '<strong>$1</strong>', $text);
|
||
$text = preg_replace('/\*(.+?)\*/s', '<em>$1</em>', $text);
|
||
$text = preg_replace('/`([^`]+)`/', '<code>$1</code>', $text);
|
||
$text = preg_replace_callback(
|
||
'/\[([^\]]+)\]\(([^)]+)\)/',
|
||
function ($m) {
|
||
return '<a href="' . esc_url($m[2]) . '" rel="noopener noreferrer">' . $m[1] . '</a>';
|
||
},
|
||
$text
|
||
);
|
||
|
||
return wp_kses(wpautop($text), $allowed);
|
||
}
|
||
|
||
function hesabix2_forgejo_normalize_commit(array $raw) {
|
||
$msg = isset($raw['commit']['message']) ? (string) $raw['commit']['message'] : '';
|
||
$first = trim(strtok($msg, "\n"));
|
||
$author = '';
|
||
if (!empty($raw['author']['login'])) {
|
||
$author = (string) $raw['author']['login'];
|
||
} elseif (!empty($raw['commit']['author']['name'])) {
|
||
$author = (string) $raw['commit']['author']['name'];
|
||
}
|
||
|
||
$stats = null;
|
||
if (!empty($raw['stats']) && is_array($raw['stats'])) {
|
||
$stats = array(
|
||
'additions' => (int) ($raw['stats']['additions'] ?? 0),
|
||
'deletions' => (int) ($raw['stats']['deletions'] ?? 0),
|
||
'total' => (int) ($raw['stats']['total'] ?? 0),
|
||
);
|
||
}
|
||
|
||
return array(
|
||
'sha' => isset($raw['sha']) ? (string) $raw['sha'] : '',
|
||
'sha_short' => isset($raw['sha']) ? substr((string) $raw['sha'], 0, 8) : '',
|
||
'message' => $msg,
|
||
'summary' => $first !== '' ? $first : __('بدون پیام', 'hesabix2'),
|
||
'author' => $author,
|
||
'created' => isset($raw['created']) ? (string) $raw['created'] : '',
|
||
'date_label' => hesabix2_forgejo_format_date(isset($raw['created']) ? $raw['created'] : ''),
|
||
'html_url' => isset($raw['html_url']) ? (string) $raw['html_url'] : '',
|
||
'stats' => $stats,
|
||
);
|
||
}
|
||
|
||
function hesabix2_forgejo_normalize_release(array $raw) {
|
||
$body_raw = isset($raw['body']) ? (string) $raw['body'] : '';
|
||
$plain = wp_strip_all_tags($body_raw);
|
||
$excerpt = $plain;
|
||
if (mb_strlen($excerpt) > 200) {
|
||
$excerpt = mb_substr($excerpt, 0, 200) . '…';
|
||
}
|
||
|
||
return array(
|
||
'tag_name' => isset($raw['tag_name']) ? (string) $raw['tag_name'] : '',
|
||
'name' => isset($raw['name']) ? (string) $raw['name'] : '',
|
||
'body' => $excerpt,
|
||
'body_html' => hesabix2_forgejo_render_markdown($body_raw),
|
||
'published_at' => isset($raw['published_at']) ? (string) $raw['published_at'] : '',
|
||
'date_label' => hesabix2_forgejo_format_date(isset($raw['published_at']) ? $raw['published_at'] : ''),
|
||
'html_url' => isset($raw['html_url']) ? (string) $raw['html_url'] : '',
|
||
'prerelease' => !empty($raw['prerelease']),
|
||
);
|
||
}
|
||
|
||
function hesabix2_forgejo_empty_bundle(array $repo_def) {
|
||
$slug = $repo_def['slug'];
|
||
$owner = hesabix2_forgejo_owner();
|
||
$meta = hesabix2_forgejo_repo_meta($slug);
|
||
|
||
return array(
|
||
'slug' => $slug,
|
||
'label' => $repo_def['label'],
|
||
'description' => $repo_def['description'],
|
||
'owner' => $owner,
|
||
'repo_theme' => $meta['theme'],
|
||
'repo_label_short' => $meta['label_short'],
|
||
'error' => '',
|
||
'stale' => false,
|
||
'html_url' => 'https://source.hesabix.ir/' . rawurlencode($owner) . '/' . rawurlencode($slug),
|
||
'issues_url' => '',
|
||
'updated_at' => '',
|
||
'updated_label' => '',
|
||
'default_branch' => 'master',
|
||
'open_issues' => 0,
|
||
'commits' => array(),
|
||
'releases' => array(),
|
||
'latest_release' => null,
|
||
'status' => 'quiet',
|
||
'last_commit_at' => '',
|
||
'fetched_at' => time(),
|
||
);
|
||
}
|
||
|
||
function hesabix2_forgejo_get_repo_bundle(array $repo_def, $force_live = false) {
|
||
$slug = $repo_def['slug'];
|
||
$owner = hesabix2_forgejo_owner();
|
||
$key = 'hesabix2_forgejo_' . $slug;
|
||
$meta = hesabix2_forgejo_repo_meta($slug);
|
||
|
||
if (!$force_live && !hesabix2_forgejo_should_refresh()) {
|
||
$cached = get_transient($key);
|
||
if (is_array($cached)) {
|
||
return $cached;
|
||
}
|
||
}
|
||
|
||
$bundle = hesabix2_forgejo_empty_bundle($repo_def);
|
||
$bundle['repo_theme'] = $meta['theme'];
|
||
$bundle['repo_label_short'] = $meta['label_short'];
|
||
|
||
$info = hesabix2_forgejo_api_get('repos/' . rawurlencode($owner) . '/' . rawurlencode($slug));
|
||
if (!$info['ok'] || !is_array($info['data'])) {
|
||
$bundle['error'] = $info['error'] ?: __('مخزن یافت نشد', 'hesabix2');
|
||
$bundle['status'] = 'error';
|
||
|
||
$backup = hesabix2_forgejo_load_backup($slug);
|
||
if ($backup) {
|
||
$backup['error'] = $bundle['error'];
|
||
$backup['status'] = 'error';
|
||
$backup['api_message'] = $bundle['error'];
|
||
set_transient($key, $backup, HESABIX2_FORGEJO_CACHE_TTL);
|
||
return $backup;
|
||
}
|
||
|
||
set_transient($key, $bundle, 300);
|
||
return $bundle;
|
||
}
|
||
|
||
$r = $info['data'];
|
||
$bundle['html_url'] = !empty($r['html_url']) ? (string) $r['html_url'] : $bundle['html_url'];
|
||
$bundle['issues_url'] = hesabix2_forgejo_issues_url($bundle['html_url']);
|
||
$bundle['updated_at'] = !empty($r['updated_at']) ? (string) $r['updated_at'] : '';
|
||
$bundle['updated_label'] = hesabix2_forgejo_format_date($bundle['updated_at']);
|
||
$bundle['default_branch'] = !empty($r['default_branch']) ? (string) $r['default_branch'] : 'master';
|
||
$bundle['open_issues'] = (int) ($r['open_issues_count'] ?? 0);
|
||
$bundle['stale'] = false;
|
||
|
||
$commits_resp = hesabix2_forgejo_api_get(
|
||
'repos/' . rawurlencode($owner) . '/' . rawurlencode($slug) . '/commits?limit=10&stat=true'
|
||
);
|
||
if ($commits_resp['ok'] && is_array($commits_resp['data'])) {
|
||
foreach ($commits_resp['data'] as $c) {
|
||
if (is_array($c)) {
|
||
$bundle['commits'][] = hesabix2_forgejo_normalize_commit($c);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!empty($bundle['commits'][0]['created'])) {
|
||
$bundle['last_commit_at'] = $bundle['commits'][0]['created'];
|
||
$bundle['status'] = hesabix2_forgejo_activity_status($bundle['last_commit_at']);
|
||
}
|
||
|
||
$releases_resp = hesabix2_forgejo_api_get(
|
||
'repos/' . rawurlencode($owner) . '/' . rawurlencode($slug) . '/releases?limit=5'
|
||
);
|
||
if ($releases_resp['ok'] && is_array($releases_resp['data'])) {
|
||
foreach ($releases_resp['data'] as $rel) {
|
||
if (is_array($rel)) {
|
||
$bundle['releases'][] = hesabix2_forgejo_normalize_release($rel);
|
||
}
|
||
}
|
||
if (!empty($bundle['releases'])) {
|
||
$bundle['latest_release'] = $bundle['releases'][0];
|
||
}
|
||
}
|
||
|
||
$bundle['fetched_at'] = time();
|
||
hesabix2_forgejo_save_backup($slug, $bundle);
|
||
set_transient($key, $bundle, HESABIX2_FORGEJO_CACHE_TTL);
|
||
delete_transient('hesabix2_forgejo_feed_payload');
|
||
|
||
return $bundle;
|
||
}
|
||
|
||
function hesabix2_forgejo_development_data($force_live = false) {
|
||
$cached_key = 'hesabix2_forgejo_feed_payload';
|
||
if (!$force_live && !hesabix2_forgejo_should_refresh()) {
|
||
$payload = get_transient($cached_key);
|
||
if (is_array($payload)) {
|
||
return $payload;
|
||
}
|
||
}
|
||
|
||
$projects = array();
|
||
$global_ts = 0;
|
||
$global_iso = '';
|
||
$stale_any = false;
|
||
$error_any = false;
|
||
$ok_any = false;
|
||
|
||
foreach (hesabix2_forgejo_repos() as $def) {
|
||
$bundle = hesabix2_forgejo_get_repo_bundle($def, $force_live);
|
||
$projects[] = $bundle;
|
||
|
||
if (!empty($bundle['stale'])) {
|
||
$stale_any = true;
|
||
}
|
||
if (!empty($bundle['error'])) {
|
||
$error_any = true;
|
||
}
|
||
if (empty($bundle['error']) || !empty($bundle['commits'])) {
|
||
$ok_any = true;
|
||
}
|
||
|
||
if (!empty($bundle['updated_at'])) {
|
||
$ts = strtotime($bundle['updated_at']);
|
||
if ($ts && $ts > $global_ts) {
|
||
$global_ts = $ts;
|
||
$global_iso = $bundle['updated_at'];
|
||
}
|
||
}
|
||
}
|
||
|
||
$timeline = hesabix2_forgejo_build_timeline($projects, $force_live);
|
||
|
||
$payload = array(
|
||
'projects' => $projects,
|
||
'timeline' => $timeline,
|
||
'global_updated' => $global_iso,
|
||
'global_updated_label' => $global_iso ? hesabix2_forgejo_format_date($global_iso) : '',
|
||
'has_error' => $error_any,
|
||
'stale_mode' => $stale_any && $error_any,
|
||
'api_down' => $error_any && !$ok_any,
|
||
'cache_ttl_minutes' => (int) (HESABIX2_FORGEJO_CACHE_TTL / 60),
|
||
'feeds' => array(
|
||
'json' => hesabix2_forgejo_feed_json_url(),
|
||
'rss' => hesabix2_forgejo_feed_rss_url(),
|
||
),
|
||
);
|
||
|
||
set_transient($cached_key, $payload, HESABIX2_FORGEJO_CACHE_TTL);
|
||
return $payload;
|
||
}
|
||
|
||
function hesabix2_forgejo_build_timeline(array $projects, $force_live = false) {
|
||
$key = 'hesabix2_forgejo_timeline';
|
||
if (!$force_live && !hesabix2_forgejo_should_refresh()) {
|
||
$cached = get_transient($key);
|
||
if (is_array($cached)) {
|
||
return $cached;
|
||
}
|
||
}
|
||
|
||
$items = array();
|
||
foreach ($projects as $proj) {
|
||
if (empty($proj['commits'])) {
|
||
continue;
|
||
}
|
||
$meta = hesabix2_forgejo_repo_meta($proj['slug']);
|
||
foreach ($proj['commits'] as $c) {
|
||
$items[] = array(
|
||
'repo_slug' => $proj['slug'],
|
||
'repo_label' => $proj['label'],
|
||
'repo_label_short' => $meta['label_short'],
|
||
'repo_theme' => $meta['theme'],
|
||
'repo_url' => $proj['html_url'],
|
||
'created' => $c['created'],
|
||
'date_label' => $c['date_label'],
|
||
'summary' => $c['summary'],
|
||
'author' => $c['author'],
|
||
'sha_short' => $c['sha_short'],
|
||
'html_url' => $c['html_url'],
|
||
'stats' => $c['stats'],
|
||
);
|
||
}
|
||
}
|
||
|
||
usort($items, function ($a, $b) {
|
||
return strtotime($b['created']) <=> strtotime($a['created']);
|
||
});
|
||
|
||
$items = array_slice($items, 0, 20);
|
||
set_transient($key, $items, HESABIX2_FORGEJO_CACHE_TTL);
|
||
return $items;
|
||
}
|
||
|
||
/**
|
||
* خروجی JSON برای REST
|
||
*/
|
||
function hesabix2_forgejo_rest_payload() {
|
||
$data = hesabix2_forgejo_development_data();
|
||
$out = array(
|
||
'updated' => $data['global_updated'],
|
||
'projects' => array(),
|
||
'timeline' => $data['timeline'],
|
||
);
|
||
|
||
foreach ($data['projects'] as $p) {
|
||
$out['projects'][] = array(
|
||
'slug' => $p['slug'],
|
||
'label' => $p['label'],
|
||
'status' => $p['status'],
|
||
'stale' => !empty($p['stale']),
|
||
'html_url' => $p['html_url'],
|
||
'issues_url' => $p['issues_url'],
|
||
'open_issues' => (int) $p['open_issues'],
|
||
'last_commit_at' => $p['last_commit_at'],
|
||
'latest_release' => $p['latest_release'],
|
||
'commits' => array_slice($p['commits'], 0, 10),
|
||
);
|
||
}
|
||
|
||
return $out;
|
||
}
|
||
|
||
function hesabix2_forgejo_rest_json($request) {
|
||
return rest_ensure_response(hesabix2_forgejo_rest_payload());
|
||
}
|
||
|
||
function hesabix2_forgejo_rest_rss($request) {
|
||
$data = hesabix2_forgejo_development_data();
|
||
$title = get_bloginfo('name') . ' — ' . __('وضعیت توسعه', 'hesabix2');
|
||
$link = hesabix2_development_page_url();
|
||
$items = array();
|
||
|
||
foreach ($data['projects'] as $p) {
|
||
if (!empty($p['latest_release'])) {
|
||
$rel = $p['latest_release'];
|
||
$items[] = array(
|
||
'title' => $p['label'] . ' — ' . ($rel['tag_name'] ?: $rel['name']),
|
||
'link' => $rel['html_url'],
|
||
'date' => $rel['published_at'],
|
||
'content' => $rel['body'] ?: $rel['tag_name'],
|
||
);
|
||
}
|
||
}
|
||
|
||
foreach ($data['timeline'] as $t) {
|
||
$items[] = array(
|
||
'title' => '[' . $t['repo_label'] . '] ' . $t['summary'],
|
||
'link' => $t['html_url'],
|
||
'date' => $t['created'],
|
||
'content' => $t['summary'],
|
||
);
|
||
}
|
||
|
||
usort($items, function ($a, $b) {
|
||
return strtotime($b['date']) <=> strtotime($a['date']);
|
||
});
|
||
$items = array_slice($items, 0, 25);
|
||
|
||
$xml = '<?xml version="1.0" encoding="UTF-8"?>';
|
||
$xml .= '<rss version="2.0"><channel>';
|
||
$xml .= '<title>' . esc_xml($title) . '</title>';
|
||
$xml .= '<link>' . esc_xml($link) . '</link>';
|
||
$xml .= '<description>' . esc_xml(__('تغییرات اخیر مخازن حسابیکس', 'hesabix2')) . '</description>';
|
||
$xml .= '<language>fa-IR</language>';
|
||
|
||
foreach ($items as $item) {
|
||
$xml .= '<item>';
|
||
$xml .= '<title>' . esc_xml($item['title']) . '</title>';
|
||
$xml .= '<link>' . esc_xml($item['link']) . '</link>';
|
||
$xml .= '<pubDate>' . esc_xml(gmdate('D, d M Y H:i:s +0000', strtotime($item['date']))) . '</pubDate>';
|
||
$xml .= '<description>' . esc_xml($item['content']) . '</description>';
|
||
$xml .= '</item>';
|
||
}
|
||
|
||
$xml .= '</channel></rss>';
|
||
|
||
$response = new WP_REST_Response($xml, 200);
|
||
$response->header('Content-Type', 'application/rss+xml; charset=UTF-8');
|
||
return $response;
|
||
}
|
||
|
||
function hesabix2_forgejo_rest_webhook($request) {
|
||
$secret = (string) $request->get_header(hesabix2_forgejo_webhook_secret_header());
|
||
if ('' === $secret) {
|
||
$secret = (string) $request->get_param('secret'); // سازگاری با webhookهای قدیمی.
|
||
}
|
||
if (!hash_equals(hesabix2_forgejo_webhook_secret(), $secret)) {
|
||
return new WP_Error('forbidden', __('کلید webhook نامعتبر است.', 'hesabix2'), array('status' => 403));
|
||
}
|
||
|
||
$body = $request->get_json_params();
|
||
if (!is_array($body)) {
|
||
$body = array();
|
||
}
|
||
|
||
$action = isset($body['action']) ? (string) $body['action'] : '';
|
||
$allowed = array(
|
||
'push', 'create', 'delete', 'repository',
|
||
'release', 'issues', 'issue_comment', 'pull_request',
|
||
'sync_push', 'sync_create', 'sync_delete',
|
||
);
|
||
|
||
if ($action && !in_array($action, $allowed, true)) {
|
||
return rest_ensure_response(array('ok' => true, 'skipped' => $action));
|
||
}
|
||
|
||
hesabix2_forgejo_flush_cache();
|
||
|
||
return rest_ensure_response(array(
|
||
'ok' => true,
|
||
'action' => $action,
|
||
'message' => __('کش وضعیت توسعه پاک شد.', 'hesabix2'),
|
||
));
|
||
}
|
||
|
||
function hesabix2_forgejo_register_rest_routes() {
|
||
register_rest_route('hesabix2/v1', '/development', array(
|
||
'methods' => 'GET',
|
||
'callback' => 'hesabix2_forgejo_rest_json',
|
||
'permission_callback' => '__return_true',
|
||
));
|
||
|
||
register_rest_route('hesabix2/v1', '/development/feed', array(
|
||
'methods' => 'GET',
|
||
'callback' => 'hesabix2_forgejo_rest_rss',
|
||
'permission_callback' => '__return_true',
|
||
));
|
||
|
||
register_rest_route('hesabix2/v1', '/forgejo-webhook', array(
|
||
'methods' => 'POST',
|
||
'callback' => 'hesabix2_forgejo_rest_webhook',
|
||
'permission_callback' => '__return_true',
|
||
));
|
||
}
|
||
add_action('rest_api_init', 'hesabix2_forgejo_register_rest_routes');
|
||
|
||
function hesabix2_forgejo_enqueue_scripts() {
|
||
if (!is_page_template('page-development.php')) {
|
||
return;
|
||
}
|
||
$path = get_template_directory() . '/assets/js/development-feed.js';
|
||
wp_enqueue_script(
|
||
'hesabix2-development-feed',
|
||
get_template_directory_uri() . '/assets/js/development-feed.js',
|
||
array(),
|
||
file_exists($path) ? (string) filemtime($path) : '1.0',
|
||
true
|
||
);
|
||
}
|
||
add_action('wp_enqueue_scripts', 'hesabix2_forgejo_enqueue_scripts');
|
||
|
||
/**
|
||
* لینکهای کشف فید در head صفحهٔ توسعه
|
||
*/
|
||
function hesabix2_forgejo_feed_head_links() {
|
||
if (!is_page_template('page-development.php')) {
|
||
return;
|
||
}
|
||
$json = hesabix2_forgejo_feed_json_url();
|
||
$rss = hesabix2_forgejo_feed_rss_url();
|
||
echo '<link rel="alternate" type="application/json" href="' . esc_url($json) . '" title="' . esc_attr__('وضعیت توسعه (JSON)', 'hesabix2') . '" />' . "\n";
|
||
echo '<link rel="alternate" type="application/rss+xml" href="' . esc_url($rss) . '" title="' . esc_attr__('وضعیت توسعه (RSS)', 'hesabix2') . '" />' . "\n";
|
||
}
|
||
add_action('wp_head', 'hesabix2_forgejo_feed_head_links', 5);
|
||
|
||
function hesabix2_maybe_create_development_page() {
|
||
if (get_option('hesabix2_development_page_created')) {
|
||
return;
|
||
}
|
||
$existing = get_pages(array(
|
||
'meta_key' => '_wp_page_template',
|
||
'meta_value' => 'page-development.php',
|
||
'number' => 1,
|
||
));
|
||
if (!empty($existing)) {
|
||
update_option('hesabix2_development_page_created', 1);
|
||
return;
|
||
}
|
||
$page_id = wp_insert_post(array(
|
||
'post_title' => __('وضعیت توسعه', 'hesabix2'),
|
||
'post_name' => 'development',
|
||
'post_status' => 'publish',
|
||
'post_type' => 'page',
|
||
'post_content' => '',
|
||
), true);
|
||
if (!is_wp_error($page_id) && $page_id) {
|
||
update_post_meta($page_id, '_wp_page_template', 'page-development.php');
|
||
update_option('hesabix2_development_page_created', 1);
|
||
}
|
||
}
|
||
add_action('after_setup_theme', 'hesabix2_maybe_create_development_page', 30);
|
||
|
||
function hesabix2_forgejo_admin_refresh_handler() {
|
||
if (!hesabix2_forgejo_should_refresh()) {
|
||
return;
|
||
}
|
||
hesabix2_forgejo_flush_cache();
|
||
}
|
||
add_action('template_redirect', 'hesabix2_forgejo_admin_refresh_handler', 5);
|
||
|
||
/**
|
||
* آیکون SVG کوچک برای تم مخزن
|
||
*/
|
||
function hesabix2_forgejo_repo_icon_svg($theme) {
|
||
$icons = array(
|
||
'arc' => '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" d="M4 6h16M4 12h16M4 18h10"/></svg>',
|
||
'woc' => '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" d="M3 9h18v10a2 2 0 01-2 2H5a2 2 0 01-2-2V9z"/><path stroke-linecap="round" stroke-linejoin="round" d="M3 9l2.45-4.9A2 2 0 017.24 3h9.52a2 2 0 011.79 1.1L21 9"/></svg>',
|
||
'chat' => '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z"/></svg>',
|
||
);
|
||
return isset($icons[ $theme ]) ? $icons[ $theme ] : $icons['arc'];
|
||
}
|