prefix . 'cosmopet_likes'; $wpdb->show_errors(); if (!is_user_logged_in()) { wp_send_json_error('Необходимо авторизоваться'); die(); } $comment_id = isset($_POST['comment_id']) ? intval($_POST['comment_id']) : 0; $user_id = get_current_user_id(); if ($comment_id) { $comment_exists = get_comment($comment_id); if (!$comment_exists) { echo '0'; die(); } $existing_like = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM $table_name WHERE comment_id = %d AND user_id = %d", $comment_id, $user_id )); if (!$existing_like) { $result = $wpdb->insert( $table_name, array( 'user_id' => $user_id, 'comment_id' => $comment_id, 'date_added' => current_time('mysql') ), array('%d', '%d', '%s') ); } else { $result = $wpdb->delete( $table_name, array( 'user_id' => $user_id, 'comment_id' => $comment_id ), array('%d', '%d') ); } $likes = get_comment_likes_count($comment_id); wp_send_json(array( 'count' => $likes, 'is_liked' => !$existing_like )); } else { wp_send_json(array('count' => 0, 'is_liked' => false)); } die(); } add_action('wp_ajax_add_comment_like', 'add_comment_like'); function add_post_like() { global $wpdb; $table_name = $wpdb->prefix . 'cosmopet_likes'; if (!is_user_logged_in()) { wp_send_json_error('Необходимо авторизоваться'); die(); } $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; $user_id = get_current_user_id(); if ($post_id) { $existing_like = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM $table_name WHERE post_id = %d AND user_id = %d", $post_id, $user_id )); if (!$existing_like) { $wpdb->insert( $table_name, array( 'user_id' => $user_id, 'post_id' => $post_id, 'date_added' => current_time('mysql') ), array('%d', '%d', '%s') ); } else { $wpdb->delete( $table_name, array( 'user_id' => $user_id, 'post_id' => $post_id ), array('%d', '%d') ); } $likes = get_post_likes_count($post_id); wp_send_json(array( 'count' => $likes, 'is_liked' => !$existing_like )); } die(); } add_action('wp_ajax_add_post_like', 'add_post_like'); function check_user_likes() { global $wpdb; $table_name = $wpdb->prefix . 'cosmopet_likes'; if (!is_user_logged_in()) { wp_send_json_error('Необходимо авторизоваться'); die(); } $user_id = get_current_user_id(); $liked_posts = $wpdb->get_col($wpdb->prepare( "SELECT post_id FROM $table_name WHERE user_id = %d AND post_id > 0", $user_id )); $liked_comments = $wpdb->get_col($wpdb->prepare( "SELECT comment_id FROM $table_name WHERE user_id = %d AND comment_id > 0", $user_id )); $response = array( 'posts' => $liked_posts, 'comments' => $liked_comments ); echo json_encode($response); die(); } add_action('wp_ajax_check_user_likes', 'check_user_likes'); function get_post_likes_count($post_id) { global $wpdb; $table_name = $wpdb->prefix . 'cosmopet_likes'; $count = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM $table_name WHERE post_id = %d", $post_id )); return $count ? $count : 0; } function get_comment_likes_count($comment_id) { global $wpdb; $table_name = $wpdb->prefix . 'cosmopet_likes'; $count = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM $table_name WHERE comment_id = %d", $comment_id )); return $count ? $count : 0; } function is_user_liked_post($post_id) { if (!is_user_logged_in()) { return false; } global $wpdb; $table_name = $wpdb->prefix . 'cosmopet_likes'; $user_id = get_current_user_id(); $result = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM $table_name WHERE post_id = %d AND user_id = %d", $post_id, $user_id )); return $result > 0; } function is_user_liked_comment($comment_id) { if (!is_user_logged_in()) { return false; } global $wpdb; $table_name = $wpdb->prefix . 'cosmopet_likes'; $user_id = get_current_user_id(); $result = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM $table_name WHERE comment_id = %d AND user_id = %d", $comment_id, $user_id )); return $result > 0; } add_filter('comment_form_logged_in', '__return_empty_string'); // Создание таблицы function create_likes_table() { global $wpdb; $table_name = $wpdb->prefix . 'cosmopet_likes'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS $table_name ( id bigint(20) NOT NULL AUTO_INCREMENT, user_id bigint(20) NOT NULL, post_id bigint(20) DEFAULT '0', comment_id bigint(20) DEFAULT '0', date_added datetime DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY post_id (post_id), KEY comment_id (comment_id), KEY user_id (user_id), UNIQUE KEY user_post (user_id, post_id, comment_id) ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } add_action('after_switch_theme', 'create_likes_table');