'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 = '
No beeps yet.
'; } 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(); ?>
Your account has been suspended from beeping.

Join in. Sign in to like and reply.

Log in Sign up
No beeps yet.
'; } ?>
\d+)', [ 'methods' => 'POST', 'callback' => [__CLASS__, 'upload_media'], 'permission_callback' => function () { return is_user_logged_in() && !beep_user_is_banned(get_current_user_id()); }, ]); register_rest_route('beep/v1', '/media/(?P\d+)/(?P\d+)', [ 'methods' => 'DELETE', 'callback' => [__CLASS__, 'delete_media'], 'permission_callback' => function () { return is_user_logged_in() && !beep_user_is_banned(get_current_user_id()); }, ]); } public static function upload_media($request) { $post_id = (int) $request->get_param('post_id'); $post = get_post($post_id); if (!$post || $post->post_type !== Beep_CPT::POST_TYPE) { return new WP_Error('not_found', 'Beep not found.', 404); } if (!current_user_can('edit_post', $post_id)) { return new WP_Error('forbidden', 'Cannot edit this beep.', 403); } if (empty($_FILES['file'])) { return new WP_Error('no_file', 'No file uploaded.'); } $result = Beep_Media::handle_upload($post_id, $_FILES['file']); if (is_wp_error($result)) { return $result; } return rest_ensure_response($result); } public static function delete_media($request) { $post_id = (int) $request->get_param('post_id'); $attachment_id = (int) $request->get_param('attachment_id'); $post = get_post($post_id); if (!$post || $post->post_type !== Beep_CPT::POST_TYPE) { return new WP_Error('not_found', 'Beep not found.', 404); } if (!current_user_can('edit_post', $post_id)) { return new WP_Error('forbidden', 'Cannot edit this beep.', 403); } Beep_Media::remove_image($post_id, $attachment_id); return rest_ensure_response(['ok' => true]); } }