Add polls feature: Beep_Polls class, poll UI in composer, voting support
This commit is contained in:
@@ -57,6 +57,9 @@ function beep_render_composer() {
|
||||
<button type="button" class="beep-gif-btn" data-beep-gif-btn title="Add GIF">
|
||||
<svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor"><path d="M11.5 9H13v6h-1.5V9zM9 9H6c-.6 0-1 .5-1 1v4c0 .5.4 1 1 1h3c.6 0 1-.5 1-1v-2H8.5v1.5H7v-3c0-.6-.4-1-1-1zm0 4.5H8v-3h1v3zm10-4.5h-3v6h1.5v-2h1v2H19V9c0-.6-.4-1-1-1z"/></svg>
|
||||
</button>
|
||||
<button type="button" class="beep-poll-btn" data-beep-poll-btn title="Add poll">
|
||||
<svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor"><path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm0 16H5V5h14v14zM7 12h2v5H7v-5zm4-3h2v8h-2V9zm4-3h2v11h-2V6z"/></svg>
|
||||
</button>
|
||||
</div>
|
||||
<div class="beep-compose-quote" data-beep-compose-quote hidden>
|
||||
<div class="beep-quote-input-row">
|
||||
@@ -72,6 +75,24 @@ function beep_render_composer() {
|
||||
<button class="beep-gif-remove" data-beep-gif-remove type="button" title="Remove">×</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="beep-compose-poll" data-beep-compose-poll hidden>
|
||||
<div class="beep-poll-input-row">
|
||||
<input type="text" class="beep-poll-question" data-beep-poll-question placeholder="Ask a question...">
|
||||
</div>
|
||||
<div class="beep-poll-options" data-beep-poll-options>
|
||||
<div class="beep-poll-option-row">
|
||||
<input type="text" class="beep-poll-option" data-beep-poll-option placeholder="Option 1" maxlength="100">
|
||||
</div>
|
||||
<div class="beep-poll-option-row">
|
||||
<input type="text" class="beep-poll-option" data-beep-poll-option placeholder="Option 2" maxlength="100">
|
||||
</div>
|
||||
</div>
|
||||
<div class="beep-poll-actions">
|
||||
<button type="button" class="beep-poll-add-option" data-beep-poll-add-option>+ Add option</button>
|
||||
<button type="button" class="beep-poll-remove-option" data-beep-poll-remove-option hidden>- Remove</button>
|
||||
<button type="button" class="beep-poll-remove" data-beep-poll-remove>Remove poll</button>
|
||||
</div>
|
||||
</div>
|
||||
<span class="beep-char-counter" data-beep-counter><?php echo (int) BEEP_CHAR_LIMIT; ?></span>
|
||||
<button class="beep-button beep-post-button" data-beep-submit disabled>Beep</button>
|
||||
</div>
|
||||
@@ -167,6 +188,51 @@ function beep_render_post($post) {
|
||||
</div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php
|
||||
$poll = Beep_Polls::get_poll_by_beep_id($post->ID);
|
||||
if ($poll) : ?>
|
||||
<div class="beep-poll-container" data-beep-poll data-beep-poll-id="<?php echo (int) $poll['id']; ?>">
|
||||
<div class="beep-poll-question"><?php echo esc_html($poll['question']); ?></div>
|
||||
<div class="beep-poll-options">
|
||||
<?php
|
||||
$user_id = get_current_user_id();
|
||||
$user_vote = $user_id ? Beep_Polls::get_user_vote($poll['id'], $user_id) : null;
|
||||
$has_ended = $poll['ends_at'] && strtotime($poll['ends_at']) < time();
|
||||
$show_results = $user_vote !== null || $has_ended || !is_user_logged_in();
|
||||
$results = Beep_Polls::get_results_with_percentages($poll);
|
||||
foreach ($results as $result) :
|
||||
$is_voted = $user_vote === $result['index'];
|
||||
?>
|
||||
<?php if ($show_results) : ?>
|
||||
<div class="beep-poll-result<?php echo $is_voted ? ' is-voted' : ''; ?><?php echo ($is_voted || ($result['percentage'] >= 50)) ? ' is-winning' : ''; ?>">
|
||||
<div class="beep-poll-result-bar" style="width:<?php echo (int) $result['percentage']; ?>%"></div>
|
||||
<span class="beep-poll-result-text">
|
||||
<?php echo esc_html($result['option']); ?>
|
||||
<span class="beep-poll-result-percent"><?php echo (int) $result['percentage']; ?>%</span>
|
||||
</span>
|
||||
</div>
|
||||
<?php else : ?>
|
||||
<button class="beep-poll-vote-btn"
|
||||
type="button"
|
||||
data-beep-poll-vote="<?php echo (int) $result['index']; ?>"
|
||||
data-beep-poll-beep="<?php echo (int) $post->ID; ?>">
|
||||
<?php echo esc_html($result['option']); ?>
|
||||
</button>
|
||||
<?php endif; ?>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<div class="beep-poll-meta">
|
||||
<span class="beep-poll-votes"><?php echo (int) $poll['total_votes']; ?> votes</span>
|
||||
<?php if ($poll['ends_at']) : ?>
|
||||
<?php if ($has_ended) : ?>
|
||||
<span class="beep-poll-end">Final results</span>
|
||||
<?php else : ?>
|
||||
<span class="beep-poll-end"><?php echo esc_html(date('M j', strtotime($poll['ends_at']))); ?> · Ends</span>
|
||||
<?php endif; ?>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<div class="beep-actions">
|
||||
<button class="beep-action beep-action-reply"
|
||||
data-beep-toggle-reply
|
||||
|
||||
Reference in New Issue
Block a user