/** * Cached API endpoint for top videos */ function xx_api_top_like_cached($data) { global $xx_redis_cache; $type = $_GET['type'] ?? 'date'; $offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0; $limit = isset($_GET['limit']) ? intval($_GET['limit']) : 20; // Try to get from cache first $cached_result = xx_get_cached_top_videos($type, $offset, $limit); if ($cached_result !== false) { wp_send_json_success($cached_result['html']); return; } // Fallback to original function if cache fails $result = xx_generate_top_videos_data($type, $offset, $limit); wp_send_json_success($result['html']); } /** * Original API function (kept for fallback) */ function xx_api_top_like($data) { $result = xx_generate_top_videos_data($_GET['type'] ?? 'date', $_GET['offset'] ?? 0, $_GET['limit'] ?? 20); wp_send_json_success($result['html']); } /** * Generate top videos data (extracted for reuse) */ function xx_generate_top_videos_data($type, $offset = 0, $limit = 20) { global $wpdb, $xx_theme_options; $link = '/phim-sex-hay'; $table = $wpdb->prefix . 'xx_ulike'; $html = ""; $post_ids = []; switch($type) { case 'date': $date = date('Y-m-d'); $results = $wpdb->get_results($wpdb->prepare( "SELECT post_id FROM $table WHERE date_time = %s ORDER BY like_count DESC LIMIT %d OFFSET %d", $date, $limit, $offset ), ARRAY_N); $post_ids = wp_list_pluck($results, 0); break; case 'week': $results = $wpdb->get_results($wpdb->prepare( "SELECT post_id, SUM(like_count) as total_likes FROM $table WHERE date_time >= DATE_SUB(NOW(), INTERVAL 1 WEEK) GROUP BY post_id ORDER BY total_likes DESC LIMIT %d OFFSET %d", $limit, $offset )); $post_ids = wp_list_pluck($results, 'post_id'); break; case 'month': $start_date = date('Y-m-d', strtotime('-1 month')); $end_date = date('Y-m-d', strtotime('now')); $results = $wpdb->get_results($wpdb->prepare( "SELECT post_id, SUM(like_count) as total_likes FROM $table WHERE date_time >= %s AND date_time <= %s GROUP BY post_id ORDER BY total_likes DESC LIMIT %d OFFSET %d", $start_date, $end_date, $limit, $offset )); $post_ids = wp_list_pluck($results, 'post_id'); break; case 'all': $results = $wpdb->get_results($wpdb->prepare( "SELECT post_id, SUM(like_count) as total_likes FROM $table GROUP BY post_id ORDER BY total_likes DESC LIMIT %d OFFSET %d", $limit, $offset )); $post_ids = wp_list_pluck($results, 'post_id'); break; default: $post_ids = []; } if (!empty($post_ids)) { $args = array( 'post_type' => 'post', 'post__in' => $post_ids, 'orderby' => 'post__in', 'posts_per_page' => $limit ); $query = new WP_Query($args); if ($query && $query->have_posts()) { while ($query->have_posts()) { $query->the_post(); $like_count = 0; switch($type) { case 'date': $like_count = $wpdb->get_var($wpdb->prepare( "SELECT like_count FROM $table WHERE date_time = %s AND post_id = %d", date('Y-m-d'), get_the_ID() )); break; case 'week': $like_count = $wpdb->get_var($wpdb->prepare( "SELECT SUM(like_count) FROM $table WHERE post_id = %d AND date_time >= DATE_SUB(NOW(), INTERVAL 1 WEEK)", get_the_ID() )); break; case 'month': $like_count = $wpdb->get_var($wpdb->prepare( "SELECT SUM(like_count) FROM $table WHERE post_id = %d AND date_time >= %s AND date_time <= %s", get_the_ID(), $start_date, $end_date )); break; case 'all': $like_count = $wpdb->get_var($wpdb->prepare( "SELECT SUM(like_count) FROM $table WHERE post_id = %d", get_the_ID() )); break; } $html .= '
'; if (has_post_thumbnail()) { $thumbnail_url = get_transient('new_thumbnail_url_' . get_the_ID()); if (!$thumbnail_url) { $thumbnail_url = get_the_post_thumbnail_url(null, 'custom-video-thumbnail'); set_transient('new_thumbnail_url_' . get_the_ID(), $thumbnail_url, HOUR_IN_SECONDS); } $html .= ''; } $html .= ''; $html .= '
' . get_the_title() . '
'; } wp_reset_postdata(); } } return [ 'html' => $html, 'post_ids' => $post_ids, 'type' => $type, 'offset' => $offset, 'limit' => $limit, 'generated_at' => time() ]; } // Register both endpoints add_action('rest_api_init', function () { // Original endpoint register_rest_route('xx', '/top-like/', array( 'methods' => 'GET', 'callback' => 'xx_api_top_like', )); // Cached endpoint register_rest_route('xx', '/top-like-cached/', array( 'methods' => 'GET', 'callback' => 'xx_api_top_like_cached', )); // Worker status endpoint register_rest_route('xx', '/worker-status/', array( 'methods' => 'GET', 'callback' => 'xx_worker_status_callback', 'permission_callback' => '__return_true' )); // Like/Dislike REST API endpoints register_rest_route('xx', '/like/', array( 'methods' => 'POST', 'callback' => 'xx_api_like', 'permission_callback' => '__return_true' )); register_rest_route('xx', '/dislike/', array( 'methods' => 'POST', 'callback' => 'xx_api_dislike', 'permission_callback' => '__return_true' )); }); // Trigger cache invalidation when like/dislike happens add_action('wp_ajax_post-ulike', function() { // Invalidate cache after like/dislike action xx_invalidate_top_videos_cache(); }); add_action('wp_ajax_nopriv_post-ulike', function() { // Invalidate cache after like/dislike action xx_invalidate_top_videos_cache(); }); function xx_worker_status_callback() { global $xx_worker; $queue_status = xx_get_worker_status(); $cache_keys = get_option('xx_top_videos_cache_keys', []); return array( 'success' => true, 'queue' => array( 'total_jobs' => $queue_status['total_jobs'], 'job_types' => $queue_status['job_types'], 'oldest_job' => $queue_status['oldest_job'] ? date('Y-m-d H:i:s', $queue_status['oldest_job']) : null ), 'cache' => array( 'total_keys' => count($cache_keys), 'last_warm' => get_option('xx_worker_last_warm', 0), 'last_warm_time' => get_option('xx_worker_last_warm', 0) ? date('Y-m-d H:i:s', get_option('xx_worker_last_warm', 0)) : null ), 'timestamp' => current_time('mysql') ); } /** * REST API Like endpoint */ function xx_api_like($request) { global $wpdb; $post_id = $request->get_param('post_id'); if (!$post_id) { return new WP_Error('missing_post_id', 'Post ID is required', array('status' => 400)); } $table = $wpdb->prefix . 'xx_ulike'; $date = date('Y-m-d'); // Check if record exists xx_check_ulike_record($post_id, $date); // Get current like count $like_count = $wpdb->get_var($wpdb->prepare( "SELECT like_count FROM $table WHERE date_time = %s AND post_id = %d", $date, $post_id )); if ($like_count >= 0) { $like_count++; $wpdb->update( $table, array('like_count' => $like_count), array('date_time' => $date, 'post_id' => $post_id), array('%d'), array('%s', '%d') ); // Get totals and calculate percentage $results = $wpdb->get_results($wpdb->prepare( "SELECT SUM(like_count) AS total_likes, SUM(dislike_count) AS total_dislikes FROM $table WHERE post_id = %d", $post_id )); $total_likes = $results[0]->total_likes; $total_dislikes = $results[0]->total_dislikes; $percent = $total_likes > 0 ? round($total_likes / ($total_likes + $total_dislikes) * 100) : 0; // Invalidate cache xx_invalidate_top_videos_cache(); return array( 'success' => true, 'total_like' => $total_likes, 'total_dislike' => $total_dislikes, 'percent' => $percent ); } return new WP_Error('like_failed', 'Failed to update like count', array('status' => 500)); } /** * REST API Dislike endpoint */ function xx_api_dislike($request) { global $wpdb; $post_id = $request->get_param('post_id'); if (!$post_id) { return new WP_Error('missing_post_id', 'Post ID is required', array('status' => 400)); } $table = $wpdb->prefix . 'xx_ulike'; $date = date('Y-m-d'); // Check if record exists xx_check_ulike_record($post_id, $date); // Get current dislike count $dislike_count = $wpdb->get_var($wpdb->prepare( "SELECT dislike_count FROM $table WHERE date_time = %s AND post_id = %d", $date, $post_id )); if ($dislike_count >= 0) { $dislike_count++; $wpdb->update( $table, array('dislike_count' => $dislike_count), array('date_time' => $date, 'post_id' => $post_id), array('%d'), array('%s', '%d') ); // Get totals and calculate percentage $results = $wpdb->get_results($wpdb->prepare( "SELECT SUM(like_count) AS total_likes, SUM(dislike_count) AS total_dislikes FROM $table WHERE post_id = %d", $post_id )); $total_likes = $results[0]->total_likes; $total_dislikes = $results[0]->total_dislikes; $percent = $total_likes > 0 ? round($total_likes / ($total_likes + $total_dislikes) * 100) : 0; // Invalidate cache xx_invalidate_top_videos_cache(); return array( 'success' => true, 'total_like' => $total_likes, 'total_dislike' => $total_dislikes, 'percent' => $percent ); } return new WP_Error('dislike_failed', 'Failed to update dislike count', array('status' => 500)); } /** * Helper function to check/create ulike record */ function xx_check_ulike_record($post_id, $date) { global $wpdb; $table = $wpdb->prefix . 'xx_ulike'; $exists = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM $table WHERE date_time = %s AND post_id = %d", $date, $post_id )); if (!$exists) { $wpdb->insert( $table, array( 'post_id' => $post_id, 'like_count' => 0, 'dislike_count' => 0, 'date_time' => $date ), array('%d', '%d', '%d', '%s') ); } } ?>