Lines: 83.82%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | /** | |
| 4 | * @file | |
| 5 | * Logs visitors for your site. | |
| 6 | */ | |
| 7 | ||
| 8 | use Drupal\Core\Entity\EntityInterface; | |
| 9 | use Drupal\Core\Form\FormStateInterface; | |
| 10 | use Drupal\Core\Hook\Attribute\LegacyHook; | |
| 11 | use Drupal\Core\Render\BubbleableMetadata; | |
| 12 | use Drupal\node\NodeInterface; | |
| 13 | use Drupal\visitors\Hook\CounterHooks; | |
| 14 | use Drupal\visitors\Hook\CronHooks; | |
| 15 | use Drupal\visitors\Hook\FormHooks; | |
| 16 | use Drupal\visitors\Hook\HelpHooks; | |
| 17 | use Drupal\visitors\Hook\PageAttachmentsHooks; | |
| 18 | use Drupal\visitors\Hook\SearchHooks; | |
| 19 | use Drupal\visitors\Hook\ThemeHooks; | |
| 20 | use Drupal\visitors\Hook\TokenHooks; | |
| 21 | use Drupal\visitors\Hook\UserHooks; | |
| 22 | use Drupal\visitors\Hook\ViewsHooks; | |
| 23 | ||
| 24 | /** | |
| 25 | * Returns a hook class, instantiating directly when unavailable. | |
| 26 | * | |
| 27 | * Unit tests typically register only the services they exercise directly and | |
| 28 | * omit the hook-class service. This helper builds the hook from those | |
| 29 | * services when the hook service itself is not registered. | |
| 30 | * | |
| 31 | * @param class-string<T> $class | |
| 32 | * The hook class to load. | |
| 33 | * | |
| 34 | * @return T | |
| 35 | * The hook class instance. | |
| 36 | * | |
| 37 | * @template T of object | |
| 38 | */ | |
| 39 | function _visitors_hook(string $class): object { | |
| 40 | 25 | if (\Drupal::hasService($class)) { |
| 41 | 3 | return \Drupal::service($class); |
| 42 | } | |
| 43 | ||
| 44 | // Unit tests register the services a hook uses and omit the hook service. | |
| 45 | 22 | $container = \Drupal::getContainer(); |
| 46 | 22 | return match ($class) { |
| 47 | 5 | CounterHooks::class => new CounterHooks( |
| 48 | 5 | $container->get('current_user'), |
| 49 | 5 | $container->get('config.factory'), |
| 50 | 5 | $container->get('visitors.counter'), |
| 51 | 5 | $container->get('string_translation'), |
| 52 | 5 | ), |
| 53 | 1 | CronHooks::class => new CronHooks( |
| 54 | 1 | $container->get('visitors.cron'), |
| 55 | 1 | ), |
| 56 | 2 | PageAttachmentsHooks::class => new PageAttachmentsHooks( |
| 57 | 2 | $container->get('visitors.page_attachments'), |
| 58 | 2 | ), |
| 59 | 0 | SearchHooks::class => new SearchHooks( |
| 60 | 0 | $container->get('config.factory'), |
| 61 | 0 | $container->get('state'), |
| 62 | 0 | $container->get('string_translation'), |
| 63 | 0 | $container->get('request_stack'), |
| 64 | 0 | $container->get('pager.manager'), |
| 65 | 0 | $container->get('visitors.site_search'), |
| 66 | 0 | ), |
| 67 | 3 | ThemeHooks::class => new ThemeHooks( |
| 68 | 3 | $container->get('request_stack'), |
| 69 | 3 | ), |
| 70 | 3 | TokenHooks::class => new TokenHooks( |
| 71 | 3 | $container->get('config.factory'), |
| 72 | 3 | $container->get('token'), |
| 73 | 3 | $container->get('visitors.counter'), |
| 74 | 3 | $container->get('date.formatter'), |
| 75 | 3 | $container->get('string_translation'), |
| 76 | 3 | ), |
| 77 | 6 | UserHooks::class => new UserHooks( |
| 78 | 6 | $container->get('config.factory'), |
| 79 | 6 | $container->get('user.data'), |
| 80 | 6 | $container->get('string_translation'), |
| 81 | 6 | ), |
| 82 | 1 | ViewsHooks::class => new ViewsHooks( |
| 83 | 1 | $container->get('config.factory'), |
| 84 | 1 | $container->get('entity_type.manager'), |
| 85 | 1 | $container->get('string_translation'), |
| 86 | 1 | ), |
| 87 | 22 | default => new $class(), |
| 88 | 22 | }; |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * Implements hook_help(). | |
| 93 | */ | |
| 94 | #[LegacyHook] | |
| 95 | function visitors_help($route_name, $route_match) { | |
| 96 | 1 | return _visitors_hook(HelpHooks::class)->help($route_name, $route_match); |
| 97 | } | |
| 98 | ||
| 99 | /** | |
| 100 | * Implements hook_cron(). | |
| 101 | */ | |
| 102 | #[LegacyHook] | |
| 103 | function visitors_cron(): void { | |
| 104 | 1 | _visitors_hook(CronHooks::class)->cron(); |
| 105 | } | |
| 106 | ||
| 107 | /** | |
| 108 | * Implements hook_page_attachments(). | |
| 109 | */ | |
| 110 | #[LegacyHook] | |
| 111 | function visitors_page_attachments(array &$page) { | |
| 112 | 1 | _visitors_hook(PageAttachmentsHooks::class)->pageAttachments($page); |
| 113 | } | |
| 114 | ||
| 115 | /** | |
| 116 | * Implements hook_page_bottom(). | |
| 117 | */ | |
| 118 | #[LegacyHook] | |
| 119 | function visitors_page_bottom(array &$page_bottom): void { | |
| 120 | 1 | _visitors_hook(PageAttachmentsHooks::class)->pageBottom($page_bottom); |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * Implements hook_form_FORM_ID_alter(). | |
| 125 | * | |
| 126 | * Allow users to decide if tracking code will be added to pages or not. | |
| 127 | */ | |
| 128 | #[LegacyHook] | |
| 129 | function visitors_form_user_form_alter(&$form, FormStateInterface $form_state) { | |
| 130 | 6 | _visitors_hook(UserHooks::class)->formUserFormAlter($form, $form_state); |
| 131 | } | |
| 132 | ||
| 133 | /** | |
| 134 | * Submit callback for user profile form to save the Visitor setting. | |
| 135 | */ | |
| 136 | function visitors_user_profile_form_submit($form, FormStateInterface $form_state) { | |
| 137 | 3 | if (!$form_state->hasValue('user_account_users')) { |
| 138 | 1 | return; |
| 139 | } | |
| 140 | /** @var \Drupal\user\AccountForm $user_form */ | |
| 141 | 2 | $user_form = $form_state->getFormObject(); |
| 142 | /** @var \Drupal\user\UserInterface $account */ | |
| 143 | 2 | $account = $user_form->getEntity(); |
| 144 | ||
| 145 | 2 | $value = (int) $form_state->getValue('user_account_users'); |
| 146 | 2 | \Drupal::service('user.data') |
| 147 | 2 | ->set('visitors', $account->id(), 'user_account_users', $value); |
| 148 | } | |
| 149 | ||
| 150 | /** | |
| 151 | * Implements hook_node_links_alter(). | |
| 152 | */ | |
| 153 | #[LegacyHook] | |
| 154 | function visitors_node_links_alter(array &$links, NodeInterface $entity, array &$context) { | |
| 155 | 3 | return _visitors_hook(CounterHooks::class)->nodeLinksAlter($links, $entity, $context); |
| 156 | } | |
| 157 | ||
| 158 | /** | |
| 159 | * Implements hook_entity_delete(). | |
| 160 | */ | |
| 161 | #[LegacyHook] | |
| 162 | function visitors_entity_delete(EntityInterface $entity) { | |
| 163 | 2 | _visitors_hook(CounterHooks::class)->entityDelete($entity); |
| 164 | } | |
| 165 | ||
| 166 | /** | |
| 167 | * Implements hook_ranking(). | |
| 168 | */ | |
| 169 | #[LegacyHook] | |
| 170 | function visitors_ranking() { | |
| 171 | 0 | return _visitors_hook(SearchHooks::class)->ranking(); |
| 172 | } | |
| 173 | ||
| 174 | /** | |
| 175 | * Implements hook_node_search_ranking(). | |
| 176 | */ | |
| 177 | #[LegacyHook] | |
| 178 | function visitors_node_search_ranking() { | |
| 179 | 0 | return _visitors_hook(SearchHooks::class)->ranking(); |
| 180 | } | |
| 181 | ||
| 182 | /** | |
| 183 | * Implements hook_views_data(). | |
| 184 | */ | |
| 185 | #[LegacyHook] | |
| 186 | function visitors_views_data() { | |
| 187 | 1 | return _visitors_hook(ViewsHooks::class)->viewsData(); |
| 188 | } | |
| 189 | ||
| 190 | /** | |
| 191 | * Implements hook_token_info(). | |
| 192 | */ | |
| 193 | #[LegacyHook] | |
| 194 | function visitors_token_info() { | |
| 195 | 1 | return _visitors_hook(TokenHooks::class)->tokenInfo(); |
| 196 | } | |
| 197 | ||
| 198 | /** | |
| 199 | * Implements hook_tokens(). | |
| 200 | */ | |
| 201 | #[LegacyHook] | |
| 202 | function visitors_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) { | |
| 203 | 2 | return _visitors_hook(TokenHooks::class)->tokens($type, $tokens, $data, $options, $bubbleable_metadata); |
| 204 | } | |
| 205 | ||
| 206 | /** | |
| 207 | * Implements hook_form_alter(). | |
| 208 | */ | |
| 209 | #[LegacyHook] | |
| 210 | function visitors_form_alter(&$form, FormStateInterface $form_state, $form_id) { | |
| 211 | 3 | _visitors_hook(FormHooks::class)->formAlter($form, $form_state, $form_id); |
| 212 | } | |
| 213 | ||
| 214 | /** | |
| 215 | * Implements hook_preprocess_html(). | |
| 216 | */ | |
| 217 | #[LegacyHook] | |
| 218 | function visitors_preprocess_html(array &$variables) { | |
| 219 | 3 | _visitors_hook(ThemeHooks::class)->preprocessHtml($variables); |
| 220 | } | |
| 221 | ||
| 222 | /** | |
| 223 | * Implements hook_preprocess_views_view(). | |
| 224 | */ | |
| 225 | #[LegacyHook] | |
| 226 | function visitors_preprocess_views_view(array &$variables) { | |
| 227 | 0 | _visitors_hook(ThemeHooks::class)->preprocessViewsView($variables); |
| 228 | } |