Completed
Push — master ( 4fdc7c...c97556 )
by
unknown
07:16
created

display-upgrades.page.php ➔ wpec_license_notices()   C

Complexity

Conditions 11
Paths 14

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 20
c 1
b 0
f 0
nc 14
nop 0
dl 0
loc 33
rs 5.2653

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
function wpsc_display_upgrades_page() {
3
4
	do_action( 'wpsc_upgrades_license_activation' ); ?>
5
6
	<div class='wrap'>
7
		<div class='metabox-holder wpsc_gold_side'>
8
			<div class='metabox-holder'>
9
				<form method='post' id='product_license_box' action=''>
10
					<div class="postbox">
11
						<h3 class="hndle"><?php _e( 'Product License Registration', 'wp-e-commerce' );?></h3>
12
						<p>
13
							<label for="activation_key"><?php _e( 'License Key ', 'wp-e-commerce' ); ?>:</label>
14
							<input type="text" id="activation_key" name="product_license" size="48" value="" class="text" />
15
						</p>
16
						<p>
17
							<input type="hidden" value="true" name="product_license_key" />
18
							<button type="submit" name="submit_values" value="submit_values" class="button button-primary"><?php _e( 'Register License', 'wp-e-commerce' ); ?></button>
19
							<button type="submit" name="reset_values" value="reset_values"><?php _e( 'Reset License', 'wp-e-commerce' ); ?></button>
20
						</p>
21
						<p>
22
							<?php _e( 'In order to receive automatic plugin updates you need to Register your License for each product that provides one. ', 'wp-e-commerce' ); ?></br>
23
							<?php _e( 'Old API Keys will not work! ', 'wp-e-commerce' ); ?>
24
						</p>
25
					</div>
26
				</form>
27
			</div>
28
		</div>
29
	</div>
30
<?php
31
wpse_license_page_display_licenses();
32
}
33
34
function wpse_license_page_display_licenses () {
35
	$licenses = get_option( 'wpec_licenses_active_products', array() );
36
	?>
37
	<table class="wp-list-table widefat striped">
38
			<thead>
39
				<tr>
40
					<th scope="col" id="product_name" class="manage-column column-product_name column-primary"><?php _e('Product Name', 'wp-e-commerce'); ?></th>
41
					<th scope="col" id="product_license" class="manage-column column-product_license"><?php _e('License Key', 'wp-e-commerce'); ?></th>
42
					<th scope="col" id="product_expiry" class="manage-column column-product_expiry"><?php _e('License Expiration', 'wp-e-commerce'); ?></th>
43
				</tr>
44
			</thead>
45
			<tbody id="the-list">
46
	<?php
47
	if ( ! empty( $licenses ) ) {
48
		foreach ( (array) $licenses as $license ) { $license_info = get_option( 'wpec_product_' . $license . '_license_active', array() ); ?>
49
			<?php do_action( 'wpec_license_individual_license', $license_info ); ?>
50
			<tr><td class="product_name column-product_name"><p><strong><?php echo esc_html( $license_info->item_name ); ?></strong></p></td>
51
			<td class="product_license column-product_license"><p><strong><?php echo esc_html( $license_info->license_key ); ?></strong></p></td>
52
			<td class="product_expiry column-product_expiry"><p><strong><?php if ( $license_info->expiration == 'lifetime' ) { _e('Lifetime', 'wp-e-commerce'); } else { echo esc_html( $license_info->expiration ); } ?></strong></p></td></tr>
53
		<?php }
54
	} else {
55
		?>
56
		<tr class="no-items"><td class="colspanchange" colspan="4"><p><?php _e('No Licenses found.', 'wp-e-commerce'); ?></p></td></tr>
57
	<?php
58
	}
59
	?>
60
	</tbody>
61
		<tfoot>
62
			<tr>
63
				<th scope="col" class="manage-column column-product_name column-primary"><?php _e('Product Name', 'wp-e-commerce'); ?></th>
64
				<th scope="col" class="manage-column column-product_license"><?php _e('License Key', 'wp-e-commerce'); ?></th>
65
				<th scope="col" class="manage-column column-product_expiry"><?php _e('License Expiration', 'wp-e-commerce'); ?></th>
66
			</tr>
67
		</tfoot>
68
		</table>
69
		<?php
70
}
71
72
/**
73
 * Activate Gold Cart plugin
74
 */
