user_stats.author-pane.inc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * @file
  4. * Provides a preprocess function for the author pane used by
  5. * Advanced Forum and Advanced Profile Kit.
  6. */
  7. /**
  8. * Implements hook_preprocess_author_pane().
  9. */
  10. function user_stats_preprocess_author_pane(&$variables) {
  11. if (function_exists('author_pane_api') && author_pane_api() == "2") {
  12. // Check if this preprocess needs to be run given who's calling it.
  13. if (!author_pane_run_preprocess('user_stats', $variables['caller'])) {
  14. return;
  15. }
  16. }
  17. $account_id = $variables['account']->uid;
  18. if ($account_id != 0) {
  19. static $cached_stats = array();
  20. if (isset($cached_stats[$account_id])) {
  21. // Pull the values from the cache
  22. if ($cached_stats[$account_id]['posts'] !== FALSE) {
  23. // We don't want the variable to be created if the user doesn't have
  24. // access to see them to avoid printing the label.
  25. $variables['user_stats_posts'] = $cached_stats[$account_id]['posts'];
  26. }
  27. if ($cached_stats[$account_id]['ip'] !== FALSE) {
  28. $variables['user_stats_ip'] = $cached_stats[$account_id]['ip'];
  29. }
  30. }
  31. else {
  32. // No cached values for this user. Fetch them.
  33. $posts = user_stats_get_stats('post_count', $account_id);
  34. if ($posts !== FALSE) {
  35. $variables['user_stats_posts'] = $posts;
  36. }
  37. $ip = user_stats_get_stats('ip_address', $account_id);
  38. if ($ip !== FALSE) {
  39. $variables['user_stats_ip'] = $ip;
  40. }
  41. // Write to the cache variable. We do this even if it's FALSE because
  42. // we check for that before using it.
  43. $cached_stats[$account_id]['posts'] = $posts;
  44. $cached_stats[$account_id]['ip'] = $ip;
  45. }
  46. }
  47. }
  48. /**
  49. * Implements hook_author_pane_allow_preprocess_disable().
  50. */
  51. function user_stats_author_pane_allow_preprocess_disable() {
  52. return array('user_stats' => 'User Stats');
  53. }