mirror of
https://github.com/WenPai-org/git-embed-feicode.git
synced 2025-08-03 03:18:41 +08:00
Improve custom Git service support and UI styling
Enhanced detection of default branches for custom/self-hosted Git services and improved archive URL handling in both PHP and JS. Updated UI elements and styles for better appearance and consistency, including avatar shape, spacing, font sizes, and icon usage. Added more robust cache clearing logic and improved owner type normalization.
This commit is contained in:
parent
3b10f2574f
commit
959fc6b9b8
3 changed files with 122 additions and 40 deletions
53
block.js
53
block.js
|
@ -208,8 +208,19 @@
|
|||
const cardClass = `git-embed-card${cardStyle !== 'default' ? ` git-embed-card-${cardStyle}` : ''}`;
|
||||
const avatarClass = `git-embed-avatar git-embed-avatar-${avatarSize}`;
|
||||
const buttonClass = `git-embed-button-${buttonSize}`;
|
||||
const downloadUrl = repoData.archive_url ?
|
||||
repoData.archive_url.replace('{archive_format}', 'zipball').replace('{/ref}', '/main') : '';
|
||||
|
||||
let downloadUrl = '';
|
||||
if (repoData.archive_url) {
|
||||
const defaultBranch = repoData.default_branch || 'main';
|
||||
if (repoData.platform === 'github') {
|
||||
downloadUrl = repoData.archive_url.replace('{archive_format}', 'zipball').replace('{/ref}', `/${defaultBranch}`);
|
||||
} else {
|
||||
downloadUrl = repoData.archive_url;
|
||||
if (downloadUrl.includes('main.zip') && defaultBranch !== 'main') {
|
||||
downloadUrl = downloadUrl.replace('main.zip', `${defaultBranch}.zip`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return el('div', { className: cardClass },
|
||||
showSiteInfo && repoData.site_info && el('div', {
|
||||
|
@ -247,7 +258,7 @@
|
|||
}, repoData.full_name)
|
||||
),
|
||||
showAvatar && repoData.owner && el('div', { className: 'git-embed-owner-info' },
|
||||
el('span', { className: 'git-embed-owner-type' }, repoData.owner.type),
|
||||
repoData.owner.type && el('span', { className: 'git-embed-owner-type' }, repoData.owner.type),
|
||||
el('a', {
|
||||
href: repoData.owner.html_url,
|
||||
target: '_blank',
|
||||
|
@ -361,33 +372,43 @@
|
|||
{ label: 'Custom Git Service', value: 'custom' }
|
||||
],
|
||||
onChange: (value) => setAttributes({ platform: value }),
|
||||
help: platform !== 'github' ? 'Self-hosted Git service requires custom domain' : ''
|
||||
help: platform !== 'github' ? 'Self-hosted Git service requires custom domain' : '',
|
||||
__next40pxDefaultSize: true,
|
||||
__nextHasNoMarginBottom: true
|
||||
}),
|
||||
(platform !== 'github') && el(TextControl, {
|
||||
label: __('Custom Domain', 'git-embed-feicode'),
|
||||
value: customDomain,
|
||||
onChange: (value) => setAttributes({ customDomain: value }),
|
||||
placeholder: 'e.g. git.example.com',
|
||||
help: `Enter the domain of your ${platform.charAt(0).toUpperCase() + platform.slice(1)} instance`
|
||||
help: `Enter the domain of your ${platform.charAt(0).toUpperCase() + platform.slice(1)} instance`,
|
||||
__next40pxDefaultSize: true,
|
||||
__nextHasNoMarginBottom: true
|
||||
}),
|
||||
(platform !== 'github') && el(TextControl, {
|
||||
label: __('Custom Site Name (Optional)', 'git-embed-feicode'),
|
||||
value: customSiteName,
|
||||
onChange: (value) => setAttributes({ customSiteName: value }),
|
||||
placeholder: 'e.g. Company Git',
|
||||
help: 'Override the automatically detected site name'
|
||||
help: 'Override the automatically detected site name',
|
||||
__next40pxDefaultSize: true,
|
||||
__nextHasNoMarginBottom: true
|
||||
}),
|
||||
el(TextControl, {
|
||||
label: __('Repository Owner', 'git-embed-feicode'),
|
||||
value: owner,
|
||||
onChange: (value) => setAttributes({ owner: value }),
|
||||
placeholder: 'e.g. facebook'
|
||||
placeholder: 'e.g. facebook',
|
||||
__next40pxDefaultSize: true,
|
||||
__nextHasNoMarginBottom: true
|
||||
}),
|
||||
el(TextControl, {
|
||||
label: __('Repository Name', 'git-embed-feicode'),
|
||||
value: repo,
|
||||
onChange: (value) => setAttributes({ repo: value }),
|
||||
placeholder: 'e.g. react'
|
||||
placeholder: 'e.g. react',
|
||||
__next40pxDefaultSize: true,
|
||||
__nextHasNoMarginBottom: true
|
||||
}),
|
||||
el(Button, {
|
||||
isPrimary: true,
|
||||
|
@ -418,7 +439,9 @@
|
|||
{ label: 'Medium', value: 'medium' },
|
||||
{ label: 'Large', value: 'large' }
|
||||
],
|
||||
onChange: (value) => setAttributes({ avatarSize: value })
|
||||
onChange: (value) => setAttributes({ avatarSize: value }),
|
||||
__next40pxDefaultSize: true,
|
||||
__nextHasNoMarginBottom: true
|
||||
}),
|
||||
el(ToggleControl, {
|
||||
label: __('Show Description', 'git-embed-feicode'),
|
||||
|
@ -486,7 +509,9 @@
|
|||
{ label: 'Ghost', value: 'ghost' }
|
||||
],
|
||||
onChange: (value) => setAttributes({ buttonStyle: value }),
|
||||
disabled: !showActions
|
||||
disabled: !showActions,
|
||||
__next40pxDefaultSize: true,
|
||||
__nextHasNoMarginBottom: true
|
||||
}),
|
||||
el(SelectControl, {
|
||||
label: __('Button Size', 'git-embed-feicode'),
|
||||
|
@ -497,7 +522,9 @@
|
|||
{ label: 'Large', value: 'large' }
|
||||
],
|
||||
onChange: (value) => setAttributes({ buttonSize: value }),
|
||||
disabled: !showActions
|
||||
disabled: !showActions,
|
||||
__next40pxDefaultSize: true,
|
||||
__nextHasNoMarginBottom: true
|
||||
})
|
||||
),
|
||||
el(PanelBody, {
|
||||
|
@ -515,7 +542,9 @@
|
|||
{ label: 'Gradient', value: 'gradient' },
|
||||
{ label: 'Glassmorphism', value: 'glass' }
|
||||
],
|
||||
onChange: (value) => setAttributes({ cardStyle: value })
|
||||
onChange: (value) => setAttributes({ cardStyle: value }),
|
||||
__next40pxDefaultSize: true,
|
||||
__nextHasNoMarginBottom: true
|
||||
})
|
||||
)
|
||||
),
|
||||
|
|
|
@ -205,6 +205,11 @@ class GitEmbedFeiCode {
|
|||
|
||||
$repo_data = $this->normalize_repository_data($data, $platform, $api_config);
|
||||
|
||||
if (!isset($data['default_branch']) && in_array($platform, ['gitea', 'forgejo', 'custom'])) {
|
||||
$repo_data['default_branch'] = $this->detect_default_branch($api_config, $owner, $repo);
|
||||
$repo_data['archive_url'] = $this->get_archive_url(array_merge($data, ['default_branch' => $repo_data['default_branch']]), $platform, $api_config['base_url']);
|
||||
}
|
||||
|
||||
set_transient($cache_key, $repo_data, DAY_IN_SECONDS);
|
||||
|
||||
if (!empty($repo_data['owner']['avatar_url'])) {
|
||||
|
@ -331,6 +336,27 @@ class GitEmbedFeiCode {
|
|||
return $fallback;
|
||||
}
|
||||
|
||||
private function detect_default_branch(array $api_config, string $owner, string $repo): string {
|
||||
$branches_to_try = ['main', 'master', 'develop', 'dev'];
|
||||
$base_url = $api_config['base_url'];
|
||||
|
||||
foreach ($branches_to_try as $branch) {
|
||||
$test_url = "{$base_url}/{$owner}/{$repo}/archive/{$branch}.zip";
|
||||
$response = wp_remote_head($test_url, [
|
||||
'timeout' => 5,
|
||||
'headers' => [
|
||||
'User-Agent' => 'Git-Embed-FeiCode/1.0'
|
||||
]
|
||||
]);
|
||||
|
||||
if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
|
||||
return $branch;
|
||||
}
|
||||
}
|
||||
|
||||
return 'main';
|
||||
}
|
||||
|
||||
private function normalize_domain(string $domain): string {
|
||||
$domain = trim($domain);
|
||||
$domain = preg_replace('/^https?:\/\//', '', $domain);
|
||||
|
@ -353,11 +379,12 @@ class GitEmbedFeiCode {
|
|||
'open_issues_count' => $data['open_issues_count'] ?? 0,
|
||||
'clone_url' => $data['http_url_to_repo'],
|
||||
'archive_url' => $this->get_archive_url($data, $platform, $base_url),
|
||||
'default_branch' => $data['default_branch'] ?? 'main',
|
||||
'owner' => [
|
||||
'login' => $data['namespace']['name'] ?? $data['owner']['username'],
|
||||
'avatar_url' => $data['namespace']['avatar_url'] ?? $data['owner']['avatar_url'],
|
||||
'html_url' => $base_url . '/' . ($data['namespace']['name'] ?? $data['owner']['username']),
|
||||
'type' => $this->normalize_owner_type($data['namespace']['kind'] ?? 'user')
|
||||
'type' => $this->normalize_owner_type($data['namespace']['kind'] ?? $data['owner']['type'] ?? 'user')
|
||||
],
|
||||
'site_info' => $api_config['site_info'],
|
||||
'platform' => $platform
|
||||
|
@ -375,11 +402,12 @@ class GitEmbedFeiCode {
|
|||
'open_issues_count' => $data['open_issues_count'] ?? $data['open_issues'] ?? 0,
|
||||
'clone_url' => $data['clone_url'],
|
||||
'archive_url' => $this->get_archive_url($data, $platform, $base_url),
|
||||
'default_branch' => $data['default_branch'] ?? 'main',
|
||||
'owner' => [
|
||||
'login' => $data['owner']['login'],
|
||||
'avatar_url' => $data['owner']['avatar_url'],
|
||||
'html_url' => $data['owner']['html_url'] ?? $base_url . '/' . $data['owner']['login'],
|
||||
'type' => $this->normalize_owner_type($data['owner']['type'] ?? 'User')
|
||||
'type' => $this->normalize_owner_type($data['owner']['type'] ?? 'user')
|
||||
],
|
||||
'site_info' => $api_config['site_info'],
|
||||
'platform' => $platform
|
||||
|
@ -393,6 +421,7 @@ class GitEmbedFeiCode {
|
|||
|
||||
$owner = $data['owner']['login'] ?? $data['namespace']['name'] ?? '';
|
||||
$repo = $data['name'];
|
||||
$default_branch = $data['default_branch'] ?? 'main';
|
||||
|
||||
switch ($platform) {
|
||||
case 'github':
|
||||
|
@ -403,19 +432,21 @@ class GitEmbedFeiCode {
|
|||
case 'gitea':
|
||||
case 'forgejo':
|
||||
case 'custom':
|
||||
return "{$base_url}/{$owner}/{$repo}/archive/main.zip";
|
||||
return "{$base_url}/{$owner}/{$repo}/archive/{$default_branch}.zip";
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
private function normalize_owner_type(string $type): string {
|
||||
$type = strtolower($type);
|
||||
$type = strtolower(trim($type));
|
||||
switch ($type) {
|
||||
case 'organization':
|
||||
case 'org':
|
||||
case 'group':
|
||||
return 'Organization';
|
||||
case 'user':
|
||||
case 'individual':
|
||||
default:
|
||||
return 'User';
|
||||
}
|
||||
|
@ -500,7 +531,7 @@ class GitEmbedFeiCode {
|
|||
|
||||
<div class="git-embed-title-content">
|
||||
<h3 class="git-embed-title">
|
||||
<span class="dashicons dashicons-admin-links git-embed-repo-icon"></span>
|
||||
<span class="dashicons dashicons-embed-generic git-embed-repo-icon"></span>
|
||||
<a href="<?php echo esc_url($repo_data['html_url']); ?>" target="_blank" rel="noopener">
|
||||
<?php echo esc_html($repo_data['full_name']); ?>
|
||||
</a>
|
||||
|
@ -508,7 +539,9 @@ class GitEmbedFeiCode {
|
|||
|
||||
<?php if ($show_avatar): ?>
|
||||
<div class="git-embed-owner-info">
|
||||
<span class="git-embed-owner-type"><?php echo esc_html($repo_data['owner']['type']); ?></span>
|
||||
<?php if (!empty($repo_data['owner']['type'])): ?>
|
||||
<span class="git-embed-owner-type"><?php echo esc_html($repo_data['owner']['type']); ?></span>
|
||||
<?php endif; ?>
|
||||
<a href="<?php echo esc_url($repo_data['owner']['html_url']); ?>"
|
||||
target="_blank" rel="noopener" class="git-embed-owner-link">
|
||||
@<?php echo esc_html($repo_data['owner']['login']); ?>
|
||||
|
@ -534,7 +567,7 @@ class GitEmbedFeiCode {
|
|||
|
||||
<?php if ($show_description && $repo_data['description']): ?>
|
||||
<p class="git-embed-description">
|
||||
<span class="dashicons dashicons-text-page"></span>
|
||||
<span class="dashicons dashicons-editor-quote"></span>
|
||||
<?php echo esc_html($repo_data['description']); ?>
|
||||
</p>
|
||||
<?php endif; ?>
|
||||
|
@ -547,7 +580,7 @@ class GitEmbedFeiCode {
|
|||
<span class="git-embed-stat-value"><?php echo number_format_i18n($repo_data['stargazers_count']); ?></span>
|
||||
</span>
|
||||
<span class="git-embed-stat">
|
||||
<span class="dashicons dashicons-networking"></span>
|
||||
<span class="dashicons dashicons-share"></span>
|
||||
<span class="git-embed-stat-label">Forks:</span>
|
||||
<span class="git-embed-stat-value"><?php echo number_format_i18n($repo_data['forks_count']); ?></span>
|
||||
</span>
|
||||
|
@ -652,16 +685,20 @@ class GitEmbedFeiCode {
|
|||
private function get_download_url(array $repo_data): string {
|
||||
$platform = $repo_data['platform'] ?? 'github';
|
||||
$archive_url = $repo_data['archive_url'] ?? '';
|
||||
$default_branch = $repo_data['default_branch'] ?? 'main';
|
||||
|
||||
switch ($platform) {
|
||||
case 'github':
|
||||
$url = str_replace('{archive_format}', 'zipball', $archive_url);
|
||||
return str_replace('{/ref}', '/main', $url);
|
||||
return str_replace('{/ref}', "/{$default_branch}", $url);
|
||||
|
||||
case 'gitlab':
|
||||
case 'gitea':
|
||||
case 'forgejo':
|
||||
case 'custom':
|
||||
if (strpos($archive_url, 'main.zip') !== false && $default_branch !== 'main') {
|
||||
return str_replace('main.zip', $default_branch . '.zip', $archive_url);
|
||||
}
|
||||
return $archive_url;
|
||||
|
||||
default:
|
||||
|
@ -704,6 +741,7 @@ class GitEmbedFeiCode {
|
|||
|
||||
$platform = sanitize_text_field($_POST['platform'] ?? '');
|
||||
$custom_domain = sanitize_text_field($_POST['customDomain'] ?? '');
|
||||
$custom_site_name = sanitize_text_field($_POST['customSiteName'] ?? '');
|
||||
$owner = sanitize_text_field($_POST['owner'] ?? '');
|
||||
$repo = sanitize_text_field($_POST['repo'] ?? '');
|
||||
|
||||
|
@ -711,15 +749,20 @@ class GitEmbedFeiCode {
|
|||
wp_send_json_error('Repository information required');
|
||||
}
|
||||
|
||||
$this->clear_repository_cache($platform, $owner, $repo, $custom_domain);
|
||||
$this->clear_repository_cache($platform, $owner, $repo, $custom_domain, $custom_site_name);
|
||||
|
||||
wp_send_json_success('Cache cleared successfully');
|
||||
}
|
||||
|
||||
private function clear_repository_cache(string $platform, string $owner, string $repo, string $custom_domain = ''): void {
|
||||
$cache_key = "git_embed_{$platform}_{$owner}_{$repo}" . ($custom_domain ? "_{$custom_domain}" : '');
|
||||
private function clear_repository_cache(string $platform, string $owner, string $repo, string $custom_domain = '', string $custom_site_name = ''): void {
|
||||
$cache_key = "git_embed_{$platform}_{$owner}_{$repo}" . ($custom_domain ? "_{$custom_domain}" : '') . ($custom_site_name ? "_{$custom_site_name}" : '');
|
||||
delete_transient($cache_key);
|
||||
|
||||
if ($custom_domain) {
|
||||
$site_cache_key = 'git_embed_site_name_' . md5($custom_domain);
|
||||
delete_transient($site_cache_key);
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
$wpdb->query(
|
||||
$wpdb->prepare(
|
||||
|
|
42
style.css
42
style.css
|
@ -33,7 +33,7 @@
|
|||
background: #ffffff;
|
||||
border: 1px solid #d1d5da;
|
||||
border-radius: 8px;
|
||||
padding: 20px;
|
||||
padding: 30px;
|
||||
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
|
||||
transition: box-shadow 0.15s ease-in-out;
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
||||
|
@ -86,8 +86,8 @@
|
|||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
margin-bottom: 16px;
|
||||
padding-bottom: 12px;
|
||||
margin-bottom: 20px;
|
||||
padding-bottom: 20px;
|
||||
border-bottom: 1px solid #e1e4e8;
|
||||
}
|
||||
|
||||
|
@ -98,7 +98,7 @@
|
|||
}
|
||||
|
||||
.git-embed-site-name {
|
||||
font-size: 12px;
|
||||
font-size: 14px;
|
||||
color: #656d76;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
@ -157,7 +157,7 @@
|
|||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 12px;
|
||||
margin-bottom: 15px;
|
||||
flex-wrap: wrap;
|
||||
gap: 12px;
|
||||
}
|
||||
|
@ -176,7 +176,7 @@
|
|||
}
|
||||
|
||||
.git-embed-avatar {
|
||||
border-radius: 50%;
|
||||
border-radius: 15%;
|
||||
flex-shrink: 0;
|
||||
border: 2px solid #e1e4e8;
|
||||
}
|
||||
|
@ -239,8 +239,7 @@
|
|||
}
|
||||
|
||||
.git-embed-repo-icon {
|
||||
color: #0073aa;
|
||||
font-size: 16px;
|
||||
font-size: 20px;
|
||||
margin-top: 2px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
@ -259,23 +258,25 @@
|
|||
background: #f6f8fa;
|
||||
border: 1px solid #d1d5da;
|
||||
border-radius: 12px;
|
||||
padding: 4px 8px;
|
||||
font-size: 12px;
|
||||
padding: 1px 8px;
|
||||
font-size: 11px;
|
||||
font-weight: 500;
|
||||
color: #656d76;
|
||||
white-space: nowrap;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
gap: 1px;
|
||||
}
|
||||
|
||||
.git-embed-language .dashicons {
|
||||
font-size: 14px;
|
||||
line-height: 1.25;
|
||||
}
|
||||
|
||||
.git-embed-description {
|
||||
color: #656d76;
|
||||
font-size: 14px;
|
||||
font-size: 13px;
|
||||
font-weight: 400;
|
||||
line-height: 1.5;
|
||||
margin: 0 0 16px 0;
|
||||
display: flex;
|
||||
|
@ -286,13 +287,12 @@
|
|||
.git-embed-description .dashicons {
|
||||
margin-top: 2px;
|
||||
flex-shrink: 0;
|
||||
color: #0073aa;
|
||||
}
|
||||
|
||||
.git-embed-stats {
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
margin-bottom: 16px;
|
||||
margin-bottom: 20px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
|
@ -307,11 +307,11 @@
|
|||
|
||||
.git-embed-stat .dashicons {
|
||||
font-size: 16px;
|
||||
color: #0073aa;
|
||||
line-height: 1.25;
|
||||
}
|
||||
|
||||
.git-embed-stat-label {
|
||||
font-weight: 600;
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
.git-embed-stat-value {
|
||||
|
@ -342,6 +342,7 @@
|
|||
|
||||
.git-embed-button .dashicons {
|
||||
font-size: 16px;
|
||||
line-height: 1.25;
|
||||
}
|
||||
|
||||
.git-embed-button-primary {
|
||||
|
@ -633,6 +634,15 @@
|
|||
display: none;
|
||||
}
|
||||
|
||||
.git-embed-owner-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
margin-top: 4px;
|
||||
font-size: 12px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.git-embed-custom-domain-notice {
|
||||
background: #fff3cd;
|
||||
border: 1px solid #ffeaa7;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue