/modules/sharedaddy/sharedaddy.php

  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. $headers[] = sprintf( 'From: %1$s <%2$s>', $data['name'], $data['source'] ); 
  17.  
  18. wp_mail( $data['target'], '['.__( 'Shared Post', 'jetpack' ).'] '.$data['post']->post_title, $content, $headers ); 
  19.  
  20.  
  21. /** Checks for spam using akismet if available. */ 
  22. /** Return $data as it if email about to be send out is not spam. */ 
  23. function sharing_email_check_for_spam_via_akismet( $data ) { 
  24.  
  25. if ( ! function_exists( 'akismet_http_post' ) && ! method_exists( 'Akismet', 'http_post' ) ) 
  26. return $data; 
  27.  
  28. // Prepare the body_request for akismet 
  29. $body_request = array( 
  30. 'blog' => get_option( 'home' ),  
  31. 'permalink' => get_permalink( $data['post']->ID ),  
  32. 'comment_type' => 'share',  
  33. 'comment_author' => $data['name'],  
  34. 'comment_author_email' => $data['source'],  
  35. 'comment_content' => sharing_email_send_post_content( $data ),  
  36. 'user_agent' => ( isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : null ),  
  37. ); 
  38.  
  39. if ( method_exists( 'Akismet', 'http_post' ) ) { 
  40. $body_request['user_ip'] = Akismet::get_ip_address(); 
  41. $response = Akismet::http_post( build_query( $body_request ), 'comment-check' ); 
  42. } else { 
  43. global $akismet_api_host, $akismet_api_port; 
  44. $body_request['user_ip'] = ( isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : null ); 
  45. $response = akismet_http_post( build_query( $body_request ), $akismet_api_host, '/1.1/comment-check', $akismet_api_port ); 
  46.  
  47. // The Response is spam lets not send the email. 
  48. if ( ! empty( $response ) && isset( $response[1] ) && 'true' == trim( $response[1] ) ) { // 'true' is spam 
  49. return false; // don't send the email 
  50. return $data; 
  51.  
  52. function sharing_email_send_post_content( $data ) { 
  53. /** translators: included in e-mail when post is shared via e-mail. First item is sender's name. Second is sender's e-mail address. */ 
  54. $content = sprintf( __( '%1$s (%2$s) thinks you may be interested in the following post:', 'jetpack' ), $data['name'], $data['source'] ); 
  55. $content .= "\n\n"; 
  56. $content .= $data['post']->post_title."\n"; 
  57. $content .= get_permalink( $data['post']->ID )."\n"; 
  58. return $content; 
  59.  
  60. function sharing_add_meta_box() { 
  61. global $post; 
  62. if ( empty( $post ) ) { // If a current post is not defined, such as when editing a comment. 
  63. return; 
  64.  
  65. /** 
  66. * Filter whether to display the Sharing Meta Box or not. 
  67. * 
  68. * @module sharedaddy 
  69. * 
  70. * @since 3.8.0 
  71. * 
  72. * @param bool true Display Sharing Meta Box. 
  73. * @param $post Post. 
  74. */ 
  75. if ( ! apply_filters( 'sharing_meta_box_show', true, $post ) ) { 
  76. return; 
  77.  
  78. $post_types = get_post_types( array( 'public' => true ) ); 
  79. /** 
  80. * Filter the Sharing Meta Box title. 
  81. * 
  82. * @module sharedaddy 
  83. * 
  84. * @since 2.2.0 
  85. * 
  86. * @param string $var Sharing Meta Box title. Default is "Sharing". 
  87. */ 
  88. $title = apply_filters( 'sharing_meta_box_title', __( 'Sharing', 'jetpack' ) ); 
  89. if ( $post->ID !== get_option( 'page_for_posts' ) ) { 
  90. foreach( $post_types as $post_type ) { 
  91. add_meta_box( 'sharing_meta', $title, 'sharing_meta_box_content', $post_type, 'advanced', 'high' ); 
  92.  
  93.  
  94. function sharing_meta_box_content( $post ) { 
  95. /** 
  96. * Fires before the sharing meta box content. 
  97. * 
  98. * @module sharedaddy 
  99. * 
  100. * @since 2.2.0 
  101. * 
  102. * @param WP_Post $post The post to share. 
  103. */ 
  104. do_action( 'start_sharing_meta_box_content', $post ); 
  105.  
  106. $disabled = get_post_meta( $post->ID, 'sharing_disabled', true ); ?> 
  107.  
  108. <p> 
  109. <label for="enable_post_sharing"> 
  110. <input type="checkbox" name="enable_post_sharing" id="enable_post_sharing" value="1" <?php checked( !$disabled ); ?>> 
  111. <?php _e( 'Show sharing buttons.' , 'jetpack'); ?> 
  112. </label> 
  113. <input type="hidden" name="sharing_status_hidden" value="1" /> 
  114. </p> 
  115.  
  116. <?php 
  117. /** 
  118. * Fires after the sharing meta box content. 
  119. * 
  120. * @module sharedaddy 
  121. * 
  122. * @since 2.2.0 
  123. * 
  124. * @param WP_Post $post The post to share. 
  125. */ 
  126. do_action( 'end_sharing_meta_box_content', $post ); 
  127.  
  128. function sharing_meta_box_save( $post_id ) { 
  129. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
  130. return $post_id; 
  131.  
  132. // Record sharing disable 
  133. if ( isset( $_POST['post_type'] ) && ( $post_type_object = get_post_type_object( $_POST['post_type'] ) ) && $post_type_object->public ) { 
  134. if ( current_user_can( 'edit_post', $post_id ) ) { 
  135. if ( isset( $_POST['sharing_status_hidden'] ) ) { 
  136. if ( !isset( $_POST['enable_post_sharing'] ) ) { 
  137. update_post_meta( $post_id, 'sharing_disabled', 1 ); 
  138. } else { 
  139. delete_post_meta( $post_id, 'sharing_disabled' ); 
  140.  
  141. return $post_id; 
  142.  
  143. function sharing_meta_box_protected( $protected, $meta_key, $meta_type ) { 
  144. if ( 'sharing_disabled' == $meta_key ) 
  145. $protected = true; 
  146.  
  147. return $protected; 
  148.  
  149. add_filter( 'is_protected_meta', 'sharing_meta_box_protected', 10, 3 ); 
  150.  
  151. function sharing_plugin_settings( $links ) { 
  152. $settings_link = '<a href="options-general.php?page=sharing.php">'.__( 'Settings', 'jetpack' ).'</a>'; 
  153. array_unshift( $links, $settings_link ); 
  154. return $links; 
  155.  
  156. function sharing_add_plugin_settings($links, $file) { 
  157. if ( $file == basename( dirname( __FILE__ ) ).'/'.basename( __FILE__ ) ) { 
  158. $links[] = '<a href="options-general.php?page=sharing.php">' . __( 'Settings', 'jetpack' ) . '</a>'; 
  159. $links[] = '<a href="http://support.wordpress.com/sharing/">' . __( 'Support', 'jetpack' ) . '</a>'; 
  160.  
  161. return $links; 
  162.  
  163. function sharing_restrict_to_single( $services ) { 
  164. // This removes Press This from non-multisite blogs - doesn't make much sense 
  165. if ( is_multisite() === false ) { 
  166. unset( $services['press-this'] ); 
  167.  
  168. return $services; 
  169.  
  170. function sharing_init() { 
  171. if ( get_option( 'sharedaddy_disable_resources' ) ) { 
  172. add_filter( 'sharing_js', 'sharing_disable_js' ); 
  173. remove_action( 'wp_head', 'sharing_add_header', 1 ); 
  174.  
  175. function sharing_disable_js() { 
  176. return false; 
  177.  
  178. function sharing_global_resources() { 
  179. $disable = get_option( 'sharedaddy_disable_resources' ); 
  180. ?> 
  181. <tr valign="top"> 
  182. <th scope="row"><label for="disable_css"><?php _e( 'Disable CSS and JS', 'jetpack' ); ?></label></th> 
  183. <td> 
  184. <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> 
  185. </td> 
  186. </tr> 
  187. <?php 
  188.  
  189. function sharing_global_resources_save() { 
  190. update_option( 'sharedaddy_disable_resources', isset( $_POST['disable_resources'] ) ? 1 : 0 ); 
  191.  
  192. function sharing_email_dialog() { 
  193. require_once plugin_dir_path( __FILE__ ) . 'recaptcha.php'; 
  194.  
  195. $recaptcha = new Jetpack_ReCaptcha( RECAPTCHA_PUBLIC_KEY, RECAPTCHA_PRIVATE_KEY ); 
  196. echo $recaptcha->get_recaptcha_html(); // xss ok 
  197.  
  198. function sharing_email_check( $true, $post, $data ) { 
  199. require_once plugin_dir_path( __FILE__ ) . 'recaptcha.php'; 
  200.  
  201. $recaptcha = new Jetpack_ReCaptcha( RECAPTCHA_PUBLIC_KEY, RECAPTCHA_PRIVATE_KEY ); 
  202. $response = ! empty( $_POST['g-recaptcha-response'] ) ? $_POST['g-recaptcha-response'] : ''; 
  203. $result = $recaptcha->verify( $response, $_SERVER['REMOTE_ADDR'] ); 
  204.  
  205. return ( true === $result ); 
  206.  
  207. add_action( 'init', 'sharing_init' ); 
  208. add_action( 'add_meta_boxes', 'sharing_add_meta_box' ); 
  209. add_action( 'save_post', 'sharing_meta_box_save' ); 
  210. add_action( 'sharing_email_send_post', 'sharing_email_send_post' ); 
  211. add_filter( 'sharing_email_can_send', 'sharing_email_check_for_spam_via_akismet' ); 
  212. add_action( 'sharing_global_options', 'sharing_global_resources', 30 ); 
  213. add_action( 'sharing_admin_update', 'sharing_global_resources_save' ); 
  214. add_filter( 'sharing_services', 'sharing_restrict_to_single' ); 
  215. add_action( 'plugin_action_links_'.basename( dirname( __FILE__ ) ).'/'.basename( __FILE__ ), 'sharing_plugin_settings', 10, 4 ); 
  216. add_filter( 'plugin_row_meta', 'sharing_add_plugin_settings', 10, 2 ); 
  217.  
  218. if ( defined( 'RECAPTCHA_PUBLIC_KEY' ) && defined( 'RECAPTCHA_PRIVATE_KEY' ) ) { 
  219. add_action( 'sharing_email_dialog', 'sharing_email_dialog' ); 
  220. add_filter( 'sharing_email_check', 'sharing_email_check', 10, 3 ); 
.