Нема описа

comment-likes.php 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
  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. * @package automattic/jetpack
  14. */
  15. use Automattic\Jetpack\Assets;
  16. Assets::add_resource_hint( '//widgets.wp.com', 'dns-prefetch' );
  17. require_once __DIR__ . '/likes/jetpack-likes-master-iframe.php';
  18. require_once __DIR__ . '/likes/jetpack-likes-settings.php';
  19. /**
  20. * Jetpack Comment Like Class
  21. */
  22. class Jetpack_Comment_Likes {
  23. /**
  24. * Initialize comment like module
  25. */
  26. public static function init() {
  27. static $instance = null;
  28. if ( ! $instance ) {
  29. $instance = new Jetpack_Comment_Likes();
  30. }
  31. return $instance;
  32. }
  33. /**
  34. * Construct comment like module.
  35. */
  36. private function __construct() {
  37. $this->settings = new Jetpack_Likes_Settings();
  38. $this->blog_id = Jetpack_Options::get_option( 'id' );
  39. $this->url = home_url();
  40. $this->url_parts = wp_parse_url( $this->url );
  41. $this->domain = $this->url_parts['host'];
  42. add_action( 'template_redirect', array( $this, 'frontend_init' ) );
  43. add_action( 'admin_init', array( $this, 'admin_init' ) );
  44. if ( ! Jetpack::is_module_active( 'likes' ) ) {
  45. $active = Jetpack::get_active_modules();
  46. if ( ! in_array( 'sharedaddy', $active, true ) && ! in_array( 'publicize', $active, true ) ) {
  47. // we don't have a sharing page yet.
  48. add_action( 'admin_menu', array( $this->settings, 'sharing_menu' ) );
  49. }
  50. if ( in_array( 'publicize', $active, true ) && ! in_array( 'sharedaddy', $active, true ) ) {
  51. // we have a sharing page but not the global options area.
  52. add_action( 'pre_admin_screen_sharing', array( $this->settings, 'sharing_block' ), 20 );
  53. add_action( 'pre_admin_screen_sharing', array( $this->settings, 'updated_message' ), -10 );
  54. }
  55. if ( ! in_array( 'sharedaddy', $active, true ) ) {
  56. add_action( 'admin_init', array( $this->settings, 'process_update_requests_if_sharedaddy_not_loaded' ) );
  57. add_action( 'sharing_global_options', array( $this->settings, 'admin_settings_showbuttonon_init' ), 19 );
  58. add_action( 'sharing_admin_update', array( $this->settings, 'admin_settings_showbuttonon_callback' ), 19 );
  59. add_action( 'admin_init', array( $this->settings, 'add_meta_box' ) );
  60. } else {
  61. add_filter( 'sharing_meta_box_title', array( $this->settings, 'add_likes_to_sharing_meta_box_title' ) );
  62. add_action( 'start_sharing_meta_box_content', array( $this->settings, 'meta_box_content' ) );
  63. }
  64. add_action( 'save_post', array( $this->settings, 'meta_box_save' ) );
  65. add_action( 'edit_attachment', array( $this->settings, 'meta_box_save' ) );
  66. add_action( 'sharing_global_options', array( $this->settings, 'admin_settings_init' ), 20 );
  67. add_action( 'sharing_admin_update', array( $this->settings, 'admin_settings_callback' ), 20 );
  68. }
  69. }
  70. /**
  71. * Initialize admin section
  72. */
  73. public function admin_init() {
  74. add_filter( 'manage_edit-comments_columns', array( $this, 'add_like_count_column' ) );
  75. add_action( 'manage_comments_custom_column', array( $this, 'comment_likes_edit_column' ), 10, 2 );
  76. add_action( 'admin_print_styles-edit-comments.php', array( $this, 'enqueue_admin_styles_scripts' ) );
  77. }
  78. /**
  79. * Displays number of comment likes in comment admin page.
  80. *
  81. * @param string $column_name name of the column.
  82. * @param int $comment_id ID of the comment.
  83. */
  84. public function comment_likes_edit_column( $column_name, $comment_id ) {
  85. if ( 'comment_likes' !== $column_name ) {
  86. return;
  87. }
  88. $permalink = get_permalink( get_the_ID() );
  89. ?>
  90. <a
  91. data-comment-id="<?php echo absint( $comment_id ); ?>"
  92. data-blog-id="<?php echo absint( $this->blog_id ); ?>"
  93. class="comment-like-count"
  94. id="comment-like-count-<?php echo absint( $comment_id ); ?>"
  95. href="<?php echo esc_url( $permalink ); ?>#comment-<?php echo absint( $comment_id ); ?>"
  96. >
  97. <span class="like-count">0</span>
  98. </a>
  99. <?php
  100. }
  101. /**
  102. * Enqueue admin style scripts.
  103. */
  104. public function enqueue_admin_styles_scripts() {
  105. wp_enqueue_style( 'comment-like-count', plugins_url( 'comment-likes/admin-style.css', __FILE__ ), array(), JETPACK__VERSION );
  106. wp_enqueue_script(
  107. 'comment-like-count',
  108. Assets::get_file_url_for_environment(
  109. '_inc/build/comment-likes/comment-like-count.min.js',
  110. 'modules/comment-likes/comment-like-count.js'
  111. ),
  112. array( 'jquery' ),
  113. JETPACK__VERSION,
  114. false
  115. );
  116. }
  117. /**
  118. * Adds like count column to admin page.
  119. *
  120. * @param array $columns column of admin table.
  121. */
  122. public function add_like_count_column( $columns ) {
  123. $columns['comment_likes'] = '<span class="vers"></span>';
  124. return $columns;
  125. }
  126. /**
  127. * Initialize front end
  128. */
  129. public function frontend_init() {
  130. if ( Jetpack_AMP_Support::is_amp_request() ) {
  131. return;
  132. }
  133. add_action( 'wp_enqueue_scripts', array( $this, 'load_styles_register_scripts' ) );
  134. add_filter( 'comment_text', array( $this, 'comment_likes' ), 10, 2 );
  135. }
  136. /**
  137. * Load styling scripts
  138. */
  139. public function load_styles_register_scripts() {
  140. if ( ! $this->settings->is_likes_visible() ) {
  141. return;
  142. }
  143. if ( ! wp_style_is( 'open-sans', 'registered' ) ) {
  144. wp_register_style( 'open-sans', 'https://fonts.googleapis.com/css?family=Open+Sans', array(), JETPACK__VERSION );
  145. }
  146. wp_enqueue_style( 'jetpack_likes', plugins_url( 'likes/style.css', __FILE__ ), array( 'open-sans' ), JETPACK__VERSION );
  147. wp_enqueue_script(
  148. 'postmessage',
  149. Assets::get_file_url_for_environment( '_inc/build/postmessage.min.js', '_inc/postmessage.js' ),
  150. array(),
  151. JETPACK__VERSION,
  152. true
  153. );
  154. wp_enqueue_script(
  155. 'jetpack_resize',
  156. Assets::get_file_url_for_environment(
  157. '_inc/build/jquery.jetpack-resize.min.js',
  158. '_inc/jquery.jetpack-resize.js'
  159. ),
  160. array( 'jquery' ),
  161. JETPACK__VERSION,
  162. true
  163. );
  164. wp_enqueue_script( 'jetpack_likes_queuehandler', plugins_url( 'likes/queuehandler.js', __FILE__ ), array( 'jquery', 'postmessage', 'jetpack_resize' ), JETPACK__VERSION, true );
  165. }
  166. /**
  167. * Display like count.
  168. *
  169. * @param string $content text content of the comment itself.
  170. * @param object $comment comment object containing comment data.
  171. */
  172. public function comment_likes( $content, $comment = null ) {
  173. if ( empty( $comment ) ) {
  174. return $content;
  175. }
  176. if ( ! $this->settings->is_likes_visible() ) {
  177. return $content;
  178. }
  179. $comment_id = get_comment_ID();
  180. if ( empty( $comment_id ) && ! empty( $comment->comment_ID ) ) {
  181. $comment_id = $comment->comment_ID;
  182. }
  183. if ( empty( $content ) || empty( $comment_id ) ) {
  184. return $content;
  185. }
  186. // In case master iframe hasn't been loaded. This could be the case when Post Likes module is disabled,
  187. // or on pages on which we have comments but post likes are disabled.
  188. if ( false === has_action( 'wp_footer', 'jetpack_likes_master_iframe' ) ) {
  189. add_action( 'wp_footer', 'jetpack_likes_master_iframe', 21 );
  190. }
  191. $uniqid = uniqid();
  192. $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 );
  193. $name = sprintf( 'like-comment-frame-%1$d-%2$d-%3$s', $this->blog_id, $comment_id, $uniqid );
  194. $wrapper = sprintf( 'like-comment-wrapper-%1$d-%2$d-%3$s', $this->blog_id, $comment_id, $uniqid );
  195. $html = '';
  196. $html .= "<div class='jetpack-comment-likes-widget-wrapper jetpack-likes-widget-unloaded' id='$wrapper' data-src='$src' data-name='$name'>";
  197. $html .= "<div class='likes-widget-placeholder comment-likes-widget-placeholder comment-likes'><span class='loading'>" . esc_html__( 'Loading...', 'jetpack' ) . '</span></div>';
  198. $html .= "<div class='comment-likes-widget jetpack-likes-widget comment-likes'><span class='comment-like-feedback'></span>";
  199. $html .= "<span class='sd-text-color'></span><a class='sd-link-color'></a>";
  200. $html .= '</div></div>';
  201. /**
  202. * Filters the Comment Likes button content.
  203. *
  204. * @module comment-likes
  205. *
  206. * @since 5.1.0
  207. *
  208. * @param string $html Comment Likes button content.
  209. */
  210. $like_button = apply_filters( 'comment_like_button', $html );
  211. return $content . $like_button;
  212. }
  213. }
  214. Jetpack_Comment_Likes::init();