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
03:29
created

twitter-functions.php ➔ ppp_tw_capture_pin_auth()   B

Complexity

Conditions 8
Paths 64

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
nc 64
nop 0
dl 0
loc 43
ccs 0
cts 29
cp 0
crap 72
rs 7.9875
c 0
b 0
f 0
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
	if ( ! empty( $_POST['ppp_user_auth'] ) ) {
124
		$twitter = new PPP_Twitter;
125
		$twitter->ppp_initialize_twitter();
126
		$settings = get_option( 'ppp_social_settings', true );
127
128
		if ( ! empty( $settings['twitter']['user']->id ) ) {
129
			echo 1;
130
		} else {
131
			echo 0;
132
		}
133
	} else {
134
		$twitter = new PPP_Twitter_User( get_current_user_id() );
135
		$twitter->init();
136
137
		$user = get_user_meta( get_current_user_id(), '_ppp_twitter_data', true );
138
		if ( ! empty( $user['user']->id ) ) {
139
			echo 1;
140
		} else {
141
			echo 0;
142
		}
143
	}
144
145
	die(); // this is required to return a proper result
146
}
147
add_action( 'wp_ajax_ppp_tw_auth_pin', 'ppp_tw_capture_pin_auth' );
148
149
/**
150
 * Listen for the oAuth tokens and verifiers from Twitter when in admin
151
 * @return void
152
 */
153
function ppp_capture_twitter_oauth() {
154
	if ( isset( $_REQUEST['oauth_verifier'] ) && isset( $_REQUEST['oauth_token'] ) ) {
155
		$current_screen = get_current_screen();
156
		if ( 'user-edit' === $current_screen->base ) {
157
			$user_id = ! empty( $_GET['user_id'] ) && is_numeric( $_GET['user_id'] ) ? $_GET['user_id'] : false;
158
			$twitter = new PPP_Twitter_User( $user_id );
159
			$twitter->init();
160
			$redirect = admin_url( 'user-edit.php?updated=1&user_id=' . $user_id );
161
		} else {
162
			global $ppp_twitter_oauth;
163
			$ppp_twitter_oauth->ppp_initialize_twitter();
164
			$redirect = admin_url( 'admin.php?page=ppp-social-settings' );
165
		}
166
		?>
167
		<meta http-equiv="refresh" content="0;URL=<?php echo $redirect; ?>">
168
		<?php
169
	}
170
}
171
//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...
172
173
/**
174
 * Listen for the disconnect from Twitter
175
 * @return void
176
 */
177
function ppp_disconnect_twitter() {
178
	if ( ! empty( $_GET['user_id'] ) ) {
179
		$user_id = (int) sanitize_text_field( $_GET['user_id'] );
180
		if ( $user_id !== get_current_user_id() || ! current_user_can( PostPromoterPro::get_manage_capability() ) ) {
181
			wp_die( __( 'Unable to disconnect Twitter account', 'ppp-txt' ) );
182
		}
183
		delete_user_meta( $user_id, '_ppp_twitter_data' );
184
	} else {
185
		global $ppp_social_settings;
186
		$ppp_social_settings = get_option( 'ppp_social_settings' );
187
		if ( isset( $ppp_social_settings['twitter'] ) ) {
188
			unset( $ppp_social_settings['twitter'] );
189
			update_option( 'ppp_social_settings', $ppp_social_settings );
190
		}
191
	}
192
}
193
add_action( 'ppp_disconnect-twitter', 'ppp_disconnect_twitter', 10 );
194
195
/**
196
 * Given a message, sends a tweet
197
 * @param  string $message The Text to share as the body of the tweet
198
 * @return object          The Results from the Twitter API
199
 */
200
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...
201
	global $ppp_twitter_oauth;
202
203
	return apply_filters( 'ppp_twitter_tweet', $ppp_twitter_oauth->ppp_tweet( ppp_entities_and_slashes( $message ), $use_media ) );
204
}
205
206
/**
207
 * Send out a scheduled share to Twitter
208
 *
209
 * @since  2.3
210
 * @param  integer $post_id The Post ID to share fore
211
 * @param  integer $index   The index in the shares
212
 * @param  string  $name    The name of the Cron
213
 * @return void
214
 */
