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

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

(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) {

spinner.destroy();

// if there is an error message, alert it
if ( ! response.success ) {
alert( response.data.message );
} else {
that.parent().html(gvUserActivation.success_message);
}

});

});
},

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.

5 changes: 4 additions & 1 deletion future/lib/class-gamajo-template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ public function get_template_part( $slug, $name = null, $load = true ) {

// Get files names of templates, for given slug and name.
$templates = $this->get_template_file_names( $slug, $name );

if($slug == 'note'){
$load = false;
var_dump($templates,$this->locate_template( $templates, false, false ));exit;
}
// Return the part that is found.
return $this->locate_template( $templates, $load, false );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ public static function display_note( $note, $show_delete = false, $context = nul
ob_start();
$context->template->get_template_part( 'note', $note_row_template, true );
$note_row = ob_get_clean();

} else {
/** @deprecated path */
ob_start();
Expand All @@ -534,7 +535,7 @@ public static function display_note( $note, $show_delete = false, $context = nul
'{row_class}' => 'gv-note',
'{note_detail}' => $note_detail_html,
);

var_dump($note_row);exit;
// Strip extra whitespace in template
$output = gravityview_strip_whitespace( $note_row );

Expand Down
186 changes: 186 additions & 0 deletions includes/fields/class-gravityview-field-user-activation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?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 {
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' ) );
}

/**
* Enqueues and localizes 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.svg',
)
);
}

/**
* Registers 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 );
}

/**
* Gets 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;
}

/**
* Checks if the user exists
*
* @since TBD
*
* @param array $form
* @param array $entry
*
* @return bool
*/
public static function check_if_user_exist( $form, $entry ) {
foreach ( $form['fields'] as $field ) {
if ( $field->type !== 'email' ) {
continue;
}

$email = rgar( $entry, $field->id );
if ( $email ) {
return email_exists( $email );
}
}

return false;
}

/**
* Checks 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;
}

/**
* Checks 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 ( ( isset( $feed['is_active'] ) && 1 !== (int) $feed['is_active'] ) || !isset( $feed['addon_slug'] ) || '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();
}
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Beautifully display your Gravity Forms entries. Learn more on [gravitykit.com](h
#### 🚀 Added
* New Layout Builder View type for creating custom layouts with single or multi-column configurations and adjustable widths.
* Support for Source ID meta added in Gravity Forms 2.9
* New User Activation field in the View editor to activate users added by the Gravity Forms User Registration add-on.
* `:initials` merge tag modifier for Name fields to display initials.
* `:format` merge tag modifier for field inputs (e.g., `{Event Field:1.1:format:Y-m-d}`).

Expand Down
54 changes: 54 additions & 0 deletions templates/fields/field-user_activation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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;
}

if ( ! class_exists( 'gf_user_registration' ) ) {
echo __( 'Install/activate Gravity Forms User Registration Add-On', 'gk-gravityview' );
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' ) ) {
gravityview()->log->error( 'GFUserSignups class does not exist', array() );
echo __( 'An error occurred', 'gk-gravityview' );
return;
}

$user_exist = GravityView_Field_User_Activation::check_if_user_exist( $gravityview->view->form, $entry );
if ( $user_exist ) {
echo __( 'The user is already active', 'gk-gravityview' );
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_attr_e( 'Activate User', 'gk-gravityview' ); ?>
</a>