Completed
Push — sharedaddy/email-from ( c6339b )
by George
09:07
created

sharedaddy.php ➔ sharing_meta_box_content()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/*
3
Plugin Name: Sharedaddy
4
Description: The most super duper sharing tool on the interwebs.
5
Version: 0.3.1
6
Author: Automattic, Inc.
7
Author URI: http://automattic.com/
8
Plugin URI: http://en.blog.wordpress.com/2010/08/24/more-ways-to-share/
9
*/
10
11
require_once plugin_dir_path( __FILE__ ).'sharing.php';
12
13
function sharing_email_send_post( $data ) {
14
15
	$content = sharing_email_send_post_content( $data );
16
	// Borrowed from wp_mail();
17
	$sitename = strtolower( $_SERVER['SERVER_NAME'] );
18
	if ( substr( $sitename, 0, 4 ) == 'www.' ) {
19
		$sitename = substr( $sitename, 4 );
20
	}
21
22
	/** This filter is documented in core/src/wp-includes/pluggable.php */
23
	$from_email = apply_filters( 'wp_mail_from', 'wordpress@' . $sitename );
24
25
	if ( ! empty( $data['name'] ) ) {
26
		$s_name = (string) $data['name'];
27
		$name_needs_encoding_regex =
28
			'/[' .
29
				// SpamAssasin's list of characters which "need MIME" encoding
30
				'\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\xff' .
31
				// Our list of "unsafe" characters
32
				'<\r\n' .
33
			']/';
34
35
		$needs_encoding = preg_match( $name_needs_encoding_regex, $s_name ) ||      // If it contains any blacklisted chars,
36
				! function_exists( 'mb_convert_encoding' ) ||           // Or if we can't use `mb_convert_encoding` encode everything,
37
				mb_convert_encoding( $data['name'], 'ASCII' ) !== $s_name // Or if it's not already ASCII
38
			);
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ')'
Loading history...
39
40
		if ( $needs_encoding ) {
41
			$data['name'] = sprintf( '=?UTF-8?B?%s?=', base64_encode( $data['name'] ) );
42
		}
43
	}
44
45
	$headers[] = sprintf( 'From: %1$s <%2$s>', $data['name'], $from_email );
46
	$headers[] = sprintf( 'Reply-To: %1$s <%2$s>', $data['name'], $data['source'] );
47
48
	// Make sure to pass the title through the normal sharing filters.
49
	$title = $data['sharing_source']->get_share_title( $data['post']->ID );
50
51
	wp_mail( $data['target'], '[' . __( 'Shared Post', 'jetpack' ) . '] ' . $title, $content, $headers );
52
}
53
54
55
/* Checks for spam using akismet if available. */
56
/* Return $data as it if email about to be send out is not spam. */
57
function sharing_email_check_for_spam_via_akismet( $data ) {
58
59
	if ( ! function_exists( 'akismet_http_post' ) && ! method_exists( 'Akismet', 'http_post' ) )
60
		return $data;
61
62
	// Prepare the body_request for akismet
63
	$body_request = array(
64
		'blog'                  => get_option( 'home' ),
65
		'permalink'             => $data['sharing_source']->get_share_url( $data['post']->ID ),
66
		'comment_type'          => 'share',
67
		'comment_author'        => $data['name'],
68
		'comment_author_email'  => $data['source'],
69
		'comment_content'       => sharing_email_send_post_content( $data ),
70
		'user_agent'            => ( isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : null ),
71
		);
72
73
	if ( method_exists( 'Akismet', 'http_post' ) ) {
74
		$body_request['user_ip']	= Akismet::get_ip_address();
75
		$response = Akismet::http_post( build_query( $body_request ), 'comment-check' );
76
	} else {
77
		global $akismet_api_host, $akismet_api_port;
78
		$body_request['user_ip'] 	= ( isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : null );
79
		$response = akismet_http_post( build_query( $body_request ), $akismet_api_host, '/1.1/comment-check', $akismet_api_port );
80
	}
81
82
	// The Response is spam lets not send the email.
83
	if ( ! empty( $response ) && isset( $response[1] ) && 'true' == trim( $response[1] ) ) { // 'true' is spam
84
		return false; // don't send the email
85
	}
86
	return $data;
87
}
88
89
function sharing_email_send_post_content( $data ) {
90
	/* translators: included in email when post is shared via email. First item is sender's name. Second is sender's email address. */
91
	$content  = sprintf( __( '%1$s (%2$s) thinks you may be interested in the following post:', 'jetpack' ), $data['name'], $data['source'] );
92
	$content .= "\n\n";
93
	// Make sure to pass the title and URL through the normal sharing filters.
94
	$content .= $data['sharing_source']->get_share_title( $data['post']->ID ) . "\n";
95
	$content .= $data['sharing_source']->get_share_url( $data['post']->ID ) . "\n";
96
	return $content;
97
}
98
99
function sharing_add_meta_box() {
100
	global $post;
101
	if ( empty( $post ) ) { // If a current post is not defined, such as when editing a comment.
102
		return;
103
	}
104
105
	/**
106
	 * Filter whether to display the Sharing Meta Box or not.
107
	 *
108
	 * @module sharedaddy
109
	 *
110
	 * @since 3.8.0
111
	 *
112
	 * @param bool true Display Sharing Meta Box.
113
	 * @param $post Post.
114
	 */
115
	if ( ! apply_filters( 'sharing_meta_box_show', true, $post ) ) {
116
		return;
117
	}
118
119
	$post_types = get_post_types( array( 'public' => true ) );
120
	/**
121
	 * Filter the Sharing Meta Box title.
122
	 *
123
	 * @module sharedaddy
124
	 *
125
	 * @since 2.2.0
126
	 *
127
	 * @param string $var Sharing Meta Box title. Default is "Sharing".
128
	 */
129
	$title = apply_filters( 'sharing_meta_box_title', __( 'Sharing', 'jetpack' ) );
130
	if ( $post->ID !== get_option( 'page_for_posts' ) ) {
131
		foreach( $post_types as $post_type ) {
132
			add_meta_box( 'sharing_meta', $title, 'sharing_meta_box_content', $post_type, 'advanced', 'high' );
133
		}
134
	}
135
}
136
137
138
function sharing_meta_box_content( $post ) {
139
	/**
140
	 * Fires before the sharing meta box content.
141
	 *
142
	 * @module sharedaddy
143
	 *
144
	 * @since 2.2.0
145
	 *
146
	 * @param WP_Post $post The post to share.
147
	 */
148
	do_action( 'start_sharing_meta_box_content', $post );
149
150
	$disabled = get_post_meta( $post->ID, 'sharing_disabled', true ); ?>
151
152
	<p>
153
		<label for="enable_post_sharing">
154
			<input type="checkbox" name="enable_post_sharing" id="enable_post_sharing" value="1" <?php checked( !$disabled ); ?>>
155
			<?php _e( 'Show sharing buttons.' , 'jetpack'); ?>
156
		</label>
157
		<input type="hidden" name="sharing_status_hidden" value="1" />
158
	</p>
159
160
	<?php
161
	/**
162
	 * Fires after the sharing meta box content.
163
	 *
164
	 * @module sharedaddy
165
	 *
166
	 * @since 2.2.0
167
	 *
168
	 * @param WP_Post $post The post to share.
169
	*/
170
	do_action( 'end_sharing_meta_box_content', $post );
171
}
172
173
function sharing_meta_box_save( $post_id ) {
174
	if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
175
		return $post_id;
176
177
	// Record sharing disable
178
	if ( isset( $_POST['post_type'] ) && ( $post_type_object = get_post_type_object( $_POST['post_type'] ) ) && $post_type_object->public ) {
179
		if ( current_user_can( 'edit_post', $post_id ) ) {
180
			if ( isset( $_POST['sharing_status_hidden'] ) ) {
181
				if ( !isset( $_POST['enable_post_sharing'] ) ) {
182
					update_post_meta( $post_id, 'sharing_disabled', 1 );
183
				} else {
184
					delete_post_meta( $post_id, 'sharing_disabled' );
185
				}
186
			}
187
		}
188
	}
189
190
  	return $post_id;
191
}
192
193
function sharing_meta_box_protected( $protected, $meta_key, $meta_type ) {
194
	if ( 'sharing_disabled' == $meta_key )
195
		$protected = true;
196
197
	return $protected;
198
}
199
200
add_filter( 'is_protected_meta', 'sharing_meta_box_protected', 10, 3 );
201
202
function sharing_plugin_settings( $links ) {
203
	$settings_link = '<a href="options-general.php?page=sharing.php">'.__( 'Settings', 'jetpack' ).'</a>';
204
	array_unshift( $links, $settings_link );
205
	return $links;
206
}
207
208
function sharing_add_plugin_settings($links, $file) {
209
	if ( $file == basename( dirname( __FILE__ ) ).'/'.basename( __FILE__ ) ) {
210
		$links[] = '<a href="options-general.php?page=sharing.php">' . __( 'Settings', 'jetpack' ) . '</a>';
211
		$links[] = '<a href="http://support.wordpress.com/sharing/" target="_blank">' . __( 'Support', 'jetpack' ) . '</a>';
212
	}
213
214
	return $links;
215
}
216
217
function sharing_restrict_to_single( $services ) {
218
	// This removes Press This from non-multisite blogs - doesn't make much sense
219
	if ( is_multisite() === false ) {
220
		unset( $services['press-this'] );
221
	}
222
223
	return $services;
224
}
225
226
function sharing_init() {
227
	if ( Jetpack_Options::get_option_and_ensure_autoload( 'sharedaddy_disable_resources', '0' ) ) {
228
		add_filter( 'sharing_js', 'sharing_disable_js' );
229
		remove_action( 'wp_head', 'sharing_add_header', 1 );
230
	}
231
}
232
233
function sharing_disable_js() {
234
	return false;
235
}
236
237
function sharing_global_resources() {
238
	$disable = get_option( 'sharedaddy_disable_resources' );
239
?>
240
<tr valign="top">
241
	<th scope="row"><label for="disable_css"><?php _e( 'Disable CSS and JS', 'jetpack' ); ?></label></th>
242
	<td>
243
		<input id="disable_css" type="checkbox" name="disable_resources" <?php if ( $disable == 1 ) echo ' checked="checked"'; ?>/>  <small><em><?php _e( 'Advanced.  If this option is checked, you must include these files in your theme manually for the sharing links to work.', 'jetpack' ); ?></em></small>
244
	</td>
245
</tr>
246
<?php
247
}
248
249
function sharing_global_resources_save() {
250
	update_option( 'sharedaddy_disable_resources', isset( $_POST['disable_resources'] ) ? 1 : 0 );
251
}
252
253
function sharing_email_dialog() {
254
	require_once plugin_dir_path( __FILE__ ) . 'recaptcha.php';
255
256
	$recaptcha = new Jetpack_ReCaptcha( RECAPTCHA_PUBLIC_KEY, RECAPTCHA_PRIVATE_KEY );
257
	echo $recaptcha->get_recaptcha_html(); // xss ok
258
}
259
260
function sharing_email_check( $true, $post, $data ) {
261
	require_once plugin_dir_path( __FILE__ ) . 'recaptcha.php';
262
263
	$recaptcha = new Jetpack_ReCaptcha( RECAPTCHA_PUBLIC_KEY, RECAPTCHA_PRIVATE_KEY );
264
	$response  = ! empty( $_POST['g-recaptcha-response'] ) ? $_POST['g-recaptcha-response'] : '';
265
	$result    = $recaptcha->verify( $response, $_SERVER['REMOTE_ADDR'] );
266
267
	return ( true === $result );
268
}
269
270
add_action( 'init', 'sharing_init' );
271
add_action( 'add_meta_boxes', 'sharing_add_meta_box' );
272
add_action( 'save_post', 'sharing_meta_box_save' );
273
add_action( 'sharing_email_send_post', 'sharing_email_send_post' );
274
add_filter( 'sharing_email_can_send', 'sharing_email_check_for_spam_via_akismet' );
275
add_action( 'sharing_global_options', 'sharing_global_resources', 30 );
276
add_action( 'sharing_admin_update', 'sharing_global_resources_save' );
277
add_filter( 'sharing_services', 'sharing_restrict_to_single' );
278
add_action( 'plugin_action_links_'.basename( dirname( __FILE__ ) ).'/'.basename( __FILE__ ), 'sharing_plugin_settings', 10, 4 );
279
add_filter( 'plugin_row_meta', 'sharing_add_plugin_settings', 10, 2 );
280
281
if ( defined( 'RECAPTCHA_PUBLIC_KEY' ) && defined( 'RECAPTCHA_PRIVATE_KEY' ) ) {
282
	add_action( 'sharing_email_dialog', 'sharing_email_dialog' );
283
	add_filter( 'sharing_email_check', 'sharing_email_check', 10, 3 );
284
}
285