Plugin Directory

Changeset 768590


Ignore:
Timestamp:
09/08/2013 03:39:29 PM (12 years ago)
Author:
RosemarieP
Message:

Code cleanup

Location:
roses-like-this/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • roses-like-this/trunk/likesScript.js

    r767668 r768590  
    11var $j = jQuery.noConflict();
     2
     3function likeThisSetCookie(c_name,value,exdays)
     4{
     5  var exdate=new Date();
     6  exdate.setDate(exdate.getDate() + exdays);
     7  var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
     8  document.cookie=c_name + "=" + c_value;
     9}
     10
     11
     12function likeThisGetCookie(c_name)
     13{
     14  var i,x,y,ARRcookies=document.cookie.split(";");
     15  for (i=0;i<ARRcookies.length;i++)
     16  {
     17    x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
     18    y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
     19    x=x.replace(/^\s+|\s+$/g,"");
     20    if (x==c_name) {
     21      return unescape(y);
     22    }
     23  }
     24}
    225
    326$j(document).ready(function () {
    427  function reloadLikes(resp, $element) {
    5     $element.replaceWith(resp);
     28    $element.replaceWith(resp.element);
     29    date = new Date()
     30    if(resp.add) {
     31      date.setFullYear(date.getFullYear( ) + 10);
     32      likeThisSetCookie("like_" + resp.id, resp.id, date.toGMTString());
     33    } else {
     34      date.setFullYear(date.getFullYear( ) - 1);
     35      likeThisSetCookie("like_" + resp.id, null, -1);
     36    }
    637  } //reloadLikes
    738
     
    1243    $element.toggleClass("done");
    1344    var id = e.target.getAttribute("data-post-id");
     45    var direction = likeThisGetCookie("like_" + id);
     46
    1447    $j.ajax({
    1548      type: "POST",
    16       url: "index.php",
    17       data: "likepost=" + id,
     49      url: like_this_ajax_object.ajax_url,
     50      data: {
     51        action: "like_this_like_post",
     52        likepost: id,
     53        direction: direction ? -1 : 1
     54      },
    1855      success: function (resp) {
    1956        reloadLikes(resp, $element)
  • roses-like-this/trunk/likethis.php

    r767668 r768590  
    22/*
    33Plugin Name: Like This
    4 Description: Integrates a "Like This" option for posts, similar to the facebook Like button.  For visitors who want to let the author know that they enjoyed the post, but don't want to go to the effort of commenting.
    5 Version: 1.4
     4Plugin URI: http://r.osey.me/code/likeThis
     5Description: Integrates a "Like This" option for posts. For visitors who want to let the author know that they enjoyed the post, but don't want to go to the effort of commenting.
     6Version: 1.6
    67Author: Rose Pritchard
    78Author URI: http://lifeasrose.ca
     
    1011Copyright 2011  Rose Pritchard  (email : [email protected])
    1112
    12     This program is free software; you can redistribute it and/or modify
    13     it under the terms of the GNU General Public License, version 2, as
    14     published by the Free Software Foundation.
     13This program is free software; you can redistribute it and/or modify
     14it under the terms of the GNU General Public License, version 2, as
     15published by the Free Software Foundation.
    1516
    16     This program is distributed in the hope that it will be useful,
    17     but WITHOUT ANY WARRANTY; without even the implied warranty of
    18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19     GNU General Public License for more details.
     17This program is distributed in the hope that it will be useful,
     18but WITHOUT ANY WARRANTY; without even the implied warranty of
     19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     20GNU General Public License for more details.
    2021
    21     You should have received a copy of the GNU General Public License
    22     along with this program; if not, write to the Free Software
    23     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     22You should have received a copy of the GNU General Public License
     23along with this program; if not, write to the Free Software
     24Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    2425*/
    2526include(WP_PLUGIN_DIR . "/roses-like-this/options.php");
    26 function likeThis($post_id,$action = 'get') {
     27include(WP_PLUGIN_DIR . "/roses-like-this/widget.php");
     28include(WP_PLUGIN_DIR . "/roses-like-this/manage_posts.php");
    2729
    28   if(!is_numeric($post_id)) {
     30function likeThis($post_id, $action = 'get', $direction = 1) {
     31
     32  if (!is_numeric($post_id)) {
    2933    error_log("Error: Value submitted for post_id was not numeric");
    3034    return;
    3135  } //if
    3236
    33   switch($action) {
     37  switch ($action) {
    3438
    35   case 'get':
    36     $data = get_post_meta($post_id, '_likes');
    37     if(!$data) { $data = array('0' => 0); }
    38     if(!is_numeric($data[0])) {
    39       $data[0] = 0;
    40       add_post_meta($post_id, '_likes', '0', true);
    41     } //if
     39    case 'get':
     40      $data = get_post_meta($post_id, '_likes');
     41      if (!$data) {
     42        $data = array(
     43          '0' => 0
     44        );
     45      }
     46      if (!is_numeric($data[0])) {
     47        $data[0] = 0;
     48        add_post_meta($post_id, '_likes', '0', true);
     49      } //if
    4250
    43     return $data[0];
    44   break;
     51      return $data[0];
     52      break;
    4553
     54    case 'update':
     55      $currentValue = get_post_meta($post_id, '_likes');
    4656
    47   case 'update':
    48     $update = 1;
    49     $currentValue = get_post_meta($post_id, '_likes');
    50     if(isset($_COOKIE["like_" . $post_id])) {
    51             $update = -1;
    52                   setcookie("like_" . $post_id, null, -1);
    53     } else {
    54       setcookie("like_" . $post_id, $post_id,time()+(60*60*24*365));
    55                    $_COOKIE["like_" . $post_id] = null;
    56                 }
     57      if (!$currentValue || !is_numeric($currentValue[0])) {
     58        $currentValue = array("0" => 0);
     59        add_post_meta($post_id, '_likes', '1', true);
     60      } //if
    5761
    58     if(!is_numeric($currentValue[0])) {
    59       $currentValue[0] = 0;
    60       add_post_meta($post_id, '_likes', '1', true);
    61     } //if
     62      $currentValue[0] += $direction;
     63      update_post_meta($post_id, '_likes', $currentValue[0]);
    6264
    63     $currentValue[0] += $update;
    64     update_post_meta($post_id, '_likes', $currentValue[0]);
    65 
    66     return $update > 0;
    67 
    68   break;
     65      break;
    6966
    7067  } //switch
     
    7370
    7471function printLikes($post_id) {
    75  printLikesWithValue($post_id, isset($_COOKIE["like_" . $post_id]));
     72  print generateLikeString($post_id, isset($_COOKIE["like_" . $post_id]));
    7673}
    7774
    78 function printLikesWithValue($post_id, $value) {
     75function generateLikeString($post_id, $value) {
    7976  $likes = likeThis($post_id);
    8077
    8178  $who = str_replace("%", $likes, get_option('some_likes'));
    8279
    83   if($likes == 1) {
     80  if ($likes == 1) {
    8481    $who = str_replace("%", $likes, get_option('one_like'));
    8582  } //if
    8683
    87   if($likes == 0) {
     84  if ($likes == 0) {
    8885    $who = str_replace("%", $likes, get_option('no_likes'));
    8986  }
    9087
    91   if($value) {
    92   print '<a href="#" class="likeThis done button red favourite" id="like-'.$post_id.'"  data-post-id="'.$post_id.'">'.$who. '</a>';
    93     return;
     88  if ($value) {
     89    return '<a href="#" class="likeThis done" id="like-' . $post_id . '"  data-post-id="' . $post_id . '">' . $who . '</a>';
    9490  } //if
    9591
    96   print '<a href="#" class="likeThis button red favourite" id="like-'.$post_id.'" data-post-id="'.$post_id.'">'.$who.'</a>';
    97 } //printLikes
     92  return '<a href="#" class="likeThis" id="like-' . $post_id . '" data-post-id="' . $post_id . '">' . $who . '</a>';
     93}
    9894
    99 
    100 function setUpPostLikes($post_id) {
    101   if(!is_numeric($post_id)) {
     95function likeThisSetUpPostLikes($post_id) {
     96  if (!is_numeric($post_id)) {
    10297    error_log("Error: Value submitted for post_id was not numeric");
    10398    return;
    10499  } //if
    105100
    106 
    107101  add_post_meta($post_id, '_likes', '0', true);
    108 
    109102} //setUpPost
    110103
    111 
    112 function checkHeaders() {
    113   if(isset($_POST["likepost"])) {
    114     $value = likeThis($_POST["likepost"],'update');
    115     printLikesWithValue($_POST["likepost"], $value);
    116 exit;
     104function likeThisCheckHeaders() {
     105  if (isset($_POST["likepost"])) {
     106    $id        = $_POST["likepost"];
     107    $direction = $_POST["direction"];
     108    likeThis($id, 'update', $direction);
     109    $resp = array(
     110      "element" => generateLikeString($id, $direction > 0),
     111      "add" => $direction == 1,
     112      "id" => $id
     113    );
     114    header('Content-type: application/json');
     115    $out = json_encode($resp);
     116    die(print($out));
    117117  } //if
    118 
    119118} //checkHeaders
    120119
    121120
    122 function jsIncludes() {
     121function likeThisJsIncludes() {
    123122  wp_enqueue_script('jquery');
    124 
    125   wp_register_script('likesScript',
    126   WP_PLUGIN_URL . '/roses-like-this/likesScript.js' );
    127   wp_enqueue_script('likesScript',array('jquery'));
    128 
     123  wp_register_script('likesScript', WP_PLUGIN_URL . '/roses-like-this/likesScript.js');
     124  wp_localize_script('likesScript', 'like_this_ajax_object', array(
     125    'ajax_url' => admin_url('admin-ajax.php')
     126  ));
     127  wp_enqueue_script('likesScript', array(
     128    'jquery'
     129  ));
    129130} //jsIncludes
    130131
    131 add_action ('publish_post', 'setUpPostLikes');
    132 add_action ('init', 'checkHeaders');
    133 add_action ('get_header', 'jsIncludes');
     132add_action('publish_post', 'likeThisSetUpPostLikes');
     133add_action('wp_enqueue_scripts', 'likeThisJsIncludes');
    134134
    135 /**
    136  * Popular Post Widget Class
    137  */
    138 class MostLikedPosts extends WP_Widget {
    139   /** constructor */
    140     function __construct()
    141       {
    142          parent::__construct( 'mostlikedposts', 'Most Liked Posts' );
    143      }
    144 
    145   /** @see WP_Widget::widget */
    146   function widget( $args, $instance ) {
    147     extract( $args );
    148     $title = apply_filters( 'widget_title', $instance['title'] );
    149     $numberOfPostsToShow = apply_filters('widget_numberOfPostsToShow',$instance['numberOfPostsToShow']);
    150     print $before_widget;
    151     if ( $title )
    152       echo $before_title . $title . $after_title;
    153 
    154 
    155 global $wpdb;
    156  $querystr = "
    157     SELECT $wpdb->posts.*
    158     FROM $wpdb->posts, $wpdb->postmeta
    159     WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
    160     AND $wpdb->postmeta.meta_key = '_likes'
    161     AND $wpdb->posts.post_status = 'publish'
    162     AND $wpdb->posts.post_type = 'post'
    163     ORDER BY $wpdb->postmeta.meta_value DESC
    164     LIMIT " . $numberOfPostsToShow;
    165 
    166  $pageposts = $wpdb->get_results($querystr, OBJECT);
    167   if ($pageposts):
    168   global $post;
    169   print "<ul>";
    170   foreach ($pageposts as $post):
    171   setup_postdata($post);
    172  ?>
    173   <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    174     <?php the_title(); ?></a> (<?php print get_post_meta(get_the_id(),"_likes",1);  ?> likes)</li>
    175      <?php endforeach;
    176    print "</ul>"; ?>
    177  <?php endif;
    178 
    179       print $after_widget;
    180 
    181     }
    182 
    183   /** @see WP_Widget::update */
    184   function update( $new_instance, $old_instance ) {
    185     $instance = $old_instance;
    186     $instance['title'] = strip_tags($new_instance['title']);
    187 
    188     if(is_numeric($new_instance['numberOfPostsToShow'])) {
    189      $instance['numberOfPostsToShow'] = strip_tags($new_instance['numberOfPostsToShow']);
    190     } else {
    191 
    192      $instance['numberOfPostsToShow'] = strip_tags("5");
    193     }
    194     return $instance;
    195   }
    196 
    197   /** @see WP_Widget::form */
    198   function form( $instance ) {
    199     if ( $instance ) {
    200       $title = esc_attr( $instance[ 'title' ] );
    201       $numberOfPostsToShow = esc_attr( $instance[ 'numberOfPostsToShow' ] );
    202     }
    203     else {
    204       $title = __( 'Most Liked Posts', 'text_domain' );
    205       $numberOfPostsToShow = __( '5', 'text_domain' );
    206     }
    207     ?>
    208     <p>
    209     <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
    210     <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
    211     </p>
    212 
    213 
    214     <p>
    215     <label for="<?php echo $this->get_field_id('numberOfPostsToShow'); ?>"><?php _e('Number of Posts to Show:'); ?></label>
    216     <input class="shortfat" id="<?php echo $this->get_field_id('numberOfPostsToShow'); ?>" name="<?php echo $this->get_field_name('numberOfPostsToShow'); ?>" width="3" type="text" value="<?php echo $numberOfPostsToShow; ?>" />
    217     </p>
    218     <?php
    219   }
    220 
    221 } // class MostLikedPosts
    222 
    223 add_action( 'widgets_init', create_function( '', 'return register_widget("MostLikedPosts");' ) );
    224 
     135if (is_admin()) {
     136  add_action('wp_ajax_like_this_like_post', 'likeThisCheckHeaders');
     137  add_action('wp_ajax_nopriv_like_this_like_post', 'likeThisCheckHeaders');
     138}
    225139?>
  • roses-like-this/trunk/options.php

    r768482 r768590  
    55add_action('admin_init', 'register_mysettings');
    66
    7 function like_this_create_menu()
    8 {
    9     //create new top-level menu
    10     add_options_page('Like This Plugin Settings', 'Like This Settings', 'administrator', 'like-this-settings', 'like_this_settings_page');
     7function like_this_create_menu() {
     8  //create new top-level menu
     9  add_options_page(__('Like This Plugin Settings', 'like_this'), __('Like This Settings', 'like_this'), 'administrator', 'like-this-settings', 'like_this_settings_page');
    1110}
    1211
     
    1716}
    1817
    19 function register_mysettings()
    20 {
    21     //register our settings
    22     register_setting('like-this-settings-group', 'no_likes');
    23     register_setting('like-this-settings-group', 'one_like');
    24     register_setting('like-this-settings-group', 'some_likes');
     18function register_mysettings() {
     19  //register our settings
     20  register_setting('like-this-settings-group', 'no_likes');
     21  register_setting('like-this-settings-group', 'one_like');
     22  register_setting('like-this-settings-group', 'some_likes');
    2523}
    2624
    27 function like_this_settings_page()
    28 {
     25function like_this_settings_page() {
    2926?>
    3027<div class="wrap">
     
    3330<form method="post" action="options.php">
    3431    <?php
    35     settings_fields('like-this-settings-group');
     32  settings_fields('like-this-settings-group');
    3633?>
    37     <p>Enter like this text for no likes, one like, and many likes. The <strong>%</strong> symbol will be replaced by the number of likes.</p>
     34    <p><?php
     35  print __("Enter like this text for no likes, one like, and many likes.", "like_this");
     36?>
     37
     38     <?php
     39  print __("The", "like_this");
     40?> <strong>%</strong>
     41     <?php
     42  print __("symbol will be replaced by the number of likes.", "like_this");
     43?></p>
    3844    <table class="form-table">
    3945        <tr valign="top">
    40         <th scope="row">Text for no likes</th>
     46        <th scope="row"><?php
     47  print __("Text for no likes", "like_this");
     48?></th>
    4149        <td><input type="text" name="no_likes" value="<?php
    42     echo get_option('no_likes');
     50  echo get_option('no_likes');
    4351?>" /></td>
    4452        </tr>
    4553
    4654        <tr valign="top">
    47         <th scope="row">Text for one like</th>
     55        <th scope="row"><?php
     56  print __("Text for one like", "like_this");
     57?></th>
    4858        <td><input type="text" name="one_like" value="<?php
    49     echo get_option('one_like');
     59  echo get_option('one_like');
    5060?>" /></td>
    5161        </tr>
    5262
    5363        <tr valign="top">
    54         <th scope="row">Text for many likes</th>
     64        <th scope="row"><?php
     65  print __("Text for many likes", "like_this");
     66?></th>
    5567        <td><input type="text" name="some_likes" value="<?php
    56     echo get_option('some_likes');
     68  echo get_option('some_likes');
    5769?>" /></td>
    5870        </tr>
     
    6072
    6173    <?php
    62     submit_button();
     74  submit_button();
    6375?>
    6476
     
    6880}
    6981?>
    70 
    71 <?php
    72 
    73 function display_post_likes( $column, $post_id ) {
    74     $likes = get_post_meta($post_id, "_likes");
    75     if($likes[0]) {
    76       echo $likes[0];
    77     } else {
    78       echo 0;
    79     }
    80 }
    81 add_action( 'manage_posts_custom_column' , 'display_post_likes', 10, 2 );
    82 
    83 function add_likes_column( $columns ) {
    84     return array_merge( $columns,
    85         array( 'likes' => "Likes"));
    86 }
    87 add_filter( 'manage_posts_columns' , 'add_likes_column' );
    88 
    89 add_filter( 'manage_edit-post_sortable_columns', 'sortable_likes' ); 
    90 function sortable_likes( $columns ) { 
    91     $columns['likes'] = '_likes'; 
    92     return $columns; 
    93 
    94 
    95 add_filter( 'request', 'views_column_orderby' );
    96 function views_column_orderby( $vars ) {
    97    if ( isset( $vars['orderby'] ) && '_likes' == $vars['orderby'] ) {
    98       $vars = array_merge( $vars, array(
    99          'meta_key' => '_likes',
    100          'orderby' => 'meta_value_num'
    101       ) );
    102    }
    103  
    104    return $vars;
    105 }
    106 
    107 ?>
  • roses-like-this/trunk/readme.txt

    r768482 r768590  
    1010== Description ==
    1111A simple 'I like this' plugin inspired by the facebook 'like' functionality.  For visitors who don't want to bother with commenting.
    12 http://lifeasrose.ca/2011/03/wordpress-plugin-i-like-this
    13 has a blog entry all about it :)
    14 
    15 A big thanks to Dong ([email protected]) for finding a syntactical error that was causing problems for some people.  And thanks to Raphael ([email protected]) for noticing this error and working hard to figure out what it was.
    1612
    1713== Installation ==
     
    21173. Place `<?php printLikes(get_the_ID()); ?>` in 'the loop' of your posts wherever you want the 'like this' link to appear.
    2218
    23 
    24 IMPORTANT!!!!
    25 PLEASE MAKE SURE THAT YOUR THEME HAS THE FOLLOWING LINE IN ITS HEADER FILE:
    26 `<?php wp_print_scripts(); ?>`
    27 
    28 ...Most high quality themes should have this already but if you're writing your own theme or using a custom theme that doesn't include this line, please make sure you include it in header.php, somewhere between `<head>` and `</head>`
    29 
    3019== Frequently Asked Questions ==
    3120
    3221= How can I make the 'like this' link look prettier? =
    3322
    34 With CSS :) Here is the code that I use:
    35 `a.done {
    36 background:url("http://yoururl.com/wordpress/plugins/roses-like-this/action_check.png") bottom right no-repeat;
    37 padding-right:18px;
    38 color:#8bcb46;
    39 }`
    40 
    41 = The javascript is not working! =
    42 
    43 IMPORTANT!!!!
    44 PLEASE MAKE SURE THAT YOUR THEME HAS THE FOLLOWING LINE IN ITS HEADER FILE:
    45 `<?php wp_print_scripts(); ?>`
    46 
    47 = The javascript is STILL not working!!! =
    48 Do you call get_header() in your theme?  This is also needed, although almost certainly there anyway.
    49 
    50 = The javascript is STILL not working AGAIN!!! =
    51 The plugin expects to find the javascript file in a folder called `roses-like-this` under /plugins.  So if you have named the folder something else, you're probably getting a 404 error!
    52 
    53 To fix, you can either rename your folder `roses-like-this`  OR you can edit the `likethis.php` file and edit line `112` roses-like-this/ to yourfoldername/
     23You can use CSS.
     24- LikeThis links have the class `likeThis`
     25- Links that have been liked also have the class `done` (`.likeThis.done`)
    5426
    5527== Changelog ==
     
    7547= 1.5 =
    7648* Add likes column to post management list
     49
     50= 1.6 =
     51* Code Cleanup
Note: See TracChangeset for help on using the changeset viewer.