ap_count_post_votes_by( string $by, string $value )

Description #

Count votes of a post and property format.

Parameters #

  • $by
    string (Required) By.
  • $value
    string (Required) Value.

Source #

File: includes/votes.php

function ap_count_post_votes_by( $by, $value ) {
	$bys = array( 'post_id', 'user_id', 'actor_id' );

	if ( ! in_array( $by, $bys, true ) ) {
		return false;
	}

	$new_counts = array(
		'votes_net'  => 0,
		'votes_down' => 0,
		'votes_up'   => 0,
	);
	$args       = array(
		'vote_type' => 'vote',
		'group'     => 'vote_value',
	);

	if ( 'post_id' === $by ) {
		$args['vote_post_id'] = $value;
	} elseif ( 'user_id' === $by ) {
		$args['vote_user_id'] = $value;
	} elseif ( 'actor_id' === $by ) {
		$args['vote_actor_id'] = $value;
	}

	$rows = ap_count_votes( $args );

	if ( false !== $rows ) {
		foreach ( (array) $rows as $row ) {
			if ( is_object( $row ) && isset( $row->count ) ) {
				$type                = '-1' == $row->vote_value ? 'votes_down' : 'votes_up'; // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
				$new_counts[ $type ] = (int) $row->count;
			}
		}
		$new_counts['votes_net'] = $new_counts['votes_up'] - $new_counts['votes_down'];
	}

	return $new_counts;
}

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