Commit 71302762 authored by Kevin Stover's avatar Kevin Stover Committed by GitHub

Send an confirmation email to users who have opted into Ninja Forms tracking (#2172)

* Adding initial tracking code.

* move tracking into main plugin class.

* Final pass at opt-in admin email.
parent 556cd646
......@@ -106,10 +106,10 @@ final class NF_Tracking
/**
* Check if a site is opted in
*
* @access private
* @access public
* @return bool
*/
private function is_opted_in()
public function is_opted_in()
{
return (bool) get_option( 'ninja_forms_allow_tracking', $this->is_freemius_opted_in() );
}
......@@ -150,10 +150,10 @@ final class NF_Tracking
/**
* Check if a site is opted out
*
* @access private
* @access public
* @return bool
*/
private function is_opted_out()
public function is_opted_out()
{
return (bool) get_option( 'ninja_forms_do_not_allow_tracking', $this->is_freemius_opted_out() );
}
......@@ -192,5 +192,3 @@ final class NF_Tracking
}
} // END CLASS NF_Tracking
new NF_Tracking();
\ No newline at end of file
......@@ -134,6 +134,11 @@ if( get_option( 'ninja_forms_load_deprecated', FALSE ) && ! ( isset( $_POST[ 'nf
*/
protected $session = '';
/**
* @var NF_Tracking
*/
public $tracking;
/**
* Plugin Settings
*
......@@ -281,6 +286,11 @@ if( get_option( 'ninja_forms_load_deprecated', FALSE ) && ! ( isset( $_POST[ 'nf
self::$instance->widgets[] = new NF_Widget();
/*
* Opt-In Tracking
*/
self::$instance->tracking = new NF_Tracking();
/*
* Activation Hook
* TODO: Move to a permanent home.
......@@ -692,3 +702,63 @@ if( get_option( 'ninja_forms_load_deprecated', FALSE ) && ! ( isset( $_POST[ 'nf
}
}
}
// Scheduled Action Hook
function nf_optin_send_admin_email( ) {
/*
* If we aren't opted in, or we've specifically opted out, then return false.
*/
if ( ! Ninja_Forms()->tracking->is_opted_in() || Ninja_Forms()->tracking->is_opted_out() ) {
return false;
}
/*
* If we haven't already submitted our email to api.ninjaforms.com, submit it and set an option saying we have.
*/
if ( get_option ( 'ninja_forms_optin_admin_email', false ) ) {
return false;
}
/*
* Ping api.ninjaforms.com
*/
$admin_email = get_option('admin_email');
$url = home_url();
$response = wp_remote_post(
'http://api.ninjaforms.com',
array(
'body' => array( 'admin_email' => $admin_email, 'url' => $url ),
)
);
if( is_array($response) ) {
$header = $response['headers']; // array of http header lines
$body = $response['body']; // use the content
}
update_option( 'ninja_forms_optin_admin_email', true );
}
add_action( 'nf_optin_cron', 'nf_optin_send_admin_email' );
// Custom Cron Recurrences
function nf_custom_cron_job_recurrence( $schedules ) {
$schedules['nf-monthly'] = array(
'display' => __( 'Once per month', 'textdomain' ),
'interval' => 2678400,
);
return $schedules;
}
add_filter( 'cron_schedules', 'nf_custom_cron_job_recurrence' );
// Schedule Cron Job Event
function nf_optin_send_admin_email_cron_job() {
wp_clear_scheduled_hook( 'nf_optin_cron' );
if ( ! wp_next_scheduled( 'nf_optin_cron' ) ) {
nf_optin_send_admin_email();
wp_schedule_event( current_time( 'timestamp' ), 'nf-monthly', 'nf_optin_cron' );
}
}
add_action( 'wp', 'nf_optin_send_admin_email_cron_job' );
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