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.

linkedin-functions.php ➔ ppp_li_share_on_publish()   F
last analyzed

Complexity

Conditions 18
Paths 2180

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 342

Importance

Changes 0
Metric Value
cc 18
nc 2180
nop 3
dl 0
loc 65
ccs 0
cts 44
cp 0
crap 342
rs 0.7
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
// Exit if accessed directly
4
if ( ! defined( 'ABSPATH' ) ) {
5
	exit;
6
}
7
8
/**
9
 * Return if linkedin account is found
10
 * @return bool If the Linkedin object exists
11
 */
12
function ppp_linkedin_enabled() {
13
	global $ppp_social_settings;
14
15
	if ( isset( $ppp_social_settings['linkedin'] ) && !empty( $ppp_social_settings['linkedin'] ) ) {
16
		return true;
17
	}
18
19
	return false;
20
}
21
22
/**
23
 * Registers LinkedIn as a service
24
 * @param  array $services The registered servcies
25
 * @return array           With LinkedIn added
26
 */
27
function ppp_li_register_service( $services = array() ) {
28
	$services[] = 'li';
29
30
	return $services;
31
}
32
add_filter( 'ppp_register_social_service', 'ppp_li_register_service', 10, 1 );
33
34
/**
35
 * The LinkedIn icon
36
 * @param  string $string Default list view string for icon
37
 * @return string         The LinkedIn Icon HTML
38
 */
39
function ppp_li_account_list_icon( $string = '' ) {
40
	$string .= '<span class="dashicons icon-ppp-li"></span>';
41
42
	return $string;
43
}
44
add_filter( 'ppp_account_list_icon-li', 'ppp_li_account_list_icon', 10, 1 );
45
46
/**
47
 * The LinkedIn Avatar for the account list
48
 * @param  string $string Default icon string
49
 * @return string         The HTML for the LinkedIn Avatar
50
 */
51
function ppp_li_account_list_avatar( $string = '' ) {
52
	return $string;
53
}
54
add_filter( 'ppp_account_list_avatar-li', 'ppp_li_account_list_avatar', 10, 1 );
55
56
/**
57
 * The name for the linked LinkedIn account
58
 * @param  string $string The default list name
59
 * @return string         The name for the attached LinkedIn account
60
 */
61
function ppp_li_account_list_name( $string = '' ) {
62
63
	if ( ppp_linkedin_enabled() ) {
64
		global $ppp_social_settings;
65
		$string .= $ppp_social_settings['linkedin']->firstName . ' ' . $ppp_social_settings['linkedin']->lastName;
66
	}
67
68
	return $string;
69
}
70
add_filter( 'ppp_account_list_name-li', 'ppp_li_account_list_name', 10, 1 );
71
72
/**
73
 * The actions column of the accounts list for LinkedIn
74
 * @param  string $string The default actions string
75
 * @return string         HTML for the LinkedIn Actions
76
 */
77
function ppp_li_account_list_actions( $string = '' ) {
78
	global $ppp_linkedin_oauth, $ppp_social_settings;
79
80
	if ( ! ppp_linkedin_enabled() ) {
81
		$li_authurl = $ppp_linkedin_oauth->ppp_get_linkedin_auth_url( admin_url( 'admin.php?page=ppp-social-settings' ) );
82
83
		$string .= '<a class="button-primary" href="' . $li_authurl . '">' . __( 'Connect to Linkedin', 'ppp-txt' ) . '</a>';
84
	} else {
85
		$string  .= '<a class="button-primary" href="' . admin_url( 'admin.php?page=ppp-social-settings&ppp_social_disconnect=true&ppp_network=linkedin' ) . '" >' . __( 'Disconnect from Linkedin', 'ppp-txt' ) . '</a>&nbsp;';
86
87
		$refresh_date = (int) get_option( '_ppp_linkedin_refresh', true );
88
89
90
		if ( defined( 'LINKEDIN_KEY' ) && current_time( 'timestamp' ) > $refresh_date ) {
91
			$token       = $ppp_social_settings['linkedin']->access_token;
92
			$url         = $ppp_linkedin_oauth->ppp_get_linkedin_auth_url( admin_url( 'admin.php?page=ppp-social-settings' ) );
93
			$refresh_url = str_replace( '?ppp-social-auth', '?ppp-social-auth&ppp-refresh=true&access_token=' . $token, $url );
94
95
			$string  .= '<a class="button-secondary" href="' . $refresh_url . '" >' . __( 'Re-Authorize Linkedin', 'ppp-txt' ) . '</a>&nbsp;';
96
		}
97
	}
98
99
	return $string;
100
}
101
add_filter( 'ppp_account_list_actions-li', 'ppp_li_account_list_actions', 10, 1 );
102
103
/**
104
 * The Extras column for the account list for LinkedIn
105
 * @param  string $string Default extras column string
106
 * @return string         The HTML for the LinkedIn Extras column
107
 */
108
function ppp_li_account_list_extras( $string ) {
109
	if ( ppp_linkedin_enabled() ) {
110
		global $ppp_social_settings, $ppp_options;
111
		if ( ! empty( $ppp_options['enable_debug'] ) ) {
112
			$days_left  = absint( round( ( $ppp_social_settings['linkedin']->expires_on - current_time( 'timestamp' ) ) / DAY_IN_SECONDS ) );
113
			$refresh_in = absint( round( ( get_option( '_ppp_linkedin_refresh' ) - current_time( 'timestamp' ) ) / DAY_IN_SECONDS ) );
114
115
			$string .= '<br />' . sprintf( __( 'Token expires in %s days' , 'ppp-txt' ), $days_left );
116
			$string .= '<br />' . sprintf( __( 'Refresh notice in %s days', 'ppp-txt' ), $refresh_in );
117
		}
118
	}
119
120
	return $string;
121
122
}
123
add_filter( 'ppp_account_list_extras-li', 'ppp_li_account_list_extras', 10, 1 );
124
125
/**
126
 * Capture the oauth return from linkedin
127
 * @return void
128
 */
