ap_add_post_vote( integer $post_id, boolean|integer $user_id, string $up_vote = true )

Description #

Add post vote.

Parameters #

  • $post_id
    integer (Required) Post ID.
  • $user_id
    boolean | integer (Required) ID of user casting vote.
  • $up_vote
    string (Optional) Is up vote. Default value: true

Source #

File: includes/votes.php

function ap_add_post_vote( $post_id, $user_id = 0, $up_vote = true ) {
	if ( false === $user_id ) {
		$user_id = get_current_user_id();
	}

	$_post       = get_post( $post_id );
	$rec_user_id = $_post->post_author;

	$value = $up_vote ? '1' : '-1';
	$row   = ap_vote_insert( $post_id, $user_id, 'vote', $rec_user_id, $value );

	if ( false !== $row ) {
		// Update qameta.
		$counts    = ap_update_votes_count( $post_id );
		$vote_type = $up_vote ? 'vote_up' : 'vote_down';

		/**
		 * Action ap_[vote_type]
		 * Action triggred after adding a vote for a post.
		 *
		 * @param integer $post_id Post ID.
		 * @param array   $counts All vote counts.
		 */
		do_action( 'ap_' . $vote_type, $post_id, $counts );

		return $counts;
	}

	return false;
}

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