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