129
function ppp_capture_linkedin_oauth() {
130
	$should_capture = false;
131
132
	if ( isset( $_GET['state'] ) && strpos( $_GET['state'], 'ppp-local-keys-li' ) !== false ) {
133
		// Local config
134
		$should_capture = true;
135
	}
136
137
	if ( isset( $_REQUEST['li_access_token'] ) ) {
138
		// Returning from remote config
139
		$should_capture = true;
140
	}
141
142
	if ( $should_capture && ( isset( $_REQUEST['page'] ) && $_REQUEST['page'] == 'ppp-social-settings' ) ) {
143
		global $ppp_linkedin_oauth;
144
		$ppp_linkedin_oauth->ppp_initialize_linkedin();
145
		wp_redirect( admin_url( 'admin.php?page=ppp-social-settings' ) );
146
		die();
147
	}
148
}
149
add_action( 'admin_init', 'ppp_capture_linkedin_oauth', 10 );
150
151
/**
152
 * Capture the disconnect request from Linkedin
153
 * @return void
154
 */
155
function ppp_disconnect_linkedin() {
156
	global $ppp_social_settings;
157
	$ppp_social_settings = get_option( 'ppp_social_settings' );
158
	if ( isset( $ppp_social_settings['linkedin'] ) ) {
159
		unset( $ppp_social_settings['linkedin'] );
160
		update_option( 'ppp_social_settings', $ppp_social_settings );
161
		delete_option( '_ppp_linkedin_refresh' );
162
	}
163
}
164
add_action( 'ppp_disconnect-linkedin', 'ppp_disconnect_linkedin', 10 );
165
166
/**
167
 * Add query vars for Linkedin
168
 * @param  array $vars Currenty Query Vars
169
 * @return array       Query vars array with linkedin added
170
 */
171
function ppp_li_query_vars( $vars ) {
172
	$vars[] = 'li_access_token';
173 1
	$vars[] = 'expires_in';
174 1
175
	return $vars;
176 1
}
177
add_filter( 'query_vars', 'ppp_li_query_vars' );
178
179
/**
180
 * Refreshes the Linkedin Access Token
181
 * @return void
182
 */
183
function ppp_li_execute_refresh() {
184
185
	if ( ! ppp_linkedin_enabled() ) {
186
		return;
187
	}
188
189
	$refresh_date = (int) get_option( '_ppp_linkedin_refresh', true );
190
191
	if ( ( empty( $_GET['page' ] ) || $_GET['page'] !== 'ppp-social-settings' ) && current_time( 'timestamp' ) > $refresh_date ) {
192
		add_action( 'admin_notices', 'ppp_linkedin_refresh_notice' );
193
	}
194
}
195
add_action( 'admin_init', 'ppp_li_execute_refresh' );
196
197
/**
198
 * Displays notice when the Linkedin Token is nearing expiration
199
 * @return void
200
 */
201
function ppp_linkedin_refresh_notice() {
202
203
	if ( ! ppp_linkedin_enabled() ) {
204
		return;
205
	}
206
207
	$has_dismissed = get_transient( 'ppp-dismiss-refresh-li' . get_current_user_id() );
208
	if ( false !== $has_dismissed ) {
209
		return;
210
	}
211
212
	global $ppp_linkedin_oauth, $ppp_social_settings;
213
214
	// Look for the tokens coming back
215
	$ppp_linkedin_oauth->ppp_initialize_linkedin();
216
217
	$token = $ppp_social_settings['linkedin']->access_token;
218
	$url = $ppp_linkedin_oauth->ppp_get_linkedin_auth_url( admin_url( 'admin.php?page=ppp-social-settings' ) );
219
	$url = str_replace( '?ppp-social-auth', '?ppp-social-auth&ppp-refresh=true&access_token=' . $token, $url );
220
221
	$days_left = (int) round( ( $ppp_social_settings['linkedin']->expires_on - current_time( 'timestamp' ) ) / DAY_IN_SECONDS );
222
	?>
223
	<div class="notice notice-warning is-dismissible" data-service="li">
224
		<?php if ( $days_left > 0 ): ?>
225
			<p><strong>Post Promoter Pro: </strong><?php printf( __( 'Your LinkedIn authentication expires in %d days. Please <a href="%s">refresh access</a>.', 'ppp-txt' ), $days_left, $url ); ?></p>
226
		<?php elseif ( $days_left < 1 ): ?>
227
			<p><strong>Post Promoter Pro: </strong><?php printf( __( 'Your LinkedIn authentication has expired. Please <a href="%s">refresh access</a>.', 'ppp-txt' ), $url ); ?></p>
228
		<?php endif; ?>
229
	</div>
230
	<?php
231
}
232
233
/**
234
 * Allow dismissing of the admin notices on a user level
235
 *
236
 * @since  2.3
237
 * @return void
238
 */
