xmlsitemap_user.module 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Implements hook_entity_info_alter().
  4. */
  5. function xmlsitemap_user_entity_info_alter(&$entity_info) {
  6. $entity_info['user']['bundle label'] = t('User');
  7. $entity_info['user']['xmlsitemap'] = array(
  8. 'process callback' => 'xmlsitemap_user_xmlsitemap_process_user_links',
  9. );
  10. }
  11. /**
  12. * Implements hook_cron().
  13. *
  14. * Process old users not found in the {xmlsitemap} table.
  15. */
  16. function xmlsitemap_user_cron() {
  17. xmlsitemap_user_xmlsitemap_index_links(xmlsitemap_var('batch_limit'));
  18. }
  19. /**
  20. * Implements hook_xmlsitemap_index_links().
  21. */
  22. function xmlsitemap_user_xmlsitemap_index_links($limit) {
  23. $uids = db_query_range("SELECT u.uid FROM {users} u LEFT JOIN {xmlsitemap} x ON x.type = 'user' AND u.uid = x.id WHERE x.id IS NULL AND u.uid > 0 ORDER BY u.uid DESC", 0, $limit)->fetchCol();
  24. xmlsitemap_user_xmlsitemap_process_user_links($uids);
  25. }
  26. /**
  27. * Process user sitemap links.
  28. *
  29. * @param $uids
  30. * An array of user IDs.
  31. */
  32. function xmlsitemap_user_xmlsitemap_process_user_links(array $uids) {
  33. $accounts = user_load_multiple($uids);
  34. foreach ($accounts as $account) {
  35. $link = xmlsitemap_user_create_link($account);
  36. xmlsitemap_link_save($link);
  37. }
  38. }
  39. /**
  40. * Implements hook_user_presave().
  41. */
  42. function xmlsitemap_user_user_presave(&$edit, $account, $category) {
  43. if (!empty($account->uid)) {
  44. $link = xmlsitemap_user_create_link($account);
  45. if (isset($edit['xmlsitemap'])) {
  46. $link = $edit['xmlsitemap'] + $link;
  47. unset($edit['xmlsitemap']);
  48. }
  49. xmlsitemap_link_save($link);
  50. }
  51. }
  52. /**
  53. * Implements hook_user_insert().
  54. */
  55. function xmlsitemap_user_user_insert(&$edit, $account, $category) {
  56. $link = xmlsitemap_user_create_link($account);
  57. xmlsitemap_link_save($link);
  58. }
  59. /**
  60. * Implements hook_user_update().
  61. */
  62. function xmlsitemap_user_user_update(&$edit, $account, $category) {
  63. $link = xmlsitemap_user_create_link($account);
  64. xmlsitemap_link_save($link);
  65. }
  66. /**
  67. * Implements hook_user_delete().
  68. */
  69. function xmlsitemap_user_user_delete($account) {
  70. xmlsitemap_link_delete('user', $account->uid);
  71. }
  72. /**
  73. * Implements hook_field_extra_fields().
  74. */
  75. function xmlsitemap_user_field_extra_fields() {
  76. $extras['user']['user']['form']['xmlsitemap'] = array(
  77. 'label' => t('XML sitemap'),
  78. 'description' => t('XML sitemap module element'),
  79. 'weight' => 30,
  80. );
  81. return $extras;
  82. }
  83. /**
  84. * Implements hook_form_FORM_ID_alter().
  85. *
  86. * @see user_admin_settings()
  87. * @see xmlsitemap_add_link_bundle_settings()
  88. */
  89. function xmlsitemap_user_form_user_profile_form_alter(&$form, $form_state) {
  90. // Add the link options.
  91. module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
  92. xmlsitemap_add_form_link_options($form, 'user', 'user', $form['#user']->uid);
  93. }
  94. /**
  95. * Implements hook_form_FORM_ID_alter().
  96. *
  97. * @see user_admin_settings()
  98. * @see xmlsitemap_add_link_bundle_settings()
  99. */
  100. function xmlsitemap_user_form_user_admin_settings_alter(&$form, $form_state) {
  101. module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
  102. xmlsitemap_add_link_bundle_settings($form, $form_state, 'user', 'user');
  103. $form['email_title'] += array('#weight' => 10);
  104. $form['email'] += array('#weight' => 10);
  105. }
  106. /**
  107. * Create a sitemap link from a user.
  108. *
  109. * The link will be saved as $account->xmlsitemap.
  110. *
  111. * @param $account
  112. * A user object.
  113. */
  114. function xmlsitemap_user_create_link(stdClass &$account) {
  115. if (!isset($account->xmlsitemap)) {
  116. $account->xmlsitemap = array();
  117. if ($account->uid && $link = xmlsitemap_link_load('user', $account->uid)) {
  118. $account->xmlsitemap = $link;
  119. }
  120. }
  121. $settings = xmlsitemap_link_bundle_load('user', 'user');
  122. $uri = entity_uri('user', $account);
  123. $account->xmlsitemap += array(
  124. 'type' => 'user',
  125. 'id' => $account->uid,
  126. 'subtype' => 'user',
  127. 'status' => $settings['status'],
  128. 'status_default' => $settings['status'],
  129. 'status_override' => 0,
  130. 'priority' => $settings['priority'],
  131. 'priority_default' => $settings['priority'],
  132. 'priority_override' => 0,
  133. );
  134. // The following values must always be checked because they are volatile.
  135. $account->xmlsitemap['loc'] = $uri['path'];
  136. $account->xmlsitemap['access'] = $account->uid && $account->status;
  137. $account->xmlsitemap['language'] = !empty($account->language) ? $account->language : LANGUAGE_NONE;
  138. return $account->xmlsitemap;
  139. }
  140. /**
  141. * Implementation of hook_variables().
  142. */
  143. function xmlsitemap_user_variables() {
  144. $defaults = array();
  145. return $defaults;
  146. }