| 1 |
<?php |
| 2 |
|
| 3 |
namespace King_Addons; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) exit; |
| 6 |
|
| 7 |
class Post_Likes_Ajax |
| 8 |
{ |
| 9 |
public function __construct() |
| 10 |
{ |
| 11 |
add_action('wp_ajax_nopriv_king_addons_likes_init', [$this, 'likes_init']); |
| 12 |
add_action('wp_ajax_king_addons_likes_init', [$this, 'likes_init']); |
| 13 |
} |
| 14 |
|
| 15 |
public function likes_init() |
| 16 |
{ |
| 17 |
$nonce = isset($_REQUEST['nonce']) ? sanitize_text_field(wp_unslash($_REQUEST['nonce'])) : ''; |
| 18 |
if (!wp_verify_nonce($nonce, 'king-addons-post-likes-nonce')) exit(esc_html__('Not permitted', 'king-addons')); |
| 19 |
|
| 20 |
$js_disabled = !empty($_REQUEST['disabled']); |
| 21 |
$post_id = (!empty($_REQUEST['post_id']) && is_numeric($_REQUEST['post_id'])) ? absint($_REQUEST['post_id']) : ''; |
| 22 |
if (!$post_id) return; |
| 23 |
|
| 24 |
$count = (int)get_post_meta($post_id, '_post_like_count', true); |
| 25 |
if (!$this->already_liked($post_id)) { |
| 26 |
if (is_user_logged_in()) { |
| 27 |
$user_id = get_current_user_id(); |
| 28 |
update_user_option($user_id, '_user_like_count', (int)get_user_option('_user_like_count', $user_id) + 1); |
| 29 |
update_post_meta($post_id, '_user_liked', $this->get_user_likes($user_id, $post_id)); |
| 30 |
} else { |
| 31 |
update_post_meta($post_id, '_user_IP', $this->get_IP_likes($this->get_IP(), $post_id)); |
| 32 |
} |
| 33 |
$like_count = ++$count; |
| 34 |
$response['status'] = 'liked'; |
| 35 |
} else { |
| 36 |
if (is_user_logged_in()) { |
| 37 |
$user_id = get_current_user_id(); |
| 38 |
$user_like_count = (int)get_user_option('_user_like_count', $user_id); |
| 39 |
if ($user_like_count > 0) { |
| 40 |
update_user_option($user_id, '_user_like_count', $user_like_count - 1); |
| 41 |
} |
| 42 |
$post_users = $this->get_user_likes($user_id, $post_id); |
| 43 |
if ($post_users) { |
| 44 |
unset($post_users[array_search($user_id, $post_users, true)]); |
| 45 |
update_post_meta($post_id, '_user_liked', $post_users); |
| 46 |
} |
| 47 |
} else { |
| 48 |
$post_users = $this->get_IP_likes($this->get_IP(), $post_id); |
| 49 |
if ($post_users) { |
| 50 |
unset($post_users[array_search($this->get_IP(), $post_users, true)]); |
| 51 |
update_post_meta($post_id, '_user_IP', $post_users); |
| 52 |
} |
| 53 |
} |
| 54 |
$like_count = ($count > 0) ? --$count : 0; |
| 55 |
$response['status'] = 'unliked'; |
| 56 |
} |
| 57 |
|
| 58 |
update_post_meta($post_id, '_post_like_count', $like_count); |
| 59 |
update_post_meta($post_id, '_post_like_modified', date('Y-m-d H:i:s')); |
| 60 |
$response['count'] = $this->get_like_count($like_count); |
| 61 |
|
| 62 |
if ($js_disabled) { |
| 63 |
wp_redirect(get_permalink($post_id)); |
| 64 |
exit; |
| 65 |
} |
| 66 |
wp_send_json($response); |
| 67 |
} |
| 68 |
|
| 69 |
public function get_button($post_id, $settings) |
| 70 |
{ |
| 71 |
$nonce = wp_create_nonce('king-addons-post-likes-nonce'); |
| 72 |
$like_count = (int)get_post_meta($post_id, '_post_like_count', true); |
| 73 |
$button_text = $settings['element_like_text']; |
| 74 |
$default_text_class = $button_text === '' ? ' king-addons-likes-no-default' : ''; |
| 75 |
if ($like_count === 0) $default_text_class .= ' king-addons-likes-zero'; |
| 76 |
|
| 77 |
if ($this->already_liked($post_id)) { |
| 78 |
$title = esc_html__('Like', 'king-addons'); |
| 79 |
$liked_class = ' king-addons-already-liked'; |
| 80 |
$icon_class = str_replace('far', 'fas', $settings['element_like_icon']); |
| 81 |
} else { |
| 82 |
$title = esc_html__('Unlike', 'king-addons'); |
| 83 |
$liked_class = ''; |
| 84 |
$icon_class = $settings['element_like_icon']; |
| 85 |
} |
| 86 |
|
| 87 |
$attributes = 'href="' . esc_url(admin_url('admin-ajax.php?action=king_addons_likes_init&post_id=' . $post_id . '&nonce=' . $nonce)) . '"'; |
| 88 |
$attributes .= ' class="king-addons-post-like-button' . esc_attr($liked_class . $default_text_class) . '"'; |
| 89 |
$attributes .= ' title="' . esc_attr($title) . '"'; |
| 90 |
$attributes .= ' data-ajax="' . esc_url(admin_url('admin-ajax.php')) . '"'; |
| 91 |
$attributes .= ' data-icon="' . esc_attr($icon_class) . '"'; |
| 92 |
$attributes .= ' data-nonce="' . esc_attr($nonce) . '"'; |
| 93 |
$attributes .= ' data-post-id="' . esc_attr($post_id) . '"'; |
| 94 |
/** @noinspection DuplicatedCode */ |
| 95 |
$attributes .= ' data-text="' . esc_attr($button_text) . '"'; |
| 96 |
$output = '<a ' . $attributes . '>'; |
| 97 |
$output .= '<i class="' . esc_attr($icon_class) . '"></i>'; |
| 98 |
$output .= $this->get_like_count($like_count, $button_text); |
| 99 |
$output .= '</a>'; |
| 100 |
return $output; |
| 101 |
} |
| 102 |
|
| 103 |
public function already_liked($post_id) |
| 104 |
{ |
| 105 |
$post_meta = is_user_logged_in() ? get_post_meta($post_id, '_user_liked') : get_post_meta($post_id, '_user_IP'); |
| 106 |
$post_users = !empty($post_meta[0]) ? $post_meta[0] : []; |
| 107 |
$user_id = is_user_logged_in() ? get_current_user_id() : $this->get_IP(); |
| 108 |
return (is_array($post_users) && in_array($user_id, $post_users)); |
| 109 |
} |
| 110 |
|
| 111 |
public function get_user_likes($user_id, $post_id) |
| 112 |
{ |
| 113 |
$post_meta = get_post_meta($post_id, '_user_liked'); |
| 114 |
$post_users = !empty($post_meta[0]) ? $post_meta[0] : []; |
| 115 |
if (!in_array($user_id, $post_users)) { |
| 116 |
$post_users['user-' . $user_id] = $user_id; |
| 117 |
} |
| 118 |
return $post_users; |
| 119 |
} |
| 120 |
|
| 121 |
public function get_IP_likes($user_ip, $post_id) |
| 122 |
{ |
| 123 |
$post_meta = get_post_meta($post_id, '_user_IP'); |
| 124 |
$post_users = !empty($post_meta[0]) ? $post_meta[0] : []; |
| 125 |
if (!in_array($user_ip, $post_users)) { |
| 126 |
$post_users['ip-' . $user_ip] = $user_ip; |
| 127 |
} |
| 128 |
return $post_users; |
| 129 |
} |
| 130 |
|
| 131 |
public function get_IP() |
| 132 |
{ |
| 133 |
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { |
| 134 |
$ip = sanitize_text_field(wp_unslash($_SERVER['HTTP_CLIENT_IP'])); |
| 135 |
} else { |
| 136 |
$ip = !empty($_SERVER['REMOTE_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])) : '0.0.0.0'; |
| 137 |
} |
| 138 |
$ip = filter_var($ip, FILTER_VALIDATE_IP); |
| 139 |
return ($ip === false) ? '0.0.0.0' : $ip; |
| 140 |
} |
| 141 |
|
| 142 |
public function get_format_count($num) |
| 143 |
{ |
| 144 |
$precision = 2; |
| 145 |
if ($num >= 1e9) { |
| 146 |
$formatted = number_format($num / 1e9, $precision) . 'B'; |
| 147 |
} elseif ($num >= 1e6) { |
| 148 |
$formatted = number_format($num / 1e6, $precision) . 'M'; |
| 149 |
} elseif ($num >= 1e3) { |
| 150 |
$formatted = number_format($num / 1e3, $precision) . 'K'; |
| 151 |
} else { |
| 152 |
$formatted = $num; |
| 153 |
} |
| 154 |
return str_replace('.00', '', $formatted); |
| 155 |
} |
| 156 |
|
| 157 |
public function get_like_count($count, $like_text = '') |
| 158 |
{ |
| 159 |
$number = (is_numeric($count) && $count > 0) ? $this->get_format_count($count) : $like_text; |
| 160 |
return '<span class="king-addons-post-like-count">' . esc_html($number) . '</span>'; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
new Post_Likes_Ajax(); |