Completed
Push — master ( 5c12d6...edf3a9 )
by
unknown
13:25
created

wps_download_file_ctr   D

Complexity

Total Complexity 59

Size/Duplication

Total Lines 194
Duplicated Lines 3.61 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 7
loc 194
rs 4.5454
c 0
b 0
f 0
wmc 59
lcom 0
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
F get_product_download_link() 7 71 22
C wps_download_file() 0 67 20
F wps_after_check_order_payment_total_amount() 0 45 16

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like wps_download_file_ctr often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use wps_download_file_ctr, and based on these observations, apply Extract Interface, too.

1
<?php if ( ! defined( 'ABSPATH' ) ) {
2
	exit;
3
}
4
5
class wps_download_file_ctr {
6
7
	public function __construct() {
8
		add_action( 'admin_post_wps_download_file', array( $this, 'wps_download_file' ) );
9
		add_action( 'admin_post_nopriv_wps_download_file', array( $this, 'wps_download_file' ) );
10
		add_action( 'wps_after_check_order_payment_total_amount', array( $this, 'wps_after_check_order_payment_total_amount' ) );
11
	}
12
13
	public static function get_product_download_link( $oid, $item ) {
14
		global $wpdb;
15
16
		$parent_def = array();
17
		$item_id = $item['item_id'];
18
		$item_post_type = get_post_type( $item['item_id'] );
19
		if ( WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION === $item_post_type ) {
20
			$parent_def = wpshop_products::get_parent_variation( $item['item_id'] );
21
			if ( ! empty( $parent_def ) && ! empty( $parent_def['parent_post'] ) ) {
22
				$parent_post = $parent_def['parent_post'];
23
				$item_id = $parent_post->ID;
24
				$item_title = $parent_post->post_title;
0 ignored issues
show
Unused Code introduced by
$item_title is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
25
			}
26
		}
27
28
		$download_link = false;
29
		$item_id_for_download = null;
30
		/** Check if the product or the head product is a download product  */
31
		if ( ! empty( $parent_def ) ) {
32
			$parent_meta = $parent_def['parent_post_meta'];
33
			if ( ! empty( $parent_meta['is_downloadable_'] ) ) {
34
				$query = $wpdb->prepare( 'SELECT value FROM ' . WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS . ' WHERE id = %d', $parent_meta['is_downloadable_'] );
35
				$downloadable_option_value = $wpdb->get_var( $query );
36
				if ( empty( $downloadable_option_value ) ) {
37
					$item['item_is_downloadable_'] = 'No';
38
				} else {
39
					$item['item_is_downloadable_'] = $downloadable_option_value;
40
				}
41
			}
42
		}
43
		if ( empty( $item['item_is_downloadable_'] ) ) {
44
			$product_data = wpshop_products::get_product_data( $item['item_id'] );
45
			if ( empty( $product_data['is_downloadable_'] ) ) {
46
				$item['item_is_downloadable_'] = 'No';
47
			} else {
48
				$item['item_is_downloadable_'] = $product_data['is_downloadable_'];
49
			}
50
		}
51
		if ( strtolower( __( $item['item_is_downloadable_'], 'wpshop' ) ) === strtolower( __( 'Yes', 'wpshop' ) ) ) {
52
			$item_id_for_download = $item_id;
53
		}
54 View Code Duplication
		if ( isset( $item['item_meta']['variations'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
55
			foreach ( $item['item_meta']['variations'] as $variation_id => $variation ) {
56
				if ( isset( $variation['item_meta']['is_downloadable_'] ) ) {
57
					$item_id_for_download = $item_id . '__' . $variation_id;
58
				}
59
			}
60
		}
61
		/** In case there is a item identifier defined for download */
62
		if ( null !== $item_id_for_download ) {
63
			$cid = (int) get_post_meta( $oid, '_wpshop_order_customer_id', true );
64
			$cid = ! empty( $cid ) ? $cid : get_current_user_id();
65
			$download_codes = get_user_meta( $cid, '_order_download_codes_' . $oid, true );
66
			/** Check if the current product exist into download code list, if not check if there is a composition between parent product and children product  */
67
			if ( empty( $download_codes[ $item_id_for_download ] ) ) {
68
				$item_id_component = explode( '__', $item_id_for_download );
69
				if ( ! empty( $item_id_component ) && ( $item_id_component[0] !== $item_id_for_download ) ) {
70
					$item_id_for_download = $item_id_component[0];
71
				} elseif ( ! empty( $download_codes[ $item['item_id'] ] ) ) {
72
					$item_id_for_download = $item['item_id'];
73
				}
74
			}
75
			//var_dump($download_codes);
76
			if ( ! empty( $download_codes ) && ! empty( $download_codes[ $item_id_for_download ] ) && ! empty( $download_codes[ $item_id_for_download ]['download_code'] ) ) {
77
				$download_link = admin_url( 'admin-post.php?action=wps_download_file&amp;oid=' . $oid . '&amp;download=' . $download_codes[ $item_id_for_download ]['download_code'] );
78
			}
79
		}
80
		//exit();
81
82
		return $download_link;
83
	}
84
85
	public function wps_download_file() {
86
		$download = ! empty( $_GET['download'] ) ? sanitize_text_field( $_GET['download'] ) : '';
87
		$oid = ! empty( $_GET['oid'] ) ? (int) $_GET['oid'] : 0;
88
89
		if ( ! empty( $download ) && ! empty( $oid ) ) {
90
			$variation_id = '';
91
			$order = get_post_meta( $oid, '_order_postmeta', true );
92
			if ( ! empty( $order ) && ! empty( $order['customer_id'] ) ) {
93
				$download_codes = get_user_meta( $order['customer_id'], '_order_download_codes_' . $oid, true );
94
				if ( ! empty( $download_codes ) && is_array( $download_codes ) ) {
95
					foreach ( $download_codes as $downloadable_product_id => $d ) {
96
						$is_encrypted = false;
97
						if ( $d['download_code'] === $download ) {
98
							 wpshop_tools::create_custom_hook('encrypt_actions_for_downloadable_product', array(
99
								 'order_id' => $oid,
100
								 'download_product_id' => $downloadable_product_id,
101
102
							 ) );
103
104
							if ( get_post_type( $downloadable_product_id ) === WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION ) {
105
								$parent_def = wpshop_products::get_parent_variation( $downloadable_product_id );
106
								if ( ! empty( $parent_def ) && ! empty( $parent_def['parent_post'] ) ) {
107
									$parent_post = $parent_def['parent_post'];
108
									$variation_id = $downloadable_product_id;
109
									$downloadable_product_id = $parent_post->ID;
110
								}
111
							} else {
112
								  $downloadable_product_id = explode( '__', $downloadable_product_id );
113
								 $downloadable_product_id = isset( $downloadable_product_id[1] ) ? $downloadable_product_id[1] : $downloadable_product_id[0];
114
							}
115
116
							$link = wpshop_attributes::get_attribute_option_output(
117
								array(
118
									'item_id' => $downloadable_product_id,
119
									'item_is_downloadable_' => 'yes',
120
								),
121
								'is_downloadable_', 'file_url', $order
122
							);
123
124
							if ( false !== $link ) {
125
								$uploads = wp_upload_dir();
126
								$basedir = $uploads['basedir'];
127
								$pos = strpos( $link, 'uploads' );
128
								$link = $basedir . substr( $link,$pos + 7 );
129
								/** If plugin is encrypted */
130
								$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 );
131
								if ( ! empty( $encrypted_plugin_path ) ) {
132
									$link = WPSHOP_UPLOAD_DIR . $encrypted_plugin_path;
133
									$is_encrypted = true;
134
								}
135
136
								$overload_force_download = apply_filters( 'wps_download_file_overload_force_download', false );
137
								if ( ! $overload_force_download ) {
138
									wpshop_tools::forceDownload( $link, $is_encrypted );
139
								} else {
140
									wpshop_tools::wpshop_safe_redirect( str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $link ) );
141
								}
142
							}
143
						}// End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
144
					}// End foreach().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
145
				}// End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
146
			} else {
147
				wp_redirect( get_permalink( wpshop_tools::get_page_id( get_option( 'wpshop_myaccount_page_id' ) ) ) );
148
			}// End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
149
		}// End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
150
		esc_html_e( 'Impossible to download the file you requested', 'wpshop' );
151
	}
152
153
	public function wps_after_check_order_payment_total_amount( $order_id ) {
154
		$order_meta = ( ! empty( $order_meta ) ) ? false : get_post_meta( $order_id, '_order_postmeta', true );
0 ignored issues
show
Bug introduced by
The variable $order_meta seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
155
		/** Check if the order content a downloadable product **/
156
		if ( ! empty( $order_meta ) && ! empty( $order_meta['order_items'] ) && ! empty( $order_meta['order_status'] ) && 'completed' === $order_meta['order_status'] ) {
157
			foreach ( $order_meta['order_items'] as $key_value => $item ) {
158
				$link = self::get_product_download_link( $order_id, $item );
159
				if ( false === $link ) {
160
					\eoxia\log_class::exec(
161
						get_class(),
162
						get_class(),
163
						sprintf(
164
							__( 'Failure returned at order link generation. UserID : <b>%1$d</b>, ProductID : <b>%2$d</d>, UserMeta : <pre>%3$s</pre>', 'wpshop' ),
165
							(int) get_current_user_id(),
166
							(int) $item['item_id'],
167
							get_user_meta( (int) $order_meta['customer_id'], '_order_download_codes_' . $order_id, true )
168
						),
169
						array(
170
							'object_id' => $order_id,
171
						),
172
						0
173
					);
174
				} else {
175
					$user_data = get_userdata( $order_meta['customer_id'] );
176
					$order_info = get_post_meta( $order_id, '_order_info', true );
177
					$email = ( ! empty( $user_data ) && ! empty( $user_data->user_email ) ) ? $user_data->user_email : '';
178
					$first_name = ( ! empty( $order_info ) && ! empty( $order_info['billing'] ) && ! empty( $order_info['billing']['address']['address_first_name'] ) ? $order_info['billing']['address']['address_first_name'] : '' );
179
					$last_name = ( ! empty( $order_info ) && ! empty( $order_info['billing'] ) && ! empty( $order_info['billing']['address']['address_last_name'] ) ? $order_info['billing']['address']['address_last_name'] : '' );
180
					$link = '<a href="' . esc_url( $link ) . '" target="_blank">' . __( 'Download the product', 'wpshop' ) . '</a>';
181
					$wps_message = new wps_message_ctr();
182
					$wps_message->wpshop_prepared_email(
183
						$email,
184
						'WPSHOP_DOWNLOADABLE_FILE_IS_AVAILABLE',
185
						array(
186
							'order_key' => $order_meta['order_key'],
187
							'customer_first_name' => $first_name,
188
							'customer_last_name' => $last_name,
189
							'order_date' => $order_meta['order_date'],
190
							'download_product_link' => $link,
191
						),
192
						array()
193
					);
194
				}
195
			}
196
		}
197
	}
198
}
199