ap_prefetch_recent_activities( array $ids, string $col = 'q_id' )

Description #

Prefetch activities of posts.

Parameters #

  • $ids
    array (Required) Array of post ids.
  • $col
    string (Optional) Column. Default value: 'q_id'

Changelog #

VersionDescription
4.1.2Introduced.

Source #

File: includes/activity.php

function ap_prefetch_recent_activities( $ids, $col = 'q_id' ) {
	global $wpdb;

	$ids_string = esc_sql( sanitize_comma_delimited( $ids ) );
	$col        = 'q_id' === $col ? 'q_id' : 'a_id';

	if ( empty( $ids_string ) ) {
		return;
	}

	$q_where = '';

	if ( 'q_id' === $col && is_question() ) {
		$q_where = " AND (activity_a_id = 0 OR activity_action IN('new_a', 'unselected','selected') )";
	}

	$query = "SELECT t1.* FROM {$wpdb->ap_activity} t1 NATURAL JOIN (SELECT max(activity_date) AS activity_date FROM {$wpdb->ap_activity} WHERE activity_{$col} IN({$ids_string})$q_where GROUP BY activity_{$col}) t2 ORDER BY t2.activity_date";

	$activity = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB

	foreach ( $activity as $a ) {
		$a = ap_activity_parse( $a );
	}

	return $activity;
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment