Няма описание

class-wc-api.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * WC-API endpoint handler.
  4. *
  5. * This handles API related functionality in WooCommerce.
  6. * - wc-api endpoint - Commonly used by Payment gateways for callbacks.
  7. * - Legacy REST API - Deprecated in 2.6.0. @see class-wc-legacy-api.php
  8. * - WP REST API - The main REST API in WooCommerce which is built on top of the WP REST API.
  9. *
  10. * @package WooCommerce\RestApi
  11. * @since 2.0.0
  12. */
  13. defined( 'ABSPATH' ) || exit;
  14. /**
  15. * WC_API class.
  16. */
  17. class WC_API extends WC_Legacy_API {
  18. /**
  19. * Init the API by setting up action and filter hooks.
  20. */
  21. public function init() {
  22. parent::init();
  23. add_action( 'init', array( $this, 'add_endpoint' ), 0 );
  24. add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
  25. add_action( 'parse_request', array( $this, 'handle_api_requests' ), 0 );
  26. add_action( 'rest_api_init', array( $this, 'register_wp_admin_settings' ) );
  27. }
  28. /**
  29. * Get the version of the REST API package being ran. Since API package was merged into core, this now follows WC version.
  30. *
  31. * @since 3.7.0
  32. * @return string|null
  33. */
  34. public function get_rest_api_package_version() {
  35. if ( ! $this->is_rest_api_loaded() ) {
  36. return null;
  37. }
  38. if ( method_exists( \Automattic\WooCommerce\RestApi\Server::class, 'get_path' ) ) {
  39. $path = \Automattic\WooCommerce\RestApi\Server::get_path();
  40. if ( 0 === strpos( $path, __DIR__ ) ) {
  41. // We are loading API from included version.
  42. return WC()->version;
  43. }
  44. }
  45. // We are loading API from external plugin.
  46. return \Automattic\WooCommerce\RestApi\Package::get_version();
  47. }
  48. /**
  49. * Get the version of the REST API package being ran.
  50. *
  51. * @since 3.7.0
  52. * @return string
  53. */
  54. public function get_rest_api_package_path() {
  55. if ( ! $this->is_rest_api_loaded() ) {
  56. return null;
  57. }
  58. if ( method_exists( \Automattic\WooCommerce\RestApi\Server::class, 'get_path' ) ) {
  59. // We are loading API from included version.
  60. return \Automattic\WooCommerce\RestApi\Server::get_path();
  61. }
  62. // We are loading API from external plugin.
  63. return \Automattic\WooCommerce\RestApi\Package::get_path();
  64. }
  65. /**
  66. * Return if the rest API classes were already loaded.
  67. *
  68. * @since 3.7.0
  69. * @return boolean
  70. */
  71. protected function is_rest_api_loaded() {
  72. return class_exists( '\Automattic\WooCommerce\RestApi\Server', false );
  73. }
  74. /**
  75. * Get data from a WooCommerce API endpoint.
  76. *
  77. * @since 3.7.0
  78. * @param string $endpoint Endpoint.
  79. * @param array $params Params to passwith request.
  80. * @return array|\WP_Error
  81. */
  82. public function get_endpoint_data( $endpoint, $params = array() ) {
  83. if ( ! $this->is_rest_api_loaded() ) {
  84. return new WP_Error( 'rest_api_unavailable', __( 'The Rest API is unavailable.', 'woocommerce' ) );
  85. }
  86. $request = new \WP_REST_Request( 'GET', $endpoint );
  87. if ( $params ) {
  88. $request->set_query_params( $params );
  89. }
  90. $response = rest_do_request( $request );
  91. $server = rest_get_server();
  92. $json = wp_json_encode( $server->response_to_data( $response, false ) );
  93. return json_decode( $json, true );
  94. }
  95. /**
  96. * Add new query vars.
  97. *
  98. * @since 2.0
  99. * @param array $vars Query vars.
  100. * @return string[]
  101. */
  102. public function add_query_vars( $vars ) {
  103. $vars = parent::add_query_vars( $vars );
  104. $vars[] = 'wc-api';
  105. return $vars;
  106. }
  107. /**
  108. * WC API for payment gateway IPNs, etc.
  109. *
  110. * @since 2.0
  111. */
  112. public static function add_endpoint() {
  113. parent::add_endpoint();
  114. add_rewrite_endpoint( 'wc-api', EP_ALL );
  115. }
  116. /**
  117. * API request - Trigger any API requests.
  118. *
  119. * @since 2.0
  120. * @version 2.4
  121. */
  122. public function handle_api_requests() {
  123. global $wp;
  124. if ( ! empty( $_GET['wc-api'] ) ) { // WPCS: input var okay, CSRF ok.
  125. $wp->query_vars['wc-api'] = sanitize_key( wp_unslash( $_GET['wc-api'] ) ); // WPCS: input var okay, CSRF ok.
  126. }
  127. // wc-api endpoint requests.
  128. if ( ! empty( $wp->query_vars['wc-api'] ) ) {
  129. // Buffer, we won't want any output here.
  130. ob_start();
  131. // No cache headers.
  132. wc_nocache_headers();
  133. // Clean the API request.
  134. $api_request = strtolower( wc_clean( $wp->query_vars['wc-api'] ) );
  135. // Make sure gateways are available for request.
  136. WC()->payment_gateways();
  137. // Trigger generic action before request hook.
  138. do_action( 'woocommerce_api_request', $api_request );
  139. // Is there actually something hooked into this API request? If not trigger 400 - Bad request.
  140. status_header( has_action( 'woocommerce_api_' . $api_request ) ? 200 : 400 );
  141. // Trigger an action which plugins can hook into to fulfill the request.
  142. do_action( 'woocommerce_api_' . $api_request );
  143. // Done, clear buffer and exit.
  144. ob_end_clean();
  145. die( '-1' );
  146. }
  147. }
  148. /**
  149. * Register WC settings from WP-API to the REST API.
  150. *
  151. * @since 3.0.0
  152. */
  153. public function register_wp_admin_settings() {
  154. $pages = WC_Admin_Settings::get_settings_pages();
  155. foreach ( $pages as $page ) {
  156. new WC_Register_WP_Admin_Settings( $page, 'page' );
  157. }
  158. $emails = WC_Emails::instance();
  159. foreach ( $emails->get_emails() as $email ) {
  160. new WC_Register_WP_Admin_Settings( $email, 'email' );
  161. }
  162. }
  163. }