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 (#175)
by Chris
04:24
created

upgrades.php ➔ ppp_v22_postmeta_upgrade()   F

Complexity

Conditions 24
Paths 10768

Size

Total Lines 117
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 24
eloc 69
c 0
b 0
f 0
nc 10768
nop 0
dl 0
loc 117
rs 2

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
		// End 'Stepped' upgrade process notices
39
40
	}
41
42
}
43
add_action( 'admin_notices', 'ppp_upgrade_notices' );
44
45
/**
46
 * Run any upgrade routines needed depending on versions
47
 * @return void
48
 */
49
function ppp_upgrade_plugin() {
50
	$ppp_version = get_option( 'ppp_version' );
51
52
	$upgrades_executed = false;
53
54
	// We don't have a version yet, so we need to run the upgrader
55
	if ( !$ppp_version && PPP_VERSION == '1.3' ) {
56
		ppp_v13_upgrades();
57
		$upgrades_executed = true;
58
	}
59
60
	if ( version_compare( $ppp_version, 2.1, '<' ) ) {
61
		ppp_v21_upgrades();
62
		$upgrades_executed = true;
63
	}
64
65
	if ( $upgrades_executed || version_compare( $ppp_version, PPP_VERSION, '<' ) ) {
66
		set_transient( '_ppp_activation_redirect', '1', 60 );
67
		update_option( 'ppp_version', PPP_VERSION );
68
	}
69
70
}
71
72
/** Helper Functions **/
73
74
/**
75
 * For use when doing 'stepped' upgrade routines, to see if we need to start somewhere in the middle
76
 * @since 2.2.6
77
 * @return mixed   When nothing to resume returns false, otherwise starts the upgrade where it left off
78
 */
79
function ppp_maybe_resume_upgrade() {
80
81
	$doing_upgrade = get_option( 'ppp_doing_upgrade', false );
82
83
	if ( empty( $doing_upgrade ) ) {
84
		return false;
85
	}
86
87
	return $doing_upgrade;
88
89
}
90
91
/** End Helper Functions **/
92
93
/**
94
 * Run the 1.3 upgrades
95
 * @return void
96
 */
97
function ppp_v13_upgrades() {
98
	global $ppp_share_settings;
99
	$uq_status = ( isset( $ppp_share_settings['ppp_unique_links'] ) && $ppp_share_settings['ppp_unique_links'] == '1' ) ? $ppp_share_settings['ppp_unique_links'] : 0;
100
	$ga_status = ( isset( $ppp_share_settings['ppp_ga_tags'] ) && $ppp_share_settings['ppp_ga_tags'] == '1' ) ? $ppp_share_settings['ppp_ga_tags'] : 0;
101
102
	if ( $uq_status ) {
103
		$ppp_share_settings['analytics'] = 'unique_links';
104
		unset( $ppp_share_settings['ppp_unique_links'] );
105
	} elseif ( $ga_status ) {
106
		$ppp_share_settings['analytics'] = 'google_analytics';
107
		unset( $ppp_share_settings['ppp_ga_tags'] );
108
	}
109
110
	update_option( 'ppp_share_settings', $ppp_share_settings );
111
112
	global $ppp_options;
113
	$ppp_options['default_text'] = '{post_title}';
114
	$ppp_options['days'] = array( 'day1' => 'on', 'day2' => 'on', 'day3' => 'on', 'day4' => 'on', 'day5' => 'on', 'day6' => 'on');
115
116
	update_option( 'ppp_options', $ppp_options );
117
}
118
119
/**
120
 * Run the 2.1 updates
121
 * @return void
122
 */
123
function ppp_v21_upgrades() {
124
	// Auto load was set to true, let's make that false, we don't always need the version
125
	delete_option( 'ppp_version' );
126
	add_option( 'ppp_version', PPP_VERSION, '', 'no' );
127
}
128
129
/**
130
 * Run the 2.2 upgrades
131
 *
132
 * @return void
133
 */
