ap_vote_btn( int|object $post = null, bool $output = true )

Description #

Output or return voting button.

Parameters #

  • $post
    int | object (Optional) Post ID or object. Default value: null
  • $output
    bool (Optional) Echo or return vote button. Default value: true

Changelog #

VersionDescription
0.1Introduced.

Source #

File: includes/votes.php

function ap_vote_btn( $post = null, $output = true ) {
	$post = ap_get_post( $post );
	if ( ! $post || ( 'answer' === $post->post_type && ap_opt( 'disable_voting_on_answer' ) ) ) {
		return;
	}

	if ( 'question' === $post->post_type && ap_opt( 'disable_voting_on_question' ) ) {
		return;
	}

	$vote  = is_user_logged_in() ? ap_get_vote( $post->ID, get_current_user_id(), 'vote' ) : false;
	$voted = $vote ? true : false;

	if ( $vote && '1' === $vote->vote_value ) {
		$type = 'vote_up';
	} elseif ( $vote && '-1' === $vote->vote_value ) {
		$type = 'vote_down';
	} else {
		$type = '';
	}

	$data = array(
		'post_id' => $post->ID,
		'active'  => $type,
		'net'     => ap_get_votes_net(),
		'__nonce' => wp_create_nonce( 'vote_' . $post->ID ),
	);

	$upvote_message = sprintf(
		/* Translators: %s Question or Answer post type label for up voting the question or answer. */
		__( 'Up vote this %s', 'anspress-question-answer' ),
		( 'question' === $post->post_type ) ? esc_html__( 'question', 'anspress-question-answer' ) : esc_html__( 'answer', 'anspress-question-answer' )
	);
	$downvote_message = sprintf(
		/* Translators: %s Question or Answer post type label for down voting the question or answer. */
		__( 'Down vote this %s', 'anspress-question-answer' ),
		( 'question' === $post->post_type ) ? esc_html__( 'question', 'anspress-question-answer' ) : esc_html__( 'answer', 'anspress-question-answer' )
	);

	$html = '';

	$html .= '<div id="vote_' . $post->ID . '" class="ap-vote net-vote" ap-vote="' .
		esc_js( wp_json_encode( $data ) ) . '">' .
		'<a class="apicon-thumb-up ap-tip vote-up' . ( $voted ? ' voted' : '' ) .
		( $vote && 'vote_down' === $type ? ' disable' : '' ) . '" href="#" title="' .
		( $vote && 'vote_down' === $type ?
			__( 'You have already voted', 'anspress-question-answer' ) :
			( $voted ? __( 'Withdraw your vote', 'anspress-question-answer' ) : $upvote_message ) ) .
		'" ap="vote_up"></a>' .
		'<span class="net-vote-count" data-view="ap-net-vote" itemprop="upvoteCount" ap="votes_net">' .
		ap_get_votes_net() . '</span>';

	if ( ( 'question' === $post->post_type && ! ap_opt( 'disable_down_vote_on_question' ) ) ||
	( 'answer' === $post->post_type && ! ap_opt( 'disable_down_vote_on_answer' ) ) ) {
		$html .= '<a data-tipposition="bottom center" class="apicon-thumb-down ap-tip vote-down' .
			( $voted ? ' voted' : '' ) .
			( $vote && 'vote_up' === $type ? ' disable' : '' ) .
			'" href="#" title="' .
			( $vote && 'vote_up' === $type ? __( 'You have already voted', 'anspress-question-answer' ) :
			( $voted ? __( 'Withdraw your vote', 'anspress-question-answer' ) : $downvote_message ) ) .
			'" ap="vote_down"></a>';
	}

	$html .= '</div>';

	/**
	 * Allows overriding voting button HTML upload.
	 *
	 * @param string  $html Vote button.
	 * @param WP_Post $post WordPress post object.
	 * @since 4.1.5
	 */
	$html = apply_filters( 'ap_vote_btn_html', $html, $post );

	if ( ! $output ) {
		return $html;
	}

	echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment