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

class-wc-rest-wccom-site-installer-controller.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * WCCOM Site Installer REST API Controller
  4. *
  5. * Handles requests to /installer.
  6. *
  7. * @package WooCommerce\WCCom\API
  8. * @since 3.7.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * REST API WCCOM Site Installer Controller Class.
  13. *
  14. * @extends WC_REST_Controller
  15. */
  16. class WC_REST_WCCOM_Site_Installer_Controller extends WC_REST_Controller {
  17. /**
  18. * Endpoint namespace.
  19. *
  20. * @var string
  21. */
  22. protected $namespace = 'wccom-site/v1';
  23. /**
  24. * Route base.
  25. *
  26. * @var string
  27. */
  28. protected $rest_base = 'installer';
  29. /**
  30. * Register the routes for product reviews.
  31. *
  32. * @since 3.7.0
  33. */
  34. public function register_routes() {
  35. register_rest_route(
  36. $this->namespace,
  37. '/' . $this->rest_base,
  38. array(
  39. array(
  40. 'methods' => WP_REST_Server::READABLE,
  41. 'callback' => array( $this, 'get_install_state' ),
  42. 'permission_callback' => array( $this, 'check_permission' ),
  43. ),
  44. array(
  45. 'methods' => WP_REST_Server::CREATABLE,
  46. 'callback' => array( $this, 'install' ),
  47. 'permission_callback' => array( $this, 'check_permission' ),
  48. 'args' => array(
  49. 'products' => array(
  50. 'required' => true,
  51. 'type' => 'object',
  52. ),
  53. ),
  54. ),
  55. array(
  56. 'methods' => WP_REST_Server::DELETABLE,
  57. 'callback' => array( $this, 'reset_install' ),
  58. 'permission_callback' => array( $this, 'check_permission' ),
  59. ),
  60. )
  61. );
  62. }
  63. /**
  64. * Check permissions.
  65. *
  66. * @since 3.7.0
  67. * @param WP_REST_Request $request Full details about the request.
  68. * @return bool|WP_Error
  69. */
  70. public function check_permission( $request ) {
  71. $current_user = wp_get_current_user();
  72. if ( empty( $current_user ) || ( $current_user instanceof WP_User && ! $current_user->exists() ) ) {
  73. return apply_filters(
  74. WC_WCCOM_Site::AUTH_ERROR_FILTER_NAME,
  75. new WP_Error(
  76. WC_REST_WCCOM_Site_Installer_Errors::NOT_AUTHENTICATED_CODE,
  77. WC_REST_WCCOM_Site_Installer_Errors::NOT_AUTHENTICATED_MESSAGE,
  78. array( 'status' => WC_REST_WCCOM_Site_Installer_Errors::NOT_AUTHENTICATED_HTTP_CODE )
  79. )
  80. );
  81. }
  82. if ( ! user_can( $current_user, 'install_plugins' ) || ! user_can( $current_user, 'install_themes' ) ) {
  83. return new WP_Error(
  84. WC_REST_WCCOM_Site_Installer_Errors::NO_PERMISSION_CODE,
  85. WC_REST_WCCOM_Site_Installer_Errors::NO_PERMISSION_MESSAGE,
  86. array( 'status' => WC_REST_WCCOM_Site_Installer_Errors::NO_PERMISSION_HTTP_CODE )
  87. );
  88. }
  89. return true;
  90. }
  91. /**
  92. * Get installation state.
  93. *
  94. * @since 3.7.0
  95. * @param WP_REST_Request $request Full details about the request.
  96. * @return bool|WP_Error
  97. */
  98. public function get_install_state( $request ) {
  99. $requirements_met = WC_WCCOM_Site_Installer_Requirements_Check::met_requirements();
  100. if ( is_wp_error( $requirements_met ) ) {
  101. return $requirements_met;
  102. }
  103. return rest_ensure_response( WC_WCCOM_Site_Installer::get_state() );
  104. }
  105. /**
  106. * Install WooCommerce.com products.
  107. *
  108. * @since 3.7.0
  109. * @param WP_REST_Request $request Full details about the request.
  110. * @return bool|WP_Error
  111. */
  112. public function install( $request ) {
  113. $requirements_met = WC_WCCOM_Site_Installer_Requirements_Check::met_requirements();
  114. if ( is_wp_error( $requirements_met ) ) {
  115. return $requirements_met;
  116. }
  117. if ( empty( $request['products'] ) ) {
  118. return new WP_Error( 'missing_products', __( 'Missing products in request body.', 'woocommerce' ), array( 'status' => 400 ) );
  119. }
  120. $validation_result = $this->validate_products( $request['products'] );
  121. if ( is_wp_error( $validation_result ) ) {
  122. return $validation_result;
  123. }
  124. return rest_ensure_response( WC_WCCOM_Site_Installer::schedule_install( $request['products'] ) );
  125. }
  126. /**
  127. * Reset installation state.
  128. *
  129. * @since 3.7.0
  130. * @param WP_REST_Request $request Full details about the request.
  131. * @return bool|WP_Error
  132. */
  133. public function reset_install( $request ) {
  134. $resp = rest_ensure_response( WC_WCCOM_Site_Installer::reset_state() );
  135. $resp->set_status( 204 );
  136. return $resp;
  137. }
  138. /**
  139. * Validate products from request body.
  140. *
  141. * @since 3.7.0
  142. * @param array $products Array of products where key is product ID and
  143. * element is install args.
  144. * @return bool|WP_Error
  145. */
  146. protected function validate_products( $products ) {
  147. $err = new WP_Error( 'invalid_products', __( 'Invalid products in request body.', 'woocommerce' ), array( 'status' => 400 ) );
  148. if ( ! is_array( $products ) ) {
  149. return $err;
  150. }
  151. foreach ( $products as $product_id => $install_args ) {
  152. if ( ! absint( $product_id ) ) {
  153. return $err;
  154. }
  155. if ( empty( $install_args ) || ! is_array( $install_args ) ) {
  156. return $err;
  157. }
  158. }
  159. return true;
  160. }
  161. }