NF_Tracking

Class NF_Tracking.

Defined (1)

The class is defined in the following location(s).

/lib/NF_Tracking.php  
  1. final class NF_Tracking 
  2. const OPT_IN = 1; 
  3. const OPT_OUT = 0; 
  4. const FLAG = 'ninja_forms_opt_in'; 
  5.  
  6. /** 
  7. * NF_Tracking constructor. 
  8. */ 
  9. public function __construct() 
  10. if( isset( $_GET[ self::FLAG ] ) ) { 
  11. add_action( 'admin_init', array( $this, 'maybe_opt_in' ) ); 
  12.  
  13. add_filter( 'nf_admin_notices', array( $this, 'admin_notice' ) ); 
  14.  
  15. add_action( 'ninja_forms_upgrade', array( $this, 'opt_in' ) ); 
  16.  
  17. /** 
  18. * Check if an opt in/out action should be performed. 
  19. * @access public 
  20. * @hook admin_init 
  21. */ 
  22. public function maybe_opt_in() 
  23. if( $this->can_opt_in() ) { 
  24.  
  25. $opt_in_action = htmlspecialchars( $_GET[ self::FLAG ] ); 
  26.  
  27. if( self::OPT_IN == $opt_in_action ) { 
  28. $this->opt_in(); 
  29.  
  30. if( self::OPT_OUT == $opt_in_action ) { 
  31. $this->opt_out(); 
  32. header( 'Location: ' . admin_url( 'admin.php?page=ninja-forms' ) ); 
  33.  
  34. /** 
  35. * Register the Admin Notice for asking users to opt in to tracking 
  36. * @access public 
  37. * @hook nf_admin_notices 
  38. * @param array $notices 
  39. * @return array $notices 
  40. */ 
  41. public function admin_notice( $notices ) 
  42. // Check if the user is allowed to opt in. 
  43. if( ! $this->can_opt_in() ) return $notices; 
  44.  
  45. // Check if the user is already opted in/out. 
  46. if( $this->is_opted_in() || $this->is_opted_out() ) return $notices; 
  47.  
  48. $notices[ 'allow_tracking' ] = array( 
  49. 'title' => __( 'Please help us improve Ninja Forms!', 'ninja-forms' ),  
  50. 'msg' => implode( '<br />', array( 
  51. __( 'If you opt-in, some data about your installation of Ninja Forms will be sent to NinjaForms.com (this does NOT include your submissions).', 'ninja-forms' ),  
  52. __( 'If you skip this, that\'s okay! Ninja Forms will still work just fine.', 'ninja-forms' ),  
  53. )),  
  54. 'link' => implode( ' ', array( 
  55. sprintf( __( '%sAllow%s', 'ninja-forms' ), '<a href="' . $this->get_opt_in_url( admin_url( 'admin.php?page=ninja-forms' ) ) . '" class="button-primary" id="ninja-forms-allow-tracking">', '</a>' ),  
  56. sprintf( __( '%sDo not allow%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-allow-tracking">', '</a>' ),  
  57. )),  
  58. 'int' => 0, // No delay 
  59. 'blacklist' => array( 
  60. 'ninja-forms-three' 
  61. ); 
  62.  
  63. return $notices; 
  64.  
  65. /** 
  66. * Check if the current user is allowed to opt in on behalf of a site 
  67. * @return bool 
  68. */ 
  69. private function can_opt_in() 
  70. return current_user_can( apply_filters( 'ninja_forms_admin_opt_in_capabilities', 'manage_options' ) ); 
  71.  
  72. /** 
  73. * Check if a site is opted in 
  74. * @access public 
  75. * @return bool 
  76. */ 
  77. public function is_opted_in() 
  78. return (bool) get_option( 'ninja_forms_allow_tracking', $this->is_freemius_opted_in() ); 
  79.  
  80. private function is_freemius_opted_in() 
  81. $freemius = get_option( 'fs_accounts' ); 
  82. if( ! $freemius ) return false; 
  83. if( ! isset( $freemius[ 'plugin_data' ] ) ) return false; 
  84. if( ! isset( $freemius[ 'plugin_data' ][ 'ninja-forms' ] ) ) return false; 
  85. if( ! isset( $freemius[ 'plugin_data' ][ 'ninja-forms' ][ 'activation_timestamp' ] ) ) return false; 
  86. return true; 
  87.  
  88. /** 
  89. * Opt In a site for tracking 
  90. * @access private 
  91. * @return null 
  92. */ 
  93. public function opt_in() 
  94. update_option( 'ninja_forms_allow_tracking', true ); 
  95.  
  96. /** 
  97. * Get the Opt In URL 
  98. * @access private 
  99. * @param string $url 
  100. * @return string $url 
  101. */ 
  102. private function get_opt_in_url( $url ) 
  103. return add_query_arg( 'ninja_forms_opt_in', self::OPT_IN, $url ); 
  104.  
  105. /** 
  106. * Check if a site is opted out 
  107. * @access public 
  108. * @return bool 
  109. */ 
  110. public function is_opted_out() 
  111. return (bool) get_option( 'ninja_forms_do_not_allow_tracking', $this->is_freemius_opted_out() ); 
  112.  
  113. private function is_freemius_opted_out() 
  114. $freemius = get_option( 'fs_accounts' ); 
  115. if( ! $freemius ) return false; 
  116. if( ! isset( $freemius[ 'plugin_data' ] ) ) return false; 
  117. if( ! isset( $freemius[ 'plugin_data' ][ 'ninja-forms' ] ) ) return false; 
  118. if( ! isset( $freemius[ 'plugin_data' ][ 'ninja-forms' ][ 'is_anonymous' ] ) ) return false; 
  119. return true; 
  120.  
  121. /** 
  122. * Opt Out a site from tracking 
  123. * @access private 
  124. * @return null 
  125. */ 
  126. private function opt_out() 
  127. update_option( 'ninja_forms_do_not_allow_tracking', true ); 
  128.  
  129. /** 
  130. * Get the Opt Out URL 
  131. * @access private 
  132. * @param string $url 
  133. * @return string $url 
  134. */ 
  135. private function get_opt_out_url( $url ) 
  136. return add_query_arg( 'ninja_forms_opt_in', self::OPT_OUT, $url ); 
  137.  
  138. } // END CLASS NF_Tracking