215
function ppp_tw_scheduled_share( $post_id = 0, $index = 1, $name = '' ) {
216
	global $ppp_options, $wp_logs, $wp_filter;
217
218
	$post_meta     = get_post_meta( $post_id, '_ppp_tweets', true );
219
	$this_share    = $post_meta[ $index ];
220
	$attachment_id = isset( $this_share['attachment_id'] ) ? $this_share['attachment_id'] : false;
221
222
	$share_message = ppp_tw_build_share_message( $post_id, $name );
223
224
	if ( empty( $attachment_id ) && ! empty( $this_share['image'] ) ) {
225
		$media = $this_share['image'];
226
	} else {
227
		$use_media = ppp_tw_use_media( $post_id, $index );
228
		$media     = ppp_post_has_media( $post_id, 'tw', $use_media, $attachment_id );
229
	}
230
231
	$status = ppp_send_tweet( $share_message, $post_id, $media );
232
233
	$log_title = ppp_tw_build_share_message( $post_id, $name, false, false );
234
235
	$log_data = array(
236
		'post_title'    => $log_title,
237
		'post_content'  => '',
238
		'post_parent'   => $post_id,
239
		'log_type'      => 'ppp_share'
240
	);
241
242
	$log_meta = array(
243
		'network'   => 'tw',
244
		'share_id'  => $index,
245
	);
246
247
	$log_entry = WP_Logging::insert_log( $log_data, $log_meta );
248
249
	update_post_meta( $log_entry, '_ppp_share_status', $status );
250
251
	if ( ! empty( $status->id_str ) ) {
252
		$post      = get_post( $post_id );
253
		$author_id = $post->post_author;
254
		$author_rt = get_user_meta( $author_id, '_ppp_share_scheduled', true );
255
256
		if ( $author_rt ) {
257
			$twitter_user = new PPP_Twitter_User( $author_id );
258
			$twitter_user->retweet( $status->id_str );
259
		}
260
261
		/* Get an array of users who should retweet if another author has a scheduled share
262
		 * Exclude the author that retweeted earlier to avoid duplicate retweets.
263
		 */
264
		$args = array(
265
			'meta_query' => array(
266
				array(
267
					'key' 		=> '_ppp_share_others_scheduled',
268
					'value'		=> true,
269
					'compare'	=> '='
270
				),
271
			),
272
			'fields'	=> array( 'ID' ),
273
			'exclude'	=> array( $author_id )
274
		);
275
		$other_rt = get_users( $args );
276
277
		if ( $other_rt ){
278
			foreach ( $other_rt as $user ) {
279
				$twitter_user = new PPP_Twitter_User( $user->ID );
280
				$twitter_user->retweet( $status->id_str );
281
			}
282
		}
283
	}
284
}
285
add_action( 'ppp_share_scheduled_tw', 'ppp_tw_scheduled_share', 10, 3 );
286
287
/**
288
 * Combines the results from ppp_generate_share_content and ppp_generate_link into a single string
289
 * @param  int $post_id The Post ID
290
 * @param  string $name    The 'name' element from the Cron
291
 * @param  boolean $scheduled If the item is being requsted by a scheduled post
292
 * @param  bool $include_link If a link should be included in the text
293
 * @return string          The Full text for the social share
294
 */
295
function ppp_tw_build_share_message( $post_id, $name, $scheduled = true, $include_link = true ) {
296
	$share_content = ppp_tw_generate_share_content( $post_id, $name, $scheduled );
297
298
	if ( $include_link ) {
299
		$share_link    = ppp_generate_link( $post_id, $name, $scheduled );
300
		$share_content = $share_content . ' ' . $share_link;
301
	}
302
303
	return apply_filters( 'ppp_tw_build_share_message', $share_content );
304
}
305
306
/**
307
 * Generate the content for the shares
308
 * @param  int $post_id The Post ID
309
 * @param  string $name    The 'Name' from the cron
310
 * @return string          The Content to include in the social media post
311
 */
312
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...
313
	global $ppp_options;
314
	$default_text = isset( $ppp_options['default_text'] ) ? $ppp_options['default_text'] : '';
315
	$ppp_tweets   = get_post_meta( $post_id, '_ppp_tweets', true );
316
317
	if ( ! empty( $ppp_tweets ) ) {
318
		$name_array    = explode( '_', $name );
319
		$index         = $name_array[1];
320
		if ( isset( $ppp_tweets[ $index ] ) ) {
321
			$share_content = $ppp_tweets[ $index ]['text'];
322
		}
323
	}
324
325
	// If an override was found, use it, otherwise try the default text content
326
	$share_content = ( isset( $share_content ) && !empty( $share_content ) ) ? $share_content : $default_text;
327
328
	// If the content is still empty, just use the post title
329
	$share_content = ( isset( $share_content ) && !empty( $share_content ) ) ? $share_content : get_the_title( $post_id );
330
331
	return apply_filters( 'ppp_share_content', $share_content, array( 'post_id' => $post_id ) );
332
}
333
334
/**
335
 * Return if media is supported for this scheduled tweet
336
 * @param  int $post_id The Post ID
337
 * @param  int $index   The index of this tweet in the _ppp_tweets data
338
 * @return bool         Whether or not this tweet should contain a media post
339
 */
340
function ppp_tw_use_media( $post_id, $index ) {
341
	if ( empty( $post_id ) || empty( $index ) ) {
342
		return false;
343
	}
344
345
	$share_data = get_post_meta( $post_id, '_ppp_tweets', true );
346
	$use_media  = ! empty( $share_data[$index]['attachment_id'] ) || ! empty( $share_data[$index]['image'] ) ? true : false;
347
348
	return $use_media;
349
}
350
351
/**
352
 * Sets the constants for the oAuth tokens for Twitter
353
 * @param  array $social_tokens The tokens stored in the transient
354
 * @return void
355
 */
356
function ppp_set_tw_token_constants( $social_tokens ) {
357
	if ( !empty( $social_tokens ) && property_exists( $social_tokens, 'twitter' ) ) {
358
		define( 'PPP_TW_CONSUMER_KEY', $social_tokens->twitter->consumer_token );
359
		define( 'PPP_TW_CONSUMER_SECRET', $social_tokens->twitter->consumer_secret );
360
	}
361
}
362
add_action( 'ppp_set_social_token_constants', 'ppp_set_tw_token_constants', 10, 1 );
363
364
/**
365
 * Register Twitter for the Social Media Accounts section
366
 * @param  array $tabs Array of existing tabs
367
 * @return array       The Array of existing tabs with Twitter added
368
 */
