Files
wp-plugin-socialfeed/includes/shortcode.php
T

145 lines
6.6 KiB
PHP
Raw Normal View History

2026-11-18 01:22:00 +01:00
<?php
if (!defined('ABSPATH')) exit;
function hgsf_shortcode_sources(array $attributes): array {
$filter = array_map('trim', explode(',', strtolower($attributes['sources'])));
return array_intersect_key(HGSF_SOURCES, array_flip($filter));
}
function hgsf_shortcode($attributes = []): string {
$attributes = shortcode_atts(['limit' => 8, 'sources' => implode(',', array_keys(HGSF_SOURCES))], (array) $attributes, 'hg_socialfeed');
$limit = max(1, min(HGSF_POSTS_MAX, (int) $attributes['limit']));
$posts = [];
$profiles = [];
foreach (hgsf_shortcode_sources($attributes) as $source => $provider) {
$connection = hgsf_connection($source);
$profile_url = hgsf_profile_url($source, $connection);
if ($profile_url) {
$profiles[$source] = $profile_url;
}
foreach (hgsf_cached_posts($connection, $source) as $post) {
$posts[] = ['source' => $source] + $post;
}
}
if (!$profiles) {
return '';
}
$posts = array_slice(hgsf_sort_posts($posts), 0, HGSF_POSTS_MAX);
hgsf_feed_styles();
if (count($posts) > $limit) {
wp_enqueue_script('hg-socialfeed', plugins_url('assets/feed.js', HGSF_PLUGIN_FILE), [], HGSF_VERSION, ['in_footer' => true]);
}
if (in_array('x', array_column($posts, 'source'), true)) {
wp_enqueue_script('hg-socialfeed-x', 'https://platform.twitter.com/widgets.js', [], null, ['in_footer' => true, 'strategy' => 'async']);
}
$grid_id = wp_unique_id('hgsf-grid-');
ob_start();
?>
<div class="hgsf-feed" data-hgsf-limit="<?php echo esc_attr($limit); ?>">
<?php if ($posts) : ?>
<ul class="hgsf-grid" id="<?php echo esc_attr($grid_id); ?>" aria-label="Recente berichten op sociale media">
<?php foreach ($posts as $post) : ?>
<li tabindex="-1"<?php echo $post['source'] === 'x' ? ' class="hgsf-x-tile"' : ''; ?>>
<?php hgsf_render_post($post, count($profiles) > 1); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<div class="hgsf-actions">
<?php if (count($posts) > $limit) : ?>
<button type="button" class="hgsf-more" aria-controls="<?php echo esc_attr($grid_id); ?>" hidden>Meer berichten</button>
<?php endif; ?>
<?php foreach ($profiles as $source => $url) : ?>
<a class="hgsf-profile" href="<?php echo esc_url($url); ?>">Volg ons op <?php echo esc_html(HGSF_SOURCES[$source]['label']); ?> <span aria-hidden="true">↗</span></a>
<?php endforeach; ?>
</div>
</div>
<?php
return (string) ob_get_clean();
}
function hgsf_render_post(array $post, bool $show_source): void {
$source = $post['source'];
$platform = HGSF_SOURCES[$source]['label'];
$date = $post['date'] ? wp_date(get_option('date_format'), $post['date']) : '';
if ($source === 'x') : ?>
<blockquote class="twitter-tweet hgsf-x-embed" data-dnt="true" data-lang="<?php echo esc_attr(strtok(get_locale(), '_')); ?>">
<a href="<?php echo esc_url($post['link']); ?>">Bekijk bericht op X<?php echo $date ? esc_html(' · ' . $date) : ''; ?></a>
</blockquote>
<?php
return;
endif;
$label = match ($post['type']) {
'VIDEO' => 'Bekijk de video',
'CAROUSEL_ALBUM' => 'Bekijk de foto’s',
default => 'Bekijk het bericht',
};
$label .= ' op ' . $platform;
$has_image = $post['image'] !== '';
?>
<a class="hgsf-post<?php echo $has_image ? '' : ' hgsf-text-post'; ?>" href="<?php echo esc_url($post['link']); ?>"
<?php if ($has_image) : ?>aria-label="<?php echo esc_attr(trim($label . '. ' . $post['caption'])); ?>"<?php endif; ?>>
<?php if ($has_image) : ?>
<img src="<?php echo esc_url($post['image']); ?>" alt="" width="400" height="400" loading="lazy" decoding="async" referrerpolicy="no-referrer">
<?php if ($post['type'] === 'VIDEO') : ?>
<span class="hgsf-video" aria-hidden="true">&#9654;</span>
<?php endif; ?>
<?php if ($show_source) : ?>
<span class="hgsf-platform" aria-hidden="true"><?php echo esc_html($platform); ?></span>
<?php endif; ?>
<?php else : ?>
<span class="hgsf-text-platform"><?php echo esc_html($platform); ?></span>
<span><?php echo esc_html($post['caption'] ?: $label); ?></span>
<span class="hgsf-text-date"><?php echo esc_html($date); ?></span>
<?php endif; ?>
</a>
<?php
}
function hgsf_feed_styles(): void {
wp_enqueue_style('hg-socialfeed', plugins_url('assets/feed.css', HGSF_PLUGIN_FILE), [], HGSF_VERSION);
}
function hgsf_sociallinks_shortcode($attributes = []): string {
$attributes = shortcode_atts(['sources' => implode(',', array_keys(HGSF_SOURCES))], (array) $attributes, 'hg_sociallinks');
$links = [];
foreach (hgsf_shortcode_sources($attributes) as $source => $provider) {
$url = hgsf_profile_url($source, hgsf_connection($source));
if ($url) {
$links[$source] = $url;
}
}
if (!$links) {
return '';
}
hgsf_feed_styles();
ob_start();
?>
<ul class="hgsf-sociallinks" aria-label="Sociale media">
<?php foreach ($links as $source => $url) : ?>
<li><a href="<?php echo esc_url($url); ?>" aria-label="<?php echo esc_attr(HGSF_SOURCES[$source]['label']); ?>"><?php hgsf_social_icon($source); ?></a></li>
<?php endforeach; ?>
</ul>
<?php
return (string) ob_get_clean();
}
function hgsf_social_icon(string $source): void {
?>
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" aria-hidden="true" focusable="false">
<?php if ($source === 'instagram') : ?>
<rect x="3" y="3" width="18" height="18" rx="5" fill="none" stroke="currentColor" stroke-width="2"/>
<circle cx="12" cy="12" r="4" fill="none" stroke="currentColor" stroke-width="2"/>
<circle cx="17.5" cy="6.5" r="1.2" fill="currentColor"/>
<?php elseif ($source === 'facebook') : ?>
<path fill="currentColor" d="M24 12a12 12 0 1 0-13.875 11.854v-8.385H7.078V12h3.047V9.356c0-3.007 1.792-4.669 4.532-4.669 1.313 0 2.686.235 2.686.235v2.953H15.83c-1.49 0-1.955.925-1.955 1.874V12h3.328l-.532 3.469h-2.796v8.385A12 12 0 0 0 24 12Z"/>
<?php else : ?>
<path fill="currentColor" d="M18.901 1.153h3.68l-8.04 9.19L24 22.846h-7.406l-5.8-7.584-6.64 7.584H.47l8.6-9.835L0 1.154h7.594l5.243 6.932 6.064-6.933Zm-1.29 19.492h2.039L6.488 3.24H4.3Z"/>
<?php endif; ?>
</svg>
<?php
}