Your problem is mostly just a change in PHP logic, not really WordPress. Anyway, we are here to help, and that would be off-topic on other sites too, so …
-
It is unfortunate enough that you have to access a global variable in order to use the current database connection. Make others (you in 6 months!) life easier, and import global variables always at the top of a function body.
If you are searching later for all accesses to that variables, you will thank your past self for that. 🙂
In my code, I use mostly class methods as callbacks, and I pass the$wpdb
object as a dependency into the class constructor, so I have to touch that global variable just once, at the very top of my code hierarchy. -
When you are using almost the same query every time, look what the differences are, and make those either a variable, or unify them. In your case, you can just use the
$column_name
variable, because it is matching the database table column name and a whitelist of valid strings. Also, you can rename the resulting variable:SELECT x as foo FROM …
-
$item[ 'ID' ]
is unknown, it can be anything. Always prepare unknown values before you send them to the database.$wpdb
has methodprepare()
that works similar tosprintf()
. -
You don’t need to run
count()
on the query result. You get an object only on success, so the faster test againstis_object()
is good enough. -
Move that code to a separate function. It does too much.
Sample code for all of these points:
function answer_query( $column_name, $id )
{
global $wpdb;
$sql = "SELECT %s as result
FROM {$wpdb->prefix}rdp_participants_answers
WHERE user_id = %d ORDER BY id DESC LIMIT 1 ";
$query = $wpdb->get_row( $wpdb->prepare( $sql, $column_name, $id ) );
if ( is_object( $query ) )
return $query->result;
return 0;
}
And in your switch
statement, shorten the code to:
case 'answer':
case 'answer_time':
case 'ip_address':
return answer_query( $column_name, (int) $item['ID'] ) );