'GET', 'callback' => [__CLASS__, 'get_histories'], 'permission_callback' => function () { return current_user_can('manage_options'); } ] ]); } public static function get_histories(WP_REST_Request $request) { global $wpdb; $histories_table = $wpdb->prefix . 'histories'; $historymeta_table = $wpdb->prefix . 'historymeta'; $cache_key = 'histories_list'; // Try to get from cache first $histories = wp_cache_get($cache_key, 'plac_histories_cache'); if (false === $histories) { // Fetch history records with prepared statement $query = $wpdb->prepare( "SELECT * FROM {$histories_table} ORDER BY id DESC LIMIT %d OFFSET %d", 9, 0 ); $histories = $wpdb->get_results($query, ARRAY_A); if (!empty($histories)) { foreach ($histories as $key => $value) { $meta_cache_key = 'history_meta_' . $value['id']; $results = wp_cache_get($meta_cache_key, 'plac_histories_cache'); if (false === $results) { // Fetch history meta with proper prepared statement $meta_query = $wpdb->prepare( "SELECT * FROM {$historymeta_table} WHERE history_id = %d", $value['id'] ); $results = $wpdb->get_results($meta_query, ARRAY_A); wp_cache_set($meta_cache_key, $results, 'plac_histories_cache', 3600); } if (!empty($results)) { foreach ($results as $meta) { $histories[$key][$meta['history_key']] = $meta['history_value']; // Fetch user data if 'user_id' key exists if ($meta['history_key'] === 'user_id') { $user_id = intval($meta['history_value']); // Sanitize user ID $user_cache_key = 'user_' . $user_id; $user = wp_cache_get($user_cache_key, 'plac_histories_cache'); if (false === $user) { $user = get_userdata($user_id); wp_cache_set($user_cache_key, $user, 'plac_histories_cache', 3600); } if ($user) { $histories[$key]['display_name'] = esc_html($user->display_name); } } } } } } // Store in cache wp_cache_set($cache_key, $histories, 'plac_histories_cache', 3600); } return new WP_REST_Response($histories, 200); } }