WC_Product_Download

Represents a file which can be downloaded.

Defined (1)

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

/includes/class-wc-product-download.php  
  1. class WC_Product_Download implements ArrayAccess { 
  2.  
  3. /** 
  4. * Data array. 
  5. * @since 3.0.0 
  6. * @var array 
  7. */ 
  8. protected $data = array( 
  9. 'id' => '',  
  10. 'name' => '',  
  11. 'file' => '',  
  12. 'previous_hash' => '',  
  13. ); 
  14.  
  15. /** 
  16. * Returns all data for this object. 
  17. * @return array 
  18. */ 
  19. public function get_data() { 
  20. return $this->data; 
  21.  
  22. /** 
  23. * Get allowed mime types. 
  24. * @return array 
  25. */ 
  26. public function get_allowed_mime_types() { 
  27. return apply_filters( 'woocommerce_downloadable_file_allowed_mime_types', get_allowed_mime_types() ); 
  28.  
  29. /** 
  30. * Get type of file path set. 
  31. * @param string $file_path optional. 
  32. * @return string absolute, relative, or shortcode. 
  33. */ 
  34. public function get_type_of_file_path( $file_path = '' ) { 
  35. $file_path = $file_path ? $file_path : $this->get_file(); 
  36. if ( 0 === strpos( $file_path, 'http' ) ) { 
  37. return 'absolute'; 
  38. } elseif ( '[' === substr( $file_path, 0, 1 ) && ']' === substr( $file_path, -1 ) ) { 
  39. return 'shortcode'; 
  40. } else { 
  41. return 'relative'; 
  42.  
  43. /** 
  44. * Get file type. 
  45. * @return string 
  46. */ 
  47. public function get_file_type() { 
  48. $type = wp_check_filetype( strtok( $this->get_file(), '?' ), $this->get_allowed_mime_types() ); 
  49. return $type['type']; 
  50.  
  51. /** 
  52. * Get file extension. 
  53. * @return string 
  54. */ 
  55. public function get_file_extension() { 
  56. $parsed_url = parse_url( $this->get_file(), PHP_URL_PATH ); 
  57. return pathinfo( $parsed_url, PATHINFO_EXTENSION ); 
  58.  
  59. /** 
  60. * Check if file is allowed. 
  61. * @return boolean 
  62. */ 
  63. public function is_allowed_filetype() { 
  64. if ( 'relative' !== $this->get_type_of_file_path() ) { 
  65. return true; 
  66. return ! $this->get_file_extension() || in_array( $this->get_file_type(), $this->get_allowed_mime_types() ); 
  67.  
  68. /** 
  69. * Validate file exists. 
  70. * @return boolean 
  71. */ 
  72. public function file_exists() { 
  73. if ( 'relative' !== $this->get_type_of_file_path() ) { 
  74. return true; 
  75. $file_url = $this->get_file(); 
  76. if ( '..' === substr( $file_url, 0, 2 ) || '/' !== substr( $file_url, 0, 1 ) ) { 
  77. $file_url = realpath( ABSPATH . $file_url ); 
  78. } elseif ( '/wp-content' === substr( $file_url, 0, 11 ) ) { 
  79. $file_url = realpath( WP_CONTENT_DIR . substr( $file_url, 11 ) ); 
  80. return apply_filters( 'woocommerce_downloadable_file_exists', file_exists( $file_url ), $this->get_file() ); 
  81.  
  82. /** 
  83. |-------------------------------------------------------------------------- 
  84. | Setters 
  85. |-------------------------------------------------------------------------- 
  86. */ 
  87.  
  88. /** 
  89. * Set ID. 
  90. * @param string $value 
  91. */ 
  92. public function set_id( $value ) { 
  93. $this->data['id'] = wc_clean( $value ); 
  94.  
  95. /** 
  96. * Set name. 
  97. * @param string $value 
  98. */ 
  99. public function set_name( $value ) { 
  100. $this->data['name'] = wc_clean( $value ); 
  101.  
  102. /** 
  103. * Set previous_hash. 
  104. * @param string $value 
  105. */ 
  106. public function set_previous_hash( $value ) { 
  107. $this->data['previous_hash'] = wc_clean( $value ); 
  108.  
  109. /** 
  110. * Set file. 
  111. * @param string $value 
  112. */ 
  113. public function set_file( $value ) { 
  114. switch ( $this->get_type_of_file_path( $value ) ) { 
  115. case 'absolute' : 
  116. $this->data['file'] = esc_url_raw( $value ); 
  117. break; 
  118. default: 
  119. $this->data['file'] = wc_clean( $value ); 
  120. break; 
  121.  
  122. /** 
  123. |-------------------------------------------------------------------------- 
  124. | Getters 
  125. |-------------------------------------------------------------------------- 
  126. */ 
  127.  
  128. /** 
  129. * Get id. 
  130. * @return string 
  131. */ 
  132. public function get_id() { 
  133. return $this->data['id']; 
  134.  
  135. /** 
  136. * Get name. 
  137. * @return string 
  138. */ 
  139. public function get_name() { 
  140. return $this->data['name']; 
  141.  
  142. /** 
  143. * Get previous_hash. 
  144. * @return string 
  145. */ 
  146. public function get_previous_hash() { 
  147. return $this->data['previous_hash']; 
  148.  
  149. /** 
  150. * Get file. 
  151. * @return string 
  152. */ 
  153. public function get_file() { 
  154. return $this->data['file']; 
  155.  
  156. /** 
  157. |-------------------------------------------------------------------------- 
  158. | ArrayAccess/Backwards compatibility. 
  159. |-------------------------------------------------------------------------- 
  160. */ 
  161.  
  162. /** 
  163. * offsetGet 
  164. * @param string $offset 
  165. * @return mixed 
  166. */ 
  167. public function offsetGet( $offset ) { 
  168. switch ( $offset ) { 
  169. default : 
  170. if ( is_callable( array( $this, "get_$offset" ) ) ) { 
  171. return $this->{"get_$offset"}(); 
  172. break; 
  173. return ''; 
  174.  
  175. /** 
  176. * offsetSet 
  177. * @param string $offset 
  178. * @param mixed $value 
  179. */ 
  180. public function offsetSet( $offset, $value ) { 
  181. switch ( $offset ) { 
  182. default : 
  183. if ( is_callable( array( $this, "set_$offset" ) ) ) { 
  184. return $this->{"set_$offset"}( $value ); 
  185. break; 
  186.  
  187. /** 
  188. * offsetUnset 
  189. * @param string $offset 
  190. */ 
  191. public function offsetUnset( $offset ) {} 
  192.  
  193. /** 
  194. * offsetExists 
  195. * @param string $offset 
  196. * @return bool 
  197. */ 
  198. public function offsetExists( $offset ) { 
  199. return in_array( $offset, array_keys( $this->data ) );