239
function ppp_li_dismiss_notice() {
240
241
	$nag = sanitize_key( $_POST[ 'nag' ] );
242
243
	if ( $nag === $_POST[ 'nag' ] ) {
244
		set_transient( $nag . get_current_user_id(), true, DAY_IN_SECONDS );
245
	}
246
247
248
}
249
add_action( 'wp_ajax_ppp_dismiss_notice-li', 'ppp_li_dismiss_notice' );
250
251
/**
252
 * Define the linkedin tokens as constants
253
 * @param  array $social_tokens The Keys
254
 * @return void
255
 */
256
function ppp_set_li_token_constants( $social_tokens ) {
257
	if ( !empty( $social_tokens ) && property_exists( $social_tokens, 'linkedin' ) ) {
258
		define( 'LINKEDIN_KEY', $social_tokens->linkedin->api_key );
259
		define( 'LINKEDIN_SECRET', $social_tokens->linkedin->secret_key );
260
	}
261
}
262
add_action( 'ppp_set_social_token_constants', 'ppp_set_li_token_constants', 10, 1 );
263
264
/**
265
 * Share a post to Linkedin
266
 * @param  string $title       The Title of the Linkedin Post
267
 * @param  string $description The Description of the post
268
 * @param  string $link        The URL to the post
269
 * @param  mixed  $media       False for no media, url to the image if exists
270
 * @return array               The results array from the API
271
 */
272
function ppp_li_share( $title, $description, $link, $media ) {
273
	global $ppp_linkedin_oauth;
274
	$args = array (
275
		'title' => ppp_entities_and_slashes( $title ),
276
		'description' => ppp_entities_and_slashes( $description ),
277
		'submitted-url' => $link,
278
		'submitted-image-url' => $media
279
		);
280
281
	return $ppp_linkedin_oauth->ppp_linkedin_share( $args );
282
}
283
284
/**
285
 * Add the LinkedIn tab to the social media area
286
 * @param  array $tabs The existing tabs
287
 * @return array       The tabs with LinkedIn Added
288
 */
289
function ppp_li_add_admin_tab( $tabs ) {
290
	$tabs['li'] = array( 'name' => __( 'LinkedIn', 'ppp-txt' ), 'class' => 'icon-ppp-li' );
291
292
	return $tabs;
293
}
294
add_filter( 'ppp_admin_tabs', 'ppp_li_add_admin_tab', 10, 1 );
295
296
/**
297
 * Add the content box for LinkedIn in the social media settings
298
 * @param  array $content The existing content blocks
299
 * @return array          With LinkedIn
300
 */
301
function ppp_li_register_admin_social_content( $content ) {
302
	$content[] = 'li';
303
304
	return $content;
305
}
306
add_filter( 'ppp_admin_social_content', 'ppp_li_register_admin_social_content', 10, 1 );
307
308
/**
309
 * Add LinkedIn to the Meta Box Tabs
310
 * @param  array $tabs Existing Metabox Tabs
311
 * @return array       Metabox tabs with LinkedIn
312
 */
313
function ppp_li_add_meta_tab( $tabs ) {
314
	global $ppp_social_settings;
315
	if ( ! ppp_linkedin_enabled() ) {
316
		return $tabs;
317
	}
318
319
	$tabs['li'] = array( 'name' => __( 'LinkedIn', 'ppp-txt' ), 'class' => 'icon-ppp-li' );
320
321
	return $tabs;
322
}
323
add_filter( 'ppp_metabox_tabs', 'ppp_li_add_meta_tab', 10, 1 );
324
325
/**
326
 * Add LinkedIn to the Metabox Content
327
 * @param  array $content The existing metabox content
328
 * @return array          With LinkedIn
329
 */
330
function ppp_li_register_metabox_content( $content ) {
331
	global $ppp_social_settings;
332
	if ( ! ppp_linkedin_enabled() ) {
333
		return $content;
334
	}
335
336
	$content[] = 'li';
337
338
	return $content;
339
}
340
add_filter( 'ppp_metabox_content', 'ppp_li_register_metabox_content', 10, 1 );
341
342
/**
343
 * Returns the stored LinkedIn data for a post
344
 *
345
 * @since  2.3
346
 * @param  array $post_meta Array of meta data (empty)
347
 * @param  int   $post_id   The Post ID to get the meta for
348
 * @return array            The stored LinkedIn shares for a post
349
 */
350
function ppp_li_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...
351
	return get_post_meta( $post_id, '_ppp_li_shares', true );
352
}
353
add_filter( 'ppp_get_scheduled_items_li', 'ppp_li_get_post_meta', 10, 2 );
354
355
/**
356
 * Registers the thumbnail size for LinkedIn
357
 * @return void
358
 */
359
function ppp_li_register_thumbnail_size() {
360
	add_image_size( 'ppp-li-share-image', 800, 800, true );
361
}
362
add_action( 'ppp_add_image_sizes', 'ppp_li_register_thumbnail_size' );
363
364
/**
365
 * Render the Metabox content for LinkedIn
366
 * @param  object $post The post object
367
 */
