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 (#310)
by Chris
02:55
created

twitter-functions.php ➔ ppp_tw_capture_pin_auth()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
nc 32
nop 0
dl 0
loc 31
rs 8.8017
c 0
b 0
f 0
ccs 0
cts 20
cp 0
crap 42
1
<?php
2
3
// Exit if accessed directly
4
if ( ! defined( 'ABSPATH' ) ) {
5
	exit;
6
}
7
8
/**
9
 * Return if twitter account is found
10
 * @return bool If the Twitter object exists
11
 */
12
function ppp_twitter_enabled() {
13 1
	global $ppp_social_settings;
14
15 1
	if ( isset( $ppp_social_settings['twitter'] ) && !empty( $ppp_social_settings['twitter'] ) ) {
16
		return true;
17
	}
18
19 1
	return false;
20
}
21
22
/**
23
 * Register Twitter as a servcie
24
 * @param  array $services The registered services
25
 * @return array           With Twitter added
26
 */
27
function ppp_tw_register_service( $services = array() ) {
28 1
	$services[] = 'tw';
29
30 1
	return $services;
31
}
32
add_filter( 'ppp_register_social_service', 'ppp_tw_register_service', 10, 1 );
33
34
/**
35
 * The Twitter Icon
36
 * @param  string $string The default icon
37
 * @return string         The HTML for the Twitter Icon
38
 */
39
function ppp_tw_account_list_icon( $string = '' ) {
40 1
	$string .= '<span class="dashicons icon-ppp-tw"></span>';
41
42 1
	return $string;
43
}
44
add_filter( 'ppp_account_list_icon-tw', 'ppp_tw_account_list_icon', 10, 1 );
45
46
/**
47
 * The avatar for the connected Twitter Account
48
 * @param  string $string Default avatar string
49
 * @return string         The Twitter avatar
50
 */
51
function ppp_tw_account_list_avatar( $string = '' ) {
52
53
	if ( ppp_twitter_enabled() ) {
54
		global $ppp_social_settings;
55
		$avatar_url = $ppp_social_settings['twitter']['user']->profile_image_url_https;
56
		$string .= '<img class="ppp-social-icon" src="' . $avatar_url . '" />';
57
	}
58
59
	return $string;
60
}
61
add_filter( 'ppp_account_list_avatar-tw', 'ppp_tw_account_list_avatar', 10, 1 );
62
63
/**
64
 * The name of the connected Twitter account for the list view
65
 * @param  string $string The default name
66
 * @return string         The name from Twitter
67
 */
68
function ppp_tw_account_list_name( $string = '' ) {
69
70
	if ( ppp_twitter_enabled() ) {
71
		global $ppp_social_settings;
72
		$string .= $ppp_social_settings['twitter']['user']->name;
73
	}
74
75
	return $string;
76
}
77
add_filter( 'ppp_account_list_name-tw', 'ppp_tw_account_list_name', 10, 1 );
78
79
/**
80
 * The actions for the Twitter account list
81
 * @param  string $string The default actions
82
 * @return string         The actions buttons HTML for Twitter
83
 */
84
function ppp_tw_account_list_actions( $string = '' ) {
85
86
	if ( ! ppp_twitter_enabled() ) {
87
		global $ppp_twitter_oauth, $ppp_social_settings;
88
		$tw_auth    = $ppp_twitter_oauth->ppp_verify_twitter_credentials();
0 ignored issues
show
Unused Code introduced by
$tw_auth is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
89
		$tw_authurl = $ppp_twitter_oauth->ppp_get_twitter_auth_url();
90
91
		$string .= '<span id="tw-oob-auth-link-wrapper"><a id="tw-oob-auth-link" href="' . $tw_authurl . '" target="_blank"><img src="' . PPP_URL . '/includes/images/sign-in-with-twitter-gray.png" /></a></span>';
92
		$string .= '<span style="display:none;" id="tw-oob-pin-notice">' . __( 'You are being directed to Twitter to authenticate. When complete, return here and enter the PIN you were provided.', 'ppp-txt' ) . '</span>';
93
		$string .= '<span style="display:none;" id="tw-oob-pin-wrapper"><input type="text" size="10" placeholder="Enter your PIN" value="" id="tw-oob-pin" data-nonce="' . wp_create_nonce( 'ppp-tw-pin' ) . '" /> <a href="#" class="button-secondary tw-oob-pin-submit">' . __( 'Submit', 'ppp-txt' ) . '</a><span class="spinner"></span></span>';
94
	} else {
95
		$string .= '<a class="button-primary" href="' . admin_url( 'admin.php?page=ppp-social-settings&ppp_social_disconnect=true&ppp_network=twitter' ) . '" >' . __( 'Disconnect from Twitter', 'ppp-txt' ) . '</a>&nbsp;';
96
		$string .= '<a class="button-secondary" href="https://twitter.com/settings/applications" target="blank">' . __( 'Revoke Access via Twitter', 'ppp-txt' ) . '</a>';
97
	}
98
99
	return $string;
100
}
101
add_filter( 'ppp_account_list_actions-tw', 'ppp_tw_account_list_actions', 10, 1 );
102
103
104
function ppp_tw_capture_pin_auth() {
105
	global $ppp_social_settings, $ppp_twitter_oauth;
106
107
	ppp_set_social_tokens();
108
109
	$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
110
	$nonce_verified = wp_verify_nonce( $nonce, 'ppp-tw-pin' );
111
112
	if ( ! $nonce_verified ) {
113
		wp_die();
114
	}
115
116
	$pin = isset( $_POST['pin'] ) ? absint( $_POST['pin'] ) : false;
117
118
	if ( empty( $pin ) ) {
119
		wp_die();
120
	}
121
122
	$_REQUEST['oauth_verifier'] = $pin;
123
	$twitter = new PPP_Twitter;
124
	$twitter->ppp_initialize_twitter();
125
	$settings = get_option( 'ppp_social_settings', true );
126
127
	if ( ! empty( $settings['twitter']['user']->id ) ) {
128
		echo 1;
129
	} else {
130
		echo 0;
131
	}
132
133
	die(); // this is required to return a proper result
134
}
135
add_action( 'wp_ajax_ppp_tw_auth_pin', 'ppp_tw_capture_pin_auth' );
136
137
/**
138
 * Listen for the oAuth tokens and verifiers from Twitter when in admin
139
 * @return void
140
 */
141
function ppp_capture_twitter_oauth() {
142
	if ( isset( $_REQUEST['oauth_verifier'] ) && isset( $_REQUEST['oauth_token'] ) ) {
143
		$current_screen = get_current_screen();
144
		if ( 'user-edit' === $current_screen->base ) {
145
			$user_id = ! empty( $_GET['user_id'] ) && is_numeric( $_GET['user_id'] ) ? $_GET['user_id'] : false;
146
			$twitter = new PPP_Twitter_User( $user_id );
147
			$twitter->init();
148
			$redirect = admin_url( 'user-edit.php?updated=1&user_id=' . $user_id );
149
		} else {
150
			global $ppp_twitter_oauth;
151
			$ppp_twitter_oauth->ppp_initialize_twitter();
152
			$redirect = admin_url( 'admin.php?page=ppp-social-settings' );
153
		}
154
		?>
155
		<meta http-equiv="refresh" content="0;URL=<?php echo $redirect; ?>">
156
		<?php
157
	}
158
}
159
//add_action( 'admin_head', 'ppp_capture_twitter_oauth', 10 );
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
160
161
/**
162
 * Listen for the disconnect from Twitter
163
 * @return void
164
 */
165
function ppp_disconnect_twitter() {
166
	if ( ! empty( $_GET['user_id'] ) ) {
167
		$user_id = (int) sanitize_text_field( $_GET['user_id'] );
168
		if ( $user_id !== get_current_user_id() || ! current_user_can( PostPromoterPro::get_manage_capability() ) ) {
169
			wp_die( __( 'Unable to disconnect Twitter account', 'ppp-txt' ) );
170
		}
171
		delete_user_meta( $user_id, '_ppp_twitter_data' );
172
	} else {
173
		global $ppp_social_settings;
174
		$ppp_social_settings = get_option( 'ppp_social_settings' );
175
		if ( isset( $ppp_social_settings['twitter'] ) ) {
176
			unset( $ppp_social_settings['twitter'] );
177
			update_option( 'ppp_social_settings', $ppp_social_settings );
178
		}
179
	}
180
}
181
add_action( 'ppp_disconnect-twitter', 'ppp_disconnect_twitter', 10 );
182
183
/**
184
 * Given a message, sends a tweet
185
 * @param  string $message The Text to share as the body of the tweet
186
 * @return object          The Results from the Twitter API
187
 */
188
function ppp_send_tweet( $message, $post_id, $use_media = false, $name = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
189
	global $ppp_twitter_oauth;
190
191
	return apply_filters( 'ppp_twitter_tweet', $ppp_twitter_oauth->ppp_tweet( ppp_entities_and_slashes( $message ), $use_media ) );
192
}
193
194
/**
195
 * Send out a scheduled share to Twitter
196
 *
197
 * @since  2.3
198
 * @param  integer $post_id The Post ID to share fore
199
 * @param  integer $index   The index in the shares
200
 * @param  string  $name    The name of the Cron
201
 * @return void
202
 */
203
function ppp_tw_scheduled_share( $post_id = 0, $index = 1, $name = '' ) {
204
	global $ppp_options, $wp_logs, $wp_filter;
205
206
	$post_meta     = get_post_meta( $post_id, '_ppp_tweets', true );
207
	$this_share    = $post_meta[ $index ];
208
	$attachment_id = isset( $this_share['attachment_id'] ) ? $this_share['attachment_id'] : false;
209
210
	$share_message = ppp_tw_build_share_message( $post_id, $name );
211
212
	if ( empty( $attachment_id ) && ! empty( $this_share['image'] ) ) {
213
		$media = $this_share['image'];
214
	} else {
215
		$use_media = ppp_tw_use_media( $post_id, $index );
216
		$media     = ppp_post_has_media( $post_id, 'tw', $use_media, $attachment_id );
217
	}
218
219
	$status = ppp_send_tweet( $share_message, $post_id, $media );
220
221
	$log_title = ppp_tw_build_share_message( $post_id, $name, false, false );
222
223
	$log_data = array(
224
		'post_title'    => $log_title,
225
		'post_content'  => '',
226
		'post_parent'   => $post_id,
227
		'log_type'      => 'ppp_share'
228
	);
229
230
	$log_meta = array(
231
		'network'   => 'tw',
232
		'share_id'  => $index,
233
	);
234
235
	$log_entry = WP_Logging::insert_log( $log_data, $log_meta );
236
237
	update_post_meta( $log_entry, '_ppp_share_status', $status );
238
239
	if ( ! empty( $status->id_str ) ) {
240
		$post      = get_post( $post_id );
241
		$author_id = $post->post_author;
242
		$author_rt = get_user_meta( $author_id, '_ppp_share_scheduled', true );
243
244
		if ( $author_rt ) {
245
			$twitter_user = new PPP_Twitter_User( $author_id );
246
			$twitter_user->retweet( $status->id_str );
247
		}
248
249
		/* Get an array of users who should retweet if another author has a scheduled share
250
		 * Exclude the author that retweeted earlier to avoid duplicate retweets.
251
		 */
252
		$args = array(
253
			'meta_query' => array(
254
				array(
255
					'key' 		=> '_ppp_share_others_scheduled',
256
					'value'		=> true,
257
					'compare'	=> '='
258
				),
259
			),
260
			'fields'	=> array( 'ID' ),
261
			'exclude'	=> array( $author_id )
262
		);
263
		$other_rt = get_users( $args );
264
265
		if ( $other_rt ){
266
			foreach ( $other_rt as $user ) {
267
				$twitter_user = new PPP_Twitter_User( $user->ID );
268
				$twitter_user->retweet( $status->id_str );
269
			}
270
		}
271
	}
272
}
273
add_action( 'ppp_share_scheduled_tw', 'ppp_tw_scheduled_share', 10, 3 );
274
275
/**
276
 * Combines the results from ppp_generate_share_content and ppp_generate_link into a single string
277
 * @param  int $post_id The Post ID
278
 * @param  string $name    The 'name' element from the Cron
279
 * @param  boolean $scheduled If the item is being requsted by a scheduled post
280
 * @param  bool $include_link If a link should be included in the text
281
 * @return string          The Full text for the social share
282
 */
283
function ppp_tw_build_share_message( $post_id, $name, $scheduled = true, $include_link = true ) {
284
	$share_content = ppp_tw_generate_share_content( $post_id, $name, $scheduled );
285
286
	if ( $include_link ) {
287
		$share_link    = ppp_generate_link( $post_id, $name, $scheduled );
288
		$share_content = $share_content . ' ' . $share_link;
289
	}
290
291
	return apply_filters( 'ppp_tw_build_share_message', $share_content );
292
}
293
294
/**
295
 * Generate the content for the shares
296
 * @param  int $post_id The Post ID
297
 * @param  string $name    The 'Name' from the cron
298
 * @return string          The Content to include in the social media post
299
 */
300
function ppp_tw_generate_share_content( $post_id, $name, $is_scheduled = true ) {
0 ignored issues
show
Unused Code introduced by
The parameter $is_scheduled is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
301
	global $ppp_options;
302
	$default_text = isset( $ppp_options['default_text'] ) ? $ppp_options['default_text'] : '';
303
	$ppp_tweets   = get_post_meta( $post_id, '_ppp_tweets', true );
304
305
	if ( ! empty( $ppp_tweets ) ) {
306
		$name_array    = explode( '_', $name );
307
		$index         = $name_array[1];
308
		if ( isset( $ppp_tweets[ $index ] ) ) {
309
			$share_content = $ppp_tweets[ $index ]['text'];
310
		}
311
	}
312
313
	// If an override was found, use it, otherwise try the default text content
314
	$share_content = ( isset( $share_content ) && !empty( $share_content ) ) ? $share_content : $default_text;
315
316
	// If the content is still empty, just use the post title
317
	$share_content = ( isset( $share_content ) && !empty( $share_content ) ) ? $share_content : get_the_title( $post_id );
318
319
	return apply_filters( 'ppp_share_content', $share_content, array( 'post_id' => $post_id ) );
320
}
321
322
/**
323
 * Return if media is supported for this scheduled tweet
324
 * @param  int $post_id The Post ID
325
 * @param  int $index   The index of this tweet in the _ppp_tweets data
326
 * @return bool         Whether or not this tweet should contain a media post
327
 */
328
function ppp_tw_use_media( $post_id, $index ) {
329
	if ( empty( $post_id ) || empty( $index ) ) {
330
		return false;
331
	}
332
333
	$share_data = get_post_meta( $post_id, '_ppp_tweets', true );
334
	$use_media  = ! empty( $share_data[$index]['attachment_id'] ) || ! empty( $share_data[$index]['image'] ) ? true : false;
335
336
	return $use_media;
337
}
338
339
/**
340
 * Sets the constants for the oAuth tokens for Twitter
341
 * @param  array $social_tokens The tokens stored in the transient
342
 * @return void
343
 */
344
function ppp_set_tw_token_constants( $social_tokens ) {
345
	if ( !empty( $social_tokens ) && property_exists( $social_tokens, 'twitter' ) ) {
346
		define( 'PPP_TW_CONSUMER_KEY', $social_tokens->twitter->consumer_token );
347
		define( 'PPP_TW_CONSUMER_SECRET', $social_tokens->twitter->consumer_secret );
348
	}
349
}
350
add_action( 'ppp_set_social_token_constants', 'ppp_set_tw_token_constants', 10, 1 );
351
352
/**
353
 * Register Twitter for the Social Media Accounts section
354
 * @param  array $tabs Array of existing tabs
355
 * @return array       The Array of existing tabs with Twitter added
356
 */
357
function ppp_tw_add_admin_tab( $tabs ) {
358
	$tabs['tw'] = array( 'name' => __( 'Twitter', 'ppp-txt' ), 'class' => 'icon-ppp-tw' );
359
360
	return $tabs;
361
}
362
add_filter( 'ppp_admin_tabs', 'ppp_tw_add_admin_tab', 10, 1 );
363
364
/**
365
 * Register the Twitter connection area for the Social Media Accounts section
366
 * @param  array $content The existing content tokens
367
 * @return array          The content tokens with Twitter added
368
 */
369
function ppp_tw_register_admin_social_content( $content ) {
370
	$content[] = 'tw';
371
372
	return $content;
373
}
374
add_filter( 'ppp_admin_social_content', 'ppp_tw_register_admin_social_content', 10, 1 );
375
376
/**
377
 * Register the Twitter metabox tab
378
 * @param  array $tabs The tabs
379
 * @return array       The tabs with Twitter added
380
 */
381
function ppp_tw_add_meta_tab( $tabs ) {
382
	global $ppp_social_settings;
383
	if ( !isset( $ppp_social_settings['twitter'] ) ) {
384
		return $tabs;
385
	}
386
387
	$tabs['tw'] = array( 'name' => __( 'Twitter', 'ppp-txt' ), 'class' => 'icon-ppp-tw' );
388
389
	return $tabs;
390
}
391
add_filter( 'ppp_metabox_tabs', 'ppp_tw_add_meta_tab', 10, 1 );
392
393
/**
394
 * Register the metabox content for Twitter
395
 * @param  array $content The existing metabox tokens
396
 * @return array          The metabox tokens with Twitter added
397
 */
398
function ppp_tw_register_metabox_content( $content ) {
399
	global $ppp_social_settings;
400
	if ( !isset( $ppp_social_settings['twitter'] ) ) {
401
		return $content;
402
	}
403
404
	$content[] = 'tw';
405
406
	return $content;
407
}
408
add_filter( 'ppp_metabox_content', 'ppp_tw_register_metabox_content', 10, 1 );
409
410
/**
411
 * Returns the stored Twitter data for a post
412
 *
413
 * @since  2.3
414
 * @param  array $post_meta Array of meta data (empty)
415
 * @param  int   $post_id   The Post ID to get the meta for
416
 * @return array            The stored Twitter shares for a post
417
 */
418
function ppp_tw_get_post_meta( $post_meta, $post_id ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post_meta is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
419
	return get_post_meta( $post_id, '_ppp_tweets', true );
420
}
421
add_filter( 'ppp_get_scheduled_items_tw', 'ppp_tw_get_post_meta', 10, 2 );
422
423
/**
424
 * Registers the thumbnail size for Twitter
425
 * @return void
426
 */
427
function ppp_tw_register_thumbnail_size() {
428
	add_image_size( 'ppp-tw-share-image', 1024, 512, true );
429
}
430
add_action( 'ppp_add_image_sizes', 'ppp_tw_register_thumbnail_size' );
431
432
/**
433
 * The callback that adds Twitter metabox content
434
 * @param  object $post The post object
435
 * @return void         Displays the metabox content
436
 */
437
function ppp_tw_add_metabox_content( $post ) {
438
	global $ppp_options, $ppp_share_settings, $has_past_shares;
439
	$has_past_shares = 0;
440
	?>
441
		<p>
442
			<div class="ppp-post-override-wrap">
443
				<p><h3><?php _e( 'Share on Twitter', 'ppp-txt' ); ?></h3></p>
444
				<div id="ppp-tweet-fields" class="ppp-tweet-fields">
445
					<div id="ppp-tweet-fields" class="ppp-meta-table-wrap">
446
						<table class="widefat ppp-repeatable-table" width="100%" cellpadding="0" cellspacing="0">
447
							<thead>
448
								<tr>
449
									<th style="width: 100px"><?php _e( 'Date', 'ppp-txt' ); ?></th>
450
									<th style="width: 75px;"><?php _e( 'Time', 'ppp-txt' ); ?></th>
451
									<th><?php _e( 'Text', 'ppp-txt' ); ?></th>
452
									<th style"width: 200px;"><?php _e( 'Image', 'ppp-txt' ); ?></th>
453
									<th style="width: 30px;"></th>
454
								</tr>
455
							</thead>
456
							<tbody>
457
								<?php ppp_render_tweet_share_on_publish_row(); ?>
458
								<?php $tweets = get_post_meta( $post->ID, '_ppp_tweets', true ); ?>
459
								<?php if ( ! empty( $tweets ) ) : ?>
460
461
									<?php foreach ( $tweets as $key => $value ) :
462
										$date          = isset( $value['date'] )          ? $value['date']          : '';
463
										$time          = isset( $value['time'] )          ? $value['time']          : '';
464
										$text          = isset( $value['text'] )          ? $value['text']          : '';
465
										$image         = isset( $value['image'] )         ? $value['image']         : '';
466
										$attachment_id = isset( $value['attachment_id'] ) ? $value['attachment_id'] : '';
467
468
										$args = apply_filters( 'ppp_tweet_row_args', compact( 'date','time','text','image','attachment_id' ), $value );
469
										?>
470
471
										<?php ppp_render_tweet_row( $key, $args, $post->ID ); ?>
472
473
474
									<?php endforeach; ?>
475
476
									<?php
477
									if ( ! empty( $has_past_shares ) && count ( $tweets ) == $has_past_shares ) {
478
										$args = array(
479
											'date'          => '',
480
											'time'          => '',
481
											'text'          => '',
482
											'image'         => '',
483
											'attachment_id' => '',
484
										);
485
486
487
										ppp_render_tweet_row( $key + 1, $args, $post->ID );
0 ignored issues
show
Bug introduced by
The variable $key seems to be defined by a foreach iteration on line 461. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
488
									}
489
									?>
490
491
								<?php else: ?>
492
493
									<?php ppp_render_tweet_row( 1, array( 'date' => '', 'time' => '', 'text' => '', 'image' => '', 'attachment_id' => '' ), $post->ID, 1 ); ?>
0 ignored issues
show
Unused Code introduced by
The call to ppp_render_tweet_row() has too many arguments starting with 1.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
494
495
								<?php endif; ?>
496
497
								<tr class="ppp-add-repeatable-wrapper">
498
									<td class="submit" colspan="4" style="float: none; clear:both; background:#fff;">
499
										<a class="button-secondary ppp-add-repeatable" style="margin: 6px 0;"><?php _e( 'Add New Tweet', 'ppp-txt' ); ?></a>
500
										<?php if ( ! empty( $has_past_shares ) ) : ?>
501
											<a class="button-secondary ppp-view-all" style="margin: 6px 0;"><?php _e( 'Toggle Past Tweets', 'ppp-txt' ); ?></a>
502
										<?php endif; ?>
503
									</td>
504
								</tr>
505
							</tbody>
506
						</table>
507
					</div>
508
				</div><!--end #edd_variable_price_fields-->
509
510
				<p><?php _e( 'Do not include links in your text, this will be added automatically.', 'ppp-txt' ); ?></p>
511
				<p style="display: none;" id="ppp-show-conflict-warning"><?php printf( __( 'Items highlighted in red have a time assigned that is within %d minutes of an already scheduled Tweet', 'ppp-txt' ), floor( ppp_get_default_conflict_window() / 60 ) ); ?></p>
512
			</div>
513
		</p>
514
	<?php
515
}
516
add_action( 'ppp_generate_metabox_content-tw', 'ppp_tw_add_metabox_content', 10, 1 );
517
518
/**
519
 * Generates the 'share on publish row' of the Twitter Metabox content
520
 *
521
 * @since  2.3
522
 * @return void
523
 */
524
function ppp_render_tweet_share_on_publish_row() {
525
	global $post, $ppp_share_settings;
526
	$default_text = !empty( $ppp_options['default_text'] ) ? $ppp_options['default_text'] : __( 'Social Text', 'ppp-txt' );
0 ignored issues
show
Bug introduced by
The variable $ppp_options seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
Unused Code introduced by
$default_text is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
527
528
	$ppp_post_exclude = get_post_meta( $post->ID, '_ppp_post_exclude', true );
0 ignored issues
show
Unused Code introduced by
$ppp_post_exclude is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
529
530
	$ppp_share_on_publish  = get_post_meta( $post->ID, '_ppp_share_on_publish', true );
531
	$show_share_on_publish = false;
532
533
	$share_by_default      = empty( $ppp_share_settings['share_on_publish'][ $post->post_type ]['twitter'] ) ? false : true;
534
535
	if ( $ppp_share_on_publish == '1' || ( $ppp_share_on_publish == '' && $share_by_default ) ) {
536
		$show_share_on_publish = true;
537
	}
538
539
	$ppp_share_on_publish_text          = get_post_meta( $post->ID, '_ppp_share_on_publish_text', true );
540
	$ppp_share_on_publish_include_image = get_post_meta( $post->ID, '_ppp_share_on_publish_include_image', true );
541
542
	$disabled = ( $post->post_status === 'publish' && time() > strtotime( $post->post_date ) ) ? true : false;
543
	?>
544
	<tr class="ppp-tweet-wrapper ppp-repeatable-row on-publish-row">
545
		<td colspan="2" class="ppp-on-plublish-date-column">
546
			<input <?php if ( $disabled ): ?>readonly<?php endif; ?> type="checkbox" name="_ppp_share_on_publish" id="ppp_share_on_publish" value="1" <?php checked( true, $show_share_on_publish, true ); ?> />
547
			&nbsp;<label for="ppp_share_on_publish"><?php _e( 'Tweet On Publish', 'ppp-txt' ); ?></label>
548
		</td>
549
550
		<td>
551
			<textarea <?php if ( $disabled ): ?>readonly<?php endif; ?> class="ppp-tweet-text-repeatable" type="text" name="_ppp_share_on_publish_text"><?php echo esc_attr( $ppp_share_on_publish_text ); ?></textarea>
552
			<?php $length = ! empty( $ppp_share_on_publish_text ) ? strlen( $ppp_share_on_publish_text ) : 0; ?>
553
			&nbsp;<span class="ppp-text-length"><?php echo $length; ?></span>
554
		</td>
555
556
		<td style="width: 200px" colspan="2">
557
			<input class="ppp-tw-featured-image-input" <?php if ( $disabled ): ?>readonly<?php endif; ?> id="ppp-share-on-publish-image" type="checkbox" name="_ppp_share_on_publish_include_image" value="1" <?php checked( '1', $ppp_share_on_publish_include_image, true ); ?>/>
558
			&nbsp;<label for="ppp-share-on-publish-image"><?php _e( 'Featured Image', 'ppp-txt' ); ?></label>
559
		</td>
560
561
	</tr>
562
<?php
563
}
564
565
/**
566
 * Generates the row for a scheduled Tweet in the metabox
567
 *
568
 * @param  int    $key     The array index
569
 * @param  array  $args    Arguements/Data for the specific index
570
 * @param  int    $post_id The post ID
571
 * @return void
572
 */
573
function ppp_render_tweet_row( $key, $args = array(), $post_id ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
574
	global $post, $has_past_shares;
575
576
	if ( ! empty( $args['date'] ) && ! empty( $args['time'] ) ) {
577
		$share_time = ppp_generate_timestamp( $args['date'], $args['time'] );
578
		$readonly   = ppp_generate_timestamp() > $share_time ? 'readonly="readonly" ' : false;
579
	} else {
580
		$share_time = false;
0 ignored issues
show
Unused Code introduced by
$share_time is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
581
		$readonly   = false;
582
	}
583
584
	$no_date        = ! empty( $readonly ) ? ' hasDatepicker' : '';
585
	$hide           = ! empty( $readonly ) ? 'display: none;' : '';
586
	$shared         = ! empty( $readonly ) ? 'past-share' : '';
587
588
	if ( ! empty( $readonly ) ) {
589
		$has_past_shares++;
590
	}
591
	?>
592
	<tr class="ppp-tweet-wrapper ppp-repeatable-row ppp-repeatable-twitter scheduled-row <?php echo $shared; ?>" data-key="<?php echo esc_attr( $key ); ?>">
593
		<td>
594
			<input <?php echo $readonly; ?>type="text" class="share-date-selector<?php echo $no_date; ?>" name="_ppp_tweets[<?php echo $key; ?>][date]" placeholder="mm/dd/yyyy" value="<?php echo $args['date']; ?>" />
595
		</td>
596
597
		<td>
598
			<input <?php echo $readonly; ?>type="text" class="share-time-selector" name="_ppp_tweets[<?php echo $key; ?>][time]" value="<?php echo $args['time']; ?>" />
599
		</td>
600
601
		<td>
602
			<textarea class="ppp-tweet-text-repeatable" type="text" name="_ppp_tweets[<?php echo $key; ?>][text]" <?php echo $readonly; ?>><?php echo esc_attr( $args['text'] ); ?></textarea>
603
			<?php $length = ! empty( $args['text'] ) ? strlen( $args['text'] ) : 0; ?>
604
			&nbsp;<span class="ppp-text-length"><?php echo $length; ?></span>
605
		</td>
606
607
		<td class="ppp-repeatable-upload-wrapper" style="width: 200px">
608
			<div class="ppp-repeatable-upload-field-container">
609
				<input type="hidden" name="_ppp_tweets[<?php echo $key; ?>][attachment_id]" class="ppp-repeatable-attachment-id-field" value="<?php echo esc_attr( absint( $args['attachment_id'] ) ); ?>"/>
610
				<input <?php echo $readonly; ?>type="text" class="ppp-repeatable-upload-field ppp-upload-field" name="_ppp_tweets[<?php echo $key; ?>][image]" placeholder="<?php _e( 'Upload or Enter URL', 'ppp-txt' ); ?>" value="<?php echo esc_attr( $args['image'] ); ?>" />
611
612
				<span class="ppp-upload-file" style="<?php echo $hide; ?>">
613
					<a href="#" title="<?php _e( 'Insert File', 'ppp-txt' ) ?>" data-uploader-title="<?php _e( 'Insert File', 'ppp-txt' ); ?>" data-uploader-button-text="<?php _e( 'Insert', 'ppp-txt' ); ?>" class="ppp-upload-file-button" onclick="return false;">
614
						<span class="dashicons dashicons-upload"></span>
615
					</a>
616
				</span>
617
618
			</div>
619
		</td>
620
621
		<td class="ppp-action-icons">
622
			<a title="<?php _e( 'Duplicate', 'ppp-txt' ); ?>" href="#" class="ppp-clone-tweet"><i class="fa fa-repeat" aria-hidden="true"></i></a>
623
			&nbsp;<a title="<?php _e( 'Delete', 'ppp-txt' ); ?>" href="#" class="ppp-repeatable-row ppp-remove-repeatable" data-type="twitter" style="<?php echo $hide; ?>"><i class="fa fa-trash" aria-hidden="true"></i></a>
624
		</td>
625
626
	</tr>
627
<?php
628
}
629
630
/**
631
 * Save the items in our meta boxes
632
 * @param  int $post_id The Post ID being saved
633
 * @param  object $post    The Post Object being saved
634
 * @return int          The Post ID
635
 */
636
function ppp_tw_save_post_meta_boxes( $post_id, $post ) {
637
638
	if ( ! ppp_should_save( $post_id, $post ) ) {
639
		return;
640
	}
641
642
	$ppp_post_exclude = ( isset( $_REQUEST['_ppp_post_exclude'] ) ) ? $_REQUEST['_ppp_post_exclude'] : '0';
0 ignored issues
show
Unused Code introduced by
$ppp_post_exclude is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
643
644
	$ppp_share_on_publish = ( isset( $_REQUEST['_ppp_share_on_publish'] ) ) ? $_REQUEST['_ppp_share_on_publish'] : '0';
645
	$ppp_share_on_publish_text = ( isset( $_REQUEST['_ppp_share_on_publish_text'] ) ) ? $_REQUEST['_ppp_share_on_publish_text'] : '';
646
	$ppp_share_on_publish_include_image = ( isset( $_REQUEST['_ppp_share_on_publish_include_image'] ) ) ? $_REQUEST['_ppp_share_on_publish_include_image'] : '';
647
648
649
	update_post_meta( $post_id, '_ppp_share_on_publish', $ppp_share_on_publish );
650
	update_post_meta( $post_id, '_ppp_share_on_publish_text', $ppp_share_on_publish_text );
651
	update_post_meta( $post_id, '_ppp_share_on_publish_include_image', $ppp_share_on_publish_include_image );
652
653
	$tweet_data = isset( $_REQUEST['_ppp_tweets'] ) ? $_REQUEST['_ppp_tweets'] : array();
654
	foreach ( $tweet_data as $index => $tweet ) {
655
		$tweet_data[ $index ]['text'] = sanitize_text_field( $tweet['text'] );
656
	}
657
	update_post_meta( $post_id, '_ppp_tweets', $tweet_data );
658
659
}
660
add_action( 'save_post', 'ppp_tw_save_post_meta_boxes', 10, 2 ); // save the custom fields
661
662
/**
663
 * Determines if the post should be shared on publish
664
 * @param  string $old_status The old post status
665
 * @param  string $new_status The new post status
666
 * @param  object $post       The Post Object
667
 * @return void               Shares the post
668
 */
669
function ppp_tw_share_on_publish( $new_status, $old_status, $post ) {
0 ignored issues
show
Unused Code introduced by
The parameter $new_status is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $old_status is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
670
	global $ppp_options;
671
672
	$from_meta = ! empty( $_POST['ppp_post_edit'] ) ? false : get_post_meta( $post->ID, '_ppp_share_on_publish', true );
673
	$from_post = isset( $_POST['_ppp_share_on_publish'] );
674
675
	if ( empty( $from_meta ) && empty( $from_post ) ) {
676
		return;
677
	}
678
679
	// Determine if we're seeing the share on publish in meta or $_POST
680
	if ( $from_meta && !$from_post ) {
681
		$ppp_share_on_publish_text = get_post_meta( $post->ID, '_ppp_share_on_publish_text', true );
682
		$use_media = get_post_meta( $post->ID, '_ppp_share_on_publish_include_image', true );
683
	} else {
684
		$ppp_share_on_publish_text = isset( $_POST['_ppp_share_on_publish_text'] ) ? $_POST['_ppp_share_on_publish_text'] : '';
685
		$use_media = isset( $_POST['_ppp_share_on_publish_include_image'] ) ? $_POST['_ppp_share_on_publish_include_image'] : false;
686
	}
687
688
	$share_content = ( !empty( $ppp_share_on_publish_text ) ) ? $ppp_share_on_publish_text : ppp_tw_generate_share_content( $post->ID, null, false );
689
	$share_content = apply_filters( 'ppp_share_content', $share_content, array( 'post_id' => $post->ID ) );
690
	$name = 'sharedate_0_' . $post->ID;
691
	$media = ppp_post_has_media( $post->ID, 'tw', $use_media );
692
	$share_link = ppp_generate_link( $post->ID, $name, true );
693
694
	$status = ppp_send_tweet( $share_content . ' ' . $share_link, $post->ID, $media );
695
696
	$log_title = ppp_tw_build_share_message( $post->ID, $name, false, false );
697
698
	$log_data = array(
699
		'post_title'    => $log_title,
700
		'post_content'  =>  json_encode( $status ),
701
		'post_parent'   => $post->ID,
702
		'log_type'      => 'ppp_share'
703
	);
704
705
	$log_meta = array(
706
		'network'   => 'tw',
707
		'share_id'  => 0,
708
	);
709
710
	$log_entry = WP_Logging::insert_log( $log_data, $log_meta );
0 ignored issues
show
Unused Code introduced by
$log_entry is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
711
712
	if ( ! empty( $status->id_str ) ) {
713
		$author_id = $post->post_author;
714
		$author_rt = get_user_meta( $author_id, '_ppp_share_on_publish', true );
715
716
		if ( $author_rt ) {
717
			$twitter_user = new PPP_Twitter_User( $author_id );
718
			$twitter_user->retweet( $status->id_str );
719
		}
720
721
		/* Get an array of users who should retweet if another author has a post published
722
		 * Exclude the author that shared earlier to avoid duplicate retweets.
723
		 */
724
		$args = array(
725
			'meta_query' => array(
726
				array(
727
					'key' 		=> '_ppp_share_others_on_publish',
728
					'value'		=> true,
729
					'compare'	=> '='
730
				),
731
			),
732
			'fields'	=> array( 'ID' ),
733
			'exclude'	=> array( $author_id )
734
		);
735
		$other_rt = get_users( $args );
736
737
		if ( $other_rt ){
738
			foreach ( $other_rt as $user ) {
739
				$twitter_user = new PPP_Twitter_User( $user->ID );
740
				$twitter_user->retweet( $status->id_str );
741
			}
742
		}
743
744
	}
745
}
746
add_action( 'ppp_share_on_publish', 'ppp_tw_share_on_publish', 10, 3 );
747
748
/**
749
 * Generate the timestamps and names for the scheduled Twitter shares
750
 *
751
 * @since  2.3
752
 * @param  array $times   The times to save
753
 * @param  int   $post_id The Post ID of the item being saved
754
 * @return array          Array of timestamps and cron names
755
 */
756
function ppp_tw_generate_timestamps( $times, $post_id ) {
757 1
	$ppp_tweets = get_post_meta( $post_id, '_ppp_tweets', true );
758
759 1
	if ( empty( $ppp_tweets ) ) {
760 1
		$ppp_tweets = array();
761 1
	}
762
763 1
	foreach ( $ppp_tweets as $key => $data ) {
764 1
		if ( ! array_filter( $data ) ) {
765
			continue;
766
		}
767
768 1
		$timestamp = ppp_generate_timestamp( $data['date'], $data['time'] );
769
770 1
		if ( $timestamp > current_time( 'timestamp', 1 ) ) { // Make sure the timestamp we're getting is in the future
771 1
			$time_key           = strtotime( date_i18n( 'd-m-Y H:i:s', $timestamp , true ) ) . '_tw';
772 1
			$times[ $time_key ] = 'sharedate_' . $key . '_' . $post_id . '_tw';
773 1
		}
774
775 1
	}
776
777 1
	return $times;
778
}
779
add_filter( 'ppp_get_timestamps', 'ppp_tw_generate_timestamps', 10, 2 );
780
781
/**
782
 * Returns if the Twitter Cards are enabled
783
 *
784
 * @since  2.2
785
 * @return bool If the user has checked to enable Twitter cards
786
 */
787
function ppp_tw_cards_enabled() {
788
	global $ppp_share_settings;
789
790
	$ret = false;
791
792
	if ( ! empty( $ppp_share_settings['twitter']['cards_enabled'] ) ) {
793
		$ret = true;
794
	}
795
796
	return apply_filters( 'ppp_tw_cards_enabled', $ret );
797
}
798
799
/**
800
 * Output the Twitter Card Meta
801
 *
802
 * @since  2.2
803
 * @return void
804
 */
805
function ppp_tw_card_meta() {
806
807
	if ( ! is_single() || ! ppp_twitter_enabled() || ! ppp_tw_cards_enabled() ) {
808
		return;
809
	}
810
811
	global $post, $ppp_options;
812
813
	if ( ! array_key_exists( $post->post_type, $ppp_options['post_types'] ) ) {
814
		return;
815
	}
816
817
	echo ppp_tw_get_cards_meta();
818
}
819
add_action( 'wp_head', 'ppp_tw_card_meta', 10 );
820
821
/**
822
 * Generates the Twitter Card Content
823
 *
824
 * @since  2.2
825
 * @return string The Twitter card Meta tags
826
 */
827
function ppp_tw_get_cards_meta() {
828
829
	$return = '';
830
831
	if ( ! is_single() || ! ppp_tw_cards_enabled() ) {
832
		return $return;
833
	}
834
835
	global $post, $ppp_social_settings;
836
837
838
	if ( empty( $post ) ) {
839
		return;
840
	}
841
842
	$elements = ppp_tw_default_meta_elements();
843
	foreach ( $elements as $name => $content ) {
844
		$return .= '<meta name="' . $name . '" content="' . $content . '" />' . "\n";
845
	}
846
847
	return apply_filters( 'ppp_tw_card_meta', $return );
848
849
}
850
851
/**
852
 * Sets an array of names and content for Twitter Card Meta
853
 * for easy filtering by devs
854
 *
855
 * @since  2.2
856
 * @return array The array of keys and values for the Twitter Meta
857
 */
858
function ppp_tw_default_meta_elements() {
859
	global $post, $ppp_social_settings;
860
861
	$elements = array();
862
863
	$image_url = ppp_post_has_media( $post->ID, 'tw', true );
864
	if ( $image_url ) {
865
		$elements['twitter:card']      = 'summary_large_image';
866
		$elements['twitter:image:src'] = $image_url;
867
868
		$thumb_id = ppp_get_attachment_id_from_image_url( $image_url );
869
		if ( ! empty( $thumb_id ) ) {
870
			$alt_text = ppp_get_attachment_alt_text( $thumb_id );
871
			// When adding media via the WP Uploader, any 'alt text' supplied will be used as the accessible alt text.
872
			if ( ! empty( $alt_text ) ) {
873
				$elements['twitter:image:alt'] = esc_attr( $alt_text );
874
			}
875
		}
876
	} else {
877
		$elements['twitter:card'] = 'summary';
878
	}
879
880
	$elements['twitter:site']        = '@' . $ppp_social_settings['twitter']['user']->screen_name;
881
	$elements['twitter:title']       = esc_attr( strip_tags( $post->post_title ) );
882
	$elements['twitter:description'] = esc_attr( ppp_tw_get_card_description() );
883
884
	$author_twitter_handle = get_user_meta( $post->post_author, 'twitter', true );
885
	if ( ! empty( $author_twitter_handle ) ) {
886
887
		if ( strpos( $author_twitter_handle, '@' ) === false ) {
888
			$author_twitter_handle = '@' . $author_twitter_handle;
889
		}
890
891
		$elements['twitter:creator'] = esc_attr( strip_tags( $author_twitter_handle ) );
892
893
	}
894
895
	return apply_filters( 'ppp_tw_card_elements', $elements );
896
}
897
898
/**
899
 * Given a post, will give the post excerpt or the truncated post content to fit in a Twitter Card
900
 *
901
 * @since  2.2
902
 * @return string The post excerpt/description
903
 */
904
function ppp_tw_get_card_description() {
905
	global $post;
906
907
	if ( ! is_single() || empty( $post ) ) {
908
		return false;
909
	}
910
911
	$excerpt = $post->post_excerpt;
912
913
	if ( empty( $excerpt ) ) {
914
		$excerpt = ppp_tw_format_card_description( $post->post_content );
915
	}
916
917
	return apply_filters( 'ppp_tw_card_desc', $excerpt );
918
}
919
920
/**
921
 * Format a given string for the excerpt
922
 *
923
 * @since  2.3
924
 * @param  string $excerpt The string to possibly truncate
925
 * @return string          The description, truncated if over 200 characters
926
 */
927
function ppp_tw_format_card_description( $excerpt ) {
928 1
	$max_len = apply_filters( 'ppp_tw_cart_desc_length', 200 );
929 1
	$excerpt = strip_tags( $excerpt );
930
931 1
	if ( strlen( $excerpt ) > $max_len ) {
932
		$excerpt_pre = substr( $excerpt, 0, $max_len );
933
		$last_space  = strrpos( $excerpt_pre, ' ' );
934
		$excerpt     = substr( $excerpt_pre, 0, $last_space ) . '...';
935
	}
936
937 1
	return $excerpt;
938
}
939
940
/**
941
 * Add a Twitter method to the profile editor
942
 *
943
 * @since  2.2.4
944
 * @param  array $user_contactmethods List of user contact methods for the profile editor
945
 * @return array                      List of contact methods with twitter added
946
 */
947
function ppp_tw_add_contact_method( $user_contactmethods ) {
948
949
	if ( ! isset( $user_contactmethods['twitter'] ) ) {
950
		$user_contactmethods['twitter'] = __( 'Twitter', 'ppp-txt' );
951
	}
952
	// Returns the contact methods
953
	return $user_contactmethods;
954
}
955
add_filter( 'user_contactmethods', 'ppp_tw_add_contact_method' );
956
957
958
/**
959
 * Adds in the Post Promoter Pro Preferences Profile Section
960
 *
961
 * @since  2.2.7
962
 * @param  object $user The User object being viewed
963
 * @return void         Displays HTML
964
 */
965
function ppp_tw_profile_settings( $user ) {
966
	global $ppp_social_settings;
967
968
	if ( $user->ID == get_current_user_id() && ! current_user_can( 'edit_posts' ) ) {
969
		return;
970
	}
971
972
	if ( $user->ID !== get_current_user_id() && ! current_user_can( PostPromoterPro::get_manage_capability() ) ) {
973
		return;
974
	}
975
976
	if ( ! isset( $ppp_social_settings['twitter'] ) || is_null( $ppp_social_settings['twitter'] ) ) {
977
		return;
978
	}
979
980
	$connected = false;
981
	?>
982
	<h3><?php _e( 'Post Promoter Pro', 'ppp-txt' ); ?></h3>
983
	<table class="form-table">
984
		<tr>
985
			<th><?php _e( 'Connect to Twitter', 'ppp-txt' ); ?></th>
986
			<td>
987
			<?php
988
			$twitter = new PPP_Twitter_User( get_current_user_id() );
989
			$tw_user = get_user_meta( $user->ID, '_ppp_twitter_data', true );
990
			if ( empty( $tw_user ) ) {
991
				$tw_authurl = $twitter->get_auth_url( admin_url( 'user-edit.php?user_id=' . $user->ID ) );
992
993
				echo '<a target="_blank" href="' . $tw_authurl . '"><img src="' . PPP_URL . '/includes/images/sign-in-with-twitter-gray.png" /></a>';
994
			} else {
995
				$connected = true;
996
				?>
997
				<p><strong><?php _e( 'Signed in as', 'ppp-txt' ); ?>: </strong><?php echo $tw_user['user']->screen_name; ?></p>
998
				<p>
999
					<a class="button-primary" href="<?php echo admin_url( 'user-edit.php?user_id=' . $user->ID . '&ppp_social_disconnect=true&ppp_network=twitter&user_id=' . $user->ID ); ?>" ><?php _e( 'Disconnect from Twitter', 'ppp-txt' ); ?></a>&nbsp;
1000
					<a class="button-secondary" href="https://twitter.com/settings/applications" target="blank"><?php _e( 'Revoke Access via Twitter', 'ppp-txt' ); ?></a>
1001
				</p>
1002
				<?php
1003
			}
1004
			?>
1005
			</td>
1006
		</tr>
1007
1008
		<?php if ( $connected ) : ?>
1009
		<?php
1010
			$share_on_publish 			= get_user_meta( $user->ID, '_ppp_share_on_publish', true );
1011
			$share_scheduled  			= get_user_meta( $user->ID, '_ppp_share_scheduled' , true );
1012
			$share_others_on_publish 	= get_user_meta( $user->ID, '_ppp_share_others_on_publish', true );
1013
			$share_others_scheduled  	= get_user_meta( $user->ID, '_ppp_share_others_scheduled' , true );
1014
		?>
1015
		<tr>
1016
			<th><?php _e( 'Sharing Options', 'ppp-txt' ); ?></th>
1017
			<td>
1018
				<input type="checkbox" <?php checked( true, $share_on_publish, true ); ?>name="share_on_publish" value="1" id="share-on-publish" /> <label for="share-on-publish"><?php _e( 'Retweet my posts when they are published', 'ppp-txt' ); ?></label>
1019
				<p class="description"><?php printf( __( 'Retweet the primary account as %s when it Tweets on publishing my posts.', 'ppp-txt' ), $tw_user['user']->screen_name ); ?></p>
1020
			</td>
1021
		</tr>
1022
		<tr>
1023
			<th></th>
1024
			<td>
1025
				<input type="checkbox" <?php checked( true, $share_scheduled, true ); ?> name="share_scheduled" value="1" id="share-scheduled" /> <label for="share-scheduled"><?php _e( 'Retweet scheduled shares of my posts', 'ppp-txt' ); ?></label>
1026
				<p class="description"><?php printf( __( 'When the primary account schedules a Tweet for one of my posts, Retweet it as %s.', 'ppp-txt' ), $tw_user['user']->screen_name ); ?></p>
1027
			</td>
1028
		</tr>
1029
		<tr>
1030
			<th></th>
1031
			<td>
1032
				<input type="checkbox" <?php checked( true, $share_others_on_publish, true ); ?> name="share_others_on_publish" value="1" id="share-others-on-publish" /> <label for="share-others-on-publish"><?php _e( 'Retweet other author\'s posts when they are published', 'ppp-txt' ); ?></label>
1033
				<p class="description"><?php printf( __( 'Retweet the primary account as %s when it Tweets on publishing another author\'s posts.', 'ppp-txt' ), $tw_user['user']->screen_name ); ?></p>
1034
			</td>
1035
		</tr>
1036
		<tr>
1037
			<th></th>
1038
			<td>
1039
				<input type="checkbox" <?php checked( true, $share_others_scheduled, true ); ?> name="share_others_scheduled" value="1" id="share-others-scheduled" /> <label for="share-others-scheduled"><?php _e( 'Retweet scheduled shares of other author\'s posts', 'ppp-txt' ); ?></label>
1040
				<p class="description"><?php printf( __( 'When the primary account schedules a Tweet for another author\'s post, Retweet it as %s.', 'ppp-txt' ), $tw_user['user']->screen_name ); ?></p>
1041
			</td>
1042
		</tr>
1043
1044
		<?php endif; ?>
1045
	</table>
1046
	<?php
1047
}
1048
add_action( 'show_user_profile', 'ppp_tw_profile_settings' );
1049
add_action( 'edit_user_profile', 'ppp_tw_profile_settings' );
1050
1051
/**
1052
 * Saves the User Profile Settings
1053
 *
1054
 * @since  2.2.7
1055
 * @param  int $user_id The User ID being saved
1056
 * @return void         Saves to Usermeta
1057
 */
1058
function ppp_tw_save_profile( $user_id ) {
1059
	global $ppp_social_settings;
1060
1061
	if ( ! isset( $ppp_social_settings['twitter'] ) || is_null( $ppp_social_settings['twitter'] ) ) {
1062
		return;
1063
	}
1064
1065
	$share_on_publish 			= ! empty( $_POST['share_on_publish'] ) ? true : false;
1066
	$share_scheduled  			= ! empty( $_POST['share_scheduled'] )  ? true : false;
1067
	$share_others_on_publish 	= ! empty( $_POST['share_others_on_publish'] ) ? true : false;
1068
	$share_others_scheduled  	= ! empty( $_POST['share_others_scheduled'] )  ? true : false;
1069
1070
	update_user_meta( $user_id, '_ppp_share_on_publish', $share_on_publish );
1071
	update_user_meta( $user_id, '_ppp_share_scheduled', $share_scheduled  );
1072
	update_user_meta( $user_id, '_ppp_share_others_on_publish', $share_others_on_publish );
1073
	update_user_meta( $user_id, '_ppp_share_others_scheduled', $share_others_scheduled  );
1074
1075
}
1076
add_action( 'personal_options_update', 'ppp_tw_save_profile' );
1077
add_action( 'edit_user_profile_update', 'ppp_tw_save_profile' );
1078
1079
function ppp_tw_calendar_on_publish_event( $events, $post_id ) {
1080
	$share_on_publish = get_post_meta( $post_id, '_ppp_share_on_publish', true );
1081
1082
	if ( ! empty( $share_on_publish ) ) {
1083
		$share_text = get_post_meta( $post_id, '_ppp_share_on_publish_text', true );
1084
		$events[] = array(
1085
			'id' => $post_id . '-share-on-publish',
1086
			'title' => ( ! empty( $share_text ) ) ? $share_text : ppp_tw_generate_share_content( $post_id, null, false ),
1087
			'start'     => date_i18n( 'Y-m-d/TH:i:s', strtotime( get_the_date( null, $post_id ) . ' ' . get_the_time( null, $post_id ) ) + 1 ),
1088
			'end'       => date_i18n( 'Y-m-d/TH:i:s', strtotime( get_the_date( null, $post_id ) . ' ' . get_the_time( null, $post_id ) ) + 1 ),
1089
			'className' => 'ppp-calendar-item-tw cal-post-' . $post_id,
1090
			'belongsTo' => $post_id,
1091
		);
1092
	}
1093
1094
	return $events;
1095
}
1096
add_filter( 'ppp_calendar_on_publish_event', 'ppp_tw_calendar_on_publish_event', 10, 2 );
1097
1098
function ppp_tw_get_post_shares( $items, $post_id ) {
1099
	$tweets = get_post_meta( $post_id, '_ppp_tweets', true );
1100
	if ( empty( $tweets ) ) { return $items; }
1101
1102
	foreach ( $tweets as $key => $tweet ) {
1103
		$items[] = array( 'id' => $key, 'service' => 'tw' );
1104
	}
1105
	return $items;
1106
}
1107
add_filter( 'ppp_get_post_scheduled_shares', 'ppp_tw_get_post_shares', 10, 2 );
1108