Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- add_filter( 'forminator_poll_handle_form_user_can_vote', 'my_limit_forminator_vote_once_per_wp_user', 10, 2 );
- function my_limit_forminator_vote_once_per_wp_user( $can_vote, $poll_id ) {
- $POLL_ID_TO_LIMIT = 381;
- // if not our poll, leave it
- if ( $POLL_ID_TO_LIMIT !== (int) $poll_id ) {
- return $can_vote;
- }
- $user = wp_get_current_user();
- // its not logged in
- if ( ! $user instanceof WP_User || empty( $user->ID ) ) {
- return false;
- }
- // check if already vote
- $voters_post_meta = get_post_meta( $POLL_ID_TO_LIMIT, 'my_forminator_poll_voters', true );
- if ( empty( $voters_post_meta ) ) {
- $voters_post_meta = array();
- }
- if ( in_array( $user->ID, $voters_post_meta ) ) {
- return false;
- }
- return $can_vote;
- }
- add_filter( 'forminator_polls_submit_response', 'my_limit_forminator_vote_once_per_wp_user_error_response', 10, 2 );
- add_filter( 'forminator_polls_ajax_submit_response', 'my_limit_forminator_vote_once_per_wp_user_error_response', 10, 2 );
- function my_limit_forminator_vote_once_per_wp_user_error_response( $response, $poll_id ) {
- $POLL_ID_TO_LIMIT = 381;
- // if not our poll, leave it
- if ( $POLL_ID_TO_LIMIT !== (int) $poll_id ) {
- return $response;
- }
- if ( ! $response['success'] ) {
- $response['message'] = 'Make sure you are logged in before vote, and not voted this poll yet.';
- }
- return $response;
- }
- add_filter( 'forminator_polls_submit_field_data', 'my_limit_forminator_vote_once_per_wp_user_save_wp_user_on_entry', 10, 2 );
- function my_limit_forminator_vote_once_per_wp_user_save_wp_user_on_entry( $field_data_array, $poll_id ) {
- $POLL_ID_TO_LIMIT = 381;
- // if not our poll, leave it
- if ( $POLL_ID_TO_LIMIT !== (int) $poll_id ) {
- return $field_data_array;
- }
- $user = wp_get_current_user();
- $field_data_array[] = array(
- 'name' => 'my_forminator_wp_user_id',
- 'value' => $user->ID,
- );
- return $field_data_array;
- }
- add_action( 'forminator_polls_after_handle_submit', 'my_limit_forminator_vote_once_per_wp_user_save_user_on_post_meta', 10, 2 );
- add_action( 'forminator_polls_after_save_entry', 'my_limit_forminator_vote_once_per_wp_user_save_user_on_post_meta', 10, 2 );
- function my_limit_forminator_vote_once_per_wp_user_save_user_on_post_meta( $poll_id, $response ) {
- $POLL_ID_TO_LIMIT = 381;
- // if not our poll, leave it
- if ( $POLL_ID_TO_LIMIT === (int) $poll_id ) {
- $user = wp_get_current_user();
- // its success
- if ( $response['success'] ) {
- $voters_post_meta = get_post_meta( $POLL_ID_TO_LIMIT, 'my_forminator_poll_voters', true );
- if ( empty( $voters_post_meta ) ) {
- $voters_post_meta = array();
- }
- $voters_post_meta[] = $user->ID;
- sort( $voters_post_meta );
- update_post_meta( $POLL_ID_TO_LIMIT, 'my_forminator_poll_voters', $voters_post_meta );
- }
- }
- }
Add Comment
Please, Sign In to add comment