function kopa_click_like_button() { check_ajax_referer('kopa_click_like_button', 'ajax_nonce'); if (!empty($_POST['post_id'])) { $result = array('status' => 'disable', 'total' => 0); $post_id = (int) $_POST['post_id']; $status = $_POST['status']; $include_text = $_POST['include_text']; $public_key = KOPA_OPT_PREFIX . 'likes'; $single_key = KOPA_OPT_PREFIX . 'like_by_' . KopaUtil::get_client_IP(); $total = KopaUtil::get_likes($post_id); $is_liked = KopaUtil::is_liked($post_id); if ('enable' == $status && !$is_liked) { $total++; update_post_meta($post_id, $single_key, true); update_post_meta($post_id, $public_key, abs($total)); $result['class'] = 'kopa-button-likes-disable'; } else { $total--; delete_post_meta($post_id, $single_key); update_post_meta($post_id, $public_key, abs($total)); $result['class'] = 'kopa-button-likes-enable'; } if ($include_text) { if ($total < 2) { $total .= __(' Like', kopa_get_domain()); } else { $total .= __(' Likes', kopa_get_domain()); } } $result['total'] = $total; echo json_encode($result); } exit; }
/** * @package Kopa * @subpackage Core * @author thethangtran <*****@*****.**> * @since 1.0.0 */ public static function is_liked($post_id) { $key = KOPA_OPT_PREFIX . 'like_by_' . KopaUtil::get_client_IP(); return KopaUtil::get_post_meta($post_id, $key, true, 'Boolean', false); }