Controller.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. abstract class NF_Abstracts_Controller
  3. {
  4. /**
  5. * Data (Misc.) passed back to the client in the Response.
  6. *
  7. * @var array
  8. */
  9. protected $_data = array();
  10. /**
  11. * Errors passed back to the client in the Response.
  12. *
  13. * @var array
  14. */
  15. protected $_errors = array();
  16. /**
  17. * Debug Messages passed back to the client in the Response.
  18. *
  19. * @var array
  20. */
  21. protected $_debug = array();
  22. /*
  23. * PUBLIC METHODS
  24. */
  25. /**
  26. * NF_Abstracts_Controller constructor.
  27. */
  28. public function __construct()
  29. {
  30. //This section intentionally left blank.
  31. }
  32. /*
  33. * PROTECTED METHODS
  34. */
  35. /**
  36. * Respond
  37. *
  38. * A wrapper for the WordPress AJAX response pattern.
  39. */
  40. protected function _respond( $data = array() )
  41. {
  42. if( empty( $data ) ){
  43. $data = $this->_data;
  44. }
  45. if( isset( $this->_data['debug'] ) ) {
  46. $this->_debug = array_merge( $this->_debug, $this->_data[ 'debug' ] );
  47. }
  48. if( isset( $this->_data['errors'] ) && $this->_data[ 'errors' ] ) {
  49. $this->_errors = array_merge( $this->_errors, $this->_data[ 'errors' ] );
  50. }
  51. // allow for accessing and acting on $data before responding
  52. do_action( 'ninja_forms_before_response', $data );
  53. $response = array( 'data' => $data, 'errors' => $this->_errors, 'debug' => $this->_debug );
  54. echo wp_json_encode( $response );
  55. wp_die(); // this is required to terminate immediately and return a proper response
  56. }
  57. }