Coverage index

visitors.post_update.php

Lines: 47.49%

LineHitsSource
1
<?php
2
3
/**
4
 * @file
5
 * Post update functions for the Visitors module.
6
 */
7
8
use Drupal\Component\Serialization\Yaml;
9
10
/**
11
 * Adds exit and exit rate columns to the page reports.
12
 */
13
function visitors_post_update_add_exit_and_exit_rate(): void {
141
  $view_yaml = file_get_contents(__DIR__ . '/config/optional/views.view.visitors.yml');
151
  if ($view_yaml === FALSE) {
160
    return;
17
  }
181
  $config_data = Yaml::decode($view_yaml);
191
  $storage = \Drupal::entityTypeManager()->getStorage('view');
20
  /** @var \Drupal\views\Entity\View|null $view */
211
  $view = $storage->load('visitors');
221
  if (!$view || !isset($config_data['display'])) {
230
    return;
24
  }
25
261
  $displays = $view->get('display');
270
  $ids = [
280
    'path_table',
290
    'route_table',
300
    'exit_path_table',
310
    'exit_route_table',
320
  ];
331
  foreach ($ids as $id) {
341
    if (isset($config_data['display'][$id])) {
351
      $displays[$id] = $config_data['display'][$id];
36
    }
37
  }
381
  $view->set('display', $displays);
391
  $view->save();
40
}
41
42
/**
43
 * Adds the Outbound links display to the Visitors view.
44
 */
45
function visitors_post_update_add_outlinks_display(): void {
460
  $view = \Drupal::entityTypeManager()->getStorage('view')->load('visitors');
470
  if (!$view) {
480
    return;
49
  }
50
510
  $path = \Drupal::service('extension.list.module')->getPath('visitors')
520
    . '/config/optional/views.view.visitors.yml';
530
  $yaml = file_get_contents($path);
540
  if ($yaml === FALSE) {
550
    return;
56
  }
57
580
  $default = Yaml::decode($yaml);
590
  $displays = $view->get('display');
60
  if (
610
    isset($displays['outlinks_table'])
620
    || !isset($default['display']['outlinks_table'])
63
  ) {
640
    return;
65
  }
66
670
  $displays['outlinks_table'] = $default['display']['outlinks_table'];
680
  $view->set('display', $displays);
690
  $view->save();
70
}
71
72
/**
73
 * Adds the visit log display to the Visitors view.
74
 */
75
function visitors_post_update_add_visit_log_display(): void {
761
  $view = \Drupal::entityTypeManager()->getStorage('view')->load('visitors');
771
  if (!$view) {
780
    return;
79
  }
80
811
  $path = \Drupal::service('extension.list.module')->getPath('visitors')
821
    . '/config/optional/views.view.visitors.yml';
831
  $yaml = file_get_contents($path);
841
  if ($yaml === FALSE) {
850
    return;
86
  }
87
881
  $default = Yaml::decode($yaml);
891
  $displays = $view->get('display');
90
  if (
911
    isset($displays['visit_log'])
921
    || !isset($default['display']['visit_log'])
93
  ) {
941
    return;
95
  }
96
971
  $displays['visit_log'] = $default['display']['visit_log'];
981
  $view->set('display', $displays);
991
  $view->save();
100
}
101
102
/**
103
 * Adds the Events displays to the Visitors view.
104
 */
105
function visitors_post_update_add_event_displays(): void {
1061
  $view = \Drupal::entityTypeManager()->getStorage('view')->load('visitors');
1071
  if (!$view) {
1080
    return;
109
  }
110
1111
  $path = \Drupal::service('extension.list.module')->getPath('visitors')
1121
    . '/config/optional/views.view.visitors.yml';
1131
  $yaml = file_get_contents($path);
1141
  if ($yaml === FALSE) {
1150
    return;
116
  }
117
1181
  $default = Yaml::decode($yaml);
1191
  $displays = $view->get('display');
1201
  $added = FALSE;
1211
  foreach (['event_category_table', 'event_action_table', 'event_name_table'] as $id) {
1221
    if (!isset($displays[$id]) && isset($default['display'][$id])) {
1231
      $displays[$id] = $default['display'][$id];
1241
      $added = TRUE;
125
    }
126
  }
1271
  if (!$added) {
1281
    return;
129
  }
130
1311
  $view->set('display', $displays);
1321
  $view->save();
133
}
134
135
/**
136
 * Adds the Media display to the Visitors view.
137
 */
