Naguro_Dashboard_Page

The WooCommerce Naguro Connect Naguro Dashboard Page class.

Defined (1)

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

/includes/wordpress/pages/class-dashboard-page.php  
  1. class Naguro_Dashboard_Page extends Abstract_Naguro_WordPress_Settings_Page { 
  2. private $errors = array(); 
  3.  
  4. public function __construct() { 
  5. $this->maybe_save_settings(); 
  6. $this->output_tabs( 'dashboard' ); 
  7.  
  8. $activation_key = get_option( 'naguro_activation_key', false ); 
  9.  
  10. if ( false === $activation_key ) { 
  11. $this->display_api_settings(); 
  12. } else { 
  13. $this->display_authenticated_message(); 
  14.  
  15. private function display_authenticated_message() { 
  16. echo '<h3>Congratulations, Naguro is ready to use</h3>'; 
  17. echo '<p>It\'s pretty easy to configure Naguro for your products and review ordered items.</p>'; 
  18. echo '<p>If you need any help on how to do this, please visit our <a href="https://www.naguro.com/installation">Installation page</a>.</p>'; 
  19.  
  20. private function maybe_save_settings() { 
  21. if ( isset( $_POST['naguro_trial_email'] ) ) { 
  22. $request = new Naguro_Request_Trial_Key_Request( array( 
  23. 'email' => sanitize_text_field( $_POST['naguro_trial_email'] ),  
  24. 'home_url' => get_home_url(),  
  25. ) ); 
  26.  
  27. $data = $request->do_request(); 
  28. $data = json_decode( $data['body'] ); 
  29. if ( isset( $data->error ) ) { 
  30. $this->add_error_message( $data->error->message ); 
  31. } else { 
  32. $key = $data->key->api_key; 
  33. $this->request_activation_key( $key ); 
  34. } elseif ( isset( $_POST['naguro_api_key'] ) ) { 
  35. $this->request_activation_key( $_POST['naguro_api_key'] ); 
  36.  
  37. private function add_error_message($message) { 
  38. if ( is_object( $message ) ) { 
  39. foreach ( $message as $error ) { 
  40. foreach ( $error as $ding ) { 
  41. $this->errors[] = $ding; 
  42.  
  43. return; 
  44.  
  45. $this->errors[] = $message; 
  46.  
  47. private function display_api_settings() { 
  48. if ( ! empty( $this->errors ) ) { 
  49. echo '<div id="message" class="error">'; 
  50. echo '<p>' . implode( ', ', $this->errors ) . '</p>'; 
  51. echo '</div>'; 
  52.  
  53. echo '<form action="" method="POST">'; 
  54. echo '<h3>Request trial API key</h3>'; 
  55. echo '<p>You can request a 30 day trial key here, so you can try Naguro for free.</p>'; 
  56. echo '<label for="naguro_trial_email">Email address: </label>'; 
  57. echo '<input type="text" id="naguro_trial_email" name="naguro_trial_email">'; 
  58. echo '<input type="submit" class="button" value="Request">'; 
  59. echo '</form>'; 
  60.  
  61. echo '<form action="" method="POST">'; 
  62. echo '<h3>Authentication</h3>'; 
  63. echo '<p>Please enter your Naguro API key in the field below to authenticate with the API and activate your purchased modules.</p>'; 
  64. echo '<label for="naguro_api_key">API key: </label>'; 
  65. echo '<input type="text" value="' . esc_attr( get_option( 'naguro_api_key' ) ) . '" id="naguro_api_key" name="naguro_api_key">'; 
  66. echo '<input type="submit" class="button" value="Save">'; 
  67. echo '</form>'; 
  68.  
  69. private function request_activation_key( $key ) { 
  70. $key = sanitize_text_field( $key ); 
  71.  
  72. $request = new Naguro_Activate_Key_Request( array( 
  73. 'api_key' => $key,  
  74. 'home_url' => get_home_url(),  
  75. ) ); 
  76.  
  77. $data = $request->do_request(); 
  78. $data = json_decode( $data['body'] ); 
  79.  
  80. if ( isset( $data->error ) ) { 
  81. $this->add_error_message( $data->error->message ); 
  82. } else { 
  83. update_option( 'naguro_api_key', $key ); 
  84. $key = $data->activation->activation_key; 
  85. update_option( 'naguro_activation_key', $key );