-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupvote_function.txt
318 lines (269 loc) · 9.13 KB
/
upvote_function.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
/**
* Theme Functions
*
* @version 1.0.0
* @package Upvote
*/
// Hide toolbar for all users on front-end
show_admin_bar(false);
require_once( 'inc/theme-options.php' );
require_once( 'inc/bootstrap-nav-walker.php' );
require_once( 'inc/votings.php' );
/**
* Set the content width based on theme's design and stylesheet
*/
if ( ! isset ( $content_width ) ) {
$content_width = 640; # Pixels
}
/**
* Register theme features
*
* @hook after_setup_theme
*/
function upvote_theme_setup() {
// Add default posts and comments RSS feed links to head.
add_theme_support( 'automatic-feed-links' );
// Enable support for post thumbnails
add_theme_support( 'post-thumbnails' );
// Enable HTML 5 Support in WordPress
add_theme_support( 'html5' );
// Navigation Menus
register_nav_menus(array(
'primary' => 'Main Navigation'
));
}
add_action( 'after_setup_theme', 'upvote_theme_setup' );
/**
* Enqueue scripts and stylesheets
*
* @hook wp_enqueue_scripts
*/
function upvote_enqueue_scripts() {
wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/css/bootstrap.css', null, '3.0.3' );
wp_enqueue_style( 'upvote', get_stylesheet_uri(), null, '1.0.0' );
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array( 'jquery' ), '3.0.3', 1 );
wp_enqueue_script( 'upvote', get_template_directory_uri() . '/js/upvote.js', array( 'jquery' ) );
wp_localize_script( 'upvote', 'ajaxurl', admin_url( 'admin-ajax.php' ) );
}
add_action( 'wp_enqueue_scripts', 'upvote_enqueue_scripts' );
/**
* Filters `wp_title` to print a neat `<title>` tag based on what is being viewed.
*
* @global int $page
* @global int $paged
* @param string $title
* @param string $sep
* @return string
*/
function upvote_wp_title( $title, $sep ) {
global $page, $paged;
if ( is_feed() ) {
return $title;
}
$title .= get_bloginfo('name');
$site_description = get_bloginfo('description', 'display');
if ( $site_description && ( is_home() || is_front_page() ) ) {
$title .= " - $site_description";
}
if ( $paged >= 2 || $page >=2 ) {
$title .= "$sep " . sprintf( "Page %s", max( $page, $paged ) );
}
return $title;
}
add_filter( 'wp_title', 'upvote_wp_title', 10, 2 );
// Ensure only logged-in can comment.
add_filter( 'pre_option_comment_registration', '__return_true' );
/**
* Comment Callback
*
*
*/
function upvote_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
$user_id = get_current_user_id();
$upvotes = get_user_meta( $user_id, '_votes_type_comment_up' );
$downvotes = get_user_meta( $user_id, '_votes_type_comment_down' );
if ( empty ( $upvotes ) ) $upvotes = array();
if ( empty ( $downvotes ) ) $downvotes = array();
switch ( $comment->comment_type ) {
case 'pingback':
case 'trackback':
?>
<p>Pingback: <?php comment_author_link(); ?><?php edit_comment_link( 'Edit', ' ' ); ?></p>
<?php
break;
default:
if ( $depth < 2 ) {
$class = 'col-lg-12';
} else {
$class = sprintf( 'col-sm-%d col-sm-offset-%d', 13 - $depth , $depth - 1 );
}
?>
<div class="row">
<div class="<?php echo $class; ?>">
<div class="table-comment">
<!-- <div class="vote">
<?php if ( $comment->user_id != $user_id ): ?>
<?php if ( ! in_array( $comment->comment_ID, $upvotes ) ): ?>
<a class="upvote-ajax" href="<?php echo upvote_get_vote_url( get_comment_ID(), 'comment' ); ?>"><img src="<?php echo get_template_directory_uri(); ?>/img/up.png" alt="Up vote" /></a>
<?php endif; ?>
add points under comment upvote
<?php
printf(
'%d',
upvote_get_points( $comment->comment_ID, 'comment' ),
bp_core_get_userlink( $comment->user_id ),
human_time_diff( strtotime( $comment->comment_date_gmt ) ),
get_permalink( $comment->comment_post_ID )
);
?>
remove comment downvote
<?php if ( ! in_array( $comment->comment_ID, $downvotes ) ): ?>
<a class="upvote-ajax" href="<?php echo upvote_get_vote_url( get_comment_ID(), 'comment', array( 'downvote' => true ) ); ?>"><img src="<?php echo get_template_directory_uri(); ?>/img/down.png" alt="Down vote" /></a>
<?php endif; ?>
<?php endif; ?>
</div> -->
<div class="comment-block">
<div id="comment-<?php comment_ID(); ?>">
<?php if ( is_singular() ): ?>
<p class="byline"><?php
printf(
'%s %s ago — <a href="%s">link</a>',
bp_core_get_userlink( $comment->user_id ),
human_time_diff( strtotime( $comment->comment_date_gmt ) ),
get_comment_link( $comment )
);
?></p>
<?php else: ?>
<p class="byline"><?php
printf(
'%d points %s %s ago — <a href="%s">See post</a>',
upvote_get_points( $comment->comment_ID, 'comment' ),
bp_core_get_userlink( $comment->user_id ),
human_time_diff( strtotime( $comment->comment_date_gmt ) ),
get_permalink( $comment->comment_post_ID )
);
?></p>
<?php endif; ?>
<div id="div-comment-<?php comment_ID(); ?>" class="comment-body">
<?php if ( $comment->comment_approved == '0' ) : ?>
<div class="alert alert-warning">
<em class="comment-awaiting-moderation">Your comment is awaiting moderation.</em>
</div>
<?php endif; ?>
<?php comment_text( get_comment_id() ); ?>
</div>
<?php if ( is_singular() ): ?>
<p>
<?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
<?php edit_comment_link( 'Edit', ' | ' ); ?>
</p>
<?php endif; ?>
</div>
</div>
</div><!-- end table -->
</div>
<?php
}
}
/**
* Query filter
*
* @param WP_Query $query
*/
function upvote_pre_get_posts( &$query ) {
if ( ! $query->is_main_query() )
return;
if ( $query->is_home() ) {
if ( ! isset ( $_REQUEST['order'] ) || 'new' !== $_REQUEST['order'] ) {
$query->set( 'meta_key', '_upvotes' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'DESC' );
add_filter( 'posts_orderby', 'upvote_posts_orderby', 0, 2 );
add_filter( 'posts_fields', 'upvote_posts_fields', 0, 2);
}
}
if ( $query->is_author() && isset ( $query->query_vars['comments'] ) ) {
add_filter( 'posts_where', 'upvote_invalidate_author_query', 0, 2 );
}
}
add_action( 'pre_get_posts', 'upvote_pre_get_posts', 0, 1 );
function upvote_posts_orderby( $order, $query ) {
if ( $query->is_main_query() && $query->is_home() ) {
$order = 'score DESC';
}
return $order;
}
function upvote_posts_fields( $fields, $query ) {
if ( $query->is_main_query() && $query->is_home() ) {
global $wpdb;
$now = date( 'Y-m-d H:i:s', current_time( 'timestamp', true ) );
$gravity = upvote_theme_options( 'point-gravity' );
if ( ! $gravity ) $gravity = 1.8;
$default_points = ( int ) upvote_theme_options('default-points');
$fields .= ', ( ( ' . $wpdb->postmeta . '.meta_value + ' . $default_points . ' - 1 ) / pow( ( TIMESTAMPDIFF( SECOND, ' . $wpdb->posts . '.post_date_gmt, "'. $now .'" ) / 3600 ) + 2, ' . $gravity . ' ) ) as score';
}
return $fields;
}
/**
*
* @param string $where
* @param WP_Query $query
* @return string
*/
function upvote_invalidate_author_query( $where, $query ) {
if ( $query->is_main_query() && $query->is_author() ) {
$where = 'AND 0';
$comment_query = new WP_Comment_Query();
$query->comments = $comment_query->query( array(
'user_id' => get_queried_object_id(),
'number' => 50
) );
}
return $where;
}
/**
* Add rewrite rules
*
*/
function upvote_rewrite_rules() {
add_rewrite_endpoint( 'comments', EP_AUTHORS );
}
add_action( 'init', 'upvote_rewrite_rules' );
/*** Buddypress Hooks *********************************************************/
function upvote_wrap_signup_error( $error ) {
if ( ! $error ) {
return $error;
}
return '<div class="text-danger">' . $error . '</div>';
}
add_filter( 'bp_members_signup_error_message', 'upvote_wrap_signup_error' );
function upvote_bp_setup_nav() {
$bp = buddypress();
if ( ! bp_is_active ( 'xprofile' ) ) {
unset( $bp->bp_nav['profile'] );
unset( $bp->bp_options_nav['profile'] );
}
}
add_action( 'bp_setup_nav', 'upvote_bp_setup_nav', 100 );
/**
*
* @global wpdb $wpdb
* @param BP_User_Query $bp_user_query
*/
function upvote_bp_pre_user_query( &$bp_user_query ) {
global $wpdb;
$order_posts = <<<ORDER
SELECT SUM(pm.meta_value) FROM {$wpdb->posts} AS p INNER JOIN {$wpdb->postmeta} AS pm ON(p.ID = pm.post_id AND pm.meta_key = '_upvotes') WHERE p.post_author = u.user_id AND p.post_type = "post" AND p.post_status = "publish" GROUP BY p.post_author
ORDER;
$order_comments = <<<ORDER
SELECT SUM(cm.meta_value) FROM {$wpdb->comments} as c INNER JOIN {$wpdb->commentmeta} AS cm ON(c.comment_ID = cm.comment_id AND cm.meta_key = "_upvotes") WHERE c.user_id = u.user_id AND c.comment_approved = '1' GROUP BY c.user_id
ORDER;
$bp_user_query->uid_clauses['orderby'] = "ORDER BY ( IFNULL( ({$order_posts}), 0 ) + IFNULL( ({$order_comments}), 0 ) )";
}
add_action( 'bp_pre_user_query', 'upvote_bp_pre_user_query', 0, 1 );