WP2D_Contextual_Help   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 283
Duplicated Lines 2.83 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 1
dl 8
loc 283
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __wakeup() 0 1 1
A __clone() 0 1 1
A __construct() 0 1 1
A instance() 8 8 2
A _constants() 0 8 1
A _setup() 0 20 4
A _set_sidebar() 0 11 1
B _add_settings_help_tabs() 0 158 3
A _add_post_type_help_tabs() 0 10 1
B get_help_tab_quick_link() 0 13 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 15 and the first side effect is on line 10.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Plugin Contextual Help.
4
 *
5
 * @package WP_To_Diaspora\Help
6
 * @since 1.4.0
7
 */
8
9
// Exit if accessed directly.
10
defined( 'ABSPATH' ) || exit;
11
12
/**
13
 * Class that handles the contextual help.
14
 */
15
class WP2D_Contextual_Help {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
16
17
	/**
18
	 * Only instance of this class.
19
	 *
20
	 * @var WP2D_Contextual_Help
21
	 */
22
	private static $_instance = null;
23
24
	/**
25
	 * Create / Get the instance of this class.
26
	 *
27
	 * @return WP2D_Contextual_Help Instance of this class.
28
	 */
29 View Code Duplication
	public static function instance() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
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
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
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
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
76
	/** Singleton, keep private. */
77
	final private function __clone() { }
78
79
	/** Singleton, keep private. */
80
	final private function __wakeup() { }
81
82
	/** Singleton, keep private. */
83
	final private function __construct() { }
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>
91
			<ul>
92
				<li><a href="' . WP2D_EXT_GH     . '" target="_blank">GitHub</a>
93
				<li><a href="' . WP2D_EXT_WPORG  . '" target="_blank">WordPress.org</a>
94
				<li><a href="' . WP2D_EXT_I18N   . '" target="_blank">' . esc_html__( 'Help with translations', 'wp-to-diaspora' ) . '</a>
95
				<li><a href="' . WP2D_EXT_DONATE . '" target="_blank">' . esc_html__( 'Make a donation', 'wp-to-diaspora' )        . '</a>
96
			</ul>'
97
		);
98
	}
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();
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>
111
				<ol>
112
					<li>' . esc_html__( 'Enter your diaspora* login details on the "Setup" tab.', 'wp-to-diaspora' ) . '
113
					<li>' . esc_html__( 'Define the default posting behaviour on the "Defaults" tab.', 'wp-to-diaspora' ) . '
114
					<li>' . esc_html__( 'Automatically share your WordPress post on diaspora* when publishing it on your website.', 'wp-to-diaspora' ) . '
115
					<li>' . esc_html__( 'Check out your new post on diaspora*.', 'wp-to-diaspora' ) . '
116
				</ol>'
117
		) );
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>
124
				<ul>
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>
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
					<li><strong>' . esc_html__( 'Username', 'wp-to-diaspora' ) . '</strong>: ' .
129
						esc_html__( 'Your diaspora* username (without the pod domain).', 'wp-to-diaspora' ) . '
130
					<li><strong>' . esc_html__( 'Password', 'wp-to-diaspora' ) . '</strong>: ' .
131
						esc_html__( 'Your diaspora* password.', 'wp-to-diaspora' ) . '
132
				</ul>',
133
		) );
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>
140
				<ul>
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' ) . '
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' ) . '
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' ) . '
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' ) . '
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>
151
							<ul>
152
								<li><strong>' . esc_html__( 'Global tags', 'wp-to-diaspora' ) . '</strong>: ' . esc_html__( 'Tags that apply to all posts.', 'wp-to-diaspora' ) . '
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
								<li><strong>' . esc_html__( 'Post tags',   'wp-to-diaspora' ) . '</strong>: ' . esc_html__( 'Default WordPress Tags of individual posts.', 'wp-to-diaspora' ) . '
155
							</ul>
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' ) . '
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>
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
					<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>
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
				</ul>',
165
		) );
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' ) );
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>
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
				<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
				<p>' . esc_html__( 'Lucky for you though, we have you covered if this is the case for you!', 'wp-to-diaspora' ) . '</p>
188
				<ol>
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' ) . '
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' ) . '
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
						) . '
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>
206
				</ul>
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
		) );
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>
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
				<p>' . esc_html__( 'There are a few important differences to the settings page:', 'wp-to-diaspora' ) . '</p>
217
				<ul>
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' ) . '
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' ) . '
222
				</ul>
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
		) );
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>
231
				<p>' . esc_html__( 'Here are a few common errors and their possible solutions:', 'wp-to-diaspora' ) . '</p>
232
				<ul>
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.' ) . '
235
						<ul>
236
							<li>' . esc_html__( 'Make sure that your pod domain is entered correctly.', 'wp-to-diaspora' ) . '
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' ) ) . '
240
							<li>' . esc_html__( 'The pod might be offline at the moment.', 'wp-to-diaspora' ) . '
241
						</ul>
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' ) . '
244
				</ul>',
245
		) );
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>
252
				<p>' . esc_html__( 'There are many different ways that you can help out with this plugin:', 'wp-to-diaspora' ) . '</p>
253
				<ul>
254
					<li><a href="' . WP2D_EXT_GH_ISSUES_NEW . '" target="_blank">' . esc_html__( 'Report a bug', 'wp-to-diaspora' )           . '</a>
255
					<li><a href="' . WP2D_EXT_GH_ISSUES_NEW . '" target="_blank">' . esc_html__( 'Suggest a new feature', 'wp-to-diaspora' )  . '</a>
256
					<li><a href="' . WP2D_EXT_I18N          . '" target="_blank">' . esc_html__( 'Help with translations', 'wp-to-diaspora' ) . '</a>
257
					<li><a href="' . WP2D_EXT_DONATE        . '" target="_blank">' . esc_html__( 'Make a donation', 'wp-to-diaspora' )        . '</a>
258
				</ul>',
259
		) );
260
	}
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
	}
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
		}
294
295
		return '';
296
	}
297
}
298