369
function ppp_tw_add_admin_tab( $tabs ) {
370
	$tabs['tw'] = array( 'name' => __( 'Twitter', 'ppp-txt' ), 'class' => 'icon-ppp-tw' );
371
372
	return $tabs;
373
}
374
add_filter( 'ppp_admin_tabs', 'ppp_tw_add_admin_tab', 10, 1 );
375
376
/**
377
 * Register the Twitter connection area for the Social Media Accounts section
378
 * @param  array $content The existing content tokens
379
 * @return array          The content tokens with Twitter added
380
 */
381
function ppp_tw_register_admin_social_content( $content ) {
382
	$content[] = 'tw';
383
384
	return $content;
385
}
386
add_filter( 'ppp_admin_social_content', 'ppp_tw_register_admin_social_content', 10, 1 );
387
388
/**
389
 * Register the Twitter metabox tab
390
 * @param  array $tabs The tabs
391
 * @return array       The tabs with Twitter added
392
 */
393
function ppp_tw_add_meta_tab( $tabs ) {
394
	global $ppp_social_settings;
395
	if ( !isset( $ppp_social_settings['twitter'] ) ) {
396
		return $tabs;
397
	}
398
399
	$tabs['tw'] = array( 'name' => __( 'Twitter', 'ppp-txt' ), 'class' => 'icon-ppp-tw' );
400
401
	return $tabs;
402
}
403
add_filter( 'ppp_metabox_tabs', 'ppp_tw_add_meta_tab', 10, 1 );
404
405
/**
406
 * Register the metabox content for Twitter
407
 * @param  array $content The existing metabox tokens
408
 * @return array          The metabox tokens with Twitter added
409
 */
410
function ppp_tw_register_metabox_content( $content ) {
411
	global $ppp_social_settings;
412
	if ( !isset( $ppp_social_settings['twitter'] ) ) {
413
		return $content;
414
	}
415
416
	$content[] = 'tw';
417
418
	return $content;
419
}
420
add_filter( 'ppp_metabox_content', 'ppp_tw_register_metabox_content', 10, 1 );
421
422
/**
423
 * Returns the stored Twitter data for a post
424
 *
425
 * @since  2.3
426
 * @param  array $post_meta Array of meta data (empty)
427
 * @param  int   $post_id   The Post ID to get the meta for
428
 * @return array            The stored Twitter shares for a post
429
 */
430
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...
431
	return get_post_meta( $post_id, '_ppp_tweets', true );
432
}
433
add_filter( 'ppp_get_scheduled_items_tw', 'ppp_tw_get_post_meta', 10, 2 );
434
435
/**
436
 * Registers the thumbnail size for Twitter
437
 * @return void
438
 */
439
function ppp_tw_register_thumbnail_size() {
440
	add_image_size( 'ppp-tw-share-image', 1024, 512, true );
441
}
442
add_action( 'ppp_add_image_sizes', 'ppp_tw_register_thumbnail_size' );
443
444
/**
445
 * The callback that adds Twitter metabox content
446
 * @param  object $post The post object
447
 * @return void         Displays the metabox content
448
 */
449
function ppp_tw_add_metabox_content( $post ) {
450
	global $ppp_options, $ppp_share_settings, $has_past_shares;
451
	$has_past_shares = 0;
452
	?>
453
		<p>
454
			<div class="ppp-post-override-wrap">
455
				<p><h3><?php _e( 'Share on Twitter', 'ppp-txt' ); ?></h3></p>
456
				<div id="ppp-tweet-fields" class="ppp-tweet-fields">
457
					<div id="ppp-tweet-fields" class="ppp-meta-table-wrap">
458
						<table class="widefat ppp-repeatable-table" width="100%" cellpadding="0" cellspacing="0">
459
							<thead>
460
								<tr>
461
									<th style="width: 100px"><?php _e( 'Date', 'ppp-txt' ); ?></th>
462
									<th style="width: 75px;"><?php _e( 'Time', 'ppp-txt' ); ?></th>
463
									<th><?php _e( 'Text', 'ppp-txt' ); ?></th>
464
									<th style"width: 200px;"><?php _e( 'Image', 'ppp-txt' ); ?></th>
465
									<th style="width: 30px;"></th>
466
								</tr>
467
							</thead>
468
							<tbody>
469
								<?php ppp_render_tweet_share_on_publish_row(); ?>
470
								<?php $tweets = get_post_meta( $post->ID, '_ppp_tweets', true ); ?>
471
								<?php if ( ! empty( $tweets ) ) : ?>
472
473
									<?php foreach ( $tweets as $key => $value ) :
474
										$date          = isset( $value['date'] )          ? $value['date']          : '';
475
										$time          = isset( $value['time'] )          ? $value['time']          : '';
476
										$text          = isset( $value['text'] )          ? $value['text']          : '';
477
										$image         = isset( $value['image'] )         ? $value['image']         : '';
478
										$attachment_id = isset( $value['attachment_id'] ) ? $value['attachment_id'] : '';
479
480
										$args = apply_filters( 'ppp_tweet_row_args', compact( 'date','time','text','image','attachment_id' ), $value );
481
										?>
482
483
										<?php ppp_render_tweet_row( $key, $args, $post->ID ); ?>
484
485
486
									<?php endforeach; ?>
487
488
									<?php
489
									if ( ! empty( $has_past_shares ) && count ( $tweets ) == $has_past_shares ) {
490
										$args = array(
491
											'date'          => '',
492
											'time'          => '',
493
											'text'          => '',
494
											'image'         => '',
495
											'attachment_id' => '',
496
										);
497
498
499
										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 473. 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...
500
									}
501
									?>
502
503
								<?php else: ?>
504
505
									<?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...
506
507
								<?php endif; ?>
508
509
								<tr class="ppp-add-repeatable-wrapper">
510
									<td class="submit" colspan="4" style="float: none; clear:both; background:#fff;">
511
										<a class="button-secondary ppp-add-repeatable" style="margin: 6px 0;"><?php _e( 'Add New Tweet', 'ppp-txt' ); ?></a>
512
										<?php if ( ! empty( $has_past_shares ) ) : ?>
513
											<a class="button-secondary ppp-view-all" style="margin: 6px 0;"><?php _e( 'Toggle Past Tweets', 'ppp-txt' ); ?></a>
514
										<?php endif; ?>
515
									</td>
516
								</tr>
517
							</tbody>
518
						</table>
519
					</div>
520
				</div><!--end #edd_variable_price_fields-->
521
522
				<p><?php _e( 'Do not include links in your text, this will be added automatically.', 'ppp-txt' ); ?></p>
523
				<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>
524
			</div>
525
		</p>
526
	<?php
527
}
528
add_action( 'ppp_generate_metabox_content-tw', 'ppp_tw_add_metabox_content', 10, 1 );
529
530
/**
531
 * Generates the 'share on publish row' of the Twitter Metabox content
532
 *
533
 * @since  2.3
534
 * @return void
535
 */
536
function ppp_render_tweet_share_on_publish_row() {
537
	global $post, $ppp_share_settings;
538
	$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...
539
540
	$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...
541
542
	$ppp_share_on_publish  = get_post_meta( $post->ID, '_ppp_share_on_publish', true );
543
	$show_share_on_publish = false;
544
545
	$share_by_default      = empty( $ppp_share_settings['share_on_publish'][ $post->post_type ]['twitter'] ) ? false : true;
546
547
	if ( $ppp_share_on_publish == '1' || ( $ppp_share_on_publish == '' && $share_by_default ) ) {
548
		$show_share_on_publish = true;
549
	}
550
551
	$ppp_share_on_publish_text          = get_post_meta( $post->ID, '_ppp_share_on_publish_text', true );
552
	$ppp_share_on_publish_include_image = get_post_meta( $post->ID, '_ppp_share_on_publish_include_image', true );
553
554
	$disabled = ( $post->post_status === 'publish' && time() > strtotime( $post->post_date ) ) ? true : false;
555
	?>
556
	<tr class="ppp-tweet-wrapper ppp-repeatable-row on-publish-row">
557
		<td colspan="2" class="ppp-on-plublish-date-column">
558
			<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 ); ?> />
559
			&nbsp;<label for="ppp_share_on_publish"><?php _e( 'Tweet On Publish', 'ppp-txt' ); ?></label>
560
		</td>
561
562
		<td>
563
			<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>
564
			<?php $length = ! empty( $ppp_share_on_publish_text ) ? strlen( $ppp_share_on_publish_text ) : 0; ?>
565
			&nbsp;<span class="ppp-text-length"><?php echo $length; ?></span>
566
		</td>
567
568
		<td style="width: 200px" colspan="2">
569
			<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 ); ?>/>
570
			&nbsp;<label for="ppp-share-on-publish-image"><?php _e( 'Featured Image', 'ppp-txt' ); ?></label>
571
		</td>
572
573
	</tr>
574
<?php
575
}
576
577
/**
578
 * Generates the row for a scheduled Tweet in the metabox
579
 *
580
 * @param  int    $key     The array index
581
 * @param  array  $args    Arguements/Data for the specific index
582
 * @param  int    $post_id The post ID
583
 * @return void
584
 */
585
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...
586
	global $post, $has_past_shares;
587
588
	if ( ! empty( $args['date'] ) && ! empty( $args['time'] ) ) {
589
		$share_time = ppp_generate_timestamp( $args['date'], $args['time'] );
590
		$readonly   = ppp_generate_timestamp() > $share_time ? 'readonly="readonly" ' : false;
591
	} else {
592
		$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...
593
		$readonly   = false;
594
	}
595
596
	$no_date        = ! empty( $readonly ) ? ' hasDatepicker' : '';
597
	$hide           = ! empty( $readonly ) ? 'display: none;' : '';
598
	$shared         = ! empty( $readonly ) ? 'past-share' : '';
599
600
	if ( ! empty( $readonly ) ) {
601
		$has_past_shares++;
602
	}
603
	?>
604
	<tr class="ppp-tweet-wrapper ppp-repeatable-row ppp-repeatable-twitter scheduled-row <?php echo $shared; ?>" data-key="<?php echo esc_attr( $key ); ?>">
605
		<td>
606
			<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']; ?>" />
607
		</td>
608
609
		<td>
610
			<input <?php echo $readonly; ?>type="text" class="share-time-selector" name="_ppp_tweets[<?php echo $key; ?>][time]" value="<?php echo $args['time']; ?>" />
611
		</td>
612
613
		<td>
