Completed
Push — develop ( f6c608...ecad78 )
by Armando
10s
created
wp-to-diaspora.php 1 patch
Indentation   +395 added lines, -395 removed lines patch added patch discarded remove patch
@@ -40,405 +40,405 @@
 block discarded – undo
40 40
  */
41 41
 class WP_To_Diaspora {
42 42
 
43
-	/**
44
-	 * Only instance of this class.
45
-	 *
46
-	 * @var WP_To_Diaspora
47
-	 */
48
-	private static $_instance = null;
49
-
50
-	/**
51
-	 * The minimum required WordPress version.
52
-	 *
53
-	 * @since 1.5.4
54
-	 *
55
-	 * @var string
56
-	 */
57
-	private $_min_wp = '3.9.2';
58
-
59
-	/**
60
-	 * The minimum required PHP version.
61
-	 *
62
-	 * @since 1.5.4
63
-	 *
64
-	 * @var string
65
-	 */
66
-	private $_min_php = '5.3';
67
-
68
-	/**
69
-	 * Instance of the API class.
70
-	 *
71
-	 * @var WP2D_API
72
-	 */
73
-	private $_api = null;
74
-
75
-	/**
76
-	 * Create / Get the instance of this class.
77
-	 *
78
-	 * @return WP_To_Diaspora Instance of this class.
79
-	 */
80
-	public static function instance() {
81
-		if ( ! isset( self::$_instance ) ) {
82
-			self::$_instance = new self();
83
-			self::$_instance->_constants();
84
-			if ( self::$_instance->_version_check() ) {
85
-				self::$_instance->_includes();
86
-				self::$_instance->_setup();
87
-			} else {
88
-				self::$_instance = null;
89
-			}
90
-		}
91
-		return self::$_instance;
92
-	}
93
-
94
-	/**
95
-	 * Define all the required constants.
96
-	 *
97
-	 * @since 1.5.0
98
-	 */
99
-	private function _constants() {
100
-		// Are we in debugging mode?
101
-		if ( isset( $_GET['debugging'] ) ) {
102
-			define( 'WP2D_DEBUGGING', true );
103
-		}
104
-
105
-		define( 'WP2D_DIR', dirname( __FILE__ ) );
106
-		define( 'WP2D_LIB_DIR', WP2D_DIR . '/lib' );
107
-		define( 'WP2D_VENDOR_DIR', WP2D_DIR . '/vendor' );
108
-	}
109
-
110
-	/**
111
-	 * Check the minimum WordPress and PHP requirements.
112
-	 *
113
-	 * @since 1.5.4
114
-	 *
115
-	 * @return bool If version requirements are met.
116
-	 */
117
-	private function _version_check() {
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' ) );
122
-			return false;
123
-		}
124
-
125
-		return true;
126
-	}
127
-
128
-	/**
129
-	 * Callback to deactivate plugin and display admin notice.
130
-	 *
131
-	 * @since 1.5.4
132
-	 */
133
-	public function deactivate() {
134
-		// First of all, deactivate the plugin.
135
-		deactivate_plugins( plugin_basename( __FILE__ ) );
136
-
137
-		// Get rid of the "Plugin activated" message.
138
-		unset( $_GET['activate'] );
139
-
140
-		// Then display the admin notice.
141
-		?>
43
+  /**
44
+   * Only instance of this class.
45
+   *
46
+   * @var WP_To_Diaspora
47
+   */
48
+  private static $_instance = null;
49
+
50
+  /**
51
+   * The minimum required WordPress version.
52
+   *
53
+   * @since 1.5.4
54
+   *
55
+   * @var string
56
+   */
57
+  private $_min_wp = '3.9.2';
58
+
59
+  /**
60
+   * The minimum required PHP version.
61
+   *
62
+   * @since 1.5.4
63
+   *
64
+   * @var string
65
+   */
66
+  private $_min_php = '5.3';
67
+
68
+  /**
69
+   * Instance of the API class.
70
+   *
71
+   * @var WP2D_API
72
+   */
73
+  private $_api = null;
74
+
75
+  /**
76
+   * Create / Get the instance of this class.
77
+   *
78
+   * @return WP_To_Diaspora Instance of this class.
79
+   */
80
+  public static function instance() {
81
+    if ( ! isset( self::$_instance ) ) {
82
+      self::$_instance = new self();
83
+      self::$_instance->_constants();
84
+      if ( self::$_instance->_version_check() ) {
85
+        self::$_instance->_includes();
86
+        self::$_instance->_setup();
87
+      } else {
88
+        self::$_instance = null;
89
+      }
90
+    }
91
+    return self::$_instance;
92
+  }
93
+
94
+  /**
95
+   * Define all the required constants.
96
+   *
97
+   * @since 1.5.0
98
+   */
99
+  private function _constants() {
100
+    // Are we in debugging mode?
101
+    if ( isset( $_GET['debugging'] ) ) {
102
+      define( 'WP2D_DEBUGGING', true );
103
+    }
104
+
105
+    define( 'WP2D_DIR', dirname( __FILE__ ) );
106
+    define( 'WP2D_LIB_DIR', WP2D_DIR . '/lib' );
107
+    define( 'WP2D_VENDOR_DIR', WP2D_DIR . '/vendor' );
108
+  }
109
+
110
+  /**
111
+   * Check the minimum WordPress and PHP requirements.
112
+   *
113
+   * @since 1.5.4
114
+   *
115
+   * @return bool If version requirements are met.
116
+   */
117
+  private function _version_check() {
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' ) );
122
+      return false;
123
+    }
124
+
125
+    return true;
126
+  }
127
+
128
+  /**
129
+   * Callback to deactivate plugin and display admin notice.
130
+   *
131
+   * @since 1.5.4
132
+   */
133
+  public function deactivate() {
134
+    // First of all, deactivate the plugin.
135
+    deactivate_plugins( plugin_basename( __FILE__ ) );
136
+
137
+    // Get rid of the "Plugin activated" message.
138
+    unset( $_GET['activate'] );
139
+
140
+    // Then display the admin notice.
141
+    ?>
142 142
 		<div class="error">
143 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
-	}
147
-
148
-	/**
149
-	 * Include all the required files.
150
-	 *
151
-	 * @since 1.5.0
152
-	 */
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';
160
-	}
161
-
162
-	/**
163
-	 * Set up the plugin.
164
-	 */
165
-	private function _setup() {
166
-
167
-		// Load languages.
168
-		add_action( 'plugins_loaded', array( $this, 'l10n' ) );
169
-
170
-		// Add "Settings" link to plugin page.
171
-		add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'settings_link' ) );
172
-
173
-		// Perform any necessary data upgrades.
174
-		add_action( 'admin_init', array( $this, 'upgrade' ) );
175
-
176
-		// Enqueue CSS and JS scripts.
177
-		add_action( 'admin_enqueue_scripts', array( $this, 'admin_load_scripts' ) );
178
-
179
-		// Set up the options.
180
-		add_action( 'init', array( 'WP2D_Options', 'instance' ) );
181
-
182
-		// WP2D Post.
183
-		add_action( 'init', array( 'WP2D_Post', 'setup' ) );
184
-
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' ) );
189
-
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' ) );
192
-	}
193
-
194
-	/**
195
-	 * Load the diaspora* API for ease of use.
196
-	 *
197
-	 * @return WP2D_API|boolean The API object, or false.
198
-	 */
199
-	private function _load_api() {
200
-		if ( ! isset( $this->_api ) ) {
201
-			$this->_api = WP2D_Helpers::api_quick_connect();
202
-		}
203
-		return $this->_api;
204
-	}
205
-
206
-	/**
207
-	 * Initialise upgrade sequence.
208
-	 */
209
-	public function upgrade() {
210
-		// Get the current options, or assign defaults.
211
-		$options = WP2D_Options::instance();
212
-		$version = $options->get_option( 'version' );
213
-
214
-		// If the versions differ, this is probably an update. Need to save updated options.
215
-		if ( WP2D_VERSION !== $version ) {
216
-
217
-			// Password is stored encrypted since version 1.2.7.
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' ) ) );
221
-			}
222
-
223
-			if ( version_compare( $version, '1.3.0', '<' ) ) {
224
-				// The 'user' setting is renamed to 'username'.
225
-				$options->set_option( 'username', $options->get_option( 'user' ) );
226
-				$options->set_option( 'user', null );
227
-
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 ) );
231
-			}
232
-
233
-			if ( version_compare( $version, '1.4.0', '<' ) ) {
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 );
242
-			}
243
-
244
-			// Update version.
245
-			$options->set_option( 'version', WP2D_VERSION );
246
-			$options->save();
247
-		}
248
-	}
249
-
250
-	/**
251
-	 * Set up i18n.
252
-	 */
253
-	public function l10n() {
254
-		load_plugin_textdomain( 'wp-to-diaspora', false, 'wp-to-diaspora/languages' );
255
-	}
256
-
257
-	/**
258
-	 * Load scripts and styles for Settings and Post pages of allowed post types.
259
-	 */
260
-	public function admin_load_scripts() {
261
-		// Get the enabled post types to load the script for.
262
-		$enabled_post_types = WP2D_Options::instance()->get_option( 'enabled_post_types', array() );
263
-
264
-		// Get the screen to find out where we are.
265
-		$screen = get_current_screen();
266
-
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 );
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
-			) );
283
-		}
284
-	}
285
-
286
-	/**
287
-	 * Add the "Settings" link to the plugins page.
288
-	 *
289
-	 * @param array $links Links to display for plugin on plugins page.
290
-	 * @return array Links to display for plugin on plugins page.
291
-	 */
292
-	public function settings_link( $links ) {
293
-		$links[] = '<a href="' . admin_url( 'options-general.php?page=wp_to_diaspora' ) . '">' . __( 'Settings' ) . '</a>';
294
-		return $links;
295
-	}
296
-
297
-	/**
298
-	 * Fetch the updated list of pods from podupti.me and save it to the settings.
299
-	 *
300
-	 * @return array The list of pods.
301
-	 */
302
-	private function _update_pod_list() {
303
-		// API url to fetch pods list from podupti.me.
304
-		$pod_list_url = 'http://podupti.me/api.php?format=json&key=4r45tg';
305
-		$pods = array();
306
-
307
-		// Get the response from the WP_HTTP request.
308
-		$response = wp_safe_remote_get( $pod_list_url );
309
-
310
-		if ( $json = wp_remote_retrieve_body( $response ) ) {
311
-			$pod_list = json_decode( $json );
312
-
313
-			if ( isset( $pod_list->pods ) ) {
314
-				foreach ( $pod_list->pods as $pod ) {
315
-					if ( 'no' === $pod->hidden ) {
316
-						$pods[] = array(
317
-							'secure' => $pod->secure,
318
-							'domain' => $pod->domain,
319
-						);
320
-					}
321
-				}
322
-
323
-				$options = WP2D_Options::instance();
324
-				$options->set_option( 'pod_list', $pods );
325
-				$options->save();
326
-			}
327
-		}
328
-
329
-		return $pods;
330
-	}
331
-
332
-	/**
333
-	 * Update the list of pods and return them for use with AJAX.
334
-	 */
335
-	public function update_pod_list_callback() {
336
-		wp_send_json( $this->_update_pod_list() );
337
-	}
338
-
339
-	/**
340
-	 * Fetch the list of aspects or services and save them to the settings.
341
-	 *
342
-	 * NOTE: When updating the lists, always force a fresh fetch.
343
-	 *
344
-	 * @param string $type Type of list to update.
345
-	 * @return array|boolean The list of aspects or services, false if an illegal parameter is passed.
346
-	 */
347
-	private function _update_aspects_services_list( $type ) {
348
-		// Check for correct argument value.
349
-		if ( ! in_array( $type, array( 'aspects', 'services' ) ) ) {
350
-			return false;
351
-		}
352
-
353
-		$options = WP2D_Options::instance();
354
-		$list    = $options->get_option( $type . '_list' );
355
-
356
-		// Make sure that we have at least the 'Public' aspect.
357
-		if ( 'aspects' === $type && empty( $list ) ) {
358
-			$list = array( 'public' => __( 'Public' ) );
359
-		}
360
-
361
-		// Set up the connection to diaspora*.
362
-		$api = $this->_load_api();
363
-
364
-		// If there was a problem loading the API, return false.
365
-		if ( $api->has_last_error() ) {
366
-			return false;
367
-		}
368
-
369
-		if ( 'aspects' === $type ) {
370
-			$list_new = $api->get_aspects( true );
371
-		} elseif ( 'services' === $type ) {
372
-			$list_new = $api->get_services( true );
373
-		}
374
-		// If the new list couldn't be fetched successfully, return false.
375
-		if ( $api->has_last_error() ) {
376
-			return false;
377
-		}
378
-
379
-		// We have a new list to save and return!
380
-		$options->set_option( $type . '_list', $list_new );
381
-		$options->save();
382
-
383
-		return $list_new;
384
-	}
385
-
386
-	/**
387
-	 * Update the list of aspects and return them for use with AJAX.
388
-	 */
389
-	public function update_aspects_list_callback() {
390
-		wp_send_json( $this->_update_aspects_services_list( 'aspects' ) );
391
-	}
392
-
393
-	/**
394
-	 * Update the list of services and return them for use with AJAX.
395
-	 */
396
-	public function update_services_list_callback() {
397
-		wp_send_json( $this->_update_aspects_services_list( 'services' ) );
398
-	}
399
-
400
-	/**
401
-	 * Check the pod connection status.
402
-	 *
403
-	 * @return string The status of the connection.
404
-	 */
405
-	private function _check_pod_connection_status() {
406
-		$options = WP2D_Options::instance();
407
-
408
-		$status = null;
409
-
410
-		if ( $options->is_pod_set_up() ) {
411
-			$status = ! $this->_load_api()->has_last_error();
412
-		}
413
-
414
-		return $status;
415
-	}
416
-
417
-	/**
418
-	 * Check the connection to the pod and return the status for use with AJAX.
419
-	 *
420
-	 * @todo esc_html
421
-	 */
422
-	public function check_pod_connection_status_callback() {
423
-		if ( isset( $_REQUEST['debugging'] ) && ! defined( 'WP2D_DEBUGGING' ) ) {
424
-			define( 'WP2D_DEBUGGING', true );
425
-		}
426
-
427
-		$status = $this->_check_pod_connection_status();
428
-
429
-		$data = array(
430
-			'debug'   => esc_textarea( WP2D_Helpers::get_debugging() ),
431
-			'message' => __( 'Connection successful.', 'wp-to-diaspora' ),
432
-		);
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 );
439
-		}
440
-		// If $status === null, do nothing.
441
-	}
146
+  }
147
+
148
+  /**
149
+   * Include all the required files.
150
+   *
151
+   * @since 1.5.0
152
+   */
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';
160
+  }
161
+
162
+  /**
163
+   * Set up the plugin.
164
+   */
165
+  private function _setup() {
166
+
167
+    // Load languages.
168
+    add_action( 'plugins_loaded', array( $this, 'l10n' ) );
169
+
170
+    // Add "Settings" link to plugin page.
171
+    add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'settings_link' ) );
172
+
173
+    // Perform any necessary data upgrades.
174
+    add_action( 'admin_init', array( $this, 'upgrade' ) );
175
+
176
+    // Enqueue CSS and JS scripts.
177
+    add_action( 'admin_enqueue_scripts', array( $this, 'admin_load_scripts' ) );
178
+
179
+    // Set up the options.
180
+    add_action( 'init', array( 'WP2D_Options', 'instance' ) );
181
+
182
+    // WP2D Post.
183
+    add_action( 'init', array( 'WP2D_Post', 'setup' ) );
184
+
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' ) );
189
+
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' ) );
192
+  }
193
+
194
+  /**
195
+   * Load the diaspora* API for ease of use.
196
+   *
197
+   * @return WP2D_API|boolean The API object, or false.
198
+   */
199
+  private function _load_api() {
200
+    if ( ! isset( $this->_api ) ) {
201
+      $this->_api = WP2D_Helpers::api_quick_connect();
202
+    }
203
+    return $this->_api;
204
+  }
205
+
206
+  /**
207
+   * Initialise upgrade sequence.
208
+   */
209
+  public function upgrade() {
210
+    // Get the current options, or assign defaults.
211
+    $options = WP2D_Options::instance();
212
+    $version = $options->get_option( 'version' );
213
+
214
+    // If the versions differ, this is probably an update. Need to save updated options.
215
+    if ( WP2D_VERSION !== $version ) {
216
+
217
+      // Password is stored encrypted since version 1.2.7.
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' ) ) );
221
+      }
222
+
223
+      if ( version_compare( $version, '1.3.0', '<' ) ) {
224
+        // The 'user' setting is renamed to 'username'.
225
+        $options->set_option( 'username', $options->get_option( 'user' ) );
226
+        $options->set_option( 'user', null );
227
+
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 ) );
231
+      }
232
+
233
+      if ( version_compare( $version, '1.4.0', '<' ) ) {
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 );
242
+      }
243
+
244
+      // Update version.
245
+      $options->set_option( 'version', WP2D_VERSION );
246
+      $options->save();
247
+    }
248
+  }
249
+
250
+  /**
251
+   * Set up i18n.
252
+   */
253
+  public function l10n() {
254
+    load_plugin_textdomain( 'wp-to-diaspora', false, 'wp-to-diaspora/languages' );
255
+  }
256
+
257
+  /**
258
+   * Load scripts and styles for Settings and Post pages of allowed post types.
259
+   */
260
+  public function admin_load_scripts() {
261
+    // Get the enabled post types to load the script for.
262
+    $enabled_post_types = WP2D_Options::instance()->get_option( 'enabled_post_types', array() );
263
+
264
+    // Get the screen to find out where we are.
265
+    $screen = get_current_screen();
266
+
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 );
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
+      ) );
283
+    }
284
+  }
285
+
286
+  /**
287
+   * Add the "Settings" link to the plugins page.
288
+   *
289
+   * @param array $links Links to display for plugin on plugins page.
290
+   * @return array Links to display for plugin on plugins page.
291
+   */
292
+  public function settings_link( $links ) {
293
+    $links[] = '<a href="' . admin_url( 'options-general.php?page=wp_to_diaspora' ) . '">' . __( 'Settings' ) . '</a>';
294
+    return $links;
295
+  }
296
+
297
+  /**
298
+   * Fetch the updated list of pods from podupti.me and save it to the settings.
299
+   *
300
+   * @return array The list of pods.
301
+   */
302
+  private function _update_pod_list() {
303
+    // API url to fetch pods list from podupti.me.
304
+    $pod_list_url = 'http://podupti.me/api.php?format=json&key=4r45tg';
305
+    $pods = array();
306
+
307
+    // Get the response from the WP_HTTP request.
308
+    $response = wp_safe_remote_get( $pod_list_url );
309
+
310
+    if ( $json = wp_remote_retrieve_body( $response ) ) {
311
+      $pod_list = json_decode( $json );
312
+
313
+      if ( isset( $pod_list->pods ) ) {
314
+        foreach ( $pod_list->pods as $pod ) {
315
+          if ( 'no' === $pod->hidden ) {
316
+            $pods[] = array(
317
+              'secure' => $pod->secure,
318
+              'domain' => $pod->domain,
319
+            );
320
+          }
321
+        }
322
+
323
+        $options = WP2D_Options::instance();
324
+        $options->set_option( 'pod_list', $pods );
325
+        $options->save();
326
+      }
327
+    }
328
+
329
+    return $pods;
330
+  }
331
+
332
+  /**
333
+   * Update the list of pods and return them for use with AJAX.
334
+   */
335
+  public function update_pod_list_callback() {
336
+    wp_send_json( $this->_update_pod_list() );
337
+  }
338
+
339
+  /**
340
+   * Fetch the list of aspects or services and save them to the settings.
341
+   *
342
+   * NOTE: When updating the lists, always force a fresh fetch.
343
+   *
344
+   * @param string $type Type of list to update.
345
+   * @return array|boolean The list of aspects or services, false if an illegal parameter is passed.
346
+   */
347
+  private function _update_aspects_services_list( $type ) {
348
+    // Check for correct argument value.
349
+    if ( ! in_array( $type, array( 'aspects', 'services' ) ) ) {
350
+      return false;
351
+    }
352
+
353
+    $options = WP2D_Options::instance();
354
+    $list    = $options->get_option( $type . '_list' );
355
+
356
+    // Make sure that we have at least the 'Public' aspect.
357
+    if ( 'aspects' === $type && empty( $list ) ) {
358
+      $list = array( 'public' => __( 'Public' ) );
359
+    }
360
+
361
+    // Set up the connection to diaspora*.
362
+    $api = $this->_load_api();
363
+
364
+    // If there was a problem loading the API, return false.
365
+    if ( $api->has_last_error() ) {
366
+      return false;
367
+    }
368
+
369
+    if ( 'aspects' === $type ) {
370
+      $list_new = $api->get_aspects( true );
371
+    } elseif ( 'services' === $type ) {
372
+      $list_new = $api->get_services( true );
373
+    }
374
+    // If the new list couldn't be fetched successfully, return false.
375
+    if ( $api->has_last_error() ) {
376
+      return false;
377
+    }
378
+
379
+    // We have a new list to save and return!
380
+    $options->set_option( $type . '_list', $list_new );
381
+    $options->save();
382
+
383
+    return $list_new;
384
+  }
385
+
386
+  /**
387
+   * Update the list of aspects and return them for use with AJAX.
388
+   */
389
+  public function update_aspects_list_callback() {
390
+    wp_send_json( $this->_update_aspects_services_list( 'aspects' ) );
391
+  }
392
+
393
+  /**
394
+   * Update the list of services and return them for use with AJAX.
395
+   */
396
+  public function update_services_list_callback() {
397
+    wp_send_json( $this->_update_aspects_services_list( 'services' ) );
398
+  }
399
+
400
+  /**
401
+   * Check the pod connection status.
402
+   *
403
+   * @return string The status of the connection.
404
+   */
405
+  private function _check_pod_connection_status() {
406
+    $options = WP2D_Options::instance();
407
+
408
+    $status = null;
409
+
410
+    if ( $options->is_pod_set_up() ) {
411
+      $status = ! $this->_load_api()->has_last_error();
412
+    }
413
+
414
+    return $status;
415
+  }
416
+
417
+  /**
418
+   * Check the connection to the pod and return the status for use with AJAX.
419
+   *
420
+   * @todo esc_html
421
+   */
422
+  public function check_pod_connection_status_callback() {
423
+    if ( isset( $_REQUEST['debugging'] ) && ! defined( 'WP2D_DEBUGGING' ) ) {
424
+      define( 'WP2D_DEBUGGING', true );
425
+    }
426
+
427
+    $status = $this->_check_pod_connection_status();
428
+
429
+    $data = array(
430
+      'debug'   => esc_textarea( WP2D_Helpers::get_debugging() ),
431
+      'message' => __( 'Connection successful.', 'wp-to-diaspora' ),
432
+    );
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 );
439
+    }
440
+    // If $status === null, do nothing.
441
+  }
442 442
 }
443 443
 
444 444
 // Get the party started!
Please login to merge, or discard this patch.
lib/class-options.php 1 patch
Indentation   +717 added lines, -717 removed lines patch added patch discarded remove patch
@@ -14,314 +14,314 @@  discard block
 block discarded – undo
14 14
  */
15 15
 class WP2D_Options {
16 16
 
17
-	/**
18
-	 * Only instance of this class.
19
-	 *
20
-	 * @var WP2D_Options
21
-	 */
22
-	private static $_instance = null;
23
-
24
-	/**
25
-	 * All default plugin options.
26
-	 *
27
-	 * @var array
28
-	 */
29
-	private static $_default_options = array(
30
-		'pod_list'           => array(),
31
-		'aspects_list'       => array(),
32
-		'services_list'      => array(),
33
-		'post_to_diaspora'   => true,
34
-		'enabled_post_types' => array( 'post' ),
35
-		'fullentrylink'      => true,
36
-		'display'            => 'full',
37
-		'tags_to_post'       => array( 'global', 'custom', 'post' ),
38
-		'global_tags'        => '',
39
-		'aspects'            => array( 'public' ),
40
-		'services'           => array(),
41
-		'version'            => WP2D_VERSION,
42
-	);
43
-
44
-	/**
45
-	 * Valid values for select fields.
46
-	 *
47
-	 * @var array
48
-	 */
49
-	private static $_valid_values = array(
50
-		'display'      => array( 'full', 'excerpt' ),
51
-		'tags_to_post' => array( 'global', 'custom', 'post' ),
52
-	);
53
-
54
-	/**
55
-	 * All plugin options.
56
-	 *
57
-	 * @var array
58
-	 */
59
-	private static $_options = null;
60
-
61
-	/** Singleton, keep private. */
62
-	final private function __clone() { }
63
-
64
-	/** Singleton, keep private. */
65
-	final private function __construct() { }
66
-
67
-	/** Singleton, keep private. */
68
-	final private function __wakeup() { }
69
-
70
-	/**
71
-	 * Create / Get the instance of this class.
72
-	 *
73
-	 * @return WP2D_Options Instance of this class.
74
-	 */
75
-	public static function instance() {
76
-		if ( ! isset( self::$_instance ) ) {
77
-			self::$_instance = new self();
78
-			self::$_instance->_setup();
79
-		}
80
-		return self::$_instance;
81
-	}
82
-
83
-	/**
84
-	 * Set up the options menu.
85
-	 */
86
-	private function _setup() {
87
-
88
-		// Populate options array.
89
-		$this->get_option();
90
-
91
-		// Setup Options page and Contextual Help.
92
-		add_action( 'admin_menu', array( $this, 'setup_wpadmin_pages' ) );
93
-
94
-		// Register all settings.
95
-		add_action( 'admin_init', array( $this, 'register_settings' ) );
96
-	}
97
-
98
-
99
-	/**
100
-	 * Get the currently selected tab.
101
-	 *
102
-	 * @todo Multi-level if statement to make it look prettier.
103
-	 *
104
-	 * @param string $default Tab to select if the current selection is invalid.
105
-	 * @return string Return the currently selected tab.
106
-	 */
107
-	private function _current_tab( $default = 'defaults' ) {
108
-		$tab = ( isset ( $_GET['tab'] ) ? $_GET['tab'] : $default );
109
-
110
-		// If the pod settings aren't configured yet, open the 'Setup' tab.
111
-		if ( ! $this->is_pod_set_up() ) {
112
-			$tab = 'setup';
113
-		}
114
-
115
-		return $tab;
116
-	}
117
-
118
-	/**
119
-	 * Output all options tabs and return an array of them all, if requested by $return.
120
-	 *
121
-	 * @param bool $return Define if the options tabs should be returned.
122
-	 * @return array (If requested) An array of the outputted options tabs.
123
-	 */
124
-	private function _options_page_tabs( $return = false ) {
125
-		// The array defining all options sections to be shown as tabs.
126
-		$tabs = array();
127
-		if ( $this->is_pod_set_up() ) {
128
-			$tabs['defaults'] = __( 'Defaults', 'wp-to-diaspora' );
129
-		}
130
-
131
-		// Add the 'Setup' tab to the end of the list.
132
-		$tabs['setup'] = __( 'Setup', 'wp-to-diaspora' ) . '<span id="pod-connection-status" class="dashicons-before hidden"></span><span class="spinner"></span>';
133
-
134
-		// Container for all options tabs.
135
-		$out = '<h2 id="options-tabs" class="nav-tab-wrapper">';
136
-		foreach ( $tabs as $tab => $name ) {
137
-			// The tab link.
138
-			$out .= '<a class="nav-tab' . ( ( $tab === $this->_current_tab() ) ? ' nav-tab-active' : '' ) . '" href="?page=wp_to_diaspora&tab=' . $tab . '">' . $name . '</a>';
139
-		}
140
-		$out .= '</h2>';
141
-
142
-		// Output the container with all tabs.
143
-		echo $out;
144
-
145
-		// Check if the tabs should be returned.
146
-		if ( $return ) {
147
-			return $tabs;
148
-		}
149
-	}
150
-
151
-
152
-	/**
153
-	 * Set up admin options page.
154
-	 */
155
-	public function admin_options_page() {
156
-		?>
17
+  /**
18
+   * Only instance of this class.
19
+   *
20
+   * @var WP2D_Options
21
+   */
22
+  private static $_instance = null;
23
+
24
+  /**
25
+   * All default plugin options.
26
+   *
27
+   * @var array
28
+   */
29
+  private static $_default_options = array(
30
+    'pod_list'           => array(),
31
+    'aspects_list'       => array(),
32
+    'services_list'      => array(),
33
+    'post_to_diaspora'   => true,
34
+    'enabled_post_types' => array( 'post' ),
35
+    'fullentrylink'      => true,
36
+    'display'            => 'full',
37
+    'tags_to_post'       => array( 'global', 'custom', 'post' ),
38
+    'global_tags'        => '',
39
+    'aspects'            => array( 'public' ),
40
+    'services'           => array(),
41
+    'version'            => WP2D_VERSION,
42
+  );
43
+
44
+  /**
45
+   * Valid values for select fields.
46
+   *
47
+   * @var array
48
+   */
49
+  private static $_valid_values = array(
50
+    'display'      => array( 'full', 'excerpt' ),
51
+    'tags_to_post' => array( 'global', 'custom', 'post' ),
52
+  );
53
+
54
+  /**
55
+   * All plugin options.
56
+   *
57
+   * @var array
58
+   */
59
+  private static $_options = null;
60
+
61
+  /** Singleton, keep private. */
62
+  final private function __clone() { }
63
+
64
+  /** Singleton, keep private. */
65
+  final private function __construct() { }
66
+
67
+  /** Singleton, keep private. */
68
+  final private function __wakeup() { }
69
+
70
+  /**
71
+   * Create / Get the instance of this class.
72
+   *
73
+   * @return WP2D_Options Instance of this class.
74
+   */
75
+  public static function instance() {
76
+    if ( ! isset( self::$_instance ) ) {
77
+      self::$_instance = new self();
78
+      self::$_instance->_setup();
79
+    }
80
+    return self::$_instance;
81
+  }
82
+
83
+  /**
84
+   * Set up the options menu.
85
+   */
86
+  private function _setup() {
87
+
88
+    // Populate options array.
89
+    $this->get_option();
90
+
91
+    // Setup Options page and Contextual Help.
92
+    add_action( 'admin_menu', array( $this, 'setup_wpadmin_pages' ) );
93
+
94
+    // Register all settings.
95
+    add_action( 'admin_init', array( $this, 'register_settings' ) );
96
+  }
97
+
98
+
99
+  /**
100
+   * Get the currently selected tab.
101
+   *
102
+   * @todo Multi-level if statement to make it look prettier.
103
+   *
104
+   * @param string $default Tab to select if the current selection is invalid.
105
+   * @return string Return the currently selected tab.
106
+   */
107
+  private function _current_tab( $default = 'defaults' ) {
108
+    $tab = ( isset ( $_GET['tab'] ) ? $_GET['tab'] : $default );
109
+
110
+    // If the pod settings aren't configured yet, open the 'Setup' tab.
111
+    if ( ! $this->is_pod_set_up() ) {
112
+      $tab = 'setup';
113
+    }
114
+
115
+    return $tab;
116
+  }
117
+
118
+  /**
119
+   * Output all options tabs and return an array of them all, if requested by $return.
120
+   *
121
+   * @param bool $return Define if the options tabs should be returned.
122
+   * @return array (If requested) An array of the outputted options tabs.
123
+   */
124
+  private function _options_page_tabs( $return = false ) {
125
+    // The array defining all options sections to be shown as tabs.
126
+    $tabs = array();
127
+    if ( $this->is_pod_set_up() ) {
128
+      $tabs['defaults'] = __( 'Defaults', 'wp-to-diaspora' );
129
+    }
130
+
131
+    // Add the 'Setup' tab to the end of the list.
132
+    $tabs['setup'] = __( 'Setup', 'wp-to-diaspora' ) . '<span id="pod-connection-status" class="dashicons-before hidden"></span><span class="spinner"></span>';
133
+
134
+    // Container for all options tabs.
135
+    $out = '<h2 id="options-tabs" class="nav-tab-wrapper">';
136
+    foreach ( $tabs as $tab => $name ) {
137
+      // The tab link.
138
+      $out .= '<a class="nav-tab' . ( ( $tab === $this->_current_tab() ) ? ' nav-tab-active' : '' ) . '" href="?page=wp_to_diaspora&tab=' . $tab . '">' . $name . '</a>';
139
+    }
140
+    $out .= '</h2>';
141
+
142
+    // Output the container with all tabs.
143
+    echo $out;
144
+
145
+    // Check if the tabs should be returned.
146
+    if ( $return ) {
147
+      return $tabs;
148
+    }
149
+  }
150
+
151
+
152
+  /**
153
+   * Set up admin options page.
154
+   */
155
+  public function admin_options_page() {
156
+    ?>
157 157
 		<div class="wrap">
158 158
 			<h2>WP to diaspora*</h2>
159 159
 
160 160
 			<div id="wp2d-message" class="notice hidden" <?php echo ( defined( 'WP2D_DEBUGGING' ) ) ? ' data-debugging' : ''; ?>></div>
161 161
 
162 162
 			<?php
163
-			// Check the connection status to diaspora.
164
-			if ( ! $this->is_pod_set_up() ) {
165
-				add_settings_error(
166
-					'wp_to_diaspora_settings',
167
-					'wp_to_diaspora_connected',
168
-					__( 'First of all, set up the connection to your pod below.', 'wp-to-diaspora' ),
169
-					'updated'
170
-				);
171
-			} else {
172
-				// Get initial aspects list and connected services.
173
-				// DON'T check for empty services list here!!
174
-				// It could always be empty, resulting in this code being run every time the page is loaded.
175
-				// The aspects will at least have a "Public" entry after the initial fetch.
176
-				$aspects_list = $this->get_option( 'aspects_list' );
177
-				if ( ( $force = get_transient( 'wp2d_no_js_force_refetch' ) ) || empty( $aspects_list ) ) {
178
-
179
-					// Set up the connection to diaspora*.
180
-					$api = WP2D_Helpers::api_quick_connect();
181
-					if ( ! $api->has_last_error() ) {
182
-						// Get the loaded aspects.
183
-						if ( is_array( $aspects = $api->get_aspects() ) ) {
184
-							// Save the new list of aspects.
185
-							$this->set_option( 'aspects_list', $aspects );
186
-						}
187
-
188
-						// Get the loaded services.
189
-						if ( is_array( $services = $api->get_services() ) ) {
190
-							// Save the new list of services.
191
-							$this->set_option( 'services_list', $services );
192
-						}
193
-
194
-						$this->save();
195
-					}
196
-
197
-					if ( $force ) {
198
-						delete_transient( 'wp2d_no_js_force_refetch' );
199
-						$message = ( ! $api->has_last_error() ) ? __( 'Connection successful.', 'wp-to-diaspora' ) : $api->get_last_error();
200
-						add_settings_error(
201
-							'wp_to_diaspora_settings',
202
-							'wp_to_diaspora_connected',
203
-							$message,
204
-							( ! $api->has_last_error() ) ? 'updated' : 'error'
205
-						);
206
-					}
207
-				}
208
-
209
-				// Attempt to get the cacert.pem file and save it to the plugin's root directory.
210
-				if ( isset( $_GET['wp2d_temp_ssl_fix'] ) ) {
211
-					$cacert_file = file_get_contents( 'http://curl.haxx.se/ca/cacert.pem' );
212
-					if ( $cacert_file && file_put_contents( WP2D_DIR . '/cacert.pem', $cacert_file ) ) {
213
-						add_settings_error(
214
-							'wp_to_diaspora_settings',
215
-							'wp_to_diaspora_temp_ssl_fix',
216
-							__( 'Successfully saved cacert.pem!', 'wp-to-diaspora' ),
217
-							'updated'
218
-						);
219
-					} else {
220
-						add_settings_error(
221
-							'wp_to_diaspora_settings',
222
-							'wp_to_diaspora_temp_ssl_fix',
223
-							__( 'Failed to save cacert.pem!', 'wp-to-diaspora' ),
224
-							'error'
225
-						);
226
-					}
227
-				}
228
-			}
229
-
230
-			// Output success or error message.
231
-			settings_errors( 'wp_to_diaspora_settings' );
232
-			?>
163
+      // Check the connection status to diaspora.
164
+      if ( ! $this->is_pod_set_up() ) {
165
+        add_settings_error(
166
+          'wp_to_diaspora_settings',
167
+          'wp_to_diaspora_connected',
168
+          __( 'First of all, set up the connection to your pod below.', 'wp-to-diaspora' ),
169
+          'updated'
170
+        );
171
+      } else {
172
+        // Get initial aspects list and connected services.
173
+        // DON'T check for empty services list here!!
174
+        // It could always be empty, resulting in this code being run every time the page is loaded.
175
+        // The aspects will at least have a "Public" entry after the initial fetch.
176
+        $aspects_list = $this->get_option( 'aspects_list' );
177
+        if ( ( $force = get_transient( 'wp2d_no_js_force_refetch' ) ) || empty( $aspects_list ) ) {
178
+
179
+          // Set up the connection to diaspora*.
180
+          $api = WP2D_Helpers::api_quick_connect();
181
+          if ( ! $api->has_last_error() ) {
182
+            // Get the loaded aspects.
183
+            if ( is_array( $aspects = $api->get_aspects() ) ) {
184
+              // Save the new list of aspects.
185
+              $this->set_option( 'aspects_list', $aspects );
186
+            }
187
+
188
+            // Get the loaded services.
189
+            if ( is_array( $services = $api->get_services() ) ) {
190
+              // Save the new list of services.
191
+              $this->set_option( 'services_list', $services );
192
+            }
193
+
194
+            $this->save();
195
+          }
196
+
197
+          if ( $force ) {
198
+            delete_transient( 'wp2d_no_js_force_refetch' );
199
+            $message = ( ! $api->has_last_error() ) ? __( 'Connection successful.', 'wp-to-diaspora' ) : $api->get_last_error();
200
+            add_settings_error(
201
+              'wp_to_diaspora_settings',
202
+              'wp_to_diaspora_connected',
203
+              $message,
204
+              ( ! $api->has_last_error() ) ? 'updated' : 'error'
205
+            );
206
+          }
207
+        }
208
+
209
+        // Attempt to get the cacert.pem file and save it to the plugin's root directory.
210
+        if ( isset( $_GET['wp2d_temp_ssl_fix'] ) ) {
211
+          $cacert_file = file_get_contents( 'http://curl.haxx.se/ca/cacert.pem' );
212
+          if ( $cacert_file && file_put_contents( WP2D_DIR . '/cacert.pem', $cacert_file ) ) {
213
+            add_settings_error(
214
+              'wp_to_diaspora_settings',
215
+              'wp_to_diaspora_temp_ssl_fix',
216
+              __( 'Successfully saved cacert.pem!', 'wp-to-diaspora' ),
217
+              'updated'
218
+            );
219
+          } else {
220
+            add_settings_error(
221
+              'wp_to_diaspora_settings',
222
+              'wp_to_diaspora_temp_ssl_fix',
223
+              __( 'Failed to save cacert.pem!', 'wp-to-diaspora' ),
224
+              'error'
225
+            );
226
+          }
227
+        }
228
+      }
229
+
230
+      // Output success or error message.
231
+      settings_errors( 'wp_to_diaspora_settings' );
232
+      ?>
233 233
 
234 234
 			<?php $page_tabs = array_keys( $this->_options_page_tabs( true ) ); ?>
235 235
 
236 236
 			<form action="options.php" method="post">
237 237
 				<input id="wp2d_no_js" type="hidden" name="wp_to_diaspora_settings[no_js]" value="1">
238 238
 				<?php
239
-				// Load the settings fields.
240
-				settings_fields( 'wp_to_diaspora_settings' );
241
-				do_settings_sections( 'wp_to_diaspora_settings' );
239
+        // Load the settings fields.
240
+        settings_fields( 'wp_to_diaspora_settings' );
241
+        do_settings_sections( 'wp_to_diaspora_settings' );
242 242
 
243
-				// Get the name of the current tab, if set, else take the first one from the list.
244
-				$tab = $this->_current_tab( $page_tabs[0] );
243
+        // Get the name of the current tab, if set, else take the first one from the list.
244
+        $tab = $this->_current_tab( $page_tabs[0] );
245 245
 
246
-				// Add Save and Reset buttons.
247
-				echo '<input id="submit-' . esc_attr( $tab ) . '" name="wp_to_diaspora_settings[submit_' . esc_attr( $tab ) . ']" type="submit" class="button-primary" value="' . esc_attr__( 'Save Changes' ) . '" />&nbsp;';
248
-				if ( 'setup' !== $tab ) {
249
-					echo '<input id="reset-' . esc_attr( $tab ) . '" name="wp_to_diaspora_settings[reset_' . esc_attr( $tab ) . ']" type="submit" class="button-secondary" value="' . esc_attr__( 'Reset Defaults', 'wp-to-diaspora' ) . '" />';
250
-				}
251
-				?>
246
+        // Add Save and Reset buttons.
247
+        echo '<input id="submit-' . esc_attr( $tab ) . '" name="wp_to_diaspora_settings[submit_' . esc_attr( $tab ) . ']" type="submit" class="button-primary" value="' . esc_attr__( 'Save Changes' ) . '" />&nbsp;';
248
+        if ( 'setup' !== $tab ) {
249
+          echo '<input id="reset-' . esc_attr( $tab ) . '" name="wp_to_diaspora_settings[reset_' . esc_attr( $tab ) . ']" type="submit" class="button-secondary" value="' . esc_attr__( 'Reset Defaults', 'wp-to-diaspora' ) . '" />';
250
+        }
251
+        ?>
252 252
 
253 253
 			</form>
254 254
 		</div>
255 255
 
256 256
 		<?php
257
-	}
258
-
259
-	/**
260
-	 * Return if the settings for the pod setup have been entered.
261
-	 *
262
-	 * @return boolean If the setup for the pod has been done.
263
-	 */
264
-	public function is_pod_set_up() {
265
-		return ( $this->get_option( 'pod' ) && $this->get_option( 'username' ) && $this->get_option( 'password' ) );
266
-	}
267
-
268
-	/**
269
-	 * Setup Contextual Help and Options pages.
270
-	 */
271
-	public function setup_wpadmin_pages() {
272
-		// Add options page.
273
-		$hook = add_options_page( 'WP to diaspora*', 'WP to diaspora*', 'manage_options', 'wp_to_diaspora', array( $this, 'admin_options_page' ) );
274
-
275
-		// Setup the contextual help menu after the options page has been loaded.
276
-		add_action( 'load-' . $hook, array( 'WP2D_Contextual_Help', 'instance' ) );
277
-
278
-		// Setup the contextual help menu tab for post types. Checks are made there!
279
-		add_action( 'load-post.php', array( 'WP2D_Contextual_Help', 'instance' ) );
280
-		add_action( 'load-post-new.php', array( 'WP2D_Contextual_Help', 'instance' ) );
281
-	}
282
-
283
-	/**
284
-	 * Initialise the settings sections and fields of the currently selected tab.
285
-	 */
286
-	public function register_settings() {
287
-		// Register the settings with validation callback.
288
-		register_setting( 'wp_to_diaspora_settings', 'wp_to_diaspora_settings', array( $this, 'validate_settings' ) );
289
-
290
-		// Load only the sections of the selected tab.
291
-		switch ( $this->_current_tab() ) {
292
-			case 'defaults' :
293
-				// Add a "Defaults" section that contains all posting settings to be used by default.
294
-				add_settings_section( 'wp_to_diaspora_defaults_section', __( 'Posting Defaults', 'wp-to-diaspora' ), array( $this, 'defaults_section' ), 'wp_to_diaspora_settings' );
295
-				break;
296
-			case 'setup' :
297
-				// Add a "Setup" section that contains the Pod domain, Username and Password.
298
-				add_settings_section( 'wp_to_diaspora_setup_section', __( 'diaspora* Setup', 'wp-to-diaspora' ), array( $this, 'setup_section' ), 'wp_to_diaspora_settings' );
299
-				break;
300
-		}
301
-	}
302
-
303
-
304
-	/**
305
-	 * Callback for the "Setup" section.
306
-	 */
307
-	public function setup_section() {
308
-		esc_html_e( 'Set up the connection to your diaspora* account.', 'wp-to-diaspora' );
309
-
310
-		// Pod entry field.
311
-		add_settings_field( 'pod', __( 'Diaspora* Pod', 'wp-to-diaspora' ), array( $this, 'pod_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_setup_section' );
312
-
313
-		// Username entry field.
314
-		add_settings_field( 'username', __( 'Username' ), array( $this, 'username_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_setup_section' );
315
-
316
-		// Password entry field.
317
-		add_settings_field( 'password', __( 'Password' ), array( $this, 'password_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_setup_section' );
318
-	}
319
-
320
-	/**
321
-	 * Render the "Pod" field.
322
-	 */
323
-	public function pod_render() {
324
-		?>
257
+  }
258
+
259
+  /**
260
+   * Return if the settings for the pod setup have been entered.
261
+   *
262
+   * @return boolean If the setup for the pod has been done.
263
+   */
264
+  public function is_pod_set_up() {
265
+    return ( $this->get_option( 'pod' ) && $this->get_option( 'username' ) && $this->get_option( 'password' ) );
266
+  }
267
+
268
+  /**
269
+   * Setup Contextual Help and Options pages.
270
+   */
271
+  public function setup_wpadmin_pages() {
272
+    // Add options page.
273
+    $hook = add_options_page( 'WP to diaspora*', 'WP to diaspora*', 'manage_options', 'wp_to_diaspora', array( $this, 'admin_options_page' ) );
274
+
275
+    // Setup the contextual help menu after the options page has been loaded.
276
+    add_action( 'load-' . $hook, array( 'WP2D_Contextual_Help', 'instance' ) );
277
+
278
+    // Setup the contextual help menu tab for post types. Checks are made there!
279
+    add_action( 'load-post.php', array( 'WP2D_Contextual_Help', 'instance' ) );
280
+    add_action( 'load-post-new.php', array( 'WP2D_Contextual_Help', 'instance' ) );
281
+  }
282
+
283
+  /**
284
+   * Initialise the settings sections and fields of the currently selected tab.
285
+   */
286
+  public function register_settings() {
287
+    // Register the settings with validation callback.
288
+    register_setting( 'wp_to_diaspora_settings', 'wp_to_diaspora_settings', array( $this, 'validate_settings' ) );
289
+
290
+    // Load only the sections of the selected tab.
291
+    switch ( $this->_current_tab() ) {
292
+      case 'defaults' :
293
+        // Add a "Defaults" section that contains all posting settings to be used by default.
294
+        add_settings_section( 'wp_to_diaspora_defaults_section', __( 'Posting Defaults', 'wp-to-diaspora' ), array( $this, 'defaults_section' ), 'wp_to_diaspora_settings' );
295
+        break;
296
+      case 'setup' :
297
+        // Add a "Setup" section that contains the Pod domain, Username and Password.
298
+        add_settings_section( 'wp_to_diaspora_setup_section', __( 'diaspora* Setup', 'wp-to-diaspora' ), array( $this, 'setup_section' ), 'wp_to_diaspora_settings' );
299
+        break;
300
+    }
301
+  }
302
+
303
+
304
+  /**
305
+   * Callback for the "Setup" section.
306
+   */
307
+  public function setup_section() {
308
+    esc_html_e( 'Set up the connection to your diaspora* account.', 'wp-to-diaspora' );
309
+
310
+    // Pod entry field.
311
+    add_settings_field( 'pod', __( 'Diaspora* Pod', 'wp-to-diaspora' ), array( $this, 'pod_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_setup_section' );
312
+
313
+    // Username entry field.
314
+    add_settings_field( 'username', __( 'Username' ), array( $this, 'username_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_setup_section' );
315
+
316
+    // Password entry field.
317
+    add_settings_field( 'password', __( 'Password' ), array( $this, 'password_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_setup_section' );
318
+  }
319
+
320
+  /**
321
+   * Render the "Pod" field.
322
+   */
323
+  public function pod_render() {
324
+    ?>
325 325
 		https://<input type="text" name="wp_to_diaspora_settings[pod]" value="<?php echo esc_attr( $this->get_option( 'pod' ) ); ?>" placeholder="e.g. joindiaspora.com" autocomplete="on" list="pod-list" required> <a id="refresh-pod-list" class="button hide-if-no-js"><?php esc_html_e( 'Refresh pod list', 'wp-to-diaspora' ); ?></a><span class="spinner"></span>
326 326
 		<datalist id="pod-list">
327 327
 		<?php foreach ( (array) $this->get_option( 'pod_list' ) as $pod ) : ?>
@@ -329,76 +329,76 @@  discard block
 block discarded – undo
329 329
 		<?php endforeach; ?>
330 330
 		</datalist>
331 331
 		<?php
332
-	}
332
+  }
333 333
 
334
-	/**
335
-	 * Render the "Username" field.
336
-	 */
337
-	public function username_render() {
338
-		?>
334
+  /**
335
+   * Render the "Username" field.
336
+   */
337
+  public function username_render() {
338
+    ?>
339 339
 		<input type="text" name="wp_to_diaspora_settings[username]" value="<?php echo esc_attr( $this->get_option( 'username' ) ); ?>" placeholder="<?php esc_attr_e( 'Username' ); ?>" required>
340 340
 		<?php
341
-	}
342
-
343
-	/**
344
-	 * Render the "Password" field.
345
-	 */
346
-	public function password_render() {
347
-		// Special case if we already have a password.
348
-		$has_password = ( '' !== $this->get_option( 'password', '' ) );
349
-		$placeholder  = ( $has_password ) ? __( 'Password already set.', 'wp-to-diaspora' ) : __( 'Password' );
350
-		$required     = ( $has_password ) ? '' : ' required';
351
-		?>
341
+  }
342
+
343
+  /**
344
+   * Render the "Password" field.
345
+   */
346
+  public function password_render() {
347
+    // Special case if we already have a password.
348
+    $has_password = ( '' !== $this->get_option( 'password', '' ) );
349
+    $placeholder  = ( $has_password ) ? __( 'Password already set.', 'wp-to-diaspora' ) : __( 'Password' );
350
+    $required     = ( $has_password ) ? '' : ' required';
351
+    ?>
352 352
 		<input type="password" name="wp_to_diaspora_settings[password]" value="" placeholder="<?php echo esc_attr( $placeholder ); ?>"<?php echo esc_attr( $required ); ?>>
353 353
 		<?php if ( $has_password ) : ?>
354 354
 			<p class="description"><?php esc_html_e( 'If you would like to change the password type a new one. Otherwise leave this blank.', 'wp-to-diaspora' ); ?></p>
355 355
 		<?php endif;
356
-	}
356
+  }
357 357
 
358 358
 
359
-	/**
360
-	 * Callback for the "Defaults" section.
361
-	 */
362
-	public function defaults_section() {
363
-		esc_html_e( 'Define the default posting behaviour for all posts here. These settings can be modified for each post individually, by changing the values in the "WP to diaspora*" meta box, which gets displayed in your post edit screen.', 'wp-to-diaspora' );
359
+  /**
360
+   * Callback for the "Defaults" section.
361
+   */
362
+  public function defaults_section() {
363
+    esc_html_e( 'Define the default posting behaviour for all posts here. These settings can be modified for each post individually, by changing the values in the "WP to diaspora*" meta box, which gets displayed in your post edit screen.', 'wp-to-diaspora' );
364 364
 
365
-		// Post types field.
366
-		add_settings_field( 'enabled_post_types', __( 'Post types', 'wp-to-diaspora' ), array( $this, 'post_types_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_defaults_section' );
365
+    // Post types field.
366
+    add_settings_field( 'enabled_post_types', __( 'Post types', 'wp-to-diaspora' ), array( $this, 'post_types_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_defaults_section' );
367 367
 
368
-		 // Post to diaspora* checkbox.
369
-		add_settings_field( 'post_to_diaspora', __( 'Post to diaspora*', 'wp-to-diaspora' ), array( $this, 'post_to_diaspora_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_defaults_section', $this->get_option( 'post_to_diaspora' ) );
368
+      // Post to diaspora* checkbox.
369
+    add_settings_field( 'post_to_diaspora', __( 'Post to diaspora*', 'wp-to-diaspora' ), array( $this, 'post_to_diaspora_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_defaults_section', $this->get_option( 'post_to_diaspora' ) );
370 370
 
371
-		// Full entry link checkbox.
372
-		add_settings_field( 'fullentrylink', __( 'Show "Posted at" link?', 'wp-to-diaspora' ), array( $this, 'fullentrylink_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_defaults_section', $this->get_option( 'fullentrylink' ) );
371
+    // Full entry link checkbox.
372
+    add_settings_field( 'fullentrylink', __( 'Show "Posted at" link?', 'wp-to-diaspora' ), array( $this, 'fullentrylink_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_defaults_section', $this->get_option( 'fullentrylink' ) );
373 373
 
374
-		// Full text or excerpt radio buttons.
375
-		add_settings_field( 'display', __( 'Display', 'wp-to-diaspora' ), array( $this, 'display_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_defaults_section', $this->get_option( 'display' ) );
374
+    // Full text or excerpt radio buttons.
375
+    add_settings_field( 'display', __( 'Display', 'wp-to-diaspora' ), array( $this, 'display_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_defaults_section', $this->get_option( 'display' ) );
376 376
 
377
-		// Tags to post dropdown.
378
-		add_settings_field( 'tags_to_post', __( 'Tags to post', 'wp-to-diaspora' ), array( $this, 'tags_to_post_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_defaults_section', $this->get_option( 'tags_to_post', 'gc' ) );
377
+    // Tags to post dropdown.
378
+    add_settings_field( 'tags_to_post', __( 'Tags to post', 'wp-to-diaspora' ), array( $this, 'tags_to_post_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_defaults_section', $this->get_option( 'tags_to_post', 'gc' ) );
379 379
 
380
-		// Global tags field.
381
-		add_settings_field( 'global_tags', __( 'Global tags', 'wp-to-diaspora' ), array( $this, 'global_tags_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_defaults_section', $this->get_option( 'global_tags' ) );
380
+    // Global tags field.
381
+    add_settings_field( 'global_tags', __( 'Global tags', 'wp-to-diaspora' ), array( $this, 'global_tags_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_defaults_section', $this->get_option( 'global_tags' ) );
382 382
 
383
-		// Aspects checkboxes.
384
-		add_settings_field( 'aspects', __( 'Aspects', 'wp-to-diaspora' ), array( $this, 'aspects_services_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_defaults_section', array( 'aspects', $this->get_option( 'aspects' ) ) );
383
+    // Aspects checkboxes.
384
+    add_settings_field( 'aspects', __( 'Aspects', 'wp-to-diaspora' ), array( $this, 'aspects_services_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_defaults_section', array( 'aspects', $this->get_option( 'aspects' ) ) );
385 385
 
386
-		// Services checkboxes.
387
-		add_settings_field( 'services', __( 'Services', 'wp-to-diaspora' ), array( $this, 'aspects_services_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_defaults_section', array( 'services', $this->get_option( 'services' ) ) );
388
-	}
386
+    // Services checkboxes.
387
+    add_settings_field( 'services', __( 'Services', 'wp-to-diaspora' ), array( $this, 'aspects_services_render' ), 'wp_to_diaspora_settings', 'wp_to_diaspora_defaults_section', array( 'services', $this->get_option( 'services' ) ) );
388
+  }
389 389
 
390
-	/**
391
-	 * Render the "Post types" checkboxes.
392
-	 */
393
-	public function post_types_render() {
394
-		$post_types = get_post_types( array( 'public' => true ), 'objects' );
390
+  /**
391
+   * Render the "Post types" checkboxes.
392
+   */
393
+  public function post_types_render() {
394
+    $post_types = get_post_types( array( 'public' => true ), 'objects' );
395 395
 
396
-		// Remove excluded post types from the list.
397
-		$excluded_post_types = array( 'attachment', 'nav_menu_item', 'revision' );
398
-		foreach ( $excluded_post_types as $excluded ) {
399
-			unset( $post_types[ $excluded ] );
400
-		}
401
-		?>
396
+    // Remove excluded post types from the list.
397
+    $excluded_post_types = array( 'attachment', 'nav_menu_item', 'revision' );
398
+    foreach ( $excluded_post_types as $excluded ) {
399
+      unset( $post_types[ $excluded ] );
400
+    }
401
+    ?>
402 402
 
403 403
 		<select id="enabled-post-types" multiple data-placeholder="<?php esc_attr_e( 'None', 'wp-to-diaspora' ); ?>" class="chosen" name="wp_to_diaspora_settings[enabled_post_types][]">
404 404
 		<?php foreach ( $post_types as $post_type ) : ?>
@@ -409,63 +409,63 @@  discard block
 block discarded – undo
409 409
 		<p class="description"><?php esc_html_e( 'Choose which post types can be posted to diaspora*.', 'wp-to-diaspora' ); ?></p>
410 410
 
411 411
 		<?php
412
-	}
413
-
414
-	/**
415
-	 * Render the "Post to diaspora*" checkbox.
416
-	 *
417
-	 * @param bool $post_to_diaspora If this checkbox is checked or not.
418
-	 */
419
-	public function post_to_diaspora_render( $post_to_diaspora ) {
420
-		$label = ( 'settings_page_wp_to_diaspora' === get_current_screen()->id ) ? __( 'Yes' ) : __( 'Post to diaspora*', 'wp-to-diaspora' );
421
-		?>
412
+  }
413
+
414
+  /**
415
+   * Render the "Post to diaspora*" checkbox.
416
+   *
417
+   * @param bool $post_to_diaspora If this checkbox is checked or not.
418
+   */
419
+  public function post_to_diaspora_render( $post_to_diaspora ) {
420
+    $label = ( 'settings_page_wp_to_diaspora' === get_current_screen()->id ) ? __( 'Yes' ) : __( 'Post to diaspora*', 'wp-to-diaspora' );
421
+    ?>
422 422
 		<label><input type="checkbox" id="post-to-diaspora" name="wp_to_diaspora_settings[post_to_diaspora]" value="1" <?php checked( $post_to_diaspora ); ?>><?php echo esc_html( $label ); ?></label>
423 423
 		<?php
424
-	}
425
-
426
-	/**
427
-	 * Render the "Show 'Posted at' link" checkbox.
428
-	 *
429
-	 * @param bool $show_link If the checkbox is checked or not.
430
-	 */
431
-	public function fullentrylink_render( $show_link ) {
432
-		$description = __( 'Include a link back to your original post.', 'wp-to-diaspora' );
433
-		$checkbox = '<input type="checkbox" id="fullentrylink" name="wp_to_diaspora_settings[fullentrylink]" value="1"' . checked( $show_link, true, false ) . '>';
434
-
435
-		if ( 'settings_page_wp_to_diaspora' === get_current_screen()->id ) : ?>
424
+  }
425
+
426
+  /**
427
+   * Render the "Show 'Posted at' link" checkbox.
428
+   *
429
+   * @param bool $show_link If the checkbox is checked or not.
430
+   */
431
+  public function fullentrylink_render( $show_link ) {
432
+    $description = __( 'Include a link back to your original post.', 'wp-to-diaspora' );
433
+    $checkbox = '<input type="checkbox" id="fullentrylink" name="wp_to_diaspora_settings[fullentrylink]" value="1"' . checked( $show_link, true, false ) . '>';
434
+
435
+    if ( 'settings_page_wp_to_diaspora' === get_current_screen()->id ) : ?>
436 436
 			<label><?php echo $checkbox; ?><?php esc_html_e( 'Yes' ); ?></label>
437 437
 			<p class="description"><?php echo esc_html( $description ); ?></p>
438 438
 		<?php else : ?>
439 439
 			<label title="<?php echo esc_attr( $description ); ?>"><?php echo $checkbox; ?><?php esc_html_e( 'Show "Posted at" link?', 'wp-to-diaspora' ); ?></label>
440 440
 		<?php endif;
441
-	}
442
-
443
-	/**
444
-	 * Render the "Display" radio buttons.
445
-	 *
446
-	 * @param string $display The selected radio button.
447
-	 */
448
-	public function display_render( $display ) {
449
-		?>
441
+  }
442
+
443
+  /**
444
+   * Render the "Display" radio buttons.
445
+   *
446
+   * @param string $display The selected radio button.
447
+   */
448
+  public function display_render( $display ) {
449
+    ?>
450 450
 		<label><input type="radio" name="wp_to_diaspora_settings[display]" value="full" <?php checked( $display, 'full' ); ?>><?php esc_html_e( 'Full Post', 'wp-to-diaspora' ); ?></label><br />
451 451
 		<label><input type="radio" name="wp_to_diaspora_settings[display]" value="excerpt" <?php checked( $display, 'excerpt' ); ?>><?php esc_html_e( 'Excerpt' ); ?></label>
452 452
 		<?php
453
-	}
454
-
455
-	/**
456
-	 * Render the "Tags to post" field.
457
-	 *
458
-	 * @param array $tags_to_post The types of tags to be posted.
459
-	 */
460
-	public function tags_to_post_render( $tags_to_post ) {
461
-		$on_settings_page = ( 'settings_page_wp_to_diaspora' === get_current_screen()->id );
462
-		$description = esc_html__( 'Choose which tags should be posted to diaspora*.', 'wp-to-diaspora' );
463
-
464
-		if ( ! $on_settings_page ) {
465
-			echo '<label>' . esc_html( $description );
466
-		}
467
-
468
-		?>
453
+  }
454
+
455
+  /**
456
+   * Render the "Tags to post" field.
457
+   *
458
+   * @param array $tags_to_post The types of tags to be posted.
459
+   */
460
+  public function tags_to_post_render( $tags_to_post ) {
461
+    $on_settings_page = ( 'settings_page_wp_to_diaspora' === get_current_screen()->id );
462
+    $description = esc_html__( 'Choose which tags should be posted to diaspora*.', 'wp-to-diaspora' );
463
+
464
+    if ( ! $on_settings_page ) {
465
+      echo '<label>' . esc_html( $description );
466
+    }
467
+
468
+    ?>
469 469
 		<select id="tags-to-post" multiple data-placeholder="<?php esc_attr_e( 'No tags', 'wp-to-diaspora' ); ?>" class="chosen" name="wp_to_diaspora_settings[tags_to_post][]">
470 470
 			<option value="global" <?php selected( in_array( 'global', $tags_to_post ) ); ?>><?php esc_html_e( 'Global tags', 'wp-to-diaspora' ); ?></option>
471 471
 			<option value="custom" <?php selected( in_array( 'custom', $tags_to_post ) ); ?>><?php esc_html_e( 'Custom tags', 'wp-to-diaspora' ); ?></option>
@@ -477,82 +477,82 @@  discard block
 block discarded – undo
477 477
 		<?php else : ?>
478 478
 			</label>
479 479
 		<?php endif;
480
-	}
481
-
482
-	/**
483
-	 * Render the "Global tags" field.
484
-	 *
485
-	 * @param array $tags The global tags to be posted.
486
-	 */
487
-	public function global_tags_render( $tags ) {
488
-		WP2D_Helpers::arr_to_str( $tags );
489
-		?>
480
+  }
481
+
482
+  /**
483
+   * Render the "Global tags" field.
484
+   *
485
+   * @param array $tags The global tags to be posted.
486
+   */
487
+  public function global_tags_render( $tags ) {
488
+    WP2D_Helpers::arr_to_str( $tags );
489
+    ?>
490 490
 		<input type="text" class="wp2dtags" name="wp_to_diaspora_settings[global_tags]" value="<?php echo esc_attr( $tags ); ?>" placeholder="<?php esc_attr_e( 'Global tags', 'wp-to-diaspora' ); ?>" class="regular-text">
491 491
 		<p class="description"><?php esc_html_e( 'Custom tags to add to all posts being posted to diaspora*.', 'wp-to-diaspora' ); ?></p>
492 492
 		<?php
493
-	}
494
-
495
-	/**
496
-	 * Render the "Custom tags" field.
497
-	 *
498
-	 * @param array $tags The custom tags to be posted.
499
-	 */
500
-	public function custom_tags_render( $tags ) {
501
-		WP2D_Helpers::arr_to_str( $tags );
502
-		?>
493
+  }
494
+
495
+  /**
496
+   * Render the "Custom tags" field.
497
+   *
498
+   * @param array $tags The custom tags to be posted.
499
+   */
500
+  public function custom_tags_render( $tags ) {
501
+    WP2D_Helpers::arr_to_str( $tags );
502
+    ?>
503 503
 		<label title="<?php esc_attr_e( 'Custom tags to add to this post when it\'s posted to diaspora*.', 'wp-to-diaspora' ); ?>">
504 504
 			<?php esc_html_e( 'Custom tags', 'wp-to-diaspora' ); ?>
505 505
 			<input type="text" class="wp2dtags" name="wp_to_diaspora_settings[custom_tags]" value="<?php echo esc_attr( $tags ); ?>" class="widefat">
506 506
 		</label>
507 507
 		<p class="description"><?php esc_html_e( 'Separate tags with commas' ); ?></p>
508 508
 		<?php
509
-	}
510
-
511
-	/**
512
-	 * Render the "Aspects" and "Services" checkboxes.
513
-	 *
514
-	 * @param array $args Array containing the type and items to output as checkboxes.
515
-	 */
516
-	public function aspects_services_render( $args ) {
517
-		list( $type, $items ) = $args;
518
-
519
-		$refresh_button = '';
520
-		$description    = '';
521
-		$empty_label    = '';
522
-
523
-		// This is where the 2 types show their differences.
524
-		switch ( $type ) {
525
-			case 'aspects':
526
-				$refresh_button = __( 'Refresh Aspects', 'wp-to-diaspora' );
527
-				$description    = esc_html__( 'Choose which aspects to share to.', 'wp-to-diaspora' );
528
-				$empty_label    = '<input type="checkbox" name="wp_to_diaspora_settings[aspects][]" value="public" checked="checked">' . esc_html__( 'Public' );
529
-				break;
530
-
531
-			case 'services':
532
-				$refresh_button = __( 'Refresh Services', 'wp-to-diaspora' );
533
-				$description    = sprintf( '%1$s<br><a href="%2$s" target="_blank">%3$s</a>',
534
-					esc_html__( 'Choose which services to share to.', 'wp-to-diaspora' ),
535
-					esc_url( 'https://' . $this->get_option( 'pod' ) . '/services' ),
536
-					esc_html__( 'Show available services on my pod.', 'wp-to-diaspora' )
537
-				);
538
-				$empty_label    = esc_html__( 'No services connected yet.', 'wp-to-diaspora' );
539
-				break;
540
-
541
-			default:
542
-				return;
543
-		}
544
-
545
-		$items = array_filter( (array) $items ) ?: array();
546
-
547
-		// Special case for this field if it's displayed on the settings page.
548
-		$on_settings_page = ( 'settings_page_wp_to_diaspora' === get_current_screen()->id );
549
-
550
-		if ( ! $on_settings_page ) {
551
-			echo $description;
552
-			$description = '';
553
-		}
554
-
555
-		?>
509
+  }
510
+
511
+  /**
512
+   * Render the "Aspects" and "Services" checkboxes.
513
+   *
514
+   * @param array $args Array containing the type and items to output as checkboxes.
515
+   */
516
+  public function aspects_services_render( $args ) {
517
+    list( $type, $items ) = $args;
518
+
519
+    $refresh_button = '';
520
+    $description    = '';
521
+    $empty_label    = '';
522
+
523
+    // This is where the 2 types show their differences.
524
+    switch ( $type ) {
525
+      case 'aspects':
526
+        $refresh_button = __( 'Refresh Aspects', 'wp-to-diaspora' );
527
+        $description    = esc_html__( 'Choose which aspects to share to.', 'wp-to-diaspora' );
528
+        $empty_label    = '<input type="checkbox" name="wp_to_diaspora_settings[aspects][]" value="public" checked="checked">' . esc_html__( 'Public' );
529
+        break;
530
+
531
+      case 'services':
532
+        $refresh_button = __( 'Refresh Services', 'wp-to-diaspora' );
533
+        $description    = sprintf( '%1$s<br><a href="%2$s" target="_blank">%3$s</a>',
534
+          esc_html__( 'Choose which services to share to.', 'wp-to-diaspora' ),
535
+          esc_url( 'https://' . $this->get_option( 'pod' ) . '/services' ),
536
+          esc_html__( 'Show available services on my pod.', 'wp-to-diaspora' )
537
+        );
538
+        $empty_label    = esc_html__( 'No services connected yet.', 'wp-to-diaspora' );
539
+        break;
540
+
541
+      default:
542
+        return;
543
+    }
544
+
545
+    $items = array_filter( (array) $items ) ?: array();
546
+
547
+    // Special case for this field if it's displayed on the settings page.
548
+    $on_settings_page = ( 'settings_page_wp_to_diaspora' === get_current_screen()->id );
549
+
550
+    if ( ! $on_settings_page ) {
551
+      echo $description;
552
+      $description = '';
553
+    }
554
+
555
+    ?>
556 556
 		<div id="<?php echo esc_attr( $type ); ?>-container" data-<?php echo esc_attr( $type ); ?>-selected="<?php echo esc_attr( implode( ',', $items ) ); ?>">
557 557
 			<?php if ( $list = (array) $this->get_option( $type . '_list' ) ) : ?>
558 558
 				<?php foreach ( $list as $id => $name ) : ?>
@@ -569,267 +569,267 @@  discard block
 block discarded – undo
569 569
 			<span class="hide-if-js"><?php printf( esc_html_x( 'To update this list, %sre-save your login info%s.', 'placeholders are link tags to the settings page.', 'wp-to-diaspora' ), '<a href="' . admin_url( 'options-general.php?page=wp_to_diaspora' ) . '&amp;tab=setup" target="_blank">', '</a>' ); ?></span>
570 570
 		</p>
571 571
 		<?php
572
-	}
573
-
574
-
575
-	/**
576
-	 * Get a specific option.
577
-	 *
578
-	 * @param string       $option  ID of option to get.
579
-	 * @param array|string $default Override default value if option not found.
580
-	 * @return array|string Requested option value.
581
-	 */
582
-	public function get_option( $option = null, $default = null ) {
583
-		if ( ! isset( self::$_options ) ) {
584
-			self::$_options = get_option( 'wp_to_diaspora_settings', self::$_default_options );
585
-		}
586
-		if ( isset( $option ) ) {
587
-			if ( isset( self::$_options[ $option ] ) ) {
588
-				// Return found option value.
589
-				return self::$_options[ $option ];
590
-			} elseif ( isset( $default ) ) {
591
-				// Return overridden default value.
592
-				return $default;
593
-			} elseif ( isset( self::$_default_options[ $option ] ) ) {
594
-				// Return default option value.
595
-				return self::$_default_options[ $option ];
596
-			}
597
-		}
598
-	}
599
-
600
-	/**
601
-	 * Get all options.
602
-	 *
603
-	 * @return array All the options.
604
-	 */
605
-	public function get_options() {
606
-		return self::$_options;
607
-	}
608
-
609
-	/**
610
-	 * Set a certain option.
611
-	 *
612
-	 * @param string       $option ID of option to get.
613
-	 * @param array|string $value  Value to be set for the passed option.
614
-	 * @param boolean      $save   Save the options immediately after setting them.
615
-	 */
616
-	public function set_option( $option, $value, $save = false ) {
617
-		if ( isset( $option ) ) {
618
-			if ( isset( $value ) ) {
619
-				self::$_options[ $option ] = $value;
620
-			} else {
621
-				unset( self::$_options[ $option ] );
622
-			}
623
-		}
624
-		if ( $save ) {
625
-			self::save();
626
-		}
627
-	}
628
-
629
-	/**
630
-	 * Save the options.
631
-	 */
632
-	public function save() {
633
-		update_option( 'wp_to_diaspora_settings', self::$_options );
634
-	}
635
-
636
-	/**
637
-	 * Get all valid input values for the passed field.
638
-	 *
639
-	 * @param string $field Field to get the valid values for.
640
-	 * @return array List of valid values.
641
-	 */
642
-	public function get_valid_values( $field ) {
643
-		if ( array_key_exists( $field, self::$_valid_values ) ) {
644
-			return self::$_valid_values[ $field ];
645
-		}
646
-	}
647
-
648
-	/**
649
-	 * Check if a value is valid for the passed field.
650
-	 *
651
-	 * @param string $field Field to check the valid value for.
652
-	 * @param object $value Value to check validity.
653
-	 * @return boolean If the passed value is valid.
654
-	 */
655
-	public function is_valid_value( $field, $value ) {
656
-		if ( $valids = self::get_valid_values( $field ) ) {
657
-			return ( in_array( $value, $valids ) );
658
-		}
659
-		return false;
660
-	}
661
-
662
-	/**
663
-	 * Validate all settings.
664
-	 *
665
-	 * @param array $input RAW input values.
666
-	 * @return array Validated input values.
667
-	 */
668
-	public function validate_settings( $input ) {
669
-		/* Validate all settings before saving to the database. */
670
-
671
-		// Saving the pod setup details.
672
-		if ( isset( $input['submit_setup'] ) ) {
673
-			$input['pod']      = trim( sanitize_text_field( $input['pod'] ), ' /' );
674
-			$input['username'] = sanitize_text_field( $input['username'] );
675
-			$input['password'] = sanitize_text_field( $input['password'] );
676
-
677
-			// If password is blank, it hasn't been changed.
678
-			// If new password is equal to the encrypted password already saved, it was just passed again. It happens everytime update_option('wp_to_diaspora_settings') is called.
679
-			if ( '' === $input['password'] || $this->get_option( 'password' ) === $input['password'] ) {
680
-				$input['password'] = $this->get_option( 'password' );
681
-			} else {
682
-				$input['password'] = WP2D_Helpers::encrypt( $input['password'] );
683
-			}
684
-
685
-			// This is for when JS in not enabled, to make sure that the aspects and services
686
-			// are refetched when displaying the options page after saving.
687
-			if ( isset( $input['no_js'] ) ) {
688
-				set_transient( 'wp2d_no_js_force_refetch', true );
689
-			}
690
-		}
691
-
692
-		// Saving the default options.
693
-		if ( isset( $input['submit_defaults'] ) ) {
694
-			if ( ! isset( $input['enabled_post_types'] ) ) {
695
-				$input['enabled_post_types'] = array();
696
-			}
697
-
698
-			// Checkboxes.
699
-			$this->validate_checkboxes( array( 'post_to_diaspora', 'fullentrylink' ), $input );
700
-
701
-			// Single Selects.
702
-			$this->validate_single_selects( 'display', $input );
703
-
704
-			// Multiple Selects.
705
-			$this->validate_multi_selects( 'tags_to_post', $input );
706
-
707
-			// Get unique, non-empty, trimmed tags and clean them up.
708
-			$this->validate_tags( $input['global_tags'] );
709
-
710
-			// Clean up the list of aspects. If the list is empty, only use the 'Public' aspect.
711
-			$this->validate_aspects_services( $input['aspects'], array( 'public' ) );
712
-
713
-			// Clean up the list of services.
714
-			$this->validate_aspects_services( $input['services'] );
715
-		}
716
-
717
-		// Reset to defaults.
718
-		if ( isset( $input['reset_defaults'] ) ) {
719
-			// Set the input to the default options.
720
-			$input = self::$_default_options;
721
-
722
-			// Don't reset the fetched lists of pods, aspects and services.
723
-			unset( $input['pod_list'] );
724
-			unset( $input['aspects_list'] );
725
-			unset( $input['services_list'] );
726
-		}
727
-
728
-		// Unset all unused input fields.
729
-		unset( $input['submit_defaults'] );
730
-		unset( $input['reset_defaults'] );
731
-		unset( $input['submit_setup'] );
732
-
733
-		// Parse inputs with default options and return.
734
-		return wp_parse_args( $input, array_merge( self::$_default_options, self::$_options ) );
735
-	}
736
-
737
-	/**
738
-	 * Validate checkboxes, make them either true or false.
739
-	 *
740
-	 * @param string|array $checkboxes Checkboxes to validate.
741
-	 * @param array        $options    Options values themselves.
742
-	 * @return array The validated options.
743
-	 */
744
-	public function validate_checkboxes( $checkboxes, &$options ) {
745
-		foreach ( WP2D_Helpers::str_to_arr( $checkboxes ) as $checkbox ) {
746
-			$options[ $checkbox ] = isset( $options[ $checkbox ] );
747
-		}
748
-		return $options;
749
-	}
750
-
751
-	/**
752
-	 * Validate single-select fields and make sure their selected value are valid.
753
-	 *
754
-	 * @param string|array $selects Name(s) of the select fields.
755
-	 * @param array        $options Options values themselves.
756
-	 * @return array The validated options.
757
-	 */
758
-	public function validate_single_selects( $selects, &$options ) {
759
-		foreach ( WP2D_Helpers::str_to_arr( $selects ) as $select ) {
760
-			if ( isset( $options[ $select ] ) && ! $this->is_valid_value( $select, $options[ $select ] ) ) {
761
-				unset( $options[ $select ] );
762
-			}
763
-		}
764
-		return $options;
765
-	}
766
-
767
-	/**
768
-	 * Validate multi-select fields and make sure their selected values are valid.
769
-	 *
770
-	 * @param string|array $selects Name(s) of the select fields.
771
-	 * @param array        $options Options values themselves.
772
-	 * @return array The validated options.
773
-	 */
774
-	public function validate_multi_selects( $selects, &$options ) {
775
-		foreach ( WP2D_Helpers::str_to_arr( $selects ) as $select ) {
776
-			if ( isset( $options[ $select ] ) ) {
777
-				foreach ( (array) $options[ $select ] as $option_value ) {
778
-					if ( ! $this->is_valid_value( $select, $option_value ) ) {
779
-						unset( $options[ $select ] );
780
-						break;
781
-					}
782
-				}
783
-			} else {
784
-				$options[ $select ] = array();
785
-			}
786
-		}
787
-		return $options;
788
-	}
789
-
790
-	/**
791
-	 * Clean up the passed tags. Keep only alphanumeric, hyphen and underscore characters.
792
-	 *
793
-	 * @param array|string $tags Tags to be cleaned as array or comma seperated values.
794
-	 * @return array The cleaned tags.
795
-	 */
796
-	public function validate_tags( &$tags ) {
797
-		WP2D_Helpers::str_to_arr( $tags );
798
-
799
-		$tags = array_map( array( $this, 'validate_tag' ),
800
-			array_unique(
801
-				array_filter( $tags, 'trim' )
802
-			)
803
-		);
804
-		return $tags;
805
-	}
806
-
807
-	/**
808
-	 * Clean up the passed tag. Keep only alphanumeric, hyphen and underscore characters.
809
-	 *
810
-	 * @todo What about eastern characters? (chinese, indian, etc.)
811
-	 *
812
-	 * @param string $tag Tag to be cleaned.
813
-	 * @return string The clean tag.
814
-	 */
815
-	public function validate_tag( &$tag ) {
816
-		$tag = preg_replace( '/[^\w $\-]/u', '', str_replace( ' ', '-', trim( $tag ) ) );
817
-		return $tag;
818
-	}
819
-
820
-	/**
821
-	 * Validate the passed aspects or services.
822
-	 *
823
-	 * @param array $aspects_services List of aspects or services that need to be validated.
824
-	 * @param array $default          Default value if not valid.
825
-	 * @return array The validated list of aspects or services.
826
-	 */
827
-	public function validate_aspects_services( &$aspects_services, $default = array() ) {
828
-		if ( empty( $aspects_services ) || ! is_array( $aspects_services ) ) {
829
-			$aspects_services = $default;
830
-		} else {
831
-			array_walk( $aspects_services, 'sanitize_text_field' );
832
-		}
833
-		return $aspects_services;
834
-	}
572
+  }
573
+
574
+
575
+  /**
576
+   * Get a specific option.
577
+   *
578
+   * @param string       $option  ID of option to get.
579
+   * @param array|string $default Override default value if option not found.
580
+   * @return array|string Requested option value.
581
+   */
582
+  public function get_option( $option = null, $default = null ) {
583
+    if ( ! isset( self::$_options ) ) {
584
+      self::$_options = get_option( 'wp_to_diaspora_settings', self::$_default_options );
585
+    }
586
+    if ( isset( $option ) ) {
587
+      if ( isset( self::$_options[ $option ] ) ) {
588
+        // Return found option value.
589
+        return self::$_options[ $option ];
590
+      } elseif ( isset( $default ) ) {
591
+        // Return overridden default value.
592
+        return $default;
593
+      } elseif ( isset( self::$_default_options[ $option ] ) ) {
594
+        // Return default option value.
595
+        return self::$_default_options[ $option ];
596
+      }
597
+    }
598
+  }
599
+
600
+  /**
601
+   * Get all options.
602
+   *
603
+   * @return array All the options.
604
+   */
605
+  public function get_options() {
606
+    return self::$_options;
607
+  }
608
+
609
+  /**
610
+   * Set a certain option.
611
+   *
612
+   * @param string       $option ID of option to get.
613
+   * @param array|string $value  Value to be set for the passed option.
614
+   * @param boolean      $save   Save the options immediately after setting them.
615
+   */
616
+  public function set_option( $option, $value, $save = false ) {
617
+    if ( isset( $option ) ) {
618
+      if ( isset( $value ) ) {
619
+        self::$_options[ $option ] = $value;
620
+      } else {
621
+        unset( self::$_options[ $option ] );
622
+      }
623
+    }
624
+    if ( $save ) {
625
+      self::save();
626
+    }
627
+  }
628
+
629
+  /**
630
+   * Save the options.
631
+   */
632
+  public function save() {
633
+    update_option( 'wp_to_diaspora_settings', self::$_options );
634
+  }
635
+
636
+  /**
637
+   * Get all valid input values for the passed field.
638
+   *
639
+   * @param string $field Field to get the valid values for.
640
+   * @return array List of valid values.
641
+   */
642
+  public function get_valid_values( $field ) {
643
+    if ( array_key_exists( $field, self::$_valid_values ) ) {
644
+      return self::$_valid_values[ $field ];
645
+    }
646
+  }
647
+
648
+  /**
649
+   * Check if a value is valid for the passed field.
650
+   *
651
+   * @param string $field Field to check the valid value for.
652
+   * @param object $value Value to check validity.
653
+   * @return boolean If the passed value is valid.
654
+   */
655
+  public function is_valid_value( $field, $value ) {
656
+    if ( $valids = self::get_valid_values( $field ) ) {
657
+      return ( in_array( $value, $valids ) );
658
+    }
659
+    return false;
660
+  }
661
+
662
+  /**
663
+   * Validate all settings.
664
+   *
665
+   * @param array $input RAW input values.
666
+   * @return array Validated input values.
667
+   */
668
+  public function validate_settings( $input ) {
669
+    /* Validate all settings before saving to the database. */
670
+
671
+    // Saving the pod setup details.
672
+    if ( isset( $input['submit_setup'] ) ) {
673
+      $input['pod']      = trim( sanitize_text_field( $input['pod'] ), ' /' );
674
+      $input['username'] = sanitize_text_field( $input['username'] );
675
+      $input['password'] = sanitize_text_field( $input['password'] );
676
+
677
+      // If password is blank, it hasn't been changed.
678
+      // If new password is equal to the encrypted password already saved, it was just passed again. It happens everytime update_option('wp_to_diaspora_settings') is called.
679
+      if ( '' === $input['password'] || $this->get_option( 'password' ) === $input['password'] ) {
680
+        $input['password'] = $this->get_option( 'password' );
681
+      } else {
682
+        $input['password'] = WP2D_Helpers::encrypt( $input['password'] );
683
+      }
684
+
685
+      // This is for when JS in not enabled, to make sure that the aspects and services
686
+      // are refetched when displaying the options page after saving.
687
+      if ( isset( $input['no_js'] ) ) {
688
+        set_transient( 'wp2d_no_js_force_refetch', true );
689
+      }
690
+    }
691
+
692
+    // Saving the default options.
693
+    if ( isset( $input['submit_defaults'] ) ) {
694
+      if ( ! isset( $input['enabled_post_types'] ) ) {
695
+        $input['enabled_post_types'] = array();
696
+      }
697
+
698
+      // Checkboxes.
699
+      $this->validate_checkboxes( array( 'post_to_diaspora', 'fullentrylink' ), $input );
700
+
701
+      // Single Selects.
702
+      $this->validate_single_selects( 'display', $input );
703
+
704
+      // Multiple Selects.
705
+      $this->validate_multi_selects( 'tags_to_post', $input );
706
+
707
+      // Get unique, non-empty, trimmed tags and clean them up.
708
+      $this->validate_tags( $input['global_tags'] );
709
+
710
+      // Clean up the list of aspects. If the list is empty, only use the 'Public' aspect.
711
+      $this->validate_aspects_services( $input['aspects'], array( 'public' ) );
712
+
713
+      // Clean up the list of services.
714
+      $this->validate_aspects_services( $input['services'] );
715
+    }
716
+
717
+    // Reset to defaults.
718
+    if ( isset( $input['reset_defaults'] ) ) {
719
+      // Set the input to the default options.
720
+      $input = self::$_default_options;
721
+
722
+      // Don't reset the fetched lists of pods, aspects and services.
723
+      unset( $input['pod_list'] );
724
+      unset( $input['aspects_list'] );
725
+      unset( $input['services_list'] );
726
+    }
727
+
728
+    // Unset all unused input fields.
729
+    unset( $input['submit_defaults'] );
730
+    unset( $input['reset_defaults'] );
731
+    unset( $input['submit_setup'] );
732
+
733
+    // Parse inputs with default options and return.
734
+    return wp_parse_args( $input, array_merge( self::$_default_options, self::$_options ) );
735
+  }
736
+
737
+  /**
738
+   * Validate checkboxes, make them either true or false.
739
+   *
740
+   * @param string|array $checkboxes Checkboxes to validate.
741
+   * @param array        $options    Options values themselves.
742
+   * @return array The validated options.
743
+   */
744
+  public function validate_checkboxes( $checkboxes, &$options ) {
745
+    foreach ( WP2D_Helpers::str_to_arr( $checkboxes ) as $checkbox ) {
746
+      $options[ $checkbox ] = isset( $options[ $checkbox ] );
747
+    }
748
+    return $options;
749
+  }
750
+
751
+  /**
752
+   * Validate single-select fields and make sure their selected value are valid.
753
+   *
754
+   * @param string|array $selects Name(s) of the select fields.
755
+   * @param array        $options Options values themselves.
756
+   * @return array The validated options.
757
+   */
758
+  public function validate_single_selects( $selects, &$options ) {
759
+    foreach ( WP2D_Helpers::str_to_arr( $selects ) as $select ) {
760
+      if ( isset( $options[ $select ] ) && ! $this->is_valid_value( $select, $options[ $select ] ) ) {
761
+        unset( $options[ $select ] );
762
+      }
763
+    }
764
+    return $options;
765
+  }
766
+
767
+  /**
768
+   * Validate multi-select fields and make sure their selected values are valid.
769
+   *
770
+   * @param string|array $selects Name(s) of the select fields.
771
+   * @param array        $options Options values themselves.
772
+   * @return array The validated options.
773
+   */
774
+  public function validate_multi_selects( $selects, &$options ) {
775
+    foreach ( WP2D_Helpers::str_to_arr( $selects ) as $select ) {
776
+      if ( isset( $options[ $select ] ) ) {
777
+        foreach ( (array) $options[ $select ] as $option_value ) {
778
+          if ( ! $this->is_valid_value( $select, $option_value ) ) {
779
+            unset( $options[ $select ] );
780
+            break;
781
+          }
782
+        }
783
+      } else {
784
+        $options[ $select ] = array();
785
+      }
786
+    }
787
+    return $options;
788
+  }
789
+
790
+  /**
791
+   * Clean up the passed tags. Keep only alphanumeric, hyphen and underscore characters.
792
+   *
793
+   * @param array|string $tags Tags to be cleaned as array or comma seperated values.
794
+   * @return array The cleaned tags.
795
+   */
796
+  public function validate_tags( &$tags ) {
797
+    WP2D_Helpers::str_to_arr( $tags );
798
+
799
+    $tags = array_map( array( $this, 'validate_tag' ),
800
+      array_unique(
801
+        array_filter( $tags, 'trim' )
802
+      )
803
+    );
804
+    return $tags;
805
+  }
806
+
807
+  /**
808
+   * Clean up the passed tag. Keep only alphanumeric, hyphen and underscore characters.
809
+   *
810
+   * @todo What about eastern characters? (chinese, indian, etc.)
811
+   *
812
+   * @param string $tag Tag to be cleaned.
813
+   * @return string The clean tag.
814
+   */
815
+  public function validate_tag( &$tag ) {
816
+    $tag = preg_replace( '/[^\w $\-]/u', '', str_replace( ' ', '-', trim( $tag ) ) );
817
+    return $tag;
818
+  }
819
+
820
+  /**
821
+   * Validate the passed aspects or services.
822
+   *
823
+   * @param array $aspects_services List of aspects or services that need to be validated.
824
+   * @param array $default          Default value if not valid.
825
+   * @return array The validated list of aspects or services.
826
+   */
827
+  public function validate_aspects_services( &$aspects_services, $default = array() ) {
828
+    if ( empty( $aspects_services ) || ! is_array( $aspects_services ) ) {
829
+      $aspects_services = $default;
830
+    } else {
831
+      array_walk( $aspects_services, 'sanitize_text_field' );
832
+    }
833
+    return $aspects_services;
834
+  }
835 835
 }
Please login to merge, or discard this patch.
lib/class-api.php 1 patch
Indentation   +668 added lines, -668 removed lines patch added patch discarded remove patch
@@ -26,672 +26,672 @@
 block discarded – undo
26 26
  */
27 27
 class WP2D_API {
28 28
 
29
-	/**
30
-	 * The provider name to display when posting to diaspora*.
31
-	 *
32
-	 * @var string
33
-	 */
34
-	public $provider = 'WP to diaspora*';
35
-
36
-	/**
37
-	 * The last http request error that occurred.
38
-	 *
39
-	 * @var WP_Error
40
-	 */
41
-	private $_last_error;
42
-
43
-	/**
44
-	 * Security token to be used for making requests.
45
-	 *
46
-	 * @var string
47
-	 */
48
-	private $_token;
49
-
50
-	/**
51
-	 * Save the cookies for the requests.
52
-	 *
53
-	 * @var array
54
-	 */
55
-	private $_cookies;
56
-
57
-	/**
58
-	 * The last http request made to diaspora*.
59
-	 * Contains the response and request infos.
60
-	 *
61
-	 * @var object
62
-	 */
63
-	private $_last_request;
64
-
65
-	/**
66
-	 * Is this a secure server, use HTTPS instead of HTTP?
67
-	 *
68
-	 * @var boolean
69
-	 */
70
-	private $_is_secure;
71
-
72
-	/**
73
-	 * The pod domain to make the http requests to.
74
-	 *
75
-	 * @var string
76
-	 */
77
-	private $_pod;
78
-
79
-	/**
80
-	 * Username to use when logging in to diaspora*.
81
-	 *
82
-	 * @var string
83
-	 */
84
-	private $_username;
85
-
86
-	/**
87
-	 * Password to use when logging in to diaspora*.
88
-	 *
89
-	 * @var string
90
-	 */
91
-	private $_password;
92
-
93
-	/**
94
-	 * Remember the current login state.
95
-	 *
96
-	 * @var boolean
97
-	 */
98
-	private $_is_logged_in = false;
99
-
100
-	/**
101
-	 * The list of user's aspects, which get set after ever http request.
102
-	 *
103
-	 * @var array
104
-	 */
105
-	private $_aspects = array();
106
-
107
-	/**
108
-	 * The list of user's connected services, which get set after ever http request.
109
-	 *
110
-	 * @var array
111
-	 */
112
-	private $_services = array();
113
-
114
-	/**
115
-	 * List of regex expressions used to filter out details from http request responses.
116
-	 *
117
-	 * @var array
118
-	 */
119
-	private $_regexes = array(
120
-		'token'    => '/content="(.*?)" name="csrf-token"|name="csrf-token" content="(.*?)"/',
121
-		'aspects'  => '/"aspects"\:(\[.*?\])/',
122
-		'services' => '/"configured_services"\:(\[.*?\])/',
123
-	);
124
-
125
-	/**
126
-	 * The full pod url, with the used protocol.
127
-	 *
128
-	 * @param string $path Path to add to the pod url.
129
-	 * @return string Full pod url.
130
-	 */
131
-	public function get_pod_url( $path = '' ) {
132
-		$path = trim( $path, ' /' );
133
-
134
-		// Add a slash to the beginning?
135
-		if ( '' !== $path ) {
136
-			$path = '/' . $path;
137
-		}
138
-
139
-		return sprintf( 'http%s://%s%s', ( $this->_is_secure ) ? 's' : '', $this->_pod, $path );
140
-	}
141
-
142
-	/**
143
-	 * Constructor to initialise the connection to diaspora*.
144
-	 *
145
-	 * @param string  $pod       The pod domain to connect to.
146
-	 * @param boolean $is_secure Is this a secure server? (Default: true).
147
-	 */
148
-	public function __construct( $pod, $is_secure = true ) {
149
-		// Set class variables.
150
-		$this->_pod       = $pod;
151
-		$this->_is_secure = (bool) $is_secure;
152
-	}
153
-
154
-	/**
155
-	 * Initialise the connection to diaspora*. The pod and protocol can be changed by passing new parameters.
156
-	 * Check if we can connect to the pod to retrieve the token.
157
-	 *
158
-	 * @param string  $pod       Pod domain to connect to, if it should be changed.
159
-	 * @param boolean $is_secure Is this a secure server? (Default: true).
160
-	 * @return boolean True if we could get the token, else false.
161
-	 */
162
-	public function init( $pod = null, $is_secure = true ) {
163
-		// If we are changing pod, we need to fetch a new token.
164
-		$force_new_token = false;
165
-
166
-		// When initialising a connection, clear the last error.
167
-		// This is important when multiple init tries happen.
168
-		$this->_last_error = null;
169
-
170
-		// Change the pod we are connecting to?
171
-		if ( isset( $pod ) && ( $this->_pod !== $pod || $this->_is_secure !== $is_secure ) ) {
172
-			$this->_pod       = $pod;
173
-			$this->_is_secure = (bool) $is_secure;
174
-			$force_new_token  = true;
175
-		}
176
-
177
-		// Get and save the token.
178
-		if ( null === $this->_fetch_token( $force_new_token ) ) {
179
-			$error = ( $this->has_last_error() ) ? ' ' . $this->get_last_error() : '';
180
-			$this->_error( 'wp2d_api_init_failed',
181
-				sprintf(
182
-					_x( 'Failed to initialise connection to pod "%s".', 'Placeholder is the full pod URL.', 'wp-to-diaspora' ),
183
-					$this->get_pod_url()
184
-				) . $error,
185
-				array( 'help_tab' => 'troubleshooting' )
186
-			);
187
-
188
-			return false;
189
-		}
190
-		return true;
191
-	}
192
-
193
-	/**
194
-	 * Check if there is an API error around.
195
-	 *
196
-	 * @return boolean If there is an API error around.
197
-	 */
198
-	public function has_last_error() {
199
-		return is_wp_error( $this->_last_error );
200
-	}
201
-
202
-	/**
203
-	 * Get the last API error object.
204
-	 *
205
-	 * @param boolean $clear If the error should be cleared after returning it.
206
-	 * @return WP_Error|null The last API error object or null.
207
-	 */
208
-	public function get_last_error_object( $clear = true ) {
209
-		$last_error = $this->_last_error;
210
-		$clear && $this->_last_error = null;
211
-		return $last_error;
212
-	}
213
-
214
-	/**
215
-	 * Get the last API error message.
216
-	 *
217
-	 * @param boolean $clear If the error should be cleared after returning it.
218
-	 * @return string The last API error message.
219
-	 */
220
-	public function get_last_error( $clear = false ) {
221
-		$last_error = ( $this->has_last_error() ) ? $this->_last_error->get_error_message() : '';
222
-		$clear && $this->_last_error = null;
223
-		return $last_error;
224
-	}
225
-
226
-	/**
227
-	 * Fetch the secure token from Diaspora and save it for future use.
228
-	 *
229
-	 * @param boolean $force Force to fetch a new token.
230
-	 * @return string The fetched token.
231
-	 */
232
-	private function _fetch_token( $force = false ) {
233
-		if ( ! isset( $this->_token ) || (bool) $force ) {
234
-			// Go directly to the sign in page, as it would redirect to there anyway.
235
-			// Since _request function automatically saves the new token, just call it with no data.
236
-			$this->_request( '/users/sign_in' );
237
-		}
238
-		return $this->_token;
239
-	}
240
-
241
-	/**
242
-	 * Check if the API has been initialised. Otherwise set the last error.
243
-	 *
244
-	 * @return boolean Has the connection been initialised?
245
-	 */
246
-	private function _check_init() {
247
-		if ( is_null( $this->_token ) ) {
248
-			$this->_error( 'wp2d_api_connection_not_initialised', __( 'Connection not initialised.', 'wp-to-diaspora' ) );
249
-			return false;
250
-		}
251
-		return true;
252
-	}
253
-
254
-	/**
255
-	 * Check if we're logged in. Otherwise set the last error.
256
-	 *
257
-	 * @return boolean Are we logged in already?
258
-	 */
259
-	private function _check_login() {
260
-		if ( ! $this->_check_init() ) {
261
-			return false;
262
-		}
263
-		if ( ! $this->is_logged_in() ) {
264
-			$this->_error( 'wp2d_api_not_logged_in', __( 'Not logged in.', 'wp-to-diaspora' ) );
265
-			return false;
266
-		}
267
-		return true;
268
-	}
269
-
270
-	/**
271
-	 * Check if we are logged in.
272
-	 *
273
-	 * @return boolean Are we logged in already?
274
-	 */
275
-	public function is_logged_in() {
276
-		return $this->_is_logged_in;
277
-	}
278
-
279
-	/**
280
-	 * Log in to diaspora*.
281
-	 *
282
-	 * @param string  $username Username used for login.
283
-	 * @param string  $password Password used for login.
284
-	 * @param boolean $force    Force a new login even if we are already logged in.
285
-	 * @return boolean Did the login succeed?
286
-	 */
287
-	public function login( $username, $password, $force = false ) {
288
-		// Has the connection been initialised?
289
-		if ( ! $this->_check_init() ) {
290
-			$this->logout();
291
-			return false;
292
-		}
293
-
294
-		// Username and password both need to be set.
295
-		$username = ( isset( $username ) && '' !== $username ) ? $username : null;
296
-		$password = ( isset( $password ) && '' !== $password ) ? $password : null;
297
-		if ( ! isset( $username, $password ) ) {
298
-			$this->logout();
299
-			return false;
300
-		}
301
-
302
-		// If we are already logged in and not forcing a relogin, return.
303
-		if ( ! $force && $this->is_logged_in() &&
304
-			$username === $this->_username &&
305
-			$password === $this->_password ) {
306
-			return true;
307
-		}
308
-
309
-		// Set the newly passed username and password.
310
-		$this->_username = $username;
311
-		$this->_password = $password;
312
-
313
-		// Set up the login parameters.
314
-		$params = array(
315
-			'user[username]'     => $this->_username,
316
-			'user[password]'     => $this->_password,
317
-			'authenticity_token' => $this->_fetch_token(),
318
-		);
319
-
320
-		$args = array(
321
-			'method' => 'POST',
322
-			'body'   => $params,
323
-		);
324
-
325
-		// Try to sign in.
326
-		$this->_request( '/users/sign_in', $args );
327
-
328
-		// Can we load the bookmarklet to make sure we're logged in?
329
-		$response = $this->_request( '/bookmarklet' );
330
-
331
-		// If the request isn't successful, we are not logged in correctly.
332
-		if ( is_wp_error( $response ) || 200 !== $response->code ) {
333
-			// Login failed.
334
-			$this->_error( 'wp2d_api_login_failed', __( 'Login failed. Check your login details.', 'wp-to-diaspora' ), array( 'help_tab' => 'troubleshooting' ) );
335
-			$this->logout();
336
-			return false;
337
-		}
338
-
339
-		// Login succeeded.
340
-		$this->_is_logged_in = true;
341
-		return true;
342
-	}
343
-
344
-	/**
345
-	 * Perform a logout, resetting all login info.
346
-	 *
347
-	 * @since 1.6.0
348
-	 */
349
-	public function logout() {
350
-		$this->_is_logged_in = false;
351
-		$this->_username = null;
352
-		$this->_password = null;
353
-		$this->_aspects = array();
354
-		$this->_services = array();
355
-	}
356
-
357
-	/**
358
-	 * Perform a deinitialisation, resetting all class variables.
359
-	 *
360
-	 * @since 1.7.0
361
-	 */
362
-	public function deinit() {
363
-		$this->logout();
364
-		$this->_last_error = null;
365
-		$this->_token = null;
366
-		$this->_cookies = array();
367
-		$this->_last_request = null;
368
-	}
369
-
370
-	/**
371
-	 * Post to diaspora*.
372
-	 *
373
-	 * @param string       $text       The text to post.
374
-	 * @param array|string $aspects    The aspects to post to. Array or comma seperated ids.
375
-	 * @param array        $extra_data Any extra data to be added to the post call.
376
-	 * @return boolean|object Return the response data of the new diaspora* post if successfully posted, else false.
377
-	 */
378
-	public function post( $text, $aspects = 'public', $extra_data = array() ) {
379
-		// Are we logged in?
380
-		if ( ! $this->_check_login() ) {
381
-			return false;
382
-		}
383
-
384
-		// Put the aspects into a clean array.
385
-		$aspects = array_filter( WP2D_Helpers::str_to_arr( $aspects ) );
386
-
387
-		// If no aspects have been selected or the public one is also included, choose public only.
388
-		if ( empty( $aspects ) || in_array( 'public', $aspects ) ) {
389
-			$aspects = 'public';
390
-		}
391
-
392
-		// Prepare post data.
393
-		$post_data = array(
394
-			'aspect_ids'     => $aspects,
395
-			'status_message' => array(
396
-				'text' => $text,
397
-				'provider_display_name' => $this->provider,
398
-			),
399
-		);
400
-
401
-		// Add any extra data to the post.
402
-		if ( ! empty( $extra_data ) ) {
403
-				$post_data += $extra_data;
404
-		}
405
-
406
-		// Check if we can use the new wp_json_encode function.
407
-		$post_data = ( function_exists( 'wp_json_encode' ) )
408
-			? wp_json_encode( $post_data )
409
-			: json_encode( $post_data );
410
-
411
-		$args = array(
412
-			'method'  => 'POST',
413
-			'body'    => $post_data,
414
-			'headers' => array(
415
-				'Accept'       => 'application/json',
416
-				'Content-Type' => 'application/json',
417
-				'X-CSRF-Token' => $this->_fetch_token(),
418
-			),
419
-		);
420
-
421
-		// Submit the post.
422
-		$response = $this->_request( '/status_messages', $args );
423
-
424
-		if ( is_wp_error( $response ) ) {
425
-			$this->_error( 'wp2d_api_post_failed', $response->get_error_message() );
426
-			return false;
427
-		}
428
-
429
-		$diaspost = json_decode( $response->body );
430
-		if ( 201 !== $response->code ) {
431
-			$this->_error( 'wp2d_api_post_failed', ( isset( $diaspost->error ) ) ? $diaspost->error : _x( 'Unknown error occurred.', 'When an unknown error occurred in the WP2D_API object.', 'wp-to-diaspora' ) );
432
-			return false;
433
-		}
434
-
435
-		// Add additional info to our diaspora post object.
436
-		$diaspost->permalink = $this->get_pod_url( '/posts/' . $diaspost->guid );
437
-
438
-		return $diaspost;
439
-	}
440
-
441
-	/**
442
-	 * Delete a post or comment from diaspora*.
443
-	 *
444
-	 * @since 1.6.0
445
-	 *
446
-	 * @param string $what What to delete, 'post' or 'comment'.
447
-	 * @param string $id The ID of the post or comment to delete.
448
-	 * @return boolean If the deletion was successful.
449
-	 */
450
-	public function delete( $what, $id ) {
451
-		// Are we logged in?
452
-		if ( ! $this->_check_login() ) {
453
-			return false;
454
-		}
455
-
456
-		// For now, only deleting posts and comments is allowed.
457
-		if ( ! in_array( $what, array( 'post', 'comment' ) ) ) {
458
-			$this->_error( 'wp2d_api_delete_failed', __( 'You can only delete posts and comments.', 'wp-to-diaspora' ) );
459
-			return false;
460
-		}
461
-
462
-		$args = array(
463
-			'method'  => 'DELETE',
464
-			'headers' => array(
465
-				'Accept'       => 'application/json',
466
-				'Content-Type' => 'application/json',
467
-				'X-CSRF-Token' => $this->_fetch_token(),
468
-			),
469
-		);
470
-
471
-		// Try to delete the post or comment.
472
-		$response = $this->_request( '/' . $what . 's/' . $id, $args );
473
-
474
-		$error_message = '';
475
-
476
-		if ( is_wp_error( $response ) ) {
477
-			$error_message = $response->get_error_message();
478
-		} else {
479
-			switch ( $response->code ) {
480
-				case 204:
481
-					return true;
482
-				case 404:
483
-					$error_message = ( 'post' === $what )
484
-						? __( 'The post you tried to delete does not exist.', 'wp-to-diaspora' )
485
-						: __( 'The comment you tried to delete does not exist.', 'wp-to-diaspora' );
486
-					break;
487
-
488
-				// Due to diaspora* returning a proper 403 when trying to delete a foreign comment
489
-				// but returning a 500 when trying to delete a foreign post, this needs some special attention.
490
-				case 403:
491
-					if ( 'comment' === $what ) {
492
-						$error_message = __( 'The comment you tried to delete does not belong to you.', 'wp-to-diaspora' );
493
-						break;
494
-					}
495
-					// Fall through...
496
-				case 500:
497
-					if ( 'post' === $what ) {
498
-						$error_message = __( 'The post you tried to delete does not belong to you.', 'wp-to-diaspora' );
499
-						break;
500
-					}
501
-					// Fall through...
502
-				default:
503
-					$error_message = _x( 'Unknown error occurred.', 'When an unknown error occurred in the WP2D_API object.', 'wp-to-diaspora' );
504
-					break;
505
-			}
506
-		}
507
-
508
-		$this->_error( 'wp2d_api_delete_' . $what . '_failed', $error_message );
509
-		return false;
510
-	}
511
-
512
-	/**
513
-	 * Get the list of aspects.
514
-	 *
515
-	 * @param boolean $force Force to fetch new aspects.
516
-	 * @return array Array of aspect objects.
517
-	 */
518
-	public function get_aspects( $force = false ) {
519
-		$this->_aspects = $this->_get_aspects_services( 'aspects', $this->_aspects, $force );
520
-		return ( is_array( $this->_aspects ) ) ? $this->_aspects : false;
521
-	}
522
-
523
-	/**
524
-	 * Get the list of connected services.
525
-	 *
526
-	 * @param boolean $force Force to fetch new connected services.
527
-	 * @return array Array of service objects.
528
-	 */
529
-	public function get_services( $force = false ) {
530
-		$this->_services = $this->_get_aspects_services( 'services', $this->_services, $force );
531
-		return ( is_array( $this->_services ) ) ? $this->_services : false;
532
-	}
533
-
534
-	/**
535
-	 * Get the list of aspects or connected services.
536
-	 *
537
-	 * @param string  $type  Type of list to get.
538
-	 * @param array   $list  The current list of items.
539
-	 * @param boolean $force Force to fetch new list.
540
-	 * @return boolean Was the list fetched successfully?
541
-	 */
542
-	private function _get_aspects_services( $type, $list, $force ) {
543
-		if ( ! $this->_check_login() ) {
544
-			return false;
545
-		}
546
-
547
-		// Fetch the new list if the current list is empty or a reload is forced.
548
-		if ( empty( $list ) || (bool) $force ) {
549
-			$response = $this->_request( '/bookmarklet' );
550
-
551
-			if ( is_wp_error( $response ) || 200 !== $response->code ) {
552
-				switch ( $type ) {
553
-					case 'aspects':
554
-						$this->_error( 'wp2d_api_getting_aspects_failed', __( 'Error loading aspects.', 'wp-to-diaspora' ) );
555
-						break;
556
-					case 'services':
557
-						$this->_error( 'wp2d_api_getting_services_failed', __( 'Error loading services.', 'wp-to-diaspora' ) );
558
-						break;
559
-					default:
560
-						$this->_error( 'wp2d_api_getting_aspects_services_failed', _x( 'Unknown error occurred.', 'When an unknown error occurred in the WP2D_API object.', 'wp-to-diaspora' ) );
561
-						break;
562
-				}
563
-				return false;
564
-			}
565
-
566
-			// Load the aspects or services.
567
-			if ( is_array( $raw_list = json_decode( $this->_parse_regex( $type, $response->body ) ) ) ) {
568
-				// In case this fetch is forced, empty the list.
569
-				$list = array();
570
-
571
-				switch ( $type ) {
572
-					case 'aspects':
573
-						// Add the 'public' aspect, as it's global and not user specific.
574
-						$list['public'] = __( 'Public', 'wp-to-diaspora' );
575
-
576
-						// Add all user specific aspects.
577
-						foreach ( $raw_list as $aspect ) {
578
-							$list[ $aspect->id ] = $aspect->name;
579
-						}
580
-						break;
581
-					case 'services':
582
-						foreach ( $raw_list as $service ) {
583
-							$list[ $service ] = ucfirst( $service );
584
-						}
585
-						break;
586
-				}
587
-			}
588
-		}
589
-		return $list;
590
-	}
591
-
592
-	/**
593
-	 * Send an http(s) request via WP_HTTP API.
594
-	 *
595
-	 * @see WP_Http::request()
596
-	 *
597
-	 * @param string $url  The URL to request.
598
-	 * @param array  $args Arguments to be posted with the request.
599
-	 * @return object An object containing details about this request.
600
-	 */
601
-	private function _request( $url, $args = array() ) {
602
-		// Prefix the full pod URL if necessary.
603
-		if ( 0 === strpos( $url, '/' ) ) {
604
-			$url = $this->get_pod_url( $url );
605
-		}
606
-
607
-		// Disable redirections so we can verify HTTP response codes.
608
-		$defaults = array(
609
-			'redirection' => 0,
610
-			'sslverify'   => true,
611
-			'timeout'     => 60,
612
-			'method'      => 'GET',
613
-		);
614
-
615
-		// If the certificate bundle has been downloaded manually, use that instead.
616
-		// NOTE: This should actually never be necessary, it's a fallback!
617
-		if ( file_exists( WP2D_DIR . '/cacert.pem' ) ) {
618
-			$defaults['sslcertificates'] = WP2D_DIR . '/cacert.pem';
619
-		}
620
-
621
-		// Set the correct cookie.
622
-		if ( ! empty( $this->_cookies ) ) {
623
-			$defaults['cookies'] = $this->_cookies;
624
-		}
625
-
626
-		$args = wp_parse_args( $args, $defaults );
627
-
628
-		// Get the response from the WP_HTTP request.
629
-		$response = wp_remote_request( $url, $args );
630
-
631
-		if ( is_wp_error( $response ) ) {
632
-			$this->_last_error = $response;
633
-			return $response;
634
-		}
635
-
636
-		// Get the headers and the html response.
637
-		$headers = wp_remote_retrieve_headers( $response );
638
-		$body    = wp_remote_retrieve_body( $response );
639
-
640
-		// Remember this request.
641
-		$this->_last_request = new stdClass();
642
-		$this->_last_request->response = $response;
643
-		$this->_last_request->headers  = $headers;
644
-		$this->_last_request->body     = $body;
645
-		$this->_last_request->message  = wp_remote_retrieve_response_message( $response );
646
-		$this->_last_request->code     = wp_remote_retrieve_response_code( $response );
647
-
648
-		// Save the new token.
649
-		if ( $token = $this->_parse_regex( 'token', $body ) ) {
650
-			$this->_token = $token;
651
-		}
652
-
653
-		// Save the latest cookies.
654
-		if ( isset( $response['cookies'] ) ) {
655
-			$this->_cookies = $response['cookies'];
656
-		}
657
-
658
-		// Return the last request details.
659
-		return $this->_last_request;
660
-	}
661
-
662
-	/**
663
-	 * Helper method to set the last occurred error.
664
-	 *
665
-	 * @see WP_Error::__construct()
666
-	 * @since 1.6.0
667
-	 *
668
-	 * @param  string|int $code    Error code.
669
-	 * @param  string     $message Error message.
670
-	 * @param  mixed      $data    Error data.
671
-	 */
672
-	private function _error( $code, $message, $data = '' ) {
673
-		// Always add the code and message of the last request.
674
-		$data = array_merge( array_filter( (array) $data ), array(
675
-			'code'    => ( isset( $this->_last_request->code ) ) ? $this->_last_request->code : null,
676
-			'message' => ( isset( $this->_last_request->message ) ) ? $this->_last_request->message : null,
677
-		) );
678
-		$this->_last_error = new WP_Error( $code, $message, $data );
679
-	}
680
-
681
-	/**
682
-	 * Parse the regex and return the found string.
683
-	 *
684
-	 * @param string $regex   Shorthand of a saved regex or a custom regex.
685
-	 * @param string $content Text to parse the regex with.
686
-	 * @return string The found string, or an empty string.
687
-	 */
688
-	private function _parse_regex( $regex, $content ) {
689
-		// Use a shorthand regex if available.
690
-		if ( array_key_exists( $regex, $this->_regexes ) ) {
691
-			$regex = $this->_regexes[ $regex ];
692
-		}
693
-
694
-		preg_match( $regex, $content, $matches );
695
-		return trim( array_pop( $matches ) );
696
-	}
29
+  /**
30
+   * The provider name to display when posting to diaspora*.
31
+   *
32
+   * @var string
33
+   */
34
+  public $provider = 'WP to diaspora*';
35
+
36
+  /**
37
+   * The last http request error that occurred.
38
+   *
39
+   * @var WP_Error
40
+   */
41
+  private $_last_error;
42
+
43
+  /**
44
+   * Security token to be used for making requests.
45
+   *
46
+   * @var string
47
+   */
48
+  private $_token;
49
+
50
+  /**
51
+   * Save the cookies for the requests.
52
+   *
53
+   * @var array
54
+   */
55
+  private $_cookies;
56
+
57
+  /**
58
+   * The last http request made to diaspora*.
59
+   * Contains the response and request infos.
60
+   *
61
+   * @var object
62
+   */
63
+  private $_last_request;
64
+
65
+  /**
66
+   * Is this a secure server, use HTTPS instead of HTTP?
67
+   *
68
+   * @var boolean
69
+   */
70
+  private $_is_secure;
71
+
72
+  /**
73
+   * The pod domain to make the http requests to.
74
+   *
75
+   * @var string
76
+   */
77
+  private $_pod;
78
+
79
+  /**
80
+   * Username to use when logging in to diaspora*.
81
+   *
82
+   * @var string
83
+   */
84
+  private $_username;
85
+
86
+  /**
87
+   * Password to use when logging in to diaspora*.
88
+   *
89
+   * @var string
90
+   */
91
+  private $_password;
92
+
93
+  /**
94
+   * Remember the current login state.
95
+   *
96
+   * @var boolean
97
+   */
98
+  private $_is_logged_in = false;
99
+
100
+  /**
101
+   * The list of user's aspects, which get set after ever http request.
102
+   *
103
+   * @var array
104
+   */
105
+  private $_aspects = array();
106
+
107
+  /**
108
+   * The list of user's connected services, which get set after ever http request.
109
+   *
110
+   * @var array
111
+   */
112
+  private $_services = array();
113
+
114
+  /**
115
+   * List of regex expressions used to filter out details from http request responses.
116
+   *
117
+   * @var array
118
+   */
119
+  private $_regexes = array(
120
+    'token'    => '/content="(.*?)" name="csrf-token"|name="csrf-token" content="(.*?)"/',
121
+    'aspects'  => '/"aspects"\:(\[.*?\])/',
122
+    'services' => '/"configured_services"\:(\[.*?\])/',
123
+  );
124
+
125
+  /**
126
+   * The full pod url, with the used protocol.
127
+   *
128
+   * @param string $path Path to add to the pod url.
129
+   * @return string Full pod url.
130
+   */
131
+  public function get_pod_url( $path = '' ) {
132
+    $path = trim( $path, ' /' );
133
+
134
+    // Add a slash to the beginning?
135
+    if ( '' !== $path ) {
136
+      $path = '/' . $path;
137
+    }
138
+
139
+    return sprintf( 'http%s://%s%s', ( $this->_is_secure ) ? 's' : '', $this->_pod, $path );
140
+  }
141
+
142
+  /**
143
+   * Constructor to initialise the connection to diaspora*.
144
+   *
145
+   * @param string  $pod       The pod domain to connect to.
146
+   * @param boolean $is_secure Is this a secure server? (Default: true).
147
+   */
148
+  public function __construct( $pod, $is_secure = true ) {
149
+    // Set class variables.
150
+    $this->_pod       = $pod;
151
+    $this->_is_secure = (bool) $is_secure;
152
+  }
153
+
154
+  /**
155
+   * Initialise the connection to diaspora*. The pod and protocol can be changed by passing new parameters.
156
+   * Check if we can connect to the pod to retrieve the token.
157
+   *
158
+   * @param string  $pod       Pod domain to connect to, if it should be changed.
159
+   * @param boolean $is_secure Is this a secure server? (Default: true).
160
+   * @return boolean True if we could get the token, else false.
161
+   */
162
+  public function init( $pod = null, $is_secure = true ) {
163
+    // If we are changing pod, we need to fetch a new token.
164
+    $force_new_token = false;
165
+
166
+    // When initialising a connection, clear the last error.
167
+    // This is important when multiple init tries happen.
168
+    $this->_last_error = null;
169
+
170
+    // Change the pod we are connecting to?
171
+    if ( isset( $pod ) && ( $this->_pod !== $pod || $this->_is_secure !== $is_secure ) ) {
172
+      $this->_pod       = $pod;
173
+      $this->_is_secure = (bool) $is_secure;
174
+      $force_new_token  = true;
175
+    }
176
+
177
+    // Get and save the token.
178
+    if ( null === $this->_fetch_token( $force_new_token ) ) {
179
+      $error = ( $this->has_last_error() ) ? ' ' . $this->get_last_error() : '';
180
+      $this->_error( 'wp2d_api_init_failed',
181
+        sprintf(
182
+          _x( 'Failed to initialise connection to pod "%s".', 'Placeholder is the full pod URL.', 'wp-to-diaspora' ),
183
+          $this->get_pod_url()
184
+        ) . $error,
185
+        array( 'help_tab' => 'troubleshooting' )
186
+      );
187
+
188
+      return false;
189
+    }
190
+    return true;
191
+  }
192
+
193
+  /**
194
+   * Check if there is an API error around.
195
+   *
196
+   * @return boolean If there is an API error around.
197
+   */
198
+  public function has_last_error() {
199
+    return is_wp_error( $this->_last_error );
200
+  }
201
+
202
+  /**
203
+   * Get the last API error object.
204
+   *
205
+   * @param boolean $clear If the error should be cleared after returning it.
206
+   * @return WP_Error|null The last API error object or null.
207
+   */
208
+  public function get_last_error_object( $clear = true ) {
209
+    $last_error = $this->_last_error;
210
+    $clear && $this->_last_error = null;
211
+    return $last_error;
212
+  }
213
+
214
+  /**
215
+   * Get the last API error message.
216
+   *
217
+   * @param boolean $clear If the error should be cleared after returning it.
218
+   * @return string The last API error message.
219
+   */
220
+  public function get_last_error( $clear = false ) {
221
+    $last_error = ( $this->has_last_error() ) ? $this->_last_error->get_error_message() : '';
222
+    $clear && $this->_last_error = null;
223
+    return $last_error;
224
+  }
225
+
226
+  /**
227
+   * Fetch the secure token from Diaspora and save it for future use.
228
+   *
229
+   * @param boolean $force Force to fetch a new token.
230
+   * @return string The fetched token.
231
+   */
232
+  private function _fetch_token( $force = false ) {
233
+    if ( ! isset( $this->_token ) || (bool) $force ) {
234
+      // Go directly to the sign in page, as it would redirect to there anyway.
235
+      // Since _request function automatically saves the new token, just call it with no data.
236
+      $this->_request( '/users/sign_in' );
237
+    }
238
+    return $this->_token;
239
+  }
240
+
241
+  /**
242
+   * Check if the API has been initialised. Otherwise set the last error.
243
+   *
244
+   * @return boolean Has the connection been initialised?
245
+   */
246
+  private function _check_init() {
247
+    if ( is_null( $this->_token ) ) {
248
+      $this->_error( 'wp2d_api_connection_not_initialised', __( 'Connection not initialised.', 'wp-to-diaspora' ) );
249
+      return false;
250
+    }
251
+    return true;
252
+  }
253
+
254
+  /**
255
+   * Check if we're logged in. Otherwise set the last error.
256
+   *
257
+   * @return boolean Are we logged in already?
258
+   */
259
+  private function _check_login() {
260
+    if ( ! $this->_check_init() ) {
261
+      return false;
262
+    }
263
+    if ( ! $this->is_logged_in() ) {
264
+      $this->_error( 'wp2d_api_not_logged_in', __( 'Not logged in.', 'wp-to-diaspora' ) );
265
+      return false;
266
+    }
267
+    return true;
268
+  }
269
+
270
+  /**
271
+   * Check if we are logged in.
272
+   *
273
+   * @return boolean Are we logged in already?
274
+   */
275
+  public function is_logged_in() {
276
+    return $this->_is_logged_in;
277
+  }
278
+
279
+  /**
280
+   * Log in to diaspora*.
281
+   *
282
+   * @param string  $username Username used for login.
283
+   * @param string  $password Password used for login.
284
+   * @param boolean $force    Force a new login even if we are already logged in.
285
+   * @return boolean Did the login succeed?
286
+   */
287
+  public function login( $username, $password, $force = false ) {
288
+    // Has the connection been initialised?
289
+    if ( ! $this->_check_init() ) {
290
+      $this->logout();
291
+      return false;
292
+    }
293
+
294
+    // Username and password both need to be set.
295
+    $username = ( isset( $username ) && '' !== $username ) ? $username : null;
296
+    $password = ( isset( $password ) && '' !== $password ) ? $password : null;
297
+    if ( ! isset( $username, $password ) ) {
298
+      $this->logout();
299
+      return false;
300
+    }
301
+
302
+    // If we are already logged in and not forcing a relogin, return.
303
+    if ( ! $force && $this->is_logged_in() &&
304
+      $username === $this->_username &&
305
+      $password === $this->_password ) {
306
+      return true;
307
+    }
308
+
309
+    // Set the newly passed username and password.
310
+    $this->_username = $username;
311
+    $this->_password = $password;
312
+
313
+    // Set up the login parameters.
314
+    $params = array(
315
+      'user[username]'     => $this->_username,
316
+      'user[password]'     => $this->_password,
317
+      'authenticity_token' => $this->_fetch_token(),
318
+    );
319
+
320
+    $args = array(
321
+      'method' => 'POST',
322
+      'body'   => $params,
323
+    );
324
+
325
+    // Try to sign in.
326
+    $this->_request( '/users/sign_in', $args );
327
+
328
+    // Can we load the bookmarklet to make sure we're logged in?
329
+    $response = $this->_request( '/bookmarklet' );
330
+
331
+    // If the request isn't successful, we are not logged in correctly.
332
+    if ( is_wp_error( $response ) || 200 !== $response->code ) {
333
+      // Login failed.
334
+      $this->_error( 'wp2d_api_login_failed', __( 'Login failed. Check your login details.', 'wp-to-diaspora' ), array( 'help_tab' => 'troubleshooting' ) );
335
+      $this->logout();
336
+      return false;
337
+    }
338
+
339
+    // Login succeeded.
340
+    $this->_is_logged_in = true;
341
+    return true;
342
+  }
343
+
344
+  /**
345
+   * Perform a logout, resetting all login info.
346
+   *
347
+   * @since 1.6.0
348
+   */
349
+  public function logout() {
350
+    $this->_is_logged_in = false;
351
+    $this->_username = null;
352
+    $this->_password = null;
353
+    $this->_aspects = array();
354
+    $this->_services = array();
355
+  }
356
+
357
+  /**
358
+   * Perform a deinitialisation, resetting all class variables.
359
+   *
360
+   * @since 1.7.0
361
+   */
362
+  public function deinit() {
363
+    $this->logout();
364
+    $this->_last_error = null;
365
+    $this->_token = null;
366
+    $this->_cookies = array();
367
+    $this->_last_request = null;
368
+  }
369
+
370
+  /**
371
+   * Post to diaspora*.
372
+   *
373
+   * @param string       $text       The text to post.
374
+   * @param array|string $aspects    The aspects to post to. Array or comma seperated ids.
375
+   * @param array        $extra_data Any extra data to be added to the post call.
376
+   * @return boolean|object Return the response data of the new diaspora* post if successfully posted, else false.
377
+   */
378
+  public function post( $text, $aspects = 'public', $extra_data = array() ) {
379
+    // Are we logged in?
380
+    if ( ! $this->_check_login() ) {
381
+      return false;
382
+    }
383
+
384
+    // Put the aspects into a clean array.
385
+    $aspects = array_filter( WP2D_Helpers::str_to_arr( $aspects ) );
386
+
387
+    // If no aspects have been selected or the public one is also included, choose public only.
388
+    if ( empty( $aspects ) || in_array( 'public', $aspects ) ) {
389
+      $aspects = 'public';
390
+    }
391
+
392
+    // Prepare post data.
393
+    $post_data = array(
394
+      'aspect_ids'     => $aspects,
395
+      'status_message' => array(
396
+        'text' => $text,
397
+        'provider_display_name' => $this->provider,
398
+      ),
399
+    );
400
+
401
+    // Add any extra data to the post.
402
+    if ( ! empty( $extra_data ) ) {
403
+        $post_data += $extra_data;
404
+    }
405
+
406
+    // Check if we can use the new wp_json_encode function.
407
+    $post_data = ( function_exists( 'wp_json_encode' ) )
408
+      ? wp_json_encode( $post_data )
409
+      : json_encode( $post_data );
410
+
411
+    $args = array(
412
+      'method'  => 'POST',
413
+      'body'    => $post_data,
414
+      'headers' => array(
415
+        'Accept'       => 'application/json',
416
+        'Content-Type' => 'application/json',
417
+        'X-CSRF-Token' => $this->_fetch_token(),
418
+      ),
419
+    );
420
+
421
+    // Submit the post.
422
+    $response = $this->_request( '/status_messages', $args );
423
+
424
+    if ( is_wp_error( $response ) ) {
425
+      $this->_error( 'wp2d_api_post_failed', $response->get_error_message() );
426
+      return false;
427
+    }
428
+
429
+    $diaspost = json_decode( $response->body );
430
+    if ( 201 !== $response->code ) {
431
+      $this->_error( 'wp2d_api_post_failed', ( isset( $diaspost->error ) ) ? $diaspost->error : _x( 'Unknown error occurred.', 'When an unknown error occurred in the WP2D_API object.', 'wp-to-diaspora' ) );
432
+      return false;
433
+    }
434
+
435
+    // Add additional info to our diaspora post object.
436
+    $diaspost->permalink = $this->get_pod_url( '/posts/' . $diaspost->guid );
437
+
438
+    return $diaspost;
439
+  }
440
+
441
+  /**
442
+   * Delete a post or comment from diaspora*.
443
+   *
444
+   * @since 1.6.0
445
+   *
446
+   * @param string $what What to delete, 'post' or 'comment'.
447
+   * @param string $id The ID of the post or comment to delete.
448
+   * @return boolean If the deletion was successful.
449
+   */
450
+  public function delete( $what, $id ) {
451
+    // Are we logged in?
452
+    if ( ! $this->_check_login() ) {
453
+      return false;
454
+    }
455
+
456
+    // For now, only deleting posts and comments is allowed.
457
+    if ( ! in_array( $what, array( 'post', 'comment' ) ) ) {
458
+      $this->_error( 'wp2d_api_delete_failed', __( 'You can only delete posts and comments.', 'wp-to-diaspora' ) );
459
+      return false;
460
+    }
461
+
462
+    $args = array(
463
+      'method'  => 'DELETE',
464
+      'headers' => array(
465
+        'Accept'       => 'application/json',
466
+        'Content-Type' => 'application/json',
467
+        'X-CSRF-Token' => $this->_fetch_token(),
468
+      ),
469
+    );
470
+
471
+    // Try to delete the post or comment.
472
+    $response = $this->_request( '/' . $what . 's/' . $id, $args );
473
+
474
+    $error_message = '';
475
+
476
+    if ( is_wp_error( $response ) ) {
477
+      $error_message = $response->get_error_message();
478
+    } else {
479
+      switch ( $response->code ) {
480
+        case 204:
481
+          return true;
482
+        case 404:
483
+          $error_message = ( 'post' === $what )
484
+            ? __( 'The post you tried to delete does not exist.', 'wp-to-diaspora' )
485
+            : __( 'The comment you tried to delete does not exist.', 'wp-to-diaspora' );
486
+          break;
487
+
488
+        // Due to diaspora* returning a proper 403 when trying to delete a foreign comment
489
+        // but returning a 500 when trying to delete a foreign post, this needs some special attention.
490
+        case 403:
491
+          if ( 'comment' === $what ) {
492
+            $error_message = __( 'The comment you tried to delete does not belong to you.', 'wp-to-diaspora' );
493
+            break;
494
+          }
495
+          // Fall through...
496
+        case 500:
497
+          if ( 'post' === $what ) {
498
+            $error_message = __( 'The post you tried to delete does not belong to you.', 'wp-to-diaspora' );
499
+            break;
500
+          }
501
+          // Fall through...
502
+        default:
503
+          $error_message = _x( 'Unknown error occurred.', 'When an unknown error occurred in the WP2D_API object.', 'wp-to-diaspora' );
504
+          break;
505
+      }
506
+    }
507
+
508
+    $this->_error( 'wp2d_api_delete_' . $what . '_failed', $error_message );
509
+    return false;
510
+  }
511
+
512
+  /**
513
+   * Get the list of aspects.
514
+   *
515
+   * @param boolean $force Force to fetch new aspects.
516
+   * @return array Array of aspect objects.
517
+   */
518
+  public function get_aspects( $force = false ) {
519
+    $this->_aspects = $this->_get_aspects_services( 'aspects', $this->_aspects, $force );
520
+    return ( is_array( $this->_aspects ) ) ? $this->_aspects : false;
521
+  }
522
+
523
+  /**
524
+   * Get the list of connected services.
525
+   *
526
+   * @param boolean $force Force to fetch new connected services.
527
+   * @return array Array of service objects.
528
+   */
529
+  public function get_services( $force = false ) {
530
+    $this->_services = $this->_get_aspects_services( 'services', $this->_services, $force );
531
+    return ( is_array( $this->_services ) ) ? $this->_services : false;
532
+  }
533
+
534
+  /**
535
+   * Get the list of aspects or connected services.
536
+   *
537
+   * @param string  $type  Type of list to get.
538
+   * @param array   $list  The current list of items.
539
+   * @param boolean $force Force to fetch new list.
540
+   * @return boolean Was the list fetched successfully?
541
+   */
542
+  private function _get_aspects_services( $type, $list, $force ) {
543
+    if ( ! $this->_check_login() ) {
544
+      return false;
545
+    }
546
+
547
+    // Fetch the new list if the current list is empty or a reload is forced.
548
+    if ( empty( $list ) || (bool) $force ) {
549
+      $response = $this->_request( '/bookmarklet' );
550
+
551
+      if ( is_wp_error( $response ) || 200 !== $response->code ) {
552
+        switch ( $type ) {
553
+          case 'aspects':
554
+            $this->_error( 'wp2d_api_getting_aspects_failed', __( 'Error loading aspects.', 'wp-to-diaspora' ) );
555
+            break;
556
+          case 'services':
557
+            $this->_error( 'wp2d_api_getting_services_failed', __( 'Error loading services.', 'wp-to-diaspora' ) );
558
+            break;
559
+          default:
560
+            $this->_error( 'wp2d_api_getting_aspects_services_failed', _x( 'Unknown error occurred.', 'When an unknown error occurred in the WP2D_API object.', 'wp-to-diaspora' ) );
561
+            break;
562
+        }
563
+        return false;
564
+      }
565
+
566
+      // Load the aspects or services.
567
+      if ( is_array( $raw_list = json_decode( $this->_parse_regex( $type, $response->body ) ) ) ) {
568
+        // In case this fetch is forced, empty the list.
569
+        $list = array();
570
+
571
+        switch ( $type ) {
572
+          case 'aspects':
573
+            // Add the 'public' aspect, as it's global and not user specific.
574
+            $list['public'] = __( 'Public', 'wp-to-diaspora' );
575
+
576
+            // Add all user specific aspects.
577
+            foreach ( $raw_list as $aspect ) {
578
+              $list[ $aspect->id ] = $aspect->name;
579
+            }
580
+            break;
581
+          case 'services':
582
+            foreach ( $raw_list as $service ) {
583
+              $list[ $service ] = ucfirst( $service );
584
+            }
585
+            break;
586
+        }
587
+      }
588
+    }
589
+    return $list;
590
+  }
591
+
592
+  /**
593
+   * Send an http(s) request via WP_HTTP API.
594
+   *
595
+   * @see WP_Http::request()
596
+   *
597
+   * @param string $url  The URL to request.
598
+   * @param array  $args Arguments to be posted with the request.
599
+   * @return object An object containing details about this request.
600
+   */
601
+  private function _request( $url, $args = array() ) {
602
+    // Prefix the full pod URL if necessary.
603
+    if ( 0 === strpos( $url, '/' ) ) {
604
+      $url = $this->get_pod_url( $url );
605
+    }
606
+
607
+    // Disable redirections so we can verify HTTP response codes.
608
+    $defaults = array(
609
+      'redirection' => 0,
610
+      'sslverify'   => true,
611
+      'timeout'     => 60,
612
+      'method'      => 'GET',
613
+    );
614
+
615
+    // If the certificate bundle has been downloaded manually, use that instead.
616
+    // NOTE: This should actually never be necessary, it's a fallback!
617
+    if ( file_exists( WP2D_DIR . '/cacert.pem' ) ) {
618
+      $defaults['sslcertificates'] = WP2D_DIR . '/cacert.pem';
619
+    }
620
+
621
+    // Set the correct cookie.
622
+    if ( ! empty( $this->_cookies ) ) {
623
+      $defaults['cookies'] = $this->_cookies;
624
+    }
625
+
626
+    $args = wp_parse_args( $args, $defaults );
627
+
628
+    // Get the response from the WP_HTTP request.
629
+    $response = wp_remote_request( $url, $args );
630
+
631
+    if ( is_wp_error( $response ) ) {
632
+      $this->_last_error = $response;
633
+      return $response;
634
+    }
635
+
636
+    // Get the headers and the html response.
637
+    $headers = wp_remote_retrieve_headers( $response );
638
+    $body    = wp_remote_retrieve_body( $response );
639
+
640
+    // Remember this request.
641
+    $this->_last_request = new stdClass();
642
+    $this->_last_request->response = $response;
643
+    $this->_last_request->headers  = $headers;
644
+    $this->_last_request->body     = $body;
645
+    $this->_last_request->message  = wp_remote_retrieve_response_message( $response );
646
+    $this->_last_request->code     = wp_remote_retrieve_response_code( $response );
647
+
648
+    // Save the new token.
649
+    if ( $token = $this->_parse_regex( 'token', $body ) ) {
650
+      $this->_token = $token;
651
+    }
652
+
653
+    // Save the latest cookies.
654
+    if ( isset( $response['cookies'] ) ) {
655
+      $this->_cookies = $response['cookies'];
656
+    }
657
+
658
+    // Return the last request details.
659
+    return $this->_last_request;
660
+  }
661
+
662
+  /**
663
+   * Helper method to set the last occurred error.
664
+   *
665
+   * @see WP_Error::__construct()
666
+   * @since 1.6.0
667
+   *
668
+   * @param  string|int $code    Error code.
669
+   * @param  string     $message Error message.
670
+   * @param  mixed      $data    Error data.
671
+   */
672
+  private function _error( $code, $message, $data = '' ) {
673
+    // Always add the code and message of the last request.
674
+    $data = array_merge( array_filter( (array) $data ), array(
675
+      'code'    => ( isset( $this->_last_request->code ) ) ? $this->_last_request->code : null,
676
+      'message' => ( isset( $this->_last_request->message ) ) ? $this->_last_request->message : null,
677
+    ) );
678
+    $this->_last_error = new WP_Error( $code, $message, $data );
679
+  }
680
+
681
+  /**
682
+   * Parse the regex and return the found string.
683
+   *
684
+   * @param string $regex   Shorthand of a saved regex or a custom regex.
685
+   * @param string $content Text to parse the regex with.
686
+   * @return string The found string, or an empty string.
687
+   */
688
+  private function _parse_regex( $regex, $content ) {
689
+    // Use a shorthand regex if available.
690
+    if ( array_key_exists( $regex, $this->_regexes ) ) {
691
+      $regex = $this->_regexes[ $regex ];
692
+    }
693
+
694
+    preg_match( $regex, $content, $matches );
695
+    return trim( array_pop( $matches ) );
696
+  }
697 697
 }
Please login to merge, or discard this patch.
lib/class-helpers.php 1 patch
Indentation   +138 added lines, -138 removed lines patch added patch discarded remove patch
@@ -14,142 +14,142 @@
 block discarded – undo
14 14
  */
15 15
 class WP2D_Helpers {
16 16
 
17
-	/**
18
-	 * Debug text that get's accumulated before output.
19
-	 *
20
-	 * @var string
21
-	 */
22
-	private static $_debugging = '';
23
-
24
-	/**
25
-	 * Add a line to the debug output. Include the stack trace to see where it's coming from.
26
-	 *
27
-	 * @param string $text Text to add.
28
-	 */
29
-	public static function add_debugging( $text ) {
30
-		// Make sure we're in debug mode.
31
-		if ( defined( 'WP2D_DEBUGGING' ) && true === WP2D_DEBUGGING ) {
32
-			$d = '';
33
-			foreach ( debug_backtrace() as $dbt ) {
34
-				extract( $dbt );
35
-				// Only trace back as far as the plugin goes.
36
-				if ( strstr( $file, plugin_dir_path( dirname( __FILE__ ) ) ) ) {
37
-					$d = sprintf( "%s%s%s [%s:%s]\n", $class, $type, $function, basename( $file ), $line ) . $d;
38
-				}
39
-			}
40
-
41
-			self::$_debugging .= sprintf( "%s\n%s\n", date( 'Y.m.d H:i:s' ), $d . $text );
42
-
43
-			return true;
44
-		}
45
-		return false;
46
-	}
47
-
48
-	/**
49
-	 * Return the debug output.
50
-	 *
51
-	 * @return string The debug output.
52
-	 */
53
-	public static function get_debugging() {
54
-		if ( defined( 'WP2D_DEBUGGING' ) && true === WP2D_DEBUGGING ) {
55
-			return self::$_debugging;
56
-		}
57
-		return false;
58
-	}
59
-
60
-	/**
61
-	 * Convert a string with comma seperated values to an array.
62
-	 *
63
-	 * @todo Make $input by value.
64
-	 *
65
-	 * @param array|string $input The string to be converted.
66
-	 * @return array The converted array.
67
-	 */
68
-	public static function str_to_arr( &$input ) {
69
-		if ( ! is_array( $input ) ) {
70
-			// Explode string > Trim each entry > Remove blanks > Re-index array.
71
-			$input = array_values( array_filter( array_map( 'trim', explode( ',', $input ) ) ) );
72
-		} else {
73
-			// If we're already an array, make sure we return it clean.
74
-			self::arr_to_str( $input );
75
-			self::str_to_arr( $input );
76
-		}
77
-		return $input;
78
-	}
79
-
80
-	/**
81
-	 * Convert an array to a string with comma seperated values.
82
-	 *
83
-	 * @todo Make $input by value.
84
-	 *
85
-	 * @param array|string $input The array to be converted.
86
-	 * @return string The converted string.
87
-	 */
88
-	public static function arr_to_str( &$input ) {
89
-		if ( is_array( $input ) ) {
90
-			// Trim each entry > Remove blanks > Implode them together.
91
-			$input = implode( ',', array_filter( array_map( 'trim', $input ) ) );
92
-		} else {
93
-			// If we're already a string, make sure we return it clean.
94
-			self::str_to_arr( $input );
95
-			self::arr_to_str( $input );
96
-		}
97
-		return $input;
98
-	}
99
-
100
-	/**
101
-	 * Encrypt the passed string with the passed key.
102
-	 *
103
-	 * @param string $input String to be encrypted.
104
-	 * @param string $key   The key used for the encryption.
105
-	 * @return string The encrypted string.
106
-	 */
107
-	public static function encrypt( $input, $key = AUTH_KEY ) {
108
-		if ( is_null( $input ) || '' === $input ) {
109
-			return false;
110
-		}
111
-		global $wpdb;
112
-		return $wpdb->get_var( $wpdb->prepare( 'SELECT HEX(AES_ENCRYPT(%s,%s))', $input, $key ) );
113
-	}
114
-
115
-	/**
116
-	 * Decrypt the passed string with the passed key.
117
-	 *
118
-	 * @param string $input String to be decrypted.
119
-	 * @param string $key   The key used for the decryption.
120
-	 * @return string The decrypted string.
121
-	 */
122
-	public static function decrypt( $input, $key = AUTH_KEY ) {
123
-		if ( is_null( $input ) || '' === $input ) {
124
-			return false;
125
-		}
126
-		global $wpdb;
127
-		return $wpdb->get_var( $wpdb->prepare( 'SELECT AES_DECRYPT(UNHEX(%s),%s)', $input, $key ) );
128
-	}
129
-
130
-	/**
131
-	 * Set up and return an API connection using the currently saved options..
132
-	 *
133
-	 * @return WP2D_API The API object.
134
-	 */
135
-	public static function api_quick_connect() {
136
-		$options   = WP2D_Options::instance();
137
-		$pod       = (string) $options->get_option( 'pod' );
138
-		$is_secure = true;
139
-		$username  = (string) $options->get_option( 'username' );
140
-		$password  = WP2D_Helpers::decrypt( (string) $options->get_option( 'password' ) );
141
-
142
-		$api = new WP2D_API( $pod, $is_secure );
143
-
144
-		// This is necessary for correct error handling!
145
-		if ( $api->init() ) {
146
-			$api->login( $username, $password );
147
-		}
148
-
149
-		if ( $api->has_last_error() ) {
150
-			self::add_debugging( $api->get_last_error() );
151
-		}
152
-
153
-		return $api;
154
-	}
17
+  /**
18
+   * Debug text that get's accumulated before output.
19
+   *
20
+   * @var string
21
+   */
22
+  private static $_debugging = '';
23
+
24
+  /**
25
+   * Add a line to the debug output. Include the stack trace to see where it's coming from.
26
+   *
27
+   * @param string $text Text to add.
28
+   */
29
+  public static function add_debugging( $text ) {
30
+    // Make sure we're in debug mode.
31
+    if ( defined( 'WP2D_DEBUGGING' ) && true === WP2D_DEBUGGING ) {
32
+      $d = '';
33
+      foreach ( debug_backtrace() as $dbt ) {
34
+        extract( $dbt );
35
+        // Only trace back as far as the plugin goes.
36
+        if ( strstr( $file, plugin_dir_path( dirname( __FILE__ ) ) ) ) {
37
+          $d = sprintf( "%s%s%s [%s:%s]\n", $class, $type, $function, basename( $file ), $line ) . $d;
38
+        }
39
+      }
40
+
41
+      self::$_debugging .= sprintf( "%s\n%s\n", date( 'Y.m.d H:i:s' ), $d . $text );
42
+
43
+      return true;
44
+    }
45
+    return false;
46
+  }
47
+
48
+  /**
49
+   * Return the debug output.
50
+   *
51
+   * @return string The debug output.
52
+   */
53
+  public static function get_debugging() {
54
+    if ( defined( 'WP2D_DEBUGGING' ) && true === WP2D_DEBUGGING ) {
55
+      return self::$_debugging;
56
+    }
57
+    return false;
58
+  }
59
+
60
+  /**
61
+   * Convert a string with comma seperated values to an array.
62
+   *
63
+   * @todo Make $input by value.
64
+   *
65
+   * @param array|string $input The string to be converted.
66
+   * @return array The converted array.
67
+   */
68
+  public static function str_to_arr( &$input ) {
69
+    if ( ! is_array( $input ) ) {
70
+      // Explode string > Trim each entry > Remove blanks > Re-index array.
71
+      $input = array_values( array_filter( array_map( 'trim', explode( ',', $input ) ) ) );
72
+    } else {
73
+      // If we're already an array, make sure we return it clean.
74
+      self::arr_to_str( $input );
75
+      self::str_to_arr( $input );
76
+    }
77
+    return $input;
78
+  }
79
+
80
+  /**
81
+   * Convert an array to a string with comma seperated values.
82
+   *
83
+   * @todo Make $input by value.
84
+   *
85
+   * @param array|string $input The array to be converted.
86
+   * @return string The converted string.
87
+   */
88
+  public static function arr_to_str( &$input ) {
89
+    if ( is_array( $input ) ) {
90
+      // Trim each entry > Remove blanks > Implode them together.
91
+      $input = implode( ',', array_filter( array_map( 'trim', $input ) ) );
92
+    } else {
93
+      // If we're already a string, make sure we return it clean.
94
+      self::str_to_arr( $input );
95
+      self::arr_to_str( $input );
96
+    }
97
+    return $input;
98
+  }
99
+
100
+  /**
101
+   * Encrypt the passed string with the passed key.
102
+   *
103
+   * @param string $input String to be encrypted.
104
+   * @param string $key   The key used for the encryption.
105
+   * @return string The encrypted string.
106
+   */
107
+  public static function encrypt( $input, $key = AUTH_KEY ) {
108
+    if ( is_null( $input ) || '' === $input ) {
109
+      return false;
110
+    }
111
+    global $wpdb;
112
+    return $wpdb->get_var( $wpdb->prepare( 'SELECT HEX(AES_ENCRYPT(%s,%s))', $input, $key ) );
113
+  }
114
+
115
+  /**
116
+   * Decrypt the passed string with the passed key.
117
+   *
118
+   * @param string $input String to be decrypted.
119
+   * @param string $key   The key used for the decryption.
120
+   * @return string The decrypted string.
121
+   */
122
+  public static function decrypt( $input, $key = AUTH_KEY ) {
123
+    if ( is_null( $input ) || '' === $input ) {
124
+      return false;
125
+    }
126
+    global $wpdb;
127
+    return $wpdb->get_var( $wpdb->prepare( 'SELECT AES_DECRYPT(UNHEX(%s),%s)', $input, $key ) );
128
+  }
129
+
130
+  /**
131
+   * Set up and return an API connection using the currently saved options..
132
+   *
133
+   * @return WP2D_API The API object.
134
+   */
135
+  public static function api_quick_connect() {
136
+    $options   = WP2D_Options::instance();
137
+    $pod       = (string) $options->get_option( 'pod' );
138
+    $is_secure = true;
139
+    $username  = (string) $options->get_option( 'username' );
140
+    $password  = WP2D_Helpers::decrypt( (string) $options->get_option( 'password' ) );
141
+
142
+    $api = new WP2D_API( $pod, $is_secure );
143
+
144
+    // This is necessary for correct error handling!
145
+    if ( $api->init() ) {
146
+      $api->login( $username, $password );
147
+    }
148
+
149
+    if ( $api->has_last_error() ) {
150
+      self::add_debugging( $api->get_last_error() );
151
+    }
152
+
153
+    return $api;
154
+  }
155 155
 }
Please login to merge, or discard this patch.
lib/class-contextual-help.php 1 patch
Indentation   +190 added lines, -190 removed lines patch added patch discarded remove patch
@@ -14,241 +14,241 @@  discard block
 block discarded – undo
14 14
  */
15 15
 class WP2D_Contextual_Help {
16 16
 
17
-	/**
18
-	 * Only instance of this class.
19
-	 *
20
-	 * @var WP2D_Contextual_Help
21
-	 */
22
-	private static $_instance = null;
17
+  /**
18
+   * Only instance of this class.
19
+   *
20
+   * @var WP2D_Contextual_Help
21
+   */
22
+  private static $_instance = null;
23 23
 
24
-	/**
25
-	 * Create / Get the instance of this class.
26
-	 *
27
-	 * @return WP2D_Contextual_Help Instance of this class.
28
-	 */
29
-	public static function instance() {
30
-		if ( ! isset( self::$_instance ) ) {
31
-			self::$_instance = new self();
32
-			self::$_instance->_constants();
33
-			self::$_instance->_setup();
34
-		}
35
-		return self::$_instance;
36
-	}
24
+  /**
25
+   * Create / Get the instance of this class.
26
+   *
27
+   * @return WP2D_Contextual_Help Instance of this class.
28
+   */
29
+  public static function instance() {
30
+    if ( ! isset( self::$_instance ) ) {
31
+      self::$_instance = new self();
32
+      self::$_instance->_constants();
33
+      self::$_instance->_setup();
34
+    }
35
+    return self::$_instance;
36
+  }
37 37
 
38
-	/**
39
-	 * Define all the required constants.
40
-	 *
41
-	 * @since 1.5.0
42
-	 */
43
-	private function _constants() {
44
-		define( 'WP2D_EXT_WPORG',         esc_url( 'https://wordpress.org/plugins/wp-to-diaspora' ) );
45
-		define( 'WP2D_EXT_I18N',          esc_url( 'https://poeditor.com/join/project?hash=c085b3654a5e04c69ec942e0f136716a' ) );
46
-		define( 'WP2D_EXT_GH',            esc_url( 'https://github.com/gutobenn/wp-to-diaspora' ) );
47
-		define( 'WP2D_EXT_DONATE',        esc_url( 'https://github.com/gutobenn/wp-to-diaspora#donate' ) );
48
-		define( 'WP2D_EXT_GH_ISSUES',     esc_url( 'https://github.com/gutobenn/wp-to-diaspora/issues' ) );
49
-		define( 'WP2D_EXT_GH_ISSUES_NEW', esc_url( 'https://github.com/gutobenn/wp-to-diaspora/issues/new' ) );
50
-	}
38
+  /**
39
+   * Define all the required constants.
40
+   *
41
+   * @since 1.5.0
42
+   */
43
+  private function _constants() {
44
+    define( 'WP2D_EXT_WPORG',         esc_url( 'https://wordpress.org/plugins/wp-to-diaspora' ) );
45
+    define( 'WP2D_EXT_I18N',          esc_url( 'https://poeditor.com/join/project?hash=c085b3654a5e04c69ec942e0f136716a' ) );
46
+    define( 'WP2D_EXT_GH',            esc_url( 'https://github.com/gutobenn/wp-to-diaspora' ) );
47
+    define( 'WP2D_EXT_DONATE',        esc_url( 'https://github.com/gutobenn/wp-to-diaspora#donate' ) );
48
+    define( 'WP2D_EXT_GH_ISSUES',     esc_url( 'https://github.com/gutobenn/wp-to-diaspora/issues' ) );
49
+    define( 'WP2D_EXT_GH_ISSUES_NEW', esc_url( 'https://github.com/gutobenn/wp-to-diaspora/issues/new' ) );
50
+  }
51 51
 
52
-	/**
53
-	 * Set up the contextual help menu.
54
-	 */
55
-	private function _setup() {
56
-		// Do we display the help tabs?
57
-		$post_type = get_current_screen()->post_type;
58
-		$enabled_post_types = WP2D_Options::instance()->get_option( 'enabled_post_types' );
59
-		if ( '' !== $post_type && ! in_array( $post_type, $enabled_post_types ) ) {
60
-			return;
61
-		}
52
+  /**
53
+   * Set up the contextual help menu.
54
+   */
55
+  private function _setup() {
56
+    // Do we display the help tabs?
57
+    $post_type = get_current_screen()->post_type;
58
+    $enabled_post_types = WP2D_Options::instance()->get_option( 'enabled_post_types' );
59
+    if ( '' !== $post_type && ! in_array( $post_type, $enabled_post_types ) ) {
60
+      return;
61
+    }
62 62
 
63
-		// If we don't have a post type, we're on the main settings page.
64
-		if ( '' === $post_type ) {
65
-			// Set the sidebar in the contextual help.
66
-			$this->_set_sidebar();
63
+    // If we don't have a post type, we're on the main settings page.
64
+    if ( '' === $post_type ) {
65
+      // Set the sidebar in the contextual help.
66
+      $this->_set_sidebar();
67 67
 
68
-			// Add the main settings tabs and their content.
69
-			$this->_add_settings_help_tabs();
70
-		} else {
71
-			// Add the post type specific tabs and their content.
72
-			$this->_add_post_type_help_tabs();
73
-		}
74
-	}
68
+      // Add the main settings tabs and their content.
69
+      $this->_add_settings_help_tabs();
70
+    } else {
71
+      // Add the post type specific tabs and their content.
72
+      $this->_add_post_type_help_tabs();
73
+    }
74
+  }
75 75
 
76
-	/** Singleton, keep private. */
77
-	final private function __clone() { }
76
+  /** Singleton, keep private. */
77
+  final private function __clone() { }
78 78
 
79
-	/** Singleton, keep private. */
80
-	final private function __wakeup() { }
79
+  /** Singleton, keep private. */
80
+  final private function __wakeup() { }
81 81
 
82
-	/** Singleton, keep private. */
83
-	final private function __construct() { }
82
+  /** Singleton, keep private. */
83
+  final private function __construct() { }
84 84
 
85
-	/**
86
-	 * Set the sidebar in the contextual help.
87
-	 */
88
-	private function _set_sidebar() {
89
-		get_current_screen()->set_help_sidebar(
90
-			'<p><strong>' . esc_html__( 'WP to diaspora*', 'wp-to-diaspora' ) . '</strong></p>
85
+  /**
86
+   * Set the sidebar in the contextual help.
87
+   */
88
+  private function _set_sidebar() {
89
+    get_current_screen()->set_help_sidebar(
90
+      '<p><strong>' . esc_html__( 'WP to diaspora*', 'wp-to-diaspora' ) . '</strong></p>
91 91
 			<ul>
92 92
 				<li><a href="' . WP2D_EXT_GH     . '" target="_blank">GitHub</a>
93 93
 				<li><a href="' . WP2D_EXT_WPORG  . '" target="_blank">WordPress.org</a>
94 94
 				<li><a href="' . WP2D_EXT_I18N   . '" target="_blank">' . esc_html__( 'Help with translations', 'wp-to-diaspora' ) . '</a>
95 95
 				<li><a href="' . WP2D_EXT_DONATE . '" target="_blank">' . esc_html__( 'Make a donation', 'wp-to-diaspora' )        . '</a>
96 96
 			</ul>'
97
-		);
98
-	}
97
+    );
98
+  }
99 99
 
100
-	/**
101
-	 * Add help tabs to the contextual help on the settings page.
102
-	 */
103
-	private function _add_settings_help_tabs() {
104
-		$screen = get_current_screen();
100
+  /**
101
+   * Add help tabs to the contextual help on the settings page.
102
+   */
103
+  private function _add_settings_help_tabs() {
104
+    $screen = get_current_screen();
105 105
 
106
-		// A short overview of the plugin.
107
-		$screen->add_help_tab( array(
108
-			'id'      => 'overview',
109
-			'title'   => esc_html__( 'Overview', 'wp-to-diaspora' ),
110
-			'content' => '<p><strong>' . esc_html__( 'With WP to diaspora*, sharing your WordPress posts to diaspora* is as easy as ever.', 'wp-to-diaspora' ) . '</strong></p>
106
+    // A short overview of the plugin.
107
+    $screen->add_help_tab( array(
108
+      'id'      => 'overview',
109
+      'title'   => esc_html__( 'Overview', 'wp-to-diaspora' ),
110
+      'content' => '<p><strong>' . esc_html__( 'With WP to diaspora*, sharing your WordPress posts to diaspora* is as easy as ever.', 'wp-to-diaspora' ) . '</strong></p>
111 111
 				<ol>
112 112
 					<li>' . esc_html__( 'Enter your diaspora* login details on the "Setup" tab.', 'wp-to-diaspora' ) . '
113 113
 					<li>' . esc_html__( 'Define the default posting behaviour on the "Defaults" tab.', 'wp-to-diaspora' ) . '
114 114
 					<li>' . esc_html__( 'Automatically share your WordPress post on diaspora* when publishing it on your website.', 'wp-to-diaspora' ) . '
115 115
 					<li>' . esc_html__( 'Check out your new post on diaspora*.', 'wp-to-diaspora' ) . '
116 116
 				</ol>'
117
-		) );
117
+    ) );
118 118
 
119
-		// How to set up the connection to diaspora*.
120
-		$screen->add_help_tab( array(
121
-			'id'      => 'setup',
122
-			'title'   => esc_html__( 'Setup', 'wp-to-diaspora' ),
123
-			'content' => '<p><strong>' . esc_html__( 'Enter your diaspora* login details to connect your account.', 'wp-to-diaspora' ) . '</strong></p>
119
+    // How to set up the connection to diaspora*.
120
+    $screen->add_help_tab( array(
121
+      'id'      => 'setup',
122
+      'title'   => esc_html__( 'Setup', 'wp-to-diaspora' ),
123
+      'content' => '<p><strong>' . esc_html__( 'Enter your diaspora* login details to connect your account.', 'wp-to-diaspora' ) . '</strong></p>
124 124
 				<ul>
125 125
 					<li><strong>' . esc_html__( 'diaspora* Pod', 'wp-to-diaspora' ) . '</strong>: ' .
126
-						esc_html__( 'This is the domain name of the pod you are on (e.g. joindiaspora.com)', 'wp-to-diaspora' ) . '<br>
126
+            esc_html__( 'This is the domain name of the pod you are on (e.g. joindiaspora.com)', 'wp-to-diaspora' ) . '<br>
127 127
 							<em>' . sprintf( esc_html__( 'Use the "%s" button to prepopulate the input field to help choose your pod.', 'wp-to-diaspora' ), esc_html__( 'Refresh pod list', 'wp-to-diaspora' ) ) . '</em>
128 128
 					<li><strong>' . esc_html__( 'Username', 'wp-to-diaspora' ) . '</strong>: ' .
129
-						esc_html__( 'Your diaspora* username (without the pod domain).', 'wp-to-diaspora' ) . '
129
+            esc_html__( 'Your diaspora* username (without the pod domain).', 'wp-to-diaspora' ) . '
130 130
 					<li><strong>' . esc_html__( 'Password', 'wp-to-diaspora' ) . '</strong>: ' .
131
-						esc_html__( 'Your diaspora* password.', 'wp-to-diaspora' ) . '
131
+            esc_html__( 'Your diaspora* password.', 'wp-to-diaspora' ) . '
132 132
 				</ul>',
133
-		) );
133
+    ) );
134 134
 
135
-		// Explain the default options and what they do.
136
-		$screen->add_help_tab( array(
137
-			'id'      => 'defaults',
138
-			'title'   => esc_html__( 'Defaults', 'wp-to-diaspora' ),
139
-			'content' => '<p><strong>' . esc_html__( 'Define the default posting behaviour.', 'wp-to-diaspora' ) . '</strong></p>
135
+    // Explain the default options and what they do.
136
+    $screen->add_help_tab( array(
137
+      'id'      => 'defaults',
138
+      'title'   => esc_html__( 'Defaults', 'wp-to-diaspora' ),
139
+      'content' => '<p><strong>' . esc_html__( 'Define the default posting behaviour.', 'wp-to-diaspora' ) . '</strong></p>
140 140
 				<ul>
141 141
 					<li><strong>' . esc_html__( 'Post types', 'wp-to-diaspora' ) . '</strong>: ' .
142
-						esc_html__( 'Choose the post types that are allowed to be shared to diaspora*.', 'wp-to-diaspora' ) . '
142
+            esc_html__( 'Choose the post types that are allowed to be shared to diaspora*.', 'wp-to-diaspora' ) . '
143 143
 					<li><strong>' . esc_html__( 'Post to diaspora*', 'wp-to-diaspora' ) . '</strong>: ' .
144
-						esc_html__( 'Automatically share new posts to diaspora* when publishing them.', 'wp-to-diaspora' ) . '
144
+            esc_html__( 'Automatically share new posts to diaspora* when publishing them.', 'wp-to-diaspora' ) . '
145 145
 					<li><strong>' . esc_html__( 'Show "Posted at" link?', 'wp-to-diaspora' ) . '</strong>: ' .
146
-						esc_html__( 'Add a link back to your original post, at the bottom of the diaspora* post.', 'wp-to-diaspora' ) . '
146
+            esc_html__( 'Add a link back to your original post, at the bottom of the diaspora* post.', 'wp-to-diaspora' ) . '
147 147
 					<li><strong>' . esc_html__( 'Display', 'wp-to-diaspora' ) . '</strong>: ' .
148
-						esc_html__( 'Choose whether you would like to post the whole post or just the excerpt.', 'wp-to-diaspora' ) . '
148
+            esc_html__( 'Choose whether you would like to post the whole post or just the excerpt.', 'wp-to-diaspora' ) . '
149 149
 					<li><strong>' . esc_html__( 'Tags to post', 'wp-to-diaspora' ) . '</strong>: ' .
150
-						esc_html__( 'You can add tags to your post to make it easier to find on diaspora*.' ) . '<br>
150
+            esc_html__( 'You can add tags to your post to make it easier to find on diaspora*.' ) . '<br>
151 151
 							<ul>
152 152
 								<li><strong>' . esc_html__( 'Global tags', 'wp-to-diaspora' ) . '</strong>: ' . esc_html__( 'Tags that apply to all posts.', 'wp-to-diaspora' ) . '
153 153
 								<li><strong>' . esc_html__( 'Custom tags', 'wp-to-diaspora' ) . '</strong>: ' . esc_html__( 'Tags that apply to individual posts (can be set on each post).', 'wp-to-diaspora' ) . '
154 154
 								<li><strong>' . esc_html__( 'Post tags',   'wp-to-diaspora' ) . '</strong>: ' . esc_html__( 'Default WordPress Tags of individual posts.', 'wp-to-diaspora' ) . '
155 155
 							</ul>
156 156
 					<li><strong>' . esc_html__( 'Global tags', 'wp-to-diaspora' ) . '</strong>: ' .
157
-						esc_html__( 'A list of tags that gets added to every post.', 'wp-to-diaspora' ) . '
157
+            esc_html__( 'A list of tags that gets added to every post.', 'wp-to-diaspora' ) . '
158 158
 					<li><strong>' . esc_html__( 'Aspects', 'wp-to-diaspora' ) . '</strong>: ' .
159
-						esc_html__( 'Decide which of your diaspora* aspects can see your posts.', 'wp-to-diaspora' ) . '<br>
159
+            esc_html__( 'Decide which of your diaspora* aspects can see your posts.', 'wp-to-diaspora' ) . '<br>
160 160
 							<em>' . sprintf( esc_html__( 'Use the "%s" button to load your aspects from diaspora*.', 'wp-to-diaspora' ), esc_html__( 'Refresh Aspects', 'wp-to-diaspora' ) ) . '</em>
161 161
 					<li><strong>' . esc_html__( 'Services', 'wp-to-diaspora' ) . '</strong>: ' .
162
-						esc_html__( 'Choose the services your new diaspora* post gets shared to.', 'wp-to-diaspora' ) . '<br>
162
+            esc_html__( 'Choose the services your new diaspora* post gets shared to.', 'wp-to-diaspora' ) . '<br>
163 163
 							<em>' . sprintf( esc_html__( 'Use the "%s" button to fetch the list of your connected services from diaspora*.', 'wp-to-diaspora' ), esc_html__( 'Refresh Services', 'wp-to-diaspora' ) ) . '</em>
164 164
 				</ul>',
165
-		) );
165
+    ) );
166 166
 
167
-		// Explain the importance of SSL connections to the pod and the CA certificate bundle.
168
-		$defined_functions = get_defined_functions();
169
-		$ssl_can_install = ( ! array_diff( array( 'fopen', 'fwrite', 'fclose', 'file_get_contents', 'file_put_contents' ), $defined_functions['internal'] ) );
170
-		$ssl_cert_is_installed = ( file_exists( WP2D_DIR . '/cacert.pem' ) );
167
+    // Explain the importance of SSL connections to the pod and the CA certificate bundle.
168
+    $defined_functions = get_defined_functions();
169
+    $ssl_can_install = ( ! array_diff( array( 'fopen', 'fwrite', 'fclose', 'file_get_contents', 'file_put_contents' ), $defined_functions['internal'] ) );
170
+    $ssl_cert_is_installed = ( file_exists( WP2D_DIR . '/cacert.pem' ) );
171 171
 
172
-		$ssl_install_output = '';
173
-		if ( $ssl_cert_is_installed ) {
174
-			$ssl_install_output = esc_html__( 'Looks like you already have a custom bundle installed!', 'wp-to-diaspora' );
175
-		} elseif ( $ssl_can_install ) {
176
-			$ssl_install_output = sprintf(
177
-				esc_html_x( 'Your server should allow us to %sdo this%s for you :-)', 'Placeholders are HTML for links.', 'wp-to-diaspora' ),
178
-				'<a href="' . add_query_arg( 'wp2d_temp_ssl_fix', '' ) . '" class="button">', '</a>'
179
-			);
180
-		}
181
-		$screen->add_help_tab( array(
182
-			'id'      => 'ssl',
183
-			'title'   => esc_html__( 'SSL', 'wp-to-diaspora' ),
184
-			'content' => '<p><strong>' . esc_html__( 'WP to diaspora* makes sure the connection to your pod is secure!', 'wp-to-diaspora' ) . '</strong></p>
172
+    $ssl_install_output = '';
173
+    if ( $ssl_cert_is_installed ) {
174
+      $ssl_install_output = esc_html__( 'Looks like you already have a custom bundle installed!', 'wp-to-diaspora' );
175
+    } elseif ( $ssl_can_install ) {
176
+      $ssl_install_output = sprintf(
177
+        esc_html_x( 'Your server should allow us to %sdo this%s for you :-)', 'Placeholders are HTML for links.', 'wp-to-diaspora' ),
178
+        '<a href="' . add_query_arg( 'wp2d_temp_ssl_fix', '' ) . '" class="button">', '</a>'
179
+      );
180
+    }
181
+    $screen->add_help_tab( array(
182
+      'id'      => 'ssl',
183
+      'title'   => esc_html__( 'SSL', 'wp-to-diaspora' ),
184
+      'content' => '<p><strong>' . esc_html__( 'WP to diaspora* makes sure the connection to your pod is secure!', 'wp-to-diaspora' ) . '</strong></p>
185 185
 				<p>' . esc_html__( 'Most diaspora* pods are secured using SSL (Secure Sockets Layer), which makes your connection encrypted. For this connection to work, your server needs to know that those SSL certificates can be trusted.', 'wp-to-diaspora' ) . '</p>
186 186
 				<p>' . esc_html__( 'Therefore, if your WordPress installation or server does not have an up to date CA certificate bundle, WP to diaspora* may not work for you.', 'wp-to-diaspora' ) . '</p>
187 187
 				<p>' . esc_html__( 'Lucky for you though, we have you covered if this is the case for you!', 'wp-to-diaspora' ) . '</p>
188 188
 				<ol>
189 189
 					<li><strong>' . esc_html__( 'Check the WordPress certificate bundle', 'wp-to-diaspora' ) . '</strong>: ' .
190
-						esc_html__( 'The best option is to make sure that the WordPress internal certificate bundle is available and accessible. You should be able to find it here: "wp-includes/certificates/ca-bundle.crt".', 'wp-to-diaspora' ) . '
190
+            esc_html__( 'The best option is to make sure that the WordPress internal certificate bundle is available and accessible. You should be able to find it here: "wp-includes/certificates/ca-bundle.crt".', 'wp-to-diaspora' ) . '
191 191
 					<li><strong>' . esc_html__( 'Get in touch with your hosting provider', 'wp-to-diaspora' ) . '</strong>: ' .
192
-						esc_html__( 'Get in touch with your hosting provider and ask them to update the bundle on the server for you. They should know what you\'re talking about.', 'wp-to-diaspora' ) . '
192
+            esc_html__( 'Get in touch with your hosting provider and ask them to update the bundle on the server for you. They should know what you\'re talking about.', 'wp-to-diaspora' ) . '
193 193
 					<li><strong>' . esc_html__( 'Install the CA bundle yourself', 'wp-to-diaspora' ) . '</strong>: ' .
194
-						sprintf(
195
-							esc_html_x( 'If you maintain your own server, it\'s your job to keep the bundle up to date. You can find a short and simple way on how to do this %shere%s.', 'Placeholders are HTML for a link.', 'wp-to-diaspora' ),
196
-							'<a href="http://serverfault.com/a/394835" target="_blank">', '</a>'
197
-						) . '
194
+            sprintf(
195
+              esc_html_x( 'If you maintain your own server, it\'s your job to keep the bundle up to date. You can find a short and simple way on how to do this %shere%s.', 'Placeholders are HTML for a link.', 'wp-to-diaspora' ),
196
+              '<a href="http://serverfault.com/a/394835" target="_blank">', '</a>'
197
+            ) . '
198 198
 					<li><strong>' . esc_html__( 'Quick "temporary" fix', 'wp-to-diaspora' ) . '</strong>: ' .
199
-						sprintf(
200
-							esc_html_x( 'As a temporary solution, you can download the up to date %sCA certificate bundle%s (Right-click &#8594; Save As...) and place the cacert.pem file at the top level of the WP to diaspora* plugin folder. This is a "last resort" option.', 'Placeholders are HTML for links.', 'wp-to-diaspora' ),
201
-							'<a href="http://curl.haxx.se/ca/cacert.pem" download>', '</a>'
202
-						)
203
-						. '<br><p>' .
204
-						$ssl_install_output
205
-						. '</p>
199
+            sprintf(
200
+              esc_html_x( 'As a temporary solution, you can download the up to date %sCA certificate bundle%s (Right-click &#8594; Save As...) and place the cacert.pem file at the top level of the WP to diaspora* plugin folder. This is a "last resort" option.', 'Placeholders are HTML for links.', 'wp-to-diaspora' ),
201
+              '<a href="http://curl.haxx.se/ca/cacert.pem" download>', '</a>'
202
+            )
203
+            . '<br><p>' .
204
+            $ssl_install_output
205
+            . '</p>
206 206
 				</ul>
207 207
 				<p class="dashicons-before dashicons-info">' . esc_html__( 'NOTE: If you choose the temporary option, the copy procedure needs to be done every time the plugin is updated because all files get replaced!', 'wp-to-diaspora' ) . '</p>',
208
-		) );
208
+    ) );
209 209
 
210
-		// Explain the meta box and the differences to the global defaults.
211
-		$screen->add_help_tab( array(
212
-			'id' => 'meta-box',
213
-			'title'   => esc_html__( 'Meta Box', 'wp-to-diaspora' ),
214
-			'content' => '<p><strong>' . esc_html__( 'The Meta Box is the new "WP to diaspora*" box you see when editing a post.', 'wp-to-diaspora' ) . '</strong></p>
210
+    // Explain the meta box and the differences to the global defaults.
211
+    $screen->add_help_tab( array(
212
+      'id' => 'meta-box',
213
+      'title'   => esc_html__( 'Meta Box', 'wp-to-diaspora' ),
214
+      'content' => '<p><strong>' . esc_html__( 'The Meta Box is the new "WP to diaspora*" box you see when editing a post.', 'wp-to-diaspora' ) . '</strong></p>
215 215
 				<p>' . esc_html__( 'When creating or editing a post, you will notice a new meta box called "WP to diaspora*" which has some options. These options are almost the same as the options you can find in the "Defaults" tab on the settings page. These options are post-specific though, meaning they override the global defaults for the post itself. You will see that the default values are filled in automatically, allowing you to change individual ones as you please.', 'wp-to-diaspora' ) . '</p>
216 216
 				<p>' . esc_html__( 'There are a few important differences to the settings page:', 'wp-to-diaspora' ) . '</p>
217 217
 				<ul>
218 218
 					<li><strong>' . esc_html__( 'Already posted to diaspora*', 'wp-to-diaspora' ) . '</strong>: ' .
219
-						esc_html__( 'If the post has already been posted to diaspora* a link to the diaspora* post will appear at the top.', 'wp-to-diaspora' ) . '
219
+            esc_html__( 'If the post has already been posted to diaspora* a link to the diaspora* post will appear at the top.', 'wp-to-diaspora' ) . '
220 220
 					<li><strong>' . esc_html__( 'Custom tags', 'wp-to-diaspora' ) . '</strong>: ' .
221
-						esc_html__( 'A list of tags that gets added to this post. Note that they are seperate from the WordPress post tags!', 'wp-to-diaspora' ) . '
221
+            esc_html__( 'A list of tags that gets added to this post. Note that they are seperate from the WordPress post tags!', 'wp-to-diaspora' ) . '
222 222
 				</ul>
223 223
 				<p class="dashicons-before dashicons-info">' . esc_html__( 'If you don\'t see the meta box, make sure the post type you\'re on has been added to the "Post types" list on the settings page. Also make sure it has been selected from the "Screen Options" at the top of the screen.', 'wp-to-diaspora' ) . '</p>',
224
-		) );
224
+    ) );
225 225
 
226
-		// Troubleshooting.
227
-		$screen->add_help_tab( array(
228
-			'id' => 'troubleshooting',
229
-			'title'   => esc_html__( 'Troubleshooting', 'wp-to-diaspora' ),
230
-			'content' => '<p><strong>' . esc_html__( 'Troubleshooting common errors.', 'wp-to-diaspora' ) . '</strong></p>
226
+    // Troubleshooting.
227
+    $screen->add_help_tab( array(
228
+      'id' => 'troubleshooting',
229
+      'title'   => esc_html__( 'Troubleshooting', 'wp-to-diaspora' ),
230
+      'content' => '<p><strong>' . esc_html__( 'Troubleshooting common errors.', 'wp-to-diaspora' ) . '</strong></p>
231 231
 				<p>' . esc_html__( 'Here are a few common errors and their possible solutions:', 'wp-to-diaspora' ) . '</p>
232 232
 				<ul>
233 233
 					<li><strong>' . esc_html( sprintf( __( 'Failed to initialise connection to pod "%s"', 'wp-to-diaspora' ), 'xyz' ) ) . '</strong>: ' .
234
-						esc_html__( 'This could have multiple reasons.' ) . '
234
+            esc_html__( 'This could have multiple reasons.' ) . '
235 235
 						<ul>
236 236
 							<li>' . esc_html__( 'Make sure that your pod domain is entered correctly.', 'wp-to-diaspora' ) . '
237 237
 							<li>' .
238
-								esc_html__( 'It might be an SSL problem.', 'wp-to-diaspora' ) .
239
-								sprintf( ' <a href="#" class="open-help-tab" data-help-tab="ssl">%s</a>', esc_html__( 'Learn More', 'wp-to-diaspora' ) ) . '
238
+                esc_html__( 'It might be an SSL problem.', 'wp-to-diaspora' ) .
239
+                sprintf( ' <a href="#" class="open-help-tab" data-help-tab="ssl">%s</a>', esc_html__( 'Learn More', 'wp-to-diaspora' ) ) . '
240 240
 							<li>' . esc_html__( 'The pod might be offline at the moment.', 'wp-to-diaspora' ) . '
241 241
 						</ul>
242 242
 					<li><strong>' . esc_html__( 'Login failed. Check your login details.', 'wp-to-diaspora' ) . '</strong>: ' .
243
-						esc_html__( 'Make sure that your username and password are entered correctly.', 'wp-to-diaspora' ) . '
243
+            esc_html__( 'Make sure that your username and password are entered correctly.', 'wp-to-diaspora' ) . '
244 244
 				</ul>',
245
-		) );
245
+    ) );
246 246
 
247
-		// Show different ways to contribute to the plugin.
248
-		$screen->add_help_tab( array(
249
-			'id' => 'contributing',
250
-			'title'   => esc_html__( 'Contributing', 'wp-to-diaspora' ),
251
-			'content' => '<p><strong>' . esc_html__( 'So you feel like contributing to the WP to diaspora* plugin? Great!', 'wp-to-diaspora' ) . '</strong></p>
247
+    // Show different ways to contribute to the plugin.
248
+    $screen->add_help_tab( array(
249
+      'id' => 'contributing',
250
+      'title'   => esc_html__( 'Contributing', 'wp-to-diaspora' ),
251
+      'content' => '<p><strong>' . esc_html__( 'So you feel like contributing to the WP to diaspora* plugin? Great!', 'wp-to-diaspora' ) . '</strong></p>
252 252
 				<p>' . esc_html__( 'There are many different ways that you can help out with this plugin:', 'wp-to-diaspora' ) . '</p>
253 253
 				<ul>
254 254
 					<li><a href="' . WP2D_EXT_GH_ISSUES_NEW . '" target="_blank">' . esc_html__( 'Report a bug', 'wp-to-diaspora' )           . '</a>
@@ -256,42 +256,42 @@  discard block
 block discarded – undo
256 256
 					<li><a href="' . WP2D_EXT_I18N          . '" target="_blank">' . esc_html__( 'Help with translations', 'wp-to-diaspora' ) . '</a>
257 257
 					<li><a href="' . WP2D_EXT_DONATE        . '" target="_blank">' . esc_html__( 'Make a donation', 'wp-to-diaspora' )        . '</a>
258 258
 				</ul>',
259
-		) );
260
-	}
259
+    ) );
260
+  }
261 261
 
262
-	/**
263
-	 * Add help tabs to the contextual help on the post pages.
264
-	 */
265
-	private function _add_post_type_help_tabs() {
266
-		get_current_screen()->add_help_tab( array(
267
-			'id'       => 'wp-to-diaspora',
268
-			'title'    => esc_html__( 'WP to diaspora*', 'wp-to-diaspora' ),
269
-			'content'  => '<p>' . sprintf(
270
-				esc_html__( 'For detailed information, refer to the contextual help on the %sWP to diaspora*%s settings page.', 'Placeholders represent the link.', 'wp-to-diaspora' ),
271
-				'<a href="options-general.php?page=wp_to_diaspora" target="_blank">', '</a>'
272
-			) . '</p>',
273
-		) );
274
-	}
262
+  /**
263
+   * Add help tabs to the contextual help on the post pages.
264
+   */
265
+  private function _add_post_type_help_tabs() {
266
+    get_current_screen()->add_help_tab( array(
267
+      'id'       => 'wp-to-diaspora',
268
+      'title'    => esc_html__( 'WP to diaspora*', 'wp-to-diaspora' ),
269
+      'content'  => '<p>' . sprintf(
270
+        esc_html__( 'For detailed information, refer to the contextual help on the %sWP to diaspora*%s settings page.', 'Placeholders represent the link.', 'wp-to-diaspora' ),
271
+        '<a href="options-general.php?page=wp_to_diaspora" target="_blank">', '</a>'
272
+      ) . '</p>',
273
+    ) );
274
+  }
275 275
 
276
-	/**
277
-	 * Get a link that directly opens a help tab via JS.
278
-	 *
279
-	 * @since 1.6.0
280
-	 *
281
-	 * @param WP_Error|string $error The WP_Error object with the tab id as data or the tab id itself.
282
-	 * @return string HTML link.
283
-	 */
284
-	public static function get_help_tab_quick_link( $error ) {
285
-		$help_tab = '';
286
-		if ( is_wp_error( $error ) && ( $error_data = $error->get_error_data() ) && array_key_exists( 'help_tab', $error_data ) ) {
287
-			$help_tab = $error_data['help_tab'];
288
-		} elseif ( is_string( $error ) ) {
289
-			$help_tab = $error;
290
-		}
291
-		if ( '' !== $help_tab ) {
292
-			return sprintf( '<a href="#" class="open-help-tab" data-help-tab="%1$s">%2$s</a>', $help_tab, esc_html__( 'Help', 'wp-to-diaspora' ) );
293
-		}
276
+  /**
277
+   * Get a link that directly opens a help tab via JS.
278
+   *
279
+   * @since 1.6.0
280
+   *
281
+   * @param WP_Error|string $error The WP_Error object with the tab id as data or the tab id itself.
282
+   * @return string HTML link.
283
+   */
284
+  public static function get_help_tab_quick_link( $error ) {
285
+    $help_tab = '';
286
+    if ( is_wp_error( $error ) && ( $error_data = $error->get_error_data() ) && array_key_exists( 'help_tab', $error_data ) ) {
287
+      $help_tab = $error_data['help_tab'];
288
+    } elseif ( is_string( $error ) ) {
289
+      $help_tab = $error;
290
+    }
291
+    if ( '' !== $help_tab ) {
292
+      return sprintf( '<a href="#" class="open-help-tab" data-help-tab="%1$s">%2$s</a>', $help_tab, esc_html__( 'Help', 'wp-to-diaspora' ) );
293
+    }
294 294
 
295
-		return '';
296
-	}
295
+    return '';
296
+  }
297 297
 }
Please login to merge, or discard this patch.
lib/class-post.php 1 patch
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.