comment-likes.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * Module Name: Comment Likes
  4. * Module Description: Increase visitor engagement by adding a Like button to comments.
  5. * Sort Order: 39
  6. * Recommendation Order: 17
  7. * First Introduced: 5.1
  8. * Requires Connection: Yes
  9. * Auto Activate: No
  10. * Module Tags: Social
  11. * Additional Search Queries: like widget, like button, like, likes
  12. */
  13. Jetpack::dns_prefetch( array(
  14. '//widgets.wp.com',
  15. '//s0.wp.com',
  16. '//s1.wp.com',
  17. '//s2.wp.com',
  18. '//0.gravatar.com',
  19. '//1.gravatar.com',
  20. '//2.gravatar.com',
  21. ) );
  22. require_once dirname( __FILE__ ) . '/likes/jetpack-likes-master-iframe.php';
  23. require_once dirname( __FILE__ ) . '/likes/jetpack-likes-settings.php';
  24. class Jetpack_Comment_Likes {
  25. public static function init() {
  26. static $instance = NULL;
  27. if ( ! $instance ) {
  28. $instance = new Jetpack_Comment_Likes;
  29. }
  30. return $instance;
  31. }
  32. private function __construct() {
  33. $this->settings = new Jetpack_Likes_Settings();
  34. $this->blog_id = Jetpack_Options::get_option( 'id' );
  35. $this->url = home_url();
  36. $this->url_parts = parse_url( $this->url );
  37. $this->domain = $this->url_parts['host'];
  38. add_action( 'init', array( $this, 'frontend_init' ) );
  39. add_action( 'admin_init', array( $this, 'admin_init' ) );
  40. if ( ! Jetpack::is_module_active( 'likes' ) ) {
  41. $active = Jetpack::get_active_modules();
  42. if ( ! in_array( 'sharedaddy', $active ) && ! in_array( 'publicize', $active ) ) {
  43. // we don't have a sharing page yet
  44. add_action( 'admin_menu', array( $this->settings, 'sharing_menu' ) );
  45. }
  46. if ( in_array( 'publicize', $active ) && ! in_array( 'sharedaddy', $active ) ) {
  47. // we have a sharing page but not the global options area
  48. add_action( 'pre_admin_screen_sharing', array( $this->settings, 'sharing_block' ), 20 );
  49. add_action( 'pre_admin_screen_sharing', array( $this->settings, 'updated_message' ), -10 );
  50. }
  51. if( ! in_array( 'sharedaddy', $active ) ) {
  52. add_action( 'admin_init', array( $this->settings, 'process_update_requests_if_sharedaddy_not_loaded' ) );
  53. add_action( 'sharing_global_options', array( $this->settings, 'admin_settings_showbuttonon_init' ), 19 );
  54. add_action( 'sharing_admin_update', array( $this->settings, 'admin_settings_showbuttonon_callback' ), 19 );
  55. add_action( 'admin_init', array( $this->settings, 'add_meta_box' ) );
  56. } else {
  57. add_filter( 'sharing_meta_box_title', array( $this->settings, 'add_likes_to_sharing_meta_box_title' ) );
  58. add_action( 'start_sharing_meta_box_content', array( $this->settings, 'meta_box_content' ) );
  59. }
  60. add_action( 'save_post', array( $this->settings, 'meta_box_save' ) );
  61. add_action( 'edit_attachment', array( $this->settings, 'meta_box_save' ) );
  62. add_action( 'sharing_global_options', array( $this->settings, 'admin_settings_init' ), 20 );
  63. add_action( 'sharing_admin_update', array( $this->settings, 'admin_settings_callback' ), 20 );
  64. }
  65. }
  66. public function admin_init() {
  67. add_filter( 'manage_edit-comments_columns', array( $this, 'add_like_count_column' ) );
  68. add_action( 'manage_comments_custom_column', array( $this, 'comment_likes_edit_column' ), 10, 2 );
  69. add_action( 'admin_print_styles-edit-comments.php', array( $this, 'enqueue_admin_styles_scripts' ) );
  70. }
  71. public function comment_likes_edit_column( $column_name, $comment_id ) {
  72. if ( 'comment_likes' !== $column_name ) {
  73. return;
  74. }
  75. $permalink = get_permalink( get_the_ID() );
  76. ?>
  77. <a
  78. data-comment-id="<?php echo absint( $comment_id ); ?>"
  79. data-blog-id="<?php echo absint( $this->blog_id ); ?>"
  80. class="comment-like-count"
  81. id="comment-like-count-<?php echo absint( $comment_id ); ?>"
  82. href="<?php echo esc_url( $permalink ); ?>#comment-<?php echo absint( $comment_id ); ?>"
  83. >
  84. <span class="like-count">0</span>
  85. </a>
  86. <?php
  87. }
  88. function enqueue_admin_styles_scripts() {
  89. wp_enqueue_style( 'comment-like-count', plugins_url( 'comment-likes/admin-style.css', __FILE__ ), array(), JETPACK__VERSION );
  90. wp_enqueue_script(
  91. 'comment-like-count',
  92. Jetpack::get_file_url_for_environment(
  93. '_inc/build/comment-likes/comment-like-count.min.js',
  94. 'modules/comment-likes/comment-like-count.js'
  95. ),
  96. array( 'jquery' ),
  97. JETPACK__VERSION
  98. );
  99. }
  100. public function add_like_count_column( $columns ) {
  101. $columns['comment_likes'] = '<span class="vers"></span>';
  102. return $columns;
  103. }
  104. public function frontend_init() {
  105. if ( is_admin() ) {
  106. return;
  107. }
  108. if ( Jetpack_AMP_Support::is_amp_request() ) {
  109. return;
  110. }
  111. add_action( 'wp_enqueue_scripts', array( $this, 'load_styles_register_scripts' ) );
  112. add_filter( 'comment_text', array( $this, 'comment_likes' ), 10, 2 );
  113. }
  114. public function load_styles_register_scripts() {
  115. if ( ! wp_style_is( 'open-sans', 'registered' ) ) {
  116. wp_register_style( 'open-sans', 'https://fonts.googleapis.com/css?family=Open+Sans', array(), JETPACK__VERSION );
  117. }
  118. wp_enqueue_style( 'jetpack_likes', plugins_url( 'likes/style.css', __FILE__ ), array( 'open-sans' ), JETPACK__VERSION );
  119. wp_enqueue_script(
  120. 'postmessage',
  121. Jetpack::get_file_url_for_environment( '_inc/build/postmessage.min.js', '_inc/postmessage.js' ),
  122. array( 'jquery' ),
  123. JETPACK__VERSION,
  124. false
  125. );
  126. wp_enqueue_script(
  127. 'jetpack_resize',
  128. Jetpack::get_file_url_for_environment(
  129. '_inc/build/jquery.jetpack-resize.min.js',
  130. '_inc/jquery.jetpack-resize.js'
  131. ),
  132. array( 'jquery' ),
  133. JETPACK__VERSION,
  134. false
  135. );
  136. wp_enqueue_script( 'jetpack_likes_queuehandler', plugins_url( 'likes/queuehandler.js' , __FILE__ ), array( 'jquery', 'postmessage', 'jetpack_resize' ), JETPACK__VERSION, true );
  137. }
  138. public function comment_likes( $content, $comment = null ) {
  139. if ( empty( $comment ) ) {
  140. return $content;
  141. }
  142. if ( ! $this->settings->is_likes_visible() ) {
  143. return $content;
  144. }
  145. $comment_id = get_comment_ID();
  146. if ( empty( $comment_id ) && ! empty( $comment->comment_ID ) ) {
  147. $comment_id = $comment->comment_ID;
  148. }
  149. if ( empty( $content ) || empty( $comment_id ) ) {
  150. return $content;
  151. }
  152. // In case master iframe hasn't been loaded. This could be the case when Post Likes module is disabled,
  153. // or on pages on which we have comments but post likes are disabled.
  154. if ( false === has_action( 'wp_footer', 'jetpack_likes_master_iframe' ) ) {
  155. add_action( 'wp_footer', 'jetpack_likes_master_iframe', 21 );
  156. }
  157. $uniqid = uniqid();
  158. $src = sprintf( 'https://widgets.wp.com/likes/#blog_id=%1$d&amp;comment_id=%2$d&amp;origin=%3$s&amp;obj_id=%1$d-%2$d-%4$s', $this->blog_id, $comment_id, $this->domain, $uniqid );
  159. $name = sprintf( 'like-comment-frame-%1$d-%2$d-%3$s', $this->blog_id, $comment_id, $uniqid );
  160. $wrapper = sprintf( 'like-comment-wrapper-%1$d-%2$d-%3$s', $this->blog_id, $comment_id, $uniqid );
  161. $html = '';
  162. $html .= "<div class='jetpack-comment-likes-widget-wrapper jetpack-likes-widget-unloaded' id='$wrapper' data-src='$src' data-name='$name'>";
  163. $html .= "<div class='likes-widget-placeholder comment-likes-widget-placeholder comment-likes'><span class='loading'>" . esc_html__( 'Loading...', 'jetpack' ) . "</span></div>";
  164. $html .= "<div class='comment-likes-widget jetpack-likes-widget comment-likes'><span class='comment-like-feedback'></span>";
  165. $html .= "<span class='sd-text-color'></span><a class='sd-link-color'></a>";
  166. $html .= '</div></div>';
  167. /**
  168. * Filters the Comment Likes button content.
  169. *
  170. * @module comment-likes
  171. *
  172. * @since 5.1.0
  173. *
  174. * @param string $html Comment Likes button content.
  175. */
  176. $like_button = apply_filters( 'comment_like_button', $html );
  177. return $content . $like_button;
  178. }
  179. }
  180. Jetpack_Comment_Likes::init();