forms
5 days ago
wpr-ajax-search.php
5 days ago
wpr-filter-grid-media.php
5 days ago
wpr-form-handlers.php
5 days ago
wpr-grid-helpers.php
5 days ago
wpr-load-more-instagram-posts.php
5 days ago
wpr-load-more-tweets.php
5 days ago
wpr-post-likes.php
5 days ago
wpr-woo-grid-helpers.php
5 days ago
wpr-post-likes.php
319 lines
| 1 | <?php |
| 2 | namespace WprAddons\Classes\Modules; |
| 3 | |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | exit; // Exit if accessed directly. |
| 6 | } |
| 7 | |
| 8 | /** |
| 9 | * WPR_Post_Likes setup |
| 10 | * |
| 11 | * @since 1.0 |
| 12 | */ |
| 13 | class WPR_Post_Likes { |
| 14 | |
| 15 | /** |
| 16 | ** Constructor |
| 17 | */ |
| 18 | public function __construct() { |
| 19 | add_action( 'wp_ajax_nopriv_wpr_likes_init', [ $this, 'wpr_likes_init' ] ); |
| 20 | add_action( 'wp_ajax_wpr_likes_init', [ $this, 'wpr_likes_init' ] ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | ** Likes Init |
| 25 | */ |
| 26 | public function wpr_likes_init() { |
| 27 | // Security |
| 28 | $nonce = isset( $_REQUEST['nonce'] ) ? sanitize_text_field( wp_unslash($_REQUEST['nonce']) ) : 0; |
| 29 | |
| 30 | if ( ! wp_verify_nonce( $nonce, 'wpr-post-likes-nonce' ) ) { |
| 31 | exit( esc_html__( 'Not permitted', 'wpr-addons' ) ); |
| 32 | } |
| 33 | |
| 34 | // Test if javascript is disabled |
| 35 | $js_disabled = ( isset( $_REQUEST['disabled'] ) && $_REQUEST['disabled'] == true ) ? true : false; |
| 36 | |
| 37 | // Base variables |
| 38 | $post_id = ( isset( $_REQUEST['post_id'] ) && is_numeric( $_REQUEST['post_id'] ) ) ? absint($_REQUEST['post_id']) : ''; |
| 39 | |
| 40 | $post_users = NULL; |
| 41 | $like_count = 0; |
| 42 | |
| 43 | // Init |
| 44 | if ( $post_id != '' ) { |
| 45 | // Likes Count |
| 46 | $count = get_post_meta( $post_id, '_post_like_count', true ); |
| 47 | $count = ( isset( $count ) && is_numeric( $count ) ) ? $count : 0; |
| 48 | |
| 49 | // Like the Post |
| 50 | if ( ! $this->already_liked( $post_id ) ) { |
| 51 | // Logged-in User |
| 52 | if ( is_user_logged_in() ) { |
| 53 | $user_id = get_current_user_id(); |
| 54 | $post_users = $this->get_user_likes( $user_id, $post_id ); |
| 55 | |
| 56 | // Get Like Count |
| 57 | $user_like_count = get_user_option( '_user_like_count', $user_id ); |
| 58 | $user_like_count = ( isset( $user_like_count ) && is_numeric( $user_like_count ) ) ? $user_like_count : 0; |
| 59 | |
| 60 | // Update Like Count |
| 61 | update_user_option( $user_id, '_user_like_count', ++$user_like_count ); |
| 62 | |
| 63 | // Update Post |
| 64 | if ( $post_users ) { |
| 65 | update_post_meta( $post_id, '_user_liked', $post_users ); |
| 66 | } |
| 67 | |
| 68 | // Anonymous User |
| 69 | } else { |
| 70 | $post_users = $this->get_IP_likes( $this->get_IP(), $post_id ); |
| 71 | |
| 72 | // Update Post |
| 73 | if ( $post_users ) { |
| 74 | update_post_meta( $post_id, '_user_IP', $post_users ); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Send to JS |
| 79 | $like_count = ++$count; |
| 80 | $response['status'] = 'liked'; |
| 81 | |
| 82 | // Unlike the Post |
| 83 | } else { |
| 84 | // Logged-in User |
| 85 | if ( is_user_logged_in() ) { |
| 86 | $user_id = get_current_user_id(); |
| 87 | $post_users = $this->get_user_likes( $user_id, $post_id ); |
| 88 | |
| 89 | // Get Like Count |
| 90 | $user_like_count = get_user_option( '_user_like_count', $user_id ); |
| 91 | $user_like_count = ( isset( $user_like_count ) && is_numeric( $user_like_count ) ) ? $user_like_count : 0; |
| 92 | |
| 93 | // Update Like Count |
| 94 | if ( $user_like_count > 0 ) { |
| 95 | update_user_option( $user_id, '_user_like_count', --$user_like_count ); |
| 96 | } |
| 97 | |
| 98 | // Update Post |
| 99 | if ( $post_users ) { |
| 100 | $uid_key = array_search( $user_id, $post_users ); |
| 101 | unset( $post_users[$uid_key] ); |
| 102 | update_post_meta( $post_id, '_user_liked', $post_users ); |
| 103 | } |
| 104 | |
| 105 | // Anonymous User |
| 106 | } else { |
| 107 | $post_users = $this->get_IP_likes( $this->get_IP(), $post_id ); |
| 108 | |
| 109 | // Update Post |
| 110 | if ( $post_users ) { |
| 111 | |
| 112 | unset( $post_users[ array_search( $this->get_IP(), $post_users ) ] ); |
| 113 | update_post_meta( $post_id, '_user_IP', $post_users ); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // Send to JS |
| 118 | $like_count = ( $count > 0 ) ? --$count : 0; |
| 119 | $response['status'] = 'unliked'; |
| 120 | } |
| 121 | |
| 122 | // Update Post |
| 123 | update_post_meta( $post_id, '_post_like_count', $like_count ); |
| 124 | update_post_meta( $post_id, '_post_like_modified', date( 'Y-m-d H:i:s' ) ); |
| 125 | |
| 126 | // Send to JS |
| 127 | $response['count'] = $this->get_like_count( $like_count ); |
| 128 | |
| 129 | // JavaScript Disabled |
| 130 | if ( $js_disabled == true ) { |
| 131 | wp_redirect( get_permalink( $post_id ) ); |
| 132 | exit(); |
| 133 | } else { |
| 134 | wp_send_json( $response ); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | ** Get Button |
| 141 | */ |
| 142 | public function get_button( $post_id, $settings ) { |
| 143 | $nonce = wp_create_nonce( 'wpr-post-likes-nonce' ); // Security |
| 144 | $like_count = get_post_meta( $post_id, '_post_like_count', true ); |
| 145 | $like_count = ( isset( $like_count ) && is_numeric( $like_count ) ) ? $like_count : 0; |
| 146 | $default_text_class = ''; |
| 147 | $button_text = $settings['element_like_text']; |
| 148 | |
| 149 | // Already Liked |
| 150 | if ( $this->already_liked( $post_id ) ) { |
| 151 | $title = esc_html__( 'Like', 'wpr-addons' ); |
| 152 | $liked_class = esc_attr( ' wpr-already-liked' ); |
| 153 | $icon_class = str_replace( 'far', 'fas', $settings['element_like_icon'] ); |
| 154 | } else { |
| 155 | $title = esc_html__( 'Unlike', 'wpr-addons' ); |
| 156 | $liked_class = ''; |
| 157 | $icon_class = $settings['element_like_icon']; |
| 158 | } |
| 159 | |
| 160 | // Default Text |
| 161 | if ( '' === $settings['element_like_text'] ) { |
| 162 | $default_text_class = ' wpr-likes-no-default'; |
| 163 | } |
| 164 | |
| 165 | // Zero Likes Class |
| 166 | if ( 0 == $like_count ) { |
| 167 | $default_text_class .= ' wpr-likes-zero'; |
| 168 | } |
| 169 | |
| 170 | // Button Attributes |
| 171 | $attributes = 'href="'. esc_url(admin_url( 'admin-ajax.php?action=wpr_likes_init&post_id='. $post_id .'&nonce='. $nonce )) .'"'; |
| 172 | $attributes .= ' class="wpr-post-like-button'. esc_attr($liked_class . $default_text_class) .'"'; |
| 173 | $attributes .= ' title="'. esc_attr($title) .'"'; |
| 174 | $attributes .= ' data-nonce="'. esc_attr($nonce) .'"'; |
| 175 | $attributes .= ' data-post-id="'. esc_attr($post_id) .'"'; |
| 176 | $attributes .= ' data-ajax="'. esc_url(admin_url( 'admin-ajax.php' )) .'"'; |
| 177 | $attributes .= ' data-icon="'. esc_attr($icon_class) .'"'; |
| 178 | $attributes .= ' data-text="'. esc_attr($button_text) .'"'; |
| 179 | |
| 180 | // Output |
| 181 | $output = '<a '. $attributes .'>'; |
| 182 | $output .= '<i class="'. esc_attr($icon_class) .'"></i>'; |
| 183 | $output .= $this->get_like_count( $like_count, $button_text ); |
| 184 | $output .= '</a>'; |
| 185 | |
| 186 | return $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | ** Utility: Already Liked |
| 191 | */ |
| 192 | public function already_liked( $post_id ) { |
| 193 | $post_users = NULL; |
| 194 | $user_id = NULL; |
| 195 | |
| 196 | // Logged-in User |
| 197 | if ( is_user_logged_in() ) { |
| 198 | $user_id = get_current_user_id(); |
| 199 | $post_meta_users = get_post_meta( $post_id, '_user_liked' ); |
| 200 | |
| 201 | if ( count( $post_meta_users ) != 0 ) { |
| 202 | $post_users = $post_meta_users[0]; |
| 203 | } |
| 204 | |
| 205 | // Anonymous User |
| 206 | } else { |
| 207 | $user_id = $this->get_IP(); |
| 208 | $post_meta_users = get_post_meta( $post_id, '_user_IP' ); |
| 209 | |
| 210 | if ( count( $post_meta_users ) != 0 ) { |
| 211 | $post_users = $post_meta_users[0]; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | if ( is_array( $post_users ) && in_array( $user_id, $post_users ) ) { |
| 216 | return true; |
| 217 | } else { |
| 218 | return false; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | |
| 223 | /** |
| 224 | ** Utility: Get User Likes |
| 225 | */ |
| 226 | public function get_user_likes( $user_id, $post_id ) { |
| 227 | $post_users = ''; |
| 228 | $post_meta_users = get_post_meta( $post_id, '_user_liked' ); |
| 229 | |
| 230 | if ( count( $post_meta_users ) != 0 ) { |
| 231 | $post_users = $post_meta_users[0]; |
| 232 | } |
| 233 | |
| 234 | if ( !is_array( $post_users ) ) { |
| 235 | $post_users = array(); |
| 236 | } |
| 237 | |
| 238 | if ( !in_array( $user_id, $post_users ) ) { |
| 239 | $post_users['user-'. $user_id] = $user_id; |
| 240 | } |
| 241 | |
| 242 | return $post_users; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | ** Utility: Get IP Likes |
| 247 | */ |
| 248 | public function get_IP_likes( $user_ip, $post_id ) { |
| 249 | $post_users = ''; |
| 250 | $post_meta_users = get_post_meta( $post_id, '_user_IP' ); |
| 251 | |
| 252 | // Retrieve post information |
| 253 | if ( count( $post_meta_users ) != 0 ) { |
| 254 | $post_users = $post_meta_users[0]; |
| 255 | } |
| 256 | |
| 257 | if ( !is_array( $post_users ) ) { |
| 258 | $post_users = array(); |
| 259 | } |
| 260 | |
| 261 | if ( !in_array( $user_ip, $post_users ) ) { |
| 262 | $post_users['ip-'. $user_ip] = $user_ip; |
| 263 | } |
| 264 | |
| 265 | return $post_users; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | ** Utility: Get IP |
| 270 | */ |
| 271 | public function get_IP() { |
| 272 | if ( isset( $_SERVER['HTTP_CLIENT_IP'] ) && ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { |
| 273 | $ip = sanitize_text_field(wp_unslash($_SERVER['HTTP_CLIENT_IP'])); |
| 274 | } else { |
| 275 | $ip = ( isset( $_SERVER['REMOTE_ADDR'] ) ) ? sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])) : '0.0.0.0'; |
| 276 | } |
| 277 | |
| 278 | $ip = filter_var( $ip, FILTER_VALIDATE_IP ); |
| 279 | $ip = ( $ip === false ) ? '0.0.0.0' : $ip; |
| 280 | |
| 281 | return $ip; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | ** Utility: Format Likes Number |
| 286 | */ |
| 287 | public function get_format_count( $number ) { |
| 288 | $precision = 2; |
| 289 | |
| 290 | if ( $number >= 1000 && $number < 1000000 ) { |
| 291 | $formatted = number_format( $number/1000, $precision ).'K'; |
| 292 | } elseif ( $number >= 1000000 && $number < 1000000000 ) { |
| 293 | $formatted = number_format( $number/1000000, $precision ).'M'; |
| 294 | } elseif ( $number >= 1000000000 ) { |
| 295 | $formatted = number_format( $number/1000000000, $precision ).'B'; |
| 296 | } else { |
| 297 | $formatted = $number; // Number is less than 1000 |
| 298 | } |
| 299 | |
| 300 | $formatted = str_replace( '.00', '', $formatted ); |
| 301 | |
| 302 | return $formatted; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | ** Utility: Get Likes Count |
| 307 | */ |
| 308 | public function get_like_count( $like_count, $like_text = '' ) { |
| 309 | if ( is_numeric( $like_count ) && $like_count > 0 ) { |
| 310 | $number = $this->get_format_count( $like_count ); |
| 311 | } else { |
| 312 | $number = $like_text; |
| 313 | } |
| 314 | |
| 315 | return '<span class="wpr-post-like-count">'. esc_html($number) .'</span>'; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | new WPR_Post_Likes(); |