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.

facebook-functions.php ➔ ppp_fb_share_on_publish()   F
last analyzed

Complexity

Conditions 17
Paths 1156

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 0
Metric Value
cc 17
nc 1156
nop 3
dl 0
loc 66
ccs 0
cts 41
cp 0
crap 306
rs 1.0499
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 Facebook account is found
10
 * @return bool If the Twitter object exists
11
 */
12
function ppp_facebook_enabled() {
13 5
	global $ppp_social_settings;
14
15 5
	if ( isset( $ppp_social_settings['facebook'] ) && !empty( $ppp_social_settings['facebook'] ) ) {
16 5
		return true;
17
	}
18
19 1
	return false;
20
}
21
22
/**
23
 * Register Facebook as a service
24
 * @param  array $services The Currently registered services
25
 * @return array           The services with Facebook added
26
 */
27
function ppp_fb_register_service( $services = array() ) {
28 1
	$services[] = 'fb';
29
30 1
	return $services;
31
}
32
add_filter( 'ppp_register_social_service', 'ppp_fb_register_service', 10, 1 );
33
34
/**
35
 * Registers the facebook icon
36
 * @param  string $string The item passed into the list icons
37
 * @return string         The Facebook Icon
38
 */
39
function ppp_fb_account_list_icon( $string = '' ) {
40 1
	$string .= '<span class="dashicons icon-ppp-fb"></span>';
41
42 1
	return $string;
43
}
44
add_filter( 'ppp_account_list_icon-fb', 'ppp_fb_account_list_icon', 10, 1 );
45
46
/**
47
 * Show the Facebook Avatar in the account list
48
 * @param  string $string The list default
49
 * @return string         The Facebook avatar
50
 */
51
function ppp_fb_account_list_avatar( $string = '' ) {
52
53 1
	if ( ppp_facebook_enabled() ) {
54 1
		global $ppp_social_settings;
55 1
		$avatar_url = $ppp_social_settings['facebook']->avatar;
56 1
		$string = '<img class="ppp-social-icon" src="' . $avatar_url . '" />';
57 1
	}
58
59 1
	return $string;
60
}
61
add_filter( 'ppp_account_list_avatar-fb', 'ppp_fb_account_list_avatar', 10, 1 );
62
63
/**
64
 * Adds Facebook name to the list-class
65
 * @param  string $string The default name
66
 * @return string         The name of the auth'd Facebook Profile
67
 */
68
function ppp_fb_account_list_name( $string = '' ) {
69
70 1
	if ( ppp_facebook_enabled() ) {
71 1
		global $ppp_social_settings;
72 1
		$string  = $ppp_social_settings['facebook']->name;
73 1
	}
74
75 1
	return $string;
76
}
77
add_filter( 'ppp_account_list_name-fb', 'ppp_fb_account_list_name', 10, 1 );
78
79
/**
80
 * The Facebook actions for the list view
81
 * @param  string $string The default list view actions
82
 * @return string         The HTML for the actions
83
 */
84
function ppp_fb_account_list_actions( $string = '' ) {
85 1
	global $ppp_facebook_oauth, $ppp_social_settings;
86
87 1
	if ( ! ppp_facebook_enabled() ) {
88
89
		$fb_authurl = $ppp_facebook_oauth->ppp_get_facebook_auth_url( admin_url( 'admin.php?page=ppp-social-settings' ) );
90
91
		$string .= '<a class="button-primary" href="' . $fb_authurl . '">' . __( 'Connect to Facebook', 'ppp-txt' ) . '</a>';
92
	} else {
93 1
		$string  .= '<a class="button-primary" href="' . admin_url( 'admin.php?page=ppp-social-settings&ppp_social_disconnect=true&ppp_network=facebook' ) . '" >' . __( 'Disconnect from Facebook', 'ppp-txt' ) . '</a>&nbsp;';
94
95 1
		$refresh_date = (int) get_option( '_ppp_facebook_refresh', true );
96
97 1
		if ( defined( 'PPP_FB_APP_ID' ) && current_time( 'timestamp' ) > $refresh_date ) {
98
			$token       = $ppp_social_settings['facebook']->access_token;
99
			$url         = $ppp_facebook_oauth->ppp_get_facebook_auth_url( admin_url( 'admin.php?page=ppp-social-settings' ) );
100
			$refresh_url = str_replace( '?ppp-social-auth', '?ppp-social-auth&ppp-refresh=true&access_token=' . $token, $url );
101
102
			$string  .= '<a class="button-secondary" href="' . $refresh_url . '" >' . __( 'Re-Authorize Facebook', 'ppp-txt' ) . '</a>&nbsp;';
103
		}
104
	}
105
106 1
	return $string;
107
}
108
add_filter( 'ppp_account_list_actions-fb', 'ppp_fb_account_list_actions', 10, 1 );
109
110
/**
111
 * The Facebook Extras section for the list-class
112
 * @param  string $string The default extras colun
113
 * @return string         The HTML for the Pages dropdown and debug info
114
 */
115
function ppp_fb_account_list_extras( $string ) {
116
117
	if ( ppp_facebook_enabled() ) {
118
		global $ppp_social_settings, $ppp_facebook_oauth, $ppp_options;
119
		$pages = $ppp_facebook_oauth->ppp_get_fb_user_pages( $ppp_social_settings['facebook']->access_token );
120
		$selected = isset( $ppp_social_settings['facebook']->page ) ? stripslashes( $ppp_social_settings['facebook']->page ) : 'me';
121
122
		if ( !empty( $pages ) ) {
123
			$string = '<label>' . __( 'Publish as:', 'ppp-txt' ) . '</label><br />';
124
			$string .= '<select id="fb-page">';
125
			foreach ( $pages as $page ) {
126
				$value = $page->name . '|' . $page->access_token . '|' . $page->id;
127
				$string .= '<option ' . selected( $value, $selected, false ) . ' value="' . $value . '">' . $page->name . '</option>';
128
			}
129
			$string .= '</select><span class="spinner"></span>';
130
		}
131
132
		if ( ! empty( $ppp_options['enable_debug'] ) ) {
133
			$days_left  = absint( round( ( $ppp_social_settings['facebook']->expires_on - current_time( 'timestamp' ) ) / DAY_IN_SECONDS ) );
134
			$refresh_in = absint( round( ( get_option( '_ppp_facebook_refresh' ) - current_time( 'timestamp' ) ) / DAY_IN_SECONDS ) );
135
136
			$string .= '<br />' . sprintf( __( 'Token expires in %s days' , 'ppp-txt' ), $days_left );
137
			$string .= '<br />' . sprintf( __( 'Refresh notice in %s days', 'ppp-txt' ), $refresh_in );
138
		}
139
	}
140
141
	return $string;
142
}
143
add_filter( 'ppp_account_list_extras-fb', 'ppp_fb_account_list_extras', 10, 1 );
144
145
/**
146
 * Sets the constants for the oAuth tokens for Twitter
147
 * @param  array $social_tokens The tokens stored in the transient
148
 * @return void
149
 */
150
function ppp_set_fb_token_constants( $social_tokens ) {
151
	if ( !empty( $social_tokens ) && property_exists( $social_tokens, 'facebook' ) ) {
152
		define( 'PPP_FB_APP_ID', $social_tokens->facebook->app_id );
153
		define( 'PPP_FB_APP_SECRET', $social_tokens->facebook->app_secret );
154
	}
155
}
156
add_action( 'ppp_set_social_token_constants', 'ppp_set_fb_token_constants', 10, 1 );
157
158
/**
159
 * Capture the oauth return from facebook
160
 * @return void
161
 */
162
function ppp_capture_facebook_oauth() {
163
	$should_capture = false;
164
165
	if ( isset( $_GET['state'] ) && strpos( $_GET['state'], 'ppp-local-keys-fb' ) !== false ) {
166
		// Local config
167
		$should_capture = true;
168
	}
169
170
	if ( isset( $_REQUEST['fb_access_token'] ) ) {
171
		// Returning from remote config
172
		$should_capture = true;
173
	}
174
175
	if ( $should_capture && ( isset( $_REQUEST['page'] ) && $_REQUEST['page'] == 'ppp-social-settings' ) ) {
176
		global $ppp_facebook_oauth;
177
		$ppp_facebook_oauth->ppp_initialize_facebook();
178
		wp_redirect( admin_url( 'admin.php?page=ppp-social-settings' ) );
179
		die();
180
	}
181
182
}
183
add_action( 'admin_init', 'ppp_capture_facebook_oauth', 10 );
184
185
/**
186
 * Capture the disconnect request from Facebook
187
 * @return void
188
 */
189
function ppp_disconnect_facebook() {
190
	global $ppp_social_settings;
191
	$ppp_social_settings = get_option( 'ppp_social_settings' );
192
	if ( isset( $ppp_social_settings['facebook'] ) ) {
193
		unset( $ppp_social_settings['facebook'] );
194
		update_option( 'ppp_social_settings', $ppp_social_settings );
195
		delete_option( '_ppp_facebook_refresh' );
196
	}
197
}
198
add_action( 'ppp_disconnect-facebook', 'ppp_disconnect_facebook', 10 );
199
200
/**
201
 * Add query vars for Facebook
202
 * @param  array $vars Currenty Query Vars
203
 * @return array       Query vars array with facebook added
204
 */
205
function ppp_fb_query_vars( $vars ) {
206
	$vars[] = 'fb_access_token';
207 1
	$vars[] = 'expires_in';
208 1
209
	return $vars;
210 1
}
211
add_filter( 'query_vars', 'ppp_fb_query_vars' );
212
213
/**
214
 * Refreshes the Facebook Access Token
215
 * @return void
216
 */
217
function ppp_fb_execute_refresh() {
218
219
	if ( ! ppp_facebook_enabled() ) {
220
		return;
221
	}
222
223
	$refresh_date = (int) get_option( '_ppp_facebook_refresh', true );
224
225
	if ( ( empty( $_GET['page' ] ) || $_GET['page'] !== 'ppp-social-settings' ) && current_time( 'timestamp' ) > $refresh_date ) {
226
		add_action( 'admin_notices', 'ppp_facebook_refresh_notice' );
227
	}
228
}
229
add_action( 'admin_init', 'ppp_fb_execute_refresh', 99 );
230
231
/**
232
 * Displays notice when the Facebook Token is nearing expiration
233
 * @return void
234
 */
235
function ppp_facebook_refresh_notice() {
236
237
	if ( ! ppp_facebook_enabled() ) {
238
		return;
239
	}
240
241
	$has_dismissed = get_transient( 'ppp-dismiss-refresh-fb' . get_current_user_id() );
242
	if ( false !== $has_dismissed ) {
243
		return;
244
	}
245
246
	global $ppp_facebook_oauth, $ppp_social_settings;
247
248
	// Look for the tokens coming back
249
	$ppp_facebook_oauth->ppp_initialize_facebook();
250
251
	$token = $ppp_social_settings['facebook']->access_token;
252
	$url = $ppp_facebook_oauth->ppp_get_facebook_auth_url( admin_url( 'admin.php?page=ppp-social-settings' ) );
253
	$url = str_replace( '?ppp-social-auth', '?ppp-social-auth&ppp-refresh=true&access_token=' . $token, $url );
254
255
	$days_left = (int) round( ( $ppp_social_settings['facebook']->expires_on - current_time( 'timestamp' ) ) / DAY_IN_SECONDS );
256
	?>
257
	<div class="notice notice-warning is-dismissible" data-service="fb">
258
		<?php if ( $days_left > 0 ): ?>
259
			<p><strong>Post Promoter Pro: </strong><?php printf( __( 'Your Facebook authentication expires in %d days. Please <a href="%s">refresh access</a>.', 'ppp-txt' ), $days_left, $url ); ?></p>
260
		<?php elseif ( $days_left < 1 ): ?>
261
			<p><strong>Post Promoter Pro: </strong><?php printf( __( 'Your Facebook authentication has expired. Please <a href="%s">refresh access</a>.', 'ppp-txt' ), $url ); ?></p>
262
		<?php endif; ?>
263
	</div>
264
	<?php
265
}
266
267
/**
268
 * Allow dismissing of the admin notices on a user level
269
 *
270
 * @since  2.3
271
 * @return void
272
 */
273
function ppp_fb_dismiss_notice() {
274
275
	$nag = sanitize_key( $_POST[ 'nag' ] );
276
277
	if ( $nag === $_POST[ 'nag' ] ) {
278
		set_transient( $nag . get_current_user_id(), true, DAY_IN_SECONDS );
279
	}
280
281
282
}
283
add_action( 'wp_ajax_ppp_dismiss_notice-fb', 'ppp_fb_dismiss_notice' );
284
285
/**
286
 * Share a post to Facebook
287
 * @param  string $link        The link to Share
288
 * @param  string $message     The message attached to the link
289
 * @return array               The results array from the API
290
 */
291
function ppp_fb_share( $link, $message, $picture ) {
292
	global $ppp_facebook_oauth;
293
294
	return $ppp_facebook_oauth->ppp_fb_share_link( $link, ppp_entities_and_slashes( $message ), $picture );
295
}
296
297
/**
298
 * Send out a scheduled share to Facebook
299
 *
300
 * @since  2.3
301
 * @param  integer $post_id The Post ID to share fore
302
 * @param  integer $index   The index in the shares
303
 * @param  string  $name    The name of the Cron
304
 * @return void
305
 */
306
function ppp_fb_scheduled_share(  $post_id = 0, $index = 1, $name = ''  ) {
307
	global $ppp_options;
308
309
	$link = ppp_generate_link( $post_id, $name );
310
311
	$post_meta     = get_post_meta( $post_id, '_ppp_fb_shares', true );
312
	$this_share    = $post_meta[ $index ];
313
	$attachment_id = isset( $this_share['attachment_id'] ) ? $this_share['attachment_id'] : false;
314
315
	$share_message = ppp_fb_build_share_message( $post_id, $name );
316
317
	if ( empty( $attachment_id ) && ! empty( $this_share['image'] ) ) {
318
		$media = $this_share['image'];
319
	} else {
320
		$use_media = ppp_fb_use_media( $post_id, $index );
321
		$media     = ppp_post_has_media( $post_id, 'fb', $use_media, $attachment_id );
322
	}
323
324
	$status = ppp_fb_share( $link, $share_message, $media );
325
326
	$log_title = ppp_fb_build_share_message( $post_id, $name );
327
328
	$log_data = array(
329
		'post_title'    => $log_title,
330
		'post_content'  =>  '',
331
		'post_parent'   => $post_id,
332
		'log_type'      => 'ppp_share'
333
	);
334
335
	$log_meta = array(
336
		'network'   => 'fb',
337
		'share_id'  => $index,
338
	);
339
340
	$log_entry = WP_Logging::insert_log( $log_data, $log_meta );
341
342
	update_post_meta( $log_entry, '_ppp_share_status', $status );
343
}
344
add_action( 'ppp_share_scheduled_fb', 'ppp_fb_scheduled_share', 10, 3 );
345
346
/**
347
 * Returns the stored Facebook data for a post
348
 *
349
 * @since  2.3
350
 * @param  array $post_meta Array of meta data (empty)
351
 * @param  int   $post_id   The Post ID to get the meta for
352
 * @return array            The stored Facebook shares for a post
353
 */
354
function ppp_fb_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...
355
	return get_post_meta( $post_id, '_ppp_fb_shares', true );
356
}
357
add_filter( 'ppp_get_scheduled_items_fb', 'ppp_fb_get_post_meta', 10, 2 );
358
359
/**
360
 * Registers the thumbnail size for Facebook
361
 * @return void
362
 */
363
function ppp_fb_register_thumbnail_size() {
364
	add_image_size( 'ppp-fb-share-image', 1200, 628, true );
365
}
366
add_action( 'ppp_add_image_sizes', 'ppp_fb_register_thumbnail_size' );
367
368
/**
369
 * Add Facebook to the Meta Box Tabs
370
 * @param  array $tabs Existing Metabox Tabs
371
 * @return array       Metabox tabs with Facebook
372
 */
373
function ppp_fb_add_meta_tab( $tabs ) {
374
	global $ppp_social_settings;
375 1
	if ( ! ppp_facebook_enabled() ) {
376 1
		return $tabs;
377
	}
378
379
	$tabs['fb'] = array( 'name' => __( 'Facebook', 'ppp-txt' ), 'class' => 'icon-ppp-fb' );
380 1
381
	return $tabs;
382 1
}
383
add_filter( 'ppp_metabox_tabs', 'ppp_fb_add_meta_tab', 10, 1 );
384
385
/**
386
 * Add Facebook to the Metabox Content
387
 * @param  array $content The existing metabox content
388
 * @return array          With Facebook
389
 */
390
function ppp_fb_register_metabox_content( $content ) {
391
	global $ppp_social_settings;
392 1
	if ( ! ppp_facebook_enabled() ) {
393 1
		return $content;
394
	}
395
396
	$content[] = 'fb';
397 1
398
	return $content;
399 1
}
400
add_filter( 'ppp_metabox_content', 'ppp_fb_register_metabox_content', 10, 1 );
401
402
/**
403
 * Render the Metabox content for Facebook
404
 * @param  object $post The Post object being edited
405
 */
406
function ppp_fb_add_metabox_content( $post ) {
407
	global $ppp_options, $ppp_share_settings;
408
	$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...
409
410
	$ppp_fb_share_on_publish               = get_post_meta( $post->ID, '_ppp_fb_share_on_publish', true );
411
	$ppp_share_on_publish_title            = get_post_meta( $post->ID, '_ppp_fb_share_on_publish_title', true );
412
	$ppp_fb_share_on_publish_attachment_id = get_post_meta( $post->ID, '_ppp_fb_share_on_publish_attachment_id', true );
413
	$ppp_fb_share_on_publish_image_url     = get_post_meta( $post->ID, '_ppp_fb_share_on_publish_image_url', true );
414
415
	$show_share_on_publish = false;
416
417
	$share_by_default      = empty( $ppp_share_settings['share_on_publish'][ $post->post_type ]['facebook'] ) ? false : true;
418
419
	if ( $ppp_fb_share_on_publish == '1' || ( $ppp_fb_share_on_publish == '' && $share_by_default ) ) {
420
		$show_share_on_publish = true;
421
	}
422
423
	?>
424
	<p>
425
		<div class="ppp-post-override-wrap">
426
			<p><h3><?php _e( 'Share on Facebook', 'ppp-txt' ); ?></h3></p>
427
			<p>
428
				<?php $disabled = ( $post->post_status === 'publish' && time() > strtotime( $post->post_date ) ) ? true : false; ?>
429
				<label for="ppp_fb_share_on_publish"><?php _e( 'Share this post on Facebook&hellip;', 'ppp-txt' ); ?></label>
430
				<select name="_ppp_fb_share_on_publish" id="ppp_fb_share_on_publish" class="ppp-toggle-share-on-publish">
431
					<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>
432
					<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>
433
					<option value="0" <?php selected( false, $show_share_on_publish, true ); ?>><?php _e( 'After this post is published', 'ppp-txt' ); ?></option>
434
				</select>
435
			</p>
436
			<div id="ppp-fb-fields" class="ppp-fields">
437
				<div id="ppp-fb-fields" class="ppp-meta-table-wrap">
438
					<table class="widefat ppp-repeatable-table" width="100%" cellpadding="0" cellspacing="0">
439
						<thead>
440
							<tr>
441
								<th style="width: 100px"><?php _e( 'Date', 'ppp-txt' ); ?></th>
442
								<th style="width: 75px;"><?php _e( 'Time', 'ppp-txt' ); ?></th>
443
								<th><?php _e( 'Link Message', 'ppp-txt' ); ?></th>
444
								<th style"width: 200px;"><?php _e( 'Image', 'ppp-txt' ); ?></th>
445
								<th style="width: 10px;"></th>
446
							</tr>
447
						</thead>
448
						<tbody id="fb-share-on-publish" class="ppp-share-on-publish" <?php if ( false === $show_share_on_publish ) : echo 'style="display: none;"'; endif; ?>>
449
							<?php
450
								$args = array(
451
									'text'          => $ppp_share_on_publish_title,
452
									'attachment_id' => $ppp_fb_share_on_publish_attachment_id,
453
									'image'         => $ppp_fb_share_on_publish_image_url,
454
								);
455
456
								ppp_render_fb_share_on_publish_row( $args );
457
							?>
458
						</tbody>
459
						<tbody id="fb-schedule-share" class="ppp-schedule-share" <?php if ( true === $show_share_on_publish ) : echo 'style="display: none;"'; endif; ?>>
460
							<?php $shares = get_post_meta( $post->ID, '_ppp_fb_shares', true ); ?>
461
							<?php if ( ! empty( $shares ) ) : ?>
462
463
								<?php foreach ( $shares as $key => $value ) :
464
									$date          = isset( $value['date'] )          ? $value['date']          : '';
465
									$time          = isset( $value['time'] )          ? $value['time']          : '';
466
									$text          = isset( $value['text'] )          ? $value['text']          : '';
467
									$image         = isset( $value['image'] )         ? $value['image']         : '';
468
									$attachment_id = isset( $value['attachment_id'] ) ? $value['attachment_id'] : '';
469
470
									$args = apply_filters( 'ppp_fb_row_args', compact( 'date','time','text','image','attachment_id' ), $value );
471
									?>
472
473
									<?php ppp_render_fb_share_row( $key, $args, $post->ID ); ?>
474
475
476
								<?php endforeach; ?>
477
478
							<?php else: ?>
479
480
								<?php ppp_render_fb_share_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_fb_share_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...
481
482
							<?php endif; ?>
483
						</tbody>
484
					</table>
485
				</div>
486
			</div><!--end #edd_variable_price_fields-->
487
488
			<p><?php _e( 'Do not include links in your text, this will be added automatically.', 'ppp-txt' ); ?></p>
489
		</div>
490
		<?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' ); ?>
491
	</p>
492
	<?php
493
}
494
add_action( 'ppp_generate_metabox_content-fb', 'ppp_fb_add_metabox_content', 10, 1 );
495
496
/**
497
 * Render the Facebook share on publish row
498
 *
499
 * @since  2.3
500
 * @param  array  $args Contains share on publish data, if there is any
501
 * @return void
502
 */
503
function ppp_render_fb_share_on_publish_row( $args = array() ) {
504
	global $post;
505
	$readonly = $post->post_status !== 'publish' ? '' : 'readonly="readonly" ';
506
	?>
507
	<tr class="ppp-fb-wrapper ppp-repeatable-row on-publish-row">
508
		<td colspan="2" class="ppp-on-plublish-date-column">
509
			<?php _e( 'Share On Publish', 'ppp-txt' ); ?>
510
		</td>
511
512
		<td>
513
			<input <?php echo $readonly; ?>class="ppp-tweet-text-repeatable" type="text" name="_ppp_fb_share_on_publish_title" value="<?php echo esc_attr( $args['text'] ); ?>" />
514
		</td>
515
516
		<td class="ppp-repeatable-upload-wrapper" style="width: 200px">
517
			<div class="ppp-repeatable-upload-field-container">
518
				<input type="hidden" name="_ppp_fb_share_on_publish_attachment_id" class="ppp-repeatable-attachment-id-field" value="<?php echo esc_attr( absint( $args['attachment_id'] ) ); ?>"/>
519
				<input <?php echo $readonly; ?>type="text" class="ppp-repeatable-upload-field ppp-upload-field" name="_ppp_fb_share_on_publish_image_url" placeholder="<?php _e( 'Upload or Enter URL', 'ppp-txt' ); ?>" value="<?php echo esc_attr( $args['image'] ); ?>" />
520
521
				<span class="ppp-upload-file">
522
					<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;">
523
						<span class="dashicons dashicons-upload"></span>
524
					</a>
525
				</span>
526
527
			</div>
528
		</td>
529
530
		<td>&nbsp;</td>
531
532
	</tr>
533
<?php
534
}
535
536
/**
537
 * Render the scheduled share row for Facebook
538
 *
539
 * @since  2.3
540
 * @param  int $key        The key in the array
541
 * @param  array  $args    Arguements for the current post's share data
542
 * @param  int    $post_id The post ID being edited
543
 * @return void
544
 */
545
function ppp_render_fb_share_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...
546
	global $post;
547
548
	$share_time = ppp_generate_timestamp( $args['date'], $args['time'] );
549
	$readonly   = ppp_generate_timestamp() > $share_time ? 'readonly="readonly" ' : false;
550
	$no_date    = ! empty( $readonly ) ? ' hasDatepicker' : '';
551
	$hide       = ! empty( $readonly ) ? 'display: none;' : '';
552
	?>
553
	<tr class="ppp-fb-wrapper ppp-repeatable-row ppp-repeatable-facebook scheduled-row" data-key="<?php echo esc_attr( $key ); ?>">
554
		<td>
555
			<input <?php echo $readonly; ?>type="text" class="share-date-selector<?php echo $no_date; ?>" name="_ppp_fb_shares[<?php echo $key; ?>][date]" placeholder="mm/dd/yyyy" value="<?php echo $args['date']; ?>" />
556
		</td>
557
558
		<td>
559
			<input <?php echo $readonly; ?>type="text" class="share-time-selector" name="_ppp_fb_shares[<?php echo $key; ?>][time]" value="<?php echo $args['time']; ?>" />
560
		</td>
561
562
		<td>
563
			<input <?php echo $readonly; ?>class="ppp-tweet-text-repeatable" type="text" name="_ppp_fb_shares[<?php echo $key; ?>][text]" value="<?php echo esc_attr( $args['text'] ); ?>" />
564
		</td>
565
566
		<td class="ppp-repeatable-upload-wrapper" style="width: 200px">
567
			<div class="ppp-repeatable-upload-field-container">
568
				<input type="hidden" name="_ppp_fb_shares[<?php echo $key; ?>][attachment_id]" class="ppp-repeatable-attachment-id-field" value="<?php echo esc_attr( absint( $args['attachment_id'] ) ); ?>"/>
569
				<input <?php echo $readonly; ?>type="text" class="ppp-repeatable-upload-field ppp-upload-field" name="_ppp_fb_shares[<?php echo $key; ?>][image]" placeholder="<?php _e( 'Upload or Enter URL', 'ppp-txt' ); ?>" value="<?php echo esc_attr( $args['image'] ); ?>" />
570
571
				<span class="ppp-upload-file" style="<?php echo $hide; ?>">
572
					<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;">
573
						<span class="dashicons dashicons-upload"></span>
574
					</a>
575
				</span>
576
577
			</div>
578
		</td>
579
580
		<td>
581
			<a href="#" class="ppp-repeatable-row ppp-remove-repeatable" data-type="facebook" style="<?php echo $hide; ?>"><i class="fa fa-trash" aria-hidden="true"></i></a>
582
		</td>
583
584
	</tr>
585
<?php
586
}
587
588
/**
589
 * Save the items in our meta boxes
590
 * @param  int $post_id The Post ID being saved
591
 * @param  object $post    The Post Object being saved
592
 * @return int          The Post ID
593
 */
594
function ppp_fb_save_post_meta_boxes( $post_id, $post ) {
595
596
	if ( ! ppp_should_save( $post_id, $post ) ) {
597
		return;
598
	}
599
600
	$ppp_fb_share_on_publish            = ( isset( $_REQUEST['_ppp_fb_share_on_publish'] ) )               ? $_REQUEST['_ppp_fb_share_on_publish']               : '-1';
601
	$ppp_share_on_publish_title         = ( isset( $_REQUEST['_ppp_fb_share_on_publish_title'] ) )         ? $_REQUEST['_ppp_fb_share_on_publish_title']         : '';
602
	$ppp_share_on_publish_image_url     = ( isset( $_REQUEST['_ppp_fb_share_on_publish_image_url'] ) )     ? $_REQUEST['_ppp_fb_share_on_publish_image_url']     : '';
603
	$ppp_share_on_publish_attachment_id = ( isset( $_REQUEST['_ppp_fb_share_on_publish_attachment_id'] ) ) ? $_REQUEST['_ppp_fb_share_on_publish_attachment_id'] : '';
604
605
	update_post_meta( $post_id, '_ppp_fb_share_on_publish',               $ppp_fb_share_on_publish );
606
	update_post_meta( $post_id, '_ppp_fb_share_on_publish_title',         $ppp_share_on_publish_title );
607
	update_post_meta( $post_id, '_ppp_fb_share_on_publish_image_url',     $ppp_share_on_publish_image_url );
608
	update_post_meta( $post_id, '_ppp_fb_share_on_publish_attachment_id', $ppp_share_on_publish_attachment_id );
609
610
	$fb_data = ( isset( $_REQUEST['_ppp_fb_shares'] ) && empty( $ppp_fb_share_on_publish ) ) ? $_REQUEST['_ppp_fb_shares'] : array();
611
	foreach ( $fb_data as $index => $share ) {
612
		$fb_data[ $index ]['text'] = sanitize_text_field( $share['text'] );
613
	}
614
615
	update_post_meta( $post_id, '_ppp_fb_shares', $fb_data );
616
}
617
add_action( 'save_post', 'ppp_fb_save_post_meta_boxes', 10, 2 ); // save the custom fields
618
619
/**
620
 * Share a Facebook post on Publish
621
 * @param  string $old_status The old post status
622
 * @param  string $new_status The new post status
623
 * @param  object $post       The Post object
624
 * @return void
625
 */
626
function ppp_fb_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...
627
	global $ppp_options;
628
629
	$from_meta = ! empty( $_POST['ppp_post_edit'] ) ? false : get_post_meta( $post->ID, '_ppp_fb_share_on_publish', true );
630
	$from_post = isset( $_POST['_ppp_fb_share_on_publish'] ) ? $_POST['_ppp_fb_share_on_publish'] : '0';
631
632
	if ( '1' != $from_meta && '1' != $from_post ) {
633
		return;
634
	}
635
636
	$from_meta = $from_meta == '1' ? true : false;
637
	$from_post = $from_post == '1' ? true : false;
638
639
	$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...
640
	$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...
641
	$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...
642
643
	// Determine if we're seeing the share on publish in meta or $_POST
644
	if ( $from_meta && ! $from_post ) {
645
		$title         = get_post_meta( $post->ID, '_ppp_fb_share_on_publish_title',         true );
646
		$attachment_id = get_post_meta( $post->ID, '_ppp_fb_share_on_publish_attachment_id', true );
647
		$image_url     = get_post_meta( $post->ID, '_ppp_fb_share_on_publish_image_url',     true );
648
	} else {
649
		$title         = isset( $_POST['_ppp_fb_share_on_publish_title'] )         ? $_POST['_ppp_fb_share_on_publish_title']         : '';
650
		$attachment_id = isset( $_POST['_ppp_fb_share_on_publish_attachment_id'] ) ? $_POST['_ppp_fb_share_on_publish_attachment_id'] : 0;
651
		$image_url     = isset( $_POST['_ppp_fb_share_on_publish_image_url'] )     ? $_POST['_ppp_fb_share_on_publish_image_url']     : '';
652
	}
653
654
	$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...
655
	if ( empty( $attachment_id ) && ! empty( $image_url ) ) {
656
		$thumbnail = $image_url;
657
	} else {
658
		$thumbnail = ppp_post_has_media( $post->ID, 'fb', true, $attachment_id );
659
	}
660
661
	$name = 'sharedate_0_' . $post->ID . '_fb';
662
663
	$default_title = isset( $ppp_options['default_text'] ) ? $ppp_options['default_text'] : '';
664
	// If an override was found, use it, otherwise try the default text content
665
	if ( empty( $title ) && empty( $default_title ) ) {
666
		$title = get_the_title( $post->ID );
667
	}
668
669
	$title = apply_filters( 'ppp_share_content', $title, array( 'post_id' => $post->ID ) );
670
	$link  = ppp_generate_link( $post->ID, $name, true );
671
672
	$status = ppp_fb_share( $link, $title, $thumbnail );
673
674
	$log_title = ppp_fb_build_share_message( $post->ID, $name );
675
676
	$log_data = array(
677
		'post_title'    => $log_title,
678
		'post_content'  => '',
679
		'post_parent'   => $post->ID,
680
		'log_type'      => 'ppp_share'
681
	);
682
683
	$log_meta = array(
684
		'network'   => 'fb',
685
		'share_id'  => 0,
686
	);
687
688
	$log_entry = WP_Logging::insert_log( $log_data, $log_meta );
689
690
	update_post_meta( $log_entry, '_ppp_share_status', $status );
691
}
692
add_action( 'ppp_share_on_publish', 'ppp_fb_share_on_publish', 10, 3 );
693
694
/**
695
 * Generate the timestamps and names for the scheduled Facebook shares
696
 *
697
 * @since  2.3
698
 * @param  array $times   The times to save
699
 * @param  int   $post_id The Post ID of the item being saved
700
 * @return array          Array of timestamps and cron names
701
 */
702
function ppp_fb_generate_timestamps( $times, $post_id ) {
703
	$fb_shares = get_post_meta( $post_id, '_ppp_fb_shares', true );
704 1
705
	if ( empty( $fb_shares ) ) {
706 1
		$fb_shares = array();
707 1
	}
708 1
709
	foreach ( $fb_shares as $key => $data ) {
710 1
		if ( ! array_filter( $data ) ) {
711
			continue;
712
		}
713
714
		$timestamp = ppp_generate_timestamp( $data['date'], $data['time'] );
715
716
		if ( $timestamp > current_time( 'timestamp', 1 ) ) { // Make sure the timestamp we're getting is in the future
717
			$time_key           = strtotime( date_i18n( 'd-m-Y H:i:s', $timestamp , true ) ) . '_fb';
718
			$times[ $time_key ] = 'sharedate_' . $key . '_' . $post_id . '_fb';
719
		}
720
721
	}
722 1
723
	return $times;
724 1
}
725
add_filter( 'ppp_get_timestamps', 'ppp_fb_generate_timestamps', 10, 2 );
726
727
/**
728
 * Build the text for the Facebook share
729
 *
730
 * @since  2.3
731
 * @param  int     $post_id   The Post ID
732
 * @param  string  $name      The cron name
733
 * @param  boolean $scheduled If the item is being fired by a schedule (default, true), or retrieved for display (false)
734
 * @return string             The message to share
735
 */
736
function ppp_fb_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...
737
	$share_content = ppp_fb_generate_share_content( $post_id, $name );
738
739
	return apply_filters( 'ppp_fb_build_share_message', $share_content );
740
}
741
742
/**
743
 * Build the link for the Facebook Share
744
 *
745
 * @since  2.3
746
 * @param  int     $post_id   The post ID being shared
747
 * @param  string  $name      The cron name
748
 * @param  boolean $scheduled If the item is being fired by a schedule (default, true), or retrieved for display (false)
749
 * @return string             The formatted link to the post
750
 */
751
function ppp_fb_build_share_link( $post_id, $name, $scheduled = true ) {
752
	$share_link = ppp_generate_link( $post_id, $name, $scheduled );
753
754
	return $share_link;
755
}
756
757
/**
758
 * The worker function for ppp_fb_build_share_message
759
 *
760
 * @since  2.3
761
 * @param  int     $post_id      Post ID
762
 * @param  string  $name         The cron name
763
 * @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...
764
 * @return string                The formatted link to the post
765
 */
766
function ppp_fb_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...
767
	global $ppp_options;
768
	$default_text = isset( $ppp_options['default_text'] ) ? $ppp_options['default_text'] : '';
769
	$fb_shares    = get_post_meta( $post_id, '_ppp_fb_shares', true );
770
771
	if ( ! empty( $fb_shares ) ) {
772
		$name_array    = explode( '_', $name );
773
		$index         = $name_array[1];
774
		if ( isset( $fb_shares[ $index ] ) ) {
775
			$share_content = $fb_shares[ $index ]['text'];
776
		}
777
	}
778
779
	// If an override was found, use it, otherwise try the default text content
780
	$share_content = ( isset( $share_content ) && !empty( $share_content ) ) ? $share_content : $default_text;
781
782
	// If the content is still empty, just use the post title
783
	$share_content = ( isset( $share_content ) && !empty( $share_content ) ) ? $share_content : get_the_title( $post_id );
784
785
	return apply_filters( 'ppp_share_content_fb', $share_content, array( 'post_id' => $post_id ) );
786
}
787
788
/**
789
 * Return if media is supported for this scheduled post
790
 * @param  int $post_id The Post ID
791
 * @param  int $index   The index of this tweet in the _ppp_tweets data
792
 * @return bool         Whether or not this tweet should contain a media post
793
 */
794
function ppp_fb_use_media( $post_id, $index ) {
795
	if ( empty( $post_id ) || empty( $index ) ) {
796
		return false;
797
	}
798
799
	return true; // Always include an image for facebook, even if it's a fallback to the featured image
800
}
801
802
/**
803
 * Update the Post As field for Facebook
804
 * @return sends 1 when successfully updated
805
 */
806
function ppp_fb_update_page() {
807
	global $ppp_social_settings, $ppp_facebook_oauth;
808
809
	ppp_set_social_tokens();
810
811
	$account = isset( $_POST['account'] ) ? $_POST['account'] : false;
812
813
	if ( !empty( $account ) ) {
814
		$ppp_social_settings['facebook']->page = $account;
815
816
		update_option( 'ppp_social_settings', $ppp_social_settings );
817
		echo 1;
818
	} else {
819
		echo 0;
820
	}
821
822
	die(); // this is required to return a proper result
823
}
824
add_action( 'wp_ajax_fb_set_page', 'ppp_fb_update_page' );
825
826
function ppp_fb_calendar_on_publish_event( $events, $post_id ) {
827
	$share_on_publish = get_post_meta( $post_id, '_ppp_fb_share_on_publish', true );
828
829
	if ( ! empty( $share_on_publish ) ) {
830
		$share_text = get_post_meta( $post_id, '_ppp_fb_share_on_publish_title', true );
831
		$events[] = array(
832
			'id' => $post_id . '-share-on-publish',
833
			'title' => ( ! empty( $share_text ) ) ? $share_text : ppp_fb_generate_share_content( $post_id, null, false ),
834
			'start'     => date_i18n( 'Y-m-d/TH:i:s', strtotime( get_the_date( null, $post_id ) . ' ' . get_the_time( null, $post_id ) ) + 1 ),
835
			'end'       => date_i18n( 'Y-m-d/TH:i:s', strtotime( get_the_date( null, $post_id ) . ' ' . get_the_time( null, $post_id ) ) + 1 ),
836
			'className' => 'ppp-calendar-item-fb cal-post-' . $post_id,
837
			'belongsTo' => $post_id,
838
		);
839
	}
840
841
	return $events;
842
}
843
add_filter( 'ppp_calendar_on_publish_event', 'ppp_fb_calendar_on_publish_event', 10, 2 );
844
845
function ppp_fb_get_post_shares( $items, $post_id ) {
846
	$shares = get_post_meta( $post_id, '_ppp_fb_shares', true );
847
	if ( empty( $shares ) ) { return $items; }
848
849
	foreach ( $shares as $key => $share ) {
850
		$items[] = array( 'id' => $key, 'service' => 'fb' );
851
	}
852
	return $items;
853
}
854
add_filter( 'ppp_get_post_scheduled_shares', 'ppp_fb_get_post_shares', 10, 2 );
855
856
/**
857
 * When a post is updated, if the status is 'publish', clear the Open Graph cache
858
 *
859
 * @since 2.3.11
860
 * 
861
 * @param $post_id
862
 * @param $post_after
863
 * @param $post_before
864
 */
865
function ppp_fb_clear_open_graph_cache( $post_id, $post_after, $post_before ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post_before 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...
866
	$post_types = ppp_allowed_post_types();
867
	if ( ! in_array( $post_after->post_type, $post_types ) ) {
868
		return;
869
	}
870
871
	if ( 'publish' == $post_after->post_status ) {
872
		global $ppp_facebook_oauth;
873
874
		return $ppp_facebook_oauth->clear_og_cache( $post_id );
875
	}
876
}
877
add_action( 'post_updated', 'ppp_fb_clear_open_graph_cache', 10, 3 );
878