NF_Tracking.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * Tracking functions for reporting plugin usage to the Ninja Forms site for users that have opted in
  4. *
  5. * @package Ninja Forms
  6. * @subpackage Admin
  7. * @copyright Copyright (c) 2016, The WP Ninjas
  8. * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
  9. * @since 2.9.52
  10. */
  11. // Exit if accessed directly
  12. if ( ! defined( 'ABSPATH' ) ) exit;
  13. /**
  14. * Class NF_Tracking
  15. */
  16. final class NF_Tracking
  17. {
  18. const OPT_IN = 1;
  19. const OPT_OUT = 0;
  20. const FLAG = 'ninja_forms_opt_in';
  21. /**
  22. * NF_Tracking constructor.
  23. */
  24. public function __construct()
  25. {
  26. // Temporary: Report previously opted-in users that were not already reported. @todo Remove after a couple of versions.
  27. // add_action( 'admin_init', array( $this, 'report_optin' ) );
  28. add_action( 'wp_ajax_nf_optin', array( $this, 'maybe_opt_in' ) );
  29. add_filter( 'ninja_forms_check_setting_allow_tracking', array( $this, 'check_setting' ) );
  30. add_filter( 'ninja_forms_update_setting_allow_tracking', array( $this, 'update_setting' ) );
  31. }
  32. /**
  33. * Check if an opt in/out action should be performed.
  34. *
  35. * @access public
  36. * @hook admin_init
  37. */
  38. public function maybe_opt_in()
  39. {
  40. if( $this->can_opt_in() ) {
  41. $opt_in_action = htmlspecialchars( $_POST[ self::FLAG ] );
  42. if( self::OPT_IN == $opt_in_action ){
  43. $this->opt_in();
  44. }
  45. if( self::OPT_OUT == $opt_in_action ){
  46. $this->opt_out();
  47. }
  48. }
  49. die( 1 );
  50. }
  51. /**
  52. * Report that a user has opted-in.
  53. *
  54. * @param array $data Dispatch event data.
  55. */
  56. function report_optin($data = array() )
  57. {
  58. // Only send initial opt-in.
  59. if( get_option( 'ninja_forms_optin_reported', 0 ) ) return;
  60. $data = wp_parse_args( $data, array(
  61. 'send_email' => 0, // Do not send email by default.
  62. 'user_email' => ''
  63. ) );
  64. Ninja_Forms()->dispatcher()->send( 'optin', $data );
  65. Ninja_Forms()->dispatcher()->update_environment_vars();
  66. // Debounce opt-in dispatch.
  67. update_option( 'ninja_forms_optin_reported', 1 );
  68. }
  69. /**
  70. * Check if the current user is allowed to opt in on behalf of a site
  71. *
  72. * @return bool
  73. */
  74. private function can_opt_in()
  75. {
  76. return current_user_can( apply_filters( 'ninja_forms_admin_opt_in_capabilities', 'manage_options' ) );
  77. }
  78. /**
  79. * Check if a site is opted in
  80. *
  81. * @access public
  82. * @return bool
  83. */
  84. public function is_opted_in()
  85. {
  86. return (bool) get_option( 'ninja_forms_allow_tracking' );
  87. }
  88. /**
  89. * Opt In a site for tracking
  90. *
  91. * @access private
  92. * @return null
  93. */
  94. public function opt_in()
  95. {
  96. if( $this->is_opted_in() ) return;
  97. /**
  98. * Update our tracking options.
  99. */
  100. update_option( 'ninja_forms_allow_tracking', true );
  101. update_option( 'ninja_forms_do_not_allow_tracking', false );
  102. /**
  103. * Send updated environment variables.
  104. */
  105. Ninja_Forms()->dispatcher()->update_environment_vars();
  106. /**
  107. * Send our optin event
  108. */
  109. if ( isset ( $_REQUEST[ 'send_email' ] ) ) {
  110. $send_email = absint( $_REQUEST[ 'send_email' ] );
  111. $user_email = $_REQUEST[ 'user_email' ];
  112. add_option( 'ninja_forms_optin_email', $user_email, '', 'no' );
  113. } else {
  114. $send_email = 0;
  115. $user_email = '';
  116. }
  117. $this->report_optin( array( 'send_email' => $send_email, 'user_email' => $user_email ) );
  118. }
  119. /**
  120. * Check if a site is opted out
  121. *
  122. * @access public
  123. * @return bool
  124. */
  125. public function is_opted_out()
  126. {
  127. return (bool) get_option( 'ninja_forms_do_not_allow_tracking' );
  128. }
  129. /**
  130. * Opt Out a site from tracking
  131. *
  132. * @access private
  133. * @return null
  134. */
  135. private function opt_out()
  136. {
  137. if( $this->is_opted_out() ) return;
  138. $data = array();
  139. $user_email = get_option( 'ninja_forms_optin_email' );
  140. if ( $user_email ) {
  141. $data[ 'user_email' ] = $user_email;
  142. }
  143. Ninja_Forms()->dispatcher()->send( 'optout', $data );
  144. delete_option( 'ninja_forms_optin_email' );
  145. // Disable tracking.
  146. update_option( 'ninja_forms_allow_tracking', false );
  147. update_option( 'ninja_forms_do_not_allow_tracking', true );
  148. // Clear dispatch debounce flag.
  149. update_option( 'ninja_forms_optin_reported', 0 );
  150. }
  151. public function check_setting( $setting )
  152. {
  153. if( $this->is_opted_in() && ! $this->is_opted_out() ) {
  154. $setting[ 'value' ] = "1";
  155. } else {
  156. $setting[ 'value' ] = "0";
  157. }
  158. return $setting;
  159. }
  160. public function update_setting( $value )
  161. {
  162. if( "1" == $value ){ // Allow Tracking
  163. $this->opt_in();
  164. } else {
  165. $this->opt_out();
  166. }
  167. return $value;
  168. }
  169. } // END CLASS NF_Tracking