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

share-functions.php ➔ ppp_share_post()   C

Complexity

Conditions 10
Paths 35

Size

Total Lines 49
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 6
Bugs 2 Features 0
Metric Value
cc 10
eloc 27
c 6
b 2
f 0
nc 35
nop 2
dl 0
loc 49
rs 5.5471
ccs 0
cts 28
cp 0
crap 110

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
3
// Exit if accessed directly
4
if ( ! defined( 'ABSPATH' ) ) {
5
	exit;
6
}
7
8
/**
9
 * Determine if we should share this post when it's being published
10
 *
11
 * @param string $new_status The new status of the post
12
 * @param string $old_status The old status of the poast
13
 * @param  object $post      The Post Object
14
 *
15
 * @return void
16
 */
17
function ppp_share_on_publish( $new_status, $old_status, $post ) {
18
	// don't publish password protected posts
19 35
	if ( '' !== $post->post_password ) {
20
		return;
21
	}
22
23 35
	if ( $new_status == 'publish' && $old_status != 'publish' ) {
24 35
		global $ppp_options;
25
26 35
		$allowed_post_types = isset( $ppp_options['post_types'] ) ? $ppp_options['post_types'] : array();
27 35
		$allowed_post_types = apply_filters( 'ppp_schedule_share_post_types', $allowed_post_types );
28
29 35
		if ( !isset( $post->post_status ) || !array_key_exists( $post->post_type, $allowed_post_types ) ) {
30 35
			return false;
31
		}
32
33
		do_action( 'ppp_share_on_publish', $new_status, $old_status, $post );
34
	}
35
36
	return true;
37
}
38
39
/**
40
 * Create timestamps and unique identifiers for each cron.
41
 * @param  int $post_id
42
 * @return array
43
 */
44
function ppp_get_timestamps( $post_id ) {
45 1
	$times = array();
46 1
	return apply_filters( 'ppp_get_timestamps', $times, $post_id );
47
}
48
49
/**
50
 * Hook for the crons to fire and send shares
51
 * @param  id $post_id
52
 * @param  string $name
53
 * @return void
54
 */
55
function ppp_share_post( $post_id, $name ) {
56
	global $ppp_options, $ppp_social_settings, $ppp_share_settings, $ppp_twitter_oauth;
57
58
	// If we've already started to share this, don't share it again.
59
	// Compensates for wp-cron's race conditions
60
	if ( get_transient( 'ppp_sharing' . $name ) === 'true' ) {
61
		return;
62
	}
63
64
	// For 10 seconds, don't allow another share to go for this post
65
	set_transient( 'ppp_sharing' . $name, 'true', 10 );
66
67
	$name_parts = explode( '_', $name );
68
	$index      = $name_parts[1];
69
	$service    = isset( $name_parts[3] ) ? $name_parts[3] : 'tw';
70
71
	// If we're fired on a cron, check for stale cron runs
72
	if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
73
		switch( $service ) {
74
			case 'tw':
75
				$post_meta = get_post_meta( $post_id, '_ppp_tweets', true );
76
				break;
77
78
			case 'fb':
79
				$post_meta = get_post_meta( $post_id, '_ppp_fb_shares', true );
80
				break;
81
82
			case 'li':
83
				$post_meta = get_post_meta( $post_id, '_ppp_li_shares', true );
84
				break;
85
		}
86
87
		$share_data = array();
88
		if ( isset( $post_meta[ $index ] ) ) {
89
			$share_data = $post_meta[ $index ];
90
		}
91
92
		$timestamp = ppp_generate_timestamp( $share_data['date'], $share_data['time'] );
93
94
		// If the current time is more than 60 minutes (filterable) overdue, don't fire the share
95
		$share_buffer = apply_filters( 'ppp_share_buffer', HOUR_IN_SECONDS );
96
		if ( ( current_time( 'timestamp', 1 ) - $timestamp ) > $share_buffer ) {
97
			return;
98
		}
99
	}
100
101
	do_action( 'ppp_share_scheduled_' . $service, $post_id, $index, $name );
102
103
}
104
105
/**
106
 * Get the Social Share Tokens from the API
107
 * @return void
108
 */
