WC_Naguro_Ajax

The WooCommerce Naguro Connect WC Naguro Ajax class.

Defined (1)

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

/includes/woocommerce/core/class-wc-naguro-ajax.php  
  1. class WC_Naguro_Ajax { 
  2. public function __construct() { 
  3. // Add the dispatch action for both logged in and not logged in visitors 
  4. add_action( 'wp_ajax_naguro', array( $this, 'dispatch' ) ); 
  5. add_action( 'wp_ajax_nopriv_naguro', array( $this, 'dispatch' ) ); 
  6.  
  7. /** 
  8. * Fires up the correct request handler based on the posted model and method 
  9. */ 
  10. public function dispatch() { 
  11. $model = $_POST['model']; 
  12. $method = $_POST['method']; 
  13.  
  14. try { 
  15. $request = $this->get_request_by_model_method( $model, $method ); 
  16. $request->output(); 
  17. } catch ( Exception $e ) { 
  18. return; 
  19.  
  20. /** 
  21. * @param $model 
  22. * @param $method 
  23. * @return Naguro_Request 
  24. * @throws Exception 
  25. */ 
  26. private function get_request_by_model_method( $model, $method ) { 
  27. $available_model_factories = array( 'session', 'font', 'text', 'order', 'image' ); 
  28.  
  29. if ( ! in_array( $model, $available_model_factories ) ) { 
  30. throw new Exception('Invalid model.'); 
  31.  
  32. $factory = $this->get_factory_by_model( $model ); 
  33.  
  34. try { 
  35. $request = $factory->get_request_by_method( $method ); 
  36. return $request; 
  37. } catch( Exception $e ) { 
  38. throw $e; 
  39.  
  40. /** 
  41. * @param string $model 
  42. * @return Naguro_Request_Factory 
  43. */ 
  44. private function get_factory_by_model( $model ) { 
  45. $factory_class_name = 'Naguro_' . ucwords( $model ) . '_Request_Factory'; 
  46. return new $factory_class_name();