'Elementor Pro form Custom PHP action adding +1 to post meta field 'votes' and collecting post ID in user meta field array 'voted_tokens'

I'm trying to create a vote button for custom posts type ('tokens'), I'm using the Elementor pro template with a form that contains only submit button and execute Collect Submissions and Custom PHP Script action with the help of e-addons. With a few lines of PHP, I should be able to update the meta field ('votes') to add +1 and also add the current Post ID to a Custom User Meta Field array ('voted_tokens') which contain all IDs of the posts the user vote on.

Can someone help with this 🙏

Then I can limit how many times users can vote on post by adding maybe hidden field 'email' that is predefined with current user email and adding this code to the custom PHP action

    global $wpdb;
    $form_id = $_POST['form_id']; // only for current form
    $email = $fields['email']; // change email field Custom ID if different
    $sql = 'SELECT count(*) FROM '.$wpdb->prefix.'e_submissions AS es,  '.$wpdb->prefix.'e_submissions_values AS esv WHERE es.id = esv.submission_id AND es.element_id = "'.$form_id.'" AND esv.key = "email" AND esv.value = "'.$email.'"';
    $count = intval(reset($wpdb->get_col( $sql )));
    if ($count > 1) { // you can set an higher limit, now single submission
        wp_send_json_error([
            'message' => 'You can vote once per post!', // customize error text
            'data' => $ajax_handler->data,
        ]);
    }
} ```
  


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source