Completed
Push — master ( 9a3ee6...bca146 )
by Justin
05:42
created

cron.php ➔ wpsc_lic_weekly_license_check()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 21
nc 4
nop 0
dl 0
loc 38
rs 8.5806
c 0
b 0
f 0
1
<?php
2
add_action( 'wpsc_hourly_cron_task', 'wpsc_clear_stock_claims' );
3
add_action( 'wpsc_hourly_cron_task', '_wpsc_delete_expired_visitors' );
4
add_action( 'wpsc_weekly_cron_task', 'wpsc_lic_weekly_license_check' );
5
6
/** 
7
 * Checks any active Addons license keys and updates license data
8
 *
9
 * @since 3.12.0
10
 */
11
function wpsc_lic_weekly_license_check() {
12
13
	$active_licenses = get_option( 'wpec_licenses_active_products', array() );
14
	
15
	if ( empty( $active_licenses ) ) {
16
		return;
17
	}
18
19
	foreach ( (array) $active_licenses as $license ) {
20
		$license_info = get_option( 'wpec_product_' . $license . '_license_active' );
21
22
		// data to send in our API request
23
		$api_params = array(
24
			'wpec_lic_action'=> 'check_license',
25
			'license' 	=> $license_info->license_key,
26
			'item_id' 	=> $license_info->item_id,
27
			'url'       => home_url()
28
		);
29
30
		// Call the API
31
		$response = wp_safe_remote_post(
32
			'https://wpecommerce.org/',
33
			array(
34
				'timeout'   => 15,
35
				'sslverify' => false,
36
				'body'      => $api_params
37
			)
38
		);
39
40
		// make sure the response came back okay
41
		if ( is_wp_error( $response ) ) {
42
			return false;
43
		}
44
45
		$license_data = json_decode( wp_remote_retrieve_body( $response ) );
46
		update_option( 'wpec_product_' . $license . '_license_active', $license_data );
47
	}
48
}
49
50
function wpsc_add_tracking_cron() {
51
    $tracking = new WPSC_Tracking();
52
    $tracking->send_data();
53
}
54
add_action( 'wpsc_weekly_cron_task', 'wpsc_add_tracking_cron' );
55
56
/**
57
 * wpsc_clear_stock_claims, clears the stock claims, runs using wp-cron and when editing purchase log statuses via the dashboard
58
 */
59
function wpsc_clear_stock_claims() {
60
	global $wpdb;
61
62
	$time = (float) get_option( 'wpsc_stock_keeping_time', 1 );
63
	$interval = get_option( 'wpsc_stock_keeping_interval', 'day' );
64
65
	// we need to convert into seconds because we're allowing decimal intervals like 1.5 days
66
	$convert = array(
67
		'hour' => 3600,
68
		'day'  => 86400,
69
		'week' => 604800,
70
	);
71
72
	$seconds = floor( $time * $convert[ $interval ] );
73
74
	$sql = $wpdb->prepare( 'DELETE FROM ' . WPSC_TABLE_CLAIMED_STOCK . ' WHERE last_activity < UTC_TIMESTAMP() - INTERVAL %d SECOND', $seconds );
75
	$wpdb->query( $sql );
76
}
77
78
79
/** Start the process that cleans up user profiles
80
 *
81
 * Request is made through AJAX to ensure all of the WordPress admin functionality  is loaded.  Necessary because there is
82
 * delete logic that is admin only.  It is also possible that a plugin may have filters that need to run when users are deleted.
83
 * @access private
84
 * @since 3.8.14
85
 *
86
 * @return how many expired visitors remain to be deleted, 0 if all done
87
 */
