Plugin Directory

Changeset 1076493


Ignore:
Timestamp:
01/27/2015 07:17:12 AM (11 years ago)
Author:
jakob42
Message:

testing a new graph feature, changed how reactions are saved

Location:
reaction-buttons/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • reaction-buttons/trunk/reaction_buttons.css

    r308543 r1076493  
    22div.reaction_buttons{
    33    margin-top: 20px;
     4    margin-bottom: 25px;
    45    font-size: 0.8em;
    56    text-align: left;
    67}
    78
    8 /* a single button */
    9 div.reaction_buttons .reaction_button {
    10     line-height: 3.2em;
     9/* a single normal button */
     10div.reaction_buttons li.reaction_button {
     11    border: 1px dotted #000;
     12    line-height: 2em;
    1113    padding: 5px;
    12     margin: 0 3px;
    13     margin-bottom: 25px;
    14     border: 1px dotted #000;
     14    margin: 3px;
    1515    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);
    1623}
    1724
    1825/* the button while hovering over it */
    19 div.reaction_buttons > .reaction_button:hover {
    20     background-color: #0F6;
     26div.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%);
    2132}
    2233
    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/ */
     36div.reaction_buttons ul.graph {
     37    clear: both;
     38    padding: 0;
     39    width: 100%;
    2540}
    26  /*
     41div.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 */
     79div.reaction_buttons li.voted,
     80div.reaction_buttons li.voted:hover {
     81    cursor: default;
     82}
    2783
    2884/* number of votes including parantheses.
     
    3692 */
    3793 
    38 
     94/* simple sidebar settings */
    3995#sidebar div.widget_reaction_buttons h3 {
    4096    margin: 0;
  • reaction-buttons/trunk/reaction_buttons.php

    r1065133 r1076493  
    44   Plugin URI: http://blog.jl42.de/reaction-buttons/
    55   Description: Adds Buttons for very simple and fast feedback to your post. Inspired by Blogger.
    6    Version: 1.8.2
     6   Version: 2.0.0
    77   Author: Jakob Lenfers
    88   Author URI: http://blog.jl42.de
     
    3131// Determine the location
    3232$reaction_buttons_plugin_path = WP_CONTENT_URL.'/plugins/'.plugin_basename(dirname(__FILE__)).'/';
    33 
    34 /**
    35  * Quote the button name, so that JQuery works
    36  */
    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 names
    44  */
    45 function prepare_attr_jl($str) {
    46     return esc_attr(str_replace(" ", "___", stripslashes(trim($str))));
    47 }
    48 
    4933
    5034/**
     
    7761    $use_percentages = get_option("reaction_buttons_percentages", false);
    7862    $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
    8164    $show_after_votes = get_option("reaction_buttons_show_after_votes");
    8265
     
    11396    // checks if the user has already voted on this post
    11497    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]) {
    118100                $already_voted_other = true;
    119101                break;
     
    125107    if($use_percentages){
    126108        $count_all = 0;
    127         foreach($buttons as $button){
    128             $clean_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>";
    133115    // print every button
    134     foreach($buttons as $button){
     116    foreach($buttons as $button_id => $button){
    135117        $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
    140121        if($use_percentages){
    141122            if(empty($count_all)){
     
    146127            }
    147128        }
    148        
     129
     130        $html .= $show_graphs?"<li style='height: $count'":"<li ";
     131        $html .= "class='reaction_button reaction_button_" . $button_id;
     132
    149133        if(($voted || $only_one_vote && $already_voted_other) && !$use_as_counter){
    150134            if(empty($already_voted_text)){
     
    152136            }
    153137            else{
    154                 $html .= " voted' href=\"javascript:alert('".htmlspecialchars($already_voted_text)."');\">";
     138                $html .= " voted' onclick=\"alert('".htmlspecialchars($already_voted_text)."');\">";
    155139            }
    156140        }
    157141        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>";
    161147        if(!$show_after_votes || $already_voted_other){
    162             $html .= $clean_button . "&nbsp;<span class='count'>(<span class='count_number'>" . $count . "</span>)</span></a> ";
     148            $html .= "&nbsp;<span class='count'>(<span class='count_number'>" . $count . "</span>)</span>";
    163149        }
    164150        else{
    165             $html .= $clean_button . "&nbsp;<span class='count' style='display: none;'>(<span class='count_number'>" . $count . "</span>)</span></a> ";
    166         }
    167     }
    168     $html .= "</div>\n";
     151            $html .= "&nbsp;<span class='count' style='display: none;'>(<span class='count_number'>" . $count . "</span>)</span>";
     152        }
     153        $html .= "</div></li>";
     154    }
     155    $html .= "</ul></div>\n";
    169156
    170157    return $html;
    171158}
    172159
     160/**
     161 * returns the name to a button id
     162 */
     163function reaction_buttons_id_to_name($id){
     164    $buttons = explode(",", preg_replace("/,\s+/", ",", get_option('reaction_buttons_button_names')));
     165    return $buttons[$id];
     166}
    173167
    174168/**
     
    260254        update_option('reaction_buttons_use_as_counter', false);
    261255    }
    262    
     256
    263257    if ( $force or !( get_option('reaction_buttons_usecookies')) ) {
    264258        update_option('reaction_buttons_usecookies', true);
     
    392386    $table = $wpdb->prefix . "postmeta";
    393387
    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'))));
    395389    $delete_meta_ids = array();
    396390
     
    431425    $nonce = wp_create_nonce( 'reaction_buttons' );
    432426    $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");
    434428    $show_after_votes = get_option("reaction_buttons_show_after_votes");
    435429    $use_as_counter = get_option("reaction_buttons_use_as_counter");
    436430    $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
    439433    ?>
    440434    <script type='text/javascript'><!--
    441     function prepare_attr_jl(str) {
    442         return str.replace(/\ /g, "___");
    443     }
    444435    function reaction_buttons_increment_button_ajax(post_id, button){
    445436        var already_voted_text = '<?php echo htmlspecialchars($already_voted_text); ?>';
     
    449440        var use_percentages = <?php echo $use_percentages ? "true" : "false"; ?>;
    450441        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')){
    453444            return;
    454445        }
    455        
     446
    456447        if(!use_as_counter){
    457448            // remove the href attribute before sending the request to make
    458449            // sure no one votes more than once by clicking ten times fast
    459450            if(only_one_vote){
    460                 // remove all the href's from the posts and replace it by the
     451                // remove all the onclicks from the posts and replace it by the
    461452                // alert not to vote twice if set
    462453                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 + '\');');
    464455                }
    465456                else{
    466                     jQuery("#reaction_buttons_post" + post_id + " .reaction_button").removeAttr('href');
     457                    jQuery("#reaction_buttons_post" + post_id + " .reaction_button").removeAttr('onclick');
    467458                }
    468459            }
     
    470461                // remove/replace only on the clicked button
    471462                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 + '\');');
    473464                }
    474465                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');
    476467                }
    477468            }
     
    486477                            for(i = 0; i < buttons.length; ++i){
    487478                                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]);
    489480                            }
    490481                        }
    491482                        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']);
    493484                        }
    494485                        if(only_one_vote){
     
    496487                        }
    497488                        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');
    499490                        }
    500491                        if(show_after_votes){
     
    519510}
    520511
     512
     513if(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
    521519add_action('wp_enqueue_scripts', 'reaction_buttons_init' );
    522520add_action('plugins_loaded', 'reaction_buttons_i18n_init');
     
    532530    check_ajax_referer("reaction_buttons");
    533531
    534     if(!$_POST['post_id'] || !$_POST['button']) die();
     532    if(!$_POST['post_id'] || !isset($_POST['button'])) die();
     533
    535534    $post_id = intval($_POST['post_id']);
    536     $button = stripslashes($_POST['button']);
     535    $button = intval($_POST['button']);
    537536    $result = array();
    538537    $use_percentages = get_option("reaction_buttons_percentages", false);
     
    540539
    541540    // 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'))));
    543542    // if the ajax request submitted a button which isn't in the config, don't do anything
    544543    if(!in_array($button, $buttons)) die();
    545544
    546545    // 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);
    549548
    550549    // support for clearing the articles cache if w3total cache is installed
     
    559558        if ( $_COOKIE["reaction_buttons_" . $post_id] ) {
    560559            $json = stripslashes($_COOKIE["reaction_buttons_" . $post_id]);
    561             $cookie = json_decode($json, true); 
     560            $cookie = json_decode($json, true);
    562561            if(!is_array($cookie)) $cookie = array();
    563562        }
     
    575574        $count_all = 0;
    576575        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
    581579        if(empty($count_all)){
    582580            $result['percentage'] = number_format(0, $use_percentages_precision) . "%";;
     
    584582        else{
    585583            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));
    588585                $result['percentage'][$button] = number_format(100*$count/$count_all, $use_percentages_precision) . "%";
    589586            }
     
    591588    }
    592589
    593    
     590
    594591    $result['count'] = $current;
    595592
    596     // return the new value, so that the js can insert it into the blog
     593    // return the new value, so that the js can insert it into the blog
    597594    echo json_encode($result);
    598595    die();
     
    819816                    </th>
    820817                    <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/>
    822819                        <input size="80" type="text" name="button_names" value="<?php echo esc_attr(stripslashes(get_option('reaction_buttons_button_names'))); ?>" />
    823820                    </td>
     
    1001998
    1002999    // get all buttons and get the top $limit_posts for those buttons
    1003     foreach($buttons as $button){
     1000    foreach($buttons as $button_id => $button){
    10041001        $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");
    10061003
    10071004        $output_as_table == false ? $html .= "<h3>$button</h3>" : $html_table_th .= '<th style="text-align: center">'.$button.'</th>';
    10081005
    10091006        if($limit_posts > 1) $output_as_table == false ? $html .= "<ol>" : $html .= "<td style='vertical-align: top;'>";
    1010        
     1007
    10111008        foreach($posts as $postdb){
    10121009            $post = get_post(intval($postdb->post_id));
     
    10491046
    10501047/**
    1051  * Function to gather the top clicked posts.
     1048 * Function to gather the top clicked posts over all buttons.
    10521049 */
    10531050function reaction_buttons_get_top_clicked_posts($limit_posts = 5, $excerpt_length = 0, $page = 1, $show_post_thumb = false){
     
    10571054    // check options
    10581055    if($limit_posts < 1) return "";
    1059 
    1060     // get the buttons from the options and remove the unwanted ones if applicable
    1061     $buttons = explode(",", preg_replace("/,\s+/", ",", get_option('reaction_buttons_button_names')));
    1062     if(!empty($only_buttons)){
    1063         $buttons = array_intersect($buttons, $only_buttons);
    1064     }
    10651056
    10661057    // output var
     
    11171108
    11181109    $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'");
    11211112        if(intval($result[0]->count) > $maxRecords) $maxRecords = intval($result[0]->count);
    11221113    }
     
    12031194            }
    12041195        }
    1205    
     1196
    12061197        if (isset($_POST['reaction_buttons_widget_excerpt'])){
    12071198            $excerpt = $_POST['reaction_buttons_widget_excerpt'];
     
    12881279            }
    12891280        }
    1290    
     1281
    12911282        if (isset($_POST['reaction_buttons_clicked_widget_excerpt'])){
    12921283            $excerpt = $_POST['reaction_buttons_clicked_widget_excerpt'];
     
    13841375
    13851376/**
    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.
    13871378 * Use "%s" in the summary_text for the position of the buttons name.
    13881379 */
     
    14001391    }
    14011392    $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>";
    14041395}
    14051396
    14061397/**
    14071398 * Function for the shortcode [reaction_buttons_reaction_summary]
    1408  * 
     1399 *
    14091400 * shortcode [reaction_buttons_reaction_summary] returns a text including the
    14101401 * name of the button clicked the most. The text can be configured with the
     
    14141405function reaction_buttons_reaction_summary_shortcode($atts){
    14151406    extract(shortcode_atts(array('summary_text' => "Most people think this post is %s!"), $atts));
    1416    
     1407
    14171408    return reaction_buttons_reaction_summary($summary_text);
    14181409}
    14191410add_shortcode('reaction_buttons_reaction_summary', 'reaction_buttons_reaction_summary_shortcode');
    14201411
     1412/**
     1413 * Upgrades the database to the new format for v2.0.0
     1414 */
     1415function 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
    14211430?>
Note: See TracChangeset for help on using the changeset viewer.