PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce-services/classes/class-wc-connect-service-schemas-store.php

https://gitlab.com/remyvianne/krowkaramel
PHP | 266 lines | 161 code | 49 blank | 56 comment | 28 complexity | 9391353097845e03b1e145aa5139564f MD5 | raw file
  1. <?php
  2. if ( ! class_exists( 'WC_Connect_Service_Schemas_Store' ) ) {
  3. class WC_Connect_Service_Schemas_Store {
  4. /**
  5. * @var WC_Connect_API_Client
  6. */
  7. protected $api_client;
  8. /**
  9. * @var WC_Connect_Logger
  10. */
  11. protected $logger;
  12. public function __construct( WC_Connect_API_Client $api_client, WC_Connect_Logger $logger ) {
  13. $this->api_client = $api_client;
  14. $this->logger = $logger;
  15. }
  16. public function fetch_service_schemas_from_connect_server() {
  17. $response_body = $this->api_client->get_service_schemas();
  18. if ( is_wp_error( $response_body ) ) {
  19. $error_data = $response_body->get_error_data();
  20. if ( isset( $error_data['response_status_code'] ) ) {
  21. $this->update_last_fetch_result_code( $error_data['response_status_code'] );
  22. }
  23. $this->logger->log( $response_body, __FUNCTION__ );
  24. return false;
  25. }
  26. $this->update_last_fetch_result_code( '200' );
  27. $this->logger->log( 'Successfully loaded service schemas from server response.', __FUNCTION__ );
  28. $this->update_last_fetch_timestamp();
  29. $this->maybe_update_heartbeat();
  30. $old_schemas = $this->get_service_schemas();
  31. if ( $old_schemas == $response_body ) {
  32. // schemas weren't changed, but were fetched without problems
  33. return true;
  34. }
  35. // If we made it this far, it is safe to store the object
  36. return $this->update_service_schemas( $response_body );
  37. }
  38. public function get_service_schemas() {
  39. return WC_Connect_Options::get_option( 'services', null );
  40. }
  41. protected function update_service_schemas( $service_schemas ) {
  42. return WC_Connect_Options::update_option( 'services', $service_schemas );
  43. }
  44. public function get_last_fetch_timestamp() {
  45. return WC_Connect_Options::get_option( 'services_last_update', null );
  46. }
  47. protected function update_last_fetch_timestamp() {
  48. WC_Connect_Options::update_option( 'services_last_update', time() );
  49. }
  50. public function get_last_fetch_result_code() {
  51. return WC_Connect_Options::get_option( 'services_last_result_code' );
  52. }
  53. /**
  54. * @param int $result_status_code
  55. */
  56. protected function update_last_fetch_result_code( $result_status_code ) {
  57. WC_Connect_Options::update_option( 'services_last_result_code', $result_status_code );
  58. }
  59. protected function maybe_update_heartbeat() {
  60. $last_heartbeat = WC_Connect_Options::get_option( 'last_heartbeat' );
  61. $now = time();
  62. if ( ! $last_heartbeat ) {
  63. $should_update = true;
  64. } else {
  65. $last_heartbeat = absint( $last_heartbeat );
  66. if ( $last_heartbeat > $now ) {
  67. // last heartbeat in the future? wacky
  68. $should_update = true;
  69. } else {
  70. $elapsed = $now - $last_heartbeat;
  71. $should_update = $elapsed > DAY_IN_SECONDS;
  72. }
  73. }
  74. if ( $should_update ) {
  75. WC_Connect_Options::update_option( 'last_heartbeat', $now );
  76. }
  77. }
  78. /**
  79. * Returns all service ids of a specific type (e.g. shipping)
  80. *
  81. * @param string $type The type of services to return
  82. *
  83. * @return array An array of that type's service ids, or an empty array if no such type is known
  84. */
  85. public function get_all_service_ids_of_type( $type ) {
  86. if ( empty( $type ) ) {
  87. return array();
  88. }
  89. $service_schemas = $this->get_service_schemas();
  90. if ( ! is_object( $service_schemas ) || ! property_exists( $service_schemas, $type ) || ! is_array( $service_schemas->$type ) ) {
  91. return array();
  92. }
  93. $service_schema_ids = array();
  94. foreach ( $service_schemas->$type as $service_schema ) {
  95. $service_schema_ids[] = $service_schema->id;
  96. }
  97. return $service_schema_ids;
  98. }
  99. /**
  100. * Returns all shipping method ids
  101. *
  102. * @return array|bool An array of supported shipping method ids or false if schema does not support method_id
  103. */
  104. public function get_all_shipping_method_ids() {
  105. $shipping_method_ids = array();
  106. $service_schemas = $this->get_service_schemas();
  107. if ( ! is_object( $service_schemas ) || ! property_exists( $service_schemas, 'shipping' ) || ! is_array( $service_schemas->shipping ) ) {
  108. return $shipping_method_ids;
  109. }
  110. foreach ( $service_schemas->shipping as $service_schema ) {
  111. if ( ! property_exists( $service_schema, 'method_id' ) ) {
  112. continue;
  113. }
  114. $shipping_method_ids[] = $service_schema->method_id;
  115. }
  116. return $shipping_method_ids;
  117. }
  118. /**
  119. * Returns a particular service's schema given its id
  120. *
  121. * @param string $service_id The service id for which to return the schema
  122. *
  123. * @return object|null The service schema or null if no such id was found
  124. */
  125. public function get_service_schema_by_id( $service_id ) {
  126. $service_schemas = $this->get_service_schemas();
  127. if ( ! is_object( $service_schemas ) ) {
  128. return null;
  129. }
  130. foreach ( $service_schemas as $service_type => $service_type_service_schemas ) {
  131. $matches = wp_filter_object_list( $service_type_service_schemas, array( 'id' => $service_id ) );
  132. if ( $matches ) {
  133. return array_shift( $matches );
  134. }
  135. }
  136. return null;
  137. }
  138. /**
  139. * Returns a particular service's schema given its method_id
  140. *
  141. * @param $method_id
  142. *
  143. * @return object|null The service schema or null if no such id was found
  144. */
  145. public function get_service_schema_by_method_id( $method_id ) {
  146. $service_schemas = $this->get_service_schemas();
  147. if ( ! is_object( $service_schemas ) ) {
  148. return null;
  149. }
  150. foreach ( $service_schemas as $service_type => $service_type_service_schemas ) {
  151. $matches = wp_filter_object_list( $service_type_service_schemas, array( 'method_id' => $method_id ) );
  152. if ( $matches ) {
  153. return array_shift( $matches );
  154. }
  155. }
  156. return null;
  157. }
  158. /**
  159. * Returns a service's schema given its shipping zone instance
  160. *
  161. * @param string $instance_id The shipping zone instance id for which to return the schema
  162. *
  163. * @return object|null The service schema or null if no such instance was found
  164. */
  165. public function get_service_schema_by_instance_id( $instance_id ) {
  166. global $wpdb;
  167. $method_id = $wpdb->get_var(
  168. $wpdb->prepare(
  169. "SELECT method_id FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE instance_id = %d;",
  170. $instance_id
  171. )
  172. );
  173. return $this->get_service_schema_by_method_id( $method_id );
  174. }
  175. /**
  176. * Returns a service's schema given an id or shipping zone instance.
  177. *
  178. * @param string $id_or_instance_id String ID or numeric instance ID.
  179. * @return object|null Service schema on success, null on failure
  180. */
  181. public function get_service_schema_by_id_or_instance_id( $id_or_instance_id ) {
  182. if ( is_numeric( $id_or_instance_id ) ) {
  183. return $this->get_service_schema_by_instance_id( $id_or_instance_id );
  184. }
  185. if ( ! empty( $id_or_instance_id ) ) {
  186. return $this->get_service_schema_by_method_id( $id_or_instance_id );
  187. }
  188. return null;
  189. }
  190. /**
  191. * Returns packages schema
  192. *
  193. * @return object|null Packages schema on success, null on failure
  194. */
  195. public function get_packages_schema() {
  196. $service_schemas = $this->get_service_schemas();
  197. if ( ! is_object( $service_schemas ) || ! property_exists( $service_schemas, 'boxes' ) ) {
  198. return null;
  199. }
  200. return $service_schemas->boxes;
  201. }
  202. public function get_predefined_packages_schema() {
  203. $service_schemas = $this->get_service_schemas();
  204. if ( ! is_object( $service_schemas ) ) {
  205. return null;
  206. }
  207. $predefined_packages = array();
  208. foreach ( $service_schemas->shipping as $service_schema ) {
  209. if ( ! isset( $service_schema->packages ) ) {
  210. continue;
  211. }
  212. $predefined_packages[ $service_schema->id ] = $service_schema->packages;
  213. }
  214. return $predefined_packages;
  215. }
  216. }
  217. }