75
function wpec_licenses_action_stuff() {
76
	//Activate a new Product License
77
	
78
	if( ! current_user_can( 'manage_options' ) ) {
79
		return;
80
	}
81
	
82
	if ( isset( $_POST['product_license_key'] ) && $_POST['product_license_key'] == 'true' ) {
83
		if ( isset( $_POST['product_license'] ) && $_POST['product_license'] != '' ) {
84
			
85
			//Do stuff
86
			$params = array (
0 ignored issues
show
introduced by
There must be no space between the Array keyword and the opening parenthesis
Loading history...
87
				'license'   			=> sanitize_text_field( $_POST['product_license'] ),
88
				'url'        			=> home_url()
89
			);
90
			
91
			$activation = false;
92
			if ( isset( $_POST['submit_values'] ) && $_POST['submit_values'] == 'submit_values' ) {
93
				$activation = true;
94
				$params['wpec_lic_action'] = 'activate_license';
95
			} elseif ( isset( $_POST['reset_values'] ) && $_POST['reset_values'] == 'reset_values' ) {
96
				$params['wpec_lic_action'] = 'deactivate_license';
97
			}
98
			
99
			$response = wp_remote_post(
100
				'https://wpecommerce.org/',
101
				array(
102
					'timeout'   => 15,
103
					'sslverify' => false,
104
					'body'      => $params
105
				)
106
			);
107
			
108
			// Make sure there are no errors
109
			if ( is_wp_error( $response ) ) {
110
				return;
111
			}
112
			
113
			// Decode license data
114
			$license_data = json_decode( wp_remote_retrieve_body( $response ) );
115
			$active_licenses = get_option( 'wpec_licenses_active_products', array() );
116
117
			if( $license_data->success === true ) {
118
				if ( $activation ) {
119
					// Tell WordPress to look for updates
120
					set_site_transient( 'update_plugins', null );
121
					$active_licenses[] = $license_data->item_id;
122
					update_option( 'wpec_licenses_active_products', $active_licenses );
123
					update_option( 'wpec_product_' . $license_data->item_id . '_license_active', $license_data );
124
				} else {
125
					$key = array_search( $license_data->item_id, $active_licenses );
126
					unset( $active_licenses[ $key ] );
127
					update_option( 'wpec_licenses_active_products', $active_licenses );
128
					delete_option( 'wpec_product_' . $license_data->item_id . '_license_active' );
129
				}
130
				
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
131
132
				echo '<div class="updated"><p>'.esc_html( $license_data->message ).'</p></div>';
133
			} else {
134
				echo '<div class="error"><p>'.esc_html( $license_data->message ).'</p></div>';
135
			}
136
		}
137
	}
138
}
139
add_action( 'wpsc_upgrades_license_activation', 'wpec_licenses_action_stuff' );
140
141
function wpec_lic_weekly_license_check() {
142
	
143
		if( ! empty( $_POST['product_license_key'] ) ) {
144
			return; // Don't fire when saving settings
145
		}
146
147
		$active_licenses = get_option( 'wpec_licenses_active_products', array() );
148
		if( empty( $active_licenses ) ) {
149
			return;
150
		}
151
		
152
		foreach ( (array) $active_licenses as $license ) {
153
			$license_info = get_option( 'wpec_product_' . $license . '_license_active' );
154
			
155
			// data to send in our API request
156
			$api_params = array(
157
				'wpec_lic_action'=> 'check_license',
158
				'license' 	=> $license_info->license_key,
159
				'item_id' 	=> $license_info->item_id,
160
				'url'       => home_url()
161
			);
162
163
			// Call the API
164
			$response = wp_remote_post(
165
				'https://wpecommerce.org/',
166
				array(
167
					'timeout'   => 15,
168
					'sslverify' => false,
169
					'body'      => $api_params
170
				)
171
			);
172
173
			// make sure the response came back okay
174
			if ( is_wp_error( $response ) ) {
175
				return false;
176
			}
177
178
			$license_data = json_decode( wp_remote_retrieve_body( $response ) );
179
			update_option( 'wpec_product_' . $license_info->item_id . '_license_active', $license_data );	
180
		}
181
}
182
add_action( 'wpsc_weekly_cron_task', 'wpec_lic_weekly_license_check' ); // For testing use admin_init
183
184
function wpec_license_notices() {
185
		if( ! current_user_can( 'manage_options' ) ) {
186
			return;
187
		}
188
		
189
		$active_licenses = get_option( 'wpec_licenses_active_products', array() );
190
		if( empty( $active_licenses ) ) {
191
			return;
192
		}
193
		
194
		$messages = array();
195
		
196
		foreach ( (array) $active_licenses as $license ) {
197
			$license = get_option( 'wpec_product_' . $license . '_license_active' );
198
			if( is_object( $license ) && 'valid' !== $license->license && empty( $showed_invalid_message ) ) {
199
				if( isset( $_GET['page'] ) && 'wpsc-upgrades' !== $_GET['page'] ) {
200
					$messages[] = sprintf(
201
						__( 'You have invalid or expired license keys for WP eCommerce. Please go to the <a href="%s" title="Go to Licenses page">Licenses page</a> to correct this issue.', 'wp-e-commerce' ),
202
						admin_url( 'index.php?page=wpsc-upgrades' )
203
					);
204
					$showed_invalid_message = true;
205
				}
206
			}		
207
		}
208
209
		if( ! empty( $messages ) ) {
210
			foreach( $messages as $message ) {
211
				echo '<div class="error">';
212
					echo '<p>' . $message . '</p>';
213
				echo '</div>';
214
			}
215
		}	
216
}
217
add_action( 'admin_notices', 'wpec_license_notices' );
218