Completed
Push — add/sync-action ( abe827...b1c6ef )
by
unknown
18:07 queued 09:09
created

Jetpack_Post_By_Email   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 211
Duplicated Lines 45.97 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 26
c 1
b 0
f 0
lcom 1
cbo 3
dl 97
loc 211
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 9 2
A __construct() 0 3 1
A configuration_redirect() 0 4 1
A action_init() 0 11 2
A profile_scripts() 0 7 1
A check_user_connection() 0 10 3
A user_profile() 0 58 4
A get_post_by_email_address() 16 16 3
B create_post_by_email_address() 27 27 3
B regenerate_post_by_email_address() 27 27 3
B delete_post_by_email_address() 27 27 3

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
2
3
/**
4
 * Module Name: Post by Email
5
 * Module Description: Publish posts by email, using any device and email client.
6
 * First Introduced: 2.0
7
 * Sort Order: 14
8
 * Requires Connection: Yes
9
 * Auto Activate: Yes
10
 * Module Tags: Writing
11
 * Additional Search Queries: post by email, email
12
 */
13
14
add_action( 'jetpack_modules_loaded', array( 'Jetpack_Post_By_Email', 'init' ) );
15
16
Jetpack::enable_module_configurable( __FILE__ );
17
Jetpack::module_configuration_load( __FILE__, array( 'Jetpack_Post_By_Email', 'configuration_redirect' ) );
18
19
class Jetpack_Post_By_Email {
20
	public static function init() {
21
		static $instance = NULL;
22
23
		if ( !$instance ) {
24
			$instance = new Jetpack_Post_By_Email;
25
		}
26
27
		return $instance;
28
	}
29
30
	function __construct() {
31
		add_action( 'init', array( &$this, 'action_init' ) );
32
	}
33
34
	static function configuration_redirect() {
35
		wp_safe_redirect( get_edit_profile_url( get_current_user_id() ) . '#post-by-email' );
36
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method configuration_redirect() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
37
	}
38
39
	function action_init() {
40
		if ( ! current_user_can( 'edit_posts' ) )
41
			return;
42
43
		add_action( 'profile_personal_options', array( &$this, 'user_profile' ) );
44
		add_action( 'admin_print_scripts-profile.php', array( &$this, 'profile_scripts' ) );
45
46
		add_action( 'wp_ajax_jetpack_post_by_email_enable', array( &$this, 'create_post_by_email_address' ) );
47
		add_action( 'wp_ajax_jetpack_post_by_email_regenerate', array( &$this, 'regenerate_post_by_email_address' ) );
48
		add_action( 'wp_ajax_jetpack_post_by_email_disable', array( &$this, 'delete_post_by_email_address' ) );
49
	}
50
51
	function profile_scripts() {
52
		wp_enqueue_script( 'post-by-email', plugins_url( 'post-by-email/post-by-email.js', __FILE__ ), array( 'jquery' ) );
53
		wp_enqueue_style( 'post-by-email', plugins_url( 'post-by-email/post-by-email.css', __FILE__ ) );
54
		wp_style_add_data( 'post-by-email', 'jetpack-inline', true );
55
		// Do we really need `admin_styles`? With the new admin UI, it's breaking some bits.
56
		// Jetpack::init()->admin_styles();
57
	}
58
59
	function check_user_connection() {
60
		$user_token = Jetpack_Data::get_access_token( get_current_user_id() );
61
		$is_user_connected = $user_token && !is_wp_error( $user_token );
62
63
		// If the user is already connected via Jetpack, then we're good
64
		if ( $is_user_connected )
65
			return true;
66
67
		return false;
68
	}
69
70
	function user_profile() {
71
		$blog_name = get_bloginfo( 'blogname' );
72
		if ( empty( $blog_name ) ) {
73
			$blog_name = home_url( '/' );
74
		}
75
76
		?>
77
		<div id="post-by-email" class="jetpack-targetable">
78
		<h3><?php esc_html_e( 'Post by Email', 'jetpack' ); ?></h3>
79
		<table class="form-table">
80
			<tr>
81
				<th scope="row"><?php esc_html_e( 'Email Address', 'jetpack' ); ?><span id="jp-pbe-spinner" class="spinner"></span></th>
82
				<td>
83
				<div id="jp-pbe-error" class="jetpack-inline-error"></div> <?php
84
85
				if ( $this->check_user_connection() ) {
86
					$email = $this->get_post_by_email_address();
87
88
					if ( empty( $email ) ) {
89
						$enable_hidden = '';
90
						$info_hidden = ' style="display: none;"';
91
					} else {
92
						$enable_hidden = ' style="display: none;"';
93
						$info_hidden = '';
94
					} ?>
95
96
					<input type="button" name="jp-pbe-enable" id="jp-pbe-enable" class="button" value="<?php esc_attr_e( 'Enable Post By Email', 'jetpack' ); ?> "<?php echo $enable_hidden; ?> />
97
					<div id="jp-pbe-info"<?php echo $info_hidden; ?>>
98
						<p id="jp-pbe-email-wrapper">
99
							<input type="text" id="jp-pbe-email" value="<?php echo esc_attr( $email ); ?>" readonly="readonly" class="regular-text" />
100
							<span class="description"><a target="_blank" href="http://jetpack.com/support/post-by-email/"><?php esc_html_e( 'More information', 'jetpack' ); ?></a></span>
101
						</p>
102
						<p>
103
							<input type="button" name="jp-pbe-regenerate" id="jp-pbe-regenerate" class="button" value="<?php esc_attr_e( 'Regenerate Address', 'jetpack' ); ?> " />
104
							<input type="button" name="jp-pbe-disable" id="jp-pbe-disable" class="button" value="<?php esc_attr_e( 'Disable Post By Email', 'jetpack' ); ?> " />
105
						</p>
106
					</div> <?php
107
				} else {
108
					$jetpack = Jetpack::init(); ?>
109
110
					<p class="jetpack-inline-message">
111
						<?php printf(
112
							esc_html( wptexturize( __( 'To use Post By Email, you need to link your %s account to your WordPress.com account.', 'jetpack' ) ) ),
113
							'<strong>' . esc_html( $blog_name ) . '</strong>'
114
						); ?><br />
115
						<?php echo esc_html( wptexturize( __( "If you don't have a WordPress.com account yet, you can sign up for free in just a few seconds.", 'jetpack' ) ) ); ?>
116
					</p>
117
					<p>
118
						<a href="<?php echo $jetpack->build_connect_url( false, get_edit_profile_url( get_current_user_id() ) . '#post-by-email' ); ?>" class="button button-connector" id="wpcom-connect"><?php esc_html_e( 'Link account with WordPress.com', 'jetpack' ); ?></a>
119
					</p>
120
					<?php
121
				} ?>
122
				</td>
123
			</tr>
124
		</table>
125
		</div>
126
	<?php
127
	}
128
129 View Code Duplication
	function get_post_by_email_address() {
130
		Jetpack::load_xml_rpc_client();
131
		$xml = new Jetpack_IXR_Client( array(
132
			'user_id' => get_current_user_id(),
133
		) );
134
		$xml->query( 'jetpack.getPostByEmailAddress' );
135
136
		if ( $xml->isError() )
137
			return NULL;
138
139
		$response = $xml->getResponse();
140
		if ( empty( $response ) )
141
			return NULL;
142
143
		return $response;
144
	}
145
146 View Code Duplication
	function create_post_by_email_address() {
147
		Jetpack::load_xml_rpc_client();
148
		$xml = new Jetpack_IXR_Client( array(
149
			'user_id' => get_current_user_id(),
150
		) );
151
		$xml->query( 'jetpack.createPostByEmailAddress' );
152
153
		if ( $xml->isError() ) {
154
			echo json_encode( array(
155
				'response' => 'error',
156
				'message' => __( 'Unable to create your Post By Email address. Please try again later.', 'jetpack' )
157
			) );
158
			die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method create_post_by_email_address() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
159
		}
160
161
		$response = $xml->getResponse();
162
		if ( empty( $response ) ) {
163
			echo json_encode( array(
164
				'response' => 'error',
165
				'message' => __( 'Unable to create your Post By Email address. Please try again later.', 'jetpack' )
166
			) );
167
			die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method create_post_by_email_address() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
168
		}
169
170
		echo $response;
171
		die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method create_post_by_email_address() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
172
	}
173
174 View Code Duplication
	function regenerate_post_by_email_address() {
175
		Jetpack::load_xml_rpc_client();
176
		$xml = new Jetpack_IXR_Client( array(
177
			'user_id' => get_current_user_id(),
178
		) );
179
		$xml->query( 'jetpack.regeneratePostByEmailAddress' );
180
181
		if ( $xml->isError() ) {
182
			echo json_encode( array(
183
				'response' => 'error',
184
				'message' => __( 'Unable to regenerate your Post By Email address. Please try again later.', 'jetpack' )
185
			) );
186
			die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method regenerate_post_by_email_address() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
187
		}
188
189
		$response = $xml->getResponse();
190
		if ( empty( $response ) ) {
191
			echo json_encode( array(
192
				'response' => 'error',
193
				'message' => __( 'Unable to regenerate your Post By Email address. Please try again later.', 'jetpack' )
194
			) );
195
			die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method regenerate_post_by_email_address() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
196
		}
197
198
		echo $response;
199
		die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method regenerate_post_by_email_address() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
200
	}
201
202 View Code Duplication
	function delete_post_by_email_address() {
203
		Jetpack::load_xml_rpc_client();
204
		$xml = new Jetpack_IXR_Client( array(
205
			'user_id' => get_current_user_id(),
206
		) );
207
		$xml->query( 'jetpack.deletePostByEmailAddress' );
208
209
		if ( $xml->isError() ) {
210
			echo json_encode( array(
211
				'response' => 'error',
212
				'message' => __( 'Unable to disable your Post By Email address. Please try again later.', 'jetpack' )
213
			) );
214
			die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method delete_post_by_email_address() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
215
		}
216
217
		$response = $xml->getResponse();
218
		if ( empty( $response ) ) {
219
			echo json_encode( array(
220
				'response' => 'error',
221
				'message' => __( 'Unable to disable your Post By Email address. Please try again later.', 'jetpack' )
222
			) );
223
			die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method delete_post_by_email_address() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
224
		}
225
226
		echo $response;
227
		die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method delete_post_by_email_address() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
228
	}
229
}
230