1
|
|
|
<?php if ( !defined( 'ABSPATH' ) ) exit; |
2
|
|
|
|
3
|
|
|
class wps_download_file_ctr { |
4
|
|
|
public function __construct() { |
5
|
|
|
add_action( 'admin_post_wps_download_file', array( $this, 'wps_download_file' ) ); |
6
|
|
|
add_action( 'admin_post_nopriv_wps_download_file', array( $this, 'wps_download_file' ) ); |
7
|
|
|
} |
8
|
|
|
|
9
|
|
|
// Function to find download link by |
10
|
|
|
public static function get_product_download_link( $oid, $item ) { |
11
|
|
|
$parent_def = array(); |
12
|
|
|
$item_id = $item['item_id']; |
13
|
|
|
$item_post_type = get_post_type( $item['item_id'] ); |
14
|
|
|
if ( WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION === $item_post_type ) { |
15
|
|
|
$parent_def = wpshop_products::get_parent_variation( $item['item_id'] ); |
16
|
|
|
if ( ! empty( $parent_def ) && ! empty( $parent_def['parent_post'] ) ) { |
17
|
|
|
$parent_post = $parent_def['parent_post']; |
18
|
|
|
$item_id = $parent_post->ID; |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
/** Downloadable link in Order recap */ |
22
|
|
|
$download_link = false; |
23
|
|
|
$item_id_for_download = null; |
24
|
|
|
/** Check if the product or the head product is a download product */ |
25
|
|
|
if ( ! empty( $parent_def ) ) { |
26
|
|
|
$parent_meta = $parent_def['parent_post_meta']; |
27
|
|
View Code Duplication |
if ( ! empty( $parent_meta['is_downloadable_'] ) ) { |
|
|
|
|
28
|
|
|
$downloadable_option_value = $wpdb->get_var( $wpdb->prepare( 'SELECT value FROM ' . WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS . ' WHERE id = %d', $parent_meta['is_downloadable_'] ) ); |
|
|
|
|
29
|
|
|
if ( ! empty( $downloadable_option_value ) ) { |
30
|
|
|
$item['item_is_downloadable_'] = $downloadable_option_value; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
if ( ! empty( $item ) && ! empty( $item['item_is_downloadable_'] ) && ( strtolower( __( $item['item_is_downloadable_'], 'wpshop' ) ) === strtolower( __( 'Yes', 'wpshop' ) ) ) ) { |
35
|
|
|
$item_id_for_download = $item_id; |
36
|
|
View Code Duplication |
if ( isset( $item['item_meta']['variations'] ) ) { |
|
|
|
|
37
|
|
|
foreach ( $item['item_meta']['variations'] as $variation_id => $variation ) { |
38
|
|
|
if ( isset( $variation['item_meta']['is_downloadable_'] ) ) { |
39
|
|
|
$item_id_for_download = $item_id . '__' . $variation_id; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** In case there is a item identifier defined for download */ |
46
|
|
|
if ( null !== $item_id_for_download ) { |
47
|
|
|
$download_codes = get_user_meta( get_current_user_id(), '_order_download_codes_' . $oid, true ); |
48
|
|
|
/** Check if the current product exist into download code list, if not check if there is a composition between parent product and children product */ |
49
|
|
|
if ( empty( $download_codes[ $item_id_for_download ] ) ) { |
50
|
|
|
$item_id_component = explode( '__', $item_id_for_download ); |
51
|
|
|
if ( ! empty( $item_id_component ) && ( $item_id_component[0] !== $item_id_for_download ) ) { |
52
|
|
|
$item_id_for_download = $item_id_component[0]; |
53
|
|
|
} elseif ( ! empty( $download_codes[ $item['item_id'] ] ) ) { |
54
|
|
|
$item_id_for_download = $item['item_id']; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ( ! empty( $download_codes ) && ! empty( $download_codes[ $item_id_for_download ] ) && ! empty( $download_codes[ $item_id_for_download ]['download_code'] ) ) { |
59
|
|
|
$download_link = admin_url( 'admin-post.php?action=wps_download_file&oid=' . $oid . '&download=' . $download_codes[ $item_id_for_download ]['download_code'] ); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
return $download_link; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Download product downloadable |
66
|
|
|
public function wps_download_file() { |
67
|
|
|
$download = !empty( $_GET['download'] ) ? sanitize_text_field( $_GET['download'] ) : ''; |
68
|
|
|
$oid = !empty( $_GET['oid'] ) ? (int) $_GET['oid'] : 0; |
69
|
|
|
|
70
|
|
|
if ( !empty( $download ) && !empty( $oid ) ) { |
71
|
|
|
$variation_id = ''; |
72
|
|
|
$order = get_post_meta($oid, '_order_postmeta', true); |
73
|
|
|
if(!empty($order) && !empty( $order['customer_id'] )) { |
74
|
|
|
$download_codes = get_user_meta( /*wps_customer_ctr::get_author_id_by_customer_id(*/ $order['customer_id'] /*)*/, '_order_download_codes_'.$oid, true); |
75
|
|
|
if ( !empty($download_codes) && is_array($download_codes) ) { |
76
|
|
|
foreach ( $download_codes as $downloadable_product_id => $d ) { |
77
|
|
|
$is_encrypted = false; |
78
|
|
|
if ( $d['download_code'] == $download ) { |
79
|
|
|
wpshop_tools::create_custom_hook ('encrypt_actions_for_downloadable_product', array( 'order_id' => $oid, 'download_product_id' => $downloadable_product_id ) ); |
80
|
|
|
|
81
|
|
|
if ( get_post_type( $downloadable_product_id ) == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION ) { |
82
|
|
|
$parent_def = wpshop_products::get_parent_variation( $downloadable_product_id ); |
83
|
|
|
if ( !empty($parent_def) && !empty($parent_def['parent_post']) ) { |
84
|
|
|
$parent_post = $parent_def['parent_post']; |
85
|
|
|
$variation_id = $downloadable_product_id; |
86
|
|
|
$downloadable_product_id = $parent_post->ID; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} else { |
90
|
|
|
$downloadable_product_id = explode( "__", $downloadable_product_id ); |
91
|
|
|
$downloadable_product_id = isset( $downloadable_product_id[1] ) ? $downloadable_product_id[1] : $downloadable_product_id[0]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$link = wpshop_attributes::get_attribute_option_output( |
95
|
|
|
array('item_id' => $downloadable_product_id, 'item_is_downloadable_'=>'yes'), |
96
|
|
|
'is_downloadable_', 'file_url', $order |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
if ( $link !== false ) { |
100
|
|
|
$uploads = wp_upload_dir(); |
101
|
|
|
$basedir = $uploads['basedir']; |
102
|
|
|
$pos = strpos($link, 'uploads'); |
103
|
|
|
$link = $basedir.substr($link,$pos+7); |
104
|
|
|
/** If plugin is encrypted **/ |
105
|
|
|
$encrypted_plugin_path = get_post_meta( $oid, '_download_file_path_'.$oid.'_'.( ( !empty( $variation_id ) && get_post_type( $variation_id ) == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION ) ? $variation_id : $downloadable_product_id ), true); |
106
|
|
|
if ( !empty($encrypted_plugin_path) ) { |
107
|
|
|
$link = WPSHOP_UPLOAD_DIR.$encrypted_plugin_path; |
108
|
|
|
$is_encrypted = true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$overload_force_download = apply_filters( 'wps_download_file_overload_force_download', false ); |
112
|
|
|
if ( !$overload_force_download ) { |
113
|
|
|
wpshop_tools::forceDownload($link, $is_encrypted); |
114
|
|
|
} |
115
|
|
|
else { |
116
|
|
|
wpshop_tools::wpshop_safe_redirect( str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $link ) ); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
else { |
124
|
|
|
wp_redirect( get_permalink( wpshop_tools::get_page_id( get_option('wpshop_myaccount_page_id') ) ) ); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
echo __('Impossible to download the file you requested', 'wpshop'); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.