Completed
Push — master ( 8a7e31...f76ead )
by Armando
12s
created
lib/class-post.php 2 patches
Indentation   +740 added lines, -740 removed lines patch added patch discarded remove patch
@@ -18,625 +18,625 @@  discard block
 block discarded – undo
18 18
  */
19 19
 class WP2D_Post {
20 20
 
21
-	/**
22
-	 * The original post object.
23
-	 *
24
-	 * @var WP_Posts
25
-	 * @since 1.5.0
26
-	 */
27
-	public $post = null;
28
-
29
-	/**
30
-	 * The original post ID.
31
-	 *
32
-	 * @var int
33
-	 * @since 1.5.0
34
-	 */
35
-	public $ID = null;
36
-
37
-	/**
38
-	 * If this post should be shared on diaspora*.
39
-	 *
40
-	 * @var bool
41
-	 * @since 1.5.0
42
-	 */
43
-	public $post_to_diaspora = null;
44
-
45
-	/**
46
-	 * If a link back to the original post should be added.
47
-	 *
48
-	 * @var bool
49
-	 * @since 1.5.0
50
-	 */
51
-	public $fullentrylink = null;
52
-
53
-	/**
54
-	 * What content gets posted.
55
-	 *
56
-	 * @var string
57
-	 * @since 1.5.0
58
-	 */
59
-	public $display = null;
60
-
61
-	/**
62
-	 * The types of tags to post. (global,custom,post)
63
-	 *
64
-	 * @var array
65
-	 * @since 1.5.0
66
-	 */
67
-	public $tags_to_post = null;
68
-
69
-	/**
70
-	 * The post's custom tags.
71
-	 *
72
-	 * @var array
73
-	 * @since 1.5.0
74
-	 */
75
-	public $custom_tags = null;
76
-
77
-	/**
78
-	 * Aspects this post gets posted to.
79
-	 *
80
-	 * @var array
81
-	 * @since 1.5.0
82
-	 */
83
-	public $aspects = null;
84
-
85
-	/**
86
-	 * Services this post gets posted to.
87
-	 *
88
-	 * @var array
89
-	 * @since 1.5.0
90
-	 */
91
-	public $services = null;
92
-
93
-
94
-	/**
95
-	 * The post's history of diaspora* posts.
96
-	 *
97
-	 * @var array
98
-	 * @since 1.5.0
99
-	 */
100
-	public $post_history = null;
101
-
102
-	/**
103
-	 * If the post actions have all been set up already.
104
-	 *
105
-	 * @var boolean
106
-	 * @since 1.5.0
107
-	 */
108
-	private static $_is_set_up = false;
109
-
110
-	/**
111
-	 * Setup all the necessary WP callbacks.
112
-	 *
113
-	 * @since 1.5.0
114
-	 */
115
-	public static function setup() {
116
-		if ( self::$_is_set_up ) {
117
-			return;
118
-		}
119
-
120
-		$instance = new WP2D_Post( null );
121
-
122
-		// Notices when a post has been shared or if it has failed.
123
-		add_action( 'admin_notices', array( $instance, 'admin_notices' ) );
124
-		add_action( 'admin_init', array( $instance, 'ignore_post_error' ) );
125
-
126
-		// Handle diaspora* posting when saving the post.
127
-		add_action( 'save_post', array( $instance, 'post' ), 11, 2 );
128
-		add_action( 'save_post', array( $instance, 'save_meta_box_data' ), 10 );
129
-
130
-		// Add meta boxes.
131
-		add_action( 'add_meta_boxes', array( $instance, 'add_meta_boxes' ) );
132
-
133
-		self::$_is_set_up = true;
134
-	}
135
-
136
-	/**
137
-	 * Constructor.
138
-	 *
139
-	 * @since 1.5.0
140
-	 *
141
-	 * @param int|WP_Post $post Post ID or the post itself.
142
-	 */
143
-	public function __construct( $post ) {
144
-		$this->_assign_wp_post( $post );
145
-	}
146
-
147
-	/**
148
-	 * Assign the original WP_Post object and all the custom meta data.
149
-	 *
150
-	 * @since 1.5.0
151
-	 *
152
-	 * @param int|WP_Post $post Post ID or the post itself.
153
-	 */
154
-	private function _assign_wp_post( $post ) {
155
-		if ( $this->post = get_post( $post ) ) {
156
-			$this->ID = $this->post->ID;
157
-
158
-			$options = WP2D_Options::instance();
159
-
160
-			// Assign all meta values, expanding non-existent ones with the defaults.
161
-			$meta_current = get_post_meta( $this->ID, '_wp_to_diaspora', true );
162
-			$meta = wp_parse_args(
163
-				$meta_current,
164
-				$options->get_options()
165
-			);
166
-			if ( $meta ) {
167
-				foreach ( $meta as $key => $value ) {
168
-					$this->$key = $value;
169
-				}
170
-			}
171
-
172
-			// If no WP2D meta data has been saved yet, this post shouldn't be published.
173
-			// This can happen if existing posts (before WP2D) get updated externally, not through the post edit screen.
174
-			// Check gutobenn/wp-to-diaspora#91 for reference.
175
-			// Also, when we have a post scheduled for publishing, don't touch it.
176
-			// This is important when modifying scheduled posts using Quick Edit.
177
-			if ( ! in_array( $this->post->post_status, array( 'auto-draft', 'future' ) ) && ! $meta_current ) {
178
-				$this->post_to_diaspora = false;
179
-			}
180
-
181
-			$this->post_history = get_post_meta( $this->ID, '_wp_to_diaspora_post_history', true );
182
-		}
183
-	}
184
-
185
-	/**
186
-	 * Post to diaspora* when saving a post.
187
-	 *
188
-	 * @since 1.5.0
189
-	 *
190
-	 * @todo Maybe somebody wants to share a password protected post to a closed aspect.
191
-	 *
192
-	 * @param integer $post_id ID of the post being saved.
193
-	 * @param WP_Post $post    Post object being saved.
194
-	 * @return boolean If the post was posted successfully.
195
-	 */
196
-	public function post( $post_id, $post ) {
197
-		$this->_assign_wp_post( $post );
198
-
199
-		$options = WP2D_Options::instance();
200
-
201
-		// Is this post type enabled for posting?
202
-		if ( ! in_array( $post->post_type, $options->get_option( 'enabled_post_types' ) ) ) {
203
-			return false;
204
-		}
205
-
206
-		// Make sure we're posting to diaspora* and the post isn't password protected.
207
-		if ( ! ( $this->post_to_diaspora && 'publish' === $post->post_status && '' === $post->post_password ) ) {
208
-			return false;
209
-		}
210
-
211
-		$status_message = $this->_get_title_link();
212
-
213
-		// Post the full post text or just the excerpt?
214
-		if ( 'full' === $this->display ) {
215
-			$status_message .= $this->_get_full_content();
216
-		} else {
217
-			$status_message .= $this->_get_excerpt_content();
218
-		}
219
-
220
-		// Add the tags assigned to the post.
221
-		$status_message .= $this->_get_tags_to_add();
222
-
223
-		// Add the original entry link to the post?
224
-		$status_message .= $this->_get_posted_at_link();
225
-
226
-		$status_converter = new HtmlConverter( array( 'strip_tags' => true ) );
227
-		$status_message  = $status_converter->convert( $status_message );
228
-
229
-		// Set up the connection to diaspora*.
230
-		$api = WP2D_Helpers::api_quick_connect();
231
-		if ( ! empty( $status_message ) ) {
232
-			if ( $api->has_last_error() ) {
233
-				// Save the post error as post meta data, so we can display it to the user.
234
-				update_post_meta( $post_id, '_wp_to_diaspora_post_error', $api->get_last_error() );
235
-				return false;
236
-			}
237
-
238
-			// Add services to share to via diaspora*.
239
-			$extra_data = array(
240
-				'services' => $this->services,
241
-			);
242
-
243
-			// Try to post to diaspora*.
244
-			if ( $response = $api->post( $status_message, $this->aspects, $extra_data ) ) {
245
-				// Save certain diaspora* post data as meta data for future reference.
246
-				$this->_save_to_history( (object) $response );
247
-
248
-				// If there is still a previous post error around, remove it.
249
-				delete_post_meta( $post_id, '_wp_to_diaspora_post_error' );
250
-
251
-				// Unset post_to_diaspora meta field to prevent mistakenly republishing to diaspora*.
252
-				$meta = get_post_meta( $post_id, '_wp_to_diaspora', true );
253
-				$meta['post_to_diaspora'] = false;
254
-				update_post_meta( $post_id, '_wp_to_diaspora', $meta );
255
-			}
256
-		} else {
257
-			return false;
258
-		}
259
-	}
260
-
261
-	/**
262
-	 * Get the title of the post linking to the post itself.
263
-	 *
264
-	 * @since 1.5.0
265
-	 *
266
-	 * @return string Post title as a link.
267
-	 */
268
-	private function _get_title_link() {
269
-		$title = esc_html( $this->post->post_title );
270
-		$permalink = get_permalink( $this->ID );
271
-		$default = sprintf( '<strong><a href="%2$s" title="%2$s">%1$s</a></strong>', $title, $permalink );
272
-
273
-		/**
274
-		 * Filter the title link at the top of the post.
275
-		 *
276
-		 * @since 1.5.4.1
277
-		 *
278
-		 * @param string $default   The whole HTML of the title link to be outputted.
279
-		 * @param string $title     The title of the original post.
280
-		 * @param string $permalink The permalink of the original post.
281
-		 */
282
-		$link = apply_filters( 'wp2d_title_filter', $default, $title, $permalink );
283
-
284
-		return '<p>' . $link . '</p>';
285
-	}
286
-
287
-	/**
288
-	 * Get the full post content with only default filters applied.
289
-	 *
290
-	 * @since 1.5.0
291
-	 *
292
-	 * @return string The full post content.
293
-	 */
294
-	private function _get_full_content() {
295
-		// Only allow certain shortcodes.
296
-		global $shortcode_tags;
297
-		$shortcode_tags_bkp = array();
298
-
299
-		foreach ( $shortcode_tags as $shortcode_tag => $shortcode_function ) {
300
-			if ( ! in_array( $shortcode_tag, apply_filters( 'wp2d_shortcodes_filter', array( 'wp_caption', 'caption', 'gallery' ) ) ) ) {
301
-				$shortcode_tags_bkp[ $shortcode_tag ] = $shortcode_function;
302
-				unset( $shortcode_tags[ $shortcode_tag ] );
303
-			}
304
-		}
305
-
306
-		// Disable all filters and then enable only defaults. This prevents additional filters from being posted to diaspora*.
307
-		remove_all_filters( 'the_content' );
308
-		foreach ( apply_filters( 'wp2d_content_filters_filter', array( 'do_shortcode', 'wptexturize', 'convert_smilies', 'convert_chars', 'wpautop', 'shortcode_unautop', 'prepend_attachment', array( $this, 'embed_remove' ) ) ) as $filter ) {
309
-			add_filter( 'the_content', $filter );
310
-		}
311
-
312
-		// Extract URLs from [embed] shortcodes.
313
-		add_filter( 'embed_oembed_html', array( $this, 'embed_url' ), 10, 2 );
314
-
315
-		// Add the pretty caption after the images.
316
-		add_filter( 'img_caption_shortcode', array( $this, 'custom_img_caption' ), 10, 3 );
317
-
318
-		// Overwrite the native shortcode handler to add pretty captions.
319
-		// http://wordpress.stackexchange.com/a/74675/54456 for explanation.
320
-		add_shortcode( 'gallery', array( $this, 'custom_gallery_shortcode' ) );
321
-
322
-		$full_content = apply_filters( 'the_content', $this->post->post_content );
323
-
324
-		// Put the removed shortcode tags back again.
325
-		$shortcode_tags += $shortcode_tags_bkp;
326
-
327
-		return $full_content;
328
-	}
329
-
330
-	/**
331
-	 * Get the post's excerpt in a nice format.
332
-	 *
333
-	 * @since 1.5.0
334
-	 *
335
-	 * @return string Post's excerpt.
336
-	 */
337
-	private function _get_excerpt_content() {
338
-		// Look for the excerpt in the following order:
339
-		// 1. Custom post excerpt.
340
-		// 2. Text up to the <!--more--> tag.
341
-		// 3. Manually trimmed content.
342
-		$content = $this->post->post_content;
343
-		$excerpt = $this->post->post_excerpt;
344
-		if ( '' === $excerpt ) {
345
-			if ( $more_pos = strpos( $content, '<!--more' ) ) {
346
-				$excerpt = substr( $content, 0, $more_pos );
347
-			} else {
348
-				$excerpt = wp_trim_words( $content, 42, '[...]' );
349
-			}
350
-		}
351
-		return '<p>' . $excerpt . '</p>';
352
-	}
353
-
354
-	/**
355
-	 * Get a string of tags that have been added to the post.
356
-	 *
357
-	 * @since 1.5.0
358
-	 *
359
-	 * @return string Tags added to the post.
360
-	 */
361
-	private function _get_tags_to_add() {
362
-		$options = WP2D_Options::instance();
363
-		$tags_to_post = $this->tags_to_post;
364
-		$tags_to_add  = '';
365
-
366
-		// Add any diaspora* tags?
367
-		if ( ! empty( $tags_to_post ) ) {
368
-			// The diaspora* tags to add to the post.
369
-			$diaspora_tags = array();
370
-
371
-			// Add global tags?
372
-			$global_tags = $options->get_option( 'global_tags' );
373
-			if ( in_array( 'global', $tags_to_post ) && is_array( $global_tags ) ) {
374
-				$diaspora_tags += array_flip( $global_tags );
375
-			}
376
-
377
-			// Add custom tags?
378
-			if ( in_array( 'custom', $tags_to_post ) && is_array( $this->custom_tags ) ) {
379
-				$diaspora_tags += array_flip( $this->custom_tags );
380
-			}
381
-
382
-			// Add post tags?
383
-			$post_tags = wp_get_post_tags( $this->ID, array( 'fields' => 'slugs' ) );
384
-			if ( in_array( 'post', $tags_to_post ) && is_array( $post_tags ) ) {
385
-				$diaspora_tags += array_flip( $post_tags );
386
-			}
387
-
388
-			// Get an array of cleaned up tags.
389
-			// NOTE: Validate method needs a variable, as it's passed by reference!
390
-			$diaspora_tags = array_keys( $diaspora_tags );
391
-			$options->validate_tags( $diaspora_tags );
392
-
393
-			// Get all the tags and list them all nicely in a row.
394
-			$diaspora_tags_clean = array();
395
-			foreach ( $diaspora_tags as $tag ) {
396
-				$diaspora_tags_clean[] = '#' . $tag;
397
-			}
398
-
399
-			// Add all the found tags.
400
-			if ( ! empty( $diaspora_tags_clean ) ) {
401
-				$tags_to_add = implode( ' ', $diaspora_tags_clean ) . '<br />';
402
-			}
403
-		}
404
-
405
-		return $tags_to_add;
406
-	}
407
-
408
-	/**
409
-	 * Get the link to the original post.
410
-	 *
411
-	 * @since 1.5.0
412
-	 *
413
-	 * @return string Original post link.
414
-	 */
415
-	private function _get_posted_at_link() {
416
-		$link = '';
417
-		if ( $this->fullentrylink ) {
418
-
419
-			$text = esc_html( 'Originally posted at:', 'wp-to-diaspora' );
420
-			$permalink = get_permalink( $this->ID );
421
-			$title = esc_html( 'Permalink', 'wp-to-diaspora' );
422
-			$default = sprintf( '%1$s <a href="%2$s" title="%3$s">%2$s</a>', $text, $permalink, $title );
423
-
424
-			/**
425
-			 * Filter the "Originally posted at" link at the bottom of the post.
426
-			 *
427
-			 * @since 1.5.4.1
428
-			 *
429
-			 * @param string $default   The whole HTML of the text and link to be outputted.
430
-			 * @param string $text      The "Originally posted at:" text before the link.
431
-			 * @param string $permalink The permalink of the original post.
432
-			 * @param string $title     The "Permalink" title of the link.
433
-			 */
434
-			$link = apply_filters( 'wp2d_posted_at_link_filter', $default, $text, $permalink, $title );
435
-
436
-			$link = '<p>' . $link . '</p>';
437
-		}
438
-
439
-		return $link;
440
-	}
441
-
442
-	/**
443
-	 * Save the details of the new diaspora* post to this post's history.
444
-	 *
445
-	 * @since 1.5.0
446
-	 *
447
-	 * @param object $response Response from the API containing the diaspora* post details.
448
-	 */
449
-	private function _save_to_history( $response ) {
450
-		// Make sure the post history is an array.
451
-		if ( empty( $this->post_history ) ) {
452
-			$this->post_history = array();
453
-		}
454
-
455
-		// Add a new entry to the history.
456
-		$this->post_history[] = array(
457
-			'id'         => $response->id,
458
-			'guid'       => $response->guid,
459
-			'created_at' => $this->post->post_modified,
460
-			'aspects'    => $this->aspects,
461
-			'nsfw'       => $response->nsfw,
462
-			'post_url'   => $response->permalink,
463
-		);
464
-
465
-		update_post_meta( $this->ID, '_wp_to_diaspora_post_history', $this->post_history );
466
-	}
467
-
468
-	/**
469
-	 * Return URL from [embed] shortcode instead of generated iframe.
470
-	 *
471
-	 * @since 1.5.0
472
-	 * @see WP_Embed::shortcode()
473
-	 *
474
-	 * @param mixed  $html The cached HTML result, stored in post meta.
475
-	 * @param string $url  The attempted embed URL.
476
-	 * @return string URL of the embed.
477
-	 */
478
-	public function embed_url( $html, $url ) {
479
-		return $url;
480
-	}
481
-
482
-	/**
483
-	 * Removes '[embed]' and '[/embed]' left by embed_url.
484
-	 *
485
-	 * @since 1.5.0
486
-	 * @todo It would be great to fix it using only one filter.
487
-	 *       It's happening because embed filter is being removed by remove_all_filters('the_content') on WP2D_Post::post().
488
-	 *
489
-	 * @param string $content Content of the post.
490
-	 * @return string The content with the embed tags removed.
491
-	 */
492
-	public function embed_remove( $content ) {
493
-		return str_replace( array( '[embed]', '[/embed]' ), array( '<p>', '</p>' ), $content );
494
-	}
495
-
496
-	/**
497
-	 * Prettify the image caption.
498
-	 *
499
-	 * @since 1.5.3
500
-	 *
501
-	 * @param string $caption Caption to be prettified.
502
-	 * @return string Prettified image caption.
503
-	 */
504
-	public function get_img_caption( $caption ) {
505
-		$caption = trim( $caption );
506
-		if ( '' === $caption ) {
507
-			return '';
508
-		}
509
-
510
-		$default = sprintf( '<blockquote>%s</blockquote>',  $caption );
511
-
512
-		/**
513
-		 * Filter the image caption to be displayed after images with captions.
514
-		 *
515
-		 * @since 1.5.3
516
-		 *
517
-		 * @param string $default The whole HTML of the caption.
518
-		 * @param string $caption The caption text.
519
-		 */
520
-		return apply_filters( 'wp2d_image_caption', $default, $caption );
521
-	}
522
-
523
-	/**
524
-	 * Filter the default caption shortcode output.
525
-	 *
526
-	 * @since 1.5.3
527
-	 * @see img_caption_shortcode()
528
-	 *
529
-	 * @param string $empty   The caption output. Default empty.
530
-	 * @param array  $attr    Attributes of the caption shortcode.
531
-	 * @param string $content The image element, possibly wrapped in a hyperlink.
532
-	 * @return string The caption shortcode output.
533
-	 */
534
-	public function custom_img_caption( $empty, $attr, $content ) {
535
-		$content = do_shortcode( $content );
536
-
537
-		// If a caption attribute is defined, we'll add it after the image.
538
-		if ( isset( $attr['caption'] ) && '' !== $attr['caption'] ) {
539
-			$content .= "\n" . $this->get_img_caption( $attr['caption'] );
540
-		}
541
-
542
-		return $content;
543
-	}
544
-
545
-	/**
546
-	 * Create a custom gallery caption output.
547
-	 *
548
-	 * @since 1.5.3
549
-	 *
550
-	 * @param   array $attr Gallery attributes.
551
-	 * @return  string
552
-	 */
553
-	public function custom_gallery_shortcode( $attr ) {
554
-		// Default value in WordPress.
555
-		$captiontag = ( current_theme_supports( 'html5', 'gallery' ) ) ? 'figcaption' : 'dd';
556
-
557
-		// User value.
558
-		if ( isset( $attr['captiontag'] ) ) {
559
-			$captiontag = $attr['captiontag'];
560
-		}
561
-
562
-		// Let WordPress create the regular gallery.
563
-		$gallery = gallery_shortcode( $attr );
564
-
565
-		// Change the content of the captions.
566
-		$gallery = preg_replace_callback(
567
-			'~(<' . $captiontag . '.*>)(.*)(</' . $captiontag . '>)~mUus',
568
-			array( $this, 'custom_gallery_regex_callback' ),
569
-			$gallery
570
-		);
571
-
572
-		return $gallery;
573
-	}
574
-
575
-	/**
576
-	 * Change the result of the regex match from custom_gallery_shortcode.
577
-	 *
578
-	 * @param array $m Regex matches.
579
-	 * @return string Prettified gallery image caption.
580
-	 */
581
-	public function custom_gallery_regex_callback( $m ) {
582
-		return $this->get_img_caption( $m[2] );
583
-	}
584
-
585
-	/*
21
+  /**
22
+   * The original post object.
23
+   *
24
+   * @var WP_Posts
25
+   * @since 1.5.0
26
+   */
27
+  public $post = null;
28
+
29
+  /**
30
+   * The original post ID.
31
+   *
32
+   * @var int
33
+   * @since 1.5.0
34
+   */
35
+  public $ID = null;
36
+
37
+  /**
38
+   * If this post should be shared on diaspora*.
39
+   *
40
+   * @var bool
41
+   * @since 1.5.0
42
+   */
43
+  public $post_to_diaspora = null;
44
+
45
+  /**
46
+   * If a link back to the original post should be added.
47
+   *
48
+   * @var bool
49
+   * @since 1.5.0
50
+   */
51
+  public $fullentrylink = null;
52
+
53
+  /**
54
+   * What content gets posted.
55
+   *
56
+   * @var string
57
+   * @since 1.5.0
58
+   */
59
+  public $display = null;
60
+
61
+  /**
62
+   * The types of tags to post. (global,custom,post)
63
+   *
64
+   * @var array
65
+   * @since 1.5.0
66
+   */
67
+  public $tags_to_post = null;
68
+
69
+  /**
70
+   * The post's custom tags.
71
+   *
72
+   * @var array
73
+   * @since 1.5.0
74
+   */
75
+  public $custom_tags = null;
76
+
77
+  /**
78
+   * Aspects this post gets posted to.
79
+   *
80
+   * @var array
81
+   * @since 1.5.0
82
+   */
83
+  public $aspects = null;
84
+
85
+  /**
86
+   * Services this post gets posted to.
87
+   *
88
+   * @var array
89
+   * @since 1.5.0
90
+   */
91
+  public $services = null;
92
+
93
+
94
+  /**
95
+   * The post's history of diaspora* posts.
96
+   *
97
+   * @var array
98
+   * @since 1.5.0
99
+   */
100
+  public $post_history = null;
101
+
102
+  /**
103
+   * If the post actions have all been set up already.
104
+   *
105
+   * @var boolean
106
+   * @since 1.5.0
107
+   */
108
+  private static $_is_set_up = false;
109
+
110
+  /**
111
+   * Setup all the necessary WP callbacks.
112
+   *
113
+   * @since 1.5.0
114
+   */
115
+  public static function setup() {
116
+    if ( self::$_is_set_up ) {
117
+      return;
118
+    }
119
+
120
+    $instance = new WP2D_Post( null );
121
+
122
+    // Notices when a post has been shared or if it has failed.
123
+    add_action( 'admin_notices', array( $instance, 'admin_notices' ) );
124
+    add_action( 'admin_init', array( $instance, 'ignore_post_error' ) );
125
+
126
+    // Handle diaspora* posting when saving the post.
127
+    add_action( 'save_post', array( $instance, 'post' ), 11, 2 );
128
+    add_action( 'save_post', array( $instance, 'save_meta_box_data' ), 10 );
129
+
130
+    // Add meta boxes.
131
+    add_action( 'add_meta_boxes', array( $instance, 'add_meta_boxes' ) );
132
+
133
+    self::$_is_set_up = true;
134
+  }
135
+
136
+  /**
137
+   * Constructor.
138
+   *
139
+   * @since 1.5.0
140
+   *
141
+   * @param int|WP_Post $post Post ID or the post itself.
142
+   */
143
+  public function __construct( $post ) {
144
+    $this->_assign_wp_post( $post );
145
+  }
146
+
147
+  /**
148
+   * Assign the original WP_Post object and all the custom meta data.
149
+   *
150
+   * @since 1.5.0
151
+   *
152
+   * @param int|WP_Post $post Post ID or the post itself.
153
+   */
154
+  private function _assign_wp_post( $post ) {
155
+    if ( $this->post = get_post( $post ) ) {
156
+      $this->ID = $this->post->ID;
157
+
158
+      $options = WP2D_Options::instance();
159
+
160
+      // Assign all meta values, expanding non-existent ones with the defaults.
161
+      $meta_current = get_post_meta( $this->ID, '_wp_to_diaspora', true );
162
+      $meta = wp_parse_args(
163
+        $meta_current,
164
+        $options->get_options()
165
+      );
166
+      if ( $meta ) {
167
+        foreach ( $meta as $key => $value ) {
168
+          $this->$key = $value;
169
+        }
170
+      }
171
+
172
+      // If no WP2D meta data has been saved yet, this post shouldn't be published.
173
+      // This can happen if existing posts (before WP2D) get updated externally, not through the post edit screen.
174
+      // Check gutobenn/wp-to-diaspora#91 for reference.
175
+      // Also, when we have a post scheduled for publishing, don't touch it.
176
+      // This is important when modifying scheduled posts using Quick Edit.
177
+      if ( ! in_array( $this->post->post_status, array( 'auto-draft', 'future' ) ) && ! $meta_current ) {
178
+        $this->post_to_diaspora = false;
179
+      }
180
+
181
+      $this->post_history = get_post_meta( $this->ID, '_wp_to_diaspora_post_history', true );
182
+    }
183
+  }
184
+
185
+  /**
186
+   * Post to diaspora* when saving a post.
187
+   *
188
+   * @since 1.5.0
189
+   *
190
+   * @todo Maybe somebody wants to share a password protected post to a closed aspect.
191
+   *
192
+   * @param integer $post_id ID of the post being saved.
193
+   * @param WP_Post $post    Post object being saved.
194
+   * @return boolean If the post was posted successfully.
195
+   */
196
+  public function post( $post_id, $post ) {
197
+    $this->_assign_wp_post( $post );
198
+
199
+    $options = WP2D_Options::instance();
200
+
201
+    // Is this post type enabled for posting?
202
+    if ( ! in_array( $post->post_type, $options->get_option( 'enabled_post_types' ) ) ) {
203
+      return false;
204
+    }
205
+
206
+    // Make sure we're posting to diaspora* and the post isn't password protected.
207
+    if ( ! ( $this->post_to_diaspora && 'publish' === $post->post_status && '' === $post->post_password ) ) {
208
+      return false;
209
+    }
210
+
211
+    $status_message = $this->_get_title_link();
212
+
213
+    // Post the full post text or just the excerpt?
214
+    if ( 'full' === $this->display ) {
215
+      $status_message .= $this->_get_full_content();
216
+    } else {
217
+      $status_message .= $this->_get_excerpt_content();
218
+    }
219
+
220
+    // Add the tags assigned to the post.
221
+    $status_message .= $this->_get_tags_to_add();
222
+
223
+    // Add the original entry link to the post?
224
+    $status_message .= $this->_get_posted_at_link();
225
+
226
+    $status_converter = new HtmlConverter( array( 'strip_tags' => true ) );
227
+    $status_message  = $status_converter->convert( $status_message );
228
+
229
+    // Set up the connection to diaspora*.
230
+    $api = WP2D_Helpers::api_quick_connect();
231
+    if ( ! empty( $status_message ) ) {
232
+      if ( $api->has_last_error() ) {
233
+        // Save the post error as post meta data, so we can display it to the user.
234
+        update_post_meta( $post_id, '_wp_to_diaspora_post_error', $api->get_last_error() );
235
+        return false;
236
+      }
237
+
238
+      // Add services to share to via diaspora*.
239
+      $extra_data = array(
240
+        'services' => $this->services,
241
+      );
242
+
243
+      // Try to post to diaspora*.
244
+      if ( $response = $api->post( $status_message, $this->aspects, $extra_data ) ) {
245
+        // Save certain diaspora* post data as meta data for future reference.
246
+        $this->_save_to_history( (object) $response );
247
+
248
+        // If there is still a previous post error around, remove it.
249
+        delete_post_meta( $post_id, '_wp_to_diaspora_post_error' );
250
+
251
+        // Unset post_to_diaspora meta field to prevent mistakenly republishing to diaspora*.
252
+        $meta = get_post_meta( $post_id, '_wp_to_diaspora', true );
253
+        $meta['post_to_diaspora'] = false;
254
+        update_post_meta( $post_id, '_wp_to_diaspora', $meta );
255
+      }
256
+    } else {
257
+      return false;
258
+    }
259
+  }
260
+
261
+  /**
262
+   * Get the title of the post linking to the post itself.
263
+   *
264
+   * @since 1.5.0
265
+   *
266
+   * @return string Post title as a link.
267
+   */
268
+  private function _get_title_link() {
269
+    $title = esc_html( $this->post->post_title );
270
+    $permalink = get_permalink( $this->ID );
271
+    $default = sprintf( '<strong><a href="%2$s" title="%2$s">%1$s</a></strong>', $title, $permalink );
272
+
273
+    /**
274
+     * Filter the title link at the top of the post.
275
+     *
276
+     * @since 1.5.4.1
277
+     *
278
+     * @param string $default   The whole HTML of the title link to be outputted.
279
+     * @param string $title     The title of the original post.
280
+     * @param string $permalink The permalink of the original post.
281
+     */
282
+    $link = apply_filters( 'wp2d_title_filter', $default, $title, $permalink );
283
+
284
+    return '<p>' . $link . '</p>';
285
+  }
286
+
287
+  /**
288
+   * Get the full post content with only default filters applied.
289
+   *
290
+   * @since 1.5.0
291
+   *
292
+   * @return string The full post content.
293
+   */
294
+  private function _get_full_content() {
295
+    // Only allow certain shortcodes.
296
+    global $shortcode_tags;
297
+    $shortcode_tags_bkp = array();
298
+
299
+    foreach ( $shortcode_tags as $shortcode_tag => $shortcode_function ) {
300
+      if ( ! in_array( $shortcode_tag, apply_filters( 'wp2d_shortcodes_filter', array( 'wp_caption', 'caption', 'gallery' ) ) ) ) {
301
+        $shortcode_tags_bkp[ $shortcode_tag ] = $shortcode_function;
302
+        unset( $shortcode_tags[ $shortcode_tag ] );
303
+      }
304
+    }
305
+
306
+    // Disable all filters and then enable only defaults. This prevents additional filters from being posted to diaspora*.
307
+    remove_all_filters( 'the_content' );
308
+    foreach ( apply_filters( 'wp2d_content_filters_filter', array( 'do_shortcode', 'wptexturize', 'convert_smilies', 'convert_chars', 'wpautop', 'shortcode_unautop', 'prepend_attachment', array( $this, 'embed_remove' ) ) ) as $filter ) {
309
+      add_filter( 'the_content', $filter );
310
+    }
311
+
312
+    // Extract URLs from [embed] shortcodes.
313
+    add_filter( 'embed_oembed_html', array( $this, 'embed_url' ), 10, 2 );
314
+
315
+    // Add the pretty caption after the images.
316
+    add_filter( 'img_caption_shortcode', array( $this, 'custom_img_caption' ), 10, 3 );
317
+
318
+    // Overwrite the native shortcode handler to add pretty captions.
319
+    // http://wordpress.stackexchange.com/a/74675/54456 for explanation.
320
+    add_shortcode( 'gallery', array( $this, 'custom_gallery_shortcode' ) );
321
+
322
+    $full_content = apply_filters( 'the_content', $this->post->post_content );
323
+
324
+    // Put the removed shortcode tags back again.
325
+    $shortcode_tags += $shortcode_tags_bkp;
326
+
327
+    return $full_content;
328
+  }
329
+
330
+  /**
331
+   * Get the post's excerpt in a nice format.
332
+   *
333
+   * @since 1.5.0
334
+   *
335
+   * @return string Post's excerpt.
336
+   */
337
+  private function _get_excerpt_content() {
338
+    // Look for the excerpt in the following order:
339
+    // 1. Custom post excerpt.
340
+    // 2. Text up to the <!--more--> tag.
341
+    // 3. Manually trimmed content.
342
+    $content = $this->post->post_content;
343
+    $excerpt = $this->post->post_excerpt;
344
+    if ( '' === $excerpt ) {
345
+      if ( $more_pos = strpos( $content, '<!--more' ) ) {
346
+        $excerpt = substr( $content, 0, $more_pos );
347
+      } else {
348
+        $excerpt = wp_trim_words( $content, 42, '[...]' );
349
+      }
350
+    }
351
+    return '<p>' . $excerpt . '</p>';
352
+  }
353
+
354
+  /**
355
+   * Get a string of tags that have been added to the post.
356
+   *
357
+   * @since 1.5.0
358
+   *
359
+   * @return string Tags added to the post.
360
+   */
361
+  private function _get_tags_to_add() {
362
+    $options = WP2D_Options::instance();
363
+    $tags_to_post = $this->tags_to_post;
364
+    $tags_to_add  = '';
365
+
366
+    // Add any diaspora* tags?
367
+    if ( ! empty( $tags_to_post ) ) {
368
+      // The diaspora* tags to add to the post.
369
+      $diaspora_tags = array();
370
+
371
+      // Add global tags?
372
+      $global_tags = $options->get_option( 'global_tags' );
373
+      if ( in_array( 'global', $tags_to_post ) && is_array( $global_tags ) ) {
374
+        $diaspora_tags += array_flip( $global_tags );
375
+      }
376
+
377
+      // Add custom tags?
378
+      if ( in_array( 'custom', $tags_to_post ) && is_array( $this->custom_tags ) ) {
379
+        $diaspora_tags += array_flip( $this->custom_tags );
380
+      }
381
+
382
+      // Add post tags?
383
+      $post_tags = wp_get_post_tags( $this->ID, array( 'fields' => 'slugs' ) );
384
+      if ( in_array( 'post', $tags_to_post ) && is_array( $post_tags ) ) {
385
+        $diaspora_tags += array_flip( $post_tags );
386
+      }
387
+
388
+      // Get an array of cleaned up tags.
389
+      // NOTE: Validate method needs a variable, as it's passed by reference!
390
+      $diaspora_tags = array_keys( $diaspora_tags );
391
+      $options->validate_tags( $diaspora_tags );
392
+
393
+      // Get all the tags and list them all nicely in a row.
394
+      $diaspora_tags_clean = array();
395
+      foreach ( $diaspora_tags as $tag ) {
396
+        $diaspora_tags_clean[] = '#' . $tag;
397
+      }
398
+
399
+      // Add all the found tags.
400
+      if ( ! empty( $diaspora_tags_clean ) ) {
401
+        $tags_to_add = implode( ' ', $diaspora_tags_clean ) . '<br />';
402
+      }
403
+    }
404
+
405
+    return $tags_to_add;
406
+  }
407
+
408
+  /**
409
+   * Get the link to the original post.
410
+   *
411
+   * @since 1.5.0
412
+   *
413
+   * @return string Original post link.
414
+   */
415
+  private function _get_posted_at_link() {
416
+    $link = '';
417
+    if ( $this->fullentrylink ) {
418
+
419
+      $text = esc_html( 'Originally posted at:', 'wp-to-diaspora' );
420
+      $permalink = get_permalink( $this->ID );
421
+      $title = esc_html( 'Permalink', 'wp-to-diaspora' );
422
+      $default = sprintf( '%1$s <a href="%2$s" title="%3$s">%2$s</a>', $text, $permalink, $title );
423
+
424
+      /**
425
+       * Filter the "Originally posted at" link at the bottom of the post.
426
+       *
427
+       * @since 1.5.4.1
428
+       *
429
+       * @param string $default   The whole HTML of the text and link to be outputted.
430
+       * @param string $text      The "Originally posted at:" text before the link.
431
+       * @param string $permalink The permalink of the original post.
432
+       * @param string $title     The "Permalink" title of the link.
433
+       */
434
+      $link = apply_filters( 'wp2d_posted_at_link_filter', $default, $text, $permalink, $title );
435
+
436
+      $link = '<p>' . $link . '</p>';
437
+    }
438
+
439
+    return $link;
440
+  }
441
+
442
+  /**
443
+   * Save the details of the new diaspora* post to this post's history.
444
+   *
445
+   * @since 1.5.0
446
+   *
447
+   * @param object $response Response from the API containing the diaspora* post details.
448
+   */
449
+  private function _save_to_history( $response ) {
450
+    // Make sure the post history is an array.
451
+    if ( empty( $this->post_history ) ) {
452
+      $this->post_history = array();
453
+    }
454
+
455
+    // Add a new entry to the history.
456
+    $this->post_history[] = array(
457
+      'id'         => $response->id,
458
+      'guid'       => $response->guid,
459
+      'created_at' => $this->post->post_modified,
460
+      'aspects'    => $this->aspects,
461
+      'nsfw'       => $response->nsfw,
462
+      'post_url'   => $response->permalink,
463
+    );
464
+
465
+    update_post_meta( $this->ID, '_wp_to_diaspora_post_history', $this->post_history );
466
+  }
467
+
468
+  /**
469
+   * Return URL from [embed] shortcode instead of generated iframe.
470
+   *
471
+   * @since 1.5.0
472
+   * @see WP_Embed::shortcode()
473
+   *
474
+   * @param mixed  $html The cached HTML result, stored in post meta.
475
+   * @param string $url  The attempted embed URL.
476
+   * @return string URL of the embed.
477
+   */
478
+  public function embed_url( $html, $url ) {
479
+    return $url;
480
+  }
481
+
482
+  /**
483
+   * Removes '[embed]' and '[/embed]' left by embed_url.
484
+   *
485
+   * @since 1.5.0
486
+   * @todo It would be great to fix it using only one filter.
487
+   *       It's happening because embed filter is being removed by remove_all_filters('the_content') on WP2D_Post::post().
488
+   *
489
+   * @param string $content Content of the post.
490
+   * @return string The content with the embed tags removed.
491
+   */
492
+  public function embed_remove( $content ) {
493
+    return str_replace( array( '[embed]', '[/embed]' ), array( '<p>', '</p>' ), $content );
494
+  }
495
+
496
+  /**
497
+   * Prettify the image caption.
498
+   *
499
+   * @since 1.5.3
500
+   *
501
+   * @param string $caption Caption to be prettified.
502
+   * @return string Prettified image caption.
503
+   */
504
+  public function get_img_caption( $caption ) {
505
+    $caption = trim( $caption );
506
+    if ( '' === $caption ) {
507
+      return '';
508
+    }
509
+
510
+    $default = sprintf( '<blockquote>%s</blockquote>',  $caption );
511
+
512
+    /**
513
+     * Filter the image caption to be displayed after images with captions.
514
+     *
515
+     * @since 1.5.3
516
+     *
517
+     * @param string $default The whole HTML of the caption.
518
+     * @param string $caption The caption text.
519
+     */
520
+    return apply_filters( 'wp2d_image_caption', $default, $caption );
521
+  }
522
+
523
+  /**
524
+   * Filter the default caption shortcode output.
525
+   *
526
+   * @since 1.5.3
527
+   * @see img_caption_shortcode()
528
+   *
529
+   * @param string $empty   The caption output. Default empty.
530
+   * @param array  $attr    Attributes of the caption shortcode.
531
+   * @param string $content The image element, possibly wrapped in a hyperlink.
532
+   * @return string The caption shortcode output.
533
+   */
534
+  public function custom_img_caption( $empty, $attr, $content ) {
535
+    $content = do_shortcode( $content );
536
+
537
+    // If a caption attribute is defined, we'll add it after the image.
538
+    if ( isset( $attr['caption'] ) && '' !== $attr['caption'] ) {
539
+      $content .= "\n" . $this->get_img_caption( $attr['caption'] );
540
+    }
541
+
542
+    return $content;
543
+  }
544
+
545
+  /**
546
+   * Create a custom gallery caption output.
547
+   *
548
+   * @since 1.5.3
549
+   *
550
+   * @param   array $attr Gallery attributes.
551
+   * @return  string
552
+   */
553
+  public function custom_gallery_shortcode( $attr ) {
554
+    // Default value in WordPress.
555
+    $captiontag = ( current_theme_supports( 'html5', 'gallery' ) ) ? 'figcaption' : 'dd';
556
+
557
+    // User value.
558
+    if ( isset( $attr['captiontag'] ) ) {
559
+      $captiontag = $attr['captiontag'];
560
+    }
561
+
562
+    // Let WordPress create the regular gallery.
563
+    $gallery = gallery_shortcode( $attr );
564
+
565
+    // Change the content of the captions.
566
+    $gallery = preg_replace_callback(
567
+      '~(<' . $captiontag . '.*>)(.*)(</' . $captiontag . '>)~mUus',
568
+      array( $this, 'custom_gallery_regex_callback' ),
569
+      $gallery
570
+    );
571
+
572
+    return $gallery;
573
+  }
574
+
575
+  /**
576
+   * Change the result of the regex match from custom_gallery_shortcode.
577
+   *
578
+   * @param array $m Regex matches.
579
+   * @return string Prettified gallery image caption.
580
+   */
581
+  public function custom_gallery_regex_callback( $m ) {
582
+    return $this->get_img_caption( $m[2] );
583
+  }
584
+
585
+  /*
586 586
 	 * META BOX
587 587
 	 */
588 588
 
589
-	/**
590
-	 * Adds a meta box to the main column on the enabled Post Types' edit screens.
591
-	 *
592
-	 * @since 1.5.0
593
-	 */
594
-	public function add_meta_boxes() {
595
-		$options = WP2D_Options::instance();
596
-		foreach ( $options->get_option( 'enabled_post_types' ) as $post_type ) {
597
-			add_meta_box(
598
-				'wp_to_diaspora_meta_box',
599
-				'WP to diaspora*',
600
-				array( $this, 'meta_box_render' ),
601
-				$post_type,
602
-				'side',
603
-				'high'
604
-			);
605
-		}
606
-	}
607
-
608
-	/**
609
-	 * Prints the meta box content.
610
-	 *
611
-	 * @since 1.5.0
612
-	 *
613
-	 * @param WP_Post $post The object for the current post.
614
-	 */
615
-	public function meta_box_render( $post ) {
616
-		$this->_assign_wp_post( $post );
617
-
618
-		// Add an nonce field so we can check for it later.
619
-		wp_nonce_field( 'wp_to_diaspora_meta_box', 'wp_to_diaspora_meta_box_nonce' );
620
-
621
-		// Get the default values to use, but give priority to the meta data already set.
622
-		$options = WP2D_Options::instance();
623
-
624
-		// Make sure we have some value for post meta fields.
625
-		$this->custom_tags = $this->custom_tags ?: array();
626
-
627
-		// If this post is already published, don't post again to diaspora* by default.
628
-		$this->post_to_diaspora = ( $this->post_to_diaspora && 'publish' !== get_post_status( $this->ID ) );
629
-		$this->aspects          = $this->aspects  ?: array();
630
-		$this->services         = $this->services ?: array();
631
-
632
-		// Have we already posted on diaspora*?
633
-		if ( is_array( $this->post_history ) ) {
634
-			$latest_post = end( $this->post_history );
635
-			?>
589
+  /**
590
+   * Adds a meta box to the main column on the enabled Post Types' edit screens.
591
+   *
592
+   * @since 1.5.0
593
+   */
594
+  public function add_meta_boxes() {
595
+    $options = WP2D_Options::instance();
596
+    foreach ( $options->get_option( 'enabled_post_types' ) as $post_type ) {
597
+      add_meta_box(
598
+        'wp_to_diaspora_meta_box',
599
+        'WP to diaspora*',
600
+        array( $this, 'meta_box_render' ),
601
+        $post_type,
602
+        'side',
603
+        'high'
604
+      );
605
+    }
606
+  }
607
+
608
+  /**
609
+   * Prints the meta box content.
610
+   *
611
+   * @since 1.5.0
612
+   *
613
+   * @param WP_Post $post The object for the current post.
614
+   */
615
+  public function meta_box_render( $post ) {
616
+    $this->_assign_wp_post( $post );
617
+
618
+    // Add an nonce field so we can check for it later.
619
+    wp_nonce_field( 'wp_to_diaspora_meta_box', 'wp_to_diaspora_meta_box_nonce' );
620
+
621
+    // Get the default values to use, but give priority to the meta data already set.
622
+    $options = WP2D_Options::instance();
623
+
624
+    // Make sure we have some value for post meta fields.
625
+    $this->custom_tags = $this->custom_tags ?: array();
626
+
627
+    // If this post is already published, don't post again to diaspora* by default.
628
+    $this->post_to_diaspora = ( $this->post_to_diaspora && 'publish' !== get_post_status( $this->ID ) );
629
+    $this->aspects          = $this->aspects  ?: array();
630
+    $this->services         = $this->services ?: array();
631
+
632
+    // Have we already posted on diaspora*?
633
+    if ( is_array( $this->post_history ) ) {
634
+      $latest_post = end( $this->post_history );
635
+      ?>
636 636
 			<p><a href="<?php echo esc_attr( $latest_post['post_url'] ); ?>" target="_blank"><?php esc_html_e( 'Already posted to diaspora*.', 'wp-to-diaspora' ); ?></a></p>
637 637
 			<?php
638
-		}
639
-		?>
638
+    }
639
+    ?>
640 640
 
641 641
 		<p><?php $options->post_to_diaspora_render( $this->post_to_diaspora ); ?></p>
642 642
 		<p><?php $options->fullentrylink_render( $this->fullentrylink ); ?></p>
@@ -647,133 +647,133 @@  discard block
 block discarded – undo
647 647
 		<p><?php $options->aspects_services_render( array( 'services', $this->services ) ); ?></p>
648 648
 
649 649
 		<?php
650
-	}
651
-
652
-	/**
653
-	 * When the post is saved, save our meta data.
654
-	 *
655
-	 * @since 1.5.0
656
-	 *
657
-	 * @param integer $post_id The ID of the post being saved.
658
-	 */
659
-	public function save_meta_box_data( $post_id ) {
660
-		/*
650
+  }
651
+
652
+  /**
653
+   * When the post is saved, save our meta data.
654
+   *
655
+   * @since 1.5.0
656
+   *
657
+   * @param integer $post_id The ID of the post being saved.
658
+   */
659
+  public function save_meta_box_data( $post_id ) {
660
+    /*
661 661
 		 * We need to verify this came from our screen and with proper authorization,
662 662
 		 * because the save_post action can be triggered at other times.
663 663
 		 */
664
-		if ( ! $this->_is_safe_to_save() ) {
665
-			return;
666
-		}
667
-
668
-		/* OK, it's safe for us to save the data now. */
669
-
670
-		// Meta data to save.
671
-		$meta_to_save = $_POST['wp_to_diaspora_settings'];
672
-		$options = WP2D_Options::instance();
673
-
674
-		// Checkboxes.
675
-		$options->validate_checkboxes( array( 'post_to_diaspora', 'fullentrylink' ), $meta_to_save );
676
-
677
-		// Single Selects.
678
-		$options->validate_single_selects( 'display', $meta_to_save );
679
-
680
-		// Multiple Selects.
681
-		$options->validate_multi_selects( 'tags_to_post', $meta_to_save );
682
-
683
-		// Save custom tags as array.
684
-		$options->validate_tags( $meta_to_save['custom_tags'] );
685
-
686
-		// Clean up the list of aspects. If the list is empty, only use the 'Public' aspect.
687
-		$options->validate_aspects_services( $meta_to_save['aspects'], array( 'public' ) );
688
-
689
-		// Clean up the list of services.
690
-		$options->validate_aspects_services( $meta_to_save['services'] );
691
-
692
-		// Update the meta data for this post.
693
-		update_post_meta( $post_id, '_wp_to_diaspora', $meta_to_save );
694
-	}
695
-
696
-	/**
697
-	 * Perform all checks to see if we are allowed to save the meta data.
698
-	 *
699
-	 * @since 1.5.0
700
-	 *
701
-	 * @return boolean If the verification checks have passed.
702
-	 */
703
-	private function _is_safe_to_save() {
704
-		// Verify that our nonce is set and  valid.
705
-		if ( ! ( isset( $_POST['wp_to_diaspora_meta_box_nonce'] ) && wp_verify_nonce( $_POST['wp_to_diaspora_meta_box_nonce'], 'wp_to_diaspora_meta_box' ) ) ) {
706
-			return false;
707
-		}
708
-
709
-		// If this is an autosave, our form has not been submitted, so we don't want to do anything.
710
-		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
711
-			return false;
712
-		}
713
-
714
-		// Check the user's permissions.
715
-		$permission = ( isset( $_POST['post_type'] ) && 'page' === $_POST['post_type'] ) ? 'edit_pages' : 'edit_posts';
716
-		if ( ! current_user_can( $permission, $this->ID ) ) {
717
-			return false;
718
-		}
719
-
720
-		// Make real sure that we have some meta data to save.
721
-		if ( ! isset( $_POST['wp_to_diaspora_settings'] ) ) {
722
-			return false;
723
-		}
724
-
725
-		return true;
726
-	}
727
-
728
-	/**
729
-	 * Add admin notices when a post gets displayed.
730
-	 *
731
-	 * @since 1.5.0
732
-	 *
733
-	 * @todo Ignore post error with AJAX.
734
-	 */
735
-	public function admin_notices() {
736
-		global $post, $pagenow;
737
-		if ( ! $post || 'post.php' !== $pagenow ) {
738
-			return;
739
-		}
740
-
741
-		if ( ( $error = get_post_meta( $post->ID, '_wp_to_diaspora_post_error', true ) ) && is_wp_error( $error ) ) {
742
-			// Are we adding a help tab link to this notice?
743
-			$help_link = WP2D_Contextual_Help::get_help_tab_quick_link( $error );
744
-
745
-			// This notice will only be shown if posting to diaspora* has failed.
746
-			printf( '<div class="error notice is-dismissible"><p>%1$s %2$s %3$s <a href="%4$s">%5$s</a></p></div>',
747
-				esc_html__( 'Failed to post to diaspora*.', 'wp-to-diaspora' ),
748
-				esc_html__( $error->get_error_message() ),
749
-				$help_link,
750
-				esc_url( add_query_arg( 'wp2d_ignore_post_error', '' ) ),
751
-				esc_html__( 'Ignore', 'wp-to-diaspora' )
752
-			);
753
-		} elseif ( ( $diaspora_post_history = get_post_meta( $post->ID, '_wp_to_diaspora_post_history', true ) ) && is_array( $diaspora_post_history ) ) {
754
-			// Get the latest post from the history.
755
-			$latest_post = end( $diaspora_post_history );
756
-
757
-			// Only show if this post is showing a message and the post is a fresh share.
758
-			if ( isset( $_GET['message'] ) && $post->post_modified === $latest_post['created_at'] ) {
759
-				printf( '<div class="updated notice is-dismissible"><p>%1$s <a href="%2$s" target="_blank">%3$s</a></p></div>',
760
-					esc_html__( 'Successfully posted to diaspora*.', 'wp-to-diaspora' ),
761
-					esc_url( $latest_post['post_url'] ),
762
-					esc_html__( 'View Post' )
763
-				);
764
-			}
765
-		}
766
-	}
767
-
768
-	/**
769
-	 * Delete the error post meta data if it gets ignored.
770
-	 *
771
-	 * @since 1.5.0
772
-	 */
773
-	public function ignore_post_error() {
774
-		// If "Ignore" link has been clicked, delete the post error meta data.
775
-		if ( isset( $_GET['wp2d_ignore_post_error'], $_GET['post'] ) ) {
776
-			delete_post_meta( $_GET['post'], '_wp_to_diaspora_post_error' );
777
-		}
778
-	}
664
+    if ( ! $this->_is_safe_to_save() ) {
665
+      return;
666
+    }
667
+
668
+    /* OK, it's safe for us to save the data now. */
669
+
670
+    // Meta data to save.
671
+    $meta_to_save = $_POST['wp_to_diaspora_settings'];
672
+    $options = WP2D_Options::instance();
673
+
674
+    // Checkboxes.
675
+    $options->validate_checkboxes( array( 'post_to_diaspora', 'fullentrylink' ), $meta_to_save );
676
+
677
+    // Single Selects.
678
+    $options->validate_single_selects( 'display', $meta_to_save );
679
+
680
+    // Multiple Selects.
681
+    $options->validate_multi_selects( 'tags_to_post', $meta_to_save );
682
+
683
+    // Save custom tags as array.
684
+    $options->validate_tags( $meta_to_save['custom_tags'] );
685
+
686
+    // Clean up the list of aspects. If the list is empty, only use the 'Public' aspect.
687
+    $options->validate_aspects_services( $meta_to_save['aspects'], array( 'public' ) );
688
+
689
+    // Clean up the list of services.
690
+    $options->validate_aspects_services( $meta_to_save['services'] );
691
+
692
+    // Update the meta data for this post.
693
+    update_post_meta( $post_id, '_wp_to_diaspora', $meta_to_save );
694
+  }
695
+
696
+  /**
697
+   * Perform all checks to see if we are allowed to save the meta data.
698
+   *
699
+   * @since 1.5.0
700
+   *
701
+   * @return boolean If the verification checks have passed.
702
+   */
703
+  private function _is_safe_to_save() {
704
+    // Verify that our nonce is set and  valid.
705
+    if ( ! ( isset( $_POST['wp_to_diaspora_meta_box_nonce'] ) && wp_verify_nonce( $_POST['wp_to_diaspora_meta_box_nonce'], 'wp_to_diaspora_meta_box' ) ) ) {
706
+      return false;
707
+    }
708
+
709
+    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
710
+    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
711
+      return false;
712
+    }
713
+
714
+    // Check the user's permissions.
715
+    $permission = ( isset( $_POST['post_type'] ) && 'page' === $_POST['post_type'] ) ? 'edit_pages' : 'edit_posts';
716
+    if ( ! current_user_can( $permission, $this->ID ) ) {
717
+      return false;
718
+    }
719
+
720
+    // Make real sure that we have some meta data to save.
721
+    if ( ! isset( $_POST['wp_to_diaspora_settings'] ) ) {
722
+      return false;
723
+    }
724
+
725
+    return true;
726
+  }
727
+
728
+  /**
729
+   * Add admin notices when a post gets displayed.
730
+   *
731
+   * @since 1.5.0
732
+   *
733
+   * @todo Ignore post error with AJAX.
734
+   */
735
+  public function admin_notices() {
736
+    global $post, $pagenow;
737
+    if ( ! $post || 'post.php' !== $pagenow ) {
738
+      return;
739
+    }
740
+
741
+    if ( ( $error = get_post_meta( $post->ID, '_wp_to_diaspora_post_error', true ) ) && is_wp_error( $error ) ) {
742
+      // Are we adding a help tab link to this notice?
743
+      $help_link = WP2D_Contextual_Help::get_help_tab_quick_link( $error );
744
+
745
+      // This notice will only be shown if posting to diaspora* has failed.
746
+      printf( '<div class="error notice is-dismissible"><p>%1$s %2$s %3$s <a href="%4$s">%5$s</a></p></div>',
747
+        esc_html__( 'Failed to post to diaspora*.', 'wp-to-diaspora' ),
748
+        esc_html__( $error->get_error_message() ),
749
+        $help_link,
750
+        esc_url( add_query_arg( 'wp2d_ignore_post_error', '' ) ),
751
+        esc_html__( 'Ignore', 'wp-to-diaspora' )
752
+      );
753
+    } elseif ( ( $diaspora_post_history = get_post_meta( $post->ID, '_wp_to_diaspora_post_history', true ) ) && is_array( $diaspora_post_history ) ) {
754
+      // Get the latest post from the history.
755
+      $latest_post = end( $diaspora_post_history );
756
+
757
+      // Only show if this post is showing a message and the post is a fresh share.
758
+      if ( isset( $_GET['message'] ) && $post->post_modified === $latest_post['created_at'] ) {
759
+        printf( '<div class="updated notice is-dismissible"><p>%1$s <a href="%2$s" target="_blank">%3$s</a></p></div>',
760
+          esc_html__( 'Successfully posted to diaspora*.', 'wp-to-diaspora' ),
761
+          esc_url( $latest_post['post_url'] ),
762
+          esc_html__( 'View Post' )
763
+        );
764
+      }
765
+    }
766
+  }
767
+
768
+  /**
769
+   * Delete the error post meta data if it gets ignored.
770
+   *
771
+   * @since 1.5.0
772
+   */
773
+  public function ignore_post_error() {
774
+    // If "Ignore" link has been clicked, delete the post error meta data.
775
+    if ( isset( $_GET['wp2d_ignore_post_error'], $_GET['post'] ) ) {
776
+      delete_post_meta( $_GET['post'], '_wp_to_diaspora_post_error' );
777
+    }
778
+  }
779 779
 }
Please login to merge, or discard this patch.
Spacing   +146 added lines, -146 removed lines patch added patch discarded remove patch
@@ -7,7 +7,7 @@  discard block
 block discarded – undo
7 7
  */
8 8
 
9 9
 // Exit if accessed directly.
10
-defined( 'ABSPATH' ) || exit;
10
+defined('ABSPATH') || exit;
11 11
 
12 12
 use League\HTMLToMarkdown\HtmlConverter;
13 13
 
@@ -113,22 +113,22 @@  discard block
 block discarded – undo
113 113
 	 * @since 1.5.0
114 114
 	 */
115 115
 	public static function setup() {
116
-		if ( self::$_is_set_up ) {
116
+		if (self::$_is_set_up) {
117 117
 			return;
118 118
 		}
119 119
 
120
-		$instance = new WP2D_Post( null );
120
+		$instance = new WP2D_Post(null);
121 121
 
122 122
 		// Notices when a post has been shared or if it has failed.
123
-		add_action( 'admin_notices', array( $instance, 'admin_notices' ) );
124
-		add_action( 'admin_init', array( $instance, 'ignore_post_error' ) );
123
+		add_action('admin_notices', array($instance, 'admin_notices'));
124
+		add_action('admin_init', array($instance, 'ignore_post_error'));
125 125
 
126 126
 		// Handle diaspora* posting when saving the post.
127
-		add_action( 'save_post', array( $instance, 'post' ), 11, 2 );
128
-		add_action( 'save_post', array( $instance, 'save_meta_box_data' ), 10 );
127
+		add_action('save_post', array($instance, 'post'), 11, 2);
128
+		add_action('save_post', array($instance, 'save_meta_box_data'), 10);
129 129
 
130 130
 		// Add meta boxes.
131
-		add_action( 'add_meta_boxes', array( $instance, 'add_meta_boxes' ) );
131
+		add_action('add_meta_boxes', array($instance, 'add_meta_boxes'));
132 132
 
133 133
 		self::$_is_set_up = true;
134 134
 	}
@@ -140,8 +140,8 @@  discard block
 block discarded – undo
140 140
 	 *
141 141
 	 * @param int|WP_Post $post Post ID or the post itself.
142 142
 	 */
143
-	public function __construct( $post ) {
144
-		$this->_assign_wp_post( $post );
143
+	public function __construct($post) {
144
+		$this->_assign_wp_post($post);
145 145
 	}
146 146
 
147 147
 	/**
@@ -151,20 +151,20 @@  discard block
 block discarded – undo
151 151
 	 *
152 152
 	 * @param int|WP_Post $post Post ID or the post itself.
153 153
 	 */
154
-	private function _assign_wp_post( $post ) {
155
-		if ( $this->post = get_post( $post ) ) {
154
+	private function _assign_wp_post($post) {
155
+		if ($this->post = get_post($post)) {
156 156
 			$this->ID = $this->post->ID;
157 157
 
158 158
 			$options = WP2D_Options::instance();
159 159
 
160 160
 			// Assign all meta values, expanding non-existent ones with the defaults.
161
-			$meta_current = get_post_meta( $this->ID, '_wp_to_diaspora', true );
161
+			$meta_current = get_post_meta($this->ID, '_wp_to_diaspora', true);
162 162
 			$meta = wp_parse_args(
163 163
 				$meta_current,
164 164
 				$options->get_options()
165 165
 			);
166
-			if ( $meta ) {
167
-				foreach ( $meta as $key => $value ) {
166
+			if ($meta) {
167
+				foreach ($meta as $key => $value) {
168 168
 					$this->$key = $value;
169 169
 				}
170 170
 			}
@@ -174,11 +174,11 @@  discard block
 block discarded – undo
174 174
 			// Check gutobenn/wp-to-diaspora#91 for reference.
175 175
 			// Also, when we have a post scheduled for publishing, don't touch it.
176 176
 			// This is important when modifying scheduled posts using Quick Edit.
177
-			if ( ! in_array( $this->post->post_status, array( 'auto-draft', 'future' ) ) && ! $meta_current ) {
177
+			if ( ! in_array($this->post->post_status, array('auto-draft', 'future')) && ! $meta_current) {
178 178
 				$this->post_to_diaspora = false;
179 179
 			}
180 180
 
181
-			$this->post_history = get_post_meta( $this->ID, '_wp_to_diaspora_post_history', true );
181
+			$this->post_history = get_post_meta($this->ID, '_wp_to_diaspora_post_history', true);
182 182
 		}
183 183
 	}
184 184
 
@@ -193,25 +193,25 @@  discard block
 block discarded – undo
193 193
 	 * @param WP_Post $post    Post object being saved.
194 194
 	 * @return boolean If the post was posted successfully.
195 195
 	 */
196
-	public function post( $post_id, $post ) {
197
-		$this->_assign_wp_post( $post );
196
+	public function post($post_id, $post) {
197
+		$this->_assign_wp_post($post);
198 198
 
199 199
 		$options = WP2D_Options::instance();
200 200
 
201 201
 		// Is this post type enabled for posting?
202
-		if ( ! in_array( $post->post_type, $options->get_option( 'enabled_post_types' ) ) ) {
202
+		if ( ! in_array($post->post_type, $options->get_option('enabled_post_types'))) {
203 203
 			return false;
204 204
 		}
205 205
 
206 206
 		// Make sure we're posting to diaspora* and the post isn't password protected.
207
-		if ( ! ( $this->post_to_diaspora && 'publish' === $post->post_status && '' === $post->post_password ) ) {
207
+		if ( ! ($this->post_to_diaspora && 'publish' === $post->post_status && '' === $post->post_password)) {
208 208
 			return false;
209 209
 		}
210 210
 
211 211
 		$status_message = $this->_get_title_link();
212 212
 
213 213
 		// Post the full post text or just the excerpt?
214
-		if ( 'full' === $this->display ) {
214
+		if ('full' === $this->display) {
215 215
 			$status_message .= $this->_get_full_content();
216 216
 		} else {
217 217
 			$status_message .= $this->_get_excerpt_content();
@@ -223,15 +223,15 @@  discard block
 block discarded – undo
223 223
 		// Add the original entry link to the post?
224 224
 		$status_message .= $this->_get_posted_at_link();
225 225
 
226
-		$status_converter = new HtmlConverter( array( 'strip_tags' => true ) );
227
-		$status_message  = $status_converter->convert( $status_message );
226
+		$status_converter = new HtmlConverter(array('strip_tags' => true));
227
+		$status_message  = $status_converter->convert($status_message);
228 228
 
229 229
 		// Set up the connection to diaspora*.
230 230
 		$api = WP2D_Helpers::api_quick_connect();
231
-		if ( ! empty( $status_message ) ) {
232
-			if ( $api->has_last_error() ) {
231
+		if ( ! empty($status_message)) {
232
+			if ($api->has_last_error()) {
233 233
 				// Save the post error as post meta data, so we can display it to the user.
234
-				update_post_meta( $post_id, '_wp_to_diaspora_post_error', $api->get_last_error() );
234
+				update_post_meta($post_id, '_wp_to_diaspora_post_error', $api->get_last_error());
235 235
 				return false;
236 236
 			}
237 237
 
@@ -241,17 +241,17 @@  discard block
 block discarded – undo
241 241
 			);
242 242
 
243 243
 			// Try to post to diaspora*.
244
-			if ( $response = $api->post( $status_message, $this->aspects, $extra_data ) ) {
244
+			if ($response = $api->post($status_message, $this->aspects, $extra_data)) {
245 245
 				// Save certain diaspora* post data as meta data for future reference.
246
-				$this->_save_to_history( (object) $response );
246
+				$this->_save_to_history((object) $response);
247 247
 
248 248
 				// If there is still a previous post error around, remove it.
249
-				delete_post_meta( $post_id, '_wp_to_diaspora_post_error' );
249
+				delete_post_meta($post_id, '_wp_to_diaspora_post_error');
250 250
 
251 251
 				// Unset post_to_diaspora meta field to prevent mistakenly republishing to diaspora*.
252
-				$meta = get_post_meta( $post_id, '_wp_to_diaspora', true );
252
+				$meta = get_post_meta($post_id, '_wp_to_diaspora', true);
253 253
 				$meta['post_to_diaspora'] = false;
254
-				update_post_meta( $post_id, '_wp_to_diaspora', $meta );
254
+				update_post_meta($post_id, '_wp_to_diaspora', $meta);
255 255
 			}
256 256
 		} else {
257 257
 			return false;
@@ -266,9 +266,9 @@  discard block
 block discarded – undo
266 266
 	 * @return string Post title as a link.
267 267
 	 */
268 268
 	private function _get_title_link() {
269
-		$title = esc_html( $this->post->post_title );
270
-		$permalink = get_permalink( $this->ID );
271
-		$default = sprintf( '<strong><a href="%2$s" title="%2$s">%1$s</a></strong>', $title, $permalink );
269
+		$title = esc_html($this->post->post_title);
270
+		$permalink = get_permalink($this->ID);
271
+		$default = sprintf('<strong><a href="%2$s" title="%2$s">%1$s</a></strong>', $title, $permalink);
272 272
 
273 273
 		/**
274 274
 		 * Filter the title link at the top of the post.
@@ -279,9 +279,9 @@  discard block
 block discarded – undo
279 279
 		 * @param string $title     The title of the original post.
280 280
 		 * @param string $permalink The permalink of the original post.
281 281
 		 */
282
-		$link = apply_filters( 'wp2d_title_filter', $default, $title, $permalink );
282
+		$link = apply_filters('wp2d_title_filter', $default, $title, $permalink);
283 283
 
284
-		return '<p>' . $link . '</p>';
284
+		return '<p>'.$link.'</p>';
285 285
 	}
286 286
 
287 287
 	/**
@@ -296,30 +296,30 @@  discard block
 block discarded – undo
296 296
 		global $shortcode_tags;
297 297
 		$shortcode_tags_bkp = array();
298 298
 
299
-		foreach ( $shortcode_tags as $shortcode_tag => $shortcode_function ) {
300
-			if ( ! in_array( $shortcode_tag, apply_filters( 'wp2d_shortcodes_filter', array( 'wp_caption', 'caption', 'gallery' ) ) ) ) {
301
-				$shortcode_tags_bkp[ $shortcode_tag ] = $shortcode_function;
302
-				unset( $shortcode_tags[ $shortcode_tag ] );
299
+		foreach ($shortcode_tags as $shortcode_tag => $shortcode_function) {
300
+			if ( ! in_array($shortcode_tag, apply_filters('wp2d_shortcodes_filter', array('wp_caption', 'caption', 'gallery')))) {
301
+				$shortcode_tags_bkp[$shortcode_tag] = $shortcode_function;
302
+				unset($shortcode_tags[$shortcode_tag]);
303 303
 			}
304 304
 		}
305 305
 
306 306
 		// Disable all filters and then enable only defaults. This prevents additional filters from being posted to diaspora*.
307
-		remove_all_filters( 'the_content' );
308
-		foreach ( apply_filters( 'wp2d_content_filters_filter', array( 'do_shortcode', 'wptexturize', 'convert_smilies', 'convert_chars', 'wpautop', 'shortcode_unautop', 'prepend_attachment', array( $this, 'embed_remove' ) ) ) as $filter ) {
309
-			add_filter( 'the_content', $filter );
307
+		remove_all_filters('the_content');
308
+		foreach (apply_filters('wp2d_content_filters_filter', array('do_shortcode', 'wptexturize', 'convert_smilies', 'convert_chars', 'wpautop', 'shortcode_unautop', 'prepend_attachment', array($this, 'embed_remove'))) as $filter) {
309
+			add_filter('the_content', $filter);
310 310
 		}
311 311
 
312 312
 		// Extract URLs from [embed] shortcodes.
313
-		add_filter( 'embed_oembed_html', array( $this, 'embed_url' ), 10, 2 );
313
+		add_filter('embed_oembed_html', array($this, 'embed_url'), 10, 2);
314 314
 
315 315
 		// Add the pretty caption after the images.
316
-		add_filter( 'img_caption_shortcode', array( $this, 'custom_img_caption' ), 10, 3 );
316
+		add_filter('img_caption_shortcode', array($this, 'custom_img_caption'), 10, 3);
317 317
 
318 318
 		// Overwrite the native shortcode handler to add pretty captions.
319 319
 		// http://wordpress.stackexchange.com/a/74675/54456 for explanation.
320
-		add_shortcode( 'gallery', array( $this, 'custom_gallery_shortcode' ) );
320
+		add_shortcode('gallery', array($this, 'custom_gallery_shortcode'));
321 321
 
322
-		$full_content = apply_filters( 'the_content', $this->post->post_content );
322
+		$full_content = apply_filters('the_content', $this->post->post_content);
323 323
 
324 324
 		// Put the removed shortcode tags back again.
325 325
 		$shortcode_tags += $shortcode_tags_bkp;
@@ -341,14 +341,14 @@  discard block
 block discarded – undo
341 341
 		// 3. Manually trimmed content.
342 342
 		$content = $this->post->post_content;
343 343
 		$excerpt = $this->post->post_excerpt;
344
-		if ( '' === $excerpt ) {
345
-			if ( $more_pos = strpos( $content, '<!--more' ) ) {
346
-				$excerpt = substr( $content, 0, $more_pos );
344
+		if ('' === $excerpt) {
345
+			if ($more_pos = strpos($content, '<!--more')) {
346
+				$excerpt = substr($content, 0, $more_pos);
347 347
 			} else {
348
-				$excerpt = wp_trim_words( $content, 42, '[...]' );
348
+				$excerpt = wp_trim_words($content, 42, '[...]');
349 349
 			}
350 350
 		}
351
-		return '<p>' . $excerpt . '</p>';
351
+		return '<p>'.$excerpt.'</p>';
352 352
 	}
353 353
 
354 354
 	/**
@@ -364,41 +364,41 @@  discard block
 block discarded – undo
364 364
 		$tags_to_add  = '';
365 365
 
366 366
 		// Add any diaspora* tags?
367
-		if ( ! empty( $tags_to_post ) ) {
367
+		if ( ! empty($tags_to_post)) {
368 368
 			// The diaspora* tags to add to the post.
369 369
 			$diaspora_tags = array();
370 370
 
371 371
 			// Add global tags?
372
-			$global_tags = $options->get_option( 'global_tags' );
373
-			if ( in_array( 'global', $tags_to_post ) && is_array( $global_tags ) ) {
374
-				$diaspora_tags += array_flip( $global_tags );
372
+			$global_tags = $options->get_option('global_tags');
373
+			if (in_array('global', $tags_to_post) && is_array($global_tags)) {
374
+				$diaspora_tags += array_flip($global_tags);
375 375
 			}
376 376
 
377 377
 			// Add custom tags?
378
-			if ( in_array( 'custom', $tags_to_post ) && is_array( $this->custom_tags ) ) {
379
-				$diaspora_tags += array_flip( $this->custom_tags );
378
+			if (in_array('custom', $tags_to_post) && is_array($this->custom_tags)) {
379
+				$diaspora_tags += array_flip($this->custom_tags);
380 380
 			}
381 381
 
382 382
 			// Add post tags?
383
-			$post_tags = wp_get_post_tags( $this->ID, array( 'fields' => 'slugs' ) );
384
-			if ( in_array( 'post', $tags_to_post ) && is_array( $post_tags ) ) {
385
-				$diaspora_tags += array_flip( $post_tags );
383
+			$post_tags = wp_get_post_tags($this->ID, array('fields' => 'slugs'));
384
+			if (in_array('post', $tags_to_post) && is_array($post_tags)) {
385
+				$diaspora_tags += array_flip($post_tags);
386 386
 			}
387 387
 
388 388
 			// Get an array of cleaned up tags.
389 389
 			// NOTE: Validate method needs a variable, as it's passed by reference!
390
-			$diaspora_tags = array_keys( $diaspora_tags );
391
-			$options->validate_tags( $diaspora_tags );
390
+			$diaspora_tags = array_keys($diaspora_tags);
391
+			$options->validate_tags($diaspora_tags);
392 392
 
393 393
 			// Get all the tags and list them all nicely in a row.
394 394
 			$diaspora_tags_clean = array();
395
-			foreach ( $diaspora_tags as $tag ) {
396
-				$diaspora_tags_clean[] = '#' . $tag;
395
+			foreach ($diaspora_tags as $tag) {
396
+				$diaspora_tags_clean[] = '#'.$tag;
397 397
 			}
398 398
 
399 399
 			// Add all the found tags.
400
-			if ( ! empty( $diaspora_tags_clean ) ) {
401
-				$tags_to_add = implode( ' ', $diaspora_tags_clean ) . '<br />';
400
+			if ( ! empty($diaspora_tags_clean)) {
401
+				$tags_to_add = implode(' ', $diaspora_tags_clean).'<br />';
402 402
 			}
403 403
 		}
404 404
 
@@ -414,12 +414,12 @@  discard block
 block discarded – undo
414 414
 	 */
415 415
 	private function _get_posted_at_link() {
416 416
 		$link = '';
417
-		if ( $this->fullentrylink ) {
417
+		if ($this->fullentrylink) {
418 418
 
419
-			$text = esc_html( 'Originally posted at:', 'wp-to-diaspora' );
420
-			$permalink = get_permalink( $this->ID );
421
-			$title = esc_html( 'Permalink', 'wp-to-diaspora' );
422
-			$default = sprintf( '%1$s <a href="%2$s" title="%3$s">%2$s</a>', $text, $permalink, $title );
419
+			$text = esc_html('Originally posted at:', 'wp-to-diaspora');
420
+			$permalink = get_permalink($this->ID);
421
+			$title = esc_html('Permalink', 'wp-to-diaspora');
422
+			$default = sprintf('%1$s <a href="%2$s" title="%3$s">%2$s</a>', $text, $permalink, $title);
423 423
 
424 424
 			/**
425 425
 			 * Filter the "Originally posted at" link at the bottom of the post.
@@ -431,9 +431,9 @@  discard block
 block discarded – undo
431 431
 			 * @param string $permalink The permalink of the original post.
432 432
 			 * @param string $title     The "Permalink" title of the link.
433 433
 			 */
434
-			$link = apply_filters( 'wp2d_posted_at_link_filter', $default, $text, $permalink, $title );
434
+			$link = apply_filters('wp2d_posted_at_link_filter', $default, $text, $permalink, $title);
435 435
 
436
-			$link = '<p>' . $link . '</p>';
436
+			$link = '<p>'.$link.'</p>';
437 437
 		}
438 438
 
439 439
 		return $link;
@@ -446,9 +446,9 @@  discard block
 block discarded – undo
446 446
 	 *
447 447
 	 * @param object $response Response from the API containing the diaspora* post details.
448 448
 	 */
449
-	private function _save_to_history( $response ) {
449
+	private function _save_to_history($response) {
450 450
 		// Make sure the post history is an array.
451
-		if ( empty( $this->post_history ) ) {
451
+		if (empty($this->post_history)) {
452 452
 			$this->post_history = array();
453 453
 		}
454 454
 
@@ -462,7 +462,7 @@  discard block
 block discarded – undo
462 462
 			'post_url'   => $response->permalink,
463 463
 		);
464 464
 
465
-		update_post_meta( $this->ID, '_wp_to_diaspora_post_history', $this->post_history );
465
+		update_post_meta($this->ID, '_wp_to_diaspora_post_history', $this->post_history);
466 466
 	}
467 467
 
468 468
 	/**
@@ -475,7 +475,7 @@  discard block
 block discarded – undo
475 475
 	 * @param string $url  The attempted embed URL.
476 476
 	 * @return string URL of the embed.
477 477
 	 */
478
-	public function embed_url( $html, $url ) {
478
+	public function embed_url($html, $url) {
479 479
 		return $url;
480 480
 	}
481 481
 
@@ -489,8 +489,8 @@  discard block
 block discarded – undo
489 489
 	 * @param string $content Content of the post.
490 490
 	 * @return string The content with the embed tags removed.
491 491
 	 */
492
-	public function embed_remove( $content ) {
493
-		return str_replace( array( '[embed]', '[/embed]' ), array( '<p>', '</p>' ), $content );
492
+	public function embed_remove($content) {
493
+		return str_replace(array('[embed]', '[/embed]'), array('<p>', '</p>'), $content);
494 494
 	}
495 495
 
496 496
 	/**
@@ -501,13 +501,13 @@  discard block
 block discarded – undo
501 501
 	 * @param string $caption Caption to be prettified.
502 502
 	 * @return string Prettified image caption.
503 503
 	 */
504
-	public function get_img_caption( $caption ) {
505
-		$caption = trim( $caption );
506
-		if ( '' === $caption ) {
504
+	public function get_img_caption($caption) {
505
+		$caption = trim($caption);
506
+		if ('' === $caption) {
507 507
 			return '';
508 508
 		}
509 509
 
510
-		$default = sprintf( '<blockquote>%s</blockquote>',  $caption );
510
+		$default = sprintf('<blockquote>%s</blockquote>', $caption);
511 511
 
512 512
 		/**
513 513
 		 * Filter the image caption to be displayed after images with captions.
@@ -517,7 +517,7 @@  discard block
 block discarded – undo
517 517
 		 * @param string $default The whole HTML of the caption.
518 518
 		 * @param string $caption The caption text.
519 519
 		 */
520
-		return apply_filters( 'wp2d_image_caption', $default, $caption );
520
+		return apply_filters('wp2d_image_caption', $default, $caption);
521 521
 	}
522 522
 
523 523
 	/**
@@ -531,12 +531,12 @@  discard block
 block discarded – undo
531 531
 	 * @param string $content The image element, possibly wrapped in a hyperlink.
532 532
 	 * @return string The caption shortcode output.
533 533
 	 */
534
-	public function custom_img_caption( $empty, $attr, $content ) {
535
-		$content = do_shortcode( $content );
534
+	public function custom_img_caption($empty, $attr, $content) {
535
+		$content = do_shortcode($content);
536 536
 
537 537
 		// If a caption attribute is defined, we'll add it after the image.
538
-		if ( isset( $attr['caption'] ) && '' !== $attr['caption'] ) {
539
-			$content .= "\n" . $this->get_img_caption( $attr['caption'] );
538
+		if (isset($attr['caption']) && '' !== $attr['caption']) {
539
+			$content .= "\n".$this->get_img_caption($attr['caption']);
540 540
 		}
541 541
 
542 542
 		return $content;
@@ -550,22 +550,22 @@  discard block
 block discarded – undo
550 550
 	 * @param   array $attr Gallery attributes.
551 551
 	 * @return  string
552 552
 	 */
553
-	public function custom_gallery_shortcode( $attr ) {
553
+	public function custom_gallery_shortcode($attr) {
554 554
 		// Default value in WordPress.
555
-		$captiontag = ( current_theme_supports( 'html5', 'gallery' ) ) ? 'figcaption' : 'dd';
555
+		$captiontag = (current_theme_supports('html5', 'gallery')) ? 'figcaption' : 'dd';
556 556
 
557 557
 		// User value.
558
-		if ( isset( $attr['captiontag'] ) ) {
558
+		if (isset($attr['captiontag'])) {
559 559
 			$captiontag = $attr['captiontag'];
560 560
 		}
561 561
 
562 562
 		// Let WordPress create the regular gallery.
563
-		$gallery = gallery_shortcode( $attr );
563
+		$gallery = gallery_shortcode($attr);
564 564
 
565 565
 		// Change the content of the captions.
566 566
 		$gallery = preg_replace_callback(
567
-			'~(<' . $captiontag . '.*>)(.*)(</' . $captiontag . '>)~mUus',
568
-			array( $this, 'custom_gallery_regex_callback' ),
567
+			'~(<'.$captiontag.'.*>)(.*)(</'.$captiontag.'>)~mUus',
568
+			array($this, 'custom_gallery_regex_callback'),
569 569
 			$gallery
570 570
 		);
571 571
 
@@ -578,8 +578,8 @@  discard block
 block discarded – undo
578 578
 	 * @param array $m Regex matches.
579 579
 	 * @return string Prettified gallery image caption.
580 580
 	 */
581
-	public function custom_gallery_regex_callback( $m ) {
582
-		return $this->get_img_caption( $m[2] );
581
+	public function custom_gallery_regex_callback($m) {
582
+		return $this->get_img_caption($m[2]);
583 583
 	}
584 584
 
585 585
 	/*
@@ -593,11 +593,11 @@  discard block
 block discarded – undo
593 593
 	 */
594 594
 	public function add_meta_boxes() {
595 595
 		$options = WP2D_Options::instance();
596
-		foreach ( $options->get_option( 'enabled_post_types' ) as $post_type ) {
596
+		foreach ($options->get_option('enabled_post_types') as $post_type) {
597 597
 			add_meta_box(
598 598
 				'wp_to_diaspora_meta_box',
599 599
 				'WP to diaspora*',
600
-				array( $this, 'meta_box_render' ),
600
+				array($this, 'meta_box_render'),
601 601
 				$post_type,
602 602
 				'side',
603 603
 				'high'
@@ -612,11 +612,11 @@  discard block
 block discarded – undo
612 612
 	 *
613 613
 	 * @param WP_Post $post The object for the current post.
614 614
 	 */
615
-	public function meta_box_render( $post ) {
616
-		$this->_assign_wp_post( $post );
615
+	public function meta_box_render($post) {
616
+		$this->_assign_wp_post($post);
617 617
 
618 618
 		// Add an nonce field so we can check for it later.
619
-		wp_nonce_field( 'wp_to_diaspora_meta_box', 'wp_to_diaspora_meta_box_nonce' );
619
+		wp_nonce_field('wp_to_diaspora_meta_box', 'wp_to_diaspora_meta_box_nonce');
620 620
 
621 621
 		// Get the default values to use, but give priority to the meta data already set.
622 622
 		$options = WP2D_Options::instance();
@@ -625,26 +625,26 @@  discard block
 block discarded – undo
625 625
 		$this->custom_tags = $this->custom_tags ?: array();
626 626
 
627 627
 		// If this post is already published, don't post again to diaspora* by default.
628
-		$this->post_to_diaspora = ( $this->post_to_diaspora && 'publish' !== get_post_status( $this->ID ) );
629
-		$this->aspects          = $this->aspects  ?: array();
628
+		$this->post_to_diaspora = ($this->post_to_diaspora && 'publish' !== get_post_status($this->ID));
629
+		$this->aspects          = $this->aspects ?: array();
630 630
 		$this->services         = $this->services ?: array();
631 631
 
632 632
 		// Have we already posted on diaspora*?
633
-		if ( is_array( $this->post_history ) ) {
634
-			$latest_post = end( $this->post_history );
633
+		if (is_array($this->post_history)) {
634
+			$latest_post = end($this->post_history);
635 635
 			?>
636
-			<p><a href="<?php echo esc_attr( $latest_post['post_url'] ); ?>" target="_blank"><?php esc_html_e( 'Already posted to diaspora*.', 'wp-to-diaspora' ); ?></a></p>
636
+			<p><a href="<?php echo esc_attr($latest_post['post_url']); ?>" target="_blank"><?php esc_html_e('Already posted to diaspora*.', 'wp-to-diaspora'); ?></a></p>
637 637
 			<?php
638 638
 		}
639 639
 		?>
640 640
 
641
-		<p><?php $options->post_to_diaspora_render( $this->post_to_diaspora ); ?></p>
642
-		<p><?php $options->fullentrylink_render( $this->fullentrylink ); ?></p>
643
-		<p><?php $options->display_render( $this->display ); ?></p>
644
-		<p><?php $options->tags_to_post_render( $this->tags_to_post ); ?></p>
645
-		<p><?php $options->custom_tags_render( $this->custom_tags ); ?></p>
646
-		<p><?php $options->aspects_services_render( array( 'aspects', $this->aspects ) ); ?></p>
647
-		<p><?php $options->aspects_services_render( array( 'services', $this->services ) ); ?></p>
641
+		<p><?php $options->post_to_diaspora_render($this->post_to_diaspora); ?></p>
642
+		<p><?php $options->fullentrylink_render($this->fullentrylink); ?></p>
643
+		<p><?php $options->display_render($this->display); ?></p>
644
+		<p><?php $options->tags_to_post_render($this->tags_to_post); ?></p>
645
+		<p><?php $options->custom_tags_render($this->custom_tags); ?></p>
646
+		<p><?php $options->aspects_services_render(array('aspects', $this->aspects)); ?></p>
647
+		<p><?php $options->aspects_services_render(array('services', $this->services)); ?></p>
648 648
 
649 649
 		<?php
650 650
 	}
@@ -656,12 +656,12 @@  discard block
 block discarded – undo
656 656
 	 *
657 657
 	 * @param integer $post_id The ID of the post being saved.
658 658
 	 */
659
-	public function save_meta_box_data( $post_id ) {
659
+	public function save_meta_box_data($post_id) {
660 660
 		/*
661 661
 		 * We need to verify this came from our screen and with proper authorization,
662 662
 		 * because the save_post action can be triggered at other times.
663 663
 		 */
664
-		if ( ! $this->_is_safe_to_save() ) {
664
+		if ( ! $this->_is_safe_to_save()) {
665 665
 			return;
666 666
 		}
667 667
 
@@ -672,25 +672,25 @@  discard block
 block discarded – undo
672 672
 		$options = WP2D_Options::instance();
673 673
 
674 674
 		// Checkboxes.
675
-		$options->validate_checkboxes( array( 'post_to_diaspora', 'fullentrylink' ), $meta_to_save );
675
+		$options->validate_checkboxes(array('post_to_diaspora', 'fullentrylink'), $meta_to_save);
676 676
 
677 677
 		// Single Selects.
678
-		$options->validate_single_selects( 'display', $meta_to_save );
678
+		$options->validate_single_selects('display', $meta_to_save);
679 679
 
680 680
 		// Multiple Selects.
681
-		$options->validate_multi_selects( 'tags_to_post', $meta_to_save );
681
+		$options->validate_multi_selects('tags_to_post', $meta_to_save);
682 682
 
683 683
 		// Save custom tags as array.
684
-		$options->validate_tags( $meta_to_save['custom_tags'] );
684
+		$options->validate_tags($meta_to_save['custom_tags']);
685 685
 
686 686
 		// Clean up the list of aspects. If the list is empty, only use the 'Public' aspect.
687
-		$options->validate_aspects_services( $meta_to_save['aspects'], array( 'public' ) );
687
+		$options->validate_aspects_services($meta_to_save['aspects'], array('public'));
688 688
 
689 689
 		// Clean up the list of services.
690
-		$options->validate_aspects_services( $meta_to_save['services'] );
690
+		$options->validate_aspects_services($meta_to_save['services']);
691 691
 
692 692
 		// Update the meta data for this post.
693
-		update_post_meta( $post_id, '_wp_to_diaspora', $meta_to_save );
693
+		update_post_meta($post_id, '_wp_to_diaspora', $meta_to_save);
694 694
 	}
695 695
 
696 696
 	/**
@@ -702,23 +702,23 @@  discard block
 block discarded – undo
702 702
 	 */
703 703
 	private function _is_safe_to_save() {
704 704
 		// Verify that our nonce is set and  valid.
705
-		if ( ! ( isset( $_POST['wp_to_diaspora_meta_box_nonce'] ) && wp_verify_nonce( $_POST['wp_to_diaspora_meta_box_nonce'], 'wp_to_diaspora_meta_box' ) ) ) {
705
+		if ( ! (isset($_POST['wp_to_diaspora_meta_box_nonce']) && wp_verify_nonce($_POST['wp_to_diaspora_meta_box_nonce'], 'wp_to_diaspora_meta_box'))) {
706 706
 			return false;
707 707
 		}
708 708
 
709 709
 		// If this is an autosave, our form has not been submitted, so we don't want to do anything.
710
-		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
710
+		if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
711 711
 			return false;
712 712
 		}
713 713
 
714 714
 		// Check the user's permissions.
715
-		$permission = ( isset( $_POST['post_type'] ) && 'page' === $_POST['post_type'] ) ? 'edit_pages' : 'edit_posts';
716
-		if ( ! current_user_can( $permission, $this->ID ) ) {
715
+		$permission = (isset($_POST['post_type']) && 'page' === $_POST['post_type']) ? 'edit_pages' : 'edit_posts';
716
+		if ( ! current_user_can($permission, $this->ID)) {
717 717
 			return false;
718 718
 		}
719 719
 
720 720
 		// Make real sure that we have some meta data to save.
721
-		if ( ! isset( $_POST['wp_to_diaspora_settings'] ) ) {
721
+		if ( ! isset($_POST['wp_to_diaspora_settings'])) {
722 722
 			return false;
723 723
 		}
724 724
 
@@ -734,32 +734,32 @@  discard block
 block discarded – undo
734 734
 	 */
735 735
 	public function admin_notices() {
736 736
 		global $post, $pagenow;
737
-		if ( ! $post || 'post.php' !== $pagenow ) {
737
+		if ( ! $post || 'post.php' !== $pagenow) {
738 738
 			return;
739 739
 		}
740 740
 
741
-		if ( ( $error = get_post_meta( $post->ID, '_wp_to_diaspora_post_error', true ) ) && is_wp_error( $error ) ) {
741
+		if (($error = get_post_meta($post->ID, '_wp_to_diaspora_post_error', true)) && is_wp_error($error)) {
742 742
 			// Are we adding a help tab link to this notice?
743
-			$help_link = WP2D_Contextual_Help::get_help_tab_quick_link( $error );
743
+			$help_link = WP2D_Contextual_Help::get_help_tab_quick_link($error);
744 744
 
745 745
 			// This notice will only be shown if posting to diaspora* has failed.
746
-			printf( '<div class="error notice is-dismissible"><p>%1$s %2$s %3$s <a href="%4$s">%5$s</a></p></div>',
747
-				esc_html__( 'Failed to post to diaspora*.', 'wp-to-diaspora' ),
748
-				esc_html__( $error->get_error_message() ),
746
+			printf('<div class="error notice is-dismissible"><p>%1$s %2$s %3$s <a href="%4$s">%5$s</a></p></div>',
747
+				esc_html__('Failed to post to diaspora*.', 'wp-to-diaspora'),
748
+				esc_html__($error->get_error_message()),
749 749
 				$help_link,
750
-				esc_url( add_query_arg( 'wp2d_ignore_post_error', '' ) ),
751
-				esc_html__( 'Ignore', 'wp-to-diaspora' )
750
+				esc_url(add_query_arg('wp2d_ignore_post_error', '')),
751
+				esc_html__('Ignore', 'wp-to-diaspora')
752 752
 			);
753
-		} elseif ( ( $diaspora_post_history = get_post_meta( $post->ID, '_wp_to_diaspora_post_history', true ) ) && is_array( $diaspora_post_history ) ) {
753
+		} elseif (($diaspora_post_history = get_post_meta($post->ID, '_wp_to_diaspora_post_history', true)) && is_array($diaspora_post_history)) {
754 754
 			// Get the latest post from the history.
755
-			$latest_post = end( $diaspora_post_history );
755
+			$latest_post = end($diaspora_post_history);
756 756
 
757 757
 			// Only show if this post is showing a message and the post is a fresh share.
758
-			if ( isset( $_GET['message'] ) && $post->post_modified === $latest_post['created_at'] ) {
759
-				printf( '<div class="updated notice is-dismissible"><p>%1$s <a href="%2$s" target="_blank">%3$s</a></p></div>',
760
-					esc_html__( 'Successfully posted to diaspora*.', 'wp-to-diaspora' ),
761
-					esc_url( $latest_post['post_url'] ),
762
-					esc_html__( 'View Post' )
758
+			if (isset($_GET['message']) && $post->post_modified === $latest_post['created_at']) {
759
+				printf('<div class="updated notice is-dismissible"><p>%1$s <a href="%2$s" target="_blank">%3$s</a></p></div>',
760
+					esc_html__('Successfully posted to diaspora*.', 'wp-to-diaspora'),
761
+					esc_url($latest_post['post_url']),
762
+					esc_html__('View Post')
763 763
 				);
764 764
 			}
765 765
 		}
@@ -772,8 +772,8 @@  discard block
 block discarded – undo
772 772
 	 */
773 773
 	public function ignore_post_error() {
774 774
 		// If "Ignore" link has been clicked, delete the post error meta data.
775
-		if ( isset( $_GET['wp2d_ignore_post_error'], $_GET['post'] ) ) {
776
-			delete_post_meta( $_GET['post'], '_wp_to_diaspora_post_error' );
775
+		if (isset($_GET['wp2d_ignore_post_error'], $_GET['post'])) {
776
+			delete_post_meta($_GET['post'], '_wp_to_diaspora_post_error');
777 777
 		}
778 778
 	}
779 779
 }
Please login to merge, or discard this patch.
wp-to-diaspora.php 1 patch
Spacing   +100 added lines, -100 removed lines patch added patch discarded remove patch
@@ -30,10 +30,10 @@  discard block
 block discarded – undo
30 30
  */
31 31
 
32 32
 // Exit if accessed directly.
33
-defined( 'ABSPATH' ) || exit;
33
+defined('ABSPATH') || exit;
34 34
 
35 35
 // Set the current version.
36
-define( 'WP2D_VERSION', '1.7.1' );
36
+define('WP2D_VERSION', '1.7.1');
37 37
 
38 38
 /**
39 39
  * WP to diaspora* main plugin class.
@@ -78,10 +78,10 @@  discard block
 block discarded – undo
78 78
 	 * @return WP_To_Diaspora Instance of this class.
79 79
 	 */
80 80
 	public static function instance() {
81
-		if ( ! isset( self::$_instance ) ) {
81
+		if ( ! isset(self::$_instance)) {
82 82
 			self::$_instance = new self();
83 83
 			self::$_instance->_constants();
84
-			if ( self::$_instance->_version_check() ) {
84
+			if (self::$_instance->_version_check()) {
85 85
 				self::$_instance->_includes();
86 86
 				self::$_instance->_setup();
87 87
 			} else {
@@ -98,13 +98,13 @@  discard block
 block discarded – undo
98 98
 	 */
99 99
 	private function _constants() {
100 100
 		// Are we in debugging mode?
101
-		if ( isset( $_GET['debugging'] ) ) {
102
-			define( 'WP2D_DEBUGGING', true );
101
+		if (isset($_GET['debugging'])) {
102
+			define('WP2D_DEBUGGING', true);
103 103
 		}
104 104
 
105
-		define( 'WP2D_DIR', dirname( __FILE__ ) );
106
-		define( 'WP2D_LIB_DIR', WP2D_DIR . '/lib' );
107
-		define( 'WP2D_VENDOR_DIR', WP2D_DIR . '/vendor' );
105
+		define('WP2D_DIR', dirname(__FILE__));
106
+		define('WP2D_LIB_DIR', WP2D_DIR.'/lib');
107
+		define('WP2D_VENDOR_DIR', WP2D_DIR.'/vendor');
108 108
 	}
109 109
 
110 110
 	/**
@@ -116,9 +116,9 @@  discard block
 block discarded – undo
116 116
 	 */
117 117
 	private function _version_check() {
118 118
 		// Check for version requirements.
119
-		if ( version_compare( $GLOBALS['wp_version'], $this->_min_wp, '<' )
120
-			|| version_compare( PHP_VERSION, $this->_min_php, '<' ) ) {
121
-			add_action( 'admin_notices', array( $this, 'deactivate' ) );
119
+		if (version_compare($GLOBALS['wp_version'], $this->_min_wp, '<')
120
+			|| version_compare(PHP_VERSION, $this->_min_php, '<')) {
121
+			add_action('admin_notices', array($this, 'deactivate'));
122 122
 			return false;
123 123
 		}
124 124
 
@@ -132,15 +132,15 @@  discard block
 block discarded – undo
132 132
 	 */
133 133
 	public function deactivate() {
134 134
 		// First of all, deactivate the plugin.
135
-		deactivate_plugins( plugin_basename( __FILE__ ) );
135
+		deactivate_plugins(plugin_basename(__FILE__));
136 136
 
137 137
 		// Get rid of the "Plugin activated" message.
138
-		unset( $_GET['activate'] );
138
+		unset($_GET['activate']);
139 139
 
140 140
 		// Then display the admin notice.
141 141
 		?>
142 142
 		<div class="error">
143
-			<p><?php echo esc_html( sprintf( 'WP to diaspora* requires at least WordPress %1$s (you have %2$s) and PHP %3$s (you have %4$s)!', $this->_min_wp, $GLOBALS['wp_version'], $this->_min_php, PHP_VERSION ) ); ?></p>
143
+			<p><?php echo esc_html(sprintf('WP to diaspora* requires at least WordPress %1$s (you have %2$s) and PHP %3$s (you have %4$s)!', $this->_min_wp, $GLOBALS['wp_version'], $this->_min_php, PHP_VERSION)); ?></p>
144 144
 		</div>
145 145
 		<?php
146 146
 	}
@@ -151,12 +151,12 @@  discard block
 block discarded – undo
151 151
 	 * @since 1.5.0
152 152
 	 */
153 153
 	private function _includes() {
154
-		require WP2D_VENDOR_DIR . '/autoload.php';
155
-		require_once WP2D_LIB_DIR . '/class-api.php';
156
-		require_once WP2D_LIB_DIR . '/class-contextual-help.php';
157
-		require_once WP2D_LIB_DIR . '/class-helpers.php';
158
-		require_once WP2D_LIB_DIR . '/class-options.php';
159
-		require_once WP2D_LIB_DIR . '/class-post.php';
154
+		require WP2D_VENDOR_DIR.'/autoload.php';
155
+		require_once WP2D_LIB_DIR.'/class-api.php';
156
+		require_once WP2D_LIB_DIR.'/class-contextual-help.php';
157
+		require_once WP2D_LIB_DIR.'/class-helpers.php';
158
+		require_once WP2D_LIB_DIR.'/class-options.php';
159
+		require_once WP2D_LIB_DIR.'/class-post.php';
160 160
 	}
161 161
 
162 162
 	/**
@@ -165,30 +165,30 @@  discard block
 block discarded – undo
165 165
 	private function _setup() {
166 166
 
167 167
 		// Load languages.
168
-		add_action( 'plugins_loaded', array( $this, 'l10n' ) );
168
+		add_action('plugins_loaded', array($this, 'l10n'));
169 169
 
170 170
 		// Add "Settings" link to plugin page.
171
-		add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'settings_link' ) );
171
+		add_filter('plugin_action_links_'.plugin_basename(__FILE__), array($this, 'settings_link'));
172 172
 
173 173
 		// Perform any necessary data upgrades.
174
-		add_action( 'admin_init', array( $this, 'upgrade' ) );
174
+		add_action('admin_init', array($this, 'upgrade'));
175 175
 
176 176
 		// Enqueue CSS and JS scripts.
177
-		add_action( 'admin_enqueue_scripts', array( $this, 'admin_load_scripts' ) );
177
+		add_action('admin_enqueue_scripts', array($this, 'admin_load_scripts'));
178 178
 
179 179
 		// Set up the options.
180
-		add_action( 'init', array( 'WP2D_Options', 'instance' ) );
180
+		add_action('init', array('WP2D_Options', 'instance'));
181 181
 
182 182
 		// WP2D Post.
183
-		add_action( 'init', array( 'WP2D_Post', 'setup' ) );
183
+		add_action('init', array('WP2D_Post', 'setup'));
184 184
 
185 185
 		// AJAX actions for loading pods, aspects and services.
186
-		add_action( 'wp_ajax_wp_to_diaspora_update_pod_list', array( $this, 'update_pod_list_callback' ) );
187
-		add_action( 'wp_ajax_wp_to_diaspora_update_aspects_list', array( $this, 'update_aspects_list_callback' ) );
188
-		add_action( 'wp_ajax_wp_to_diaspora_update_services_list', array( $this, 'update_services_list_callback' ) );
186
+		add_action('wp_ajax_wp_to_diaspora_update_pod_list', array($this, 'update_pod_list_callback'));
187
+		add_action('wp_ajax_wp_to_diaspora_update_aspects_list', array($this, 'update_aspects_list_callback'));
188
+		add_action('wp_ajax_wp_to_diaspora_update_services_list', array($this, 'update_services_list_callback'));
189 189
 
190 190
 		// Check the pod connection status on the options page.
191
-		add_action( 'wp_ajax_wp_to_diaspora_check_pod_connection_status', array( $this, 'check_pod_connection_status_callback' ) );
191
+		add_action('wp_ajax_wp_to_diaspora_check_pod_connection_status', array($this, 'check_pod_connection_status_callback'));
192 192
 	}
193 193
 
194 194
 	/**
@@ -197,7 +197,7 @@  discard block
 block discarded – undo
197 197
 	 * @return WP2D_API|boolean The API object, or false.
198 198
 	 */
199 199
 	private function _load_api() {
200
-		if ( ! isset( $this->_api ) ) {
200
+		if ( ! isset($this->_api)) {
201 201
 			$this->_api = WP2D_Helpers::api_quick_connect();
202 202
 		}
203 203
 		return $this->_api;
@@ -209,40 +209,40 @@  discard block
 block discarded – undo
209 209
 	public function upgrade() {
210 210
 		// Get the current options, or assign defaults.
211 211
 		$options = WP2D_Options::instance();
212
-		$version = $options->get_option( 'version' );
212
+		$version = $options->get_option('version');
213 213
 
214 214
 		// If the versions differ, this is probably an update. Need to save updated options.
215
-		if ( WP2D_VERSION !== $version ) {
215
+		if (WP2D_VERSION !== $version) {
216 216
 
217 217
 			// Password is stored encrypted since version 1.2.7.
218 218
 			// When upgrading to it, the plain text password is encrypted and saved again.
219
-			if ( version_compare( $version, '1.2.7', '<' ) ) {
220
-				$options->set_option( 'password', WP2D_Helpers::encrypt( (string) $options->get_option( 'password' ) ) );
219
+			if (version_compare($version, '1.2.7', '<')) {
220
+				$options->set_option('password', WP2D_Helpers::encrypt((string) $options->get_option('password')));
221 221
 			}
222 222
 
223
-			if ( version_compare( $version, '1.3.0', '<' ) ) {
223
+			if (version_compare($version, '1.3.0', '<')) {
224 224
 				// The 'user' setting is renamed to 'username'.
225
-				$options->set_option( 'username', $options->get_option( 'user' ) );
226
-				$options->set_option( 'user', null );
225
+				$options->set_option('username', $options->get_option('user'));
226
+				$options->set_option('user', null);
227 227
 
228 228
 				// Save tags as arrays instead of comma seperated values.
229
-				$global_tags = $options->get_option( 'global_tags' );
230
-				$options->set_option( 'global_tags', $options->validate_tags( $global_tags ) );
229
+				$global_tags = $options->get_option('global_tags');
230
+				$options->set_option('global_tags', $options->validate_tags($global_tags));
231 231
 			}
232 232
 
233
-			if ( version_compare( $version, '1.4.0', '<' ) ) {
233
+			if (version_compare($version, '1.4.0', '<')) {
234 234
 				// Turn tags_to_post string into an array.
235
-				$tags_to_post_old = $options->get_option( 'tags_to_post' );
236
-				$tags_to_post = array_filter( array(
237
-					( ( false !== strpos( $tags_to_post_old, 'g' ) ) ? 'global' : null ),
238
-					( ( false !== strpos( $tags_to_post_old, 'c' ) ) ? 'custom' : null ),
239
-					( ( false !== strpos( $tags_to_post_old, 'p' ) ) ? 'post'   : null ),
240
-				) );
241
-				$options->set_option( 'tags_to_post', $tags_to_post );
235
+				$tags_to_post_old = $options->get_option('tags_to_post');
236
+				$tags_to_post = array_filter(array(
237
+					((false !== strpos($tags_to_post_old, 'g')) ? 'global' : null),
238
+					((false !== strpos($tags_to_post_old, 'c')) ? 'custom' : null),
239
+					((false !== strpos($tags_to_post_old, 'p')) ? 'post' : null),
240
+				));
241
+				$options->set_option('tags_to_post', $tags_to_post);
242 242
 			}
243 243
 
244 244
 			// Update version.
245
-			$options->set_option( 'version', WP2D_VERSION );
245
+			$options->set_option('version', WP2D_VERSION);
246 246
 			$options->save();
247 247
 		}
248 248
 	}
@@ -251,7 +251,7 @@  discard block
 block discarded – undo
251 251
 	 * Set up i18n.
252 252
 	 */
253 253
 	public function l10n() {
254
-		load_plugin_textdomain( 'wp-to-diaspora', false, 'wp-to-diaspora/languages' );
254
+		load_plugin_textdomain('wp-to-diaspora', false, 'wp-to-diaspora/languages');
255 255
 	}
256 256
 
257 257
 	/**
@@ -259,27 +259,27 @@  discard block
 block discarded – undo
259 259
 	 */
260 260
 	public function admin_load_scripts() {
261 261
 		// Get the enabled post types to load the script for.
262
-		$enabled_post_types = WP2D_Options::instance()->get_option( 'enabled_post_types', array() );
262
+		$enabled_post_types = WP2D_Options::instance()->get_option('enabled_post_types', array());
263 263
 
264 264
 		// Get the screen to find out where we are.
265 265
 		$screen = get_current_screen();
266 266
 
267 267
 		// Only load the styles and scripts on the settings page and the allowed post types.
268
-		if ( 'settings_page_wp_to_diaspora' === $screen->id || ( in_array( $screen->post_type, $enabled_post_types ) && 'post' === $screen->base ) ) {
269
-			wp_enqueue_style( 'tag-it', plugins_url( '/css/jquery.tagit.css', __FILE__ ) );
270
-			wp_enqueue_style( 'chosen', plugins_url( '/css/chosen.min.css', __FILE__ ) );
271
-			wp_enqueue_style( 'wp-to-diaspora-admin', plugins_url( '/css/wp-to-diaspora.css', __FILE__ ) );
272
-			wp_enqueue_script( 'chosen', plugins_url( '/js/chosen.jquery.min.js', __FILE__ ), array( 'jquery' ), false, true );
273
-			wp_enqueue_script( 'tag-it', plugins_url( '/js/tag-it.min.js', __FILE__ ), array( 'jquery', 'jquery-ui-autocomplete' ), false, true );
274
-			wp_enqueue_script( 'wp-to-diaspora-admin', plugins_url( '/js/wp-to-diaspora.js', __FILE__ ), array( 'jquery' ), false, true );
268
+		if ('settings_page_wp_to_diaspora' === $screen->id || (in_array($screen->post_type, $enabled_post_types) && 'post' === $screen->base)) {
269
+			wp_enqueue_style('tag-it', plugins_url('/css/jquery.tagit.css', __FILE__));
270
+			wp_enqueue_style('chosen', plugins_url('/css/chosen.min.css', __FILE__));
271
+			wp_enqueue_style('wp-to-diaspora-admin', plugins_url('/css/wp-to-diaspora.css', __FILE__));
272
+			wp_enqueue_script('chosen', plugins_url('/js/chosen.jquery.min.js', __FILE__), array('jquery'), false, true);
273
+			wp_enqueue_script('tag-it', plugins_url('/js/tag-it.min.js', __FILE__), array('jquery', 'jquery-ui-autocomplete'), false, true);
274
+			wp_enqueue_script('wp-to-diaspora-admin', plugins_url('/js/wp-to-diaspora.js', __FILE__), array('jquery'), false, true);
275 275
 			// Javascript-specific l10n.
276
-			wp_localize_script( 'wp-to-diaspora-admin', 'WP2DL10n', array(
277
-				'no_services_connected' => __( 'No services connected yet.', 'wp-to-diaspora' ),
278
-				'sure_reset_defaults'   => __( 'Are you sure you want to reset to default values?', 'wp-to-diaspora' ),
279
-				'conn_testing'          => __( 'Testing connection...', 'wp-to-diaspora' ),
280
-				'conn_successful'       => __( 'Connection successful.', 'wp-to-diaspora' ),
281
-				'conn_failed'           => __( 'Connection failed.', 'wp-to-diaspora' ),
282
-			) );
276
+			wp_localize_script('wp-to-diaspora-admin', 'WP2DL10n', array(
277
+				'no_services_connected' => __('No services connected yet.', 'wp-to-diaspora'),
278
+				'sure_reset_defaults'   => __('Are you sure you want to reset to default values?', 'wp-to-diaspora'),
279
+				'conn_testing'          => __('Testing connection...', 'wp-to-diaspora'),
280
+				'conn_successful'       => __('Connection successful.', 'wp-to-diaspora'),
281
+				'conn_failed'           => __('Connection failed.', 'wp-to-diaspora'),
282
+			));
283 283
 		}
284 284
 	}
285 285
 
@@ -289,8 +289,8 @@  discard block
 block discarded – undo
289 289
 	 * @param array $links Links to display for plugin on plugins page.
290 290
 	 * @return array Links to display for plugin on plugins page.
291 291
 	 */
292
-	public function settings_link( $links ) {
293
-		$links[] = '<a href="' . admin_url( 'options-general.php?page=wp_to_diaspora' ) . '">' . __( 'Settings' ) . '</a>';
292
+	public function settings_link($links) {
293
+		$links[] = '<a href="'.admin_url('options-general.php?page=wp_to_diaspora').'">'.__('Settings').'</a>';
294 294
 		return $links;
295 295
 	}
296 296
 
@@ -305,14 +305,14 @@  discard block
 block discarded – undo
305 305
 		$pods = array();
306 306
 
307 307
 		// Get the response from the WP_HTTP request.
308
-		$response = wp_safe_remote_get( $pod_list_url );
308
+		$response = wp_safe_remote_get($pod_list_url);
309 309
 
310
-		if ( $json = wp_remote_retrieve_body( $response ) ) {
311
-			$pod_list = json_decode( $json );
310
+		if ($json = wp_remote_retrieve_body($response)) {
311
+			$pod_list = json_decode($json);
312 312
 
313
-			if ( isset( $pod_list->pods ) ) {
314
-				foreach ( $pod_list->pods as $pod ) {
315
-					if ( 'no' === $pod->hidden ) {
313
+			if (isset($pod_list->pods)) {
314
+				foreach ($pod_list->pods as $pod) {
315
+					if ('no' === $pod->hidden) {
316 316
 						$pods[] = array(
317 317
 							'secure' => $pod->secure,
318 318
 							'domain' => $pod->domain,
@@ -321,7 +321,7 @@  discard block
 block discarded – undo
321 321
 				}
322 322
 
323 323
 				$options = WP2D_Options::instance();
324
-				$options->set_option( 'pod_list', $pods );
324
+				$options->set_option('pod_list', $pods);
325 325
 				$options->save();
326 326
 			}
327 327
 		}
@@ -333,7 +333,7 @@  discard block
 block discarded – undo
333 333
 	 * Update the list of pods and return them for use with AJAX.
334 334
 	 */
335 335
 	public function update_pod_list_callback() {
336
-		wp_send_json( $this->_update_pod_list() );
336
+		wp_send_json($this->_update_pod_list());
337 337
 	}
338 338
 
339 339
 	/**
@@ -344,40 +344,40 @@  discard block
 block discarded – undo
344 344
 	 * @param string $type Type of list to update.
345 345
 	 * @return array|boolean The list of aspects or services, false if an illegal parameter is passed.
346 346
 	 */
347
-	private function _update_aspects_services_list( $type ) {
347
+	private function _update_aspects_services_list($type) {
348 348
 		// Check for correct argument value.
349
-		if ( ! in_array( $type, array( 'aspects', 'services' ) ) ) {
349
+		if ( ! in_array($type, array('aspects', 'services'))) {
350 350
 			return false;
351 351
 		}
352 352
 
353 353
 		$options = WP2D_Options::instance();
354
-		$list    = $options->get_option( $type . '_list' );
354
+		$list    = $options->get_option($type.'_list');
355 355
 
356 356
 		// Make sure that we have at least the 'Public' aspect.
357
-		if ( 'aspects' === $type && empty( $list ) ) {
358
-			$list = array( 'public' => __( 'Public' ) );
357
+		if ('aspects' === $type && empty($list)) {
358
+			$list = array('public' => __('Public'));
359 359
 		}
360 360
 
361 361
 		// Set up the connection to diaspora*.
362 362
 		$api = $this->_load_api();
363 363
 
364 364
 		// If there was a problem loading the API, return false.
365
-		if ( $api->has_last_error() ) {
365
+		if ($api->has_last_error()) {
366 366
 			return false;
367 367
 		}
368 368
 
369
-		if ( 'aspects' === $type ) {
370
-			$list_new = $api->get_aspects( true );
371
-		} elseif ( 'services' === $type ) {
372
-			$list_new = $api->get_services( true );
369
+		if ('aspects' === $type) {
370
+			$list_new = $api->get_aspects(true);
371
+		} elseif ('services' === $type) {
372
+			$list_new = $api->get_services(true);
373 373
 		}
374 374
 		// If the new list couldn't be fetched successfully, return false.
375
-		if ( $api->has_last_error() ) {
375
+		if ($api->has_last_error()) {
376 376
 			return false;
377 377
 		}
378 378
 
379 379
 		// We have a new list to save and return!
380
-		$options->set_option( $type . '_list', $list_new );
380
+		$options->set_option($type.'_list', $list_new);
381 381
 		$options->save();
382 382
 
383 383
 		return $list_new;
@@ -387,14 +387,14 @@  discard block
 block discarded – undo
387 387
 	 * Update the list of aspects and return them for use with AJAX.
388 388
 	 */
389 389
 	public function update_aspects_list_callback() {
390
-		wp_send_json( $this->_update_aspects_services_list( 'aspects' ) );
390
+		wp_send_json($this->_update_aspects_services_list('aspects'));
391 391
 	}
392 392
 
393 393
 	/**
394 394
 	 * Update the list of services and return them for use with AJAX.
395 395
 	 */
396 396
 	public function update_services_list_callback() {
397
-		wp_send_json( $this->_update_aspects_services_list( 'services' ) );
397
+		wp_send_json($this->_update_aspects_services_list('services'));
398 398
 	}
399 399
 
400 400
 	/**
@@ -407,7 +407,7 @@  discard block
 block discarded – undo
407 407
 
408 408
 		$status = null;
409 409
 
410
-		if ( $options->is_pod_set_up() ) {
410
+		if ($options->is_pod_set_up()) {
411 411
 			$status = ! $this->_load_api()->has_last_error();
412 412
 		}
413 413
 
@@ -420,22 +420,22 @@  discard block
 block discarded – undo
420 420
 	 * @todo esc_html
421 421
 	 */
422 422
 	public function check_pod_connection_status_callback() {
423
-		if ( isset( $_REQUEST['debugging'] ) && ! defined( 'WP2D_DEBUGGING' ) ) {
424
-			define( 'WP2D_DEBUGGING', true );
423
+		if (isset($_REQUEST['debugging']) && ! defined('WP2D_DEBUGGING')) {
424
+			define('WP2D_DEBUGGING', true);
425 425
 		}
426 426
 
427 427
 		$status = $this->_check_pod_connection_status();
428 428
 
429 429
 		$data = array(
430
-			'debug'   => esc_textarea( WP2D_Helpers::get_debugging() ),
431
-			'message' => __( 'Connection successful.', 'wp-to-diaspora' ),
430
+			'debug'   => esc_textarea(WP2D_Helpers::get_debugging()),
431
+			'message' => __('Connection successful.', 'wp-to-diaspora'),
432 432
 		);
433 433
 
434
-		if ( true === $status ) {
435
-			wp_send_json_success( $data );
436
-		} elseif ( false === $status && $this->_load_api()->has_last_error() ) {
437
-			$data['message'] = $this->_load_api()->get_last_error() . ' ' . WP2D_Contextual_Help::get_help_tab_quick_link( $this->_load_api()->get_last_error_object() );
438
-			wp_send_json_error( $data );
434
+		if (true === $status) {
435
+			wp_send_json_success($data);
436
+		} elseif (false === $status && $this->_load_api()->has_last_error()) {
437
+			$data['message'] = $this->_load_api()->get_last_error().' '.WP2D_Contextual_Help::get_help_tab_quick_link($this->_load_api()->get_last_error_object());
438
+			wp_send_json_error($data);
439 439
 		}
440 440
 		// If $status === null, do nothing.
441 441
 	}
Please login to merge, or discard this patch.