Completed
Push — add/jetpack-data-methods ( d7e5a6...2c93cb )
by
unknown
07:30
created

Jetpack_Post_By_Email::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Module Name: Post by email
5
 * Module Description: Publish posts by sending an email
6
 * First Introduced: 2.0
7
 * Sort Order: 14
8
 * Requires Connection: Yes
9
 * Auto Activate: No
10
 * Module Tags: Writing
11
 * Feature: Writing
12
 * Additional Search Queries: post by email, email
13
 */
14
15
add_action( 'jetpack_modules_loaded', array( 'Jetpack_Post_By_Email', 'init' ) );
16
17
Jetpack::enable_module_configurable( __FILE__ );
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
	function action_init() {
35
		if ( ! current_user_can( 'edit_posts' ) )
36
			return;
37
38
		add_action( 'profile_personal_options', array( &$this, 'user_profile' ) );
39
		add_action( 'admin_print_scripts-profile.php', array( &$this, 'profile_scripts' ) );
40
41
		add_action( 'wp_ajax_jetpack_post_by_email_enable', array( &$this, 'create_post_by_email_address' ) );
42
		add_action( 'wp_ajax_jetpack_post_by_email_regenerate', array( &$this, 'regenerate_post_by_email_address' ) );
43
		add_action( 'wp_ajax_jetpack_post_by_email_disable', array( &$this, 'delete_post_by_email_address' ) );
44
	}
45
46
	function profile_scripts() {
47
		wp_enqueue_script( 'post-by-email', plugins_url( 'post-by-email/post-by-email.js', __FILE__ ), array( 'jquery' ) );
48
		wp_localize_script( 'post-by-email', 'pbeVars', array(
49
			'nonces' => array(
50
				'enable'     => wp_create_nonce( 'jetpack.createPostByEmailAddress' ),
51
				'regenerate' => wp_create_nonce( 'jetpack.regeneratePostByEmailAddress' ),
52
				'disable'    => wp_create_nonce( 'jetpack.deletePostByEmailAddress' ),
53
			),
54
		));
55
		wp_enqueue_style( 'post-by-email', plugins_url( 'post-by-email/post-by-email.css', __FILE__ ) );
56
		wp_style_add_data( 'post-by-email', 'jetpack-inline', true );
57
		// Do we really need `admin_styles`? With the new admin UI, it's breaking some bits.
58
		// Jetpack::init()->admin_styles();
59
	}
60
61
	function check_user_connection() {
62
		$user_token = Jetpack_Data::get_access_token( get_current_user_id() );
0 ignored issues
show
Deprecated Code introduced by
The method Jetpack_Data::get_access_token() has been deprecated with message: 7.5 Use Connection_Manager instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
63
		$is_user_connected = $user_token && !is_wp_error( $user_token );
64
65
		// If the user is already connected via Jetpack, then we're good
66
		if ( $is_user_connected )
67
			return true;
68
69
		return false;
70
	}
