GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#175)
by Chris
04:24
created

facebook-functions.php ➔ ppp_fb_share_on_publish()   F

Complexity

Conditions 16
Paths 578

Size

Total Lines 65
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 16
eloc 41
c 4
b 0
f 0
nc 578
nop 3
dl 0
loc 65
rs 3.2461
ccs 0
cts 41
cp 0
crap 272

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