Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active December 16, 2019 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/50ce770831da6558499450eb0fbd0412 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/50ce770831da6558499450eb0fbd0412 to your computer and use it in GitHub Desktop.
[Forminator] Disallow guests to vote in polls
<?php
/**
* Plugin Name: [Forminator] Disallow guests to vote in polls
* Plugin URI: https://premium.wpmudev.org/
* Description: This plugin disables voting for guest users (as of 1.10.2)
* Author: Alessandro Kaounas @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
die();
}
if ( ! class_exists( 'WPMUDEV_Forminator_Poll_Login_Vote' ) ) {
class WPMUDEV_Forminator_Poll_Login_Vote {
// Settings
private $url = ''; // Add url to redirect. Default: WP Login Page
private $forms = array( 73 ); // Form ids which limitation will apply seperated with commas
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_Forminator_Poll_Login_Vote();
}
return self::$_instance;
}
private function __construct() {
$this->init();
}
private function init(){
// Add CSS for guest users
add_action( "wp_enqueue_scripts", array( $this, "wpmudev_forminator_poll_login_note" ) );
// Add content before render the form
add_action( "forminator_before_form_render", array( $this, "wpmudev_forminator_init" ), 10, 2 );
// Prevent submissions from guests
add_action( "forminator_polls_before_handle_submit", array( $this, "wpmudev_forminator_polls_before_handle_submit" ) );
// Prevent AJAX submissions from guests
add_action( "forminator_polls_before_save_entry", array( $this, "wpmudev_forminator_polls_before_save_entry" ) );
}
public function wpmudev_forminator_init( $id, $form_type ){
if( ! in_array( $id, $this->forms ) && is_user_logged_in() ){
return;
}
// Include a login message to the form
add_filter( "forminator_poll_header", array( $this, "wpmudev_forminator_poll_header" ) );
// Add class to guest form
add_filter( "forminator_render_form_design_class", array( $this, "wpmudev_forminator_render_form_design_class" ) );
}
public function wpmudev_forminator_polls_before_handle_submit( $form_id ){
if( ! in_array( $id, $this->forms ) && is_user_logged_in() ){
return;
}
wp_die( __( 'Guests are not allowed to vote!', Forminator::DOMAIN ), __( 'Forminator error', Forminator::DOMAIN ), array( 'back_link' => true ) );
}
public function wpmudev_forminator_polls_before_save_entry( $form_id ){
if( ! in_array( $id, $this->forms ) && is_user_logged_in() ){
return;
}
$response = array(
'success' => false,
'notice' => 'error',
'message' => __( 'Guests are not allowed to vote!', Forminator::DOMAIN )
);
wp_send_json_error( $response );
exit;
}
public function wpmudev_forminator_poll_header( $html ){
if( ! is_user_logged_in() && ! isset( $_REQUEST['saved'] ) ){
$html = '<div class="forminator-poll-note"><p><a href="' . ( $this->url === '' ? wp_login_url() : $this->url ) .'">' . __( 'Login to vote', Forminator::DOMAIN ) . '</a></p></div>' . $html;
}
return $html;
}
public function wpmudev_forminator_render_form_design_class( $classes ){
if( ! is_user_logged_in() ){
$classes .= ' forminator-poll--guest';
}
return $classes;
}
public function wpmudev_forminator_poll_login_note(){
global $post;
if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'forminator_poll') ) {
wp_register_style( 'wpmudev_forminator_poll_login_note', false );
wp_enqueue_style( 'wpmudev_forminator_poll_login_note' );
wp_add_inline_style( 'wpmudev_forminator_poll_login_note', '
form.forminator-poll.forminator-poll--guest{
border: 2px solid #ddd !important;
padding: 10px !important;
}
form.forminator-poll--guest > .forminator-poll-note{
position: absolute;
display: flex;
z-index: 10;
top: 0;
left: 0;
width: 100%;
height: 100%;
flex-direction: column;
align-items: center;
justify-content: center;
}
form.forminator-poll--guest > .forminator-poll-note > p{
border: 2px solid #ddd;
padding: 5px 10px;
background: #fff;
}
form.forminator-poll--guest{
position: relative;
}
form.forminator-poll--guest::before {
content: "";
position: absolute;
background: #fff;
width: 100%;
height: 100%;
z-index: 10;
top: 0;
left: 0;
opacity: 0.75;
}'
);
}
}
}
if ( ! function_exists( 'wpmudev_forminator_poll_login_vote' ) ) {
function wpmudev_forminator_poll_login_vote() {
return WPMUDEV_Forminator_Poll_Login_Vote::get_instance();
};
add_action( 'plugins_loaded', 'wpmudev_forminator_poll_login_vote', 99 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment