Commit baa4021b authored by KR Moorhouse's avatar KR Moorhouse Committed by Kenny Hall

Dispatch forms (#3458)

* Added new function to Dispatcher to handle sending form data to API. (Unfinished)

* Added new end point for sending form data to dispatch.

* Added AJAX call to dashboard view that will load up and send a single form at a time to API.

* Completed form data dispatch method. Updated endpoint to expect a security nonce. Defined localized variable nfAdmin to be used by the dashboard page.

* Corrected typo. Added since to doc block in DispatchPoints.php.
parent 81828a07
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -81,6 +81,18 @@ define( [ 'views/sections/widgets.js', 'views/sections/apps.js', 'views/sections
var childView = new WidgetView();
}
this.showChildView('content', childView );
// 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',
security: nfAdmin.ajaxNonce
}
// Make our AJAX call.
jQuery.post( ajaxurl, data );
}
},
templateContext: function() {
......
......@@ -9,10 +9,13 @@ class NF_AJAX_Controllers_DispatchPoints
{
// Add out ajax end points.
add_action( 'wp_ajax_nf_undo_click', array( $this, 'undo_click' ) );
add_action( 'wp_ajax_nf_form_telemetry', array( $this, 'form_telemetry' ) );
}
/*
* Function called when the undo manager is used in the builder.
*
* @since 3.2
*/
public function undo_click() {
// Make sure we have a valid nonce.
......@@ -22,4 +25,18 @@ class NF_AJAX_Controllers_DispatchPoints
// Exit.
die( 1 );
}
/*
* Function to startup our form data telemtry.
*
* @since 3.2
*/
public function form_telemetry() {
// Make sure we have a valid nonce.
check_ajax_referer( 'ninja_forms_dashboard_nonce', 'security' );
// Send the action to our dispatcher.
Ninja_Forms()->dispatcher()->form_data();
// Exit.
die( 1 );
}
}
\ No newline at end of file
......@@ -128,6 +128,10 @@ final class NF_Admin_Menus_Forms extends NF_Abstracts_Menu
wp_enqueue_script( 'nf-dashboard', Ninja_Forms::$url . 'assets/js/min/dashboard.min.js', array( 'backbone-radio', 'backbone-marionette-3' ) );
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
) );
wp_enqueue_style( 'nf-builder', Ninja_Forms::$url . 'assets/css/builder.css' );
wp_enqueue_style( 'nf-dashboard', Ninja_Forms::$url . 'assets/css/dashboard.min.css' );
......
......@@ -91,6 +91,83 @@ final class NF_Dispatcher
$this->send( 'update_environment_vars', $environment );
}
/**
* Package up our form data and send it to our API endpoint.
*
* @since 3.2
* @return void
*/
public function form_data() {
global $wpdb;
// If we have not finished the process...
if ( ! get_option( 'nf_form_tel_sent' ) || 'false' == get_option( 'nf_form_tel_sent' ) ) {
// Get our list of already processed forms (if it exists).
$forms_ref = get_option( 'nf_form_tel_data' );
// Get a list of Forms on this site.
$sql = "SELECT id FROM `" . $wpdb->prefix . "nf3_forms`";
$forms = $wpdb->get_results( $sql, 'ARRAY_A' );
// If our list of processed forms already exists...
if ( ! empty( $forms_ref ) ) {
// Break those into an array.
$forms_ref = explode( ',', $forms_ref );
} // Otherwise...
else {
// Make sure we have an array.
$forms_ref = array();
}
$match_found = false;
// For each form...
foreach ( $forms as $form ) {
// If the current form is not in our list of sent values...
if ( ! in_array( $form[ 'id' ], $forms_ref ) ) {
// Set our target ID.
$id = $form[ 'id' ];
// Record that we found a match.
$match_found = true;
}
}
// If we didn't find a match.
if ( ! $match_found ) {
// Record that we're done.
update_option( 'nf_form_tel_sent', 'true', false );
// Exit.
return false;
}// Otherwise... (We did find a match.)
// Get our form.
$form_data = Ninja_Forms()->form( intval( $id ) )->get();
// Setup our data value.
$data = array();
// Set the form title.
$data[ 'title' ] = $form_data->get_setting( 'title' );
$sql = "SELECT COUNT(meta_id) AS total FROM `" . $wpdb->prefix . "postmeta` WHERE meta_key = '_form_id' AND meta_value = '" . intval( $id ) . "'";
$result = $wpdb->get_results( $sql, 'ARRAY_A' );
// Set the number of submissions.
$data[ 'subs' ] = $result[ 0 ][ 'total' ];
// Get our fields.
$field_data = Ninja_Forms()->form( intval( $id ) )->get_fields();
$data[ 'fields' ] = array();
// For each field on the form...
foreach ( $field_data as $field ) {
// Add that data to our array.
$data[ 'fields' ][] = $field->get_setting( 'type' );
}
// Get our actions.
$action_data = Ninja_Forms()->form( intval( $id ) )->get_actions();
$data[ 'actions' ] = array();
// For each action on the form...
foreach ( $action_data as $action ) {
// Add that data to our array.
$data[ 'actions' ][] = $action->get_setting( 'type' );
}
// Add this form ID to our option.
$forms_ref[] = $id;
// Update our option.
update_option( 'nf_form_tel_data', implode( ',', $forms_ref ), false );
$this->send( 'form_data', $data );
}
}
/**
* Sends a campaign slug and data to our API endpoint.
......
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