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.

upgrades.php ➔ ppp_fix_scheduled_shares_2319()   F
last analyzed

Complexity

Conditions 13
Paths 328

Size

Total Lines 79

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
nc 328
nop 0
dl 0
loc 79
rs 3.8048
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
	global $wpdb;
150
151
152
	$step   = isset( $_GET['step'] ) ? absint( $_GET['step'] ) : 1;
153
	$number = 25;
154
	$offset = $step == 1 ? 0 : ( $step - 1 ) * $number;
155
156
	if ( $step < 2 ) {
157
		// Check if we have any payments before moving on
158
		$sql = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_ppp_post_override_data' LIMIT 1";
159
		$has_overrides = $wpdb->get_col( $sql );
160
161
		if( empty( $has_overrides ) ) {
162
			// We had no payments, just complete
163
			update_option( 'ppp_version', preg_replace( '/[^0-9.].*/', '', PPP_VERSION ) );
164
			ppp_set_upgrade_complete( 'upgrade_post_meta' );
165
			delete_option( 'ppp_doing_upgrade' );
166
			wp_redirect( admin_url() ); exit;
167
		}
168
	}
169
170
	$total = isset( $_GET['total'] ) ? absint( $_GET['total'] ) : false;
171
172
	if ( empty( $total ) || $total <= 1 ) {
173
		$total_sql = "SELECT COUNT(post_id) as total FROM $wpdb->postmeta WHERE meta_key = '_ppp_post_override_data'";
174
		$results   = $wpdb->get_row( $total_sql, 0 );
175
176
		$total     = $results->total;
177
	}
178
179
	$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 ) );
180
	$new_post_meta = array();
181
182
	if ( $results ) {
183
		foreach ( $results as $result ) {
184
185
			$share_key     = 1;
186
187
			$override_data = unserialize( $result->meta_value );
188
189
			foreach ( $override_data as $day => $values ) {
190
191
				if ( ! isset( $values['enabled'] ) ) {
192
					continue;
193
				}
194
195
				$text = ! empty( $values['text'] ) ? $values['text'] : '';
196
				$time = ! empty( $values['time'] ) ? $values['time'] : '8:00am';
197
198
				$post          = get_post( $result->post_id );
199
				$days_ahead    = substr( $day, -1 );
200
				$date          = date( 'm\/d\/Y', strtotime( $post->post_date . '+' . $days_ahead . ' days' ) );
201
				$image         = '';
202
				$attachment_id = '';
203
204
				if ( ! empty( $values['use_image'] ) ) {
205
					$thumb_id  = get_post_thumbnail_id( $result->post_id );
206
					$thumb_url = wp_get_attachment_image_src( $thumb_id, 'ppp-tw-share-image', true );
207
208
					if ( isset( $thumb_url[0] ) && ! empty( $thumb_url[0] ) && !strpos( $thumb_url[0], 'wp-includes/images/media/default.png' ) ) {
209
						$thumb_url = $thumb_url[0];
210
					}
211
212
					if ( ! empty( $thumb_id ) && ! empty( $thumb_url ) ) {
213
						$attachment_id = $thumb_id;
214
						$image         = $thumb_url;
215
					}
216
				}
217
218
				$new_post_meta[$share_key] = array (
219
					'date'          => $date,
220
					'time'          => $time,
221
					'text'          => $text,
222
					'image'         => ! empty( $image ) ? $image : '',
223
					'attachment_id' => ! empty( $attachment_id ) ? $attachment_id : ''
224
				);
225
226
				$share_key++;
227
228
			}
229
230
			update_post_meta( $result->post_id, '_ppp_tweets', $new_post_meta );
231
		}
232
233
		// Postmeta found so upgrade them
234
		$step++;
235
		$redirect = add_query_arg( array(
236
			'page'        => 'ppp-upgrades',
237
			'ppp-upgrade' => 'upgrade_post_meta',
238
			'step'        => $step,
239
			'number'      => $number,
240
			'total'       => $total
241
		), admin_url( 'index.php' ) );
242
		wp_redirect( $redirect ); exit;
243
244
	} else {
245
246
		// No more postmeta found, finish up
247
		update_option( 'ppp_version', preg_replace( '/[^0-9.].*/', '', PPP_VERSION ) );
248
		ppp_set_upgrade_complete( 'upgrade_post_meta' );
249
		delete_option( 'ppp_doing_upgrade' );
250
		wp_redirect( admin_url() ); exit;
251
252
	}
