| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?php
- /**
- * Represents a file which can be downloaded.
- *
- * @package WooCommerce/Classes
- * @version 3.0.0
- * @since 3.0.0
- */
- defined( 'ABSPATH' ) || exit;
- /**
- * Product download class.
- */
- class WC_Product_Download implements ArrayAccess {
- /**
- * Data array.
- *
- * @since 3.0.0
- * @var array
- */
- protected $data = array(
- 'id' => '',
- 'name' => '',
- 'file' => '',
- );
- /**
- * Returns all data for this object.
- *
- * @return array
- */
- public function get_data() {
- return $this->data;
- }
- /**
- * Get allowed mime types.
- *
- * @return array
- */
- public function get_allowed_mime_types() {
- return apply_filters( 'woocommerce_downloadable_file_allowed_mime_types', get_allowed_mime_types() );
- }
- /**
- * Get type of file path set.
- *
- * @param string $file_path optional.
- * @return string absolute, relative, or shortcode.
- */
- public function get_type_of_file_path( $file_path = '' ) {
- $file_path = $file_path ? $file_path : $this->get_file();
- if ( 0 === strpos( $file_path, 'http' ) || 0 === strpos( $file_path, '//' ) ) {
- return 'absolute';
- } elseif ( '[' === substr( $file_path, 0, 1 ) && ']' === substr( $file_path, -1 ) ) {
- return 'shortcode';
- } else {
- return 'relative';
- }
- }
- /**
- * Get file type.
- *
- * @return string
- */
- public function get_file_type() {
- $type = wp_check_filetype( strtok( $this->get_file(), '?' ), $this->get_allowed_mime_types() );
- return $type['type'];
- }
- /**
- * Get file extension.
- *
- * @return string
- */
- public function get_file_extension() {
- $parsed_url = wp_parse_url( $this->get_file(), PHP_URL_PATH );
- return pathinfo( $parsed_url, PATHINFO_EXTENSION );
- }
- /**
- * Check if file is allowed.
- *
- * @return boolean
- */
- public function is_allowed_filetype() {
- if ( 'relative' !== $this->get_type_of_file_path() ) {
- return true;
- }
- return ! $this->get_file_extension() || in_array( $this->get_file_type(), $this->get_allowed_mime_types(), true );
- }
- /**
- * Validate file exists.
- *
- * @return boolean
- */
- public function file_exists() {
- if ( 'relative' !== $this->get_type_of_file_path() ) {
- return true;
- }
- $file_url = $this->get_file();
- if ( '..' === substr( $file_url, 0, 2 ) || '/' !== substr( $file_url, 0, 1 ) ) {
- $file_url = realpath( ABSPATH . $file_url );
- } elseif ( substr( WP_CONTENT_DIR, strlen( untrailingslashit( ABSPATH ) ) ) === substr( $file_url, 0, strlen( substr( WP_CONTENT_DIR, strlen( untrailingslashit( ABSPATH ) ) ) ) ) ) {
- $file_url = realpath( WP_CONTENT_DIR . substr( $file_url, 11 ) );
- }
- return apply_filters( 'woocommerce_downloadable_file_exists', file_exists( $file_url ), $this->get_file() );
- }
- /*
- |--------------------------------------------------------------------------
- | Setters
- |--------------------------------------------------------------------------
- */
- /**
- * Set ID.
- *
- * @param string $value Download ID.
- */
- public function set_id( $value ) {
- $this->data['id'] = wc_clean( $value );
- }
- /**
- * Set name.
- *
- * @param string $value Download name.
- */
- public function set_name( $value ) {
- $this->data['name'] = wc_clean( $value );
- }
- /**
- * Set previous_hash.
- *
- * @deprecated 3.3.0 No longer using filename based hashing to keep track of files.
- * @param string $value Previous hash.
- */
- public function set_previous_hash( $value ) {
- wc_deprecated_function( __FUNCTION__, '3.3' );
- $this->data['previous_hash'] = wc_clean( $value );
- }
- /**
- * Set file.
- *
- * @param string $value File.
- */
- public function set_file( $value ) {
- switch ( $this->get_type_of_file_path( $value ) ) {
- case 'absolute':
- $this->data['file'] = esc_url_raw( $value );
- break;
- default:
- $this->data['file'] = wc_clean( $value );
- break;
- }
- }
- /*
- |--------------------------------------------------------------------------
- | Getters
- |--------------------------------------------------------------------------
- */
- /**
- * Get id.
- *
- * @return string
- */
- public function get_id() {
- return $this->data['id'];
- }
- /**
- * Get name.
- *
- * @return string
- */
- public function get_name() {
- return $this->data['name'];
- }
- /**
- * Get previous_hash.
- *
- * @deprecated 3.3.0 No longer using filename based hashing to keep track of files.
- * @return string
- */
- public function get_previous_hash() {
- wc_deprecated_function( __FUNCTION__, '3.3' );
- return $this->data['previous_hash'];
- }
- /**
- * Get file.
- *
- * @return string
- */
- public function get_file() {
- return $this->data['file'];
- }
- /*
- |--------------------------------------------------------------------------
- | ArrayAccess/Backwards compatibility.
- |--------------------------------------------------------------------------
- */
- /**
- * OffsetGet.
- *
- * @param string $offset Offset.
- * @return mixed
- */
- public function offsetGet( $offset ) {
- switch ( $offset ) {
- default:
- if ( is_callable( array( $this, "get_$offset" ) ) ) {
- return $this->{"get_$offset"}();
- }
- break;
- }
- return '';
- }
- /**
- * OffsetSet.
- *
- * @param string $offset Offset.
- * @param mixed $value Value.
- */
- public function offsetSet( $offset, $value ) {
- switch ( $offset ) {
- default:
- if ( is_callable( array( $this, "set_$offset" ) ) ) {
- return $this->{"set_$offset"}( $value );
- }
- break;
- }
- }
- /**
- * OffsetUnset.
- *
- * @param string $offset Offset.
- */
- public function offsetUnset( $offset ) {}
- /**
- * OffsetExists.
- *
- * @param string $offset Offset.
- * @return bool
- */
- public function offsetExists( $offset ) {
- return in_array( $offset, array_keys( $this->data ), true );
- }
- }
|