109
function ppp_set_social_tokens() {
110
	if ( ( defined( 'PPP_TW_CONSUMER_KEY' ) && defined( 'PPP_TW_CONSUMER_SECRET' ) ) ||
111
	     ( defined( 'LINKEDIN_KEY' ) && defined( 'LINKEDIN_SECRET' ) ) ||
112
	     ( defined( 'bitly_clientid' ) && defined( 'bitly_secret' ) ) ||
113
	     ( defined( 'PPP_FB_APP_ID' ) && defined( 'PPP_FB_APP_SECRET' ) )
114
	   ) {
115
		return;
116
	}
117
118
	$social_tokens = ppp_has_local_tokens();
119
120
	if ( false === $social_tokens ) {
121
		define( 'PPP_LOCAL_TOKENS', false );
122
		$social_tokens = get_transient( 'ppp_social_tokens' );
123
124
		if ( ! $social_tokens ) {
125
			$license = trim( get_option( '_ppp_license_key' ) );
126
			$url = PPP_STORE_URL . '/?ppp-get-tokens&ppp-license-key=' . $license . '&ver=' . md5( time() . $license );
127
			$response = wp_remote_get( $url, array( 'timeout' => 15, 'sslverify' => false ) );
128
129
			if ( is_wp_error( $response ) ) {
130
				return false;
131
			}
132
133
			$social_tokens = json_decode( wp_remote_retrieve_body( $response ) );
134
135
		}
136
137
	} else {
138
		define( 'PPP_LOCAL_TOKENS', true );
139
		delete_transient( 'ppp_social_tokens' );
140
141
		if ( isset( $social_tokens->options ) ) {
142
			foreach ( $social_tokens->options as $constant => $value ) {
143
144
				$constant = strtoupper( $constant );
145
146
				if ( defined( $constant ) ) {
147
					continue;
148
				}
149
150
				switch( $constant ) {
151
152
					case 'NO_AUTO_UPDATE':
153
						// Avoid the call to the API to check for software updates
154
						$value = is_bool( $value ) ? $value : false;
155
						define( 'NO_AUTO_UPDATE', $value );
156
						break;
157
158
				}
159
			}
160
		}
161
	}
162
163
	if ( false === PPP_LOCAL_TOKENS && ! isset( $social_tokens->error ) && ( isset( $social_tokens->twitter ) || isset( $social_tokens->facebook ) || isset( $social_tokens->linkedin ) ) ) {
164
		set_transient( 'ppp_social_tokens', $social_tokens, WEEK_IN_SECONDS );
165
	}
166
167
	do_action( 'ppp_set_social_token_constants', $social_tokens );
168
}
169
170
/**
171
 * Checks if the user has uploaded a social media tokens JSON file
172
 * @return boolean If a file exists, true, else false.
173
 */
174
function ppp_has_local_tokens() {
175
176 1
	$token_file   = apply_filters( 'ppp_local_social_token_path', ppp_get_upload_path() . '/ppp-social-tokens.json' );
177 1
	$local_tokens = false;
178
179 1
	if ( ! file_exists( $token_file ) ) {
180 1
		return $local_tokens;
181
	}
182
183
	$local_tokens = json_decode( file_get_contents( $token_file ) );
184
185
	// Failed to parse as JSON
186
	if ( false === $local_tokens ) {
187
		return $local_tokens;
188
	}
189
190
	// No social tokens found in the format we accept or it was empty
191
	if ( empty( $local_tokens ) || ( ! isset( $local_tokens->twitter ) || ! isset( $local_tokens->facebook ) || ! isset( $local_tokens->linkedin ) ) ) {
192
		return false;
193
	}
194
195
	return apply_filters( 'ppp_local_tokens', $local_tokens, $token_file );
196
197
}
198
199
/**
200
 * Generate the link for the share
201
 * @param  int $post_id The Post ID
202
 * @param  string $name    The 'Name from the cron'
203
 * @return string          The URL to the post, to share
204
 */
205
function ppp_generate_link( $post_id, $name, $scheduled = true ) {
206 1
	global $ppp_share_settings;
207 1
	$share_link = get_permalink( $post_id );
208
209 1
	if ( ppp_link_tracking_enabled() ) {
210 1
		$share_link = ppp_generate_link_tracking( $share_link, $post_id, $name );
211 1
	}
212
213 1
	if ( ppp_is_shortener_enabled() && $scheduled ) {
214
		$shortener_name = $ppp_share_settings['shortener'];
215
		$share_link = apply_filters( 'ppp_apply_shortener-' . $shortener_name, $share_link );
216
	}
217
218
219 1
	return apply_filters( 'ppp_share_link', $share_link );
220
}
221
222
/**
223
 * Given a link, determine if link tracking needs to be applied
224
 * @param  string $share_link The Link to share
225
 * @param  int    $post_id    The Post ID the link belongs to
226
 * @param  string $name       The Name string from the cron
227
 * @return string             The URL to post, with proper analytics applied if necessary
228
 */
229
function ppp_generate_link_tracking( $share_link, $post_id, $name ) {
230 1
	if ( ppp_link_tracking_enabled() ) {
231 1
		global $ppp_share_settings;
232 1
		$link_tracking_type = $ppp_share_settings['analytics'];
233
234
		// Given the setting name, devs can extend this and apply a filter of ppp_analytics-[setting value]
235
		// to apply their own rules for link tracking
236 1
		$share_link = apply_filters( 'ppp_analytics-' . $link_tracking_type, $share_link, $post_id, $name );
237 1
	}
238
239 1
	$share_link = apply_filters( 'ppp_generate_link_tracking', $share_link, $post_id, $name );
240
241 1
	return $share_link;
242
}
243
244
/**
245
 * Determines if the post being shared should has media attached
246
 * @param  int $post_id      Post ID
247
 * @param  string $network   The Network being shared to
248
 * @param  bool $use_media   If this share should use media or not
249
 * @return mixed             If a thumbnail is found returns the URL, otherwise returns false
250
 */
251
function ppp_post_has_media( $post_id, $network, $use_media, $attachment_id = false ) {
252
	if ( !$use_media || empty( $post_id ) || empty( $network ) ) {
253
		return false;
254
	}
255
256
	$thumb_id = empty( $attachment_id ) ? get_post_thumbnail_id( $post_id ) : $attachment_id;
257
	$thumb_url = wp_get_attachment_image_src( $thumb_id, 'ppp-' . $network . '-share-image', true );
258
259
	if ( isset( $thumb_url[0] ) && ! empty( $thumb_url[0] ) && !strpos( $thumb_url[0], 'wp-includes/images/media/default.png' ) ) {
260
		return $thumb_url[0];
261
	}
262
263
	return false;
264
}
265