253
}
254
add_action( 'ppp_upgrade_post_meta', 'ppp_v22_postmeta_upgrade' );
255
256
function ppp_fix_scheduled_shares_2319() {
257
258
	if( ! current_user_can( PostPromoterPro::get_manage_capability() ) ) {
259
		wp_die( __( 'You do not have permission to do upgrades', 'ppp-txt' ), __( 'Error', 'ppp-txt' ), array( 'response' => 403 ) );
260
	}
261
262
	ignore_user_abort( true );
263
264
	global $wpdb;
265
266
267
	$step   = isset( $_GET['step'] ) ? absint( $_GET['step'] ) : 1;
268
	$number = 25;
269
	$offset = $step == 1 ? 0 : ( $step - 1 ) * $number;
270
271
	if ( $step < 2 ) {
272
		// Check if we have any payments before moving on
273
		$sql = "SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key IN ( '_ppp_tweets', '_ppp_fb_shares', '_ppp_li_shares' ) LIMIT 1";
274
		$has_shares = $wpdb->get_col( $sql );
275
276
		if( empty( $has_shares ) ) {
277
			// We had no payments, just complete
278
			update_option( 'ppp_version', preg_replace( '/[^0-9.].*/', '', PPP_VERSION ) );
279
			ppp_set_upgrade_complete( 'fix_scheduled_shares_2319' );
280
			delete_option( 'ppp_doing_upgrade' );
281
			wp_redirect( admin_url() ); exit;
282
		}
283
	}
284
285
	$total = isset( $_GET['total'] ) ? absint( $_GET['total'] ) : false;
286
287
	if ( empty( $total ) || $total <= 1 ) {
288
		$total_sql = "SELECT COUNT( DISTINCT post_id ) as total FROM $wpdb->postmeta WHERE meta_key IN ( '_ppp_tweets', '_ppp_fb_shares', '_ppp_li_shares' )";
289
		$results   = $wpdb->get_row( $total_sql, 0 );
290
291
		$total     = $results->total;
292
	}
293
294
	$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 post_id DESC LIMIT %d,%d;", $offset, $number ) );
295
296
	if ( $results ) {
297
		$allowed_post_types = ppp_allowed_post_types();
298
299
		foreach ( $results as $result ) {
300
			$post = get_post( $result->post_id );
301
			if ( ! in_array( $post->post_type, $allowed_post_types ) ) {
302
				continue;
303
			}
304
305
			ppp_remove_scheduled_shares( $result->post_id );
306
			$timestamps = ppp_get_timestamps( $result->post_id );
307
308
			foreach ( $timestamps as $timestamp => $name ) {
309
				$timestamp = substr( $timestamp, 0, strlen( $timestamp ) - 3 );
310
				wp_schedule_single_event( $timestamp, 'ppp_share_post_event', array( $result->post_id, $name ) );
311
			}
312
		}
313
314
		// Postmeta found so upgrade them
315
		$step++;
316
		$redirect = add_query_arg( array(
317
			'page'        => 'ppp-upgrades',
318
			'ppp-upgrade' => 'fix_scheduled_shares_2319',
319
			'step'        => $step,
320
			'number'      => $number,
321
			'total'       => $total
322
		), admin_url( 'index.php' ) );
323
		wp_redirect( $redirect ); exit;
324
325
	} else {
326
327
		// No more postmeta found, finish up
328
		update_option( 'ppp_version', preg_replace( '/[^0-9.].*/', '', PPP_VERSION ) );
329
		ppp_set_upgrade_complete( 'fix_scheduled_shares_2319' );
330
		delete_option( 'ppp_doing_upgrade' );
331
		wp_redirect( admin_url() ); exit;
332
333
	}
334
}
335
add_action( 'ppp_fix_scheduled_shares_2319', 'ppp_fix_scheduled_shares_2319' );
336