GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#307)
by Chris
16:52
created

upgrades.php ➔ ppp_fix_scheduled_shares_2319()   F

Complexity

Conditions 14
Paths 528

Size

Total Lines 82
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 48
nc 528
nop 0
dl 0
loc 82
rs 2.8788
c 0
b 0
f 0

How to fix   Long Method    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
3
// Exit if accessed directly
4
if ( ! defined( 'ABSPATH' ) ) {
5
	exit;
6
}
7
8
function ppp_upgrade_notices() {
9
10
	if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'ppp-about' || $_GET['page'] == 'ppp-upgrades' ) ) {
11
		return; // Don't show notices on the upgrades page
12
	}
13
14
	$ppp_version = get_option( 'ppp_version' );
15
16
	// Sequential Orders was the first stepped upgrade, so check if we have a stalled upgrade
17
	$resume_upgrade = ppp_maybe_resume_upgrade();
18
	if ( ! empty( $resume_upgrade ) ) {
19
20
		$resume_url = add_query_arg( $resume_upgrade, admin_url( 'index.php' ) );
21
		printf(
22
			'<div class="error"><p>' . __( 'Post Promoter Pro needs to complete a database upgrade that was previously started, click <a href="%s">here</a> to resume the upgrade.', 'ppp-txt' ) . '</p></div>',
23
			esc_url( $resume_url )
24
		);
25
26
	} else {
27
28
		// Include all 'Stepped' upgrade process notices in this else statement,
29
		// to avoid having a pending, and new upgrade suggested at the same time
30
31
		if ( version_compare( $ppp_version, '2.2', '<' ) || ! ppp_has_upgrade_completed( 'upgrade_post_meta' ) ) {
32
			printf(
33
				'<div class="notice notice-info"><p>' . __( 'Post Promoter Pro needs to upgrade share override data, click <a href="%s">here</a> to start the upgrade.', 'ppp-txt' ) . '</p></div>',
34
				esc_url( admin_url( 'index.php?page=ppp-upgrades&ppp-upgrade=upgrade_post_meta' ) )
35
			);
36
		}
37
38
		if ( version_compare( $ppp_version, '2.3.19', '<' ) || ! ppp_has_upgrade_completed( 'fix_scheduled_shares_2319' ) ) {
39
			printf(
40
				'<div class="notice notice-info"><p>' . __( 'Post Promoter Pro needs to fix an issue with scheduled shares, click <a href="%s">here</a> to start the upgrade.', 'ppp-txt' ) . '</p></div>',
41
				esc_url( admin_url( 'index.php?page=ppp-upgrades&ppp-upgrade=fix_scheduled_shares_2319' ) )
42
			);
43
		}
44
45
		// End 'Stepped' upgrade process notices
46
47
	}
48
49
}
50
add_action( 'admin_notices', 'ppp_upgrade_notices' );
51
52
/**
53
 * Run any upgrade routines needed depending on versions
54
 * @return void
55
 */
56
function ppp_upgrade_plugin() {
57
	$ppp_version = get_option( 'ppp_version' );
58
59
	$upgrades_executed = false;
60
61
	// We don't have a version yet, so we need to run the upgrader
62
	if ( !$ppp_version && PPP_VERSION == '1.3' ) {
63
		ppp_v13_upgrades();
64
		$upgrades_executed = true;
65
	}
66
67
	if ( version_compare( $ppp_version, 2.1, '<' ) ) {
68
		ppp_v21_upgrades();
69
		$upgrades_executed = true;
70
	}
71
72
	if ( $upgrades_executed || version_compare( $ppp_version, PPP_VERSION, '<' ) ) {
73
		set_transient( '_ppp_activation_redirect', '1', 60 );
74
		update_option( 'ppp_version', PPP_VERSION );
75
	}
76
77
}
78
79
/** Helper Functions **/
80
81
/**
82
 * For use when doing 'stepped' upgrade routines, to see if we need to start somewhere in the middle
83
 * @since 2.2.6
84
 * @return mixed   When nothing to resume returns false, otherwise starts the upgrade where it left off
85
 */
86
function ppp_maybe_resume_upgrade() {
87
88
	$doing_upgrade = get_option( 'ppp_doing_upgrade', false );
89
90
	if ( empty( $doing_upgrade ) ) {
91
		return false;
92
	}
93
94
	return $doing_upgrade;
95
96
}
97
98
/** End Helper Functions **/
99
100
/**
101
 * Run the 1.3 upgrades
102
 * @return void
103
 */
