Changeset 1076493
- Timestamp:
- 01/27/2015 07:17:12 AM (11 years ago)
- Location:
- reaction-buttons/trunk
- Files:
-
- 2 edited
-
reaction_buttons.css (modified) (2 diffs)
-
reaction_buttons.php (modified) (31 diffs)
Legend:
- Unmodified
- Added
- Removed
-
reaction-buttons/trunk/reaction_buttons.css
r308543 r1076493 2 2 div.reaction_buttons{ 3 3 margin-top: 20px; 4 margin-bottom: 25px; 4 5 font-size: 0.8em; 5 6 text-align: left; 6 7 } 7 8 8 /* a single button */ 9 div.reaction_buttons .reaction_button { 10 line-height: 3.2em; 9 /* a single normal button */ 10 div.reaction_buttons li.reaction_button { 11 border: 1px dotted #000; 12 line-height: 2em; 11 13 padding: 5px; 12 margin: 0 3px; 13 margin-bottom: 25px; 14 border: 1px dotted #000; 14 margin: 3px; 15 15 cursor: pointer; 16 list-style: none; 17 text-align: center; 18 display: inline-block; 19 20 -moz-box-shadow: inset 0 1px 0 0 rgba(255, 255, 255, 0.6); 21 -webkit-box-shadow: inset 0 1px 0 0 rgba(255, 255, 255, 0.6); 22 box-shadow: inset 0 1px 0 0 rgba(255, 255, 255, 0.6); 16 23 } 17 24 18 25 /* the button while hovering over it */ 19 div.reaction_buttons > .reaction_button:hover { 20 background-color: #0F6; 26 div.reaction_buttons li.reaction_button:hover { 27 background: #4ecdc4; 28 border: 1px solid #4ecdc4; 29 background-image: -moz-linear-gradient(#76d8d1, #4ecdc4 70%); 30 background-image: -webkit-linear-gradient(#76d8d1, #4ecdc4 70%); 31 background-image: linear-gradient(#76d8d1, #4ecdc4 70%); 21 32 } 22 33 23 /* the button after voting 24 div.reaction_buttons > .voted, div.reaction_buttons > .voted:hover { 34 /* special setings for display as a graph, 35 * inspired by http://geoffgraham.me/creating-a-responsive-css-bar-chart/ */ 36 div.reaction_buttons ul.graph { 37 clear: both; 38 padding: 0; 39 width: 100%; 25 40 } 26 /* 41 div.reaction_buttons ul.graph li.reaction_button { 42 height: 125px; 43 padding: 0; 44 position: relative; 45 vertical-align: bottom; 46 margin: 0 auto; 47 padding-left: 10px; 48 text-align: left; 49 50 background: #4ecdc4; 51 border: 1px solid #4ecdc4; 52 background-image: -moz-linear-gradient(#76d8d1, #4ecdc4 70%); 53 background-image: -webkit-linear-gradient(#76d8d1, #4ecdc4 70%); 54 background-image: linear-gradient(#76d8d1, #4ecdc4 70%); 55 -moz-border-radius: 4px 4px 0 0; 56 -webkit-border-radius: 4px; 57 border-radius: 4px 4px 0 0; 58 -moz-box-shadow: inset 0 1px 0 0 rgba(255, 255, 255, 0.6); 59 -webkit-box-shadow: inset 0 1px 0 0 rgba(255, 255, 255, 0.6); 60 box-shadow: inset 0 1px 0 0 rgba(255, 255, 255, 0.6); 61 } 62 @media (min-width: 700px) { 63 div.reaction_buttons ul.graph { 64 height: 250px; 65 } 66 div.reaction_buttons ul.graph li.reaction_button { 67 height: 250px; 68 margin: 0 1.8% 0 0; 69 padding: 0; 70 width: 18%; 71 } 72 div.reaction_buttons ul.graph li.reaction_button > div { 73 bottom: -4em; 74 position: absolute; 75 } 76 } 77 78 /* the button after voting */ 79 div.reaction_buttons li.voted, 80 div.reaction_buttons li.voted:hover { 81 cursor: default; 82 } 27 83 28 84 /* number of votes including parantheses. … … 36 92 */ 37 93 38 94 /* simple sidebar settings */ 39 95 #sidebar div.widget_reaction_buttons h3 { 40 96 margin: 0; -
reaction-buttons/trunk/reaction_buttons.php
r1065133 r1076493 4 4 Plugin URI: http://blog.jl42.de/reaction-buttons/ 5 5 Description: Adds Buttons for very simple and fast feedback to your post. Inspired by Blogger. 6 Version: 1.8.26 Version: 2.0.0 7 7 Author: Jakob Lenfers 8 8 Author URI: http://blog.jl42.de … … 31 31 // Determine the location 32 32 $reaction_buttons_plugin_path = WP_CONTENT_URL.'/plugins/'.plugin_basename(dirname(__FILE__)).'/'; 33 34 /**35 * Quote the button name, so that JQuery works36 */37 function prepare_js_jl($str) {38 return str_replace("'", "\\\\'", esc_js(trim($str)));39 }40 41 42 /**43 * Replace spaces with three underscores for class names44 */45 function prepare_attr_jl($str) {46 return esc_attr(str_replace(" ", "___", stripslashes(trim($str))));47 }48 49 33 50 34 /** … … 77 61 $use_percentages = get_option("reaction_buttons_percentages", false); 78 62 $use_percentages_precision = get_option("reaction_buttons_percentages_precision", 1); 79 80 // show results only after your vote 63 $show_graphs = get_option("reaction_buttons_graphs", true) && $use_percentages; // todo 81 64 $show_after_votes = get_option("reaction_buttons_show_after_votes"); 82 65 … … 113 96 // checks if the user has already voted on this post 114 97 if(!$use_as_counter){ 115 foreach($buttons as $button){ 116 $clean_button = stripslashes(trim($button)); 117 if (array_key_exists(addslashes($clean_button), $cookie) && $cookie[addslashes($clean_button)]) { 98 foreach($buttons as $button_id => $button){ 99 if (array_key_exists($button_id, $cookie) && $cookie[$button_id]) { 118 100 $already_voted_other = true; 119 101 break; … … 125 107 if($use_percentages){ 126 108 $count_all = 0; 127 foreach($buttons as $button ){128 $c lean_button = stripslashes(trim($button));129 $count_all += intval(get_post_meta(get_the_ID(), "_reaction_buttons_" . $clean_button, true));130 }131 } 132 109 foreach($buttons as $button_id => $button){ 110 $count_all += intval(get_post_meta(get_the_ID(), "_reaction_buttons_" . $button_id, true)); 111 } 112 } 113 114 $html .= $show_graphs?"<ul class='graph'>":"<ul>"; 133 115 // print every button 134 foreach($buttons as $button ){116 foreach($buttons as $button_id => $button){ 135 117 $clean_button = stripslashes(trim($button)); 136 $count = intval(get_post_meta(get_the_ID(), "_reaction_buttons_" . $clean_button, true)); 137 $html .= "<a class='reaction_button reaction_button_" . prepare_attr_jl($button) . "_count"; 138 $voted = array_key_exists(addslashes($clean_button), $cookie) && $cookie[addslashes($clean_button)]; 139 118 $count = intval(get_post_meta(get_the_ID(), "_reaction_buttons_" . $button_id, true)); 119 $voted = array_key_exists($button_id, $cookie) && $cookie[$button_id]; 120 140 121 if($use_percentages){ 141 122 if(empty($count_all)){ … … 146 127 } 147 128 } 148 129 130 $html .= $show_graphs?"<li style='height: $count'":"<li "; 131 $html .= "class='reaction_button reaction_button_" . $button_id; 132 149 133 if(($voted || $only_one_vote && $already_voted_other) && !$use_as_counter){ 150 134 if(empty($already_voted_text)){ … … 152 136 } 153 137 else{ 154 $html .= " voted' href=\"javascript:alert('".htmlspecialchars($already_voted_text)."');\">";138 $html .= " voted' onclick=\"alert('".htmlspecialchars($already_voted_text)."');\">"; 155 139 } 156 140 } 157 141 else{ 158 $html .= "' href=\"javascript:reaction_buttons_increment_button_ajax('" . get_the_ID() . "', '" . 159 prepare_js_jl($button) . "');\">"; 160 } 142 $html .= "' onclick=\"reaction_buttons_increment_button_ajax('" . get_the_ID() . "', '" . 143 $button_id . "');\">"; 144 } 145 $html .= "<div>"; 146 $html .= "<span class='button_name'>" . $button . "</span>"; 161 147 if(!$show_after_votes || $already_voted_other){ 162 $html .= $clean_button . " <span class='count'>(<span class='count_number'>" . $count . "</span>)</span></a>";148 $html .= " <span class='count'>(<span class='count_number'>" . $count . "</span>)</span>"; 163 149 } 164 150 else{ 165 $html .= $clean_button . " <span class='count' style='display: none;'>(<span class='count_number'>" . $count . "</span>)</span></a> "; 166 } 167 } 168 $html .= "</div>\n"; 151 $html .= " <span class='count' style='display: none;'>(<span class='count_number'>" . $count . "</span>)</span>"; 152 } 153 $html .= "</div></li>"; 154 } 155 $html .= "</ul></div>\n"; 169 156 170 157 return $html; 171 158 } 172 159 160 /** 161 * returns the name to a button id 162 */ 163 function reaction_buttons_id_to_name($id){ 164 $buttons = explode(",", preg_replace("/,\s+/", ",", get_option('reaction_buttons_button_names'))); 165 return $buttons[$id]; 166 } 173 167 174 168 /** … … 260 254 update_option('reaction_buttons_use_as_counter', false); 261 255 } 262 256 263 257 if ( $force or !( get_option('reaction_buttons_usecookies')) ) { 264 258 update_option('reaction_buttons_usecookies', true); … … 392 386 $table = $wpdb->prefix . "postmeta"; 393 387 394 $buttons = explode(",", preg_replace("/,\s+/", ",", get_option('reaction_buttons_button_names')));388 $buttons = array_keys(explode(",", preg_replace("/,\s+/", ",", get_option('reaction_buttons_button_names')))); 395 389 $delete_meta_ids = array(); 396 390 … … 431 425 $nonce = wp_create_nonce( 'reaction_buttons' ); 432 426 $already_voted_text = get_option("reaction_buttons_already_voted_text"); 433 $only_one_vote = get_option("reaction_buttons_only_one_vote"); 427 $only_one_vote = get_option("reaction_buttons_only_one_vote"); 434 428 $show_after_votes = get_option("reaction_buttons_show_after_votes"); 435 429 $use_as_counter = get_option("reaction_buttons_use_as_counter"); 436 430 $use_percentages = get_option("reaction_buttons_percentages", false); 437 $buttons = explode(",", preg_replace("/,\s/", ",", get_option('reaction_buttons_button_names')));438 431 $buttons = array_keys(explode(",", preg_replace("/,\s/", ",", get_option('reaction_buttons_button_names')))); 432 439 433 ?> 440 434 <script type='text/javascript'><!-- 441 function prepare_attr_jl(str) {442 return str.replace(/\ /g, "___");443 }444 435 function reaction_buttons_increment_button_ajax(post_id, button){ 445 436 var already_voted_text = '<?php echo htmlspecialchars($already_voted_text); ?>'; … … 449 440 var use_percentages = <?php echo $use_percentages ? "true" : "false"; ?>; 450 441 var buttons = <?php echo json_encode($buttons); ?>; 451 452 if( jQuery("#reaction_buttons_post" + post_id + " .reaction_button_" + prepare_attr_jl(button) + "_count").hasClass('voted')){442 443 if(!use_as_counter && jQuery("#reaction_buttons_post" + post_id + " .reaction_button_" + button).hasClass('voted')){ 453 444 return; 454 445 } 455 446 456 447 if(!use_as_counter){ 457 448 // remove the href attribute before sending the request to make 458 449 // sure no one votes more than once by clicking ten times fast 459 450 if(only_one_vote){ 460 // remove all the href's from the posts and replace it by the451 // remove all the onclicks from the posts and replace it by the 461 452 // alert not to vote twice if set 462 453 if(already_voted_text){ 463 jQuery("#reaction_buttons_post" + post_id + " .reaction_button").attr(' href', 'javascript:alert(\'' + already_voted_text + '\');');454 jQuery("#reaction_buttons_post" + post_id + " .reaction_button").attr('onclick', 'javascript:alert(\'' + already_voted_text + '\');'); 464 455 } 465 456 else{ 466 jQuery("#reaction_buttons_post" + post_id + " .reaction_button").removeAttr(' href');457 jQuery("#reaction_buttons_post" + post_id + " .reaction_button").removeAttr('onclick'); 467 458 } 468 459 } … … 470 461 // remove/replace only on the clicked button 471 462 if(already_voted_text){ 472 jQuery("#reaction_buttons_post" + post_id + " .reaction_button_" + prepare_attr_jl(button) + "_count").attr('href', 'javascript:alert(\'' + already_voted_text + '\');');463 jQuery("#reaction_buttons_post" + post_id + " .reaction_button_" + button).attr('onclick', 'javascript:alert(\'' + already_voted_text + '\');'); 473 464 } 474 465 else{ 475 jQuery("#reaction_buttons_post" + post_id + " .reaction_button_" + prepare_attr_jl(button) + "_count").removeAttr('href');466 jQuery("#reaction_buttons_post" + post_id + " .reaction_button_" + button).removeAttr('onclick'); 476 467 } 477 468 } … … 486 477 for(i = 0; i < buttons.length; ++i){ 487 478 b = buttons[i]; 488 jQuery("#reaction_buttons_post" + post_id + " .reaction_button_" + prepare_attr_jl(b) + "_count .count").html("("+data['percentage'][b]+")");479 jQuery("#reaction_buttons_post" + post_id + " .reaction_button_" + b + " .count_number").html(data['percentage'][b]); 489 480 } 490 481 } 491 482 else{ 492 jQuery("#reaction_buttons_post" + post_id + " .reaction_button_" + prepare_attr_jl(button) + "_count .count").html("("+data['count']+")");483 jQuery("#reaction_buttons_post" + post_id + " .reaction_button_" + button + " .count_number").html(data['count']); 493 484 } 494 485 if(only_one_vote){ … … 496 487 } 497 488 else{ 498 jQuery("#reaction_buttons_post" + post_id + " .reaction_button_" + prepare_attr_jl(button) + "_count .count").addClass('voted');489 jQuery("#reaction_buttons_post" + post_id + " .reaction_button_" + button).addClass('voted'); 499 490 } 500 491 if(show_after_votes){ … … 519 510 } 520 511 512 513 if(version_compare(get_option('reaction_buttons_version'), '2', '<')){ 514 error_log("noooo"); 515 reaction_buttons_upgrade_v200(); 516 update_option('reaction_buttons_version', '2.0.0'); 517 } 518 521 519 add_action('wp_enqueue_scripts', 'reaction_buttons_init' ); 522 520 add_action('plugins_loaded', 'reaction_buttons_i18n_init'); … … 532 530 check_ajax_referer("reaction_buttons"); 533 531 534 if(!$_POST['post_id'] || !$_POST['button']) die(); 532 if(!$_POST['post_id'] || !isset($_POST['button'])) die(); 533 535 534 $post_id = intval($_POST['post_id']); 536 $button = stripslashes($_POST['button']);535 $button = intval($_POST['button']); 537 536 $result = array(); 538 537 $use_percentages = get_option("reaction_buttons_percentages", false); … … 540 539 541 540 // get all the buttons, stripped of whitespaces 542 $buttons = explode(",", preg_replace("/,\s/", ",", get_option('reaction_buttons_button_names')));541 $buttons = array_keys(explode(",", preg_replace("/,\s/", ",", get_option('reaction_buttons_button_names')))); 543 542 // if the ajax request submitted a button which isn't in the config, don't do anything 544 543 if(!in_array($button, $buttons)) die(); 545 544 546 545 // get old button value and update it 547 $current = intval(get_post_meta($post_id, "_reaction_buttons_" . stripslashes($button), true));548 update_post_meta($post_id, "_reaction_buttons_" . stripslashes($button), ++$current);546 $current = intval(get_post_meta($post_id, "_reaction_buttons_" . $button, true)); 547 update_post_meta($post_id, "_reaction_buttons_" . $button, ++$current); 549 548 550 549 // support for clearing the articles cache if w3total cache is installed … … 559 558 if ( $_COOKIE["reaction_buttons_" . $post_id] ) { 560 559 $json = stripslashes($_COOKIE["reaction_buttons_" . $post_id]); 561 $cookie = json_decode($json, true); 560 $cookie = json_decode($json, true); 562 561 if(!is_array($cookie)) $cookie = array(); 563 562 } … … 575 574 $count_all = 0; 576 575 foreach($buttons as $button){ 577 $clean_button = stripslashes(trim($button)); 578 $count_all += intval(get_post_meta($post_id, "_reaction_buttons_" . $clean_button, true)); 579 } 580 576 $count_all += intval(get_post_meta($post_id, "_reaction_buttons_" . $button, true)); 577 } 578 581 579 if(empty($count_all)){ 582 580 $result['percentage'] = number_format(0, $use_percentages_precision) . "%";; … … 584 582 else{ 585 583 foreach($buttons as $button){ 586 $clean_button = stripslashes(trim($button)); 587 $count = intval(get_post_meta($post_id, "_reaction_buttons_" . $clean_button, true)); 584 $count = intval(get_post_meta($post_id, "_reaction_buttons_" . $button, true)); 588 585 $result['percentage'][$button] = number_format(100*$count/$count_all, $use_percentages_precision) . "%"; 589 586 } … … 591 588 } 592 589 593 590 594 591 $result['count'] = $current; 595 592 596 // return the new value, so that the js can insert it into the blog593 // return the new value, so that the js can insert it into the blog 597 594 echo json_encode($result); 598 595 die(); … … 819 816 </th> 820 817 <td> 821 <?php echo __("Reaction button titles, comma seperated.", 'reaction_buttons') . "<br />" . __("You may use spaces, but should stay away from exclamation marks and such. If your button doesn't update after you click it, there might be something like an exclamation mark in its name.", 'reaction_buttons');?><br/>818 <?php echo __("Reaction button titles, comma seperated.", 'reaction_buttons') ?><br/> 822 819 <input size="80" type="text" name="button_names" value="<?php echo esc_attr(stripslashes(get_option('reaction_buttons_button_names'))); ?>" /> 823 820 </td> … … 1001 998 1002 999 // get all buttons and get the top $limit_posts for those buttons 1003 foreach($buttons as $button ){1000 foreach($buttons as $button_id => $button){ 1004 1001 $posts = $wpdb->get_results("SELECT post_id,meta_value FROM $table WHERE " . 1005 "meta_key = '_reaction_buttons_$button ' ORDER BY CAST(meta_value AS UNSIGNED) DESC LIMIT $limit_posts OFFSET $offset");1002 "meta_key = '_reaction_buttons_$button_id' ORDER BY CAST(meta_value AS UNSIGNED) DESC LIMIT $limit_posts OFFSET $offset"); 1006 1003 1007 1004 $output_as_table == false ? $html .= "<h3>$button</h3>" : $html_table_th .= '<th style="text-align: center">'.$button.'</th>'; 1008 1005 1009 1006 if($limit_posts > 1) $output_as_table == false ? $html .= "<ol>" : $html .= "<td style='vertical-align: top;'>"; 1010 1007 1011 1008 foreach($posts as $postdb){ 1012 1009 $post = get_post(intval($postdb->post_id)); … … 1049 1046 1050 1047 /** 1051 * Function to gather the top clicked posts .1048 * Function to gather the top clicked posts over all buttons. 1052 1049 */ 1053 1050 function reaction_buttons_get_top_clicked_posts($limit_posts = 5, $excerpt_length = 0, $page = 1, $show_post_thumb = false){ … … 1057 1054 // check options 1058 1055 if($limit_posts < 1) return ""; 1059 1060 // get the buttons from the options and remove the unwanted ones if applicable1061 $buttons = explode(",", preg_replace("/,\s+/", ",", get_option('reaction_buttons_button_names')));1062 if(!empty($only_buttons)){1063 $buttons = array_intersect($buttons, $only_buttons);1064 }1065 1056 1066 1057 // output var … … 1117 1108 1118 1109 $maxRecords = 0; 1119 foreach($buttons as $button ) {1120 $result = $wpdb->get_results("SELECT COUNT(post_id) as count FROM $table WHERE meta_key = '_reaction_buttons_$button '");1110 foreach($buttons as $button_id => $button) { 1111 $result = $wpdb->get_results("SELECT COUNT(post_id) as count FROM $table WHERE meta_key = '_reaction_buttons_$button_id'"); 1121 1112 if(intval($result[0]->count) > $maxRecords) $maxRecords = intval($result[0]->count); 1122 1113 } … … 1203 1194 } 1204 1195 } 1205 1196 1206 1197 if (isset($_POST['reaction_buttons_widget_excerpt'])){ 1207 1198 $excerpt = $_POST['reaction_buttons_widget_excerpt']; … … 1288 1279 } 1289 1280 } 1290 1281 1291 1282 if (isset($_POST['reaction_buttons_clicked_widget_excerpt'])){ 1292 1283 $excerpt = $_POST['reaction_buttons_clicked_widget_excerpt']; … … 1384 1375 1385 1376 /** 1386 * Returns a text including the name of the button clicked the most. 1377 * Returns a text including the name of the button clicked the most. 1387 1378 * Use "%s" in the summary_text for the position of the buttons name. 1388 1379 */ … … 1400 1391 } 1401 1392 $button = substr($buttons[0]['meta_key'], 18); 1402 1403 return "<div class='reaction_buttons_reaction_summary'>". sprintf($summary_text, $button) ."</div>";1393 1394 return "<div class='reaction_buttons_reaction_summary'>". sprintf($summary_text, reaction_buttons_id_to_name($button)) ."</div>"; 1404 1395 } 1405 1396 1406 1397 /** 1407 1398 * Function for the shortcode [reaction_buttons_reaction_summary] 1408 * 1399 * 1409 1400 * shortcode [reaction_buttons_reaction_summary] returns a text including the 1410 1401 * name of the button clicked the most. The text can be configured with the … … 1414 1405 function reaction_buttons_reaction_summary_shortcode($atts){ 1415 1406 extract(shortcode_atts(array('summary_text' => "Most people think this post is %s!"), $atts)); 1416 1407 1417 1408 return reaction_buttons_reaction_summary($summary_text); 1418 1409 } 1419 1410 add_shortcode('reaction_buttons_reaction_summary', 'reaction_buttons_reaction_summary_shortcode'); 1420 1411 1412 /** 1413 * Upgrades the database to the new format for v2.0.0 1414 */ 1415 function reaction_buttons_upgrade_v200(){ 1416 global $wpdb; 1417 $table = $wpdb->prefix . "postmeta"; 1418 1419 $buttons = explode(",", preg_replace("/,\s+/", ",", get_option('reaction_buttons_button_names'))); 1420 1421 foreach($buttons as $button_id=>$button){ 1422 $wpdb->query($wpdb->prepare( 1423 "UPDATE $table SET `meta_key` = '_reaction_buttons_$button_id' 1424 WHERE `meta_key` = '_reaction_buttons_$button'" 1425 )); 1426 } 1427 } 1428 1429 1421 1430 ?>
Note: See TracChangeset
for help on using the changeset viewer.