88
function _wpsc_delete_expired_visitors() {
89
90
	if ( ! defined( 'WPSC_MAX_DELETE_PROFILE_TIME' ) ) {
91
		define( 'WPSC_MAX_DELETE_PROFILE_TIME', 20 );
92
	}
93
94
	if ( ! defined( 'WPSC_MAX_DELETE_MEMORY_USAGE' ) ) {
95
		define( 'WPSC_MAX_DELETE_MEMORY_USAGE',  20 * 1024 * 1024 ); // allow up to 20 megabytes to be consumed by the delete processing
96
	}
97
98
	// We are going to record a little option so that support can confirm that the delete users cron is running
99
	add_option( '_wpsc_last_delete_expired_visitors_cron', date( 'Y-m-d H:i:s' ), null, 'no' );
100
101
	$expired_visitor_ids = wpsc_get_expired_visitor_ids();
102
103
	$a_little_bit_of_time_after_start = time() + WPSC_MAX_DELETE_PROFILE_TIME;
104
	$too_much_memory_is_being_used = memory_get_usage( true ) + WPSC_MAX_DELETE_MEMORY_USAGE;
105
106
	// For each of the ids double check to be sure there isn't any important data associated with the temporary user.
107
	// If important data is found the user is no longer temporary. We also use a filter so that if other plug-ins
108
	// want to either stop the user from being deleted, or do something with the information in the profile they
109
	// have that chance.
110
	foreach ( $expired_visitor_ids as $index => $expired_visitor_id ) {
111
		wpsc_do_delete_visitor_ajax( $expired_visitor_id );
112
113
		unset( $expired_visitor_ids[$index] );
114
115
		// in case we have a lot of users to delete we do some checking to make sure we don't
116
		// get caught in a loop using server resources for an extended period of time without yielding.
117
		// Different environments will be able to delete a different number of users in the allowed time,
118
		// that's the reason for the defined variable
119
		if ( (time() > $a_little_bit_of_time_after_start) || ( memory_get_usage( true ) > $too_much_memory_is_being_used ) ) {
120
			// next delete processing will happen no sooner than in a couple minutes, but as the time allowed for
121
			// delete processing increases the interval between cycles will also extend.
122
			wp_schedule_single_event( time() + ( 120 + 2 * WPSC_MAX_DELETE_PROFILE_TIME ), '_wpsc_delete_expired_visitors_action' );
123
			break;
124
		}
125
	}
126
127
	// Since there are no visitors to delete this is a good time to cleanup the visitors meta table
128
	// eliminating any orphaned meta data, asingle SQL query will do it!
129
	if ( ! count( $expired_visitor_ids ) ) {
130
		global $wpdb;
131
		$sql = 'DELETE vm FROM ' . $wpdb->wpsc_visitormeta . ' vm LEFT JOIN ' . $wpdb->wpsc_visitors . ' v  on v.id = vm.wpsc_visitor_id WHERE v.id IS NULL';
132
		$wpdb->query( $sql );
133
	}
134
135
	return count( $expired_visitor_ids );
136
}
137
138
139
/**
140
 * Request a visitor be deleted via the WordPRess admin ajax path
141
 *
142
 * @access private
143
 * @since 3.8.14
144
 *
145
 * @param int $visitor_id
146
 *
147
 * @return boolean, true on success, false on failure
0 ignored issues
show
Documentation introduced by
The doc-type boolean, could not be parsed: Expected "|" or "end of type", but got "," at position 7. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
148
 *
149
 */
150
function wpsc_do_delete_visitor_ajax( $visitor_id ) {
151
152
	$delete_visitor_nonce_action = 'wpsc_delete_visitor_id_' .  $visitor_id;
153
154
	$wpsc_security = wp_create_nonce( $delete_visitor_nonce_action );
155
156
	$response = wp_safe_remote_post(
157
									admin_url( 'admin-ajax.php' ),
158
									array(
159
										'method'      => 'POST',
160
										'timeout'     => 15,
161
										'redirection' => 5,
162
										'httpversion' => '1.0',
163
										'blocking'    => true,
164
										'headers'     => array(),
165
										'body'        => array( 'action' => 'wpsc_delete_visitor', 'wpsc_visitor_id' => $visitor_id, 'wpsc_security' => $wpsc_security, ),
166
										'cookies'     => array(),
167
									)
168
								);
169
170
	if ( is_wp_error( $response ) ) {
171
		$result = false;
172
	} else {
173
		$result = true;
174
	}
175
176
	return $result;
177
}
178
179
180
// add admin action for convenience
181
add_action( '_wpsc_delete_expired_visitors_action' , '_wpsc_delete_expired_visitors' );
182