Completed
Push — add/jetpack-start-reconnect-us... ( 7f1fbb )
by
unknown
10:36
created

Jetpack_Comment_Likes::frontend_init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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
Jetpack::dns_prefetch( array(
15
	'//widgets.wp.com',
16
	'//s0.wp.com',
17
	'//s1.wp.com',
18
	'//s2.wp.com',
19
	'//0.gravatar.com',
20
	'//1.gravatar.com',
21
	'//2.gravatar.com',
22
) );
23
24
require_once dirname( __FILE__ ) . '/likes/jetpack-likes-master-iframe.php';
25
require_once dirname( __FILE__ ) . '/likes/jetpack-likes-settings.php';
26
27
class Jetpack_Comment_Likes {
28
	public static function init() {
29
		static $instance = NULL;
30
31
		if ( ! $instance ) {
32
			$instance = new Jetpack_Comment_Likes;
33
		}
34
35
		return $instance;
36
	}
37
38
	private function __construct() {
39
		$this->settings = new Jetpack_Likes_Settings();
0 ignored issues
show
Bug introduced by
The property settings does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40
		add_action( 'init', 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 View Code Duplication
			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 View Code Duplication
			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 View Code Duplication
			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
		$blog_id = Jetpack_Options::get_option( 'id' );
86
87
		$permalink = get_permalink( get_the_ID() );
88
		?>
89
		<a title=""
90
		   data-comment-id="<?php echo (int) $comment_id; ?>"
91
		   data-blog-id="<?php echo (int) $blog_id; ?>"
92
		   class="comment-like-count"
93
		   id="comment-like-count-<?php echo (int) $comment_id; ?>"
94
		   href="<?php echo esc_url( $permalink ); ?>#comment-<?php echo (int) $comment_id; ?>"
95
		>
96
			<span class="like-count">0</span>
97
		</a>
98
		<?php
99
	}
100
101
	function enqueue_admin_styles_scripts() {
102
		wp_enqueue_style( 'comment-like-count', plugins_url( 'comment-likes/admin-style.css', __FILE__ ), array(), JETPACK__VERSION );
103
		wp_enqueue_script( 'comment-like-count', plugins_url( 'comment-likes/comment-like-count.js', __FILE__ ), array( 'jquery' ), JETPACK__VERSION );
104
	}
105
106
	public function add_like_count_column( $columns ) {
107
		$columns['comment_likes'] = '<span class="vers"><img title="' . esc_attr__( 'Comment likes', 'jetpack' ) . '" alt="' . esc_attr__( 'Comment likes', 'jetpack' ) . '" src="//s0.wordpress.com/i/like-grey-icon.png" /></span>';
108
109
		return $columns;
110
	}
111
112
	public function frontend_init() {
113
		if ( is_admin() ) {
114
			return;
115
		}
116
117
		add_action( 'wp_enqueue_scripts', array( $this, 'load_styles_register_scripts' ) );
118
		add_filter( 'comment_text', array( $this, 'comment_likes' ), 10, 2 );
119
	}
120
121
	public function load_styles_register_scripts() {
122
		wp_enqueue_style( 'jetpack_likes', plugins_url( 'likes/style.css', __FILE__ ), array(), JETPACK__VERSION );
123
		wp_enqueue_script( 'postmessage', plugins_url( '_inc/postmessage.js', dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false );
124
		wp_enqueue_script( 'jetpack_resize', plugins_url( '_inc/jquery.jetpack-resize.js' , dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false );
125
		wp_enqueue_script( 'jetpack_likes_queuehandler', plugins_url( 'likes/queuehandler.js' , __FILE__ ), array( 'jquery', 'postmessage', 'jetpack_resize' ), JETPACK__VERSION, true );
126
	}
127
128
	public function comment_likes( $content, $comment = null ) {
129
		if ( empty( $comment ) ) {
130
			return $content;
131
		}
132
133
		if ( ! $this->settings->is_likes_visible() ) {
134
			return $content;
135
		}
136
137
		$blog_id   = Jetpack_Options::get_option( 'id' );
138
		$url       = home_url();
139
		$url_parts = parse_url( $url );
140
		$domain    = $url_parts['host'];
141
142
		$comment_id = get_comment_ID();
143
		if ( empty( $comment_id ) && ! empty( $comment->comment_ID ) ) {
144
			$comment_id = $comment->comment_ID;
145
		}
146
147
		if ( empty( $content ) || empty( $comment_id ) ) {
148
			return $content;
149
		}
150
151
		// In case master iframe hasn't been loaded. This could be the case when Post Likes module is disabled,
152
		// or on pages on which we have comments but post likes are disabled.
153
		if ( ! has_action( 'wp_footer', 'jetpack_likes_master_iframe' ) ) {
154
			add_action( 'wp_footer', 'jetpack_likes_master_iframe', 21 );
155
		}
156
157
		$uniqid = uniqid();
158
159
		$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', $blog_id, $comment_id, $domain, $uniqid );
160
		$name    = sprintf( 'like-comment-frame-%1$d-%2$d-%3$s', $blog_id, $comment_id, $uniqid );
161
		$wrapper = sprintf( 'like-comment-wrapper-%1$d-%2$d-%3$s', $blog_id, $comment_id, $uniqid );
162
163
		$html = '';
164
		$html .= "<div class='jetpack-comment-likes-widget-wrapper jetpack-likes-widget-unloaded' id='$wrapper' data-src='$src' data-name='$name'>";
165
		$html .= "<div class='likes-widget-placeholder comment-likes-widget-placeholder comment-likes'><span class='loading'>" . esc_html__( 'Loading...', 'jetpack' ) . "</span></div>";
166
		$html .= "<div class='comment-likes-widget jetpack-likes-widget comment-likes'><span class='comment-like-feedback'></span>";
167
		$html .= "<span class='sd-text-color'></span><a class='sd-link-color'></a>";
168
		$html .= '</div></div>';
169
170
		/**
171
		 * Filters the Comment Likes button content.
172
		 *
173
		 * @module comment-likes
174
		 *
175
		 * @since 5.1.0
176
		 *
177
		 * @param string Comment Likes button content.
178
		 */
179
		$like_button = apply_filters( 'comment_like_button', $html, '' );
180
181
		return $content . $like_button;
182
	}
183
}
184
185
Jetpack_Comment_Likes::init();
186