class-wc-webhook-data-store.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. /**
  3. * Webhook Data Store
  4. *
  5. * @version 3.3.0
  6. * @package WooCommerce/Classes/Data_Store
  7. */
  8. if ( ! defined( 'ABSPATH' ) ) {
  9. exit;
  10. }
  11. /**
  12. * Webhook data store class.
  13. */
  14. class WC_Webhook_Data_Store implements WC_Webhook_Data_Store_Interface {
  15. /**
  16. * Create a new webhook in the database.
  17. *
  18. * @since 3.3.0
  19. * @param WC_Webhook $webhook Webhook instance.
  20. */
  21. public function create( &$webhook ) {
  22. global $wpdb;
  23. $changes = $webhook->get_changes();
  24. if ( isset( $changes['date_created'] ) ) {
  25. $date_created = $webhook->get_date_created()->date( 'Y-m-d H:i:s' );
  26. $date_created_gmt = gmdate( 'Y-m-d H:i:s', $webhook->get_date_created()->getTimestamp() );
  27. } else {
  28. $date_created = current_time( 'mysql' );
  29. $date_created_gmt = current_time( 'mysql', 1 );
  30. $webhook->set_date_created( $date_created );
  31. }
  32. // Pending delivery by default if not set while creating a new webhook.
  33. if ( ! isset( $changes['pending_delivery'] ) ) {
  34. $webhook->set_pending_delivery( true );
  35. }
  36. $data = array(
  37. 'status' => $webhook->get_status( 'edit' ),
  38. 'name' => $webhook->get_name( 'edit' ),
  39. 'user_id' => $webhook->get_user_id( 'edit' ),
  40. 'delivery_url' => $webhook->get_delivery_url( 'edit' ),
  41. 'secret' => $webhook->get_secret( 'edit' ),
  42. 'topic' => $webhook->get_topic( 'edit' ),
  43. 'date_created' => $date_created,
  44. 'date_created_gmt' => $date_created_gmt,
  45. 'api_version' => $this->get_api_version_number( $webhook->get_api_version( 'edit' ) ),
  46. 'failure_count' => $webhook->get_failure_count( 'edit' ),
  47. 'pending_delivery' => $webhook->get_pending_delivery( 'edit' ),
  48. );
  49. $wpdb->insert( $wpdb->prefix . 'wc_webhooks', $data ); // WPCS: DB call ok.
  50. $webhook_id = $wpdb->insert_id;
  51. $webhook->set_id( $webhook_id );
  52. $webhook->apply_changes();
  53. delete_transient( 'woocommerce_webhook_ids' );
  54. WC_Cache_Helper::incr_cache_prefix( 'webhooks' );
  55. do_action( 'woocommerce_new_webhook', $webhook_id );
  56. }
  57. /**
  58. * Read a webhook from the database.
  59. *
  60. * @since 3.3.0
  61. * @param WC_Webhook $webhook Webhook instance.
  62. * @throws Exception When webhook is invalid.
  63. */
  64. public function read( &$webhook ) {
  65. global $wpdb;
  66. $data = wp_cache_get( $webhook->get_id(), 'webhooks' );
  67. if ( false === $data ) {
  68. $data = $wpdb->get_row( $wpdb->prepare( "SELECT webhook_id, status, name, user_id, delivery_url, secret, topic, date_created, date_modified, api_version, failure_count, pending_delivery FROM {$wpdb->prefix}wc_webhooks WHERE webhook_id = %d LIMIT 1;", $webhook->get_id() ), ARRAY_A ); // WPCS: cache ok, DB call ok.
  69. wp_cache_add( $webhook->get_id(), $data, 'webhooks' );
  70. }
  71. if ( is_array( $data ) ) {
  72. $webhook->set_props(
  73. array(
  74. 'id' => $data['webhook_id'],
  75. 'status' => $data['status'],
  76. 'name' => $data['name'],
  77. 'user_id' => $data['user_id'],
  78. 'delivery_url' => $data['delivery_url'],
  79. 'secret' => $data['secret'],
  80. 'topic' => $data['topic'],
  81. 'date_created' => '0000-00-00 00:00:00' === $data['date_created'] ? null : $data['date_created'],
  82. 'date_modified' => '0000-00-00 00:00:00' === $data['date_modified'] ? null : $data['date_modified'],
  83. 'api_version' => $data['api_version'],
  84. 'failure_count' => $data['failure_count'],
  85. 'pending_delivery' => $data['pending_delivery'],
  86. )
  87. );
  88. $webhook->set_object_read( true );
  89. do_action( 'woocommerce_webhook_loaded', $webhook );
  90. } else {
  91. throw new Exception( __( 'Invalid webhook.', 'woocommerce' ) );
  92. }
  93. }
  94. /**
  95. * Update a webhook.
  96. *
  97. * @since 3.3.0
  98. * @param WC_Webhook $webhook Webhook instance.
  99. */
  100. public function update( &$webhook ) {
  101. global $wpdb;
  102. $changes = $webhook->get_changes();
  103. $trigger = isset( $changes['delivery_url'] );
  104. if ( isset( $changes['date_modified'] ) ) {
  105. $date_modified = $webhook->get_date_modified()->date( 'Y-m-d H:i:s' );
  106. $date_modified_gmt = gmdate( 'Y-m-d H:i:s', $webhook->get_date_modified()->getTimestamp() );
  107. } else {
  108. $date_modified = current_time( 'mysql' );
  109. $date_modified_gmt = current_time( 'mysql', 1 );
  110. $webhook->set_date_modified( $date_modified );
  111. }
  112. $data = array(
  113. 'status' => $webhook->get_status( 'edit' ),
  114. 'name' => $webhook->get_name( 'edit' ),
  115. 'user_id' => $webhook->get_user_id( 'edit' ),
  116. 'delivery_url' => $webhook->get_delivery_url( 'edit' ),
  117. 'secret' => $webhook->get_secret( 'edit' ),
  118. 'topic' => $webhook->get_topic( 'edit' ),
  119. 'date_modified' => $date_modified,
  120. 'date_modified_gmt' => $date_modified_gmt,
  121. 'api_version' => $this->get_api_version_number( $webhook->get_api_version( 'edit' ) ),
  122. 'failure_count' => $webhook->get_failure_count( 'edit' ),
  123. 'pending_delivery' => $webhook->get_pending_delivery( 'edit' ),
  124. );
  125. $wpdb->update(
  126. $wpdb->prefix . 'wc_webhooks',
  127. $data,
  128. array(
  129. 'webhook_id' => $webhook->get_id( 'edit' ),
  130. )
  131. ); // WPCS: DB call ok.
  132. $webhook->apply_changes();
  133. wp_cache_delete( $webhook->get_id(), 'webhooks' );
  134. WC_Cache_Helper::incr_cache_prefix( 'webhooks' );
  135. if ( 'active' === $webhook->get_status() && ( $trigger || $webhook->get_pending_delivery() ) ) {
  136. $webhook->deliver_ping();
  137. }
  138. do_action( 'woocommerce_webhook_updated', $webhook->get_id() );
  139. }
  140. /**
  141. * Remove a webhook from the database.
  142. *
  143. * @since 3.3.0
  144. * @param WC_Webhook $webhook Webhook instance.
  145. * @param bool $force_delete Skip trash bin forcing to delete.
  146. */
  147. public function delete( &$webhook, $force_delete = false ) {
  148. global $wpdb;
  149. $wpdb->delete(
  150. $wpdb->prefix . 'wc_webhooks',
  151. array(
  152. 'webhook_id' => $webhook->get_id(),
  153. ),
  154. array( '%d' )
  155. ); // WPCS: cache ok, DB call ok.
  156. delete_transient( 'woocommerce_webhook_ids' );
  157. WC_Cache_Helper::incr_cache_prefix( 'webhooks' );
  158. do_action( 'woocommerce_webhook_deleted', $webhook->get_id(), $webhook );
  159. }
  160. /**
  161. * Get API version number.
  162. *
  163. * @since 3.3.0
  164. * @param string $api_version REST API version.
  165. * @return int
  166. */
  167. public function get_api_version_number( $api_version ) {
  168. return 'legacy_v3' === $api_version ? -1 : intval( substr( $api_version, -1 ) );
  169. }
  170. /**
  171. * Get all webhooks IDs.
  172. *
  173. * @since 3.3.0
  174. * @return int[]
  175. */
  176. public function get_webhooks_ids() {
  177. global $wpdb;
  178. $ids = get_transient( 'woocommerce_webhook_ids' );
  179. if ( false === $ids ) {
  180. $results = $wpdb->get_results( "SELECT webhook_id FROM {$wpdb->prefix}wc_webhooks" ); // WPCS: cache ok, DB call ok.
  181. $ids = array_map( 'intval', wp_list_pluck( $results, 'webhook_id' ) );
  182. set_transient( 'woocommerce_webhook_ids', $ids );
  183. }
  184. return $ids;
  185. }
  186. /**
  187. * Search webhooks.
  188. *
  189. * @param array $args Search arguments.
  190. * @return array
  191. */
  192. public function search_webhooks( $args ) {
  193. global $wpdb;
  194. $args = wp_parse_args(
  195. $args, array(
  196. 'limit' => 10,
  197. 'offset' => 0,
  198. 'order' => 'DESC',
  199. 'orderby' => 'id',
  200. )
  201. );
  202. // Map post statuses.
  203. $statuses = array(
  204. 'publish' => 'active',
  205. 'draft' => 'paused',
  206. 'pending' => 'disabled',
  207. );
  208. // Map orderby to support a few post keys.
  209. $orderby_mapping = array(
  210. 'ID' => 'webhook_id',
  211. 'id' => 'webhook_id',
  212. 'name' => 'name',
  213. 'title' => 'name',
  214. 'post_title' => 'name',
  215. 'post_name' => 'name',
  216. 'date_created' => 'date_created_gmt',
  217. 'date' => 'date_created_gmt',
  218. 'post_date' => 'date_created_gmt',
  219. 'date_modified' => 'date_modified_gmt',
  220. 'modified' => 'date_modified_gmt',
  221. 'post_modified' => 'date_modified_gmt',
  222. );
  223. $orderby = isset( $orderby_mapping[ $args['orderby'] ] ) ? $orderby_mapping[ $args['orderby'] ] : 'webhook_id';
  224. $limit = -1 < $args['limit'] ? sprintf( 'LIMIT %d', $args['limit'] ) : '';
  225. $offset = 0 < $args['offset'] ? sprintf( 'OFFSET %d', $args['offset'] ) : '';
  226. $status = ! empty( $args['status'] ) ? "AND `status` = '" . sanitize_key( isset( $statuses[ $args['status'] ] ) ? $statuses[ $args['status'] ] : $args['status'] ) . "'" : '';
  227. $search = ! empty( $args['search'] ) ? "AND `name` LIKE '%" . $wpdb->esc_like( sanitize_text_field( $args['search'] ) ) . "%'" : '';
  228. $include = '';
  229. $exclude = '';
  230. $date_created = '';
  231. $date_modified = '';
  232. if ( ! empty( $args['include'] ) ) {
  233. $args['include'] = implode( ',', wp_parse_id_list( $args['include'] ) );
  234. $include = 'AND webhook_id IN (' . $args['include'] . ')';
  235. }
  236. if ( ! empty( $args['exclude'] ) ) {
  237. $args['exclude'] = implode( ',', wp_parse_id_list( $args['exclude'] ) );
  238. $exclude = 'AND webhook_id NOT IN (' . $args['exclude'] . ')';
  239. }
  240. if ( ! empty( $args['after'] ) || ! empty( $args['before'] ) ) {
  241. $args['after'] = empty( $args['after'] ) ? '0000-00-00' : $args['after'];
  242. $args['before'] = empty( $args['before'] ) ? current_time( 'mysql', 1 ) : $args['before'];
  243. $date_created = "AND `date_created_gmt` BETWEEN STR_TO_DATE('" . $args['after'] . "', '%Y-%m-%d %H:%i:%s') and STR_TO_DATE('" . $args['before'] . "', '%Y-%m-%d %H:%i:%s')";
  244. }
  245. if ( ! empty( $args['modified_after'] ) || ! empty( $args['modified_before'] ) ) {
  246. $args['modified_after'] = empty( $args['modified_after'] ) ? '0000-00-00' : $args['modified_after'];
  247. $args['modified_before'] = empty( $args['modified_before'] ) ? current_time( 'mysql', 1 ) : $args['modified_before'];
  248. $date_modified = "AND `date_modified_gmt` BETWEEN STR_TO_DATE('" . $args['modified_after'] . "', '%Y-%m-%d %H:%i:%s') and STR_TO_DATE('" . $args['modified_before'] . "', '%Y-%m-%d %H:%i:%s')";
  249. }
  250. $order = "ORDER BY {$orderby} " . strtoupper( sanitize_key( $args['order'] ) );
  251. // Check for cache.
  252. $cache_key = WC_Cache_Helper::get_cache_prefix( 'webhooks' ) . 'search_webhooks' . md5( implode( ',', $args ) );
  253. $ids = wp_cache_get( $cache_key, 'webhook_search_results' );
  254. if ( false !== $ids ) {
  255. return $ids;
  256. }
  257. $query = trim(
  258. "SELECT webhook_id
  259. FROM {$wpdb->prefix}wc_webhooks
  260. WHERE 1=1
  261. {$status}
  262. {$search}
  263. {$include}
  264. {$exclude}
  265. {$date_created}
  266. {$date_modified}
  267. {$order}
  268. {$limit}
  269. {$offset}"
  270. );
  271. $results = $wpdb->get_results( $query ); // WPCS: cache ok, DB call ok, unprepared SQL ok.
  272. $ids = wp_list_pluck( $results, 'webhook_id' );
  273. wp_cache_set( $cache_key, $ids, 'webhook_search_results' );
  274. return $ids;
  275. }
  276. /**
  277. * Get total webhook counts by status.
  278. *
  279. * @return array
  280. */
  281. public function get_count_webhooks_by_status() {
  282. $statuses = array_keys( wc_get_webhook_statuses() );
  283. $counts = array();
  284. foreach ( $statuses as $status ) {
  285. $count = count(
  286. $this->search_webhooks(
  287. array(
  288. 'limit' => -1,
  289. 'status' => $status,
  290. )
  291. )
  292. );
  293. $counts[ $status ] = $count;
  294. }
  295. return $counts;
  296. }
  297. }