104
function ppp_v13_upgrades() {
105
	global $ppp_share_settings;
106
	$uq_status = ( isset( $ppp_share_settings['ppp_unique_links'] ) && $ppp_share_settings['ppp_unique_links'] == '1' ) ? $ppp_share_settings['ppp_unique_links'] : 0;
107
	$ga_status = ( isset( $ppp_share_settings['ppp_ga_tags'] ) && $ppp_share_settings['ppp_ga_tags'] == '1' ) ? $ppp_share_settings['ppp_ga_tags'] : 0;
108
109
	if ( $uq_status ) {
110
		$ppp_share_settings['analytics'] = 'unique_links';
111
		unset( $ppp_share_settings['ppp_unique_links'] );
112
	} elseif ( $ga_status ) {
113
		$ppp_share_settings['analytics'] = 'google_analytics';
114
		unset( $ppp_share_settings['ppp_ga_tags'] );
115
	}
116
117
	update_option( 'ppp_share_settings', $ppp_share_settings );
118
119
	global $ppp_options;
120
	$ppp_options['default_text'] = '{post_title}';
121
	$ppp_options['days'] = array( 'day1' => 'on', 'day2' => 'on', 'day3' => 'on', 'day4' => 'on', 'day5' => 'on', 'day6' => 'on');
122
123
	update_option( 'ppp_options', $ppp_options );
124
}
125
126
/**
127
 * Run the 2.1 updates
128
 * @return void
129
 */
130
function ppp_v21_upgrades() {
131
	// Auto load was set to true, let's make that false, we don't always need the version
132
	delete_option( 'ppp_version' );
133
	add_option( 'ppp_version', PPP_VERSION, '', 'no' );
134
}
135
136
/**
137
 * Run the 2.2 upgrades
138
 *
139
 * @return void
140
 */
