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