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;
 }
Пример #2
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));
}