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 ) ) {
			$where .= ' AND vote_type IN (' . sanitize_comma_delimited( $type, 'str' ) . ')';
		} else {
			$where .= " AND vote_type = '" . sanitize_text_field( $type ) . "'";
		}
	}

	if ( ! empty( $value ) ) {
		if ( is_array( $value ) ) {
			$where .= ' AND vote_value IN (' . sanitize_comma_delimited( $value, 'str' ) . ')';
		} else {
			$where .= " AND vote_value = '" . sanitize_text_field( $value ) . "'";
		}
	}

	$vote = $wpdb->get_row( $where . $wpdb->prepare( ' AND vote_post_id = %d AND  vote_user_id = %d LIMIT 1', $post_id, $user_id ) ); // 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