141
function ppp_v22_postmeta_upgrade() {
142
143
	if( ! current_user_can( PostPromoterPro::get_manage_capability() ) ) {
144
		wp_die( __( 'You do not have permission to do upgrades', 'ppp-txt' ), __( 'Error', 'ppp-txt' ), array( 'response' => 403 ) );
145
	}
146
147
	ignore_user_abort( true );
148
149
	if ( ! ini_get( 'safe_mode' ) ) {
150
		@set_time_limit(0);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
151
	}
152
153
	global $wpdb;
154
155
156
	$step   = isset( $_GET['step'] ) ? absint( $_GET['step'] ) : 1;
157
	$number = 25;
158
	$offset = $step == 1 ? 0 : ( $step - 1 ) * $number;
159
160
	if ( $step < 2 ) {
161
		// Check if we have any payments before moving on
162
		$sql = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_ppp_post_override_data' LIMIT 1";
163
		$has_overrides = $wpdb->get_col( $sql );
164
165
		if( empty( $has_overrides ) ) {
166
			// We had no payments, just complete
167
			update_option( 'ppp_version', preg_replace( '/[^0-9.].*/', '', PPP_VERSION ) );
168
			ppp_set_upgrade_complete( 'upgrade_post_meta' );
169
			delete_option( 'ppp_doing_upgrade' );
170
			wp_redirect( admin_url() ); exit;
171
		}
172
	}
173
174
	$total = isset( $_GET['total'] ) ? absint( $_GET['total'] ) : false;
175
176
	if ( empty( $total ) || $total <= 1 ) {
177
		$total_sql = "SELECT COUNT(post_id) as total FROM $wpdb->postmeta WHERE meta_key = '_ppp_post_override_data'";
178
		$results   = $wpdb->get_row( $total_sql, 0 );
179
180
		$total     = $results->total;
181
	}
182
183
	$results       = $wpdb->get_results( $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_ppp_post_override_data' ORDER BY meta_id DESC LIMIT %d,%d;", $offset, $number ) );
184
	$new_post_meta = array();
185
186
	if ( $results ) {
187
		foreach ( $results as $result ) {
188
189
			$share_key     = 1;
190
191
			$override_data = unserialize( $result->meta_value );
192
193
			foreach ( $override_data as $day => $values ) {
194
195
				if ( ! isset( $values['enabled'] ) ) {
196
					continue;
197
				}
198
199
				$text = ! empty( $values['text'] ) ? $values['text'] : '';
200
				$time = ! empty( $values['time'] ) ? $values['time'] : '8:00am';
201
202
				$post          = get_post( $result->post_id );
203
				$days_ahead    = substr( $day, -1 );
204
				$date          = date( 'm\/d\/Y', strtotime( $post->post_date . '+' . $days_ahead . ' days' ) );
205
				$image         = '';
206
				$attachment_id = '';
207
208
				if ( ! empty( $values['use_image'] ) ) {
209
					$thumb_id  = get_post_thumbnail_id( $result->post_id );
210
					$thumb_url = wp_get_attachment_image_src( $thumb_id, 'ppp-tw-share-image', true );
211
212
					if ( isset( $thumb_url[0] ) && ! empty( $thumb_url[0] ) && !strpos( $thumb_url[0], 'wp-includes/images/media/default.png' ) ) {
213
						$thumb_url = $thumb_url[0];
214
					}
215
216
					if ( ! empty( $thumb_id ) && ! empty( $thumb_url ) ) {
217
						$attachment_id = $thumb_id;
218
						$image         = $thumb_url;
219
					}
220
				}
221
222
				$new_post_meta[$share_key] = array (
223
					'date'          => $date,
224
					'time'          => $time,
225
					'text'          => $text,
226
					'image'         => ! empty( $image ) ? $image : '',
227
					'attachment_id' => ! empty( $attachment_id ) ? $attachment_id : ''
228
				);
229
230
				$share_key++;
231
232
			}
233
234
			update_post_meta( $result->post_id, '_ppp_tweets', $new_post_meta );
235
		}
236
237
		// Postmeta found so upgrade them
238
		$step++;
239
		$redirect = add_query_arg( array(
240
			'page'        => 'ppp-upgrades',
241
			'ppp-upgrade' => 'upgrade_post_meta',
242
			'step'        => $step,
243
			'number'      => $number,
244
			'total'       => $total
245
		), admin_url( 'index.php' ) );
246
		wp_redirect( $redirect ); exit;
247
248
	} else {
249
250
		// No more postmeta found, finish up
251
		update_option( 'ppp_version', preg_replace( '/[^0-9.].*/', '', PPP_VERSION ) );
252
		ppp_set_upgrade_complete( 'upgrade_post_meta' );
253
		delete_option( 'ppp_doing_upgrade' );
254
		wp_redirect( admin_url() ); exit;
255
256
	}
257
}
258
add_action( 'ppp_upgrade_post_meta', 'ppp_v22_postmeta_upgrade' );
259
260
function ppp_fix_scheduled_shares_2319() {
261
262
	if( ! current_user_can( PostPromoterPro::get_manage_capability() ) ) {
263
		wp_die( __( 'You do not have permission to do upgrades', 'ppp-txt' ), __( 'Error', 'ppp-txt' ), array( 'response' => 403 ) );
264
	}
265
266
	ignore_user_abort( true );
267
268
	if ( ! ini_get( 'safe_mode' ) ) {
269
		@set_time_limit(0);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
270
	}
271
272
	global $wpdb;
273
274
275
	$step   = isset( $_GET['step'] ) ? absint( $_GET['step'] ) : 1;
276
	$number = 25;
277
	$offset = $step == 1 ? 0 : ( $step - 1 ) * $number;
278
279
	if ( $step < 2 ) {
280
		// Check if we have any payments before moving on
281
		$sql = "SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key IN ( '_ppp_tweets', '_ppp_fb_shares', '_ppp_li_shares' ) LIMIT 1";
282
		$has_shares = $wpdb->get_col( $sql );
283
284
		if( empty( $has_shares ) ) {
285
			// We had no payments, just complete
286
			update_option( 'ppp_version', preg_replace( '/[^0-9.].*/', '', PPP_VERSION ) );
287
			ppp_set_upgrade_complete( 'fix_scheduled_shares_2319' );
288
			delete_option( 'ppp_doing_upgrade' );
289
			wp_redirect( admin_url() ); exit;
290
		}
291
	}
292
293
	$total = isset( $_GET['total'] ) ? absint( $_GET['total'] ) : false;
294
295
	if ( empty( $total ) || $total <= 1 ) {
296
		$total_sql = "SELECT COUNT( DISTINCT post_id ) as total FROM $wpdb->postmeta WHERE meta_key IN ( '_ppp_tweets', '_ppp_fb_shares', '_ppp_li_shares' )";
297
		$results   = $wpdb->get_row( $total_sql, 0 );
298
299
		$total     = $results->total;
300
	}
301
302
	$results = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key IN ( '_ppp_tweets', '_ppp_fb_shares', '_ppp_li_shares' ) ORDER BY meta_id DESC LIMIT %d,%d;", $offset, $number ) );
303
304
	if ( $results ) {
305
		$allowed_post_types = ppp_allowed_post_types();
306
307
		foreach ( $results as $result ) {
308
			$post = get_post( $result->post_id );
309
			if ( ! in_array( $post->post_type, $allowed_post_types ) ) {
310
				continue;
311
			}
312
313
			$timestamps = ppp_get_timestamps( $result->post_id );
314
315
			foreach ( $timestamps as $timestamp => $name ) {
316
				$timestamp = substr( $timestamp, 0, strlen( $timestamp ) - 3 );
317
				wp_schedule_single_event( $timestamp, 'ppp_share_post_event', array( $result->post_id, $name ) );
318
			}
319
		}
320
321
		// Postmeta found so upgrade them
322
		$step++;
323
		$redirect = add_query_arg( array(
324
			'page'        => 'ppp-upgrades',
325
			'ppp-upgrade' => 'ppp_fix_scheduled_shares_2319',
326
			'step'        => $step,
327
			'number'      => $number,
328
			'total'       => $total
329
		), admin_url( 'index.php' ) );
330
		wp_redirect( $redirect ); exit;
331
332
	} else {
333
334
		// No more postmeta found, finish up
335
		update_option( 'ppp_version', preg_replace( '/[^0-9.].*/', '', PPP_VERSION ) );
336
		ppp_set_upgrade_complete( 'fix_scheduled_shares_2319' );
337
		delete_option( 'ppp_doing_upgrade' );
338
		wp_redirect( admin_url() ); exit;
339
340
	}
341
}
342
add_action( 'ppp_fix_scheduled_shares_2319', 'ppp_fix_scheduled_shares_2319' );
343