Guest User

Untitled

a guest
Jul 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. <?php
  2. add_filter( 'forminator_poll_handle_form_user_can_vote', 'my_limit_forminator_vote_once_per_wp_user', 10, 2 );
  3.  
  4. function my_limit_forminator_vote_once_per_wp_user( $can_vote, $poll_id ) {
  5. $POLL_ID_TO_LIMIT = 381;
  6.  
  7. // if not our poll, leave it
  8. if ( $POLL_ID_TO_LIMIT !== (int) $poll_id ) {
  9. return $can_vote;
  10. }
  11.  
  12. $user = wp_get_current_user();
  13.  
  14. // its not logged in
  15. if ( ! $user instanceof WP_User || empty( $user->ID ) ) {
  16. return false;
  17. }
  18.  
  19. // check if already vote
  20. $voters_post_meta = get_post_meta( $POLL_ID_TO_LIMIT, 'my_forminator_poll_voters', true );
  21. if ( empty( $voters_post_meta ) ) {
  22. $voters_post_meta = array();
  23. }
  24.  
  25. if ( in_array( $user->ID, $voters_post_meta ) ) {
  26. return false;
  27. }
  28.  
  29. return $can_vote;
  30. }
  31.  
  32. add_filter( 'forminator_polls_submit_response', 'my_limit_forminator_vote_once_per_wp_user_error_response', 10, 2 );
  33. add_filter( 'forminator_polls_ajax_submit_response', 'my_limit_forminator_vote_once_per_wp_user_error_response', 10, 2 );
  34. function my_limit_forminator_vote_once_per_wp_user_error_response( $response, $poll_id ) {
  35. $POLL_ID_TO_LIMIT = 381;
  36.  
  37. // if not our poll, leave it
  38. if ( $POLL_ID_TO_LIMIT !== (int) $poll_id ) {
  39. return $response;
  40. }
  41.  
  42. if ( ! $response['success'] ) {
  43. $response['message'] = 'Make sure you are logged in before vote, and not voted this poll yet.';
  44. }
  45.  
  46. return $response;
  47. }
  48.  
  49. add_filter( 'forminator_polls_submit_field_data', 'my_limit_forminator_vote_once_per_wp_user_save_wp_user_on_entry', 10, 2 );
  50. function my_limit_forminator_vote_once_per_wp_user_save_wp_user_on_entry( $field_data_array, $poll_id ) {
  51. $POLL_ID_TO_LIMIT = 381;
  52.  
  53. // if not our poll, leave it
  54. if ( $POLL_ID_TO_LIMIT !== (int) $poll_id ) {
  55. return $field_data_array;
  56. }
  57.  
  58. $user = wp_get_current_user();
  59.  
  60. $field_data_array[] = array(
  61. 'name' => 'my_forminator_wp_user_id',
  62. 'value' => $user->ID,
  63. );
  64.  
  65. return $field_data_array;
  66. }
  67.  
  68. add_action( 'forminator_polls_after_handle_submit', 'my_limit_forminator_vote_once_per_wp_user_save_user_on_post_meta', 10, 2 );
  69. add_action( 'forminator_polls_after_save_entry', 'my_limit_forminator_vote_once_per_wp_user_save_user_on_post_meta', 10, 2 );
  70. function my_limit_forminator_vote_once_per_wp_user_save_user_on_post_meta( $poll_id, $response ) {
  71. $POLL_ID_TO_LIMIT = 381;
  72.  
  73. // if not our poll, leave it
  74. if ( $POLL_ID_TO_LIMIT === (int) $poll_id ) {
  75. $user = wp_get_current_user();
  76. // its success
  77. if ( $response['success'] ) {
  78. $voters_post_meta = get_post_meta( $POLL_ID_TO_LIMIT, 'my_forminator_poll_voters', true );
  79. if ( empty( $voters_post_meta ) ) {
  80. $voters_post_meta = array();
  81. }
  82. $voters_post_meta[] = $user->ID;
  83. sort( $voters_post_meta );
  84. update_post_meta( $POLL_ID_TO_LIMIT, 'my_forminator_poll_voters', $voters_post_meta );
  85. }
  86. }
  87. }
Add Comment
Please, Sign In to add comment