Advertisement
Guest User

WTI like methods

a guest
Aug 4th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.36 KB | None | 0 0
  1. /**
  2.  * Return true if 'Like' functionality is available and not excluded for this post ID
  3.  */
  4. function ff_is_like_enabled($post_id=NULL) {
  5.     if($post_id == NULL) $post_id = get_the_ID();
  6.  
  7.     // if plugin not installed/active then abort and return false
  8.     if( ! function_exists("GetWtiLikePost")) return FALSE;
  9.  
  10.     // Get the posts ids where we do not need to show like functionality
  11.     $allowed_posts          = explode(",", get_option('wti_like_post_allowed_posts'));
  12.     $excluded_posts         = explode(",", get_option('wti_like_post_excluded_posts'));
  13.     $excluded_categories    = get_option('wti_like_post_excluded_categories');
  14.     $excluded_sections      = get_option('wti_like_post_excluded_sections');
  15.  
  16.     if(empty($excluded_categories)) {
  17.         $excluded_categories = array();
  18.     }
  19.  
  20.     // Checking for excluded section. if yes, then dont show the like/dislike option
  21.     if(!empty($excluded_sections)) {
  22.         if((in_array('home', $excluded_sections) && is_home()) ||
  23.                 (in_array('archive', $excluded_sections) && is_archive()) ||
  24.                     (in_array('search', $excluded_sections) && is_search())) {
  25.             return FALSE;
  26.         }
  27.     }
  28.  
  29.     // Checking for excluded categories
  30.     $category = get_the_category($post_id);
  31.     foreach($category as $cat) {
  32.         if(in_array($cat->cat_ID, $excluded_categories) && !in_array($post_id, $allowed_posts)) {
  33.             return FALSE;
  34.         }
  35.     }
  36.  
  37.     // Checking for excluded posts
  38.     if(in_array($post_id, $excluded_posts)) {
  39.         return FALSE;
  40.     }
  41.  
  42.     // if we get here then the functionality is enabled
  43.     return TRUE;
  44. }
  45.  
  46. /**
  47.  * Convenience methods for retrieving like / unlike count
  48.  */
  49. function ff_get_like_count($post_id=NULL) {
  50.     if($post_id == NULL) $post_id = get_the_ID();
  51.  
  52.     if(ff_is_like_enabled($post_id)) {
  53.         return GetWtiLikeCount($post_id);
  54.     } else {
  55.         return 0;
  56.     }
  57. }
  58.  
  59. function ff_like_count($post_id=NULL) {
  60.     if($post_id == NULL) $post_id = get_the_ID();
  61.  
  62.     echo ff_get_like_count($post_id);
  63. }
  64.  
  65. function ff_get_unlike_count($post_id=NULL) {
  66.     if($post_id == NULL) $post_id = get_the_ID();
  67.  
  68.     if(ff_is_like_enabled($post_id)) {
  69.         return GetWtiUnlikeCount($post_id);
  70.     } else {
  71.         return 0;
  72.     }
  73. }
  74.  
  75. function ff_unlike_count($post_id=NULL) {
  76.     if($post_id == NULL) $post_id = get_the_ID();
  77.  
  78.     echo ff_get_unlike_count($post_id);
  79. }
  80.  
  81. /**
  82.  * return markup for LIKE / UNLIKE buttons
  83.  * or empty string if not available
  84.  */
  85. function ff_get_like_buttons($post_id=NULL) {
  86.     if($post_id == NULL) $post_id = get_the_ID();
  87.  
  88.     // abort here if functionality is not enabled
  89.     if( ! ff_is_like_enabled($post_id)) return "";
  90.  
  91.  
  92.     // Check for title text. if empty then have the default value
  93.     $title_text = get_option('wti_like_post_title_text');
  94.     if(empty($title_text)) {
  95.         $title_text_like    = __('Like', 'wti-like-post');
  96.         $title_text_unlike  = __('Unlike', 'wti-like-post');
  97.     } else {
  98.         $title_text = explode('/', get_option('wti_like_post_title_text'));
  99.         $title_text_like    = $title_text[0];
  100.         $title_text_unlike  = $title_text[1];
  101.     }
  102.  
  103.     // Get the nonce for security purpose and create the like and unlike urls
  104.     $nonce              = wp_create_nonce("wti_like_post_vote_nonce");
  105.     $ajax_like_link     = admin_url('admin-ajax.php?action=wti_like_post_process_vote&task=like&post_id=' . $post_id . '&nonce=' . $nonce);
  106.     $ajax_unlike_link   = admin_url('admin-ajax.php?action=wti_like_post_process_vote&task=unlike&post_id=' . $post_id . '&nonce=' . $nonce);
  107.  
  108.     $like_count     = GetWtiLikeCount($post_id);
  109.     $unlike_count   = GetWtiUnlikeCount($post_id);
  110.     $show_dislike   = get_option('wti_like_post_show_dislike');
  111.  
  112.     // Get voted details
  113.     $ip             = WtiGetRealIpAddress();
  114.     $voted_result   = HasWtiAlreadyVoted($post_id, $ip);
  115.     $wti_has_voted  = $voted_result['has_voted'];
  116.     $voted_count    = $voted_result['voted_count'];
  117.  
  118.     // generate button output
  119.     $output         = "";
  120.  
  121.     // like button
  122.     $output         .= sprintf("<a class='jlk btn-small like like-%d' data-task='like' data-post_id='%d' data-nonce='%s' rel='nofollow'>%s</a>",
  123.                                $post_id, $post_id, $nonce, $title_text_like);
  124.     if($show_dislike) {
  125.         $output         .= sprintf("<a class='jlk btn-small unlike unlike-%d' data-task='unlike' data-post_id='%d' data-nonce='%s' rel='nofollow'>%s</a>",
  126.                                $post_id, $post_id, $nonce, $title_text_unlike);
  127.     }
  128.  
  129.     return $output;
  130. }
  131.  
  132. /**
  133.  * Echo output of ff_get_like_buttons
  134.  */
  135. function ff_the_like_buttons($post_id=NULL) {
  136.     if($post_id == NULL) $post_id = get_the_ID();
  137.  
  138.     echo ff_get_like_buttons($post_id);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement