PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 4.9.3
Jetpack – WP Security, Backup, Speed, & Growth v4.9.3
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / sharedaddy / sharedaddy.php

sharedaddy.php in Jetpack – WP Security, Backup, Speed, & Growth 4.9.3, at modules/sharedaddy/sharedaddy.php

288 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ( ! function_exists( 'akismet_http_post' ) && ! method_exists( 'Akismet', 'http_post' ) )
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 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_restrict_to_single( $services ) {
221 // This removes Press This from non-multisite blogs - doesn't make much sense
222 if ( is_multisite() === false ) {
223 unset( $services['press-this'] );
224 }
225
226 return $services;
227 }
228
229 function sharing_init() {
230 if ( Jetpack_Options::get_option_and_ensure_autoload( 'sharedaddy_disable_resources', '0' ) ) {
231 add_filter( 'sharing_js', 'sharing_disable_js' );
232 remove_action( 'wp_head', 'sharing_add_header', 1 );
233 }
234 }
235
236 function sharing_disable_js() {
237 return false;
238 }
239
240 function sharing_global_resources() {
241 $disable = get_option( 'sharedaddy_disable_resources' );
242 ?>
243 <tr valign="top">
244 <th scope="row"><label for="disable_css"><?php _e( 'Disable CSS and JS', 'jetpack' ); ?></label></th>
245 <td>
246 <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>
247 </td>
248 </tr>
249 <?php
250 }
251
252 function sharing_global_resources_save() {
253 update_option( 'sharedaddy_disable_resources', isset( $_POST['disable_resources'] ) ? 1 : 0 );
254 }
255
256 function sharing_email_dialog() {
257 require_once plugin_dir_path( __FILE__ ) . 'recaptcha.php';
258
259 $recaptcha = new Jetpack_ReCaptcha( RECAPTCHA_PUBLIC_KEY, RECAPTCHA_PRIVATE_KEY );
260 echo $recaptcha->get_recaptcha_html(); // xss ok
261 }
262
263 function sharing_email_check( $true, $post, $data ) {
264 require_once plugin_dir_path( __FILE__ ) . 'recaptcha.php';
265
266 $recaptcha = new Jetpack_ReCaptcha( RECAPTCHA_PUBLIC_KEY, RECAPTCHA_PRIVATE_KEY );
267 $response = ! empty( $_POST['g-recaptcha-response'] ) ? $_POST['g-recaptcha-response'] : '';
268 $result = $recaptcha->verify( $response, $_SERVER['REMOTE_ADDR'] );
269
270 return ( true === $result );
271 }
272
273 add_action( 'init', 'sharing_init' );
274 add_action( 'add_meta_boxes', 'sharing_add_meta_box' );
275 add_action( 'save_post', 'sharing_meta_box_save' );
276 add_action( 'sharing_email_send_post', 'sharing_email_send_post' );
277 add_filter( 'sharing_email_can_send', 'sharing_email_check_for_spam_via_akismet' );
278 add_action( 'sharing_global_options', 'sharing_global_resources', 30 );
279 add_action( 'sharing_admin_update', 'sharing_global_resources_save' );
280 add_filter( 'sharing_services', 'sharing_restrict_to_single' );
281 add_action( 'plugin_action_links_'.basename( dirname( __FILE__ ) ).'/'.basename( __FILE__ ), 'sharing_plugin_settings', 10, 4 );
282 add_filter( 'plugin_row_meta', 'sharing_add_plugin_settings', 10, 2 );
283
284 if ( defined( 'RECAPTCHA_PUBLIC_KEY' ) && defined( 'RECAPTCHA_PRIVATE_KEY' ) ) {
285 add_action( 'sharing_email_dialog', 'sharing_email_dialog' );
286 add_filter( 'sharing_email_check', 'sharing_email_check', 10, 3 );
287 }
288