Dispatcher.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * Handles sending information to our api.ninjaforms.com endpoint.
  4. *
  5. * @since 3.2
  6. */
  7. final class NF_Dispatcher
  8. {
  9. private $api_url = 'http://api.ninjaforms.com/';
  10. /**
  11. * Returns bool true if we are opted-in or have a premium add-on.
  12. * If a premium add-on is installed, then users have opted into tracked via our terms and conditions.
  13. * If no premium add-ons are installed, check to see if the user has opted in or out of anonymous usage tracking.
  14. *
  15. * @since version
  16. * @return bool
  17. */
  18. public function should_we_send() {
  19. /**
  20. * TODO:
  21. * Prevent certain URLS or IPs from submitting. i.e. staging, 127.0.0.1, localhost, etc.
  22. */
  23. if ( ! has_filter( 'ninja_forms_settings_licenses_addons' ) && ( ! Ninja_Forms()->tracking->is_opted_in() || Ninja_Forms()->tracking->is_opted_out() ) ) {
  24. return false;
  25. }
  26. return true;
  27. }
  28. /**
  29. * Package up our environment variables and send those to our API endpoint.
  30. *
  31. * @since 3.2
  32. * @return void
  33. */
  34. public function update_environment_vars() {
  35. global $wpdb;
  36. // Plugins
  37. $active_plugins = (array) get_option( 'active_plugins', array() );
  38. //WP_DEBUG
  39. if ( defined('WP_DEBUG') && WP_DEBUG ){
  40. $debug = 1;
  41. } else {
  42. $debug = 0;
  43. }
  44. //WPLANG
  45. if ( defined( 'WPLANG' ) && WPLANG ) {
  46. $lang = WPLANG;
  47. } else {
  48. $lang = 'default';
  49. }
  50. $ip_address = '';
  51. if ( array_key_exists( 'SERVER_ADDR', $_SERVER ) ) {
  52. $ip_address = $_SERVER[ 'SERVER_ADDR' ];
  53. } else if ( array_key_exists( 'LOCAL_ADDR', $_SERVER ) ) {
  54. $ip_address = $_SERVER[ 'LOCAL_ADDR' ];
  55. }
  56. $host_name = gethostbyaddr( $ip_address );
  57. if ( is_multisite() ) {
  58. $multisite_enabled = 1;
  59. } else {
  60. $multisite_enabled = 0;
  61. }
  62. $environment = array(
  63. 'nf_version' => Ninja_Forms::VERSION,
  64. 'wp_version' => get_bloginfo('version'),
  65. 'multisite_enabled' => $multisite_enabled,
  66. 'server_type' => $_SERVER['SERVER_SOFTWARE'],
  67. 'php_version' => phpversion(),
  68. 'mysql_version' => $wpdb->db_version(),
  69. 'wp_memory_limit' => WP_MEMORY_LIMIT,
  70. 'wp_debug_mode' => $debug,
  71. 'wp_lang' => $lang,
  72. 'wp_max_upload_size' => size_format( wp_max_upload_size() ),
  73. 'php_max_post_size' => ini_get( 'post_max_size' ),
  74. 'hostname' => $host_name,
  75. 'smtp' => ini_get('SMTP'),
  76. 'smtp_port' => ini_get('smtp_port'),
  77. 'active_plugins' => $active_plugins,
  78. );
  79. $this->send( 'update_environment_vars', $environment );
  80. }
  81. /**
  82. * Package up our form data and send it to our API endpoint.
  83. *
  84. * @since 3.2
  85. * @return void
  86. */
  87. public function form_data() {
  88. global $wpdb;
  89. // If we have not finished the process...
  90. if ( ! get_option( 'nf_form_tel_sent' ) || 'false' == get_option( 'nf_form_tel_sent' ) ) {
  91. // Get our list of already processed forms (if it exists).
  92. $forms_ref = get_option( 'nf_form_tel_data' );
  93. // Get a list of Forms on this site.
  94. $sql = "SELECT id FROM `" . $wpdb->prefix . "nf3_forms`";
  95. $forms = $wpdb->get_results( $sql, 'ARRAY_A' );
  96. // If our list of processed forms already exists...
  97. if ( ! empty( $forms_ref ) ) {
  98. // Break those into an array.
  99. $forms_ref = explode( ',', $forms_ref );
  100. } // Otherwise...
  101. else {
  102. // Make sure we have an array.
  103. $forms_ref = array();
  104. }
  105. $match_found = false;
  106. // For each form...
  107. foreach ( $forms as $form ) {
  108. // If the current form is not in our list of sent values...
  109. if ( ! in_array( $form[ 'id' ], $forms_ref ) ) {
  110. // Set our target ID.
  111. $id = $form[ 'id' ];
  112. // Record that we found a match.
  113. $match_found = true;
  114. }
  115. }
  116. // If we didn't find a match.
  117. if ( ! $match_found ) {
  118. // Record that we're done.
  119. update_option( 'nf_form_tel_sent', 'true', false );
  120. // Exit.
  121. return false;
  122. }// Otherwise... (We did find a match.)
  123. // Get our form.
  124. $form_data = Ninja_Forms()->form( intval( $id ) )->get();
  125. // Setup our data value.
  126. $data = array();
  127. // Set the form title.
  128. $data[ 'title' ] = $form_data->get_setting( 'title' );
  129. $sql = "SELECT COUNT(meta_id) AS total FROM `" . $wpdb->prefix . "postmeta` WHERE meta_key = '_form_id' AND meta_value = '" . intval( $id ) . "'";
  130. $result = $wpdb->get_results( $sql, 'ARRAY_A' );
  131. // Set the number of submissions.
  132. $data[ 'subs' ] = $result[ 0 ][ 'total' ];
  133. // Get our fields.
  134. $field_data = Ninja_Forms()->form( intval( $id ) )->get_fields();
  135. $data[ 'fields' ] = array();
  136. // For each field on the form...
  137. foreach ( $field_data as $field ) {
  138. // Add that data to our array.
  139. $data[ 'fields' ][] = $field->get_setting( 'type' );
  140. }
  141. // Get our actions.
  142. $action_data = Ninja_Forms()->form( intval( $id ) )->get_actions();
  143. $data[ 'actions' ] = array();
  144. // For each action on the form...
  145. foreach ( $action_data as $action ) {
  146. // Add that data to our array.
  147. $data[ 'actions' ][] = $action->get_setting( 'type' );
  148. }
  149. // Add this form ID to our option.
  150. $forms_ref[] = $id;
  151. // Update our option.
  152. update_option( 'nf_form_tel_data', implode( ',', $forms_ref ), false );
  153. $this->send( 'form_data', $data );
  154. }
  155. }
  156. /**
  157. * Sends a campaign slug and data to our API endpoint.
  158. * Checks to ensure that the user has 1) opted into tracking or 2) they have a premium add-on installed.
  159. *
  160. * @since 3.2
  161. * @param string $slug Campaign slug
  162. * @param array $data Array of data being sent. Should NOT already be a JSON string.
  163. * @return void
  164. */
  165. public function send( $slug, $data = array() ) {
  166. if ( ! $this->should_we_send() ) {
  167. return false;
  168. }
  169. /**
  170. * Gather site data before we send.
  171. *
  172. * We send the following site data with our passed data:
  173. * IP Address
  174. * Email
  175. * Site Url
  176. */
  177. $ip_address = '';
  178. if ( array_key_exists( 'SERVER_ADDR', $_SERVER ) ) {
  179. $ip_address = $_SERVER[ 'SERVER_ADDR' ];
  180. } else if ( array_key_exists( 'LOCAL_ADDR', $_SERVER ) ) {
  181. $ip_address = $_SERVER[ 'LOCAL_ADDR' ];
  182. }
  183. /**
  184. * Email address of the current user.
  185. * (if one was provided)
  186. */
  187. $email = isset( $data[ 'user_email' ] ) ? $data[ 'user_email' ] : '';
  188. $site_data = array(
  189. 'url' => site_url(),
  190. 'ip_address' => $ip_address,
  191. 'email' => $email,
  192. );
  193. /*
  194. * Send our data using wp_remote_post.
  195. */
  196. $response = wp_remote_post(
  197. $this->api_url,
  198. array(
  199. 'body' => array(
  200. 'slug' => $slug,
  201. 'data' => wp_json_encode( $data ),
  202. 'site_data' => wp_json_encode( $site_data ),
  203. ),
  204. )
  205. );
  206. }
  207. }