Commit a6913474 authored by KR Moorhouse's avatar KR Moorhouse

Updated current codebase to handle the new opt-in modal. Updated response to...

Updated current codebase to handle the new opt-in modal. Updated response to handle new modal calls.
parent 66bdb108
This diff is collapsed.
This diff is collapsed.
......@@ -84,7 +84,6 @@ define( [ 'views/sections/widgets.js', 'views/sections/apps.js', 'views/sections
// If form telemetry is defined...
// AND if we should run it...
if ( 'undefined' !== typeof nfAdmin.formTelemetry && 1 == nfAdmin.formTelemetry ) {
console.log( 'got here' );
// Make our AJAX call.
var data = {
action: 'nf_form_telemetry',
......@@ -93,6 +92,155 @@ define( [ 'views/sections/widgets.js', 'views/sections/apps.js', 'views/sections
// Make our AJAX call.
jQuery.post( ajaxurl, data );
}
// If the user has not seen the opt-in modal yet...
if ( '1' == nfAdmin.showOptin ) {
// Declare all of our opt-in code here.
var optinModal = new jBox( 'Modal', {
closeOnEsc: false,
closeOnClick: false,
width: 400
} );
// Define the modal title.
var title = document.createElement( 'div' );
title.id = 'optin-modal-title';
var titleStyling = document.createElement( 'h2' );
titleStyling.style.fontSize = '130%';
titleStyling.innerHTML = 'Help make Ninja Forms better!';
titleStyling.style.textAlign = 'left';
title.appendChild( titleStyling );
// Define the modal content.
var content = document.createElement( 'div' );
content.style.padding = '5px 15px';
var p = document.createElement( 'p' );
p.innerHTML = 'We would like to collect data about how Ninja Forms is used so that we can improve the experience for everyone. This data will not include ANY submission data or personally identifiable information.';
content.appendChild( p );
var text = document.createTextNode( 'Please check out our ' );
var privacy = document.createElement( 'a' );
privacy.setAttribute( 'href', 'https://ninjaforms.com/privacy-policy/' );
privacy.setAttribute( 'target', '_blank' );
privacy.innerHTML = 'privacy policy';
p = document.createElement( 'p' );
p.appendChild( text );
p.appendChild( privacy );
text = document.createTextNode ( ' for additional clarification.' );
p.appendChild( text );
content.appendChild( p );
p = document.createElement( 'p' );
var checkBox = document.createElement( 'input' );
checkBox.id = 'optin-send-email';
checkBox.setAttribute( 'type', 'checkbox' );
checkBox.style.margin = '7px';
var label = document.createElement( 'label' );
label.setAttribute( 'for', 'optin-send-email' );
label.innerHTML = ' Yes, please send me occasional emails about Ninja Forms.';
p.appendChild( checkBox );
p.appendChild( label );
content.appendChild( p );
p = document.createElement( 'p' );
p.id = 'optin-block';
p.style.paddingTop = '10px';
p.style.display = 'none';
var email = document.createElement( 'input' );
email.id = 'optin-email-address';
email.setAttribute( 'type', 'text' );
email.setAttribute( 'value', nfAdmin.currentUserEmail );
email.style.width = '100%';
email.style.fontSize = '16px';
p.appendChild( email );
content.appendChild( p );
var spinner = document.createElement( 'span' );
spinner.id = 'optin-spinner';
spinner.classList.add( 'spinner' );
content.appendChild( spinner );
var actions = document.createElement( 'div' );
actions.id = 'optin-buttons';
actions.style.paddingTop = '15px';
actions.style.width = '100%';
actions.style.clear = 'both';
var cancel = document.createElement( 'button' );
cancel.id = 'optout';
cancel.classList.add( 'button-secondary' );
cancel.style.marginBottom = '10px';
cancel.style.fontSize = '16px';
cancel.innerHTML = 'Not Now';
actions.appendChild( cancel );
var confirm = document.createElement( 'button' );
confirm.id = 'optin';
confirm.classList.add( 'button-primary' );
confirm.style.marginBottom = '10px';
confirm.style.fontSize = '16px';
confirm.style.float = 'right';
confirm.innerHTML = 'Yes, I agree!';
actions.appendChild( confirm );
content.appendChild( actions );
// Define the success title.
var successTitle = document.createElement( 'h2' );
successTitle.style.fontSize = '130%';
successTitle.innerHTML = 'Keep being awesome!';
// Define the success content.
var successContent = document.createElement( 'div' );
successContent.id = 'optin-thankyou';
successContent.style.padding = '10px 15px';
successContent.style.fontSize = '120%';
successContent.innerHTML = 'Thank you for opting in!';
// Set the options for the modal and open it.
optinModal.setContent( document.createElement( 'div' ).appendChild( content ).innerHTML );
optinModal.setTitle( document.createElement( 'div' ).appendChild( title ).innerHTML );
optinModal.open();
// Show/Hide email field, based on the opt-in checkbox.
jQuery( '#optin-send-email' ).click( function( e ) {
if( jQuery( this ).is( ':checked' ) ) {
jQuery( '#optin-block' ).show();
} else {
jQuery( '#optin-block' ).hide();
}
} );
// Setup the optin click event.
jQuery( '#optin' ).click( function( e ) {
var sendEmail;
if ( jQuery( '#optin-send-email' ).attr( 'checked' ) ) {
sendEmail = 1;
userEmail = jQuery( '#optin-email-address' ).val();
} else {
sendEmail = 0;
userEmail = '';
}
// Show spinner
jQuery( '#optin-spinner' ).css( 'visibility', 'visible' );
jQuery( '#optin-buttons' ).css( 'visibility', 'hidden' );
// Hit AJAX endpoint and opt-in.
jQuery.post( ajaxurl, { action: 'nf_optin', ninja_forms_opt_in: 1, send_email: sendEmail, user_email: userEmail },
function( response ) {
jQuery( '#optin-spinner' ).css( 'visibility', 'hidden' );
optinModal.setTitle( document.createElement( 'div' ).appendChild( successTitle ).innerHTML );
optinModal.setContent( document.createElement( 'div' ).appendChild( successContent ).innerHTML );
/**
* When we get a response from our endpoint, show a thank you and set a timeout
* to close the modal.
*/
setTimeout (
function(){
optinModal.close();
},
2000
);
} );
} );
// Setup the optout click event.
jQuery( '#optout' ).click( function( e ) {
// Show spinner
jQuery( '#optin-spinner' ).css( 'visibility', 'visible' );
jQuery( '#optin-buttons' ).css( 'visibility', 'hidden' );
// Hit AJAX endpoint and opt-in.
jQuery.post( ajaxurl, { action: 'nf_optin', ninja_forms_opt_in: 0 }, function( response ) {
jQuery( '#optin-spinner' ).css( 'visibility', 'hidden' );
// When we get a response from our endpoint, close the modal.
optinModal.close();
} );
} );
}
},
templateContext: function() {
......
......@@ -20,7 +20,7 @@ jQuery(document).ready(function($) {
} else {
jQuery( '#optin-block' ).hide();
}
} )
} );
jQuery( '#optin' ).click( function( e ) {
var sendEmail;
......
......@@ -127,10 +127,14 @@ final class NF_Admin_Menus_Forms extends NF_Abstracts_Menu
wp_enqueue_script( 'nf-moment', Ninja_Forms::$url . 'assets/js/lib/moment-with-locales.min.js', array( 'jquery', 'nf-dashboard' ) );
wp_enqueue_script( 'nf-dashboard', Ninja_Forms::$url . 'assets/js/min/dashboard.min.js', array( 'backbone-radio', 'backbone-marionette-3' ) );
$current_user = wp_get_current_user();
wp_localize_script( 'nf-dashboard', 'nfi18n', Ninja_Forms::config( 'i18nDashboard' ) );
wp_localize_script( 'nf-dashboard', 'nfAdmin', array(
'ajaxNonce' => wp_create_nonce( 'ninja_forms_dashboard_nonce' ),
'formTelemetry' => ( get_option( 'nf_form_tel_sent' ) ) ? 0 : 1
'formTelemetry' => ( get_option( 'nf_form_tel_sent' ) ) ? 0 : 1,
'showOptin' => ( get_option( 'ninja_forms_do_not_allow_tracking' ) ||
get_option( 'ninja_forms_allow_tracking' ) ) ? 0 : 1,
'currentUserEmail' => $current_user->user_email
) );
wp_enqueue_style( 'nf-builder', Ninja_Forms::$url . 'assets/css/builder.css' );
......
......@@ -201,14 +201,9 @@ final class NF_Dispatcher
}
/**
* Email address of the current user, defaulting to admin email if they do not have one.
* Email address of the current user.
*/
$current_user = wp_get_current_user();
if ( ! empty ( $current_user->user_email ) ) {
$email = $current_user->user_email;
} else {
$email = get_option( 'admin_email' );
}
$email = $data[ 'user_email' ];
$site_data = array(
'url' => site_url(),
......
......@@ -26,15 +26,11 @@ final class NF_Tracking
*/
public function __construct()
{
if( isset( $_GET[ self::FLAG ] ) ){
add_action( 'admin_init', array( $this, 'maybe_opt_in' ) );
}
// Temporary: Report previously opted-in users that were not already reported. @todo Remove after a couple of versions.
add_action( 'admin_init', array( $this, 'report_optin' ) );
// add_filter( 'nf_admin_notices', array( $this, 'admin_notice' ) );
add_action( 'wp_ajax_nf_optin', array( $this, 'maybe_opt_in' ) );
add_filter( 'ninja_forms_check_setting_allow_tracking', array( $this, 'check_setting' ) );
add_filter( 'ninja_forms_update_setting_allow_tracking', array( $this, 'update_setting' ) );
}
......@@ -49,7 +45,7 @@ final class NF_Tracking
{
if( $this->can_opt_in() ) {
$opt_in_action = htmlspecialchars( $_GET[ self::FLAG ] );
$opt_in_action = htmlspecialchars( $_POST[ self::FLAG ] );
if( self::OPT_IN == $opt_in_action ){
$this->opt_in();
......@@ -59,7 +55,8 @@ final class NF_Tracking
$this->opt_out();
}
}
header( 'Location: ' . admin_url( 'admin.php?page=ninja-forms' ) );
die( 1 );
// header( 'Location: ' . admin_url( 'admin.php?page=ninja-forms' ) );
}
/**
......@@ -73,7 +70,8 @@ final class NF_Tracking
if( get_option( 'ninja_forms_optin_reported', 0 ) ) return;
$data = wp_parse_args( $data, array(
'send_email' => 1 // "Send Email" by default, if not specified (legacy).
'send_email' => 0, // Do not send email by default.
'user_email' => ''
) );
Ninja_Forms()->dispatcher()->send( 'optin', $data );
......@@ -83,61 +81,6 @@ final class NF_Tracking
update_option( 'ninja_forms_optin_reported', 1 );
}
/**
* Register the Admin Notice for asking users to opt in to tracking
*
* @access public
* @hook nf_admin_notices
* @param array $notices
* @return array $notices
*/
public function admin_notice( $notices )
{
// Check if the user is allowed to opt in.
if( ! $this->can_opt_in() ) return $notices;
// Check if the user is already opted in/out.
if( $this->is_opted_in() || $this->is_opted_out() ) return $notices;
$notices[ 'allow_tracking' ] = array(
'title' => __( 'Please help us improve Ninja Forms!', 'ninja-forms' ),
'msg' => '
If you agree, we will collect some server data and information about how you use Ninja Forms.
<em>No submission data will be collected.</em>
This data will help us troubleshoot errors and improve your Ninja Forms experience.
<p>
<input id="nf-optin-send-email" type="checkbox" checked="checked"> You can also occasionally send me an email about using Ninja Forms.
</p>',
'link' => implode( ' ', array(
sprintf( __( '%sYes, I want to make Ninja Forms better!%s', 'ninja-forms' ), '<a href="' . $this->get_opt_in_url( admin_url( 'admin.php?page=ninja-forms' ) ) . '" class="button-primary" id="ninja-forms-improve">', '</a>' ),
sprintf( __( '%sNo, please don\'t collect errors or other data.%s', 'ninja-forms' ), '<a href="' . $this->get_opt_out_url( admin_url( 'admin.php?page=ninja-forms' ) ) . '" class="button-secondary" id="ninja-forms-do-not-improve">', '</a>' ),
)),
'int' => 0, // No delay
'blacklist' => array(
'ninja-forms-three'
)
);
echo "<script type='text/javascript'>
jQuery( document ).ready( function( $ ) {
jQuery( '#ninja-forms-improve' ).click( function( e ) {
e.preventDefault();
var send_email, url;
if ( jQuery( '#nf-optin-send-email' ).attr( 'checked' ) ) {
send_email = 1;
} else {
send_email = 0;
}
url = jQuery( e.target ).attr( 'href' );
window.location.href = url + '&send_email=' + send_email;
} );
} );
</script>";
return $notices;
}
/**
* Check if the current user is allowed to opt in on behalf of a site
*
......@@ -195,23 +138,13 @@ final class NF_Tracking
*/
if ( isset ( $_REQUEST[ 'send_email' ] ) ) {
$send_email = absint( $_REQUEST[ 'send_email' ] );
$user_email = $_REQUEST[ 'user_email' ];
} else {
$send_email = 1;
$send_email = 0;
$user_email = '';
}
$this->report_optin( array( 'send_email' => $send_email ) );
}
/**
* Get the Opt In URL
*
* @access private
* @param string $url
* @return string $url
*/
private function get_opt_in_url( $url )
{
return add_query_arg( 'ninja_forms_opt_in', self::OPT_IN, $url );
$this->report_optin( array( 'send_email' => $send_email, 'user_email' => $user_email ) );
}
/**
......@@ -253,18 +186,6 @@ final class NF_Tracking
update_option( 'ninja_forms_optin_reported', 0 );
}
/**
* Get the Opt Out URL
*
* @access private
* @param string $url
* @return string $url
*/
private function get_opt_out_url( $url )
{
return add_query_arg( 'ninja_forms_opt_in', self::OPT_OUT, $url );
}
public function check_setting( $setting )
{
if( $this->is_opted_in() && ! $this->is_opted_out() ) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment