/includes/data-stores/class-wc-customer-download-data-store.php

  1. <?php 
  2. if ( ! defined( 'ABSPATH' ) ) { 
  3. exit; 
  4.  
  5. /** 
  6. * WC Customer Download Data Store. 
  7. * 
  8. * @version 3.0.0 
  9. * @category Class 
  10. * @author WooThemes 
  11. */ 
  12. class WC_Customer_Download_Data_Store implements WC_Customer_Download_Data_Store_Interface { 
  13.  
  14. /** 
  15. * Create download permission for a user. 
  16. * 
  17. * @param WC_Customer_Download $download 
  18. */ 
  19. public function create( &$download ) { 
  20. global $wpdb; 
  21.  
  22. // Always set a access granted date. 
  23. if ( is_null( $download->get_access_granted( 'edit' ) ) ) { 
  24. $download->set_access_granted( current_time( 'timestamp', true ) ); 
  25.  
  26. $data = array( 
  27. 'download_id' => $download->get_download_id( 'edit' ),  
  28. 'product_id' => $download->get_product_id( 'edit' ),  
  29. 'user_id' => $download->get_user_id( 'edit' ),  
  30. 'user_email' => $download->get_user_email( 'edit' ),  
  31. 'order_id' => $download->get_order_id( 'edit' ),  
  32. 'order_key' => $download->get_order_key( 'edit' ),  
  33. 'downloads_remaining' => $download->get_downloads_remaining( 'edit' ),  
  34. 'access_granted' => date( 'Y-m-d', $download->get_access_granted( 'edit' )->getTimestamp() ),  
  35. 'download_count' => $download->get_download_count( 'edit' ),  
  36. 'access_expires' => ! is_null( $download->get_access_expires( 'edit' ) ) ? date( 'Y-m-d', $download->get_access_expires( 'edit' )->getTimestamp() ) : null,  
  37. ); 
  38.  
  39. $format = array( 
  40. '%s',  
  41. '%s',  
  42. '%s',  
  43. '%s',  
  44. '%s',  
  45. '%s',  
  46. '%s',  
  47. '%s',  
  48. '%d',  
  49. '%s',  
  50. ); 
  51.  
  52. $result = $wpdb->insert( 
  53. $wpdb->prefix . 'woocommerce_downloadable_product_permissions',  
  54. apply_filters( 'woocommerce_downloadable_file_permission_data', $data ),  
  55. apply_filters( 'woocommerce_downloadable_file_permission_format', $format, $data ) 
  56. ); 
  57.  
  58. do_action( 'woocommerce_grant_product_download_access', $data ); 
  59.  
  60. if ( $result ) { 
  61. $download->set_id( $wpdb->insert_id ); 
  62. $download->apply_changes(); 
  63.  
  64. /** 
  65. * Method to read a download permission from the database. 
  66. * 
  67. * @param WC_Customer_Download 
  68. */ 
  69. public function read( &$download ) { 
  70. global $wpdb; 
  71.  
  72. $download->set_defaults(); 
  73.  
  74. if ( ! $download->get_id() || ! ( $raw_download = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE permission_id = %d", $download->get_id() ) ) ) ) { 
  75. throw new Exception( __( 'Invalid download.', 'woocommerce' ) ); 
  76.  
  77. $download->set_props( array( 
  78. 'download_id' => $raw_download->download_id,  
  79. 'product_id' => $raw_download->product_id,  
  80. 'user_id' => $raw_download->user_id,  
  81. 'user_email' => $raw_download->user_email,  
  82. 'order_id' => $raw_download->order_id,  
  83. 'order_key' => $raw_download->order_key,  
  84. 'downloads_remaining' => $raw_download->downloads_remaining,  
  85. 'access_granted' => strtotime( $raw_download->access_granted ),  
  86. 'download_count' => $raw_download->download_count,  
  87. 'access_expires' => is_null( $raw_download->access_expires ) ? null : strtotime( $raw_download->access_expires ),  
  88. ) ); 
  89. $download->set_object_read( true ); 
  90.  
  91. /** 
  92. * Method to update a download in the database. 
  93. * 
  94. * @param WC_Customer_Download $download 
  95. */ 
  96. public function update( &$download ) { 
  97. global $wpdb; 
  98.  
  99. $data = array( 
  100. 'download_id' => $download->get_download_id( 'edit' ),  
  101. 'product_id' => $download->get_product_id( 'edit' ),  
  102. 'user_id' => $download->get_user_id( 'edit' ),  
  103. 'user_email' => $download->get_user_email( 'edit' ),  
  104. 'order_id' => $download->get_order_id( 'edit' ),  
  105. 'order_key' => $download->get_order_key( 'edit' ),  
  106. 'downloads_remaining' => $download->get_downloads_remaining( 'edit' ),  
  107. 'access_granted' => date( 'Y-m-d', $download->get_access_granted( 'edit' )->getTimestamp() ),  
  108. 'download_count' => $download->get_download_count( 'edit' ),  
  109. 'access_expires' => ! is_null( $download->get_access_expires( 'edit' ) ) ? date( 'Y-m-d', $download->get_access_expires( 'edit' )->getTimestamp() ) : null,  
  110. ); 
  111.  
  112. $format = array( 
  113. '%s',  
  114. '%s',  
  115. '%s',  
  116. '%s',  
  117. '%s',  
  118. '%s',  
  119. '%s',  
  120. '%s',  
  121. '%d',  
  122. '%s',  
  123. ); 
  124.  
  125. $wpdb->update( 
  126. $wpdb->prefix . 'woocommerce_downloadable_product_permissions',  
  127. $data,  
  128. array( 
  129. 'permission_id' => $download->get_id(),  
  130. ),  
  131. $format 
  132. ); 
  133. $download->apply_changes(); 
  134.  
  135. /** 
  136. * Method to delete a download permission from the database. 
  137. * 
  138. * @param WC_Customer_Download $download 
  139. * @param array $args Array of args to pass to the delete method. 
  140. */ 
  141. public function delete( &$download, $args = array() ) { 
  142. global $wpdb; 
  143.  
  144. $wpdb->query( $wpdb->prepare( " 
  145. DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions 
  146. WHERE permission_id = %d 
  147. ", $download->get_id() ) ); 
  148.  
  149. $download->set_id( 0 ); 
  150.  
  151. /** 
  152. * Method to delete a download permission from the database by ID. 
  153. * 
  154. * @param int $id 
  155. */ 
  156. public function delete_by_id( $id ) { 
  157. global $wpdb; 
  158. $wpdb->query( $wpdb->prepare( " 
  159. DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions 
  160. WHERE permission_id = %d 
  161. ", $id ) ); 
  162.  
  163. /** 
  164. * Method to delete a download permission from the database by order ID. 
  165. * 
  166. * @param int $id 
  167. */ 
  168. public function delete_by_order_id( $id ) { 
  169. global $wpdb; 
  170. $wpdb->query( $wpdb->prepare( " 
  171. DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions 
  172. WHERE order_id = %d 
  173. ", $id ) ); 
  174.  
  175. /** 
  176. * Method to delete a download permission from the database by download ID. 
  177. * 
  178. * @param int $id 
  179. */ 
  180. public function delete_by_download_id( $id ) { 
  181. global $wpdb; 
  182. $wpdb->query( $wpdb->prepare( " 
  183. DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions 
  184. WHERE download_id = %s 
  185. ", $id ) ); 
  186.  
  187. /** 
  188. * Get a download object. 
  189. * 
  190. * @param array $data From the DB. 
  191. * @return WC_Customer_Download 
  192. */ 
  193. private function get_download( $data ) { 
  194. return new WC_Customer_Download( $data ); 
  195.  
  196. /** 
  197. * Get array of download ids by specified args. 
  198. * 
  199. * @param array $args 
  200. * @return array 
  201. */ 
  202. public function get_downloads( $args = array() ) { 
  203. global $wpdb; 
  204.  
  205. $args = wp_parse_args( $args, array( 
  206. 'user_email' => '',  
  207. 'order_id' => '',  
  208. 'order_key' => '',  
  209. 'product_id' => '',  
  210. 'download_id' => '',  
  211. 'orderby' => 'permission_id',  
  212. 'order' => 'DESC',  
  213. 'limit' => -1,  
  214. 'return' => 'objects',  
  215. ) ); 
  216.  
  217. $query = array(); 
  218. $query[] = "SELECT * FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE 1=1"; 
  219.  
  220. if ( $args['user_email'] ) { 
  221. $query[] = $wpdb->prepare( "AND user_email = %s", sanitize_email( $args['user_email'] ) ); 
  222.  
  223. if ( $args['order_id'] ) { 
  224. $query[] = $wpdb->prepare( "AND order_id = %d", $args['order_id'] ); 
  225.  
  226. if ( $args['order_key'] ) { 
  227. $query[] = $wpdb->prepare( "AND order_key = %s", $args['order_key'] ); 
  228.  
  229. if ( $args['product_id'] ) { 
  230. $query[] = $wpdb->prepare( "AND product_id = %d", $args['product_id'] ); 
  231.  
  232. if ( $args['download_id'] ) { 
  233. $query[] = $wpdb->prepare( "AND download_id = %s", $args['download_id'] ); 
  234.  
  235. $allowed_orders = array( 'permission_id', 'download_id', 'product_id', 'order_id', 'order_key', 'user_email', 'user_id', 'downloads_remaining', 'access_granted', 'access_expires', 'download_count' ); 
  236. $order = in_array( $args['order'], $allowed_orders ) ? $args['order'] : 'permission_id'; 
  237. $orderby = 'DESC' === strtoupper( $args['orderby'] ) ? 'DESC' : 'ASC'; 
  238. $orderby_sql = sanitize_sql_orderby( "{$order} {$orderby}" ); 
  239. $query[] = "ORDER BY {$orderby_sql}"; 
  240.  
  241. if ( 0 < $args['limit'] ) { 
  242. $query[] = $wpdb->prepare( "LIMIT %d", $args['limit'] ); 
  243.  
  244. $raw_downloads = $wpdb->get_results( implode( ' ', $query ) ); 
  245.  
  246. switch ( $args['return'] ) { 
  247. case 'ids' : 
  248. return wp_list_pluck( $raw_downloads, 'permission_id' ); 
  249. default : 
  250. return array_map( array( $this, 'get_download' ), $raw_downloads ); 
  251.  
  252. /** 
  253. * Update download ids if the hash changes. 
  254. * 
  255. * @param int $product_id 
  256. * @param string $old_id 
  257. * @param string $new_id 
  258. */ 
  259. public function update_download_id( $product_id, $old_id, $new_id ) { 
  260. global $wpdb; 
  261.  
  262. $wpdb->update( 
  263. $wpdb->prefix . 'woocommerce_downloadable_product_permissions',  
  264. array( 
  265. 'download_id' => $new_id,  
  266. ),  
  267. array( 
  268. 'download_id' => $old_id,  
  269. 'product_id' => $product_id,  
  270. ); 
  271.  
  272. /** 
  273. * Get a customers downloads. 
  274. * 
  275. * @param int $customer_id 
  276. * @return array 
  277. */ 
  278. public function get_downloads_for_customer( $customer_id ) { 
  279. global $wpdb; 
  280.  
  281. return $wpdb->get_results( 
  282. $wpdb->prepare( " 
  283. SELECT * FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions as permissions 
  284. WHERE user_id = %d 
  285. AND permissions.order_id > 0 
  286. AND 
  287. permissions.downloads_remaining > 0 
  288. OR permissions.downloads_remaining = '' 
  289. AND 
  290. permissions.access_expires IS NULL 
  291. OR permissions.access_expires >= %s 
  292. OR permissions.access_expires = '0000-00-00 00:00:00' 
  293. ORDER BY permissions.order_id, permissions.product_id, permissions.permission_id; 
  294. ",  
  295. $customer_id,  
  296. date( 'Y-m-d', current_time( 'timestamp' ) ) 
  297. ); 
  298.  
  299. /** 
  300. * Update user prop for downloads based on order id. 
  301. * 
  302. * @param int $order_id 
  303. * @param int $customer_id 
  304. * @param string $email 
  305. */ 
  306. public function update_user_by_order_id( $order_id, $customer_id, $email ) { 
  307. global $wpdb; 
  308. $wpdb->update( $wpdb->prefix . 'woocommerce_downloadable_product_permissions',  
  309. array( 
  310. 'user_id' => $customer_id,  
  311. 'user_email' => $email,  
  312. ),  
  313. array( 
  314. 'order_id' => $order_id,  
  315. ),  
  316. array( 
  317. '%d',  
  318. '%s',  
  319. ),  
  320. array( 
  321. '%d',  
  322. ); 
.