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);
     }
     if (empty($instance['entity_name'])) {
         $instance['entity_name'] = array(LIKEBTN_ENTITY_POST);
     }
     if (empty($instance['exclude_categories'])) {
         $instance['exclude_categories'] = array();
     }
     // Author
     $author_in = '';
     if (!empty($instance['author'])) {
         $author_in = "'" . implode("','", explode(",", preg_replace("/[^0-9,]/", '', $instance['author']))) . "'";
     }
     // 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_exclude_categories = '';
         if (count($instance['exclude_categories'])) {
             $query_exclude_categories = "'" . implode("','", $instance['exclude_categories']) . "'";
         }
         $query_attachment = '';
         if (in_array(LIKEBTN_ENTITY_ATTACHMENT, $instance['entity_name'])) {
             $query_attachment = " OR (p.post_type = 'attachment') ";
         }
         $query .= "\n                 SELECT\n                    DISTINCT 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, SIGNED 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 ($query_exclude_categories) {
             //              ".($query_exclude_categories ? "
             // LEFT JOIN {$wpdb->term_relationships} t_rel ON (t_rel.object_id = p.ID)
             // LEFT JOIN {$wpdb->term_taxonomy} t_tax ON (t_tax.term_taxonomy_id = t_rel.term_taxonomy_id)" : '')."
             $query .= " AND NOT EXISTS (\n                    SELECT t_tax.term_id\n                    FROM {$wpdb->term_relationships} t_rel\n                    LEFT JOIN {$wpdb->term_taxonomy} t_tax ON (t_tax.term_taxonomy_id = t_rel.term_taxonomy_id)\n                    WHERE t_rel.object_id = p.ID AND t_tax.term_id IN ({$query_exclude_categories})\n                ) ";
         }
         if (!empty($instance['time_range']) && $instance['time_range'] != 'all') {
             $query .= " AND p.post_date >= '" . $this->timeRangeToDateTime($instance['time_range']) . "' ";
         }
         if ($author_in) {
             $query .= " AND p.post_author IN (" . $author_in . ") ";
         }
         $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, SIGNED 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']) . "'";
         }
         if ($author_in) {
             $query .= " AND p.comment_author IN (" . $author_in . ") ";
         }
         $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 (_likebtn_is_bp_active() && 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, SIGNED 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 (_likebtn_is_bp_active() && (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, SIGNED 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']) . "'";
         }
         if ($author_in) {
             $query .= " AND p.user_id IN (" . $author_in . ") ";
         }
         $post_types_count++;
     }
     // bbPress Post
     if (_likebtn_is_bbp_active() && 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, SIGNED 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']) . "'";
         }
         if ($author_in) {
             $query .= " AND p.post_author IN (" . $author_in . ") ";
         }
         $post_types_count++;
     }
     // bbPress User
     if (_likebtn_is_bbp_active() && 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, SIGNED 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' => '', 'author_id' => '', 'author_name' => '');
             if (empty($instance['title_length'])) {
                 $instance['title_length'] = LIKEBTN_WIDGET_TITLE_LENGTH;
             }
             // Title
             $post['title'] = _likebtn_prepare_title($db_post->post_type, $db_post->post_title, $instance['title_length']);
             // 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_author) {
                 $author_id = _likebtn_get_author_id($db_post->post_type, $db_post->post_id);
                 if ($author_id) {
                     $post['author_id'] = $author_id;
                     $post['author_name'] = _likebtn_get_entity_title(LIKEBTN_ENTITY_USER, $author_id);
                 }
             }
             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'], $instance['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_get_entities($no_list = false, $include_invisible = false, $builtin = true, $mode = 'assoc')
{
    /*global $likebtn_entities;
    
        if (count($likebtn_entities)) {
            return $likebtn_entities;
        }*/
    $likebtn_entities = array(LIKEBTN_ENTITY_POST => _likebtn_get_entity_name_title(LIKEBTN_ENTITY_POST), LIKEBTN_ENTITY_POST_LIST => _likebtn_get_entity_name_title(LIKEBTN_ENTITY_POST_LIST), LIKEBTN_ENTITY_PAGE => _likebtn_get_entity_name_title(LIKEBTN_ENTITY_PAGE), LIKEBTN_ENTITY_PAGE_LIST => _likebtn_get_entity_name_title(LIKEBTN_ENTITY_PAGE_LIST));
    $post_types = get_post_types(array('public' => true, '_builtin' => $builtin));
    if (!empty($post_types)) {
        foreach ($post_types as $post_type) {
            // If bbPress is active miss bbPress custom post types
            if (_likebtn_is_bbp_active() && in_array($post_type, array('forum', 'topic', 'reply'))) {
                continue;
            }
            $likebtn_entities[$post_type] = _likebtn_get_entity_name_title($post_type);
            $likebtn_entities[$post_type . LIKEBTN_LIST_FLAG] = _likebtn_get_entity_name_title($post_type . LIKEBTN_LIST_FLAG);
        }
    }
    // Append BuddyPress
    if (_likebtn_is_bp_active()) {
        $likebtn_entities[LIKEBTN_ENTITY_BP_ACTIVITY_POST] = _likebtn_get_entity_name_title(LIKEBTN_ENTITY_BP_ACTIVITY_POST);
        $likebtn_entities[LIKEBTN_ENTITY_BP_ACTIVITY_UPDATE] = _likebtn_get_entity_name_title(LIKEBTN_ENTITY_BP_ACTIVITY_UPDATE);
        $likebtn_entities[LIKEBTN_ENTITY_BP_ACTIVITY_COMMENT] = _likebtn_get_entity_name_title(LIKEBTN_ENTITY_BP_ACTIVITY_COMMENT);
        $likebtn_entities[LIKEBTN_ENTITY_BP_ACTIVITY_TOPIC] = _likebtn_get_entity_name_title(LIKEBTN_ENTITY_BP_ACTIVITY_TOPIC);
        $likebtn_entities[LIKEBTN_ENTITY_BP_MEMBER] = _likebtn_get_entity_name_title(LIKEBTN_ENTITY_BP_MEMBER);
    }
    // Append bbPress
    if (_likebtn_is_bbp_active()) {
        $likebtn_entities[LIKEBTN_ENTITY_BBP_POST] = _likebtn_get_entity_name_title(LIKEBTN_ENTITY_BBP_POST);
        $likebtn_entities[LIKEBTN_ENTITY_BBP_USER] = _likebtn_get_entity_name_title(LIKEBTN_ENTITY_BBP_USER);
    }
    // append Comments
    $likebtn_entities[LIKEBTN_ENTITY_COMMENT] = _likebtn_get_entity_name_title(LIKEBTN_ENTITY_COMMENT);
    // translate entity names
    // does not work here
    /* load_plugin_textdomain(LIKEBTN_I18N_DOMAIN, false, dirname(plugin_basename(__FILE__)) . '/languages');
    
          foreach ($entities as $entity_name => $entity_title) {
          $entities[$entity_name] = __($entity_title, LIKEBTN_I18N_DOMAIN);
          } */
    // Remove excerpt entities
    if ($no_list) {
        foreach ($likebtn_entities as $entity_name => $entity_title) {
            if (_likebtn_has_list_flag($entity_name)) {
                unset($likebtn_entities[$entity_name]);
            }
        }
    }
    // Add invisible
    if ($include_invisible) {
        $likebtn_entities = array_merge($likebtn_entities, array(LIKEBTN_ENTITY_USER => _likebtn_get_entity_name_title(LIKEBTN_ENTITY_USER)));
    }
    switch ($mode) {
        case 'assoc':
            return $likebtn_entities;
            break;
        case 'keys':
            return array_keys($likebtn_entities);
            break;
        case 'titles':
            return array_values($likebtn_entities);
            break;
    }
}