function widget($args, $instance = array())
 {
     global $wpdb;
     global $likebtn_nonpost_entities;
     global $likebtn_bbp_post_types;
     $has_posts = false;
     $post_types_count = 0;
     if (is_array($args)) {
         extract($args);
     }
     $instance = LikeBtnLikeButtonMostLikedWidget::prepareInstance($instance);
     if (is_array($instance)) {
         extract($instance);
     }
     /*$title = '';
             if (isset($instance['title'])) {
                 $title = $instance['title'];
             }
             $show_thumbnail = '';
             if (isset($instance['show_thumbnail'])) {
                 $show_thumbnail = $instance['show_thumbnail'];
             }
             $show_excerpt = '';
             if (isset($instance['show_excerpt'])) {
                 $show_excerpt = $instance['show_excerpt'];
             }
             $show_date = '';
             if (isset($instance['show_date'])) {
                 $show_date = $instance['show_date'];
             }
             $show_likes = '';
             if (isset($instance['show_likes'])) {
                 $show_likes = $instance['show_likes'];
             }
             $show_dislikes = '';
             if (isset($instance['show_dislikes'])) {
                 $show_dislikes = $instance['show_dislikes'];
             }
     
             // validate parameters
             if ($show_thumbnail == 'true') {
                 $show_thumbnail = '1';
             }
             if ($show_date == 'true') {
                 $show_date = '1';
             }
             if ($show_likes == 'true') {
                 $show_likes = '1';
             }
             if ($show_dislikes == 'true') {
                 $show_dislikes = '1';
             }*/
     if (empty($instance['entity_name'])) {
         $instance['entity_name'] = array(LIKEBTN_ENTITY_POST);
     }
     // Add bbPress
     $nonpost_entities = $likebtn_nonpost_entities;
     $nonpost_entities[] = LIKEBTN_ENTITY_BBP_POST;
     foreach ($instance['entity_name'] as $entity_index => $entity_name) {
         $instance['entity_name'][$entity_index] = str_replace("'", '', trim($entity_name));
         if (!in_array($entity_name, $nonpost_entities)) {
             $has_posts = true;
         }
     }
     $query_limit = '';
     if (isset($instance['number']) && (int) $instance['number'] > 0) {
         $query_limit = "LIMIT " . (int) $instance['number'];
     }
     // getting the most liked content
     $query = '';
     // Posts
     if ($has_posts) {
         $query_post_types = "'" . implode("','", $instance['entity_name']) . "'";
         $query_attachment = '';
         if (in_array(LIKEBTN_ENTITY_ATTACHMENT, $instance['entity_name'])) {
             $query_attachment = " OR (p.post_type = 'attachment') ";
         }
         $query .= "\n                 SELECT\n                    p.ID as 'post_id',\n                    p.post_title,\n                    p.post_date,\n                    CONVERT(pm_likes.meta_value, UNSIGNED INTEGER) as 'likes',\n                    CONVERT(pm_dislikes.meta_value, UNSIGNED INTEGER) as 'dislikes',\n                    CONVERT(pm_likes_minus_dislikes.meta_value, UNSIGNED INTEGER) as 'likes_minus_dislikes',\n                    p.post_type,\n                    p.post_mime_type,\n                    '' as url\n                 FROM {$wpdb->prefix}postmeta pm_likes\n                 LEFT JOIN {$wpdb->prefix}posts p\n                     ON (p.ID = pm_likes.post_id)\n                 LEFT JOIN {$wpdb->prefix}postmeta pm_dislikes\n                     ON (pm_dislikes.post_id = pm_likes.post_id AND pm_dislikes.meta_key = '" . LIKEBTN_META_KEY_DISLIKES . "')\n                 LEFT JOIN {$wpdb->prefix}postmeta pm_likes_minus_dislikes\n                     ON (pm_likes_minus_dislikes.post_id = pm_likes.post_id AND pm_likes_minus_dislikes.meta_key = '" . LIKEBTN_META_KEY_LIKES_MINUS_DISLIKES . "')\n                 WHERE\n                    pm_likes.meta_key = '" . LIKEBTN_META_KEY_LIKES . "'\n                    AND ((p.post_status = 'publish'\n                    AND p.post_type in ({$query_post_types})) {$query_attachment})";
         if (!empty($instance['time_range']) && $instance['time_range'] != 'all') {
             $query .= " AND post_date >= '" . $this->timeRangeToDateTime($instance['time_range']) . "'";
         }
         $post_types_count++;
     }
     // Comments
     if (in_array(LIKEBTN_ENTITY_COMMENT, $instance['entity_name'])) {
         if ($post_types_count > 0) {
             $query .= " UNION ";
         }
         $query .= "\n                 SELECT\n                    p.comment_ID as 'post_id',\n                    p.comment_content as post_title,\n                    p.comment_date as 'post_date',\n                    CONVERT(pm_likes.meta_value, UNSIGNED INTEGER) as 'likes',\n                    CONVERT(pm_dislikes.meta_value, UNSIGNED INTEGER) as 'dislikes',\n                    CONVERT(pm_likes_minus_dislikes.meta_value, UNSIGNED INTEGER) as 'likes_minus_dislikes',\n                    '" . LIKEBTN_ENTITY_COMMENT . "' as post_type,\n                    '' as post_mime_type,\n                    '' as url\n                 FROM {$wpdb->prefix}commentmeta pm_likes\n                 LEFT JOIN {$wpdb->prefix}comments p\n                    ON (p.comment_ID = pm_likes.comment_id)\n                 LEFT JOIN {$wpdb->prefix}commentmeta pm_dislikes\n                    ON (pm_dislikes.comment_id = pm_likes.comment_id AND pm_dislikes.meta_key = '" . LIKEBTN_META_KEY_DISLIKES . "')\n                 LEFT JOIN {$wpdb->prefix}commentmeta pm_likes_minus_dislikes\n                     ON (pm_likes_minus_dislikes.comment_id = pm_likes.comment_id AND pm_likes_minus_dislikes.meta_key = '" . LIKEBTN_META_KEY_LIKES_MINUS_DISLIKES . "')\n                 WHERE\n                    pm_likes.meta_key = '" . LIKEBTN_META_KEY_LIKES . "' \n                    AND p.comment_approved = 1 ";
         if (!empty($instance['time_range']) && $instance['time_range'] != 'all') {
             $query .= " AND comment_date >= '" . $this->timeRangeToDateTime($instance['time_range']) . "'";
         }
         $post_types_count++;
     }
     // Custom items
     if (in_array(LIKEBTN_ENTITY_CUSTOM_ITEM, $instance['entity_name'])) {
         if ($post_types_count > 0) {
             $query .= " UNION ";
         }
         $query_post_types = "'" . implode("','", $instance['entity_name']) . "'";
         $query .= "\n                 SELECT\n                    p.ID as 'post_id',\n                    p.identifier as 'post_title',\n                    '' as 'post_date',\n                    p.likes,\n                    p.dislikes,\n                    p.likes_minus_dislikes,\n                    '" . LIKEBTN_ENTITY_CUSTOM_ITEM . "' as 'post_type',\n                    '' as 'post_mime_type',\n                    url\n                 FROM {$wpdb->prefix}" . LIKEBTN_TABLE_ITEM . " p\n                 WHERE\n                    1 = 1 ";
         $post_types_count++;
     }
     // BuddyPress Member
     if (in_array(LIKEBTN_ENTITY_BP_MEMBER, $instance['entity_name'])) {
         if ($post_types_count > 0) {
             $query .= " UNION ";
         }
         $query .= "\n                 SELECT\n                    p.ID as 'post_id',\n                    p.display_name as post_title,\n                    p.user_registered as 'post_date',\n                    CONVERT(pm_likes.meta_value, UNSIGNED INTEGER) as 'likes',\n                    CONVERT(pm_dislikes.meta_value, UNSIGNED INTEGER) as 'dislikes',\n                    CONVERT(pm_likes_minus_dislikes.meta_value, UNSIGNED INTEGER) as 'likes_minus_dislikes',\n                    '" . LIKEBTN_ENTITY_BP_MEMBER . "' as post_type,\n                    '' as post_mime_type,\n                    '' as url\n                 FROM {$wpdb->prefix}bp_xprofile_meta pm_likes\n                 LEFT JOIN {$wpdb->prefix}users p\n                    ON (p.ID = pm_likes.object_id AND pm_likes.object_type = '" . LIKEBTN_BP_XPROFILE_OBJECT_TYPE . "')\n                 LEFT JOIN {$wpdb->prefix}bp_xprofile_meta pm_dislikes\n                    ON (pm_dislikes.object_id = pm_likes.object_id AND pm_dislikes.object_type = '" . LIKEBTN_BP_XPROFILE_OBJECT_TYPE . "' AND pm_dislikes.meta_key = '" . LIKEBTN_META_KEY_DISLIKES . "')\n                 LEFT JOIN {$wpdb->prefix}bp_xprofile_meta pm_likes_minus_dislikes\n                    ON (pm_likes_minus_dislikes.object_id = pm_likes.object_id AND pm_likes_minus_dislikes.object_type = '" . LIKEBTN_BP_XPROFILE_OBJECT_TYPE . "' AND pm_likes_minus_dislikes.meta_key = '" . LIKEBTN_META_KEY_LIKES_MINUS_DISLIKES . "')\n                 WHERE\n                    pm_likes.meta_key = '" . LIKEBTN_META_KEY_LIKES . "' \n                    AND p.user_status = 0 ";
         if (!empty($instance['time_range']) && $instance['time_range'] != 'all') {
             $query .= " AND p.user_registered >= '" . $this->timeRangeToDateTime($instance['time_range']) . "'";
         }
         $post_types_count++;
     }
     // BuddyPress Activities
     if (in_array(LIKEBTN_ENTITY_BP_ACTIVITY_POST, $instance['entity_name']) || in_array(LIKEBTN_ENTITY_BP_ACTIVITY_UPDATE, $instance['entity_name']) || in_array(LIKEBTN_ENTITY_BP_ACTIVITY_COMMENT, $instance['entity_name']) || in_array(LIKEBTN_ENTITY_BP_ACTIVITY_TOPIC, $instance['entity_name'])) {
         if ($post_types_count > 0) {
             $query .= " UNION ";
         }
         $query .= "\n                SELECT \n                    p.id as 'post_id',\n                    CONCAT( IF(p.action != '', p.action, IF(p.content !='', p.content, IF(p.primary_link != '', p.primary_link, p.type))), IF(p.content != '' && p.type != 'bbp_topic_create' && p.type != 'new_blog_post', CONCAT(': ', p.content), '') ) as 'post_title',\n                    p.date_recorded as 'post_date',\n                    CONVERT(pm_likes.meta_value, UNSIGNED INTEGER) as 'likes',\n                    CONVERT(pm_dislikes.meta_value, UNSIGNED INTEGER) as 'dislikes',\n                    CONVERT(pm_likes_minus_dislikes.meta_value, UNSIGNED INTEGER) as 'likes_minus_dislikes',\n                    IF (p.type = 'bbp_topic_create',\n                        '" . LIKEBTN_ENTITY_BP_ACTIVITY_TOPIC . "',\n                        IF (p.type = 'new_blog_post',\n                            '" . LIKEBTN_ENTITY_BP_ACTIVITY_POST . "',\n                            '" . LIKEBTN_ENTITY_BP_ACTIVITY_UPDATE . "'\n                        )\n                    ) as post_type,\n                    '' as post_mime_type,\n                    '' as url\n                 FROM {$wpdb->prefix}bp_activity_meta pm_likes\n                 LEFT JOIN {$wpdb->prefix}bp_activity p\n                     ON (p.id = pm_likes.activity_id)\n                 LEFT JOIN {$wpdb->prefix}bp_activity_meta pm_dislikes\n                     ON (pm_dislikes.activity_id = pm_likes.activity_id AND pm_dislikes.meta_key = '" . LIKEBTN_META_KEY_DISLIKES . "')\n                 LEFT JOIN {$wpdb->prefix}bp_activity_meta pm_likes_minus_dislikes\n                     ON (pm_likes_minus_dislikes.activity_id = pm_likes.activity_id AND pm_likes_minus_dislikes.meta_key = '" . LIKEBTN_META_KEY_LIKES_MINUS_DISLIKES . "')\n                 WHERE\n                    pm_likes.meta_key = '" . LIKEBTN_META_KEY_LIKES . "' \n                    AND p.hide_sitewide = 0\n                    AND p.is_spam = 0 ";
         if (!empty($instance['time_range']) && $instance['time_range'] != 'all') {
             $query .= " AND p.user_registered >= '" . $this->timeRangeToDateTime($instance['time_range']) . "'";
         }
         $post_types_count++;
     }
     // bbPress Post
     if (in_array(LIKEBTN_ENTITY_BBP_POST, $instance['entity_name'])) {
         if ($post_types_count > 0) {
             $query .= " UNION ";
         }
         $query .= "\n                 SELECT\n                    p.ID as 'post_id',\n                    IF (p.post_title != '', p.post_title, p.post_content) as post_title,\n                    p.post_date,\n                    CONVERT(pm_likes.meta_value, UNSIGNED INTEGER) as 'likes',\n                    CONVERT(pm_dislikes.meta_value, UNSIGNED INTEGER) as 'dislikes',\n                    CONVERT(pm_likes_minus_dislikes.meta_value, UNSIGNED INTEGER) as 'likes_minus_dislikes',\n                    p.post_type,\n                    p.post_mime_type,\n                    '' as url\n                 FROM {$wpdb->prefix}postmeta pm_likes\n                 LEFT JOIN {$wpdb->prefix}posts p\n                     ON (p.ID = pm_likes.post_id)\n                 LEFT JOIN {$wpdb->prefix}postmeta pm_dislikes\n                     ON (pm_dislikes.post_id = pm_likes.post_id AND pm_dislikes.meta_key = '" . LIKEBTN_META_KEY_DISLIKES . "')\n                 LEFT JOIN {$wpdb->prefix}postmeta pm_likes_minus_dislikes\n                     ON (pm_likes_minus_dislikes.post_id = pm_likes.post_id AND pm_likes_minus_dislikes.meta_key = '" . LIKEBTN_META_KEY_LIKES_MINUS_DISLIKES . "')\n                 WHERE\n                    pm_likes.meta_key = '" . LIKEBTN_META_KEY_LIKES . "'\n                    AND p.post_type in ('" . implode("', '", $likebtn_bbp_post_types) . "') \n                    AND p.post_status = 'publish' ";
         if (!empty($instance['time_range']) && $instance['time_range'] != 'all') {
             $query .= " AND post_date >= '" . $this->timeRangeToDateTime($instance['time_range']) . "'";
         }
         $post_types_count++;
     }
     // bbPress User
     if (in_array(LIKEBTN_ENTITY_BBP_USER, $instance['entity_name'])) {
         if ($post_types_count > 0) {
             $query .= " UNION ";
         }
         $query .= "\n                 SELECT\n                    p.ID as 'post_id',\n                    p.display_name as post_title,\n                    p.user_registered as 'post_date',\n                    CONVERT(pm_likes.meta_value, UNSIGNED INTEGER) as 'likes',\n                    CONVERT(pm_dislikes.meta_value, UNSIGNED INTEGER) as 'dislikes',\n                    CONVERT(pm_likes_minus_dislikes.meta_value, UNSIGNED INTEGER) as 'likes_minus_dislikes',\n                    '" . LIKEBTN_ENTITY_BBP_USER . "' as post_type,\n                    '' as post_mime_type,\n                    '' as url\n                 FROM {$wpdb->prefix}usermeta pm_likes\n                 LEFT JOIN {$wpdb->prefix}users p\n                    ON (p.ID = pm_likes.user_id)\n                 LEFT JOIN {$wpdb->prefix}usermeta pm_dislikes\n                    ON (pm_dislikes.user_id = pm_likes.user_id AND pm_dislikes.meta_key = '" . LIKEBTN_META_KEY_DISLIKES . "')\n                 LEFT JOIN {$wpdb->prefix}usermeta pm_likes_minus_dislikes\n                    ON (pm_likes_minus_dislikes.user_id = pm_likes.user_id AND pm_likes_minus_dislikes.meta_key = '" . LIKEBTN_META_KEY_LIKES_MINUS_DISLIKES . "')\n                 WHERE\n                    pm_likes.meta_key = '" . LIKEBTN_META_KEY_LIKES . "' \n                    AND p.user_status = 0 ";
         if (!empty($instance['time_range']) && $instance['time_range'] != 'all') {
             $query .= " AND p.user_registered >= '" . $this->timeRangeToDateTime($instance['time_range']) . "'";
         }
         $post_types_count++;
     }
     if ($post_types_count > 1) {
         $query = "SELECT * FROM (" . $query . ") main_query";
     }
     $query .= "\n            ORDER BY ";
     switch ($instance['order']) {
         default:
         case 'likes':
             $query .= "likes";
             break;
         case 'dislikes':
             $query .= "dislikes";
             break;
         case 'likes_minus_dislikes':
             $query .= "likes_minus_dislikes";
             break;
     }
     $query .= " DESC";
     $query .= " {$query_limit}";
     // echo "<pre>";
     // print_r($query);
     // exit();
     $posts = $wpdb->get_results($query);
     $post_loop = array();
     if (count($posts) > 0) {
         foreach ($posts as $i => $db_post) {
             $post = array('id' => $db_post->post_id, 'type' => $db_post->post_type, 'post_mime_type' => $db_post->post_mime_type, 'title' => '', 'link' => '', 'likes' => '', 'dislikes' => '', 'date' => '', 'excerpt' => '');
             // Title
             $post['title'] = _likebtn_prepare_title($db_post->post_type, $db_post->post_title);
             // Link
             $post['link'] = _likebtn_get_entity_url($db_post->post_type, $db_post->post_id, $db_post->url);
             $post['likes'] = $db_post->likes;
             $post['dislikes'] = $db_post->dislikes;
             if ($show_date) {
                 $post['date'] = strtotime($db_post->post_date);
             }
             if ($show_excerpt) {
                 $post['excerpt'] = _likebtn_get_entity_excerpt($db_post->post_type, $db_post->post_id);
             }
             // For bbPress replies
             if (!$post['title']) {
                 $post['title'] = _likebtn_shorten_title($post['excerpt'], LIKEBTN_WIDGET_TITLE_LENGTH);
             }
             $post_loop[$i] = $post;
         }
     }
     // Get and include the template we're going to use
     ob_start();
     include $this->getTemplateHierarchy(self::TEMPLATE);
     $result = ob_get_contents();
     ob_get_clean();
     return $result;
 }
