-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-reactions.php
452 lines (368 loc) · 11.7 KB
/
wp-reactions.php
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
/*
* Plugin Name: WP Reactions Box
* Description: Real-time emotion/mood rating plugin which allows visitor to rate individual post though smilie icons.
* Version: 1.0
* License: GNU General Public License 3.0 or newer (GPL) http://www.gnu.org/licenses/gpl.html
* Author: Deepak Kumar
*/
define('HKP_FEELBOX_PLUGIN_DIR', plugins_url(basename(dirname(__FILE__))));
global $hkp_reactions_db_version;
global $moods;
global $hkp_reactions_options;
global $themes;
$moods = array(
1 => "bummer",
2 => "good",
3 => "sad",
4 => "lol",
5 => "scary",
6 => "shocked",
7 => "boring",
8 => "sweet",
9 => "angry",
10 => "nerdy",
);
$themes = array(
1 => 'classic',
2 => 'emoji',
3 => 'dark'
);
$hkp_reactions_db_version = "1.0";
require_once('wp-reactions-admin.php');
function hkp_feelbox_install_db_table()
{
global $wpdb;
global $hkp_reactions_db_version;
global $hkp_reactions_options;
$table_name = $wpdb->prefix . 'hkp_reactions_posts';
$installed_ver = get_option("hkp_reactions_db_version");
if ($installed_ver != $hkp_reactions_db_version) {
$sql = "CREATE TABLE " . $table_name . " (
`ID` bigint(20) NOT NULL,
`bummer` bigint(20) DEFAULT '0' ,
`good` bigint(20) DEFAULT '0' ,
`sad` bigint(20) DEFAULT '0' ,
`lol` bigint(20) DEFAULT '0' ,
`scary` bigint(20) DEFAULT '0' ,
`shocked` bigint(20) DEFAULT '0' ,
`boring` bigint(20) DEFAULT '0' ,
`sweet` bigint(20) DEFAULT '0' ,
`angry` bigint(20) DEFAULT '0' ,
`nerdy` bigint(20) DEFAULT '0' ,
`time` datetime DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY `ID` (`ID`)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
add_option("hkp_reactions_db_version", $hkp_reactions_db_version);
}
register_activation_hook(__FILE__, 'hkp_feelbox_install_db_table');
add_action('init', 'hkp_feelbox_init');
function hkp_feelbox_init()
{
$options = get_option('hkp_reactions_options');
if (hkp_reactions_box_autoDisplay($options)) {
add_filter('the_content', 'hkp_reactions_box_to_posts');
}
}
function hkp_reactions_box_html()
{
global $wpdb;
global $post;
$options = get_option('hkp_reactions_options');
$wpReactionTitle = hkp_reactions_box_title($options);
$wpReactionTheme = hkp_reactions_box_theme($options);
$reactionsMoods = hkp_reactions_box_moods($options);
$poweredBy = hkp_reactions_box_poweredBy();
//$randumNum = rand( 0, 1 );
$randumNum = 0;
$reactionsObj = hkp_reactions_votes();
$widgethtml = '<div class="hkp-feelbox-voted" id="hkp-feelbox-widget">';
$widgethtml .= '<div id="bgLayer"></div>
<div id="feelbox-header">
<div id="header-msg">
<a alt="' . $poweredBy[$randumNum]['title'] . '" href="' . $poweredBy[$randumNum]['url'] . '" target="_blank"><span> </span></a>
</div>
<div id="hkp-feelbox-s">
' . $wpReactionTitle . '
</div>
<div style="clear:both"></div>';
//sparkbar or vote bar
if (hkp_reactions_box_showVoteBar($options)) {
$totalVotes = hkp_reactions_totalvotes($reactionsMoods, $reactionsObj);
$widgethtml .= '<div id="sparkbardiv">';
foreach ($reactionsMoods as $mood) {
$width = (isset($reactionsObj->$mood)) ? ($reactionsObj->$mood * 100) / $totalVotes : 0;
$titleAttr = ucfirst($mood) . ' ' . number_format($width, 0, '.', '') . '%';
$widgethtml .= '<div title="' . $titleAttr . '" style="width:' . $width . '%" class="spark ' . $mood . '"></div>';
}
$widgethtml .= '<div style="clear:both"></div></div>';
}
$widgethtml .= '</div>
<div style="" id="bd">';
if (hkp_reactions_box_showSocialshare($options)) {
//Social Share button
$twitter_user = 'midtbmedia';
$widgethtml .= '<div id="hkp-feelbox-social">
<div id="feelbox-msg">Share your vote!</div>
<div>
<a target="_blank" id="hkp-feelbox-twitter-button" class="socialmedia" href="https://twitter.com/intent/tweet?text=' . htmlspecialchars(urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8')), ENT_COMPAT, 'UTF-8') . '&url=' . urlencode(esc_url(get_permalink())) . '&via=' . urlencode($twitter_user ? $twitter_user : get_bloginfo('name')) . '" >share</a>
<a id="hkp-feelbox-facebook-button" class="socialmedia" href="http://www.facebook.com/sharer.php?u=' . urlencode(esc_url(get_permalink())) . '" onclick="window.open(this.href, \'mywin\',\'left=50,top=50,width=600,height=350,toolbar=0\'); return false;">share</a>
<a id="hkp-feelbox-googleplus-button" class="socialmedia" href="http://plus.google.com/share?url=' . esc_url(get_permalink()) . '" onclick="window.open(this.href, \'mywin\',\'left=50,top=50,width=600,height=350,toolbar=0\'); return false;">share</a>
</div>
<div id="hkp-feelbox-s">
<a id="clr" href="javascript:void(0)">No, thanks.</a>
</div>
</div>';
}
//Loading
$widgethtml .= '<div style="display: none;" id="loading"></div>';
$widgethtml .= '<div id="wp-reactions">';
foreach ($reactionsMoods as $mood) {
$moodVotes = (isset($reactionsObj->$mood)) ? $reactionsObj->$mood : 0;
$widgethtml .= '<div class="wp-emotion-icon">
<div alt="' . $mood . '" data-id="' . $mood . '" class="' . $mood . ' feelbox-emotion-pic ' . $wpReactionTheme . '"></div>
<div class="feelbox-emotion-counter" id="num-' . $mood . '">' . hkp_reactions_votes_count($moodVotes) . '</div>
</div>';
}
$widgethtml .= '</div>
</div>
</div>';
return $widgethtml;
}
function hkp_reactions_box_title($options)
{
if (isset($options['showtitle']) && $options['showtitle'] == 0) {
$wpReactionTitle = '';
} else {
if ($options['title'] && !empty($options['title'])) {
$wpReactionTitle = $options['title'];
} else {
$wpReactionTitle = 'How you feel for this post?';
}
}
return $wpReactionTitle;
}
function hkp_reactions_box_theme($options)
{
global $themes;
if (isset($options['theme']) && in_array($options['theme'], $themes)) {
$theme = $options['theme'];
} else {
$theme = $themes[1];
}
return $theme;
}
function hkp_reactions_box_poweredBy()
{
$poweredBy = array(
0 => array(
'url' => 'http://www.midtb.org/',
'title' => 'Powered by midtb.org - News, Entertainment, Technology news'
),
1 => array(
'url' => 'http://www.megashare-viooz.eu/',
'title' => 'Powered by Megashare-viooz.eu - Movies and Tv shows Database'
)
);
return $poweredBy;
}
function hkp_reactions_box_moods($options)
{
if (isset($options['moods'])) {
return $options['moods'];
} else {
global $moods;
return $moods;
}
}
function hkp_reactions_box_showVoteBar($options)
{
if (isset($options['votebar']) && $options['votebar'] == 0) {
return false;
} else {
return true;
}
}
function hkp_reactions_box_autoDisplay($options)
{
if (isset($options['autoshow']) && $options['autoshow'] == 0) {
return false;
} else {
return true;
}
}
function hkp_reactions_box_showSocialshare($options)
{
if (isset($options['socialshare']) && $options['socialshare'] == 0) {
return false;
} else {
return true;
}
}
function hkp_reactions_votes($postID = null)
{
global $wpdb;
if (!$postID) {
global $post;
$postID = $post->ID;
}
$votes = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}hkp_reactions_posts WHERE ID=" . $postID);
return $votes;
}
function hkp_reactions_totalvotes($reactionsMoods = null, $reactionsObj = null)
{
$totalVote = 0;
if (isset($reactionsMoods) && isset($reactionsObj)) {
foreach ($reactionsMoods as $mood) {
$moodVotes = ($reactionsObj->$mood) ? $reactionsObj->$mood : 0;
$totalVote = $totalVote + $moodVotes;
}
}
return $totalVote;
}
/*
* Auto add widget after post content.
*/
function hkp_reactions_box_to_posts($content)
{
if (is_single()) {
$content .= hkp_reactions_box_html();
}
return $content;
}
/*
* Function to call widget at in theme location
*/
function hkp_reactions_box()
{
if (is_single() || is_page() && !(is_home())) {
echo hkp_reactions_box_html();
}
}
/*
* Shortcode for widget call in post, pages
*/
add_shortcode('hkp_reactions_box', 'hkp_reactions_box');
function hkp_print_feelbox_shortcode()
{
if (is_single() || is_page() && !(is_home())) {
return feelbox_get_widget_html();
}
}
function hkp_feelbox_register_styles()
{
wp_enqueue_style('hkp_feelbox_style', plugins_url('css/style.css', __FILE__));
wp_enqueue_script('hkp_feelbox_ajax_script', plugins_url('js/ajax.js', __FILE__), array('jquery'));
global $post;
$options = get_option('hkp_reactions_options');
$nonce = wp_create_nonce('hkp-wp-feelbox');
$votebar = (hkp_reactions_box_showVoteBar($options)) ? 1 : 0;
$socialShare = (hkp_reactions_box_showSocialshare($options)) ? 1 : 0;
$enableAjaxPopulate = ($options['onloadajax'] == 1) ? 1 : 0;
if (is_single() || is_page() && !(is_home())) {
wp_localize_script('hkp_feelbox_ajax_script', 'hkpFeelboxAjax', array(
'ajaxurl' => admin_url('admin-ajax.php'),
'action' => 'wp_feelbox_cast_vote',
'token' => $nonce,
'id' => $post->ID,
'voteBar' => $votebar,
'social' => $socialShare,
'onloadAjax' => $enableAjaxPopulate,
'onloadaction' => 'hkp_reactions_populate',
));
}
}
add_action('wp_enqueue_scripts', 'hkp_feelbox_register_styles');
add_action('wp_ajax_wp_feelbox_cast_vote', 'hkp_feelbox_cast_vote');
add_action('wp_ajax_nopriv_wp_feelbox_cast_vote', 'hkp_feelbox_cast_vote');
function hkp_feelbox_cast_vote()
{
$nonce = $_POST['token'];
// is this a valid request?
if (!wp_verify_nonce($nonce, 'hkp-wp-feelbox')) {
die("Oops!");
}
$postid = $_POST['postID'];
$reaction = $_POST['reaction'];
$response = hkp_feelbox_save_results($reaction, $postid);
// response output
header("Content-Type: application/json");
echo $response;
exit;
}
function hkp_feelbox_save_results($vote, $postid)
{
global $wpdb;
global $feelbox_wp_options;
global $moods;
$wpdb->show_errors();
if ($vote && in_array($vote, $moods)) {
$table_name = $wpdb->prefix . 'hkp_reactions_posts';
$voteFields = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM " . $table_name . " WHERE ID=%s",
$postid
)
);
$options = get_option('hkp_reactions_options');
$votecount = (isset($voteFields->$vote)) ? $voteFields->$vote : '';
$reactionsMoods = hkp_reactions_box_moods($options);
$totalCount = hkp_reactions_totalvotes($reactionsMoods, $voteFields);
//$recordexists = (sizeof( $votecount ) > 0) ? true : false;
if ($voteFields) {
$row = $wpdb->update(
$table_name,
array('ID' => $postid, $vote => $votecount + 1, 'time' => current_time('mysql')),
array('ID' => $postid)
);
} else {
$row = $wpdb->insert(
$table_name,
array('ID' => $postid, $vote => 1, 'time' => current_time('mysql'))
);
}
// generate the response
$response = json_encode(array('success' => true, 'vote' => $vote, 'formatedVote' => hkp_reactions_votes_count($votecount + 1), 'voteCount' => $votecount + 1, 'totalvotes' => $totalCount + 1));
return $response;
} else {
$response = json_encode(array('success' => false));
return $response;
}
}
/*
* Ajax to populat all moods/recations if theme caching enabled
*/
add_action('wp_ajax_hkp_reactions_populate', 'hkp_reactions_ajax_populate');
add_action('wp_ajax_nopriv_hkp_reactions_populate', 'hkp_reactions_ajax_populate');
function hkp_reactions_ajax_populate()
{
global $wpdb;
$postID = $_POST['postID'];
$options = get_option('hkp_reactions_options');
$reactionsMoods = hkp_reactions_box_moods($options);
$reactionsObj = hkp_reactions_votes($postID);
$response = [];
$totalVote = 0;
foreach ($reactionsMoods as $mood) {
$moodVotes = ($reactionsObj->$mood) ? $reactionsObj->$mood : 0;
$response['reactions'][$mood] = $moodVotes;
$totalVote = $totalVote + $moodVotes;
}
$response['totalVotes'] = $totalVote;
$response = json_encode($response);
header("Content-Type: application/json");
echo $response;
exit;
}
function hkp_reactions_votes_count($votes)
{
if ($votes > 999) {
$vote = $votes / 1000;
return floor($vote) . "k+";
} else {
return $votes;
}
}