Caldera_Forms_DB_Track

Track events in Caldera Forms.

Defined (1)

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

/classes/db/track.php  
  1. class Caldera_Forms_DB_Track extends Caldera_Forms_DB_Base { 
  2.  
  3. /** 
  4. * Primary fields 
  5. * @since 1.3.5 
  6. * @var array 
  7. */ 
  8. protected $primary_fields = array( 
  9. 'form_id' => array( 
  10. '%s',  
  11. 'strip_tags' 
  12. ),  
  13. 'process_id' => array( 
  14. '%s',  
  15. 'strip_tags' 
  16.  
  17. ); 
  18.  
  19. /** 
  20. * Meta fields 
  21. * @since 1.3.5 
  22. * @var array 
  23. */ 
  24. protected $meta_fields = array( 
  25. 'event_id' => array( 
  26. '%d',  
  27. 'absint',  
  28. ),  
  29. 'meta_key' => array( 
  30. '%s',  
  31. 'strip_tags',  
  32. ),  
  33. 'meta_value' => array( 
  34. '%s',  
  35. 'strip_tags',  
  36. ),  
  37. ); 
  38.  
  39. /** 
  40. * Meta keys 
  41. * @since 1.3.5 
  42. * @var array 
  43. */ 
  44. protected $meta_keys = array( 
  45. 'event' => array( 
  46. '%s',  
  47. 'strip_tags',  
  48. ),  
  49. 'time' => array( 
  50. '%s',  
  51. 'strip_tags',  
  52. ),  
  53. 'recipients_set' => array( 
  54. '%s',  
  55. 'strip_tags',  
  56. ),  
  57. 'referrer' => array( 
  58. '%s',  
  59. 'esc_url_raw' 
  60. ); 
  61.  
  62. /** 
  63. * Name of primary index 
  64. * @since 1.3.5 
  65. * @var string 
  66. */ 
  67. protected $index = 'event_id'; 
  68.  
  69. /** 
  70. * Name of table 
  71. * @since 1.3.5 
  72. * @var string 
  73. */ 
  74. protected $table_name = 'cf_tracking'; 
  75.  
  76. /** 
  77. * Class instance 
  78. * @since 1.3.5 
  79. * @var Caldera_Forms_DB_Track 
  80. */ 
  81. private static $instance; 
  82.  
  83. /** 
  84. * Setup the actions to track upon 
  85. * @since 1.3.5 
  86. */ 
  87. protected function __construct() { 
  88. add_action( 'caldera_forms_submit_start', array( $this, 'submit_start' ), 50, 2 ); 
  89. add_action( 'caldera_forms_submit_complete', array( $this, 'submit_complete' ), 50, 3 ); 
  90. add_action( 'caldera_forms_submit_complete', array( $this, 'email_tracking' ), 51, 3 ); 
  91. add_action( 'caldera_forms_mailer_complete', array( $this, 'email_sent'), 50, 3 ); 
  92. add_action( 'caldera_forms_mailer_failed', array( $this, 'email_fail' ), 50, 4 ); 
  93. add_action( 'caldera_forms_mailer_invalid', array( $this, 'email_invalid' ), 50 ); 
  94.  
  95.  
  96. /** 
  97. * Get class instance 
  98. * @since 1.3.5 
  99. * @return \Caldera_Forms_DB_Track 
  100. */ 
  101. public static function get_instance() { 
  102. if( null == self::$instance ) { 
  103. self::$instance = new self(); 
  104.  
  105. return self::$instance; 
  106.  
  107.  
  108. /** 
  109. * Track that a form started submitting 
  110. * @since 1.3.5 
  111. * @uses "caldera_forms_submit_start" 
  112. * @param array $form Form config 
  113. * @param string $process_id Form process ID 
  114. */ 
  115. public function submit_start( $form, $process_id ) { 
  116. if( isset( $form[ 'ID' ] ) ) { 
  117. $recorded = $this->create( array( 
  118. 'event' => 'submit_start',  
  119. 'form_id' => $form[ 'ID' ],  
  120. 'process_id' => $process_id,  
  121. 'time' => current_time( 'mysql' ) 
  122. )); 
  123.  
  124.  
  125. /** 
  126. * Track that a form completed submitting 
  127. * @since 1.3.5 
  128. * @uses "caldera_forms_submit_complete" 
  129. * @param array $form Form config 
  130. * @param array $referrer URL parts for submissions URL 
  131. * @param string $process_id Form process ID 
  132. */ 
  133. public function submit_complete( $form, $referrer, $process_id ) { 
  134. if( isset( $form[ 'ID' ] ) ) { 
  135. $recorded = $this->create( array( 
  136. 'event' => 'submit_complete',  
  137. 'form_id' => $form[ 'ID' ],  
  138. 'process_id' => $process_id,  
  139. 'time' => current_time( 'mysql' ),  
  140. 'referrer' => cf_http_build_url( '', $referrer ) 
  141. )); 
  142.  
  143.  
  144. /** 
  145. * Mark that an email <em>should</em> be sent 
  146. * @since 1.3.5 
  147. * @uses "caldera_forms_submit_complete" 
  148. * @param array $form Form config 
  149. * @param string $referrer URL referring 
  150. * @param string $process_id Form process ID 
  151. */ 
  152. public function email_tracking( $form, $referrer, $process_id ) { 
  153. if( isset( $form[ 'ID' ], $form[ 'mailer' ] ) ) { 
  154. if( is_array( $form[ 'mailer' ] ) && isset( $form[ 'mailer' ][ 'on_insert' ] ) && 1 == $form[ 'mailer' ][ 'on_insert' ] ) { 
  155. $recorded = $this->create( array( 
  156. 'event' => 'email_should_send',  
  157. 'form_id' => $form[ 'ID' ],  
  158. 'process_id' => $process_id,  
  159. 'time' => current_time( 'mysql' ),  
  160. )); 
  161.  
  162.  
  163.  
  164.  
  165. /** 
  166. * Track a successful email 
  167. * @uses "caldera_forms_mailer_complete" 
  168. * @since 1.3.5 
  169. * @param array $mail Mailer data 
  170. * @param array $data Submission data 
  171. * @param array $form Form config 
  172. */ 
  173. public function email_sent( $mail, $data, $form ) { 
  174. global $process_id; 
  175. if( isset( $form[ 'ID' ] ) ) { 
  176. $recorded = $this->create( array( 
  177. 'event' => 'email_sent',  
  178. 'form_id' => $form[ 'ID' ],  
  179. 'process_id' => $process_id,  
  180. 'time' => current_time( 'mysql' ),  
  181. 'recipients_set' => self::recipients_set( $mail ) 
  182. )); 
  183.  
  184.  
  185.  
  186.  
  187. /** 
  188. * Track a failed email 
  189. * @uses "caldera_forms_mailer_failed" 
  190. * @since 1.3.5 
  191. * @param array $mail Mailer data 
  192. * @param array $data Submission data 
  193. * @param array $form Form config 
  194. * @param string $method Send method 
  195. */ 
  196. public function email_fail( $mail, $data, $form, $method ) { 
  197. global $process_id; 
  198. if( isset( $form[ 'ID' ] ) ) { 
  199. $this->create( array( 
  200. 'event' => 'email_failed',  
  201. 'form_id' => $form[ 'ID' ],  
  202. 'process_id' => $process_id,  
  203. 'time' => current_time( 'mysql' ),  
  204. 'method' => strip_tags( $method ),  
  205. 'recipients_set' => self::recipients_set( $mail ) 
  206. )); 
  207.  
  208.  
  209.  
  210. /** 
  211. * Track invalid mailer settings events 
  212. * @since 1.4.0 
  213. * @param array $form Form config 
  214. */ 
  215. public function email_invalid( $form) { 
  216. global $process_id; 
  217. if( isset( $form[ 'ID' ] ) ) { 
  218. $this->create( array( 
  219. 'event' => 'email_failed',  
  220. 'form_id' => $form[ 'ID' ],  
  221. 'process_id' => $process_id,  
  222. 'time' => current_time( 'mysql' ),  
  223. )); 
  224.  
  225.  
  226. protected function recipients_set( $mail ) { 
  227. if( ! is_array( $mail ) || ! isset( $mail[ 'recipients' ] ) || empty( $mail[ 'recipients' ] ) ) { 
  228. return false; 
  229.  
  230. return true; 
  231.  
  232. /** 
  233. * Get tracking data by event name 
  234. * @since 1.4.5 
  235. * @param string $event Event name 
  236. * @param bool $return_forms Optional. If true, the default, form IDs are returned. If false, event meta data is returned. 
  237. * @return array|null 
  238. */ 
  239. public function by_event( $event, $return_forms = true ) { 
  240. $metas = $this->query_meta( 'event', $event ); 
  241. if( ! empty( $metas ) ) { 
  242.  
  243. if( $return_forms ) { 
  244. $event_ids = array_unique( wp_list_pluck( $metas, 'event_id' ) ); 
  245. $forms = $this->form_ids_for_events( $event_ids ); 
  246. if( ! empty( $forms ) ) { 
  247. return wp_list_pluck( $forms, 'form_id' ); 
  248.  
  249. }else{ 
  250. return $metas; 
  251.  
  252.  
  253.  
  254. return array(); 
  255.  
  256.  
  257. /** 
  258. * Get form IDs or an array of event IDs 
  259. * @since 1.4.5 
  260. * @param array $event_ids Event IDs to find form IDs for 
  261. * @return array|null 
  262. */ 
  263. protected function form_ids_for_events( $event_ids ) { 
  264. global $wpdb; 
  265. $table = $this->get_table_name( false ); 
  266. $sql = $wpdb->prepare( "SELECT `form_id` FROM $table WHERE `ID` IN( '%s' )", implode( ', ', $event_ids ) ); 
  267. return $wpdb->get_results( $sql, ARRAY_A ); 
  268.  
  269.  
  270.