WC_Customer_Download_Data_Store

WC Customer Download Data Store.

Defined (1)

The class is defined in the following location(s).

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