WC_Settings_Rest_API

WC_Settings_Rest_API.

Defined (1)

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

/includes/admin/settings/class-wc-settings-api.php  
  1. class WC_Settings_Rest_API extends WC_Settings_Page { 
  2.  
  3. /** 
  4. * Constructor. 
  5. */ 
  6. public function __construct() { 
  7. $this->id = 'api'; 
  8. $this->label = __( 'API', 'woocommerce' ); 
  9.  
  10. add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 ); 
  11. add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) ); 
  12. add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) ); 
  13. add_action( 'woocommerce_settings_form_method_tab_' . $this->id, array( $this, 'form_method' ) ); 
  14. add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) ); 
  15.  
  16. $this->notices(); 
  17.  
  18. /** 
  19. * Get sections. 
  20. * @return array 
  21. */ 
  22. public function get_sections() { 
  23. $sections = array( 
  24. '' => __( 'Settings', 'woocommerce' ),  
  25. 'keys' => __( 'Keys/Apps', 'woocommerce' ),  
  26. 'webhooks' => __( 'Webhooks', 'woocommerce' ),  
  27. ); 
  28.  
  29. return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections ); 
  30.  
  31. /** 
  32. * Get settings array. 
  33. * @return array 
  34. */ 
  35. public function get_settings() { 
  36. $settings = apply_filters( 'woocommerce_settings_rest_api', array( 
  37. array( 
  38. 'title' => __( 'General options', 'woocommerce' ),  
  39. 'type' => 'title',  
  40. 'desc' => '',  
  41. 'id' => 'general_options',  
  42. ),  
  43.  
  44. array( 
  45. 'title' => __( 'API', 'woocommerce' ),  
  46. 'desc' => __( 'Enable the REST API', 'woocommerce' ),  
  47. 'id' => 'woocommerce_api_enabled',  
  48. 'type' => 'checkbox',  
  49. 'default' => 'yes',  
  50. ),  
  51.  
  52. array( 
  53. 'type' => 'sectionend',  
  54. 'id' => 'general_options',  
  55. ),  
  56. ) ); 
  57.  
  58. return apply_filters( 'woocommerce_get_settings_' . $this->id, $settings ); 
  59.  
  60. /** 
  61. * Form method. 
  62. * @param string $method 
  63. * @return string 
  64. */ 
  65. public function form_method( $method ) { 
  66. global $current_section; 
  67.  
  68. if ( 'webhooks' == $current_section ) { 
  69. if ( isset( $_GET['edit-webhook'] ) ) { 
  70. $webhook_id = absint( $_GET['edit-webhook'] ); 
  71. $webhook = new WC_Webhook( $webhook_id ); 
  72.  
  73. if ( 'trash' != $webhook->post_data->post_status ) { 
  74. return 'post'; 
  75.  
  76. return 'get'; 
  77.  
  78. if ( 'keys' == $current_section ) { 
  79. if ( isset( $_GET['create-key'] ) || isset( $_GET['edit-key'] ) ) { 
  80. return 'post'; 
  81.  
  82. return 'get'; 
  83.  
  84. return 'post'; 
  85.  
  86. /** 
  87. * Notices. 
  88. */ 
  89. private function notices() { 
  90. if ( isset( $_GET['section'] ) && 'webhooks' == $_GET['section'] ) { 
  91. WC_Admin_Webhooks::notices(); 
  92. if ( isset( $_GET['section'] ) && 'keys' == $_GET['section'] ) { 
  93. WC_Admin_API_Keys::notices(); 
  94.  
  95. /** 
  96. * Output the settings. 
  97. */ 
  98. public function output() { 
  99. global $current_section; 
  100.  
  101. if ( 'webhooks' == $current_section ) { 
  102. WC_Admin_Webhooks::page_output(); 
  103. } elseif ( 'keys' === $current_section ) { 
  104. WC_Admin_API_Keys::page_output(); 
  105. } else { 
  106. $settings = $this->get_settings( $current_section ); 
  107. WC_Admin_Settings::output_fields( $settings ); 
  108.  
  109. /** 
  110. * Save settings. 
  111. */ 
  112. public function save() { 
  113. global $current_section; 
  114.  
  115. if ( apply_filters( 'woocommerce_rest_api_valid_to_save', ! in_array( $current_section, array( 'keys', 'webhooks' ) ) ) ) { 
  116. $settings = $this->get_settings(); 
  117. WC_Admin_Settings::save_fields( $settings );