138
function visitors_post_update_add_media_display(): void {
1391
  $view = \Drupal::entityTypeManager()->getStorage('view')->load('visitors');
1401
  if (!$view) {
1410
    return;
142
  }
143
1441
  $path = \Drupal::service('extension.list.module')->getPath('visitors')
1451
    . '/config/optional/views.view.visitors.yml';
1461
  $yaml = file_get_contents($path);
1471
  if ($yaml === FALSE) {
1480
    return;
149
  }
150
1511
  $default = Yaml::decode($yaml);
1521
  $displays = $view->get('display');
153
  if (
1541
    isset($displays['media_table'])
1551
    || !isset($default['display']['media_table'])
156
  ) {
1571
    return;
158
  }
159
1601
  $displays['media_table'] = $default['display']['media_table'];
1611
  $view->set('display', $displays);
1621
  $view->save();
163
}
164
165
/**
166
 * Adds average time on page and unique pageviews to page reports.
167
 */
168
function visitors_post_update_add_time_on_page(): void {
1691
  $view_yaml = file_get_contents(__DIR__ . '/config/optional/views.view.visitors.yml');
1701
  if ($view_yaml === FALSE) {
1710
    return;
172
  }
1731
  $config_data = Yaml::decode($view_yaml);
1741
  $storage = \Drupal::entityTypeManager()->getStorage('view');
175
  /** @var \Drupal\views\Entity\View|null $view */
1761
  $view = $storage->load('visitors');
1771
  if (!$view || !isset($config_data['display'])) {
1780
    return;
179
  }
180
1811
  $displays = $view->get('display');
1821
  foreach (['path_table', 'route_table'] as $id) {
1831
    if (isset($config_data['display'][$id])) {
1841
      $displays[$id] = $config_data['display'][$id];
185
    }
186
  }
1871
  $view->set('display', $displays);
1881
  $view->save();
189
}
190
191
/**
192
 * Adds the bot name and bot path displays to the Visitors view.
193
 */
194
function visitors_post_update_add_bot_displays(): void {
1951
  $view = \Drupal::entityTypeManager()->getStorage('view')->load('visitors');
1961
  if (!$view) {
1970
    return;
198
  }
199
2001
  $path = \Drupal::service('extension.list.module')->getPath('visitors')
2011
    . '/config/optional/views.view.visitors.yml';
2021
  $yaml = file_get_contents($path);
2031
  if ($yaml === FALSE) {
2040
    return;
205
  }
206
2071
  $default = Yaml::decode($yaml);
2081
  $displays = $view->get('display');
2091
  $added = FALSE;
2101
  foreach (['bot_table', 'bot_path_table'] as $id) {
2111
    if (!isset($displays[$id]) && isset($default['display'][$id])) {
2121
      $displays[$id] = $default['display'][$id];
2131
      $added = TRUE;
214
    }
215
  }
2161
  if (!$added) {
2170
    return;
218
  }
219
2201
  $view->set('display', $displays);
2211
  $view->save();
222
}
223
224
/**
225
 * Adds the site search displays to the Visitors view.
226
 */
227
function visitors_post_update_add_site_search_displays(): void {
2281
  $settings = \Drupal::configFactory()->getEditable('visitors.settings');
2291
  if ($settings->get('site_search_views') === NULL) {
2301
    $settings->set('site_search_views', [])->save();
231
  }
232
2331
  $view = \Drupal::entityTypeManager()->getStorage('view')->load('visitors');
2341
  if (!$view) {
2350
    return;
236
  }
237
2381
  $path = \Drupal::service('extension.list.module')->getPath('visitors')
2391
    . '/config/optional/views.view.visitors.yml';
2401
  $yaml = file_get_contents($path);
2411
  if ($yaml === FALSE) {
2420
    return;
243
  }
244
2451
  $default = Yaml::decode($yaml);
2461
  $displays = $view->get('display');
2471
  $added = FALSE;
2480
  $ids = [
2490
    'site_search_chart',
2500
    'site_search_keywords',
2510
    'site_search_referrers',
2520
  ];
2531
  foreach ($ids as $id) {
2541
    if (!isset($displays[$id]) && isset($default['display'][$id])) {
2551
      $displays[$id] = $default['display'][$id];
2561
      $added = TRUE;
257
    }
258
  }
2591
  if (!$added) {
2600
    return;
261
  }
262
2631
  $view->set('display', $displays);
2641
  $view->save();
265
}
266
267
/**
268
 * Removes the theme condition from tracking visibility.
269
 */