614
			<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>
615
			<?php $length = ! empty( $args['text'] ) ? strlen( $args['text'] ) : 0; ?>
616
			&nbsp;<span class="ppp-text-length"><?php echo $length; ?></span>
617
		</td>
618
619
		<td class="ppp-repeatable-upload-wrapper" style="width: 200px">
620
			<div class="ppp-repeatable-upload-field-container">
621
				<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'] ) ); ?>"/>
622
				<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'] ); ?>" />
623
624
				<span class="ppp-upload-file" style="<?php echo $hide; ?>">
625
					<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;">
626
						<span class="dashicons dashicons-upload"></span>
627
					</a>
628
				</span>
629
630
			</div>
631
		</td>
632
633
		<td class="ppp-action-icons">
634
			<a title="<?php _e( 'Duplicate', 'ppp-txt' ); ?>" href="#" class="ppp-clone-tweet"><i class="fa fa-repeat" aria-hidden="true"></i></a>
635
			&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>
636
		</td>
637
638
	</tr>
639
<?php
640
}
641
642
/**
643
 * Save the items in our meta boxes
644
 * @param  int $post_id The Post ID being saved
645
 * @param  object $post    The Post Object being saved
646
 * @return int          The Post ID
647
 */
648
function ppp_tw_save_post_meta_boxes( $post_id, $post ) {
649
650
	if ( ! ppp_should_save( $post_id, $post ) ) {
651
		return;
652
	}
653
654
	$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...
655
656
	$ppp_share_on_publish = ( isset( $_REQUEST['_ppp_share_on_publish'] ) ) ? $_REQUEST['_ppp_share_on_publish'] : '0';
657
	$ppp_share_on_publish_text = ( isset( $_REQUEST['_ppp_share_on_publish_text'] ) ) ? $_REQUEST['_ppp_share_on_publish_text'] : '';
658
	$ppp_share_on_publish_include_image = ( isset( $_REQUEST['_ppp_share_on_publish_include_image'] ) ) ? $_REQUEST['_ppp_share_on_publish_include_image'] : '';
659
660
661
	update_post_meta( $post_id, '_ppp_share_on_publish', $ppp_share_on_publish );
662
	update_post_meta( $post_id, '_ppp_share_on_publish_text', $ppp_share_on_publish_text );
663
	update_post_meta( $post_id, '_ppp_share_on_publish_include_image', $ppp_share_on_publish_include_image );
664
665
	$tweet_data = isset( $_REQUEST['_ppp_tweets'] ) ? $_REQUEST['_ppp_tweets'] : array();
666
	foreach ( $tweet_data as $index => $tweet ) {
667
		$tweet_data[ $index ]['text'] = sanitize_text_field( $tweet['text'] );
668
	}
669
	update_post_meta( $post_id, '_ppp_tweets', $tweet_data );
670
671
}
672
add_action( 'save_post', 'ppp_tw_save_post_meta_boxes', 10, 2 ); // save the custom fields
673
674
/**
675
 * Determines if the post should be shared on publish
676
 * @param  string $old_status The old post status
677
 * @param  string $new_status The new post status
678
 * @param  object $post       The Post Object
679
 * @return void               Shares the post
680
 */
681
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...
682
	global $ppp_options;
683
684
	$from_meta = ! empty( $_POST['ppp_post_edit'] ) ? false : get_post_meta( $post->ID, '_ppp_share_on_publish', true );
685
	$from_post = isset( $_POST['_ppp_share_on_publish'] );
686
687
	if ( empty( $from_meta ) && empty( $from_post ) ) {
688
		return;
689
	}
690
691
	// Determine if we're seeing the share on publish in meta or $_POST
692
	if ( $from_meta && !$from_post ) {
693
		$ppp_share_on_publish_text = get_post_meta( $post->ID, '_ppp_share_on_publish_text', true );
694
		$use_media = get_post_meta( $post->ID, '_ppp_share_on_publish_include_image', true );
695
	} else {
696
		$ppp_share_on_publish_text = isset( $_POST['_ppp_share_on_publish_text'] ) ? $_POST['_ppp_share_on_publish_text'] : '';
697
		$use_media = isset( $_POST['_ppp_share_on_publish_include_image'] ) ? $_POST['_ppp_share_on_publish_include_image'] : false;
698
	}
699
700
	$share_content = ( !empty( $ppp_share_on_publish_text ) ) ? $ppp_share_on_publish_text : ppp_tw_generate_share_content( $post->ID, null, false );
701
	$share_content = apply_filters( 'ppp_share_content', $share_content, array( 'post_id' => $post->ID ) );
702
	$name = 'sharedate_0_' . $post->ID;
703
	$media = ppp_post_has_media( $post->ID, 'tw', $use_media );
704
	$share_link = ppp_generate_link( $post->ID, $name, true );
705
706
	$status = ppp_send_tweet( $share_content . ' ' . $share_link, $post->ID, $media );
707
708
	$log_title = ppp_tw_build_share_message( $post->ID, $name, false, false );
709
710
	$log_data = array(
711
		'post_title'    => $log_title,
712
		'post_content'  =>  json_encode( $status ),
713
		'post_parent'   => $post->ID,
714
		'log_type'      => 'ppp_share'
715
	);
716
717
	$log_meta = array(
718
		'network'   => 'tw',
719
		'share_id'  => 0,
720
	);
721
722
	$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...
723
724
	if ( ! empty( $status->id_str ) ) {
725
		$author_id = $post->post_author;
726
		$author_rt = get_user_meta( $author_id, '_ppp_share_on_publish', true );
727
728
		if ( $author_rt ) {
729
			$twitter_user = new PPP_Twitter_User( $author_id );
730
			$twitter_user->retweet( $status->id_str );
731
		}
732
733
		/* Get an array of users who should retweet if another author has a post published
734
		 * Exclude the author that shared earlier to avoid duplicate retweets.
735
		 */
736
		$args = array(
737
			'meta_query' => array(
738
				array(
739
					'key' 		=> '_ppp_share_others_on_publish',
740
					'value'		=> true,
741
					'compare'	=> '='
742
				),
743
			),
744
			'fields'	=> array( 'ID' ),
745
			'exclude'	=> array( $author_id )
746
		);
747
		$other_rt = get_users( $args );
748
749
		if ( $other_rt ){
750
			foreach ( $other_rt as $user ) {
751
				$twitter_user = new PPP_Twitter_User( $user->ID );
752
				$twitter_user->retweet( $status->id_str );
753
			}
754
		}
755
756
	}
757
}
758
add_action( 'ppp_share_on_publish', 'ppp_tw_share_on_publish', 10, 3 );
759
760
/**
761
 * Generate the timestamps and names for the scheduled Twitter shares
762
 *
763
 * @since  2.3
764
 * @param  array $times   The times to save
765
 * @param  int   $post_id The Post ID of the item being saved
766
 * @return array          Array of timestamps and cron names
767
 */
768
function ppp_tw_generate_timestamps( $times, $post_id ) {
769 1
	$ppp_tweets = get_post_meta( $post_id, '_ppp_tweets', true );
770
771 1
	if ( empty( $ppp_tweets ) ) {
772 1
		$ppp_tweets = array();
773 1
	}
774
775 1
	foreach ( $ppp_tweets as $key => $data ) {
776 1
		if ( ! array_filter( $data ) ) {
777
			continue;
778
		}
779
780 1
		$timestamp = ppp_generate_timestamp( $data['date'], $data['time'] );
781
782 1
		if ( $timestamp > current_time( 'timestamp', 1 ) ) { // Make sure the timestamp we're getting is in the future
783 1
			$time_key           = strtotime( date_i18n( 'd-m-Y H:i:s', $timestamp , true ) ) . '_tw';
784 1
			$times[ $time_key ] = 'sharedate_' . $key . '_' . $post_id . '_tw';
785 1
		}
786
787 1
	}
788
789 1
	return $times;
790
}
791
add_filter( 'ppp_get_timestamps', 'ppp_tw_generate_timestamps', 10, 2 );
792
793
/**
794
 * Returns if the Twitter Cards are enabled
795
 *
796
 * @since  2.2
797
 * @return bool If the user has checked to enable Twitter cards
798
 */
799
function ppp_tw_cards_enabled() {
800
	global $ppp_share_settings;
801
802
	$ret = false;
803
804
	if ( ! empty( $ppp_share_settings['twitter']['cards_enabled'] ) ) {
805
		$ret = true;
806
	}
807
808
	return apply_filters( 'ppp_tw_cards_enabled', $ret );
809
}
810
811
/**
812
 * Output the Twitter Card Meta
813
 *
814
 * @since  2.2
815
 * @return void
816
 */
817
function ppp_tw_card_meta() {
818
819
	if ( ! is_single() || ! ppp_twitter_enabled() || ! ppp_tw_cards_enabled() ) {
820
		return;
821
	}
822
823
	global $post, $ppp_options;
824
825
	if ( ! array_key_exists( $post->post_type, $ppp_options['post_types'] ) ) {
826
		return;
827
	}
828
829
	echo ppp_tw_get_cards_meta();
830
}
831
add_action( 'wp_head', 'ppp_tw_card_meta', 10 );
832
833
/**
834
 * Generates the Twitter Card Content
835
 *
836
 * @since  2.2
837
 * @return string The Twitter card Meta tags
838
 */
839
function ppp_tw_get_cards_meta() {
840
841
	$return = '';
842
843
	if ( ! is_single() || ! ppp_tw_cards_enabled() ) {
844
		return $return;
845
	}
846
847
	global $post, $ppp_social_settings;
848
849
850
	if ( empty( $post ) ) {
851
		return;
852
	}
853
854
	$elements = ppp_tw_default_meta_elements();
855
	foreach ( $elements as $name => $content ) {
856
		$return .= '<meta name="' . $name . '" content="' . $content . '" />' . "\n";
857
	}
858
859
	return apply_filters( 'ppp_tw_card_meta', $return );
860
861
}
862
863
/**
864
 * Sets an array of names and content for Twitter Card Meta
865
 * for easy filtering by devs
866
 *
867
 * @since  2.2
868
 * @return array The array of keys and values for the Twitter Meta
869
 */
870
function ppp_tw_default_meta_elements() {
871
	global $post, $ppp_social_settings;
872
873
	$elements = array();
874
875
	$image_url = ppp_post_has_media( $post->ID, 'tw', true );
876
	if ( $image_url ) {
877
		$elements['twitter:card']      = 'summary_large_image';
878
		$elements['twitter:image:src'] = $image_url;
879
880
		$thumb_id = ppp_get_attachment_id_from_image_url( $image_url );
881
		if ( ! empty( $thumb_id ) ) {
882
			$alt_text = ppp_get_attachment_alt_text( $thumb_id );
883
			// When adding media via the WP Uploader, any 'alt text' supplied will be used as the accessible alt text.
884
			if ( ! empty( $alt_text ) ) {
885
				$elements['twitter:image:alt'] = esc_attr( $alt_text );
886
			}
887
		}
888
	} else {
889
		$elements['twitter:card'] = 'summary';
890
	}
891
892
	$elements['twitter:site']        = '@' . $ppp_social_settings['twitter']['user']->screen_name;
893
	$elements['twitter:title']       = esc_attr( strip_tags( $post->post_title ) );
894
	$elements['twitter:description'] = esc_attr( ppp_tw_get_card_description() );
895
896
	$author_twitter_handle = get_user_meta( $post->post_author, 'twitter', true );
897
	if ( ! empty( $author_twitter_handle ) ) {
898
899
		if ( strpos( $author_twitter_handle, '@' ) === false ) {
900
			$author_twitter_handle = '@' . $author_twitter_handle;
901
		}
902
903
		$elements['twitter:creator'] = esc_attr( strip_tags( $author_twitter_handle ) );
904
905
	}
906
907
	return apply_filters( 'ppp_tw_card_elements', $elements );
908
}
909
910
/**
911
 * Given a post, will give the post excerpt or the truncated post content to fit in a Twitter Card
912
 *
913
 * @since  2.2
914
 * @return string The post excerpt/description
915
 */
916
function ppp_tw_get_card_description() {
917
	global $post;
918
919
	if ( ! is_single() || empty( $post ) ) {
920
		return false;
921
	}
922
923
	$excerpt = $post->post_excerpt;
924
925
	if ( empty( $excerpt ) ) {
926
		$excerpt = ppp_tw_format_card_description( $post->post_content );
927
	}
928
929
	return apply_filters( 'ppp_tw_card_desc', $excerpt );
930
}
931
932
/**
933
 * Format a given string for the excerpt
934
 *
935
 * @since  2.3
936
 * @param  string $excerpt The string to possibly truncate
937
 * @return string          The description, truncated if over 200 characters
938
 */
939
function ppp_tw_format_card_description( $excerpt ) {
940 1
	$max_len = apply_filters( 'ppp_tw_cart_desc_length', 200 );
941 1
	$excerpt = strip_tags( $excerpt );
942
943 1
	if ( strlen( $excerpt ) > $max_len ) {
944
		$excerpt_pre = substr( $excerpt, 0, $max_len );
945
		$last_space  = strrpos( $excerpt_pre, ' ' );
946
		$excerpt     = substr( $excerpt_pre, 0, $last_space ) . '...';
947
	}
948
949 1
	return $excerpt;
950
}
951
952
/**
953
 * Add a Twitter method to the profile editor
954
 *
955
 * @since  2.2.4
956
 * @param  array $user_contactmethods List of user contact methods for the profile editor
957
 * @return array                      List of contact methods with twitter added
958
 */
959
function ppp_tw_add_contact_method( $user_contactmethods ) {
960
961
	if ( ! isset( $user_contactmethods['twitter'] ) ) {
962
		$user_contactmethods['twitter'] = __( 'Twitter', 'ppp-txt' );
963
	}
964
	// Returns the contact methods
965
	return $user_contactmethods;
966
}
967
add_filter( 'user_contactmethods', 'ppp_tw_add_contact_method' );
968
969
970
/**
971
 * Adds in the Post Promoter Pro Preferences Profile Section
972
 *
973
 * @since  2.2.7
974
 * @param  object $user The User object being viewed
975
 * @return void         Displays HTML
976
 */
