sharedaddy.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. require_once plugin_dir_path( __FILE__ ).'sharing.php';
  11. function sharing_email_send_post( $data ) {
  12. $content = sharing_email_send_post_content( $data );
  13. // Borrowed from wp_mail();
  14. $sitename = strtolower( $_SERVER['SERVER_NAME'] );
  15. if ( substr( $sitename, 0, 4 ) == 'www.' ) {
  16. $sitename = substr( $sitename, 4 );
  17. }
  18. /** This filter is documented in core/src/wp-includes/pluggable.php */
  19. $from_email = apply_filters( 'wp_mail_from', 'wordpress@' . $sitename );
  20. if ( ! empty( $data['name'] ) ) {
  21. $s_name = (string) $data['name'];
  22. $name_needs_encoding_regex =
  23. '/[' .
  24. // SpamAssasin's list of characters which "need MIME" encoding
  25. '\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\xff' .
  26. // Our list of "unsafe" characters
  27. '<\r\n' .
  28. ']/';
  29. $needs_encoding =
  30. // If it contains any blacklisted chars,
  31. preg_match( $name_needs_encoding_regex, $s_name ) ||
  32. // Or if we can't use `mb_convert_encoding`
  33. ! function_exists( 'mb_convert_encoding' ) ||
  34. // Or if it's not already ASCII
  35. mb_convert_encoding( $data['name'], 'ASCII' ) !== $s_name;
  36. if ( $needs_encoding ) {
  37. $data['name'] = sprintf( '=?UTF-8?B?%s?=', base64_encode( $data['name'] ) );
  38. }
  39. }
  40. $headers[] = sprintf( 'From: %1$s <%2$s>', $data['name'], $from_email );
  41. $headers[] = sprintf( 'Reply-To: %1$s <%2$s>', $data['name'], $data['source'] );
  42. // Make sure to pass the title through the normal sharing filters.
  43. $title = $data['sharing_source']->get_share_title( $data['post']->ID );
  44. /**
  45. * Filter the Sharing Email Send Post Subject.
  46. *
  47. * @module sharedaddy
  48. *
  49. * @since 5.8.0
  50. *
  51. * @param string $var Sharing Email Send Post Subject. Default is "Shared Post".
  52. */
  53. $subject = apply_filters( 'wp_sharing_email_send_post_subject', '[' . __( 'Shared Post', 'jetpack' ) . '] ' . $title );
  54. wp_mail( $data['target'], $subject, $content, $headers );
  55. }
  56. /* Checks for spam using akismet if available. */
  57. /* Return $data as it if email about to be send out is not spam. */
  58. function sharing_email_check_for_spam_via_akismet( $data ) {
  59. if ( ! Jetpack::is_akismet_active() )
  60. return $data;
  61. // Prepare the body_request for akismet
  62. $body_request = array(
  63. 'blog' => get_option( 'home' ),
  64. 'permalink' => $data['sharing_source']->get_share_url( $data['post']->ID ),
  65. 'comment_type' => 'share',
  66. 'comment_author' => $data['name'],
  67. 'comment_author_email' => $data['source'],
  68. 'comment_content' => sharing_email_send_post_content( $data ),
  69. 'user_agent' => ( isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : null ),
  70. );
  71. if ( method_exists( 'Akismet', 'http_post' ) ) {
  72. $body_request['user_ip'] = Akismet::get_ip_address();
  73. $response = Akismet::http_post( build_query( $body_request ), 'comment-check' );
  74. } else {
  75. global $akismet_api_host, $akismet_api_port;
  76. $body_request['user_ip'] = ( isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : null );
  77. $response = akismet_http_post( build_query( $body_request ), $akismet_api_host, '/1.1/comment-check', $akismet_api_port );
  78. }
  79. // The Response is spam lets not send the email.
  80. if ( ! empty( $response ) && isset( $response[1] ) && 'true' == trim( $response[1] ) ) { // 'true' is spam
  81. return false; // don't send the email
  82. }
  83. return $data;
  84. }
  85. function sharing_email_send_post_content( $data ) {
  86. /* translators: included in email when post is shared via email. First item is sender's name. Second is sender's email address. */
  87. $content = sprintf( __( '%1$s (%2$s) thinks you may be interested in the following post:', 'jetpack' ), $data['name'], $data['source'] );
  88. $content .= "\n\n";
  89. // Make sure to pass the title and URL through the normal sharing filters.
  90. $content .= $data['sharing_source']->get_share_title( $data['post']->ID ) . "\n";
  91. $content .= $data['sharing_source']->get_share_url( $data['post']->ID ) . "\n";
  92. return $content;
  93. }
  94. function sharing_add_meta_box() {
  95. global $post;
  96. if ( empty( $post ) ) { // If a current post is not defined, such as when editing a comment.
  97. return;
  98. }
  99. /**
  100. * Filter whether to display the Sharing Meta Box or not.
  101. *
  102. * @module sharedaddy
  103. *
  104. * @since 3.8.0
  105. *
  106. * @param bool true Display Sharing Meta Box.
  107. * @param $post Post.
  108. */
  109. if ( ! apply_filters( 'sharing_meta_box_show', true, $post ) ) {
  110. return;
  111. }
  112. $post_types = get_post_types( array( 'public' => true ) );
  113. /**
  114. * Filter the Sharing Meta Box title.
  115. *
  116. * @module sharedaddy
  117. *
  118. * @since 2.2.0
  119. *
  120. * @param string $var Sharing Meta Box title. Default is "Sharing".
  121. */
  122. $title = apply_filters( 'sharing_meta_box_title', __( 'Sharing', 'jetpack' ) );
  123. if ( $post->ID !== get_option( 'page_for_posts' ) ) {
  124. foreach( $post_types as $post_type ) {
  125. add_meta_box( 'sharing_meta', $title, 'sharing_meta_box_content', $post_type, 'side', 'default' );
  126. }
  127. }
  128. }
  129. function sharing_meta_box_content( $post ) {
  130. /**
  131. * Fires before the sharing meta box content.
  132. *
  133. * @module sharedaddy
  134. *
  135. * @since 2.2.0
  136. *
  137. * @param WP_Post $post The post to share.
  138. */
  139. do_action( 'start_sharing_meta_box_content', $post );
  140. $disabled = get_post_meta( $post->ID, 'sharing_disabled', true ); ?>
  141. <p>
  142. <label for="enable_post_sharing">
  143. <input type="checkbox" name="enable_post_sharing" id="enable_post_sharing" value="1" <?php checked( !$disabled ); ?>>
  144. <?php _e( 'Show sharing buttons.' , 'jetpack'); ?>
  145. </label>
  146. <input type="hidden" name="sharing_status_hidden" value="1" />
  147. </p>
  148. <?php
  149. /**
  150. * Fires after the sharing meta box content.
  151. *
  152. * @module sharedaddy
  153. *
  154. * @since 2.2.0
  155. *
  156. * @param WP_Post $post The post to share.
  157. */
  158. do_action( 'end_sharing_meta_box_content', $post );
  159. }
  160. function sharing_meta_box_save( $post_id ) {
  161. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
  162. return $post_id;
  163. // Record sharing disable
  164. if ( isset( $_POST['post_type'] ) && ( $post_type_object = get_post_type_object( $_POST['post_type'] ) ) && $post_type_object->public ) {
  165. if ( current_user_can( 'edit_post', $post_id ) ) {
  166. if ( isset( $_POST['sharing_status_hidden'] ) ) {
  167. if ( !isset( $_POST['enable_post_sharing'] ) ) {
  168. update_post_meta( $post_id, 'sharing_disabled', 1 );
  169. } else {
  170. delete_post_meta( $post_id, 'sharing_disabled' );
  171. }
  172. }
  173. }
  174. }
  175. return $post_id;
  176. }
  177. function sharing_meta_box_protected( $protected, $meta_key, $meta_type ) {
  178. if ( 'sharing_disabled' == $meta_key )
  179. $protected = true;
  180. return $protected;
  181. }
  182. add_filter( 'is_protected_meta', 'sharing_meta_box_protected', 10, 3 );
  183. function sharing_plugin_settings( $links ) {
  184. $settings_link = '<a href="options-general.php?page=sharing.php">'.__( 'Settings', 'jetpack' ).'</a>';
  185. array_unshift( $links, $settings_link );
  186. return $links;
  187. }
  188. function sharing_add_plugin_settings($links, $file) {
  189. if ( $file == basename( dirname( __FILE__ ) ).'/'.basename( __FILE__ ) ) {
  190. $links[] = '<a href="options-general.php?page=sharing.php">' . __( 'Settings', 'jetpack' ) . '</a>';
  191. $links[] = '<a href="http://support.wordpress.com/sharing/" rel="noopener noreferrer" target="_blank">' . __( 'Support', 'jetpack' ) . '</a>';
  192. }
  193. return $links;
  194. }
  195. function sharing_init() {
  196. if ( Jetpack_Options::get_option_and_ensure_autoload( 'sharedaddy_disable_resources', '0' ) ) {
  197. add_filter( 'sharing_js', 'sharing_disable_js' );
  198. remove_action( 'wp_head', 'sharing_add_header', 1 );
  199. }
  200. }
  201. function sharing_disable_js() {
  202. return false;
  203. }
  204. function sharing_global_resources() {
  205. $disable = get_option( 'sharedaddy_disable_resources' );
  206. ?>
  207. <tr valign="top">
  208. <th scope="row"><label for="disable_css"><?php _e( 'Disable CSS and JS', 'jetpack' ); ?></label></th>
  209. <td>
  210. <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>
  211. </td>
  212. </tr>
  213. <?php
  214. }
  215. function sharing_global_resources_save() {
  216. update_option( 'sharedaddy_disable_resources', isset( $_POST['disable_resources'] ) ? 1 : 0 );
  217. }
  218. function sharing_email_dialog() {
  219. require_once plugin_dir_path( __FILE__ ) . 'recaptcha.php';
  220. $recaptcha = new Jetpack_ReCaptcha( RECAPTCHA_PUBLIC_KEY, RECAPTCHA_PRIVATE_KEY );
  221. echo $recaptcha->get_recaptcha_html(); // xss ok
  222. }
  223. function sharing_email_check( $true, $post, $data ) {
  224. require_once plugin_dir_path( __FILE__ ) . 'recaptcha.php';
  225. $recaptcha = new Jetpack_ReCaptcha( RECAPTCHA_PUBLIC_KEY, RECAPTCHA_PRIVATE_KEY );
  226. $response = ! empty( $_POST['g-recaptcha-response'] ) ? $_POST['g-recaptcha-response'] : '';
  227. $result = $recaptcha->verify( $response, $_SERVER['REMOTE_ADDR'] );
  228. return ( true === $result );
  229. }
  230. add_action( 'init', 'sharing_init' );
  231. add_action( 'add_meta_boxes', 'sharing_add_meta_box' );
  232. add_action( 'save_post', 'sharing_meta_box_save' );
  233. add_action( 'sharing_email_send_post', 'sharing_email_send_post' );
  234. add_filter( 'sharing_email_can_send', 'sharing_email_check_for_spam_via_akismet' );
  235. add_action( 'sharing_global_options', 'sharing_global_resources', 30 );
  236. add_action( 'sharing_admin_update', 'sharing_global_resources_save' );
  237. add_action( 'plugin_action_links_'.basename( dirname( __FILE__ ) ).'/'.basename( __FILE__ ), 'sharing_plugin_settings', 10, 4 );
  238. add_filter( 'plugin_row_meta', 'sharing_add_plugin_settings', 10, 2 );
  239. if ( defined( 'RECAPTCHA_PUBLIC_KEY' ) && defined( 'RECAPTCHA_PRIVATE_KEY' ) ) {
  240. add_action( 'sharing_email_dialog', 'sharing_email_dialog' );
  241. add_filter( 'sharing_email_check', 'sharing_email_check', 10, 3 );
  242. }