query_ideas( $paged, $type_filter, $source_filter ); $rows = $result['rows']; $total = $result['total']; $total_pages = (int) ceil( $total / self::PER_PAGE ); $type_counts = $this->get_type_counts(); include PRAUTOBLOGGER_PLUGIN_DIR . 'templates/admin/ideas-browser.php'; } /** * Query analysis results with optional filtering and pagination. * * Side effects: database SELECT. * * @param int $paged Page number (1-based). * @param string $type Filter by analysis_type (empty = all). * @param string $source Filter: 'llm_research' or 'reddit' based on source_ids content. * @return array{rows: array[], total: int} */ private function query_ideas( int $paged, string $type, string $source ): array { global $wpdb; $table = $wpdb->prefix . 'prautoblogger_analysis_results'; $where = []; $params = []; if ( '' !== $type ) { $where[] = 'analysis_type = %s'; $params[] = $type; } $where_sql = empty( $where ) ? '' : 'WHERE ' . implode( ' AND ', $where ); $offset = ( $paged - 1 ) * self::PER_PAGE; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $total = (int) $wpdb->get_var( empty( $params ) ? "SELECT COUNT(*) FROM {$table} {$where_sql}" : $wpdb->prepare( "SELECT COUNT(*) FROM {$table} {$where_sql}", ...$params ) ); $order_sql = 'ORDER BY analyzed_at DESC, relevance_score DESC'; $limit_sql = sprintf( 'LIMIT %d OFFSET %d', self::PER_PAGE, $offset ); $full_sql = "SELECT * FROM {$table} {$where_sql} {$order_sql} {$limit_sql}"; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $rows = empty( $params ) ? $wpdb->get_results( $full_sql, ARRAY_A ) : $wpdb->get_results( $wpdb->prepare( $full_sql, ...$params ), ARRAY_A ); return [ 'rows' => $rows ?: [], 'total' => $total, ]; } /** * Get counts per analysis_type for the filter badges. * * Side effects: database SELECT. * * @return array Map of analysis_type => count. */ private function get_type_counts(): array { global $wpdb; $table = $wpdb->prefix . 'prautoblogger_analysis_results'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $rows = $wpdb->get_results( "SELECT analysis_type, COUNT(*) AS cnt FROM {$table} GROUP BY analysis_type ORDER BY cnt DESC", ARRAY_A ); $counts = []; foreach ( $rows ?: [] as $row ) { $counts[ $row['analysis_type'] ] = (int) $row['cnt']; } return $counts; } }