ap_count_votes( array $args )

Description #

Get votes count.

Parameters #

  • $args
    array (Required) Arguments.
    • 'vote_post_id'
      (int) Post id.
    • 'vote_type'
      (string|array) Vote type.
    • 'vote_user_id'
      (int) User id,
    • 'vote_date'
      (string) Vote date.

Source #

File: includes/votes.php

function ap_count_votes( $args ) {
	global $wpdb;
	$args  = wp_parse_args( $args, array( 'group' => false ) );
	$where = 'SELECT count(*) as count';

	if ( $args['group'] ) {
		$where .= ', ' . esc_sql( sanitize_text_field( $args['group'] ) );
	}

	$where .= " FROM {$wpdb->ap_votes} WHERE 1=1 ";

	if ( isset( $args['vote_post_id'] ) ) {
		$where .= 'AND vote_post_id = ' . (int) $args['vote_post_id'];
	}

	if ( isset( $args['vote_type'] ) ) {
		if ( is_array( $args['vote_type'] ) ) {
			$where .= ' AND vote_type IN (' . sanitize_comma_delimited( $args['vote_type'], 'str' ) . ')';
		} else {
			$where .= " AND vote_type = '" . sanitize_text_field( $args['vote_type'] ) . "'";
		}
	}

	// Vote user id.
	if ( isset( $args['vote_user_id'] ) ) {
		if ( is_array( $args['vote_user_id'] ) ) {
			$where .= ' AND vote_user_id IN (' . sanitize_comma_delimited( $args['vote_user_id'] ) . ')';
		} else {
			$where .= " AND vote_user_id = '" . (int) $args['vote_user_id'] . "'";
		}
	}

	// Vote actor id.
	if ( isset( $args['vote_actor_id'] ) ) {
		if ( is_array( $args['vote_actor_id'] ) ) {
			$where .= ' AND vote_actor_id IN (' . sanitize_comma_delimited( $args['vote_actor_id'] ) . ')';
		} else {
			$where .= " AND vote_actor_id = '" . (int) $args['vote_actor_id'] . "'";
		}
	}

	// Vote value.
	if ( isset( $args['vote_value'] ) ) {
		if ( is_array( $args['vote_value'] ) ) {
			$where .= ' AND vote_value IN (' . sanitize_comma_delimited( $args['vote_value'], 'str' ) . ')';
		} else {
			$where .= " AND vote_value = '" . sanitize_text_field( $args['vote_value'] ) . "'";
		}
	}

	if ( $args['group'] ) {
		$where .= ' GROUP BY ' . esc_sql( sanitize_text_field( $args['group'] ) );
	}

	$rows = $wpdb->get_results( $where ); // phpcs:ignore WordPress.DB

	if ( false !== $rows ) {
		return $rows;
	}

	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