MC4WP_Form_Listener

Class MC4WP_Form_Listener.

Defined (1)

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

/includes/class-form-listener.php  
  1. class MC4WP_Form_Listener { 
  2.  
  3. /** 
  4. * @param $data 
  5. * @return bool 
  6. */ 
  7. public function listen( array $data ) { 
  8.  
  9. // only act on POST requests 
  10. if( $_SERVER['REQUEST_METHOD'] !== 'POST' ) { 
  11. return false; 
  12.  
  13. // is form submitted? 
  14. if( ! isset( $data['_mc4wp_form_submit'] ) ) { 
  15. return false; 
  16.  
  17. // determine action 
  18. if ( ! isset( $data['_mc4wp_action'] ) 
  19. || $data['_mc4wp_action'] === 'subscribe' ) { 
  20. $request = new MC4WP_Subscribe_Request( $data ); 
  21. $this->process( $request ); 
  22. } elseif ( $data['_mc4wp_action'] === 'unsubscribe' ) { 
  23. $request = new MC4WP_Unsubscribe_Request( $data ); 
  24. $this->process( $request ); 
  25.  
  26. return true; 
  27.  
  28. /** 
  29. * @param iMC4WP_Request $request 
  30. * @return bool 
  31. */ 
  32. public function process( iMC4WP_Request $request ) { 
  33.  
  34. $valid = $request->validate(); 
  35. $success = false; 
  36.  
  37. if( $valid ) { 
  38.  
  39. // prepare request data 
  40. $ready = $request->prepare(); 
  41.  
  42. // if request is ready, send an API call to MailChimp 
  43. if( $ready ) { 
  44. $success = $request->process(); 
  45.  
  46. $request->respond(); 
  47.  
  48. return $success; 
  49.