'',
'theme' => 'dark',
'height' => '600px',
'show_line_numbers' => 'true',
'initial_file' => '',
'title' => ''
), $atts, 'github_code_viewer');
// Validate repository
if (empty($atts['repo'])) {
return '
Error: No repository specified. Use repo="owner/repository"
';
}
// Sanitize attributes
$repo = sanitize_text_field($atts['repo']);
$theme = in_array($atts['theme'], array('dark', 'light', 'monokai', 'solarized')) ? $atts['theme'] : 'dark';
// Validate height - only allow specific units
$height = sanitize_text_field($atts['height']);
if (!preg_match('/^\d+(px|%|em|rem|vh)$/', $height)) {
$height = '600px'; // Default to safe value if invalid
}
$show_line_numbers = filter_var($atts['show_line_numbers'], FILTER_VALIDATE_BOOLEAN);
$initial_file = sanitize_text_field($atts['initial_file']);
$title = sanitize_text_field($atts['title']);
// Detect platform from repo string
$platform = 'github'; // default
$display_repo = $repo;
if (strpos($repo, ':') !== false) {
list($platform, $display_repo) = explode(':', $repo, 2);
} elseif (strpos($repo, 'https://') === 0 || strpos($repo, 'http://') === 0) {
// Parse URL to detect platform
if (strpos($repo, 'gitlab.com') !== false) {
$platform = 'gitlab';
} elseif (strpos($repo, 'bitbucket.org') !== false) {
$platform = 'bitbucket';
} elseif (strpos($repo, 'codeberg.org') !== false) {
$platform = 'codeberg';
}
// Extract repo name from URL
$parsed = parse_url($repo);
if ($parsed && isset($parsed['path'])) {
$display_repo = trim($parsed['path'], '/');
}
}
// Generate unique ID for this instance
$viewer_id = 'mcb-' . uniqid();
// Build the viewer HTML
ob_start();
?>
GitHub Code Viewer
Select a file from the sidebar to view its content
Ready