| ?>
/subdirectory/
multisite networks in which on-demand sites use paths.', LIKEBTN_I18N_DOMAIN) ?> |
";
// 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();
?>
_likebtn_get_entity_title(LIKEBTN_ENTITY_POST),
LIKEBTN_ENTITY_POST_LIST => _likebtn_get_entity_title(LIKEBTN_ENTITY_POST_LIST),
LIKEBTN_ENTITY_PAGE => _likebtn_get_entity_title(LIKEBTN_ENTITY_PAGE),
);
$post_types = get_post_types(array('public'=>true, '_builtin' => false));
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_title($post_type);
$likebtn_entities[$post_type.LIKEBTN_LIST_FLAG] = _likebtn_get_entity_title($post_type.LIKEBTN_LIST_FLAG);
}
}
// Append BuddyPress
if (_likebtn_is_bp_active()) {
$likebtn_entities[LIKEBTN_ENTITY_BP_ACTIVITY_POST] = _likebtn_get_entity_title(LIKEBTN_ENTITY_BP_ACTIVITY_POST);
$likebtn_entities[LIKEBTN_ENTITY_BP_ACTIVITY_UPDATE] = _likebtn_get_entity_title(LIKEBTN_ENTITY_BP_ACTIVITY_UPDATE);
$likebtn_entities[LIKEBTN_ENTITY_BP_ACTIVITY_COMMENT] = _likebtn_get_entity_title(LIKEBTN_ENTITY_BP_ACTIVITY_COMMENT);
$likebtn_entities[LIKEBTN_ENTITY_BP_ACTIVITY_TOPIC] = _likebtn_get_entity_title(LIKEBTN_ENTITY_BP_ACTIVITY_TOPIC);
$likebtn_entities[LIKEBTN_ENTITY_BP_MEMBER] = _likebtn_get_entity_title(LIKEBTN_ENTITY_BP_MEMBER);
}
// Append bbPress
if (_likebtn_is_bbp_active()) {
$likebtn_entities[LIKEBTN_ENTITY_BBP_POST] = _likebtn_get_entity_title(LIKEBTN_ENTITY_BBP_POST);
$likebtn_entities[LIKEBTN_ENTITY_BBP_USER] = _likebtn_get_entity_title(LIKEBTN_ENTITY_BBP_USER);
}
// append Comments
$likebtn_entities[LIKEBTN_ENTITY_COMMENT] = _likebtn_get_entity_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_title(LIKEBTN_ENTITY_USER),
));
}
return $likebtn_entities;
}
// short code
function likebtn_shortcode($args) {
$entity_name = get_post_type();
$entity_id = get_the_ID();
return _likebtn_get_markup($entity_name, $entity_id, $args, '', false, false);
}
add_shortcode('likebtn', 'likebtn_shortcode');
################
### Widget ###
################
require_once(dirname(__FILE__) . '/likebtn_like_button_most_liked_widget.class.php');
// most liked short code
function likebtn_most_liked_widget_shortcode($args) {
global $LikeBtnLikeButtonMostLiked;
$options = $args;
if (isset($options['number'])) {
$options['number'] = (int) $options['number'];
}
$options['entity_name'] = array();
if (isset($options['content_types'])) {
$options['entity_name'] = explode(',', $options['content_types']);
}
return $LikeBtnLikeButtonMostLiked->widget(null, $options);
}
add_shortcode('likebtn_most_liked', 'likebtn_most_liked_widget_shortcode');
// get current admin subpage
function _likebtn_get_subpage()
{
$likebtn_entities = _likebtn_get_entities();
$subpage = LIKEBTN_ENTITY_POST;
if (!empty($_POST['likebtn_subpage']) && array_key_exists($_POST['likebtn_subpage'], $likebtn_entities) ) {
$subpage = $_POST['likebtn_subpage'];
} elseif (!empty($_GET['likebtn_subpage']) && array_key_exists($_GET['likebtn_subpage'], $likebtn_entities) ) {
$subpage = $_GET['likebtn_subpage'];
}
return $subpage;
}
// Save BuddyPress Member Profile votes
function _likebtn_save_bp_member_votes($entity_id, $likes, $dislikes, $likes_minus_dislikes)
{
global $wpdb;
if (!_likebtn_is_bp_active()) {
return false;
}
$bp_xprofile = $wpdb->get_row("
SELECT id
FROM ".$wpdb->prefix."bp_xprofile_data
WHERE user_id = {$entity_id}
");
if (!empty($bp_xprofile)) {
if ($likes !== null) {
if (count(bp_xprofile_get_meta($entity_id, LIKEBTN_BP_XPROFILE_OBJECT_TYPE, LIKEBTN_META_KEY_LIKES)) > 1) {
bp_xprofile_delete_meta($entity_id, LIKEBTN_BP_XPROFILE_OBJECT_TYPE, LIKEBTN_META_KEY_LIKES);
bp_xprofile_add_meta($entity_id, LIKEBTN_BP_XPROFILE_OBJECT_TYPE, LIKEBTN_META_KEY_LIKES, $likes, true);
} else {
bp_xprofile_update_meta($entity_id, LIKEBTN_BP_XPROFILE_OBJECT_TYPE, LIKEBTN_META_KEY_LIKES, $likes);
}
}
if ($dislikes !== null) {
if (count(bp_xprofile_get_meta($entity_id, LIKEBTN_BP_XPROFILE_OBJECT_TYPE, LIKEBTN_META_KEY_DISLIKES)) > 1) {
bp_xprofile_delete_meta($entity_id, LIKEBTN_BP_XPROFILE_OBJECT_TYPE, LIKEBTN_META_KEY_DISLIKES);
bp_xprofile_add_meta($entity_id, LIKEBTN_BP_XPROFILE_OBJECT_TYPE, LIKEBTN_META_KEY_DISLIKES, $dislikes, true);
} else {
bp_xprofile_update_meta($entity_id, LIKEBTN_BP_XPROFILE_OBJECT_TYPE, LIKEBTN_META_KEY_DISLIKES, $dislikes);
}
}
if ($likes_minus_dislikes !== null) {
if (count(bp_xprofile_get_meta($entity_id, LIKEBTN_BP_XPROFILE_OBJECT_TYPE, LIKEBTN_META_KEY_LIKES_MINUS_DISLIKES)) > 1) {
bp_xprofile_delete_meta($entity_id, LIKEBTN_BP_XPROFILE_OBJECT_TYPE, LIKEBTN_META_KEY_LIKES_MINUS_DISLIKES);
bp_xprofile_add_meta($entity_id, LIKEBTN_BP_XPROFILE_OBJECT_TYPE, LIKEBTN_META_KEY_LIKES_MINUS_DISLIKES, $likes_minus_dislikes, true);
} else {
bp_xprofile_update_meta($entity_id, LIKEBTN_BP_XPROFILE_OBJECT_TYPE, LIKEBTN_META_KEY_LIKES_MINUS_DISLIKES, $likes_minus_dislikes);
}
}
return true;
}
return false;
}
// Save user votes
function _likebtn_save_user_votes($entity_id, $likes, $dislikes, $likes_minus_dislikes)
{
global $wpdb;
$user = get_user_by('id', $entity_id);
if (!empty($user)) {
if ($likes !== null) {
if (count(get_user_meta($entity_id, LIKEBTN_META_KEY_LIKES)) > 1) {
delete_user_meta($entity_id, LIKEBTN_META_KEY_LIKES);
add_user_meta($entity_id, LIKEBTN_META_KEY_LIKES, $likes, true);
} else {
update_user_meta($entity_id, LIKEBTN_META_KEY_LIKES, $likes);
}
}
if ($dislikes !== null) {
if (count(get_user_meta($entity_id, LIKEBTN_META_KEY_DISLIKES)) > 1) {
delete_user_meta($entity_id, LIKEBTN_META_KEY_DISLIKES);
add_user_meta($entity_id, LIKEBTN_META_KEY_DISLIKES, $dislikes, true);
} else {
update_user_meta($entity_id, LIKEBTN_META_KEY_DISLIKES, $dislikes);
}
}
if ($likes_minus_dislikes !== null) {
if (count(get_user_meta($entity_id, LIKEBTN_META_KEY_LIKES_MINUS_DISLIKES)) > 1) {
delete_user_meta($entity_id, LIKEBTN_META_KEY_LIKES_MINUS_DISLIKES);
add_user_meta($entity_id, LIKEBTN_META_KEY_LIKES_MINUS_DISLIKES, $likes_minus_dislikes, true);
} else {
update_user_meta($entity_id, LIKEBTN_META_KEY_LIKES_MINUS_DISLIKES, $likes_minus_dislikes);
}
}
}
return true;
}
################
### Frontend ###
################
function _likebtn_get_markup($entity_name, $entity_id, $values = null, $use_entity_name = '', $use_entity_settings = true, $wrap = true)
{
global $wp_version;
global $likebtn_map_entities;
$prepared_settings = array();
if (!$use_entity_name) {
$use_entity_name = $entity_name;
}
// Cut excerpt flag from entity_name
if ($entity_id !== 'demo') {
$entity_name = _likebtn_cut_list_flag($entity_name);
}
if ($values && $values['identifier']) {
$data = ' data-identifier="' . $values['identifier'] . '" ';
} else {
$identifier = $entity_name;
if (!empty($likebtn_map_entities[$entity_name])) {
$identifier = $likebtn_map_entities[$entity_name];
}
$data = ' data-identifier="' . $identifier . '_' . $entity_id . '" ';
}
// Site ID
if (get_option('likebtn_site_id')) {
$data .= ' data-site_id="' . get_option('likebtn_site_id') . '" ';
}
$likebtn_settings = _likebtn_get_all_settings();
foreach ($likebtn_settings as $option_name => $option_info) {
if ($values && isset($values[$option_name])) {
// if values passed
$option_value = $values[$option_name];
} elseif (!$use_entity_settings) {
// Do not use entity value - use default. Usually in shortcodes.
$option_value = $option_info['default'];
} else {
$option_value = get_option('likebtn_settings_' . $option_name . '_' . $use_entity_name);
}
$option_value_prepared = _likebtn_prepare_option($option_name, $option_value);
$prepared_settings[$option_name] = $option_value_prepared;
// do not add option if it has default value
if ($option_value == $likebtn_settings[$option_name]['default'] ||
//$option_value === '' || is_bool($option_value)
($option_value === '' && $likebtn_settings[$option_name]['default'] == '0')
) {
// option has default value
} else {
$data .= ' data-' . $option_name . '="' . $option_value_prepared . '" ';
}
}
// Add item options
$entity = null;
$entity_url = '';
$entity_title = '';
$entity_image = '';
if ($entity_name == LIKEBTN_ENTITY_COMMENT) {
$entity = get_comment($entity_id);
if ($entity) {
$entity_url = get_comment_link($entity->comment_ID);
$entity_title = $entity->comment_content;
}
} else 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))) {
$entity_title = _likebtn_bp_get_activity_title($entity_id);
if (function_exists('bp_activity_get_permalink')) {
$entity_url = bp_activity_get_permalink($entity_id);
}
} else if ($entity_name == LIKEBTN_ENTITY_BP_MEMBER) {
if (function_exists('bp_core_get_username')) {
$entity_title = bp_core_get_username($entity_id);
}
if (function_exists('bp_core_get_user_domain')) {
$entity_url = bp_core_get_user_domain($entity_id);
}
} else {
$entity = get_post($entity_id);
if ($entity) {
$entity_url = get_permalink($entity->ID);
$entity_title = $entity->post_title;
$entity_image_url = wp_get_attachment_image_src(get_post_thumbnail_id($entity->ID), 'large');
if (!empty($entity_image_url[0])) {
$entity_image = $entity_image_url[0];
}
}
}
if ($entity_url && !$prepared_settings['item_url']) {
$data .= ' data-item_url="' . $entity_url . '" ';
}
if ($entity_title && !$prepared_settings['item_title']) {
$entity_title = preg_replace('/\s+/', ' ', $entity_title);
$entity_title = htmlspecialchars($entity_title);
$data .= ' data-item_title="' . $entity_title . '" ';
}
if ($entity_image && !$prepared_settings['item_image']) {
$data .= ' data-item_image="' . $entity_image . '" ';
}
// Set engine and plugin info
$data .= ' data-engine="WordPress" ';
$data .= ' data-engine_v="' . $wp_version . '" ';
$plugin_v = LIKEBTN_VERSION;
if ($plugin_v) {
$data .= ' data-plugin_v="' . $plugin_v . '" ';
}
$public_url = _likebtn_get_public_url();
$markup = <<
MARKUP;
// HTML before
$html_before = '';
if (isset($values['html_before'])) {
$html_before = $values['html_before'];
} elseif (get_option('likebtn_html_before_' . $use_entity_name)) {
$html_before = get_option('likebtn_html_before_' . $use_entity_name);
}
if (trim($html_before)) {
$markup = $html_before . $markup;
}
// HTML after
$html_after = '';
if (isset($values['html_after'])) {
$html_after = $values['html_after'];
} elseif (get_option('likebtn_html_after_' . $use_entity_name)) {
$html_after = get_option('likebtn_html_after_' . $use_entity_name);
}
if (trim($html_after)) {
$markup = $markup . $html_after;
}
// alignment
if ($wrap) {
$alignment = get_option('likebtn_alignment_' . $use_entity_name);
if ($alignment == LIKEBTN_ALIGNMENT_RIGHT) {
$markup = '' . $markup . '';
} elseif ($alignment == LIKEBTN_ALIGNMENT_CENTER) {
$markup = '' . $markup . '';
} else {
$markup = '' . $markup . '';
}
}
return $markup;
}
// prepare option value
function _likebtn_prepare_option($option_name, $option_value)
{
global $likebtn_settings;
$option_value_prepared = $option_value;
// do not format i18n options
if (!strstr($option_name, 'i18n') &&
(!isset($likebtn_settings[$option_name]) || $likebtn_settings[$option_name]['default'] !== ''))
{
if (is_int($option_value)) {
if ($option_value) {
$option_value_prepared = 'true';
} else {
$option_value_prepared = 'false';
}
}
if ($option_value === '1') {
$option_value_prepared = 'true';
}
if ($option_value === '0' || $option_value === '') {
$option_value_prepared = 'false';
}
}
// Replace quotes with " to avoid XSS.
//$option_value_prepared = str_replace('"', '"', $option_value_prepared);
$option_value_prepared = htmlspecialchars($option_value_prepared);
return $option_value_prepared;
}
// get Entity settings
function _likebtn_get_entity_settings($entity_name) {
global $likebtn_settings;
$settings = array();
foreach ($likebtn_settings as $option_name => $option_info) {
$settings[$option_name] = get_option('likebtn_settings_' . $option_name . '_' . $entity_name);
}
return $settings;
}
// add Like Button to content
function likebtn_get_content($content, $callback_content_position = '') {
global $likebtn_no_excerpts;
if (is_feed()) {
return $content;
}
// detemine entity type
$real_entity_name = get_post_type();
// Excerpt mode
if (!is_single()) {
if (!in_array($real_entity_name, $likebtn_no_excerpts)) {
$real_entity_name = $real_entity_name . LIKEBTN_LIST_FLAG;
}
}
// get entity name whose settings should be copied
$use_entity_name = get_option('likebtn_use_settings_from_' . $real_entity_name);
if ($use_entity_name) {
$entity_name = $use_entity_name;
} else {
$entity_name = $real_entity_name;
}
if (get_option('likebtn_show_' . $real_entity_name) != '1'
|| get_option('likebtn_show_' . $entity_name) != '1')
{
return $content;
}
$entity_id = get_the_ID();
// get the Posts/Pages IDs where we do not need to show like functionality
$allow_ids = explode(",", get_option('likebtn_allow_ids_' . $entity_name));
$exclude_ids = explode(",", get_option('likebtn_exclude_ids_' . $entity_name));
$exclude_categories = get_option('likebtn_exclude_categories_' . $entity_name);
$exclude_sections = get_option('likebtn_exclude_sections_' . $entity_name);
if (empty($exclude_categories)) {
$exclude_categories = array();
}
if (empty($exclude_sections)) {
$exclude_sections = array();
}
// checking if section is excluded
if ((in_array('home', $exclude_sections) && is_home()) || (in_array('archive', $exclude_sections) && is_archive())) {
return $content;
}
// checking if category is excluded
$categories = get_the_category();
foreach ($categories as $category) {
if (in_array($category->cat_ID, $exclude_categories) && !in_array($entity_id, $allow_ids)) {
return $content;
}
}
// check if post is excluded
if (in_array($entity_id, $exclude_ids)) {
return $content;
}
// check Post view mode
/*switch (get_option('likebtn_post_view_mode_' . $entity_name)) {
case LIKEBTN_POST_VIEW_MODE_FULL:
if (!is_single()) {
return $content;
}
break;
case LIKEBTN_POST_VIEW_MODE_EXCERPT:
if (is_single()) {
return $content;
}
break;
}*/
// check Post format
$post_format = get_post_format($entity_id);
if (!$post_format) {
$post_format = 'standard';
}
if (!in_array('all', get_option('likebtn_post_format_' . $entity_name)) &&
!in_array($post_format, get_option('likebtn_post_format_' . $entity_name))
) {
return $content;
}
// check user authorization
$user_logged_in = get_option('likebtn_user_logged_in_' . $entity_name);
if ($user_logged_in === '1' || $user_logged_in === '0') {
if ($user_logged_in == '1' && !is_user_logged_in()) {
return $content;
}
if ($user_logged_in == '0' && is_user_logged_in()) {
return $content;
}
}
$html = _likebtn_get_markup($real_entity_name, $entity_id, array(), $entity_name);
$position = get_option('likebtn_position_' . $entity_name);
if ($callback_content_position && function_exists($callback_content_position)) {
$content = call_user_func($callback_content_position, $content, $html, $position);
} else {
if ($position == LIKEBTN_POSITION_TOP) {
$content = $html . $content;
} elseif ($position == LIKEBTN_POSITION_BOTTOM) {
$content = $content . $html;
} else {
$content = $html . $content . $html;
}
}
return $content;
}
// add Like Button to the entity (except Comment)
function likebtn_the_content($content) {
if (get_post_type() == LIKEBTN_ENTITY_PRODUCT) {
return $content;
}
$content = likebtn_get_content($content);
return $content;
}
add_filter('the_content', 'likebtn_the_content');
add_filter('the_excerpt', 'likebtn_the_content');
// WooCommerce - top and button
function likebtn_woocommerce_product($content) {
$content = likebtn_get_content($content);
echo $content;
}
// WooCommerce - top
function likebtn_woocommerce_product_top($content) {
$content = likebtn_get_content($content, '_likebtn_woocommerce_content_top');
echo $content;
}
function _likebtn_woocommerce_content_top($content, $html, $position) {
// WooCommerce
if ($position == LIKEBTN_POSITION_BOTTOM) {
return $content;
} else {
return $html . $content;
}
}
// WooCommerce - bottom
function likebtn_woocommerce_after_main_content($content) {
if (!is_single()) {
return false;
}
$content = likebtn_get_content($content, '_likebtn_woocommerce_content_bottom');
echo $content;
}
function likebtn_woocommerce_product_bottom($content) {
$content = likebtn_get_content($content, '_likebtn_woocommerce_content_bottom');
echo $content;
}
function _likebtn_woocommerce_content_bottom($content, $html, $position) {
// WooCommerce
if ($position == LIKEBTN_POSITION_TOP) {
return $content;
} else {
return $html . $content;
}
}
add_action('woocommerce_single_product_summary', 'likebtn_woocommerce_product_top', 7);
add_action('woocommerce_after_main_content', 'likebtn_woocommerce_after_main_content', 7);
add_action('woocommerce_after_shop_loop_item_title', 'likebtn_woocommerce_product_top', 7);
add_action('woocommerce_after_shop_loop_item_title', 'likebtn_woocommerce_product_bottom', 12);
// add Like Button to the Comment
function likebtn_comment_text($content) {
global $comment;
if (is_feed()) {
return $content;
}
// detemine entity type
$real_entity_name = LIKEBTN_ENTITY_COMMENT;
// get entity name whose settings should be copied
$use_entity_name = get_option('likebtn_use_settings_from_' . $real_entity_name);
if ($use_entity_name) {
$entity_name = $use_entity_name;
} else {
$entity_name = $real_entity_name;
}
if (get_option('likebtn_show_' . $real_entity_name) != '1'
|| get_option('likebtn_show_' . $entity_name) != '1')
{
return $content;
}
$comment_id = $comment->comment_ID;
//$comment = get_comment($comment_id, ARRAY_A);
$post_id = $comment->comment_post_ID;
// get the Posts/Pages IDs where we do not need to show like functionality
$allow_ids = explode(",", get_option('likebtn_allow_ids_' . $entity_name));
$exclude_ids = explode(",", get_option('likebtn_exclude_ids_' . $entity_name));
$exclude_categories = get_option('likebtn_exclude_categories_' . $entity_name);
$exclude_sections = get_option('likebtn_exclude_sections_' . $entity_name);
if (empty($exclude_categories)) {
$exclude_categories = array();
}
if (empty($exclude_sections)) {
$exclude_sections = array();
}
// checking if section is excluded
if ((in_array('home', $exclude_sections) && is_home()) || (in_array('archive', $exclude_sections) && is_archive())) {
return $content;
}
// checking if category is excluded
$categories = get_the_category();
foreach ($categories as $category) {
if (in_array($category->cat_ID, $exclude_categories) && !in_array($post_id, $allow_ids)) {
return $content;
}
}
// check if post is excluded
if (in_array($post_id, $exclude_ids)) {
return $content;
}
// check Post view mode - no need
// check Post format
$post_format = get_post_format($post_id);
if (!$post_format) {
$post_format = 'standard';
}
if (!in_array('all', get_option('likebtn_post_format_' . $entity_name)) &&
!in_array($post_format, get_option('likebtn_post_format_' . $entity_name))
) {
return $content;
}
$html = _likebtn_get_markup($real_entity_name, $comment_id, array(), $entity_name);
$position = get_option('likebtn_position_' . $entity_name);
if ($position == LIKEBTN_POSITION_TOP) {
$content = $html . $content;
} elseif ($position == LIKEBTN_POSITION_BOTTOM) {
$content = $content . $html;
} else {
$content = $html . $content . $html;
}
return $content;
}
add_filter('comment_text', 'likebtn_comment_text');
// show the Like Button in Post/Page
// if Like Button is enabled in admin for Post/Page do not show button twice
function likebtn_post($post_id = NULL) {
global $post;
if (empty($post_id)) {
$post_id = $post->ID;
}
// detemine entity type
if (is_page()) {
$entity_name = LIKEBTN_ENTITY_PAGE;
} else {
$entity_name = LIKEBTN_ENTITY_POST;
}
// check if the Like Button should be displayed
// if Like Button enabled for Post or Page in Admin do not show Like Button twice
if ($entity_name == LIKEBTN_ENTITY_POST && get_option('likebtn_show_' . LIKEBTN_ENTITY_POST) == '1') {
return;
}
if ($entity_name == LIKEBTN_ENTITY_PAGE && get_option('likebtn_show_' . LIKEBTN_ENTITY_PAGE) == '1') {
return;
}
// 'post' here is for the sake of backward compatibility
$html = _likebtn_get_markup('post', $post_id);
echo $html;
}
// get or echo the Like Button in comment
function likebtn_comment($comment_id = NULL) {
//global $comment;
if (empty($comment_id)) {
$comment_id = get_comment_ID();
}
// if Like Button enabled for Comment in Admin do not show Like Button twice
if (get_option('likebtn_show_' . LIKEBTN_ENTITY_COMMENT) == '1') {
return;
}
$html = _likebtn_get_markup(LIKEBTN_ENTITY_COMMENT, $comment_id);
echo $html;
}
// test synchronization callback
function likebtn_manual_sync_callback() {
$likebtn_account_email = '';
if (isset($_POST['likebtn_account_email'])) {
$likebtn_account_email = $_POST['likebtn_account_email'];
}
$likebtn_account_api_key = '';
if (isset($_POST['likebtn_account_api_key'])) {
$likebtn_account_api_key = $_POST['likebtn_account_api_key'];
}
require_once(dirname(__FILE__) . '/likebtn_like_button.class.php');
$likebtn = new LikeBtnLikeButton();
$sync_response = $likebtn->syncVotes($likebtn_account_email, $likebtn_account_api_key, true);
if ($sync_response['result'] == 'success') {
$result_text = __('OK', LIKEBTN_I18N_DOMAIN);
} else {
$result_text = __('Error', LIKEBTN_I18N_DOMAIN);
}
$response = array(
'result' => $sync_response['result'],
'result_text' => $result_text,
'message' => $sync_response['message'],
);
if (!DOING_AJAX) {
define('DOING_AJAX', true);
}
if (ob_get_contents()) {
ob_clean();
}
wp_send_json($response);
}
add_action('wp_ajax_likebtn_manual_sync', 'likebtn_manual_sync_callback');
// test synchronization callback
function likebtn_test_sync_callback() {
$likebtn_account_email = '';
if (isset($_POST['likebtn_account_email'])) {
$likebtn_account_email = $_POST['likebtn_account_email'];
}
$likebtn_account_api_key = '';
if (isset($_POST['likebtn_account_api_key'])) {
$likebtn_account_api_key = $_POST['likebtn_account_api_key'];
}
require_once(dirname(__FILE__) . '/likebtn_like_button.class.php');
$likebtn = new LikeBtnLikeButton();
$test_response = $likebtn->testSync($likebtn_account_email, $likebtn_account_api_key);
if ($test_response['result'] == 'success') {
$result_text = __('OK', LIKEBTN_I18N_DOMAIN);
} else {
$result_text = __('Error', LIKEBTN_I18N_DOMAIN);
}
$response = array(
'result' => $test_response['result'],
'result_text' => $result_text,
'message' => $test_response['message'],
);
define( 'DOING_AJAX', true );
ob_clean();
wp_send_json($response);
}
add_action('wp_ajax_likebtn_test_sync', 'likebtn_test_sync_callback');
// edit item callback
function likebtn_edit_item_callback() {
$entity_name = '';
if (isset($_POST['entity_name'])) {
$entity_name = $_POST['entity_name'];
}
$entity_id = '';
if (isset($_POST['entity_id'])) {
$entity_id = $_POST['entity_id'];
}
$identifier = likebtn_get_identifier($entity_name, $entity_id);
$type = '';
if (isset($_POST['type'])) {
$type = $_POST['type'];
}
$value = '';
if (isset($_POST['value'])) {
$value = $_POST['value'];
}
require_once(dirname(__FILE__) . '/likebtn_like_button.class.php');
$likebtn = new LikeBtnLikeButton();
$edit_response = $likebtn->edit($identifier, $type, $value);
if ($edit_response['result'] == 'success') {
$result_text = __('OK', LIKEBTN_I18N_DOMAIN);
// Update custom fields
if ($type == '1') {
$likes = $value;
$dislikes = null;
} else {
$dislikes = $value;
$likes = null;
}
$likebtn->updateCustomFields($identifier, $likes, $dislikes);
} else {
$result_text = __('Error', LIKEBTN_I18N_DOMAIN);
}
$response = array(
'result' => $edit_response['result'],
'result_text' => $result_text,
'message' => $edit_response['message'],
'value' => $value
);
define( 'DOING_AJAX', true );
ob_clean();
wp_send_json($response);
}
add_action('wp_ajax_likebtn_edit_item', 'likebtn_edit_item_callback');
// reset items likes/dislikes
function likebtn_reset($entity_name, $items)
{
$counter = 0;
if (empty($entity_name) || empty($items)) {
return false;
}
require_once(dirname(__FILE__) . '/likebtn_like_button.class.php');
$likebtn = new LikeBtnLikeButton();
// prepare an array for resettings in the CMS
$reset_list = array();
$reset_list['response'] = array('items'=>array());
foreach ($items as $item_identifier) {
$identifier = $entity_name . '_' . $item_identifier;
// reset votes in LikeBtn
$likebtn_result = $likebtn->reset($identifier);
$reset_list['response']['items'][] = array(
'identifier' => $identifier,
'likes' => 0,
'dislikes' => 0
);
if ($likebtn_result) {
$counter++;
}
}
if ($reset_list) {
$likebtn->updateVotes($reset_list);
}
return $counter;
}
// get entity identifier
function likebtn_get_identifier($entity_name, $entity_id)
{
$identifier = $entity_name . '_' . $entity_id;
return $identifier;
}
// get comments sorted by likes
function likebtn_comments_sorted_by_likes()
{
// function for sorting comments by Likes
function sort_comments_by_likes($comment_a, $comment_b)
{
$comment_a_meta = get_comment_meta($comment_a->comment_ID, LIKEBTN_META_KEY_LIKES);
$comment_b_meta = get_comment_meta($comment_b->comment_ID, LIKEBTN_META_KEY_LIKES);
$comment_a_likes = (int)$comment_a_meta[0];
$comment_b_likes = (int)$comment_b_meta[0];
if ($comment_a_likes > $comment_b_likes) {
return -1;
} elseif ($comment_a_likes < $comment_b_likes) {
return 1;
}
return 0;
}
global $wp_query;
$comments = $wp_query->comments;
usort($comments, 'sort_comments_by_likes');
return $comments;
}
// get comments sorted by dislikes
function likebtn_comments_sorted_by_dislikes()
{
// function for sorting comments by Likes
function sort_comments_by_dislikes($comment_a, $comment_b)
{
$comment_a_meta = get_comment_meta($comment_a->comment_ID, LIKEBTN_META_KEY_DISLIKES);
$comment_b_meta = get_comment_meta($comment_b->comment_ID, LIKEBTN_META_KEY_DISLIKES);
$comment_a_dislikes = (int)$comment_a_meta[0];
$comment_b_dislikes = (int)$comment_b_meta[0];
if ($comment_a_dislikes > $comment_b_dislikes) {
return -1;
} elseif ($comment_a_dislikes < $comment_b_dislikes) {
return 1;
}
return 0;
}
global $wp_query;
$comments = $wp_query->comments;
usort($comments, 'sort_comments_by_dislikes');
return $comments;
}
// Get the permalink for a comment on another blog.
function _likebtn_get_blog_comment_link( $blog_id, $comment_id ) {
switch_to_blog( $blog_id );
$link = get_comment_link( $comment_id );
restore_current_blog();
return $link;
}
// Converts entity name to title
function _likebtn_get_entity_title($entity_name)
{
global $likebtn_entity_titles;
$title = '';
$is_excerpt = false;
if (_likebtn_has_list_flag($entity_name)) {
$is_excerpt = true;
}
if (!array_key_exists($entity_name, $likebtn_entity_titles)) {
$entity_name = _likebtn_cut_list_flag($entity_name);
$title = __(str_replace('_', ' ', ucfirst($entity_name)));
if ($is_excerpt) {
$title .= ' ' . __('List');
}
} else {
$title = __($likebtn_entity_titles[$entity_name]);
}
return $title;
}
// Check if entity has a list flag
function _likebtn_has_list_flag($entity_name)
{
if (strstr($entity_name, LIKEBTN_LIST_FLAG)) {
return true;
} else {
return false;
}
}
// Cut list flag from entity name
function _likebtn_cut_list_flag($entity_name)
{
return str_replace(LIKEBTN_LIST_FLAG, '', $entity_name);
}
// Check if BuddyPress is installed and active
function _likebtn_is_bp_active()
{
if (function_exists('bp_is_active') && bp_is_active()) {
return true;
} else {
return false;
}
}
// add Like Button to content
function _likebtn_get_content_universal($real_entity_name, $entity_id, $content = '', $wrap = true, $current_position = '', $current_alignment = array())
{
// get entity name whose settings should be copied
$use_entity_name = get_option('likebtn_use_settings_from_' . $real_entity_name);
if ($use_entity_name) {
$entity_name = $use_entity_name;
} else {
$entity_name = $real_entity_name;
}
if (get_option('likebtn_show_' . $real_entity_name) != '1'
|| get_option('likebtn_show_' . $entity_name) != '1')
{
return '';
}
// check user authorization
$user_logged_in = get_option('likebtn_user_logged_in_' . $entity_name);
if ($user_logged_in === '1' || $user_logged_in === '0') {
if ($user_logged_in == '1' && !is_user_logged_in()) {
return '';
}
if ($user_logged_in == '0' && is_user_logged_in()) {
return '';
}
}
$html = _likebtn_get_markup($real_entity_name, $entity_id, array(), $entity_name, true, $wrap);
$position = get_option('likebtn_position_' . $entity_name);
if ($current_position && $position != LIKEBTN_POSITION_BOTH && $current_position != $position) {
return $content;
}
$alignment = get_option('likebtn_alignment_' . $entity_name);
if ($current_alignment && !in_array($alignment, $current_alignment)) {
return $content;
}
if ($content) {
if ($position == LIKEBTN_POSITION_TOP) {
$html = $html . $content;
} elseif ($position == LIKEBTN_POSITION_BOTTOM) {
$html = $content . $html;
} else {
$html = $html . $content . $html;
}
}
return $html;
}
// BuddyPress member profile
function likebtn_bp_member()
{
$content = _likebtn_get_content_universal(LIKEBTN_ENTITY_BP_MEMBER, buddypress()->displayed_user->id);
echo $content;
}
// User profile page.
add_action('bp_before_member_header_meta', 'likebtn_bp_member');
// BuddyPress activity
function _likebtn_bp_activity($wrap = true, $position = LIKEBTN_POSITION_BOTH, $content = '') {
$entity_id = '';
$entity_name = _likebtn_bp_get_entity_name();
//if ($entity_name == LIKEBTN_ENTITY_BP_ACTIVITY_UPDATE || $entity_name == LIKEBTN_ENTITY_BP_ACTIVITY_COMMENT) {
$entity_id = bp_get_activity_id();
/*} else {
$entity_id = bp_get_activity_secondary_item_id();
}*/
/*if ($entity_name == LIKEBTN_ENTITY_BP_ACTIVITY_TOPIC) {
echo 'entity_id'.$entity_id;
// Get bbPress topic id
if (function_exists("bb_get_first_post")) {
$post = bb_get_first_post($entity_id);
} else {
// We consider ppPress to be installed and enabled
return false;
// Get from DB
/*global $bb_table_prefix;
// Load bbPress config file
@include_once(?);
// Failed loading config file.
if (!defined("BBDB_NAME"))
return false;
$connection = null;
if (!$connection = mysql_connect(BBDB_HOST, BBDB_USER, BBDB_PASSWORD, true)){
return false;
}
if (!mysql_selectdb(BBDB_NAME, $connection)){
return false;
}
$results = mysql_query("SELECT * FROM {$bb_table_prefix}posts WHERE topic_id={$entity_id} AND post_position=1", $connection);
$post = mysql_fetch_object($results);/
}
if (empty($post->post_id)) {
return false;
}
$entity_id = $post->post_id;
}*/
if (!$entity_name || !$entity_id) {
return false;
}
echo _likebtn_get_content_universal($entity_name, $entity_id, $content, $wrap, $position);
}
// BuddyPress activity comment
function likebtn_bp_activity_comment($content) {
//return $content;
global $activities_template;
if (empty($activities_template) || empty($activities_template->activity) || empty($activities_template->activity->current_comment)) {
return $content;
}
$entity_id = $activities_template->activity->current_comment->id;
return _likebtn_get_content_universal(LIKEBTN_ENTITY_BP_ACTIVITY_COMMENT, $entity_id, $content);
}
// BuddyPress activity top
function likebtn_bp_activity_top($content)
{
return _likebtn_bp_activity(true, LIKEBTN_POSITION_TOP) . $content;
}
// BuddyPress activity bottom
function likebtn_bp_activity_bottom()
{
return _likebtn_bp_activity(false, LIKEBTN_POSITION_BOTTOM);
}
// Activity page.
//add_action("bp_has_activities", array(&$this, "BuddyPressBeforeActivityLoop"));
// BuddyPress activity top
add_filter('bp_get_activity_action', 'likebtn_bp_activity_top');
// BuddyPress activity bottom
add_action('bp_activity_entry_meta', 'likebtn_bp_activity_bottom');
// BuddyPress activity comment
add_filter('bp_get_activity_content', 'likebtn_bp_activity_comment');
// Forum topic page
add_filter('bp_has_topic_posts', 'likebtn_bp_activity');
// Get entity name of the current BuddyPress activity
// Not working for comments
function _likebtn_bp_get_entity_name()
{
$activity_type = bp_get_activity_type();
switch ($activity_type) {
case 'bbp_topic_create':
return LIKEBTN_ENTITY_BP_ACTIVITY_TOPIC;
case 'new_blog_post':
// Post
return LIKEBTN_ENTITY_BP_ACTIVITY_POST;
case 'new_member':
return LIKEBTN_ENTITY_BP_ACTIVITY_UPDATE;
default:
return LIKEBTN_ENTITY_BP_ACTIVITY_UPDATE;
}
}
// Get activity title
function _likebtn_bp_get_activity_title($id)
{
$activity = null;
$title = '';
if (function_exists('bp_activity_get_specific')) {
$activity_list = bp_activity_get_specific(array(
'activity_ids' => $id,
'display_comments' => true
));
if (!empty($activity_list['activities']) && !empty($activity_list['activities'][0])) {
$activity = $activity_list['activities'][0];
}
}
if ($activity) {
if ($activity->action) {
$title = $activity->action;
} elseif ($activity->content) {
$title = $activity->content;
} elseif ($activity->primary_link) {
$title = $activity->primary_link;
}
if ($activity->content && $activity->type != 'bbp_topic_create') {
$title = $title.': '.$activity->content;
}
$title = strip_tags($title);
}
return $title;
}
// Check if bbPress is installed and active
function _likebtn_is_bbp_active()
{
if (function_exists('bbp_has_replies')) {
return true;
} else {
return false;
}
}
// bbPress
function likebtn_bbp_reply($wrap = true, $position = LIKEBTN_POSITION_BOTH, $alignment = '', $content = '') {
// Reply to topic
$entity = bbp_get_reply(bbp_get_reply_id());
// Topic
if (!is_object($entity)) {
$entity = bbp_get_topic(bbp_get_topic_id());
}
if (!$entity) {
return false;
}
return _likebtn_get_content_universal(LIKEBTN_ENTITY_BBP_POST, $entity->ID, $content, $wrap, $position, $alignment);
}
// bbPress reply top left & center
function likebtn_bbp_has_replies($has_replies)
{
add_filter('bbp_theme_before_reply_admin_links', 'likebtn_bbp_reply_top_left');
add_filter('bbp_theme_after_reply_admin_links', 'likebtn_bbp_reply_top_right');
add_filter('bbp_get_reply_content', 'likebtn_bbp_reply_bottom');
add_filter('bbp_get_reply_author_link', 'likebtn_bbp_author_link');
return $has_replies;
}
// bbPress reply top left & center
function likebtn_bbp_reply_top_left()
{
echo likebtn_bbp_reply(false, LIKEBTN_POSITION_TOP, array(LIKEBTN_ALIGNMENT_LEFT, LIKEBTN_ALIGNMENT_CENTER));
}
// bbPress reply top left & center
function likebtn_bbp_reply_top_right()
{
echo likebtn_bbp_reply(false, LIKEBTN_POSITION_TOP, array(LIKEBTN_ALIGNMENT_RIGHT));
}
// bbPress reply bottom
function likebtn_bbp_reply_bottom($content)
{
echo $content.likebtn_bbp_reply(true, LIKEBTN_POSITION_BOTTOM);
}
// bbPress thread
function likebtn_bbp_author_link($author_link)
{
global $post;
$reply_id = bbp_get_reply_id($post->ID);
if (bbp_is_reply_anonymous($reply_id)) {
return $author_link;
}
$author_id = bbp_get_reply_author_id($reply_id);
$content = _likebtn_get_content_universal(LIKEBTN_ENTITY_BBP_USER, $author_id);
return $author_link . $content;
}
// bbPress reply bottom
function likebtn_bbp_user_profile()
{
$content = _likebtn_get_content_universal(LIKEBTN_ENTITY_BBP_USER, bbpress()->displayed_user->ID);
echo $content;
}
add_filter('bbp_has_replies', 'likebtn_bbp_has_replies');
add_action('bbp_template_after_user_profile', 'likebtn_bbp_user_profile');
// Add style to frontend
function likebtn_enqueue_style()
{
$src = _likebtn_get_public_url() . 'css/style.css?ver=' . LIKEBTN_VERSION;
wp_enqueue_style('likebtn_style', $src);
}
// Add frontend style
add_action('wp_enqueue_scripts', 'likebtn_enqueue_style');
// Current + deprecated settings
function _likebtn_get_all_settings()
{
global $likebtn_settings;
global $likebtn_settings_deprecated;
return array_merge($likebtn_settings, $likebtn_settings_deprecated);
}
// Prepare entity title
function _likebtn_prepare_title($entity_name, $title, $max = LIKEBTN_WIDGET_TITLE_LENGTH)
{
$title = stripslashes($title);
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))) {
$title = strip_tags($title);
}
if (function_exists('qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage')) {
$title = qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($title);
}
/*if ($entity_name == LIKEBTN_ENTITY_COMMENT) {
if (mb_strlen($title) > 30) {
$title = mb_substr($title, 0, 30) . '...';
}
} else*/
return _likebtn_shorten_title($title, $max);
}
// Shorten title
function _likebtn_shorten_title($title, $max = LIKEBTN_WIDGET_TITLE_LENGTH)
{
if (mb_strlen($title) > $max) {
$title = mb_substr($title, 0, $max) . '...';
}
return $title;
}
// Shorten title
function _likebtn_shorten_excerpt($excerpt, $max = LIKEBTN_WIDGET_EXCERPT_LENGTH)
{
if (mb_strlen($excerpt) > $max) {
$excerpt = mb_substr($excerpt, 0, $max) . '...';
}
return $excerpt;
}
// Get entity URL
function _likebtn_get_entity_url($entity_name, $entity_id, $url = '')
{
if ($url) {
return $url;
}
switch ($entity_name) {
case LIKEBTN_ENTITY_COMMENT:
$url = get_comment_link($entity_id);
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:
if (function_exists('bp_activity_get_permalink')) {
$url = bp_activity_get_permalink($entity_id);
}
break;
case LIKEBTN_ENTITY_BP_MEMBER:
case LIKEBTN_ENTITY_BBP_USER:
if (function_exists('bp_core_get_user_domain')) {
$url = bp_core_get_user_domain($entity_id);
}
break;
default:
$url = get_permalink($entity_id);
break;
}
return $url;
}