977
function ppp_tw_profile_settings( $user ) {
978
	global $ppp_social_settings;
979
980
	if ( $user->ID == get_current_user_id() && ! current_user_can( 'edit_posts' ) ) {
981
		return;
982
	}
983
984
	if ( $user->ID !== get_current_user_id() && ! current_user_can( PostPromoterPro::get_manage_capability() ) ) {
985
		return;
986
	}
987
988
	if ( ! isset( $ppp_social_settings['twitter'] ) || is_null( $ppp_social_settings['twitter'] ) ) {
989
		return;
990
	}
991
992
	$connected = false;
993
	?>
994
	<h3><?php _e( 'Post Promoter Pro', 'ppp-txt' ); ?></h3>
995
	<table class="form-table">
996
		<tr>
997
			<th><?php _e( 'Connect to Twitter', 'ppp-txt' ); ?></th>
998
			<td>
999
			<?php
1000
			$twitter = new PPP_Twitter_User( get_current_user_id() );
1001
			$tw_user = get_user_meta( $user->ID, '_ppp_twitter_data', true );
1002
			if ( empty( $tw_user ) ) {
1003
				$tw_authurl = $twitter->get_auth_url( admin_url( 'user-edit.php?user_id=' . $user->ID ) );
1004
1005
				$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>';
1006
				$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>';
1007
				$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>';
1008
				$string .= '<input type="hidden" name="ppp_user_auth" value="1" />';
1009
1010
				echo $string;
1011
			} else {
1012
				$connected = true;
1013
				?>
1014
				<p><strong><?php _e( 'Signed in as', 'ppp-txt' ); ?>: </strong><?php echo $tw_user['user']->screen_name; ?></p>
1015
				<p>
1016
					<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;
1017
					<a class="button-secondary" href="https://twitter.com/settings/applications" target="blank"><?php _e( 'Revoke Access via Twitter', 'ppp-txt' ); ?></a>
1018
				</p>
1019
				<?php
1020
			}
1021
			?>
1022
			</td>
1023
		</tr>
1024
1025
		<?php if ( $connected ) : ?>
1026
		<?php
1027
			$share_on_publish 			= get_user_meta( $user->ID, '_ppp_share_on_publish', true );
1028
			$share_scheduled  			= get_user_meta( $user->ID, '_ppp_share_scheduled' , true );
1029
			$share_others_on_publish 	= get_user_meta( $user->ID, '_ppp_share_others_on_publish', true );
1030
			$share_others_scheduled  	= get_user_meta( $user->ID, '_ppp_share_others_scheduled' , true );
1031
		?>
1032
		<tr>
1033
			<th><?php _e( 'Sharing Options', 'ppp-txt' ); ?></th>
1034
			<td>
1035
				<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>
1036
				<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>
1037
			</td>
1038
		</tr>
1039
		<tr>
1040
			<th></th>
1041
			<td>
1042
				<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>
1043
				<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>
1044
			</td>
1045
		</tr>
1046
		<tr>
1047
			<th></th>
1048
			<td>
1049
				<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>
1050
				<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>
1051
			</td>
1052
		</tr>
1053
		<tr>
1054
			<th></th>
1055
			<td>
1056
				<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>
1057
				<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>
1058
			</td>
1059
		</tr>
1060
1061
		<?php endif; ?>
1062
	</table>
1063
	<?php
1064
}
1065
add_action( 'show_user_profile', 'ppp_tw_profile_settings' );
1066
add_action( 'edit_user_profile', 'ppp_tw_profile_settings' );
1067
1068
/**
1069
 * Saves the User Profile Settings
1070
 *
1071
 * @since  2.2.7
1072
 * @param  int $user_id The User ID being saved
1073
 * @return void         Saves to Usermeta
1074
 */
1075
function ppp_tw_save_profile( $user_id ) {
1076
	global $ppp_social_settings;
1077
1078
	if ( ! isset( $ppp_social_settings['twitter'] ) || is_null( $ppp_social_settings['twitter'] ) ) {
1079
		return;
1080
	}
1081
1082
	$share_on_publish 			= ! empty( $_POST['share_on_publish'] ) ? true : false;
1083
	$share_scheduled  			= ! empty( $_POST['share_scheduled'] )  ? true : false;
1084
	$share_others_on_publish 	= ! empty( $_POST['share_others_on_publish'] ) ? true : false;
1085
	$share_others_scheduled  	= ! empty( $_POST['share_others_scheduled'] )  ? true : false;
1086
1087
	update_user_meta( $user_id, '_ppp_share_on_publish', $share_on_publish );
1088
	update_user_meta( $user_id, '_ppp_share_scheduled', $share_scheduled  );
1089
	update_user_meta( $user_id, '_ppp_share_others_on_publish', $share_others_on_publish );
1090
	update_user_meta( $user_id, '_ppp_share_others_scheduled', $share_others_scheduled  );
1091
1092
}
1093
add_action( 'personal_options_update', 'ppp_tw_save_profile' );
1094
add_action( 'edit_user_profile_update', 'ppp_tw_save_profile' );
1095
1096
function ppp_tw_calendar_on_publish_event( $events, $post_id ) {
1097
	$share_on_publish = get_post_meta( $post_id, '_ppp_share_on_publish', true );
1098
1099
	if ( ! empty( $share_on_publish ) ) {
1100
		$share_text = get_post_meta( $post_id, '_ppp_share_on_publish_text', true );
1101
		$events[] = array(
1102
			'id' => $post_id . '-share-on-publish',
1103
			'title' => ( ! empty( $share_text ) ) ? $share_text : ppp_tw_generate_share_content( $post_id, null, false ),
1104
			'start'     => date_i18n( 'Y-m-d/TH:i:s', strtotime( get_the_date( null, $post_id ) . ' ' . get_the_time( null, $post_id ) ) + 1 ),
1105
			'end'       => date_i18n( 'Y-m-d/TH:i:s', strtotime( get_the_date( null, $post_id ) . ' ' . get_the_time( null, $post_id ) ) + 1 ),
1106
			'className' => 'ppp-calendar-item-tw cal-post-' . $post_id,
1107
			'belongsTo' => $post_id,
1108
		);
1109
	}
1110
1111
	return $events;
1112
}
1113
add_filter( 'ppp_calendar_on_publish_event', 'ppp_tw_calendar_on_publish_event', 10, 2 );
1114
1115
function ppp_tw_get_post_shares( $items, $post_id ) {
1116
	$tweets = get_post_meta( $post_id, '_ppp_tweets', true );
1117
	if ( empty( $tweets ) ) { return $items; }
1118
1119
	foreach ( $tweets as $key => $tweet ) {
1120
		$items[] = array( 'id' => $key, 'service' => 'tw' );
1121
	}
1122
	return $items;
1123
}
1124
add_filter( 'ppp_get_post_scheduled_shares', 'ppp_tw_get_post_shares', 10, 2 );
1125