2016-11-11 15:31:00 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Represents a file which can be downloaded.
|
|
|
|
*
|
2018-03-22 18:10:22 +00:00
|
|
|
* @package WooCommerce/Classes
|
|
|
|
* @version 3.0.0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Product download class.
|
2016-11-11 15:31:00 +00:00
|
|
|
*/
|
|
|
|
class WC_Product_Download implements ArrayAccess {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data array.
|
2018-03-22 18:10:22 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-11 15:31:00 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $data = array(
|
2018-03-22 18:10:22 +00:00
|
|
|
'id' => '',
|
|
|
|
'name' => '',
|
|
|
|
'file' => '',
|
2016-11-11 15:31:00 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all data for this object.
|
2018-03-22 18:10:22 +00:00
|
|
|
*
|
2016-11-11 15:31:00 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_data() {
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get allowed mime types.
|
2018-03-22 18:10:22 +00:00
|
|
|
*
|
2016-11-11 15:31:00 +00:00
|
|
|
* @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.
|
2018-03-22 18:10:22 +00:00
|
|
|
*
|
2016-11-11 15:31:00 +00:00
|
|
|
* @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();
|
2018-01-15 15:41:31 +00:00
|
|
|
if ( 0 === strpos( $file_path, 'http' ) || 0 === strpos( $file_path, '//' ) ) {
|
2016-11-11 15:31:00 +00:00
|
|
|
return 'absolute';
|
|
|
|
} elseif ( '[' === substr( $file_path, 0, 1 ) && ']' === substr( $file_path, -1 ) ) {
|
|
|
|
return 'shortcode';
|
|
|
|
} else {
|
|
|
|
return 'relative';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get file type.
|
2018-03-22 18:10:22 +00:00
|
|
|
*
|
2016-11-11 15:31:00 +00:00
|
|
|
* @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.
|
2018-03-22 18:10:22 +00:00
|
|
|
*
|
2016-11-11 15:31:00 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_file_extension() {
|
2018-03-22 18:10:22 +00:00
|
|
|
$parsed_url = wp_parse_url( $this->get_file(), PHP_URL_PATH );
|
2016-11-11 15:31:00 +00:00
|
|
|
return pathinfo( $parsed_url, PATHINFO_EXTENSION );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if file is allowed.
|
2018-03-22 18:10:22 +00:00
|
|
|
*
|
2016-11-11 15:31:00 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function is_allowed_filetype() {
|
2017-04-12 15:49:43 +00:00
|
|
|
if ( 'relative' !== $this->get_type_of_file_path() ) {
|
2016-11-11 15:31:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-03-22 18:10:22 +00:00
|
|
|
return ! $this->get_file_extension() || in_array( $this->get_file_type(), $this->get_allowed_mime_types(), true );
|
2016-11-11 15:31:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate file exists.
|
2018-03-22 18:10:22 +00:00
|
|
|
*
|
2016-11-11 15:31:00 +00:00
|
|
|
* @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 );
|
2018-02-14 09:35:16 +00:00
|
|
|
} elseif ( substr( WP_CONTENT_DIR, strlen( untrailingslashit( ABSPATH ) ) ) === substr( $file_url, 0, strlen( substr( WP_CONTENT_DIR, strlen( untrailingslashit( ABSPATH ) ) ) ) ) ) {
|
2017-03-24 11:48:32 +00:00
|
|
|
$file_url = realpath( WP_CONTENT_DIR . substr( $file_url, 11 ) );
|
2016-11-11 15:31:00 +00:00
|
|
|
}
|
|
|
|
return apply_filters( 'woocommerce_downloadable_file_exists', file_exists( $file_url ), $this->get_file() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Setters
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set ID.
|
2018-03-22 18:10:22 +00:00
|
|
|
*
|
|
|
|
* @param string $value Download ID.
|
2016-11-11 15:31:00 +00:00
|
|
|
*/
|
|
|
|
public function set_id( $value ) {
|
|
|
|
$this->data['id'] = wc_clean( $value );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set name.
|
2018-03-22 18:10:22 +00:00
|
|
|
*
|
|
|
|
* @param string $value Download name.
|
2016-11-11 15:31:00 +00:00
|
|
|
*/
|
|
|
|
public function set_name( $value ) {
|
|
|
|
$this->data['name'] = wc_clean( $value );
|
|
|
|
}
|
|
|
|
|
2016-11-18 19:29:37 +00:00
|
|
|
/**
|
|
|
|
* Set previous_hash.
|
2018-03-22 18:10:22 +00:00
|
|
|
*
|
2017-08-05 04:42:31 +00:00
|
|
|
* @deprecated 3.3.0 No longer using filename based hashing to keep track of files.
|
2018-03-22 18:10:22 +00:00
|
|
|
* @param string $value Previous hash.
|
2016-11-18 19:29:37 +00:00
|
|
|
*/
|
|
|
|
public function set_previous_hash( $value ) {
|
2017-08-05 04:42:31 +00:00
|
|
|
wc_deprecated_function( __FUNCTION__, '3.3' );
|
2016-11-18 19:29:37 +00:00
|
|
|
$this->data['previous_hash'] = wc_clean( $value );
|
|
|
|
}
|
|
|
|
|
2016-11-11 15:31:00 +00:00
|
|
|
/**
|
|
|
|
* Set file.
|
2018-03-22 18:10:22 +00:00
|
|
|
*
|
|
|
|
* @param string $value File.
|
2016-11-11 15:31:00 +00:00
|
|
|
*/
|
|
|
|
public function set_file( $value ) {
|
|
|
|
switch ( $this->get_type_of_file_path( $value ) ) {
|
2018-03-22 18:10:22 +00:00
|
|
|
case 'absolute':
|
2016-11-11 15:31:00 +00:00
|
|
|
$this->data['file'] = esc_url_raw( $value );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->data['file'] = wc_clean( $value );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Getters
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get id.
|
2018-03-22 18:10:22 +00:00
|
|
|
*
|
2016-11-11 15:31:00 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_id() {
|
|
|
|
return $this->data['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get name.
|
2018-03-22 18:10:22 +00:00
|
|
|
*
|
2016-11-11 15:31:00 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_name() {
|
|
|
|
return $this->data['name'];
|
|
|
|
}
|
|
|
|
|
2016-11-18 19:29:37 +00:00
|
|
|
/**
|
|
|
|
* Get previous_hash.
|
2018-03-22 18:10:22 +00:00
|
|
|
*
|
2017-08-05 04:42:31 +00:00
|
|
|
* @deprecated 3.3.0 No longer using filename based hashing to keep track of files.
|
2016-11-18 19:29:37 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_previous_hash() {
|
2017-08-05 04:42:31 +00:00
|
|
|
wc_deprecated_function( __FUNCTION__, '3.3' );
|
2016-11-18 19:29:37 +00:00
|
|
|
return $this->data['previous_hash'];
|
|
|
|
}
|
|
|
|
|
2016-11-11 15:31:00 +00:00
|
|
|
/**
|
|
|
|
* Get file.
|
2018-03-22 18:10:22 +00:00
|
|
|
*
|
2016-11-11 15:31:00 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_file() {
|
|
|
|
return $this->data['file'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| ArrayAccess/Backwards compatibility.
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2018-03-22 18:10:22 +00:00
|
|
|
* OffsetGet.
|
|
|
|
*
|
|
|
|
* @param string $offset Offset.
|
2016-11-11 15:31:00 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function offsetGet( $offset ) {
|
|
|
|
switch ( $offset ) {
|
2018-03-22 18:10:22 +00:00
|
|
|
default:
|
2016-11-11 15:31:00 +00:00
|
|
|
if ( is_callable( array( $this, "get_$offset" ) ) ) {
|
|
|
|
return $this->{"get_$offset"}();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-22 18:10:22 +00:00
|
|
|
* OffsetSet.
|
|
|
|
*
|
|
|
|
* @param string $offset Offset.
|
|
|
|
* @param mixed $value Value.
|
2016-11-11 15:31:00 +00:00
|
|
|
*/
|
|
|
|
public function offsetSet( $offset, $value ) {
|
|
|
|
switch ( $offset ) {
|
2018-03-22 18:10:22 +00:00
|
|
|
default:
|
2016-11-11 15:31:00 +00:00
|
|
|
if ( is_callable( array( $this, "set_$offset" ) ) ) {
|
|
|
|
return $this->{"set_$offset"}( $value );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-22 18:10:22 +00:00
|
|
|
* OffsetUnset.
|
|
|
|
*
|
|
|
|
* @param string $offset Offset.
|
2016-11-11 15:31:00 +00:00
|
|
|
*/
|
|
|
|
public function offsetUnset( $offset ) {}
|
|
|
|
|
|
|
|
/**
|
2018-03-22 18:10:22 +00:00
|
|
|
* OffsetExists.
|
|
|
|
*
|
|
|
|
* @param string $offset Offset.
|
2016-11-11 15:31:00 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function offsetExists( $offset ) {
|
2018-03-22 18:10:22 +00:00
|
|
|
return in_array( $offset, array_keys( $this->data ), true );
|
2016-11-11 15:31:00 +00:00
|
|
|
}
|
|
|
|
}
|