270
function visitors_post_update_remove_theme_visibility(): void {
2710
  $config = \Drupal::configFactory()->getEditable('visitors.settings');
2720
  $conditions = $config->get('tracking_conditions') ?: [];
2730
  if (!isset($conditions['current_theme'])) {
2740
    return;
275
  }
2760
  unset($conditions['current_theme']);
2770
  $config->set('tracking_conditions', $conditions)->save();
278
}
279
280
/**
281
 * Sets the location plugin for existing sites.
282
 */
283
function visitors_post_update_location_plugin(): void {
2840
  $settings = \Drupal::configFactory()->getEditable('visitors.settings');
2850
  if ($settings->get('location_plugin') !== NULL) {
2860
    return;
287
  }
2880
  $settings->set('location_plugin', 'browser_language')->save();
289
}
290
291
/**
292
 * Adds the AI agent report and the human-only visit filter.
293
 */
294
function visitors_post_update_add_ai_agent_report(): void {
2950
  $view = \Drupal::entityTypeManager()->getStorage('view')->load('visitors');
2960
  if (!$view) {
2970
    return;
298
  }
299
3000
  $path = \Drupal::service('extension.list.module')->getPath('visitors')
3010
    . '/config/optional/views.view.visitors.yml';
3020
  $yaml = file_get_contents($path);
3030
  if ($yaml === FALSE) {
3040
    return;
305
  }
306
3070
  $default = Yaml::decode($yaml);
3080
  $displays = $view->get('display');
3090
  if (isset($default['display']['ai_agent_table'])) {
3100
    $displays['ai_agent_table'] = $default['display']['ai_agent_table'];
311
  }
312
3130
  $filter = [
3140
    'id' => 'ai_agent',
3150
    'table' => 'visitors_visit',
3160
    'field' => 'ai_agent',
3170
    'relationship' => 'none',
3180
    'group_type' => 'group',
3190
    'admin_label' => '',
3200
    'plugin_id' => 'boolean',
3210
    'operator' => '=',
3220
    'value' => '0',
3230
    'group' => 1,
3240
    'exposed' => FALSE,
3250
  ];
3260
  foreach ($displays as $id => &$display) {
3270
    if ($id === 'ai_agent_table') {
3280
      continue;
329
    }
3300
    $filters = $display['display_options']['filters'] ?? NULL;
3310
    if (!is_array($filters) || !isset($filters['bot'])) {
3320
      continue;
333
    }
3340
    $bot = $filters['bot'];
3350
    $excludes_bots = ($bot['operator'] ?? '') === '!='
3360
      && (string) ($bot['value'] ?? '') === '1';
3370
    if (!$excludes_bots || isset($filters['ai_agent'])) {
3380
      continue;
339
    }
3400
    $filters['ai_agent'] = $filter;
3410
    $display['display_options']['filters'] = $filters;
342
  }
3430
  unset($display);
344
3450
  $view->set('display', $displays);
3460
  $view->save();
347
}
348
349
/**
350
 * Adds the AI assistant pie, column, table, and entry page displays.
351
 */
352
function visitors_post_update_add_ai_assistant_displays(): void {
3530
  $view = \Drupal::entityTypeManager()->getStorage('view')->load('visitors');
3540
  if (!$view) {
3550
    return;
356
  }
357
3580
  $path = \Drupal::service('extension.list.module')->getPath('visitors')
3590
    . '/config/optional/views.view.visitors.yml';
3600
  $yaml = file_get_contents($path);
3610
  if ($yaml === FALSE) {
3620
    return;
363
  }
364
3650
  $default = Yaml::decode($yaml);
3660
  $displays = $view->get('display');
3670
  $ids = [
3680
    'ai_visitors_column',
3690
    'ai_visitors_pie',
3700
    'ai_visitors_table',
3710
    'ai_entry_pages',
3720
  ];
3730
  foreach ($ids as $id) {
3740
    if (isset($default['display'][$id])) {
3750
      $displays[$id] = $default['display'][$id];
376
    }
377
  }
378
3790
  $view->set('display', $displays);
3800
  $view->save();
381
}