ap_get_vote( integer $post_id, integer $user_id, string|array $type, string $value = '' )

Description #

Get a single vote from database.

Parameters #

  • $post_id
    integer (Required) Post ID.
  • $user_id
    integer (Required) User ID.
  • $type
    string | array (Required) Vote type.
  • $value
    string (Optional) Vote value. Default value: ''

Source #

File: includes/votes.php

function ap_get_vote( $post_id, $user_id, $type, $value = '' ) {
	global $wpdb;
	$where = "SELECT * FROM {$wpdb->ap_votes} WHERE 1=1 ";

	if ( ! empty( $type ) ) {
		if ( is_array( $type ) ) {
			$vote_type_in = sanitize_comma_delimited( $type, 'str' );

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

	if ( ! empty( $value ) ) {
		if ( is_array( $value ) ) {
			$value_in = sanitize_comma_delimited( $value, 'str' );

			if ( ! empty( $value_in ) ) {
				$where .= ' AND vote_value IN (' . $value_in . ')';
			}
		} else {
			$where .= $wpdb->prepare( ' AND vote_value = %s', $value );
		}
	}

	$query = $where . $wpdb->prepare( ' AND vote_post_id = %d AND  vote_user_id = %d LIMIT 1', $post_id, $user_id );

	$vote = $wpdb->get_row( $query ); // phpcs:ignore WordPress.DB

	if ( ! empty( $vote ) ) {
		return $vote;
	}

	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