134
function ppp_v22_postmeta_upgrade() {
135
136
	if( ! current_user_can( 'manage_options' ) ) {
137
		wp_die( __( 'You do not have permission to do upgrades', 'ppp-txt' ), __( 'Error', 'ppp-txt' ), array( 'response' => 403 ) );
138
	}
139
140
	ignore_user_abort( true );
141
142
	if ( ! ini_get( 'safe_mode' ) ) {
143
		@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...
144
	}
145
146
	global $wpdb;
147
148
149
	$step   = isset( $_GET['step'] ) ? absint( $_GET['step'] ) : 1;
150
	$number = 25;
151
	$offset = $step == 1 ? 0 : ( $step - 1 ) * $number;
152
153
	if ( $step < 2 ) {
154
		// Check if we have any payments before moving on
155
		$sql = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_ppp_post_override_data' LIMIT 1";
156
		$has_overrides = $wpdb->get_col( $sql );
157
158
		if( empty( $has_overrides ) ) {
159
			// We had no payments, just complete
160
			update_option( 'ppp_version', preg_replace( '/[^0-9.].*/', '', PPP_VERSION ) );
161
			ppp_set_upgrade_complete( 'upgrade_post_meta' );
162
			delete_option( 'ppp_doing_upgrade' );
163
			wp_redirect( admin_url() ); exit;
164
		}
165
	}
166
167
	$total = isset( $_GET['total'] ) ? absint( $_GET['total'] ) : false;
168
169
	if ( empty( $total ) || $total <= 1 ) {
170
		$total_sql = "SELECT COUNT(post_id) as total FROM $wpdb->postmeta WHERE meta_key = '_ppp_post_override_data'";
171
		$results   = $wpdb->get_row( $total_sql, 0 );
172
173
		$total     = $results->total;
174
	}
175
176
	$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 ) );
177
	$new_post_meta = array();
178
179
	if ( $results ) {
180
		foreach ( $results as $result ) {
181
182
			$share_key     = 1;
183
184
			$override_data = unserialize( $result->meta_value );
185
186
			foreach ( $override_data as $day => $values ) {
187
188
				if ( ! isset( $values['enabled'] ) ) {
189
					continue;
190
				}
191
192
				$text = ! empty( $values['text'] ) ? $values['text'] : '';
193
				$time = ! empty( $values['time'] ) ? $values['time'] : '8:00am';
194
195
				$post          = get_post( $result->post_id );
196
				$days_ahead    = substr( $day, -1 );
197
				$date          = date( 'm\/d\/Y', strtotime( $post->post_date . '+' . $days_ahead . ' days' ) );
198
				$image         = '';
199
				$attachment_id = '';
200
201
				if ( ! empty( $values['use_image'] ) ) {
202
					$thumb_id  = get_post_thumbnail_id( $result->post_id );
203
					$thumb_url = wp_get_attachment_image_src( $thumb_id, 'ppp-tw-share-image', true );
204
205
					if ( isset( $thumb_url[0] ) && ! empty( $thumb_url[0] ) && !strpos( $thumb_url[0], 'wp-includes/images/media/default.png' ) ) {
206
						$thumb_url = $thumb_url[0];
207
					}
208
209
					if ( ! empty( $thumb_id ) && ! empty( $thumb_url ) ) {
210
						$attachment_id = $thumb_id;
211
						$image         = $thumb_url;
212
					}
213
				}
214
215
				$new_post_meta[$share_key] = array (
216
					'date'          => $date,
217
					'time'          => $time,
218
					'text'          => $text,
219
					'image'         => ! empty( $image ) ? $image : '',
220
					'attachment_id' => ! empty( $attachment_id ) ? $attachment_id : ''
221
				);
222
223
				$share_key++;
224
225
			}
226
227
			update_post_meta( $result->post_id, '_ppp_tweets', $new_post_meta );
228
		}
229
230
		// Postmeta found so upgrade them
231
		$step++;
232
		$redirect = add_query_arg( array(
233
			'page'        => 'ppp-upgrades',
234
			'ppp-upgrade' => 'upgrade_post_meta',
235
			'step'        => $step,
236
			'number'      => $number,
237
			'total'       => $total
238
		), admin_url( 'index.php' ) );
239
		wp_redirect( $redirect ); exit;
240
241
	} else {
242
243
		// No more postmeta found, finish up
244
		update_option( 'ppp_version', preg_replace( '/[^0-9.].*/', '', PPP_VERSION ) );
245
		ppp_set_upgrade_complete( 'upgrade_post_meta' );
246
		delete_option( 'ppp_doing_upgrade' );
247
		wp_redirect( admin_url() ); exit;
248
249
	}
250
}
251
add_action( 'ppp_upgrade_post_meta', 'ppp_v22_postmeta_upgrade' );
252