71
72
	function user_profile() {
73
		$blog_name = get_bloginfo( 'blogname' );
74
		if ( empty( $blog_name ) ) {
75
			$blog_name = home_url( '/' );
76
		}
77
78
		?>
79
		<div id="post-by-email" class="jetpack-targetable">
80
		<h3><?php esc_html_e( 'Post by Email', 'jetpack' ); ?></h3>
81
		<table class="form-table">
82
			<tr>
83
				<th scope="row"><?php esc_html_e( 'Email Address', 'jetpack' ); ?><span id="jp-pbe-spinner" class="spinner"></span></th>
84
				<td>
85
				<div id="jp-pbe-error" class="jetpack-inline-error"></div> <?php
86
87
				if ( $this->check_user_connection() ) {
88
					$email = $this->get_post_by_email_address();
89
90
					if ( empty( $email ) ) {
91
						$enable_hidden = '';
92
						$info_hidden = ' style="display: none;"';
93
					} else {
94
						$enable_hidden = ' style="display: none;"';
95
						$info_hidden = '';
96
					} ?>
97
98
					<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; ?> />
99
					<div id="jp-pbe-info"<?php echo $info_hidden; ?>>
100
						<p id="jp-pbe-email-wrapper">
101
							<input type="text" id="jp-pbe-email" value="<?php echo esc_attr( $email ); ?>" readonly="readonly" class="regular-text" />
102
							<span class="description"><a target="_blank" href="http://jetpack.com/support/post-by-email/"><?php esc_html_e( 'More information', 'jetpack' ); ?></a></span>
103
						</p>
104
						<p>
105
							<input type="button" name="jp-pbe-regenerate" id="jp-pbe-regenerate" class="button" value="<?php esc_attr_e( 'Regenerate Address', 'jetpack' ); ?> " />
106
							<input type="button" name="jp-pbe-disable" id="jp-pbe-disable" class="button" value="<?php esc_attr_e( 'Disable Post By Email', 'jetpack' ); ?> " />
107
						</p>
108
					</div> <?php
109
				} else {
110
					$jetpack = Jetpack::init(); ?>
111
112
					<p class="jetpack-inline-message">
113
						<?php printf(
114
							esc_html( wptexturize( __( 'To use Post By Email, you need to link your %s account to your WordPress.com account.', 'jetpack' ) ) ),
115
							'<strong>' . esc_html( $blog_name ) . '</strong>'
116
						); ?><br />
117
						<?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' ) ) ); ?>
118
					</p>
119
					<p>
120
						<a href="<?php echo $jetpack->build_connect_url( false, get_edit_profile_url( get_current_user_id() ) . '#post-by-email', 'unlinked-user-pbe' ); ?>" class="button button-connector" id="wpcom-connect"><?php esc_html_e( 'Link account with WordPress.com', 'jetpack' ); ?></a>
121
					</p>
122
					<?php
123
				} ?>
124
				</td>
125
			</tr>
126
		</table>
127
		</div>
128
	<?php
129
	}
130
131 View Code Duplication
	function get_post_by_email_address() {
132
		Jetpack::load_xml_rpc_client();
133
		$xml = new Jetpack_IXR_Client( array(
134
			'user_id' => get_current_user_id(),
135
		) );
136
		$xml->query( 'jetpack.getPostByEmailAddress' );
137
138
		if ( $xml->isError() )
139
			return NULL;
140
141
		$response = $xml->getResponse();
142
		if ( empty( $response ) )
143
			return NULL;
144
145
		return $response;
146
	}
147
148
	function create_post_by_email_address() {
149
		self::__process_ajax_proxy_request(
150
			'jetpack.createPostByEmailAddress',
151
			__( 'Unable to create your Post By Email address. Please try again later.', 'jetpack' )
152
		);
153
	}
154
155
	function regenerate_post_by_email_address() {
156
		self::__process_ajax_proxy_request(
157
			'jetpack.regeneratePostByEmailAddress',
158
			__( 'Unable to regenerate your Post By Email address. Please try again later.', 'jetpack' )
159
		);
160
	}
161
162
	function delete_post_by_email_address() {
163
		self::__process_ajax_proxy_request(
164
			'jetpack.deletePostByEmailAddress',
165
			__( 'Unable to disable your Post By Email address. Please try again later.', 'jetpack' )
166
		);
167
	}
168
169
	/**
170
	 * Back end function to abstract the xmlrpc function calls to wpcom.
171
	 *
172
	 * @param $endpoint
173
	 * @param $error_message
174
	 */
175
	function __process_ajax_proxy_request( $endpoint, $error_message ) { // phpcs:ignore
176
		if ( ! current_user_can( 'edit_posts' ) ) {
177
			wp_send_json_error( $error_message );
178
		}
179
		if ( empty( $_REQUEST['pbe_nonce'] ) || ! wp_verify_nonce( $_REQUEST['pbe_nonce'], $endpoint ) ) {
180
			wp_send_json_error( $error_message );
181
		}
182
		Jetpack::load_xml_rpc_client();
183
		$xml = new Jetpack_IXR_Client( array(
184
			'user_id' => get_current_user_id(),
185
		) );
186
		$xml->query( $endpoint );
187
188
		if ( $xml->isError() ) {
189
			wp_send_json_error( $error_message );
190
		}
191
192
		$response = $xml->getResponse();
193
		if ( empty( $response ) ) {
194
			wp_send_json_error( $error_message );
195
		}
196
197
		// Will be used only in Jetpack_Core_Json_Api_Endpoints::get_remote_value.
198
		update_option( 'post_by_email_address' . get_current_user_id(), $response );
199
200
		wp_send_json_success( $response );
201
	}
202
}
203