Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/2076 user registration add on allow user activation on the view #2180

72 changes: 72 additions & 0 deletions assets/js/field-user-activation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

(function( $ ) {

"use strict";

var self = {
/**
* @var {string} jQuery selector used to find approval target
*/
'selector': '.gv-user-activation-link'
};

$(function() {
self.userActivation();
});


self.userActivation = function(){
$(document).on('click', self.selector, function(e){
e.preventDefault();

var that = $(this);
var activationKey = that.attr('activation-key');

if (!confirm(gvUserActivation.confirm_message)) {
return;
}
var spinner = self.ajaxSpinner(that, 'margin-left:10px');

jQuery.post(gvUserActivation.ajax_url, {
key: activationKey,
action: 'gf_user_activate',
nonce: gvUserActivation.nonce
}, function (response) {

// if there is an error message, alert it
if ( ! response.success ) {

alert( response.data.message );
spinner.destroy();
omarkasem marked this conversation as resolved.
Show resolved Hide resolved

} else {

that.parent().html(gvUserActivation.success_message);
spinner.destroy();

}

});

});
},

self.ajaxSpinner = function(elem, style) {

this.elem = elem;
this.image = '<img src="' + gvUserActivation.spinner_url + '" style="' + style + '" />';

this.init = function () {
this.spinner = jQuery(this.image);
jQuery(this.elem).after(this.spinner);
return this;
}

this.destroy = function () {
jQuery(this.spinner).remove();
}

return this.init();
}

} (jQuery) );
1 change: 1 addition & 0 deletions assets/js/field-user-activation.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

166 changes: 166 additions & 0 deletions includes/fields/class-gravityview-field-user-activation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php
/**
* User Activation field for Gravityforms User Registration Add-On.
*
* @package GravityView
* @subpackage GravityView_Field_User_Activation
*/
class GravityView_Field_User_Activation extends GravityView_Field {

omarkasem marked this conversation as resolved.
Show resolved Hide resolved
var $name = 'user_activation';

var $group = 'gravityview';

var $contexts = array( 'single', 'multiple' );

var $icon = 'dashicons-unlock';

public function __construct() {

$this->label = esc_attr__( 'User Activation', 'gk-gravityview' );

$this->description = esc_attr__( 'Activate and deactivate users.', 'gk-gravityview' );

$this->add_hooks();

parent::__construct();
}

public function add_hooks() {
add_filter( 'gravityview_field_entry_value_' . $this->name . '_pre_link', array( $this, 'get_content' ), 10, 4 );

add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts_and_styles' ) );

// Make sure scripts are registered for FSE themes
add_action( 'gravityview/template/before', array( $this, 'register_scripts_and_styles' ) );

add_action( 'gravityview/field/user_activation/load_scripts', array( $this, 'enqueue_and_localize_script' ) );

add_action( 'gravityview_datatables_scripts_styles', array( $this, 'enqueue_and_localize_script' ) );
}

/**
* Enqueue and localize the script
*
* @since TBD
*/
public function enqueue_and_localize_script() {

// The script is already registered and enqueued
if ( wp_script_is( 'gv-user-activation', 'enqueued' ) ) {
return;
}
wp_enqueue_script( 'gv-user-activation' );

wp_localize_script(
'gv-user-activation',
'gvUserActivation',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'gf_user_activate' ),
'success_message' => esc_html__( 'User Activated Successfully!', 'gk-gravityview' ),
'confirm_message' => esc_html__( 'Are you sure you want to activate this user?', 'gk-gravityview' ),
'spinner_url' => GFCommon::get_base_url() . '/images/spinner.gif',
)
);

omarkasem marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Register the scripts and styles
*
* @since TBD
*/
public function register_scripts_and_styles() {
if ( wp_script_is( 'gv-user-activation' ) ) {
return;
}

$script_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';

wp_register_script( 'gv-user-activation', GRAVITYVIEW_URL . 'assets/js/field-user-activation' . $script_debug . '.js', array( 'jquery' ), GV_PLUGIN_VERSION, true );

}

/**
* Get the content of the field
*
* @since TBD
*
* @param string $output
* @param array $entry
* @param array $field_settings
* @param array $field
*
* @return string
*/
public function get_content( $output = '', $entry = array(), $field_settings = array(), $field = array() ) {

/** Overridden by a template. */
if ( ! empty( $field['field_path'] ) ) {
return $output;
}

return $output;
}

/**
* Check if the activation key is valid
*
* @since TBD
*
* @param string $activation_key
*
* @return WP_Error|true
*/
public static function check_activation_key( $activation_key ) {
global $wpdb;

$signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}signups WHERE activation_key = %s", $activation_key ) );

if ( empty( $signup ) ) {
return new WP_Error( 'invalid_key', __( 'Invalid activation key.', 'gk-gravityview' ) );
}

if ( $signup->active ) {
return new WP_Error( 'already_active', __( 'The user is already active.', 'gk-gravityview' ) );
}

return true;
}

/**
* Check if the feeds are valid and have the user activation value set to manual.
*
* @since TBD
*
* @param int $form_id
*
* @return bool
*/
public static function check_if_feeds_are_valid( $form_id ) {
$valid = false;
$feeds = GFAPI::get_feeds( null, $form_id );
if ( empty( $feeds ) ) {
return $valid;
}

foreach ( $feeds as $feed ) {
if ( 1 !== (int) $feed['is_active'] || 'gravityformsuserregistration' !== $feed['addon_slug'] ) {
continue;
}

if ( 'manual' === $feed['meta']['userActivationValue'] ) {
$valid = true;
break;
}
}

return $valid;
}

}

if ( class_exists( 'GF_User_Registration_Bootstrap' ) ) {
new GravityView_Field_User_Activation();
}
42 changes: 42 additions & 0 deletions templates/fields/field-user_activation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* The default date created field output template.
*
* @global \GV\Template_Context $gravityview
* @since 2.0
*/

if ( ! isset( $gravityview ) || empty( $gravityview->template ) ) {
gravityview()->log->error( '{file} template loaded without context', array( 'file' => __FILE__ ) );
return;
}


require_once gf_user_registration()->get_base_path() . '/includes/signups.php';

$entry = $gravityview->entry->as_entry();
if ( ! GravityView_Field_User_Activation::check_if_feeds_are_valid( $entry['form_id'] ) ) {
echo __( 'No feeds are found or feeds are not set to manual activation', 'gk-gravityview' );
return;
}

if ( ! class_exists( 'GFUserSignups' ) ) {
echo __( 'GFUserSignups class does not exist', 'gk-gravityview' );
omarkasem marked this conversation as resolved.
Show resolved Hide resolved
return;
}

$activation_key = GFUserSignups::get_lead_activation_key( $entry['id'] );
$user_activation = GravityView_Field_User_Activation::check_activation_key( $activation_key );
if ( is_wp_error( $user_activation ) ) {
echo $user_activation->get_error_message();
return;
}


do_action( 'gravityview/field/user_activation/load_scripts', $gravityview );

?>

<a href="#" activation-key="<?php echo $activation_key; ?>" class="button gv-user-activation-link" style="cursor: pointer;">
<?php esc_html_e( 'Activate User', 'gk-gravityview' ); ?>
omarkasem marked this conversation as resolved.
Show resolved Hide resolved
</a>
Loading