Example #2
0
function likebtn_admin_statistics()
{
    global $likebtn_nonpost_entities;
    global $likebtn_page_sizes;
    global $likebtn_post_statuses;
    global $wpdb;
    $query_parameters = array();
    $likebtn_entities = _likebtn_get_entities(true);
    // Custom item
    $likebtn_entities[LIKEBTN_ENTITY_CUSTOM_ITEM] = __('Custom Item');
    // get parameters
    $entity_name = _likebtn_statistics_entity();
    // Process bulk actions
    //_likebtn_bulk_actions($entity_name);
    // Multisite - available for super admin only
    $blogs = array();
    $blog_list = array();
    $statistics_blog_id = '';
    $prefix_prepared = $wpdb->prefix;
    if (is_multisite() && is_super_admin()) {
        global $blog_id;
        $blog_list = $wpdb->get_results("\n            SELECT blog_id, domain\n            FROM {$wpdb->blogs}\n            WHERE site_id = '{$wpdb->siteid}'\n            AND spam = '0'\n            AND deleted = '0'\n            AND archived = '0'\n            ORDER BY blog_id\n        ");
        // Place current blog on the first place
        foreach ($blog_list as $blog) {
            if ($blog->blog_id == $blog_id) {
                $blogs["{$blog->blog_id}"] = get_blog_option($blog->blog_id, 'blogname') . ' - ' . $blog->domain;
                break;
            }
        }
        foreach ($blog_list as $blog) {
            if ($blog->blog_id != $blog_id) {
                $blogs["{$blog->blog_id}"] = get_blog_option($blog->blog_id, 'blogname') . ' - ' . $blog->domain;
            }
        }
        // Add all
        $blogs['all'] = __('All Sites');
        // Determine selected blog id
        if (isset($_GET['likebtn_blog_id'])) {
            if ($_GET['likebtn_blog_id'] == 'all') {
                $statistics_blog_id = $_GET['likebtn_blog_id'];
            } else {
                // Check if blog with ID exists
                foreach ($blog_list as $blog) {
                    if ($blog->blog_id == (int) $_GET['likebtn_blog_id']) {
                        $statistics_blog_id = (int) $_GET['likebtn_blog_id'];
                        break;
                    }
                }
            }
        }
        // Prepare prefix if this is not main blog
        if ($blog_id != 1) {
            $prefix_prepared = substr($wpdb->prefix, 0, strlen($wpdb->prefix) - strlen($blog_id) - 1);
        }
    }
    // add comment statuses
    $likebtn_post_statuses['0'] = __('Comment not approved', LIKEBTN_I18N_DOMAIN);
    $likebtn_post_statuses['1'] = __('Comment approved', LIKEBTN_I18N_DOMAIN);
    $sort_by = '';
    if (isset($_GET['likebtn_sort_by'])) {
        $sort_by = $_GET['likebtn_sort_by'];
    }
    if (!$sort_by) {
        $sort_by = 'likes';
    } elseif ($entity_name == LIKEBTN_ENTITY_CUSTOM_ITEM && $sort_by == 'post_id') {
        $sort_by = 'likes';
    }
    $page_size = LIKEBTN_STATISTIC_PAGE_SIZE;
    if (isset($_GET['likebtn_page_size'])) {
        $page_size = LIKEBTN_STATISTIC_PAGE_SIZE;
    }
    $post_id = '';
    if (isset($_GET['likebtn_post_id'])) {
        $post_id = trim(stripcslashes($_GET['likebtn_post_id']));
    }
    $post_title = '';
    if (isset($_GET['likebtn_post_title'])) {
        $post_title = trim(stripcslashes($_GET['likebtn_post_title']));
    }
    $post_status = '';
    if (isset($_GET['likebtn_post_status'])) {
        $post_status = trim($_GET['likebtn_post_status']);
    }
    // pagination
    require_once dirname(__FILE__) . '/includes/likebtn_like_button_pagination.class.php';
    $pagination_target = "admin.php?page=likebtn_statistics";
    foreach ($_GET as $get_parameter => $get_value) {
        $pagination_target .= '&' . $get_parameter . '=' . stripcslashes($get_value);
    }
    $p = new LikeBtnLikeButtonPagination();
    $p->limit($page_size);
    // Limit entries per page
    $p->target($pagination_target);
    //$p->currentPage(); // Gets and validates the current page
    $p->prevLabel(__('Previous', LIKEBTN_I18N_DOMAIN));
    $p->nextLabel(__('Next', LIKEBTN_I18N_DOMAIN));
    if (!isset($_GET['paging'])) {
        $p->page = 1;
    } else {
        $p->page = $_GET['paging'];
    }
    // query for limit paging
    $query_limit = "LIMIT " . ($p->page - 1) * $p->limit . ", " . $p->limit;
    // query parameters
    $query_where = '';
    // Post type
    switch ($entity_name) {
        case LIKEBTN_ENTITY_COMMENT:
        case LIKEBTN_ENTITY_CUSTOM_ITEM:
        case LIKEBTN_ENTITY_BP_ACTIVITY_POST:
        case LIKEBTN_ENTITY_BP_ACTIVITY_UPDATE:
        case LIKEBTN_ENTITY_BP_ACTIVITY_COMMENT:
        case LIKEBTN_ENTITY_BP_ACTIVITY_TOPIC:
        case LIKEBTN_ENTITY_BP_MEMBER:
        case LIKEBTN_ENTITY_BBP_POST:
        case LIKEBTN_ENTITY_BBP_USER:
            break;
        default:
            $query_where .= ' AND p.post_type = %s ';
            $query_parameters[] = $entity_name;
            break;
    }
    // Post ID
    if ($post_id) {
        switch ($entity_name) {
            case LIKEBTN_ENTITY_BP_ACTIVITY_POST:
            case LIKEBTN_ENTITY_BP_ACTIVITY_UPDATE:
            case LIKEBTN_ENTITY_BP_ACTIVITY_COMMENT:
            case LIKEBTN_ENTITY_BP_ACTIVITY_TOPIC:
            case LIKEBTN_ENTITY_BP_MEMBER:
                $query_where .= ' AND p.id = %d ';
                break;
            case LIKEBTN_ENTITY_COMMENT:
                $query_where .= ' AND p.comment_ID = %d ';
                break;
            default:
                $query_where .= ' AND p.ID = %d ';
                break;
        }
        $query_parameters[] = $post_id;
    }
    if ($post_title) {
        switch ($entity_name) {
            case LIKEBTN_ENTITY_BP_ACTIVITY_POST:
            case LIKEBTN_ENTITY_BP_ACTIVITY_UPDATE:
            case LIKEBTN_ENTITY_BP_ACTIVITY_COMMENT:
            case LIKEBTN_ENTITY_BP_ACTIVITY_TOPIC:
                $query_where .= ' AND LOWER(CONCAT(p.action, p.content)) LIKE "%%%s%%" ';
                break;
            case LIKEBTN_ENTITY_BP_MEMBER:
                $query_where .= ' AND LOWER(p.value) LIKE "%%%s%%" ';
                break;
            case LIKEBTN_ENTITY_BBP_USER:
                $query_where .= ' AND LOWER(CONCAT(p.user_login, p.display_name)) LIKE "%%%s%%" ';
                break;
            case LIKEBTN_ENTITY_COMMENT:
                $query_where .= ' AND LOWER(p.comment_content) LIKE "%%%s%%" ';
                break;
            case LIKEBTN_ENTITY_CUSTOM_ITEM:
                $query_where .= ' AND LOWER(p.identifier) LIKE "%%%s%%" ';
                break;
            default:
                $query_where .= ' AND LOWER(p.post_title) LIKE "%%%s%%" ';
                break;
        }
        $query_parameters[] = strtolower($post_title);
    }
    if ($post_status !== '') {
        if ($entity_name == LIKEBTN_ENTITY_COMMENT) {
            $query_where .= ' AND p.comment_approved = %s ';
        } elseif ($entity_name != LIKEBTN_ENTITY_CUSTOM_ITEM) {
            $query_where .= ' AND p.post_status = %s ';
        }
        $query_parameters[] = $post_status;
    }
    // order by
    switch ($sort_by) {
        case 'dislikes':
            $query_orderby = 'ORDER BY ABS(dislikes) DESC';
            break;
        case 'likes_minus_dislikes':
            $query_orderby = 'ORDER BY ABS(likes_minus_dislikes)*SIGN(likes_minus_dislikes) DESC';
            break;
        case 'post_id':
            $query_orderby = 'ORDER BY post_id ASC';
            break;
        case 'post_title':
            $query_orderby = 'ORDER BY post_title ASC';
            break;
        case 'likes':
        default:
            $query_orderby = 'ORDER BY ABS(likes) DESC';
            $sort_by = 'likes';
            break;
            /*case 'last_updated':
              $query_orderby = 'ORDER BY pm_likes.meta_id DESC';
              break;*/
    }
    // For Multisite
    $query = '';
    if ($statistics_blog_id && $statistics_blog_id != 1 && $statistics_blog_id != 'all') {
        $prefix = "{$prefix_prepared}{$statistics_blog_id}_";
        $query = _likebtn_get_statistics_sql($entity_name, $prefix, $query_where, $query_orderby, $query_limit);
        $query_prepared = $wpdb->prepare($query, $query_parameters);
    } else {
        if ($statistics_blog_id == 'all') {
            foreach ($blog_list as $blog) {
                if ($blog->blog_id == 1) {
                    $prefix = $prefix_prepared;
                } else {
                    $prefix = "{$prefix_prepared}{$blog->blog_id}_";
                }
                $query_list[] = $wpdb->prepare(_likebtn_get_statistics_sql($entity_name, $prefix, $query_where, '', '', $blog->blog_id . ' as blog_id, '), $query_parameters);
            }
            $query_prepared = ' SELECT SQL_CALC_FOUND_ROWS * from (' . implode(' UNION ', $query_list) . ") query {$query_orderby} {$query_limit} ";
        } else {
            $query = _likebtn_get_statistics_sql($entity_name, $prefix_prepared, $query_where, $query_orderby, $query_limit);
            if (count($query_parameters)) {
                $query_prepared = $wpdb->prepare($query, $query_parameters);
            } else {
                $query_prepared = $query;
            }
        }
    }
    // echo "<pre>";
    // echo $query_prepared;
    // echo $wpdb->prepare($query, $query_parameters);
    // $wpdb->show_errors();
    // exit();
    $statistics = $wpdb->get_results($query_prepared);
    $total_found = 0;
    if (isset($statistics[0])) {
        $query_found_rows = "SELECT FOUND_ROWS() as found_rows";
        $found_rows = $wpdb->get_results($query_found_rows);
        $total_found = (int) $found_rows[0]->found_rows;
        $p->items($total_found);
        $p->calculate();
        // Calculates what to show
        $p->parameterName('paging');
        $p->adjacents(1);
        // No. of page away from the current page
    }
    likebtn_admin_header();
    ?>

    <script type="text/javascript">
        var likebtn_spinner_src = '<?php 
    echo _likebtn_get_public_url() . "img/spinner.gif";
    ?>
';

        var likebtn_msg_select_items = '<?php 
    _e("Please select item(s)", LIKEBTN_I18N_DOMAIN);
    ?>
';
        var likebtn_msg_upgrade = '<?php 
    _e("Upgrade your website to VIP to be able to use the feature", LIKEBTN_I18N_DOMAIN);
    ?>
';
    </script>
    <div>

        <?php 
    if (!_likebtn_is_stat_enabled()) {
        ?>
            <div class="error">
                <br/>
                <?php 
        echo strtr(__('Statistics not available. Enable synchronization in order to view statistics:', LIKEBTN_I18N_DOMAIN), array('%url_sync%' => admin_url() . 'admin.php?page=likebtn_settings'));
        ?>
            
                <?php 
        echo _likebtn_enable_stats_msg();
        ?>
            </div>
        <?php 
    } else {
        ?>
            <p class="description">
                ● <?php 
        _e('Keep in mind that items appear in Statistics after receiving at least one vote.', LIKEBTN_I18N_DOMAIN);
        ?>
<br/>
                ● <?php 
        _e('Select <u>Custom item</u> content type to view votes of like buttons added using shortcode or HTML code.', LIKEBTN_I18N_DOMAIN);
        ?>
<br/>
                ● <?php 
        _e('To edit item votes click on a number of likes/dislikes in the statistics table.', LIKEBTN_I18N_DOMAIN);
        ?>
            </p>
        <?php 
    }
    ?>
        <br/>
        <form action="" method="get" id="statistics_form">
            <input type="hidden" name="page" value="likebtn_statistics" />

            <?php 
    if ($blogs) {
        ?>
                <label><?php 
        _e('Site', LIKEBTN_I18N_DOMAIN);
        ?>
:</label>
                <select name="likebtn_blog_id" >
                    <?php 
        foreach ($blogs as $blog_id_value => $blog_title) {
            ?>
                        <option value="<?php 
            echo $blog_id_value;
            ?>
" <?php 
            selected($statistics_blog_id, $blog_id_value);
            ?>
 ><?php 
            echo $blog_title;
            ?>
</option>
                    <?php 
        }
        ?>
                </select>
                &nbsp;&nbsp;
            <?php 
    }
    ?>

            <label><?php 
    _e('Item Type', LIKEBTN_I18N_DOMAIN);
    ?>
:</label>
            <select name="likebtn_entity_name" >
                <?php 
    foreach ($likebtn_entities as $entity_name_value => $entity_title) {
        ?>
                    <option value="<?php 
        echo $entity_name_value;
        ?>
" <?php 
        selected($entity_name, $entity_name_value);
        ?>
 ><?php 
        _e($entity_title, LIKEBTN_I18N_DOMAIN);
        ?>
</option>
                <?php 
    }
    ?>
            </select>
            &nbsp;&nbsp;
            <label><?php 
    _e('Order By', LIKEBTN_I18N_DOMAIN);
    ?>
:</label>
            <select name="likebtn_sort_by" >
                <option value="likes" <?php 
    selected('likes', $sort_by);
    ?>
 ><?php 
    _e('Likes', LIKEBTN_I18N_DOMAIN);
    ?>
</option>
                <option value="dislikes" <?php 
    selected('dislikes', $sort_by);
    ?>
 ><?php 
    _e('Dislikes', LIKEBTN_I18N_DOMAIN);
    ?>
</option>
                <option value="likes_minus_dislikes" <?php 
    selected('likes_minus_dislikes', $sort_by);
    ?>
 ><?php 
    _e('Likes minus dislikes', LIKEBTN_I18N_DOMAIN);
    ?>
</option>
                <option value="post_id" <?php 
    selected('post_id', $sort_by);
    ?>
 ><?php 
    _e('ID', LIKEBTN_I18N_DOMAIN);
    ?>
</option>
                <option value="post_title" <?php 
    selected('post_title', $sort_by);
    ?>
 ><?php 
    _e('Title', LIKEBTN_I18N_DOMAIN);
    ?>
</option>
                <?php 
    /* <option value="last_updated" <?php selected('last_updated', $sort_by); ?> ><?php _e('Last updated', LIKEBTN_I18N_DOMAIN); ?></option> */
    ?>
            </select>

            &nbsp;&nbsp;
            <label><?php 
    _e('Page Size', LIKEBTN_I18N_DOMAIN);
    ?>
:</label>
            <select name="likebtn_page_size" >
                <?php 
    foreach ($likebtn_page_sizes as $page_size_value) {
        ?>
                    <option value="<?php 
        echo $page_size_value;
        ?>
" <?php 
        selected($page_size, $page_size_value);
        ?>
 ><?php 
        echo $page_size_value;
        ?>
</option>
                <?php 
    }
    ?>

            </select>
            <br/><br/>
            <div class="postbox statistics_filter_container">
                <?php 
    /*<h3><?php _e('Filter', LIKEBTN_I18N_DOMAIN); ?></h3>*/
    ?>
                <div class="inside">
                    <label><?php 
    _e('ID', LIKEBTN_I18N_DOMAIN);
    ?>
:</label>
                    <input type="text" name="likebtn_post_id" value="<?php 
    echo htmlspecialchars($post_id);
    ?>
" size="5" />
                    &nbsp;&nbsp;
                    <label><?php 
    _e('Title');
    ?>
:</label>
                    <input type="text" name="likebtn_post_title" value="<?php 
    echo htmlspecialchars($post_title);
    ?>
" size="25"/>
                    &nbsp;&nbsp;
                    <label><?php 
    _e('Status', LIKEBTN_I18N_DOMAIN);
    ?>
:</label>
                    <select name="likebtn_post_status" >
                        <option value=""></option>
                        <?php 
    foreach ($likebtn_post_statuses as $post_status_value => $post_status_title) {
        ?>
                            <option value="<?php 
        echo $post_status_value;
        ?>
" <?php 
        selected($post_status, $post_status_value);
        ?>
 ><?php 
        echo _e($post_status_title);
        ?>
</option>
                        <?php 
    }
    ?>
                    </select>

                    &nbsp;&nbsp;
                    <input class="button-secondary" type="button" name="reset" value="<?php 
    _e('Reset filter', LIKEBTN_I18N_DOMAIN);
    ?>
" onClick="jQuery('.statistics_filter_container :input[type!=button]').val('');
                jQuery('#statistics_form').submit();"/>
                </div>
            </div>

            <input class="button-primary" type="submit" name="show" value="<?php 
    _e('View', LIKEBTN_I18N_DOMAIN);
    ?>
" />
            &nbsp;
            <?php 
    _e('Items Found', LIKEBTN_I18N_DOMAIN);
    ?>
: <strong><?php 
    echo $total_found;
    ?>
</strong>
        </form>
        <br/>
        <form method="post" action="" id="stats_actions_form">
            <input type="hidden" name="bulk_action" value="" id="stats_bulk_action" />
        <?php 
    if (count($statistics) && $p->total_pages > 0) {
        ?>
            <div class="tablenav">
                <input type="button" class="button-secondary likebtn_ttip" onclick="likebtnStatsBulkAction('reset', '<?php 
        echo get_option('likebtn_plan');
        ?>
', '<?php 
        _e("The votes count can not be recovered after resetting. Are you sure you want to reset likes and dislikes for the selected item(s)?", LIKEBTN_I18N_DOMAIN);
        ?>
')" value="<?php 
        _e('Reset', LIKEBTN_I18N_DOMAIN);
        ?>
" title="<?php 
        _e('Set to zero number of likes and dislikes for selected items', LIKEBTN_I18N_DOMAIN);
        ?>
">
                
                <input type="button" class="button-secondary likebtn_ttip" onclick="likebtnStatsBulkAction('delete', '<?php 
        echo get_option('likebtn_plan');
        ?>
', '<?php 
        _e("The votes count can not be recovered after deleting. Are you sure you want to delete selected item(s) from statistics?", LIKEBTN_I18N_DOMAIN);
        ?>
')" value="<?php 
        _e('Delete', LIKEBTN_I18N_DOMAIN);
        ?>
" title="<?php 
        _e('Delete selected items from statistics: no posts, pages, comments, etc will be deleted, just their votes will be deleted from statistics', LIKEBTN_I18N_DOMAIN);
        ?>
">

                <div class="tablenav-pages">
                    <?php 
        echo $p->show();
        ?>
                </div>
            </div>
        <?php 
    }
    ?>
        <table class="widefat" id="statistics_container">
            <thead>
                <tr>
                    <th><input type="checkbox" onclick="statisticsItemsCheckbox(this)" value="all" style="margin:0"></th>
                    <?php 
    if ($entity_name != LIKEBTN_ENTITY_CUSTOM_ITEM) {
        ?>
                        <th>ID<?php 
        if ($sort_by == 'post_id') {
            ?>
&nbsp;↓<?php 
        }
        ?>
</th>
                    <?php 
    }
    ?>
                    <?php 
    if ($entity_name == LIKEBTN_ENTITY_BP_MEMBER || $entity_name == LIKEBTN_ENTITY_BBP_USER || $entity_name == LIKEBTN_ENTITY_USER) {
        ?>
                        <th><?php 
        _e('Avatar', LIKEBTN_I18N_DOMAIN);
        ?>
</th>
                    <?php 
    } else {
        ?>
                        <th><?php 
        _e('Featured image', LIKEBTN_I18N_DOMAIN);
        ?>
</th>
                    <?php 
    }
    ?>
                    <th width="100%"><?php 
    _e('Item', LIKEBTN_I18N_DOMAIN);
    if ($sort_by == 'post_title') {
        ?>
&nbsp;↓<?php 
    }
    ?>
</th>
                    <?php 
    if ($blogs && $statistics_blog_id == 'all') {
        ?>
                        <th><?php 
        _e('Site');
        ?>
</th>
                    <?php 
    }
    ?>
                    <th><?php 
    _e('Likes', LIKEBTN_I18N_DOMAIN);
    if ($sort_by == 'likes') {
        ?>
&nbsp;↓<?php 
    }
    ?>
</th>
                    <th><?php 
    _e('Dislikes', LIKEBTN_I18N_DOMAIN);
    if ($sort_by == 'dislikes') {
        ?>
&nbsp;↓<?php 
    }
    ?>
</th>
                    <th><?php 
    _e('Likes minus dislikes', LIKEBTN_I18N_DOMAIN);
    if ($sort_by == 'likes_minus_dislikes') {
        ?>
&nbsp;↓<?php 
    }
    ?>
</th>
                    <th>&nbsp;</th>
                </tr>
            </thead>
            <tbody>
                <?php 
    foreach ($statistics as $statistics_item) {
        ?>
                    <?php 
        // URL
        if (!$blogs) {
            $post_url = _likebtn_get_entity_url($entity_name, $statistics_item->post_id, $statistics_item->url);
        } else {
            // Multisite
            switch ($entity_name) {
                case LIKEBTN_ENTITY_COMMENT:
                    $post_url = _likebtn_get_blog_comment_link($statistics_item->blog_id, $statistics_item->post_id);
                    break;
                case LIKEBTN_ENTITY_CUSTOM_ITEM:
                    $post_url = $statistics_item->url;
                    break;
                case LIKEBTN_ENTITY_BP_ACTIVITY_POST:
                case LIKEBTN_ENTITY_BP_ACTIVITY_UPDATE:
                case LIKEBTN_ENTITY_BP_ACTIVITY_COMMENT:
                case LIKEBTN_ENTITY_BP_ACTIVITY_TOPIC:
                    $post_url = bp_activity_get_permalink($statistics_item->post_id);
                    break;
                default:
                    $post_url = get_blog_permalink($statistics_item->blog_id, $statistics_item->post_id);
                    break;
            }
        }
        $statistics_item->post_title = _likebtn_prepare_title($entity_name, $statistics_item->post_title);
        $url_votes = admin_url('admin.php') . '?page=likebtn_votes&likebtn_entity_name=' . $entity_name . '&likebtn_post_id=' . $statistics_item->post_id . '&show=View';
        $image = _likebtn_get_entity_image($entity_name, $statistics_item->post_id, array(32, 32));
        ?>

                    <tr id="item_<?php 
        echo $statistics_item->post_id;
        ?>
">
                        <td><input type="checkbox" class="item_checkbox" value="<?php 
        echo $statistics_item->post_id;
        ?>
" name="item[]" <?php 
        if ($blogs && $statistics_item->blog_id != $blog_id) {
            ?>
disabled="disabled"<?php 
        }
        ?>
></td>
                        <?php 
        if ($entity_name != LIKEBTN_ENTITY_CUSTOM_ITEM) {
            ?>
                            <td><?php 
            echo $statistics_item->post_id;
            ?>
</td>
                        <?php 
        }
        ?>
                        <td>
                            <?php 
        if ($image) {
            ?>
                                <a href="<?php 
            echo $post_url;
            ?>
" target="_blank"><img src="<?php 
            echo $image;
            ?>
" width="32" height="32" /></a>
                            <?php 
        } else {
            ?>
                                &nbsp;
                            <?php 
        }
        ?>
                        </td>
                        <td><a href="<?php 
        echo $post_url;
        ?>
" target="_blank"><?php 
        echo htmlspecialchars($statistics_item->post_title);
        ?>
</a></td>
                        <?php 
        if ($blogs && $statistics_blog_id == 'all') {
            ?>
                            <td><?php 
            echo get_blog_option($statistics_item->blog_id, 'blogname');
            ?>
</td>
                        <?php 
        }
        ?>
                        <td>
                            <?php 
        if ($blogs && $statistics_item->blog_id != $blog_id) {
            ?>
                                <?php 
            echo $statistics_item->likes;
            ?>
                            <?php 
        } else {
            ?>
                                <a href="javascript:statisticsEdit('<?php 
            echo $entity_name;
            ?>
', '<?php 
            echo $statistics_item->post_id;
            ?>
', 'like', '<?php 
            echo get_option('likebtn_plan');
            ?>
', '<?php 
            _e('Enter new value:', LIKEBTN_I18N_DOMAIN);
            ?>
', '<?php 
            _e('Upgrade your website plan to the ULTRA plan to use the feature', LIKEBTN_I18N_DOMAIN);
            ?>
', '<?php 
            _e('Error occured. Please, try again later.', LIKEBTN_I18N_DOMAIN);
            ?>
');void(0);" title="<?php 
            _e('Click to change', LIKEBTN_I18N_DOMAIN);
            ?>
" class="item_like likebtn_ttip"><?php 
            echo $statistics_item->likes;
            ?>
</a>
                            <?php 
        }
        ?>
                        </td>
                        <td>
                            <?php 
        if ($blogs && $statistics_item->blog_id != $blog_id) {
            ?>
                                <?php 
            echo $statistics_item->dislikes;
            ?>
                            <?php 
        } else {
            ?>
                                <a href="javascript:statisticsEdit('<?php 
            echo $entity_name;
            ?>
', '<?php 
            echo $statistics_item->post_id;
            ?>
', 'dislike', '<?php 
            echo get_option('likebtn_plan');
            ?>
', '<?php 
            _e('Enter new value:', LIKEBTN_I18N_DOMAIN);
            ?>
', '<?php 
            _e('Upgrade your website plan to the ULTRA plan to use the feature', LIKEBTN_I18N_DOMAIN);
            ?>
', '<?php 
            _e('Error occured. Please, try again later.', LIKEBTN_I18N_DOMAIN);
            ?>
');void(0);" title="<?php 
            _e('Click to change', LIKEBTN_I18N_DOMAIN);
            ?>
" class="item_dislike likebtn_ttip"><?php 
            echo $statistics_item->dislikes;
            ?>
</a>
                            <?php 
        }
        ?>
                        </td>
                        <td><?php 
        echo $statistics_item->likes_minus_dislikes;
        ?>
</td>
                        <td><a href="<?php 
        echo $url_votes;
        ?>
" target="_blank" class="likebtn_ttip button button-secondary likebtn-action" title="<?php 
        _e('View votes', LIKEBTN_I18N_DOMAIN);
        ?>
"><img src="<?php 
        echo _likebtn_get_public_url();
        ?>
img/actions/votes.png" /></a></td>
                    </tr>
                <?php 
    }
    ?>
            </tbody>
        </table>
        </form>
        <?php 
    if (count($statistics) && $p->total_pages > 0) {
        ?>
            <div class="tablenav">
                <div class="tablenav-pages">
                    <?php 
        echo $p->show();
        ?>
                </div>
            </div>
        <?php 
    }
    ?>

    </div>

    <?php 
    _likebtn_admin_footer();
}
Example #3
0
function likebtn_admin_votes()
{
    global $likebtn_page_sizes;
    global $wpdb;
    wp_enqueue_script('jquery-ui-dialog');
    wp_enqueue_style("wp-jquery-ui-dialog");
    $query_parameters = array();
    $likebtn_entities = _likebtn_get_entities(true);
    // Custom item
    $likebtn_entities[LIKEBTN_ENTITY_CUSTOM_ITEM] = __('Custom item');
    // Multisite - available for super admin only
    $blogs = array();
    $blog_list = array();
    $votes_blog_id = '';
    $prefix_prepared = $wpdb->prefix;
    if (is_multisite() && is_super_admin()) {
        global $blog_id;
        $blog_list = $wpdb->get_results("\n            SELECT blog_id, domain\n            FROM {$wpdb->blogs}\n            WHERE site_id = '{$wpdb->siteid}'\n            AND spam = '0'\n            AND deleted = '0'\n            AND archived = '0'\n            ORDER BY blog_id\n        ");
        // Place current blog on the first place
        foreach ($blog_list as $blog) {
            if ($blog->blog_id == $blog_id) {
                $blogs["{$blog->blog_id}"] = get_blog_option($blog->blog_id, 'blogname') . ' - ' . $blog->domain;
                break;
            }
        }
        foreach ($blog_list as $blog) {
            if ($blog->blog_id != $blog_id) {
                $blogs["{$blog->blog_id}"] = get_blog_option($blog->blog_id, 'blogname') . ' - ' . $blog->domain;
            }
        }
        // Add all
        $blogs['all'] = __('All Sites');
        // Determine selected blog id
        if (isset($_GET['likebtn_blog_id'])) {
            if ($_GET['likebtn_blog_id'] == 'all') {
                $votes_blog_id = $_GET['likebtn_blog_id'];
            } else {
                // Check if blog with ID exists
                foreach ($blog_list as $blog) {
                    if ($blog->blog_id == (int) $_GET['likebtn_blog_id']) {
                        $votes_blog_id = (int) $_GET['likebtn_blog_id'];
                        break;
                    }
                }
            }
        }
        // Prepare prefix if this is not main blog
        if ($blog_id != 1) {
            $prefix_prepared = substr($wpdb->prefix, 0, strlen($wpdb->prefix) - strlen($blog_id) - 1);
        }
    }
    $entity_name = '';
    if (!empty($_GET['likebtn_entity_name'])) {
        $entity_name = $_GET['likebtn_entity_name'];
    }
    $page_size = LIKEBTN_STATISTIC_PAGE_SIZE;
    if (isset($_GET['likebtn_page_size'])) {
        $page_size = LIKEBTN_STATISTIC_PAGE_SIZE;
    }
    $post_id = '';
    if (isset($_GET['likebtn_post_id'])) {
        $post_id = trim(stripcslashes($_GET['likebtn_post_id']));
    }
    $user_id = '';
    if (isset($_GET['likebtn_user_id'])) {
        $user_id = trim(stripcslashes($_GET['likebtn_user_id']));
    }
    $ip = '';
    if (isset($_GET['likebtn_ip'])) {
        $ip = trim($_GET['likebtn_ip']);
    }
    $vote_type = '';
    if (isset($_GET['likebtn_vote_type'])) {
        $vote_type = trim($_GET['likebtn_vote_type']);
    }
    // pagination
    require_once dirname(__FILE__) . '/likebtn_like_button_pagination.class.php';
    $pagination_target = "admin.php?page=likebtn_votes";
    foreach ($_GET as $get_parameter => $get_value) {
        $pagination_target .= '&' . $get_parameter . '=' . stripcslashes($get_value);
    }
    $p = new LikeBtnLikeButtonPagination();
    $p->limit($page_size);
    // Limit entries per page
    $p->target($pagination_target);
    //$p->currentPage(); // Gets and validates the current page
    $p->prevLabel(__('Previous', LIKEBTN_I18N_DOMAIN));
    $p->nextLabel(__('Next', LIKEBTN_I18N_DOMAIN));
    if (!isset($_GET['paging'])) {
        $p->page = 1;
    } else {
        $p->page = $_GET['paging'];
    }
    $query_select = '';
    $query_join = '';
    // query for limit paging
    $query_limit = "LIMIT " . ($p->page - 1) * $p->limit . ", " . $p->limit;
    // query parameters
    $query_where = '';
    // Post type and ID
    if ($entity_name && $post_id && $entity_name != LIKEBTN_ENTITY_CUSTOM_ITEM) {
        $query_where .= ' AND v.identifier = %s ';
        $query_parameters[] = $entity_name . '_' . $post_id;
    } elseif ($entity_name == LIKEBTN_ENTITY_CUSTOM_ITEM && $post_id) {
        $query_where .= ' AND i.ID is not NULL AND v.identifier = %s ';
        $query_parameters[] = $post_id;
    } elseif ($entity_name == LIKEBTN_ENTITY_CUSTOM_ITEM) {
        $query_where .= ' AND i.ID is not NULL ';
    } elseif ($entity_name) {
        $query_where .= ' AND v.identifier LIKE "' . esc_sql($entity_name) . '_%%" ';
    } elseif ($post_id) {
        $query_where .= ' AND v.identifier LIKE "%%_%d" ';
        $query_parameters[] = $post_id;
    }
    // Order by
    $query_orderby = ' ORDER BY created_at DESC ';
    // User ID
    if ($user_id) {
        $query_where .= ' AND v.user_id = %d ';
        $query_parameters[] = $user_id;
    }
    // IP
    if ($ip) {
        $query_where .= ' AND v.ip = %s ';
        $query_parameters[] = $ip;
    }
    // Vote type
    if ($vote_type) {
        $query_where .= ' AND v.type = %d ';
        $query_parameters[] = $vote_type;
    }
    // For Multisite
    $query = '';
    if ($votes_blog_id && $votes_blog_id != 1 && $votes_blog_id != 'all') {
        $prefix = "{$prefix_prepared}{$votes_blog_id}_";
        $query = _likebtn_get_votes_sql($prefix, $query_where, $query_orderby, $query_limit, 'SQL_CALC_FOUND_ROWS ' . $query_select, $query_join);
        $query_prepared = $wpdb->prepare($query, $query_parameters);
    } else {
        if ($votes_blog_id == 'all') {
            foreach ($blog_list as $blog) {
                if ($blog->blog_id == 1) {
                    $prefix = $prefix_prepared;
                } else {
                    $prefix = "{$prefix_prepared}{$blog->blog_id}_";
                }
                $query_list[] = $wpdb->prepare(_likebtn_get_votes_sql($prefix, $query_where, '', '', $blog->blog_id . ' as blog_id, ' . $query_select, $query_join), $query_parameters);
            }
            $query_prepared = ' SELECT SQL_CALC_FOUND_ROWS * from (' . implode(' UNION ', $query_list) . ") query {$query_orderby} {$query_limit} ";
        } else {
            $query = _likebtn_get_votes_sql($prefix_prepared, $query_where, $query_orderby, $query_limit, 'SQL_CALC_FOUND_ROWS ' . $query_select, $query_join);
            if (count($query_parameters)) {
                $query_prepared = $wpdb->prepare($query, $query_parameters);
            } else {
                $query_prepared = $query;
            }
        }
    }
    // echo "<pre>";
    // echo $query;
    // echo $query_prepared;
    // echo $wpdb->prepare($query, $query_parameters);
    // $wpdb->show_errors();
    // exit();
    $votes = $wpdb->get_results($query_prepared);
    $total_found = 0;
    if (isset($votes[0])) {
        $query_found_rows = "SELECT FOUND_ROWS() as found_rows";
        $found_rows = $wpdb->get_results($query_found_rows);
        $total_found = (int) $found_rows[0]->found_rows;
        $p->items($total_found);
        $p->calculate();
        // Calculates what to show
        $p->parameterName('paging');
        $p->adjacents(1);
        // No. of page away from the current page
    } else {
        $votes = array();
    }
    $loader = _likebtn_get_public_url() . 'img/ajax_loader_hor.gif';
    likebtn_admin_header();
    ?>

    <script type="text/javascript">
        var likebtn_msg_ip_info = '<?php 
    _e("IP Info", LIKEBTN_I18N_DOMAIN);
    ?>
';
    </script>

    <div>
        <a href="javascript:jQuery('#likebtn_no_vts').toggle();void(0);"><?php 
    _e('Do not see votes?', LIKEBTN_I18N_DOMAIN);
    ?>
</a>
        <div id="likebtn_no_vts">
            <p class="description">
                ● <?php 
    _e('Votes for Custom items appear in the list after synchronization.', LIKEBTN_I18N_DOMAIN);
    ?>
            </p>
        </div>
        <br/><br/>
        <form action="" method="get" id="votes_form" autocomplete="off">
            <input type="hidden" name="page" value="likebtn_votes" />
            <div class="postbox statistics_filter_container">
                <div class="inside">
                    <label><?php 
    _e('Item Type', LIKEBTN_I18N_DOMAIN);
    ?>
:</label>
                    <select name="likebtn_entity_name" >
                        <option value="">-- <?php 
    _e('Any', LIKEBTN_I18N_DOMAIN);
    ?>
 --</option>
                        <?php 
    foreach ($likebtn_entities as $entity_name_value => $entity_title) {
        ?>
                            <option value="<?php 
        echo $entity_name_value;
        ?>
" <?php 
        selected($entity_name, $entity_name_value);
        ?>
 ><?php 
        _e($entity_title, LIKEBTN_I18N_DOMAIN);
        ?>
</option>
                        <?php 
    }
    ?>
                    </select>
                    &nbsp;&nbsp;
                    <label><?php 
    _e('Item ID', LIKEBTN_I18N_DOMAIN);
    ?>
:</label>
                    <input type="text" name="likebtn_post_id" value="<?php 
    echo htmlspecialchars($post_id);
    ?>
" size="10" />
                    <br/><br/>
                    <label><?php 
    _e('User ID', LIKEBTN_I18N_DOMAIN);
    ?>
:</label>
                    <input type="text" name="likebtn_user_id" value="<?php 
    echo htmlspecialchars($user_id);
    ?>
" size="10" />
                    &nbsp;&nbsp;
                    <label><?php 
    _e('IP');
    ?>
:</label>
                    <input type="text" name="likebtn_ip" value="<?php 
    echo htmlspecialchars($ip);
    ?>
" size="20"/>
                    &nbsp;&nbsp;
                    <label><?php 
    _e('Vote Type', LIKEBTN_I18N_DOMAIN);
    ?>
:</label>
                    <select name="likebtn_vote_type" >
                        <option value="">-- <?php 
    _e('Likes & Dislikes', LIKEBTN_I18N_DOMAIN);
    ?>
 --</option>
                        <option value="1" <?php 
    selected((int) $vote_type, 1);
    ?>
 ><?php 
    _e('Likes', LIKEBTN_I18N_DOMAIN);
    ?>
</option>
                        <option value="-1" <?php 
    selected((int) $vote_type, -1);
    ?>
 ><?php 
    _e('Dislikes', LIKEBTN_I18N_DOMAIN);
    ?>
</option>
                    </select>

                    &nbsp;&nbsp;
                    <input class="button-secondary" type="button" name="reset" value="<?php 
    _e('Reset filter', LIKEBTN_I18N_DOMAIN);
    ?>
" onClick="jQuery('.statistics_filter_container :input[type!=button]').val('');
                jQuery('#votes_form').submit();"/>
                </div>
            </div>

            <?php 
    if ($blogs) {
        ?>
                <label><?php 
        _e('Site', LIKEBTN_I18N_DOMAIN);
        ?>
:</label>
                <select name="likebtn_blog_id" >
                    <?php 
        foreach ($blogs as $blog_id_value => $blog_title) {
            ?>
                        <option value="<?php 
            echo $blog_id_value;
            ?>
" <?php 
            selected($votes_blog_id, $blog_id_value);
            ?>
 ><?php 
            echo $blog_title;
            ?>
</option>
                    <?php 
        }
        ?>
                </select>&nbsp;&nbsp;
            <?php 
    }
    ?>
            
            <label><?php 
    _e('Page Size', LIKEBTN_I18N_DOMAIN);
    ?>
:</label>
            <select name="likebtn_page_size" >
                <?php 
    foreach ($likebtn_page_sizes as $page_size_value) {
        ?>
                    <option value="<?php 
        echo $page_size_value;
        ?>
" <?php 
        selected($page_size, $page_size_value);
        ?>
 ><?php 
        echo $page_size_value;
        ?>
</option>
                <?php 
    }
    ?>

            </select><br/><br/>
            <input class="button-primary" type="submit" name="show" value="<?php 
    _e('View', LIKEBTN_I18N_DOMAIN);
    ?>
" /> 
            &nbsp;
            <?php 
    _e('Votes Found', LIKEBTN_I18N_DOMAIN);
    ?>
: <strong><?php 
    echo $total_found;
    ?>
</strong>
        </form>
        <br/>
        <form method="post" action="" id="votes_actions_form">
            <input type="hidden" name="bulk_action" value="" id="stats_bulk_action" />
        <?php 
    if (count($votes) && $p->lastpage > 1) {
        ?>
            <div class="tablenav">
                <div class="tablenav-pages">
                    <?php 
        echo $p->show();
        ?>
                </div>
            </div>
        <?php 
    }
    ?>
        <table class="widefat" id="votes_container">
            <thead>
                <tr>
                    <?php 
    /*<th><input type="checkbox" onclick="statisticsItemsCheckbox(this)" value="all" style="margin:0"></th>*/
    ?>
                    <?php 
    if ($blogs && $votes_blog_id == 'all') {
        ?>
                        <th><?php 
        _e('Site');
        ?>
</th>
                    <?php 
    }
    ?>
                    <th colspan="2"><?php 
    _e('User', LIKEBTN_I18N_DOMAIN);
    ?>
</th>
                    <th>IP</th>
                    <th><?php 
    _e('Date', LIKEBTN_I18N_DOMAIN);
    ?>
</th>
                    <th><?php 
    _e('Type', LIKEBTN_I18N_DOMAIN);
    ?>
</th>
                    <th><?php 
    _e('Item', LIKEBTN_I18N_DOMAIN);
    ?>
</th>
                </tr>
            </thead>
            <tbody>
                <?php 
    foreach ($votes as $votes_item) {
        ?>
                    <?php 
        $entity_info = _likebtn_parse_identifier($votes_item->identifier);
        $avatar_url = '';
        if ($votes_item->user_id) {
            $avatar_url = _likebtn_get_avatar_url($votes_item->user_id);
        }
        $user_url = '';
        if ($votes_item->user_id) {
            $user_url = _likebtn_get_entity_url(LIKEBTN_ENTITY_USER, $votes_item->user_id);
        }
        $item_title = '';
        $item_url = '';
        if ($votes_item->item_id) {
            $item_title = $votes_item->identifier;
            $item_url = $votes_item->url;
            $entity_type_name = __('Custom Item', LIKEBTN_I18N_DOMAIN);
        } else {
            if ($entity_info['entity_name'] && $entity_info['entity_id']) {
                $item_title = _likebtn_get_entity_title($entity_info['entity_name'], $entity_info['entity_id']);
                $item_title = _likebtn_prepare_title($entity_info['entity_name'], $item_title);
                $item_url = _likebtn_get_entity_url($entity_info['entity_name'], $entity_info['entity_id'], '', $votes_blog_id);
            }
            $entity_type_name = _likebtn_get_entity_name_title($entity_info['entity_name']);
        }
        if ((int) $votes_item->type == 1) {
            $entity_vote_type = 'like';
        } else {
            $entity_vote_type = 'dislike';
        }
        ?>

                    <tr id="vote_<?php 
        echo $votes_item->id;
        ?>
">
                        <?php 
        /*<td><input type="checkbox" class="item_checkbox" value="<?php echo $votes_item->post_id; ?>" name="item[]" <?php if ($blogs && $votes_item->blog_id != $blog_id): ?>disabled="disabled"<?php endif ?>></td>*/
        ?>
                        <?php 
        if ($blogs && $votes_blog_id == 'all') {
            ?>
                            <td><?php 
            echo get_blog_option($votes_item->blog_id, 'blogname');
            ?>
</td>
                        <?php 
        }
        ?>
                        <?php 
        if ($avatar_url) {
            ?>
                            <td width="32">
                                <a href="<?php 
            echo $user_url;
            ?>
" target="_blank"><img src="<?php 
            echo $avatar_url;
            ?>
" width="32" height="32" /></a>
                            </td>
                        <?php 
        }
        ?>
                        <td <?php 
        if (!$avatar_url) {
            ?>
colspan="2"<?php 
        }
        ?>
>
                            <?php 
        if ($votes_item->user_id) {
            ?>
                                <a href="<?php 
            echo $user_url;
            ?>
" target="_blank"><?php 
            echo _likebtn_get_entity_title(LIKEBTN_ENTITY_USER, $votes_item->user_id);
            ?>
</a>
                            <?php 
        } else {
            ?>
                                <?php 
            echo __('Anonymous', LIKEBTN_I18N_DOMAIN);
            ?>
                            <?php 
        }
        ?>
                        </td>
                        <td><a href="javascript:likebtnIpInfo('<?php 
        echo $votes_item->ip;
        ?>
');" class="likebtn_ttip" title="<?php 
        _e('View IP info', LIKEBTN_I18N_DOMAIN);
        ?>
"><?php 
        echo $votes_item->ip;
        ?>
</a></td>
                        <td><?php 
        echo date("Y.m.d H:i:s", strtotime($votes_item->created_at));
        ?>
</td>
                        <td>
                            <img src="<?php 
        echo _likebtn_get_public_url();
        ?>
img/thumb/<?php 
        echo $entity_vote_type;
        ?>
.png" alt="<?php 
        _e(ucfirst($entity_vote_type), LIKEBTN_I18N_DOMAIN);
        ?>
" title="<?php 
        _e(ucfirst($entity_vote_type), LIKEBTN_I18N_DOMAIN);
        ?>
" class="likebtn_ttip" />
                        </td>
                        <td><a href="<?php 
        echo $item_url;
        ?>
" target="_blank"><?php 
        echo $item_title;
        ?>
</a> 
                            <?php 
        if ($entity_type_name) {
            ?>
                                — <?php 
            echo $entity_type_name;
            ?>
                            <?php 
        }
        ?>
                        </td>
                    </tr>
                <?php 
    }
    ?>
            </tbody>
        </table>
        </form>
        <?php 
    if (count($votes) && $p->lastpage > 1) {
        ?>
            <div class="tablenav">
                <div class="tablenav-pages">
                    <?php 
        echo $p->show();
        ?>
                </div>
            </div>
        <?php 
    }
    ?>

    </div>

    <div id="likebtn_ip_info" class="likebtn_ip_info hidden">
        <div class="likebtn_ip_info_map"></div>
        <table class="widefat">
            <tr>
                <th><strong>IP</strong></th>
                <td class="likebtn-ii-ip" width="50%"><img src="<?php 
    echo $loader;
    ?>
" /></td>
            </tr>
            <tr>
                <th><strong><?php 
    _e('Country', LIKEBTN_I18N_DOMAIN);
    ?>
</strong></th>
                <td class="likebtn-ii-country"><img src="<?php 
    echo $loader;
    ?>
" /></td>
            </tr>
            <tr>
                <th><strong><?php 
    _e('City', LIKEBTN_I18N_DOMAIN);
    ?>
</strong></th>
                <td class="likebtn-ii-city"><img src="<?php 
    echo $loader;
    ?>
" /></td>
            </tr>
            <tr>
                <th><strong><?php 
    _e('Lat/Long', LIKEBTN_I18N_DOMAIN);
    ?>
</strong></th>
                <td class="likebtn-ii-latlon"><img src="<?php 
    echo $loader;
    ?>
" /></td>
            </tr>
            <tr>
                <th><strong><?php 
    _e('Postal Code', LIKEBTN_I18N_DOMAIN);
    ?>
</strong></th>
                <td class="likebtn-ii-postal"><img src="<?php 
    echo $loader;
    ?>
" /></td>
            </tr>
            <tr>
                <th><strong><?php 
    _e('Network', LIKEBTN_I18N_DOMAIN);
    ?>
</strong></th>
                <td class="likebtn-ii-network"><img src="<?php 
    echo $loader;
    ?>
" /></td>
            </tr>
            <tr>
                <th><strong><?php 
    _e('Hostname', LIKEBTN_I18N_DOMAIN);
    ?>
</strong></th>
                <td class="likebtn-ii-hostname"><img src="<?php 
    echo $loader;
    ?>
" /></td>
            </tr>
        </table>
        <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
            <div class="ui-dialog-buttonset">
                <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only button-secondary likebtn-button-close" role="button"><span class="ui-button-text"><?php 
    _e('Close', LIKEBTN_I18N_DOMAIN);
    ?>
</span></button>
            </div>
        </div>
    </div>
    <script async defer
        src="https://maps.googleapis.com/maps/api/js?v=3.exp&callback=showMap">
    </script>
    <?php 
    _likebtn_admin_footer();
}
Example #4
0
function _likebtn_bp_activity_add($entity_name, $entity_id, $voter_id, $vote_type, $hide_sitewide, $snippet, $snippet_html)
{
    if (!$voter_id) {
        return false;
    }
    if ($vote_type == LIKEBTN_VOTE_LIKE) {
        $type_name = __('liked', LIKEBTN_I18N_DOMAIN);
    } else {
        $type_name = __('disliked', LIKEBTN_I18N_DOMAIN);
    }
    $primary_link = _likebtn_get_entity_url($entity_name, $entity_id);
    $pattern = __('<a href="%user_url%" title="%user_name%">%user_name%</a> %type_name% %entity_name%, <a href="%entity_url%">%entity_title%</a>', LIKEBTN_I18N_DOMAIN);
    $title = _likebtn_get_entity_title($entity_name, $entity_id);
    $action = strtr($pattern, array('%user_url%' => _likebtn_get_entity_url(LIKEBTN_ENTITY_BP_MEMBER, $voter_id), '%user_name%' => bp_core_get_user_displayname($voter_id), '%type_name%' => $type_name, '%entity_url%' => _likebtn_get_entity_url($entity_name, $entity_id), '%entity_name%' => mb_strtolower(_likebtn_get_entity_name_title($entity_name, true)), '%entity_title%' => $title));
    // Add snippet
    $content = "<!--{$entity_name}_{$entity_id}-->";
    if ($snippet == '1') {
        $image_thumbnail = _likebtn_get_entity_image($entity_name, $entity_id, 'thumbnail');
        $excerpt = _likebtn_get_entity_excerpt($entity_name, $entity_id);
        if (!$snippet_html) {
            $snippet_html = LIKEBTN_BP_SNIPPET_TPL;
        }
        $snippet_html = strtr($snippet_html, array('%image_thumbnail%' => $image_thumbnail, '%title%' => $title, '%excerpt%' => $excerpt));
        $content .= $snippet_html;
        // ob_start();
        // include(_likebtn_get_template_path(LIKEBTN_TEMPLATE_ACTIVITY_SNIPPET));
        // $content .= ob_get_contents();
        // ob_get_clean();
    }
    $component = LIKEBTN_BP_COMPONENT_NAME;
    $item_id = $entity_id;
    // Determine hide_sitewide
    $private_group_activity = false;
    $group_id = 0;
    // Set hide_sitewide to true:
    // - BuddyPress forum
    // - Activity update in private group
    if ($entity_name == LIKEBTN_ENTITY_BBP_POST) {
        $group_post_id = (int) get_post_meta($entity_id, '_bbp_forum_id', true);
        if ($group_post_id) {
            $group_id_array = get_post_meta($group_post_id, '_bbp_group_ids', true);
            if ($group_id_array && isset($group_id_array[0])) {
                $group_id = (int) $group_id_array[0];
                //$group = groups_get_group( array( 'group_id' => $group_id ) );
                //$group_permalink = trailingslash*t( bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/' . $group->slug . '/' ) );
                $group_post = get_post($group_post_id);
                if ($group_post && !empty($group_post->post_status) && $group_post->post_status == 'private') {
                    $private_group_activity = true;
                }
            }
        }
    }
    if (in_array($entity_name, array(LIKEBTN_ENTITY_BP_ACTIVITY_POST, LIKEBTN_ENTITY_BP_ACTIVITY_UPDATE, LIKEBTN_ENTITY_BP_ACTIVITY_COMMENT, LIKEBTN_ENTITY_BP_ACTIVITY_TOPIC))) {
        $get_activity = bp_activity_get_specific(array('activity_ids' => $entity_id));
        if (!empty($get_activity['activities']) && !empty($get_activity['activities'][0])) {
            $activity = $get_activity['activities'][0];
            if ($activity->component == 'groups' && isset($activity->item_id) && (int) $activity->hide_sitewide == 1) {
                $private_group_activity = true;
                $group_id = $activity->item_id;
            }
        }
    }
    if ($private_group_activity && $group_id) {
        $hide_sitewide = 1;
        $component = 'groups';
        $item_id = $group_id;
    }
    bp_activity_add(array('user_id' => $voter_id, 'item_id' => $item_id, 'secondary_item_id' => $vote_type, 'primary_link' => $primary_link, 'action' => $action, 'content' => $content, 'component' => $component, 'type' => LIKEBTN_BP_ACTIVITY_TYPE, 'hide_sitewide' => (int) $hide_sitewide));
}