368
function ppp_li_add_metabox_content( $post ) {
369
	global $ppp_options, $ppp_share_settings;
370
	$default_text = !empty( $ppp_options['default_text'] ) ? $ppp_options['default_text'] : __( 'Social Text', 'ppp-txt' );
0 ignored issues
show
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...
371
372
	$ppp_li_share_on_publish               = get_post_meta( $post->ID, '_ppp_li_share_on_publish', true );
373
	$ppp_share_on_publish_title            = get_post_meta( $post->ID, '_ppp_li_share_on_publish_title', true );
374
	$ppp_share_on_publish_desc             = get_post_meta( $post->ID, '_ppp_li_share_on_publish_desc', true );
375
	$ppp_li_share_on_publish_attachment_id = get_post_meta( $post->ID, '_ppp_li_share_on_publish_attachment_id', true );
376
	$ppp_li_share_on_publish_image_url     = get_post_meta( $post->ID, '_ppp_li_share_on_publish_image_url', true );
377
378
	$show_share_on_publish = false;
379
380
	$share_by_default      = empty( $ppp_share_settings['share_on_publish'][ $post->post_type ]['linkedin'] ) ? false : true;
381
382
	if ( $ppp_li_share_on_publish == '1' || ( $ppp_li_share_on_publish == '' && $share_by_default ) ) {
383
		$show_share_on_publish = true;
384
	}
385
	?>
386
	<p>
387
		<div class="ppp-post-override-wrap">
388
			<p><h3><?php _e( 'Share on LinkedIn', 'ppp-txt' ); ?></h3></p>
389
			<p>
390
				<?php $disabled = ( $post->post_status === 'publish' && time() > strtotime( $post->post_date ) ) ? true : false; ?>
391
				<label for="ppp_li_share_on_publish"><?php _e( 'Share this post on LinkedIn&hellip;', 'ppp-txt' ); ?></label>
392
				<select name="_ppp_li_share_on_publish" id="ppp_li_share_on_publish" class="ppp-toggle-share-on-publish">
393
					<option value="-1" <?php selected( true, $show_share_on_publish, true ); ?><?php if ( $disabled ): ?>disabled<?php endif; ?>><?php _e( 'Do not share this post', 'ppp-txt' ); ?></option>
394
					<option value="1" <?php selected( true, $show_share_on_publish, true ); ?><?php if ( $disabled ): ?>disabled<?php endif; ?>><?php _e( 'When this post is published', 'ppp-txt' ); ?></option>
395
					<option value="0" <?php selected( false, $show_share_on_publish, true ); ?>><?php _e( 'After this post is published', 'ppp-txt' ); ?></option>
396
				</select>
397
			</p>
398
			<div id="ppp-li-fields" class="ppp-fields">
399
				<div id="ppp-li-fields" class="ppp-meta-table-wrap">
400
					<table class="widefat ppp-repeatable-table" width="100%" cellpadding="0" cellspacing="0">
401
						<thead>
402
							<tr>
403
								<th style="width: 100px"><?php _e( 'Date', 'ppp-txt' ); ?></th>
404
								<th style="width: 75px;"><?php _e( 'Time', 'ppp-txt' ); ?></th>
405
								<th><?php _e( 'Link Info', 'ppp-txt' ); ?></th>
406
								<th style"width: 200px;"><?php _e( 'Image', 'ppp-txt' ); ?></th>
407
								<th style="width: 10px;"></th>
408
							</tr>
409
						</thead>
410
						<tbody id="li-share-on-publish" class="ppp-share-on-publish" <?php if ( false === $show_share_on_publish ) : echo 'style="display: none;"'; endif; ?>>
411
							<?php
412
								$args = array(
413
									'text'          => $ppp_share_on_publish_title,
414
									'desc'          => $ppp_share_on_publish_desc,
415
									'attachment_id' => $ppp_li_share_on_publish_attachment_id,
416
									'image'         => $ppp_li_share_on_publish_image_url,
417
								);
418
419
								ppp_render_li_share_on_publish_row( $args );
420
							?>
421
						</tbody>
422
						<tbody id="li-schedule-share" class="ppp-schedule-share" <?php if ( true === $show_share_on_publish ) : echo 'style="display: none;"'; endif; ?>>
423
							<?php $shares = get_post_meta( $post->ID, '_ppp_li_shares', true ); ?>
424
							<?php if ( ! empty( $shares ) ) : ?>
425
426
								<?php foreach ( $shares as $key => $value ) :
427
									$date          = isset( $value['date'] )          ? $value['date']          : '';
428
									$time          = isset( $value['time'] )          ? $value['time']          : '';
429
									$text          = isset( $value['text'] )          ? $value['text']          : '';
430
									$desc          = isset( $value['desc'] )          ? $value['desc']          : '';
431
									$image         = isset( $value['image'] )         ? $value['image']         : '';
432
									$attachment_id = isset( $value['attachment_id'] ) ? $value['attachment_id'] : '';
433
434
									$args = apply_filters( 'ppp_fb_row_args', compact( 'date','time','text', 'desc', 'image','attachment_id' ), $value );
435
									?>
436
437
									<?php ppp_render_li_share_row( $key, $args ); ?>
438
439
440
								<?php endforeach; ?>
441
442
							<?php else: ?>
443
444
								<?php ppp_render_li_share_row( 1, array( 'date' => '', 'time' => '', 'text' => '', 'desc' => '', 'image' => '', 'attachment_id' => '' ) ); ?>
445
446
							<?php endif; ?>
447
						</tbody>
448
					</table>
449
				</div>
450
			</div><!--end #edd_variable_price_fields-->
451
452
			<p><?php _e( 'Do not include links in your text, this will be added automatically.', 'ppp-txt' ); ?></p>
453
		</div>
454
		<?php _e( 'Note: If no image is chosen, and the post has a featured image, the Featured image will be attached to this share', 'ppp-txt' ); ?>
455
	</p>
456
	<?php
457
}
458
add_action( 'ppp_generate_metabox_content-li', 'ppp_li_add_metabox_content', 10, 1 );
459
460
/**
461
 * Render the LinkedIn share on publish row
462
 *
463
 * @since  2.3
464
 * @param  array  $args Contains share on publish data, if there is any
465
 * @return void
466
 */
467
function ppp_render_li_share_on_publish_row( $args = array() ) {
468
	global $post;
469
	$readonly = $post->post_status !== 'publish' ? '' : 'readonly="readonly" ';
470
	$disabled = ( $post->post_status === 'publish' && time() > strtotime( $post->post_date ) ) ? true : false;
471
	?>
472
	<tr class="ppp-li-wrapper ppp-repeatable-row on-publish-row">
473
		<td colspan="2" class="ppp-on-plublish-date-column">
474
			<?php _e( 'Share On Publish', 'ppp-txt' ); ?>
475
		</td>
476
477
		<td>
478
			<input <?php echo $readonly; ?>class="ppp-tweet-text-repeatable" type="text" name="_ppp_li_share_on_publish_title" value="<?php echo esc_attr( $args['text'] ); ?>" placeholder="<?php _e( 'Link Title', 'ppp-txt' ); ?>" />
479
		</td>
480
481
		<td class="ppp-repeatable-upload-wrapper" style="width: 200px" colspan="2">
482
			<div class="ppp-repeatable-upload-field-container">
483
				<input type="hidden" name="_ppp_li_share_on_publish_attachment_id" class="ppp-repeatable-attachment-id-field" value="<?php echo esc_attr( absint( $args['attachment_id'] ) ); ?>"/>
484
				<input <?php echo $readonly; ?>type="text" class="ppp-repeatable-upload-field ppp-upload-field" name="_ppp_li_share_on_publish_image_url" placeholder="<?php _e( 'Upload or Enter URL', 'ppp-txt' ); ?>" value="<?php echo esc_attr( $args['image'] ); ?>" />
485
486
				<span class="ppp-upload-file">
487
					<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;">
488
						<span class="dashicons dashicons-upload"></span>
489
					</a>
490
				</span>
491
492
			</div>
493
		</td>
494
	</tr>
495
	<tr>
496
		<td colspan="2"></td>
497
		<td colspan="3">
498
			<textarea <?php if ( $disabled ): ?>readonly<?php endif; ?> name="_ppp_li_share_on_publish_desc" placeholder="<?php _e( 'Link Description', 'ppp-txt' ); ?>"><?php echo esc_attr( $args['desc'] ); ?></textarea>
499
		</td>
500
	</tr>
501
<?php
502
}
503
504
/**
505
 * Render the scheduled share row for LinkedIn
506
 *
507
 * @since  2.3
508
 * @param  int $key        The key in the array
509
 * @param  array  $args    Arguements for the current post's share data
510
 * @param  int    $post_id The post ID being edited
0 ignored issues
show
Bug introduced by
There is no parameter named $post_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
511
 * @return void
512
 */
513
function ppp_render_li_share_row( $key, $args = array() ) {
514
	global $post;
515
516
	$share_time = ppp_generate_timestamp( $args['date'], $args['time'] );
517
	$readonly   = ppp_generate_timestamp() > $share_time ? 'readonly="readonly" ' : false;
518
	$no_date    = ! empty( $readonly ) ? ' hasDatepicker' : '';
519
	$hide       = ! empty( $readonly ) ? 'display: none;' : '';
520
	?>
521
	<tr class="ppp-li-wrapper ppp-repeatable-row ppp-repeatable-linkedin scheduled-row" data-key="<?php echo esc_attr( $key ); ?>">
522
		<td>
523
			<input <?php echo $readonly; ?>type="text" class="share-date-selector<?php echo $no_date; ?>" name="_ppp_li_shares[<?php echo $key; ?>][date]" placeholder="mm/dd/yyyy" value="<?php echo $args['date']; ?>" />
524
		</td>
525
526
		<td>
527
			<input <?php echo $readonly; ?>type="text" class="share-time-selector" name="_ppp_li_shares[<?php echo $key; ?>][time]" value="<?php echo $args['time']; ?>" />
528
		</td>
529
530
		<td>
531
			<input <?php echo $readonly; ?>class="ppp-tweet-text-repeatable" type="text" name="_ppp_li_shares[<?php echo $key; ?>][text]" value="<?php echo esc_attr( $args['text'] ); ?>" placeholder="<?php _e( 'Link Title', 'ppp-txt' ); ?>"/>
532
		</td>
533
534
		<td class="ppp-repeatable-upload-wrapper" style="width: 200px">
535
			<div class="ppp-repeatable-upload-field-container">
536
				<input type="hidden" name="_ppp_li_shares[<?php echo $key; ?>][attachment_id]" class="ppp-repeatable-attachment-id-field" value="<?php echo esc_attr( absint( $args['attachment_id'] ) ); ?>"/>
537
				<input <?php echo $readonly; ?>type="text" class="ppp-repeatable-upload-field ppp-upload-field" name="_ppp_li_shares[<?php echo $key; ?>][image]" placeholder="<?php _e( 'Upload or Enter URL', 'ppp-txt' ); ?>" value="<?php echo esc_attr( $args['image'] ); ?>" />
538
539
				<span class="ppp-upload-file" style="<?php echo $hide; ?>">
540
					<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;">
541
						<span class="dashicons dashicons-upload"></span>
542
					</a>
543
				</span>
544
545
			</div>
546
		</td>
547
548
		<td>
549
			<a href="#" class="ppp-repeatable-row ppp-remove-repeatable" data-type="linkedin" style="<?php echo $hide; ?>"><i class="fa fa-trash" aria-hidden="true"></i></a>
550
		</td>
551
552
	</tr>
553
	<tr>
554
		<td colspan="2"></td>
555
		<td colspan="3">
556
			<textarea <?php echo $readonly; ?> class="ppp-repeatable-textarea" name="_ppp_li_shares[<?php echo $key; ?>][desc]" placeholder="<?php _e( 'Link Description', 'ppp-txt' ); ?>"><?php echo esc_attr( $args['desc'] ); ?></textarea>
557
		</td>
558
	</tr>
559
<?php
560
}
561
562
/**
563
 * Save the items in our meta boxes
564
 * @param  int $post_id The Post ID being saved
565
 * @param  object $post    The Post Object being saved
566
 * @return int          The Post ID
567
 */
568
function ppp_li_save_post_meta_boxes( $post_id, $post ) {
569
570
	if ( ! ppp_should_save( $post_id, $post ) ) {
571
		return;
572
	}
573
574
	$ppp_li_share_on_publish            = ( isset( $_REQUEST['_ppp_li_share_on_publish'] ) )               ? $_REQUEST['_ppp_li_share_on_publish']               : '-1';
575
	$ppp_share_on_publish_title         = ( isset( $_REQUEST['_ppp_li_share_on_publish_title'] ) )         ? $_REQUEST['_ppp_li_share_on_publish_title']         : '';
576
	$ppp_share_on_publish_desc          = ( isset( $_REQUEST['_ppp_li_share_on_publish_desc'] ) )          ? $_REQUEST['_ppp_li_share_on_publish_desc']          : '';
577
	$ppp_share_on_publish_image_url     = ( isset( $_REQUEST['_ppp_li_share_on_publish_image_url'] ) )     ? $_REQUEST['_ppp_li_share_on_publish_image_url']     : '';
578
	$ppp_share_on_publish_attachment_id = ( isset( $_REQUEST['_ppp_li_share_on_publish_attachment_id'] ) ) ? $_REQUEST['_ppp_li_share_on_publish_attachment_id'] : '';
579
580
	update_post_meta( $post_id, '_ppp_li_share_on_publish',               $ppp_li_share_on_publish );
581
	update_post_meta( $post_id, '_ppp_li_share_on_publish_title',         $ppp_share_on_publish_title );
582
	update_post_meta( $post_id, '_ppp_li_share_on_publish_desc',          $ppp_share_on_publish_desc );
583
	update_post_meta( $post_id, '_ppp_li_share_on_publish_image_url',     $ppp_share_on_publish_image_url );
584
	update_post_meta( $post_id, '_ppp_li_share_on_publish_attachment_id', $ppp_share_on_publish_attachment_id );
585
586
	$li_data = ( isset( $_REQUEST['_ppp_li_shares'] ) && empty( $ppp_li_share_on_publish ) ) ? $_REQUEST['_ppp_li_shares'] : array();
587
	foreach ( $li_data as $index => $share ) {
588
		$li_data[ $index ]['text'] = sanitize_text_field( $share['text'] );
589
		$li_data[ $index ]['desc'] = sanitize_text_field( $share['desc'] );
590
	}
591
592
	update_post_meta( $post_id, '_ppp_li_shares', $li_data );
593
}
594
add_action( 'save_post', 'ppp_li_save_post_meta_boxes', 10, 2 ); // save the custom fields
595
596
/**
597
 * Share a linkedin post on Publish
598
 * @param  string $old_status The old post status
599
 * @param  string $new_status The new post status
600
 * @param  object $post       The Post object
601
 * @return void
602
 */
603
function ppp_li_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...
604
	global $ppp_options;
605
	$from_meta = ! empty( $_POST['ppp_post_edit'] ) ? false : get_post_meta( $post->ID, '_ppp_li_share_on_publish', true );
606
	$from_post = isset( $_POST['_ppp_li_share_on_publish'] ) ? $_POST['_ppp_li_share_on_publish']: '0';
607
608
	if ( '1' != $from_meta && '1' != $from_post ) {
609
		return;
610
	}
611
612
	$from_meta = $from_meta == '1' ? true : false;
613
	$from_post = $from_post == '1' ? true : false;
614
615
	$title         = '';
0 ignored issues
show
Unused Code introduced by
$title 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...
616
	$desc          = '';
0 ignored issues
show
Unused Code introduced by
$desc 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...
617
	$attachment_id = 0;
0 ignored issues
show
Unused Code introduced by
$attachment_id 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...
618
	$image_url     = '';
0 ignored issues
show
Unused Code introduced by
$image_url 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...
619
620
	// Determine if we're seeing the share on publish in meta or $_POST
621
	if ( $from_meta && ! $from_post ) {
622
		$title         = get_post_meta( $post->ID, '_ppp_li_share_on_publish_title'        , true );
623
		$desc          = get_post_meta( $post->ID, '_ppp_li_share_on_publish_desc'         , true );
624
		$attachment_id = get_post_meta( $post->ID, '_ppp_li_share_on_publish_attachment_id', true );
625
		$image_url     = get_post_meta( $post->ID, '_ppp_li_share_on_publish_image_url'    , true );
626
	} else {
627
		$title         = isset( $_POST['_ppp_li_share_on_publish_title'] )         ? $_POST['_ppp_li_share_on_publish_title']         : '';
628
		$desc          = isset( $_POST['_ppp_li_share_on_publish_desc'] )          ? $_POST['_ppp_li_share_on_publish_desc']          : false;
629
		$attachment_id = isset( $_POST['_ppp_li_share_on_publish_attachment_id'] ) ? $_POST['_ppp_li_share_on_publish_attachment_id'] : 0;
630
		$image_url     = isset( $_POST['_ppp_li_share_on_publish_image_url'] )     ? $_POST['_ppp_li_share_on_publish_image_url']     : '';
631
	}
632
633
	$thumbnail = '';
0 ignored issues
show
Unused Code introduced by
$thumbnail 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...
634
	if ( empty( $attachment_id ) && ! empty( $image_url ) ) {
635
		$thumbnail = $image_url;
636
	} else {
637
		$thumbnail = ppp_post_has_media( $post->ID, 'li', true, $attachment_id );
638
	}
639
640
	$name = 'sharedate_0_' . $post->ID . '_li';
641
642
	$default_title = isset( $ppp_options['default_text'] ) ? $ppp_options['default_text'] : '';
643
	// If an override was found, use it, otherwise try the default text content
644
	if ( empty( $title ) && empty( $default_title ) ) {
645
		$title = get_the_title( $post->ID );
646
	}
647
648
	$link      = ppp_generate_link( $post->ID, $name, true );
649
	$status    = ppp_li_share( $title, $desc, $link, $thumbnail );
650
	$log_title = ppp_li_build_share_message( $post->ID, $name );
651
652
	$log_data = array(
653
		'post_title'    => $log_title,
654
		'post_content'  => '',
655
		'post_parent'   => $post->ID,
656
		'log_type'      => 'ppp_share',
657
	);
658
659
	$log_meta = array(
660
		'network'   => 'li',
661
		'share_id'  => 0,
662
	);
663
664
	$log_entry = WP_Logging::insert_log( $log_data, $log_meta );
665
666
	update_post_meta( $log_entry, '_ppp_share_status', $status );
667
}
668
add_action( 'ppp_share_on_publish', 'ppp_li_share_on_publish', 10, 3 );
669
670
/**
671
 * Send out a scheduled share to LinkedIn
672
 *
673
 * @since  2.3
674
 * @param  integer $post_id The Post ID to share fore
675
 * @param  integer $index   The index in the shares
676
 * @param  string  $name    The name of the Cron
677
 * @return void
678
 */
679
function ppp_li_scheduled_share(  $post_id = 0, $index = 1, $name = ''  ) {
680
	global $ppp_options;
681
682
	$link = ppp_generate_link( $post_id, $name );
683
684
	$post_meta     = get_post_meta( $post_id, '_ppp_li_shares', true );
685
	$this_share    = $post_meta[ $index ];
686
	$attachment_id = isset( $this_share['attachment_id'] ) ? $this_share['attachment_id'] : false;
687
688
	$share_message = ppp_li_build_share_message( $post_id, $name );
689
690
	if ( empty( $attachment_id ) && ! empty( $this_share['image'] ) ) {
691
		$media = $this_share['image'];
692
	} else {
693
		$use_media = ppp_li_use_media( $post_id, $index );
694
		$media     = ppp_post_has_media( $post_id, 'li', $use_media, $attachment_id );
695
	}
696
697
	$desc      = ppp_li_get_share_description( $post_id, $index );
698
	$status    = ppp_li_share( $share_message, $desc, $link, $media );
699
	$log_title = ppp_li_build_share_message( $post_id, $name );
700
701
	$log_data = array(
702
		'post_title'    => $log_title,
703
		'post_content'  => '',
704
		'post_parent'   => $post_id,
705
		'log_type'      => 'ppp_share'
706
	);
707
708
	$log_meta = array(
709
		'network'   => 'li',
710
		'share_id'  => $index,
711
	);
712
713
	$log_entry = WP_Logging::insert_log( $log_data, $log_meta );
714
715
	update_post_meta( $log_entry, '_ppp_share_status', $status );
716
717
}
718
add_action( 'ppp_share_scheduled_li', 'ppp_li_scheduled_share', 10, 3 );
719
720
/**
721
 * Return if media is supported for this scheduled post
722
 * @param  int $post_id The Post ID
723
 * @param  int $index   The index of this tweet in the _ppp_tweets data
724
 * @return bool         Whether or not this tweet should contain a media post
725
 */
726
function ppp_li_use_media( $post_id, $index ) {
727
	if ( empty( $post_id ) || empty( $index ) ) {
728
		return false;
729
	}
730
731
	return true; // Always include an image for facebook, even if it's a fallback to the featured image
732
}
733
734
/**
735
 * Build the text for the LinkedIn share
736
 *
737
 * @since  2.3
738
 * @param  int     $post_id   The Post ID
739
 * @param  string  $name      The cron name
740
 * @param  boolean $scheduled If the item is being fired by a schedule (default, true), or retrieved for display (false)
741
 * @return string             The message to share
742
 */
743
function ppp_li_build_share_message( $post_id, $name, $scheduled = true ) {
0 ignored issues
show
Unused Code introduced by
The parameter $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...
744
	$share_content = ppp_li_generate_share_content( $post_id, $name );
745
746
	return apply_filters( 'ppp_li_build_share_message', $share_content );
747
}
748
749
/**
750
 * The worker function for ppp_li_build_share_message
751
 *
752
 * @since  2.3
753
 * @param  int     $post_id      Post ID
754
 * @param  string  $name         The cron name
755
 * @param  boolean $scheduled    If the item is being fired by a schedule (default, true), or retrieved for display (false)
0 ignored issues
show
Documentation introduced by
There is no parameter named $scheduled. Did you maybe mean $is_scheduled?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
756
 * @return string                The formatted link to the post
757
 */
758
function ppp_li_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...
759
	global $ppp_options;
760
	$default_text = isset( $ppp_options['default_text'] ) ? $ppp_options['default_text'] : '';
761
	$li_shares    = get_post_meta( $post_id, '_ppp_li_shares', true );
762
763
	if ( ! empty( $li_shares ) ) {
764
		$name_array    = explode( '_', $name );
765
		$index         = $name_array[1];
766
		if ( isset( $li_shares[ $index ] ) ) {
767
			$share_content = $li_shares[ $index ]['text'];
768
		}
769
	}
770
771
	// If an override was found, use it, otherwise try the default text content
772
	$share_content = ( isset( $share_content ) && !empty( $share_content ) ) ? $share_content : $default_text;
773
774
	// If the content is still empty, just use the post title
775
	$share_content = ( isset( $share_content ) && !empty( $share_content ) ) ? $share_content : get_the_title( $post_id );
776
777
	return apply_filters( 'ppp_share_content_li', $share_content, array( 'post_id' => $post_id ) );
778
}
779
780
/**
781
 * Builds out the link description for sharing to LinkedIn
782
 *
783
 * @since  2.3
784
 * @param  int $post_id The Post Id
785
 * @param  int $index   The share index
786
 * @return string       Link description for the share index of the given post ID
787
 */
788
function ppp_li_get_share_description( $post_id, $index ) {
789
	$description = '';
790
	$li_shares   = get_post_meta( $post_id, '_ppp_li_shares', true );
791
792
	if ( ! empty( $li_shares[ $index ] ) ) {
793
		$description = ! empty( $li_shares[ $index ]['desc'] ) ? $li_shares[ $index ]['desc'] : '';
794
	}
795
796
	return $description;
797
}
798
799
/**
800
 * Generate the timestamps and names for the scheduled LinkedIn shares
801
 *
802
 * @since  2.3
803
 * @param  array $times   The times to save
804
 * @param  int   $post_id The Post ID of the item being saved
805
 * @return array          Array of timestamps and cron names
806
 */
807
function ppp_li_generate_timestamps( $times, $post_id ) {
808
	$li_shares = get_post_meta( $post_id, '_ppp_li_shares', true );
809 1
810
	if ( empty( $li_shares ) ) {
811 1
		$li_shares = array();
812 1
	}
813 1
814
	foreach ( $li_shares as $key => $data ) {
815 1
		if ( ! array_filter( $data ) ) {
816
			continue;
817
		}
818
819
		$timestamp = ppp_generate_timestamp( $data['date'], $data['time'] );
820
821
		if ( $timestamp > current_time( 'timestamp', 1 ) ) { // Make sure the timestamp we're getting is in the future
822
			$time_key           = strtotime( date_i18n( 'd-m-Y H:i:s', $timestamp , true ) ) . '_li';
823
			$times[ $time_key ] = 'sharedate_' . $key . '_' . $post_id . '_li';
824
		}
825
826
	}
827 1
828
	return $times;
829 1
}
830
add_filter( 'ppp_get_timestamps', 'ppp_li_generate_timestamps', 10, 2 );
831
832
/**
833
 * Builds a list of events for the Schedule calendar
834
 *
835
 * @since  2.3
836
 * @param  array $events  The currently found events
837
 * @param  int   $post_id Post ID to find items for
838
 * @return array          Array of events with any LinkedIn items
839
 */
840
function ppp_li_calendar_on_publish_event( $events, $post_id ) {
841
	$share_on_publish = get_post_meta( $post_id, '_ppp_li_share_on_publish', true );
842
843
	if ( ! empty( $share_on_publish ) ) {
844
		$share_text = get_post_meta( $post_id, '_ppp_li_share_on_publish_title', true );
845
		$events[] = array(
846
			'id'        => $post_id . '-share-on-publish',
847
			'title'     => ( ! empty( $share_text ) ) ? $share_text : ppp_li_generate_share_content( $post_id, null, false ),
848
			'start'     => date_i18n( 'Y-m-d/TH:i:s', strtotime( get_the_date( null, $post_id ) . ' ' . get_the_time( null, $post_id ) ) + 1 ),
849
			'end'       => date_i18n( 'Y-m-d/TH:i:s', strtotime( get_the_date( null, $post_id ) . ' ' . get_the_time( null, $post_id ) ) + 1 ),
850
			'className' => 'ppp-calendar-item-li cal-post-' . $post_id,
851
			'belongsTo' => $post_id,
852
		);
853
	}
854
855
	return $events;
856
}
857
add_filter( 'ppp_calendar_on_publish_event', 'ppp_li_calendar_on_publish_event', 10, 2 );
858
859
function ppp_li_get_post_shares( $items, $post_id ) {
860
	$shares = get_post_meta( $post_id, '_ppp_li_shares', true );
861
	if ( empty( $shares ) ) { return $items; }
862
863
	foreach ( $shares as $key => $share ) {
864
		$items[] = array( 'id' => $key, 'service' => 'li' );
865
	}
866
	return $items;
867
}
868
add_filter( 'ppp_get_post_scheduled_shares', 'ppp_li_get_post_shares', 10, 2 );
869