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'] ) ) {
			$in_str = sanitize_comma_delimited( $args['vote_type'], 'str' );

			if ( ! empty( $in_str ) ) {
				$where .= ' AND vote_type IN (' . $in_str . ')';
			}
		} else {
			$where .= $wpdb->prepare( ' AND vote_type = %s', $args['vote_type'] );
		}
	}

	// Vote user id.
	if ( isset( $args['vote_user_id'] ) ) {
		if ( is_array( $args['vote_user_id'] ) ) {
			$vote_user_in = sanitize_comma_delimited( $args['vote_user_id'] );
			if ( ! empty( $vote_user_in ) ) {
				$where .= ' AND vote_user_id IN (' . $vote_user_in . ')';
			}
		} 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'] ) ) {
			$vote_actor_id_in = sanitize_comma_delimited( $args['vote_actor_id'] );
			if ( ! empty( $vote_actor_id_in ) ) {
				$where .= ' AND vote_actor_id IN (' . $vote_actor_id_in . ')';
			}
		} else {
			$where .= $wpdb->prepare( ' AND vote_actor_id = %d', $args['vote_actor_id'] );
		}
	}

	// Vote value.
	if ( isset( $args['vote_value'] ) ) {
		if ( is_array( $args['vote_value'] ) ) {
			$vote_value_in = sanitize_comma_delimited( $args['vote_value'], 'str' );
			if ( ! empty( $vote_value_in ) ) {
				$where .= ' AND vote_value IN (' . $vote_value_in . ')';
			}
		} else {
			$where .= $wpdb->prepare( ' AND vote_value = %s', $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