Changeset 768590
- Timestamp:
- 09/08/2013 03:39:29 PM (12 years ago)
- Location:
- roses-like-this/trunk
- Files:
-
- 4 edited
-
likesScript.js (modified) (2 diffs)
-
likethis.php (modified) (3 diffs)
-
options.php (modified) (5 diffs)
-
readme.txt (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
roses-like-this/trunk/likesScript.js
r767668 r768590 1 1 var $j = jQuery.noConflict(); 2 3 function 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 12 function 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 } 2 25 3 26 $j(document).ready(function () { 4 27 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 } 6 37 } //reloadLikes 7 38 … … 12 43 $element.toggleClass("done"); 13 44 var id = e.target.getAttribute("data-post-id"); 45 var direction = likeThisGetCookie("like_" + id); 46 14 47 $j.ajax({ 15 48 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 }, 18 55 success: function (resp) { 19 56 reloadLikes(resp, $element) -
roses-like-this/trunk/likethis.php
r767668 r768590 2 2 /* 3 3 Plugin 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 4 Plugin URI: http://r.osey.me/code/likeThis 5 Description: 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. 6 Version: 1.6 6 7 Author: Rose Pritchard 7 8 Author URI: http://lifeasrose.ca … … 10 11 Copyright 2011 Rose Pritchard (email : [email protected]) 11 12 12 This program is free software; you can redistribute it and/or modify13 it under the terms of the GNU General Public License, version 2, as14 published by the Free Software Foundation.13 This program is free software; you can redistribute it and/or modify 14 it under the terms of the GNU General Public License, version 2, as 15 published by the Free Software Foundation. 15 16 16 This program is distributed in the hope that it will be useful,17 but WITHOUT ANY WARRANTY; without even the implied warranty of18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the19 GNU General Public License for more details.17 This program is distributed in the hope that it will be useful, 18 but WITHOUT ANY WARRANTY; without even the implied warranty of 19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 GNU General Public License for more details. 20 21 21 You should have received a copy of the GNU General Public License22 along with this program; if not, write to the Free Software23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA22 You should have received a copy of the GNU General Public License 23 along with this program; if not, write to the Free Software 24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 24 25 */ 25 26 include(WP_PLUGIN_DIR . "/roses-like-this/options.php"); 26 function likeThis($post_id,$action = 'get') { 27 include(WP_PLUGIN_DIR . "/roses-like-this/widget.php"); 28 include(WP_PLUGIN_DIR . "/roses-like-this/manage_posts.php"); 27 29 28 if(!is_numeric($post_id)) { 30 function likeThis($post_id, $action = 'get', $direction = 1) { 31 32 if (!is_numeric($post_id)) { 29 33 error_log("Error: Value submitted for post_id was not numeric"); 30 34 return; 31 35 } //if 32 36 33 switch ($action) {37 switch ($action) { 34 38 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 42 50 43 return $data[0];44 break;51 return $data[0]; 52 break; 45 53 54 case 'update': 55 $currentValue = get_post_meta($post_id, '_likes'); 46 56 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 57 61 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]); 62 64 63 $currentValue[0] += $update; 64 update_post_meta($post_id, '_likes', $currentValue[0]); 65 66 return $update > 0; 67 68 break; 65 break; 69 66 70 67 } //switch … … 73 70 74 71 function printLikes($post_id) { 75 printLikesWithValue($post_id, isset($_COOKIE["like_" . $post_id]));72 print generateLikeString($post_id, isset($_COOKIE["like_" . $post_id])); 76 73 } 77 74 78 function printLikesWithValue($post_id, $value) {75 function generateLikeString($post_id, $value) { 79 76 $likes = likeThis($post_id); 80 77 81 78 $who = str_replace("%", $likes, get_option('some_likes')); 82 79 83 if ($likes == 1) {80 if ($likes == 1) { 84 81 $who = str_replace("%", $likes, get_option('one_like')); 85 82 } //if 86 83 87 if ($likes == 0) {84 if ($likes == 0) { 88 85 $who = str_replace("%", $likes, get_option('no_likes')); 89 86 } 90 87 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>'; 94 90 } //if 95 91 96 print '<a href="#" class="likeThis button red favourite" id="like-'.$post_id.'" data-post-id="'.$post_id.'">'.$who.'</a>';97 } //printLikes92 return '<a href="#" class="likeThis" id="like-' . $post_id . '" data-post-id="' . $post_id . '">' . $who . '</a>'; 93 } 98 94 99 100 function setUpPostLikes($post_id) { 101 if(!is_numeric($post_id)) { 95 function likeThisSetUpPostLikes($post_id) { 96 if (!is_numeric($post_id)) { 102 97 error_log("Error: Value submitted for post_id was not numeric"); 103 98 return; 104 99 } //if 105 100 106 107 101 add_post_meta($post_id, '_likes', '0', true); 108 109 102 } //setUpPost 110 103 111 112 function checkHeaders() { 113 if(isset($_POST["likepost"])) { 114 $value = likeThis($_POST["likepost"],'update'); 115 printLikesWithValue($_POST["likepost"], $value); 116 exit; 104 function 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)); 117 117 } //if 118 119 118 } //checkHeaders 120 119 121 120 122 function jsIncludes() {121 function likeThisJsIncludes() { 123 122 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 )); 129 130 } //jsIncludes 130 131 131 add_action ('publish_post', 'setUpPostLikes'); 132 add_action ('init', 'checkHeaders'); 133 add_action ('get_header', 'jsIncludes'); 132 add_action('publish_post', 'likeThisSetUpPostLikes'); 133 add_action('wp_enqueue_scripts', 'likeThisJsIncludes'); 134 134 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 135 if (is_admin()) { 136 add_action('wp_ajax_like_this_like_post', 'likeThisCheckHeaders'); 137 add_action('wp_ajax_nopriv_like_this_like_post', 'likeThisCheckHeaders'); 138 } 225 139 ?> -
roses-like-this/trunk/options.php
r768482 r768590 5 5 add_action('admin_init', 'register_mysettings'); 6 6 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'); 7 function 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'); 11 10 } 12 11 … … 17 16 } 18 17 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'); 18 function 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'); 25 23 } 26 24 27 function like_this_settings_page() 28 { 25 function like_this_settings_page() { 29 26 ?> 30 27 <div class="wrap"> … … 33 30 <form method="post" action="options.php"> 34 31 <?php 35 settings_fields('like-this-settings-group');32 settings_fields('like-this-settings-group'); 36 33 ?> 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> 38 44 <table class="form-table"> 39 45 <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> 41 49 <td><input type="text" name="no_likes" value="<?php 42 echo get_option('no_likes');50 echo get_option('no_likes'); 43 51 ?>" /></td> 44 52 </tr> 45 53 46 54 <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> 48 58 <td><input type="text" name="one_like" value="<?php 49 echo get_option('one_like');59 echo get_option('one_like'); 50 60 ?>" /></td> 51 61 </tr> 52 62 53 63 <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> 55 67 <td><input type="text" name="some_likes" value="<?php 56 echo get_option('some_likes');68 echo get_option('some_likes'); 57 69 ?>" /></td> 58 70 </tr> … … 60 72 61 73 <?php 62 submit_button();74 submit_button(); 63 75 ?> 64 76 … … 68 80 } 69 81 ?> 70 71 <?php72 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 10 10 == Description == 11 11 A 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-this13 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.16 12 17 13 == Installation == … … 21 17 3. Place `<?php printLikes(get_the_ID()); ?>` in 'the loop' of your posts wherever you want the 'like this' link to appear. 22 18 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 30 19 == Frequently Asked Questions == 31 20 32 21 = How can I make the 'like this' link look prettier? = 33 22 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/ 23 You can use CSS. 24 - LikeThis links have the class `likeThis` 25 - Links that have been liked also have the class `done` (`.likeThis.done`) 54 26 55 27 == Changelog == … … 75 47 = 1.5 = 76 48 * Add likes column to post management list 49 50 = 1.6 = 51 * Code Cleanup
Note: See TracChangeset
for help on using the changeset viewer.