WC_Customer_Download

Class for customer download permissions.

Defined (1)

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

/includes/class-wc-customer-download.php  
  1. class WC_Customer_Download extends WC_Data implements ArrayAccess { 
  2.  
  3. /** 
  4. * This is the name of this object type. 
  5. * @var string 
  6. */ 
  7. protected $object_type = 'customer_download'; 
  8.  
  9. /** 
  10. * Download Data array. 
  11. * @since 3.0.0 
  12. * @var array 
  13. */ 
  14. protected $data = array( 
  15. 'download_id' => '',  
  16. 'product_id' => 0,  
  17. 'user_id' => 0,  
  18. 'user_email' => '',  
  19. 'order_id' => 0,  
  20. 'order_key' => '',  
  21. 'downloads_remaining' => '',  
  22. 'access_granted' => null,  
  23. 'access_expires' => null,  
  24. 'download_count' => 0,  
  25. ); 
  26.  
  27. /** 
  28. * Constructor. 
  29. * @param int|object|array $download 
  30. */ 
  31. public function __construct( $download = 0 ) { 
  32. parent::__construct( $download ); 
  33.  
  34. if ( is_numeric( $download ) && $download > 0 ) { 
  35. $this->set_id( $download ); 
  36. } elseif ( $download instanceof self ) { 
  37. $this->set_id( $download->get_id() ); 
  38. } elseif ( is_object( $download ) && ! empty( $download->permission_id ) ) { 
  39. $this->set_id( $download->permission_id ); 
  40. $this->set_props( (array) $download ); 
  41. $this->set_object_read( true ); 
  42. } else { 
  43. $this->set_object_read( true ); 
  44.  
  45. $this->data_store = WC_Data_Store::load( 'customer-download' ); 
  46.  
  47. if ( $this->get_id() > 0 ) { 
  48. $this->data_store->read( $this ); 
  49.  
  50. /** 
  51. |-------------------------------------------------------------------------- 
  52. | Getters 
  53. |-------------------------------------------------------------------------- 
  54. */ 
  55.  
  56. /** 
  57. * Get download id. 
  58. * @param string $context 
  59. * @return string 
  60. */ 
  61. public function get_download_id( $context = 'view' ) { 
  62. return $this->get_prop( 'download_id', $context ); 
  63.  
  64. /** 
  65. * Get product id. 
  66. * @param string $context 
  67. * @return integer 
  68. */ 
  69. public function get_product_id( $context = 'view' ) { 
  70. return $this->get_prop( 'product_id', $context ); 
  71.  
  72. /** 
  73. * Get user id. 
  74. * @param string $context 
  75. * @return integer 
  76. */ 
  77. public function get_user_id( $context = 'view' ) { 
  78. return $this->get_prop( 'user_id', $context ); 
  79.  
  80. /** 
  81. * Get user_email. 
  82. * @param string $context 
  83. * @return string 
  84. */ 
  85. public function get_user_email( $context = 'view' ) { 
  86. return $this->get_prop( 'user_email', $context ); 
  87.  
  88. /** 
  89. * Get order_id. 
  90. * @param string $context 
  91. * @return integer 
  92. */ 
  93. public function get_order_id( $context = 'view' ) { 
  94. return $this->get_prop( 'order_id', $context ); 
  95.  
  96. /** 
  97. * Get order_key. 
  98. * @param string $context 
  99. * @return string 
  100. */ 
  101. public function get_order_key( $context = 'view' ) { 
  102. return $this->get_prop( 'order_key', $context ); 
  103.  
  104. /** 
  105. * Get downloads_remaining. 
  106. * @param string $context 
  107. * @return integer|string 
  108. */ 
  109. public function get_downloads_remaining( $context = 'view' ) { 
  110. return $this->get_prop( 'downloads_remaining', $context ); 
  111.  
  112. /** 
  113. * Get access_granted. 
  114. * @param string $context 
  115. * @return WC_DateTime|null Object if the date is set or null if there is no date. 
  116. */ 
  117. public function get_access_granted( $context = 'view' ) { 
  118. return $this->get_prop( 'access_granted', $context ); 
  119.  
  120. /** 
  121. * Get access_expires. 
  122. * @param string $context 
  123. * @return WC_DateTime|null Object if the date is set or null if there is no date. 
  124. */ 
  125. public function get_access_expires( $context = 'view' ) { 
  126. return $this->get_prop( 'access_expires', $context ); 
  127.  
  128. /** 
  129. * Get download_count. 
  130. * @param string $context 
  131. * @return integer 
  132. */ 
  133. public function get_download_count( $context = 'view' ) { 
  134. return $this->get_prop( 'download_count', $context ); 
  135.  
  136. /** 
  137. |-------------------------------------------------------------------------- 
  138. | Setters 
  139. |-------------------------------------------------------------------------- 
  140. */ 
  141.  
  142. /** 
  143. * Set download id. 
  144. * @param string $value 
  145. */ 
  146. public function set_download_id( $value ) { 
  147. $this->set_prop( 'download_id', $value ); 
  148. /** 
  149. * Set product id. 
  150. * @param int $value 
  151. */ 
  152. public function set_product_id( $value ) { 
  153. $this->set_prop( 'product_id', absint( $value ) ); 
  154.  
  155. /** 
  156. * Get user id. 
  157. * @param int $value 
  158. */ 
  159. public function set_user_id( $value ) { 
  160. $this->set_prop( 'user_id', absint( $value ) ); 
  161.  
  162. /** 
  163. * Get user_email. 
  164. * @param int $value 
  165. */ 
  166. public function set_user_email( $value ) { 
  167. $this->set_prop( 'user_email', sanitize_email( $value ) ); 
  168.  
  169. /** 
  170. * Get order_id. 
  171. * @param int $value 
  172. */ 
  173. public function set_order_id( $value ) { 
  174. $this->set_prop( 'order_id', absint( $value ) ); 
  175.  
  176. /** 
  177. * Get order_key. 
  178. * @param string $value 
  179. */ 
  180. public function set_order_key( $value ) { 
  181. $this->set_prop( 'order_key', $value ); 
  182.  
  183. /** 
  184. * Get downloads_remaining. 
  185. * @param integer|string $value 
  186. */ 
  187. public function set_downloads_remaining( $value ) { 
  188. $this->set_prop( 'downloads_remaining', '' === $value ? '' : absint( $value ) ); 
  189.  
  190. /** 
  191. * Get access_granted. 
  192. * @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if their is no date. 
  193. */ 
  194. public function set_access_granted( $date = null ) { 
  195. $this->set_date_prop( 'access_granted', $date ); 
  196.  
  197. /** 
  198. * Get access_expires. 
  199. * @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if their is no date. 
  200. */ 
  201. public function set_access_expires( $date = null ) { 
  202. $this->set_date_prop( 'access_expires', $date ); 
  203.  
  204. /** 
  205. * Get download_count. 
  206. * @param int $value 
  207. */ 
  208. public function set_download_count( $value ) { 
  209. $this->set_prop( 'download_count', absint( $value ) ); 
  210.  
  211. /** 
  212. |-------------------------------------------------------------------------- 
  213. | CRUD methods 
  214. |-------------------------------------------------------------------------- 
  215. */ 
  216.  
  217. /** 
  218. * Save data to the database. 
  219. * @since 3.0.0 
  220. * @return int Item ID 
  221. */ 
  222. public function save() { 
  223. if ( $this->data_store ) { 
  224. // Trigger action before saving to the DB. Use a pointer to adjust object props before save. 
  225. do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store ); 
  226.  
  227. if ( $this->get_id() ) { 
  228. $this->data_store->update( $this ); 
  229. } else { 
  230. $this->data_store->create( $this ); 
  231. return $this->get_id(); 
  232.  
  233. /** 
  234. |-------------------------------------------------------------------------- 
  235. | ArrayAccess/Backwards compatibility. 
  236. |-------------------------------------------------------------------------- 
  237. */ 
  238.  
  239. /** 
  240. * offsetGet 
  241. * @param string $offset 
  242. * @return mixed 
  243. */ 
  244. public function offsetGet( $offset ) { 
  245. if ( is_callable( array( $this, "get_$offset" ) ) ) { 
  246. return $this->{"get_$offset"}(); 
  247.  
  248. /** 
  249. * offsetSet 
  250. * @param string $offset 
  251. * @param mixed $value 
  252. */ 
  253. public function offsetSet( $offset, $value ) { 
  254. if ( is_callable( array( $this, "set_$offset" ) ) ) { 
  255. $this->{"set_$offset"}( $value ); 
  256.  
  257. /** 
  258. * offsetUnset 
  259. * @param string $offset 
  260. */ 
  261. public function offsetUnset( $offset ) { 
  262. if ( is_callable( array( $this, "set_$offset" ) ) ) { 
  263. $this->{"set_$offset"}( '' ); 
  264.  
  265. /** 
  266. * offsetExists 
  267. * @param string $offset 
  268. * @return bool 
  269. */ 
  270. public function offsetExists( $offset ) { 
  271. return in_array( $offset, array_keys( $this->data ) ); 
  272.  
  273. /** 
  274. * Magic __isset method for backwards compatibility. Legacy properties which could be accessed directly in the past. 
  275. * @param string $key Key name. 
  276. * @return bool 
  277. */ 
  278. public function __isset( $key ) { 
  279. return in_array( $offset, array_keys( $this->data ) ); 
  280.  
  281. /** 
  282. * Magic __get method for backwards compatibility. Maps legacy vars to new getters. 
  283. * @param string $key Key name. 
  284. * @return mixed 
  285. */ 
  286. public function __get( $key ) { 
  287. if ( is_callable( array( $this, "get_$key" ) ) ) { 
  288. return $this->{"get_$key"}( '' );