Initial commit: Beep WordPress plugin
- Custom post type with likes, replies, moderation, shortcodes - Beep icon and logo assets - CSS and JS for frontend
This commit is contained in:
Executable
+133
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Beep_Shortcode {
|
||||
|
||||
public static function init() {
|
||||
add_shortcode('beep_feed', [__CLASS__, 'render_feed']);
|
||||
add_action('rest_api_init', [__CLASS__, 'register_routes']);
|
||||
}
|
||||
|
||||
public static function register_routes() {
|
||||
register_rest_route('beep/v1', '/feed', [
|
||||
'methods' => 'GET',
|
||||
'callback' => [__CLASS__, 'fetch_feed'],
|
||||
'permission_callback' => '__return_true',
|
||||
'args' => [
|
||||
'sort' => ['default' => 'latest'],
|
||||
'limit' => ['default' => 50, 'validate_callback' => function ($v) { return is_numeric($v); }],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* REST endpoint: re-render the feed list with the given sort.
|
||||
*/
|
||||
public static function fetch_feed($request) {
|
||||
$sort = $request->get_param('sort') === 'top' ? 'top' : 'latest';
|
||||
$limit = max(1, (int) $request->get_param('limit'));
|
||||
$posts = ($sort === 'top') ? self::get_top_beeps($limit) : self::get_latest_beeps($limit);
|
||||
|
||||
$html = '';
|
||||
foreach ($posts as $p) {
|
||||
$html .= beep_render_post($p);
|
||||
}
|
||||
if ($html === '') {
|
||||
$html = '<div class="beep-empty">No beeps yet.</div>';
|
||||
}
|
||||
return rest_ensure_response(['html' => $html, 'sort' => $sort]);
|
||||
}
|
||||
|
||||
public static function get_latest_beeps($limit) {
|
||||
global $wpdb;
|
||||
return $wpdb->get_results($wpdb->prepare(
|
||||
"SELECT * FROM {$wpdb->posts}
|
||||
WHERE post_type = %s AND post_status = 'publish'
|
||||
ORDER BY post_date DESC
|
||||
LIMIT %d",
|
||||
Beep_CPT::POST_TYPE, $limit
|
||||
));
|
||||
}
|
||||
|
||||
public static function get_top_beeps($limit) {
|
||||
global $wpdb;
|
||||
$likes = Beep_Likes::table_name();
|
||||
return $wpdb->get_results($wpdb->prepare(
|
||||
"SELECT p.*, COALESCE(l.cnt, 0) AS like_count
|
||||
FROM {$wpdb->posts} p
|
||||
LEFT JOIN (
|
||||
SELECT post_id, COUNT(*) AS cnt
|
||||
FROM $likes
|
||||
WHERE object_type = 'post'
|
||||
GROUP BY post_id
|
||||
) l ON l.post_id = p.ID
|
||||
WHERE p.post_type = %s AND p.post_status = 'publish'
|
||||
ORDER BY COALESCE(l.cnt, 0) DESC, p.post_date DESC
|
||||
LIMIT %d",
|
||||
Beep_CPT::POST_TYPE, $limit
|
||||
));
|
||||
}
|
||||
|
||||
public static function render_feed($atts) {
|
||||
$atts = shortcode_atts([
|
||||
'limit' => 50,
|
||||
'header' => 'Messages from',
|
||||
'sort' => 'latest',
|
||||
], $atts, 'beep_feed');
|
||||
|
||||
$limit = max(1, (int) $atts['limit']);
|
||||
$sort = ($atts['sort'] === 'top') ? 'top' : 'latest';
|
||||
$posts = ($sort === 'top') ? self::get_top_beeps($limit) : self::get_latest_beeps($limit);
|
||||
$has_posts = !empty($posts);
|
||||
|
||||
ob_start();
|
||||
?>
|
||||
<div class="beep-feed" data-beep-feed>
|
||||
<div class="beep-feed-header">
|
||||
<span class="beep-feed-header-text"><?php echo esc_html($atts['header']); ?></span>
|
||||
<?php echo beep_logo_svg(); ?>
|
||||
</div>
|
||||
|
||||
<?php if ($has_posts) : ?>
|
||||
<div class="beep-tabs" data-beep-tabs role="tablist">
|
||||
<button type="button" class="beep-tab <?php echo $sort === 'latest' ? 'is-active' : ''; ?>"
|
||||
data-beep-sort="latest" role="tab">Latest</button>
|
||||
<button type="button" class="beep-tab <?php echo $sort === 'top' ? 'is-active' : ''; ?>"
|
||||
data-beep-sort="top" role="tab">Top</button>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (is_user_logged_in() && !beep_user_is_banned(get_current_user_id())) : ?>
|
||||
<?php echo beep_render_composer(); ?>
|
||||
<?php elseif (is_user_logged_in()) : ?>
|
||||
<div class="beep-notice beep-notice-banned">
|
||||
Your account has been suspended from beeping.
|
||||
</div>
|
||||
<?php else : ?>
|
||||
<div class="beep-notice beep-notice-login">
|
||||
<p><strong>Join in.</strong> Sign in to like and reply.</p>
|
||||
<a class="beep-button" href="<?php echo esc_url(wp_login_url(get_permalink())); ?>">Log in</a>
|
||||
<?php if (get_option('users_can_register')) : ?>
|
||||
<a class="beep-button beep-button-secondary" href="<?php echo esc_url(wp_registration_url()); ?>">Sign up</a>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="beep-list" data-beep-list>
|
||||
<?php
|
||||
if ($has_posts) {
|
||||
foreach ($posts as $p) {
|
||||
echo beep_render_post($p);
|
||||
}
|
||||
} else {
|
||||
echo '<div class="beep-empty">No beeps yet.</div>';
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
return ob_get_clean();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user