Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Return true if 'Like' functionality is available and not excluded for this post ID
- */
- function ff_is_like_enabled($post_id=NULL) {
- if($post_id == NULL) $post_id = get_the_ID();
- // if plugin not installed/active then abort and return false
- if( ! function_exists("GetWtiLikePost")) return FALSE;
- // Get the posts ids where we do not need to show like functionality
- $allowed_posts = explode(",", get_option('wti_like_post_allowed_posts'));
- $excluded_posts = explode(",", get_option('wti_like_post_excluded_posts'));
- $excluded_categories = get_option('wti_like_post_excluded_categories');
- $excluded_sections = get_option('wti_like_post_excluded_sections');
- if(empty($excluded_categories)) {
- $excluded_categories = array();
- }
- // Checking for excluded section. if yes, then dont show the like/dislike option
- if(!empty($excluded_sections)) {
- if((in_array('home', $excluded_sections) && is_home()) ||
- (in_array('archive', $excluded_sections) && is_archive()) ||
- (in_array('search', $excluded_sections) && is_search())) {
- return FALSE;
- }
- }
- // Checking for excluded categories
- $category = get_the_category($post_id);
- foreach($category as $cat) {
- if(in_array($cat->cat_ID, $excluded_categories) && !in_array($post_id, $allowed_posts)) {
- return FALSE;
- }
- }
- // Checking for excluded posts
- if(in_array($post_id, $excluded_posts)) {
- return FALSE;
- }
- // if we get here then the functionality is enabled
- return TRUE;
- }
- /**
- * Convenience methods for retrieving like / unlike count
- */
- function ff_get_like_count($post_id=NULL) {
- if($post_id == NULL) $post_id = get_the_ID();
- if(ff_is_like_enabled($post_id)) {
- return GetWtiLikeCount($post_id);
- } else {
- return 0;
- }
- }
- function ff_like_count($post_id=NULL) {
- if($post_id == NULL) $post_id = get_the_ID();
- echo ff_get_like_count($post_id);
- }
- function ff_get_unlike_count($post_id=NULL) {
- if($post_id == NULL) $post_id = get_the_ID();
- if(ff_is_like_enabled($post_id)) {
- return GetWtiUnlikeCount($post_id);
- } else {
- return 0;
- }
- }
- function ff_unlike_count($post_id=NULL) {
- if($post_id == NULL) $post_id = get_the_ID();
- echo ff_get_unlike_count($post_id);
- }
- /**
- * return markup for LIKE / UNLIKE buttons
- * or empty string if not available
- */
- function ff_get_like_buttons($post_id=NULL) {
- if($post_id == NULL) $post_id = get_the_ID();
- // abort here if functionality is not enabled
- if( ! ff_is_like_enabled($post_id)) return "";
- // Check for title text. if empty then have the default value
- $title_text = get_option('wti_like_post_title_text');
- if(empty($title_text)) {
- $title_text_like = __('Like', 'wti-like-post');
- $title_text_unlike = __('Unlike', 'wti-like-post');
- } else {
- $title_text = explode('/', get_option('wti_like_post_title_text'));
- $title_text_like = $title_text[0];
- $title_text_unlike = $title_text[1];
- }
- // Get the nonce for security purpose and create the like and unlike urls
- $nonce = wp_create_nonce("wti_like_post_vote_nonce");
- $ajax_like_link = admin_url('admin-ajax.php?action=wti_like_post_process_vote&task=like&post_id=' . $post_id . '&nonce=' . $nonce);
- $ajax_unlike_link = admin_url('admin-ajax.php?action=wti_like_post_process_vote&task=unlike&post_id=' . $post_id . '&nonce=' . $nonce);
- $like_count = GetWtiLikeCount($post_id);
- $unlike_count = GetWtiUnlikeCount($post_id);
- $show_dislike = get_option('wti_like_post_show_dislike');
- // Get voted details
- $ip = WtiGetRealIpAddress();
- $voted_result = HasWtiAlreadyVoted($post_id, $ip);
- $wti_has_voted = $voted_result['has_voted'];
- $voted_count = $voted_result['voted_count'];
- // generate button output
- $output = "";
- // like button
- $output .= sprintf("<a class='jlk btn-small like like-%d' data-task='like' data-post_id='%d' data-nonce='%s' rel='nofollow'>%s</a>",
- $post_id, $post_id, $nonce, $title_text_like);
- if($show_dislike) {
- $output .= sprintf("<a class='jlk btn-small unlike unlike-%d' data-task='unlike' data-post_id='%d' data-nonce='%s' rel='nofollow'>%s</a>",
- $post_id, $post_id, $nonce, $title_text_unlike);
- }
- return $output;
- }
- /**
- * Echo output of ff_get_like_buttons
- */
- function ff_the_like_buttons($post_id=NULL) {
- if($post_id == NULL) $post_id = get_the_ID();
- echo ff_get_like_buttons($post_id);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement