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.

twitter-functions.php ➔ ppp_render_tweet_share_on_publish_row()   F
last analyzed

Complexity

Conditions 12
Paths 512

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
cc 12
nc 512
nop 0
dl 0
loc 40
rs 3.4777
c 0
b 0
f 0
ccs 0
cts 13
cp 0
crap 156

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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