Lines: 47.49%
| Line | Hits | Source |
|---|---|---|
| 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 { | |
| 14 | 1 | $view_yaml = file_get_contents(__DIR__ . '/config/optional/views.view.visitors.yml'); |
| 15 | 1 | if ($view_yaml === FALSE) { |
| 16 | 0 | return; |
| 17 | } | |
| 18 | 1 | $config_data = Yaml::decode($view_yaml); |
| 19 | 1 | $storage = \Drupal::entityTypeManager()->getStorage('view'); |
| 20 | /** @var \Drupal\views\Entity\View|null $view */ | |
| 21 | 1 | $view = $storage->load('visitors'); |
| 22 | 1 | if (!$view || !isset($config_data['display'])) { |
| 23 | 0 | return; |
| 24 | } | |
| 25 | ||
| 26 | 1 | $displays = $view->get('display'); |
| 27 | 0 | $ids = [ |
| 28 | 0 | 'path_table', |
| 29 | 0 | 'route_table', |
| 30 | 0 | 'exit_path_table', |
| 31 | 0 | 'exit_route_table', |
| 32 | 0 | ]; |
| 33 | 1 | foreach ($ids as $id) { |
| 34 | 1 | if (isset($config_data['display'][$id])) { |
| 35 | 1 | $displays[$id] = $config_data['display'][$id]; |
| 36 | } | |
| 37 | } | |
| 38 | 1 | $view->set('display', $displays); |
| 39 | 1 | $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 { | |
| 46 | 0 | $view = \Drupal::entityTypeManager()->getStorage('view')->load('visitors'); |
| 47 | 0 | if (!$view) { |
| 48 | 0 | return; |
| 49 | } | |
| 50 | ||
| 51 | 0 | $path = \Drupal::service('extension.list.module')->getPath('visitors') |
| 52 | 0 | . '/config/optional/views.view.visitors.yml'; |
| 53 | 0 | $yaml = file_get_contents($path); |
| 54 | 0 | if ($yaml === FALSE) { |
| 55 | 0 | return; |
| 56 | } | |
| 57 | ||
| 58 | 0 | $default = Yaml::decode($yaml); |
| 59 | 0 | $displays = $view->get('display'); |
| 60 | if ( | |
| 61 | 0 | isset($displays['outlinks_table']) |
| 62 | 0 | || !isset($default['display']['outlinks_table']) |
| 63 | ) { | |
| 64 | 0 | return; |
| 65 | } | |
| 66 | ||
| 67 | 0 | $displays['outlinks_table'] = $default['display']['outlinks_table']; |
| 68 | 0 | $view->set('display', $displays); |
| 69 | 0 | $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 { | |
| 76 | 1 | $view = \Drupal::entityTypeManager()->getStorage('view')->load('visitors'); |
| 77 | 1 | if (!$view) { |
| 78 | 0 | return; |
| 79 | } | |
| 80 | ||
| 81 | 1 | $path = \Drupal::service('extension.list.module')->getPath('visitors') |
| 82 | 1 | . '/config/optional/views.view.visitors.yml'; |
| 83 | 1 | $yaml = file_get_contents($path); |
| 84 | 1 | if ($yaml === FALSE) { |
| 85 | 0 | return; |
| 86 | } | |
| 87 | ||
| 88 | 1 | $default = Yaml::decode($yaml); |
| 89 | 1 | $displays = $view->get('display'); |
| 90 | if ( | |
| 91 | 1 | isset($displays['visit_log']) |
| 92 | 1 | || !isset($default['display']['visit_log']) |
| 93 | ) { | |
| 94 | 1 | return; |
| 95 | } | |
| 96 | ||
| 97 | 1 | $displays['visit_log'] = $default['display']['visit_log']; |
| 98 | 1 | $view->set('display', $displays); |
| 99 | 1 | $view->save(); |
| 100 | } | |
| 101 | ||
| 102 | /** | |
| 103 | * Adds the Events displays to the Visitors view. | |
| 104 | */ | |
| 105 | function visitors_post_update_add_event_displays(): void { | |
| 106 | 1 | $view = \Drupal::entityTypeManager()->getStorage('view')->load('visitors'); |
| 107 | 1 | if (!$view) { |
| 108 | 0 | return; |
| 109 | } | |
| 110 | ||
| 111 | 1 | $path = \Drupal::service('extension.list.module')->getPath('visitors') |
| 112 | 1 | . '/config/optional/views.view.visitors.yml'; |
| 113 | 1 | $yaml = file_get_contents($path); |
| 114 | 1 | if ($yaml === FALSE) { |
| 115 | 0 | return; |
| 116 | } | |
| 117 | ||
| 118 | 1 | $default = Yaml::decode($yaml); |
| 119 | 1 | $displays = $view->get('display'); |
| 120 | 1 | $added = FALSE; |
| 121 | 1 | foreach (['event_category_table', 'event_action_table', 'event_name_table'] as $id) { |
| 122 | 1 | if (!isset($displays[$id]) && isset($default['display'][$id])) { |
| 123 | 1 | $displays[$id] = $default['display'][$id]; |
| 124 | 1 | $added = TRUE; |
| 125 | } | |
| 126 | } | |
| 127 | 1 | if (!$added) { |
| 128 | 1 | return; |
| 129 | } | |
| 130 | ||
| 131 | 1 | $view->set('display', $displays); |
| 132 | 1 | $view->save(); |
| 133 | } | |
| 134 | ||
| 135 | /** | |
| 136 | * Adds the Media display to the Visitors view. | |
| 137 | */ | |
| 138 | function visitors_post_update_add_media_display(): void { | |
| 139 | 1 | $view = \Drupal::entityTypeManager()->getStorage('view')->load('visitors'); |
| 140 | 1 | if (!$view) { |
| 141 | 0 | return; |
| 142 | } | |
| 143 | ||
| 144 | 1 | $path = \Drupal::service('extension.list.module')->getPath('visitors') |
| 145 | 1 | . '/config/optional/views.view.visitors.yml'; |
| 146 | 1 | $yaml = file_get_contents($path); |
| 147 | 1 | if ($yaml === FALSE) { |
| 148 | 0 | return; |
| 149 | } | |
| 150 | ||
| 151 | 1 | $default = Yaml::decode($yaml); |
| 152 | 1 | $displays = $view->get('display'); |
| 153 | if ( | |
| 154 | 1 | isset($displays['media_table']) |
| 155 | 1 | || !isset($default['display']['media_table']) |
| 156 | ) { | |
| 157 | 1 | return; |
| 158 | } | |
| 159 | ||
| 160 | 1 | $displays['media_table'] = $default['display']['media_table']; |
| 161 | 1 | $view->set('display', $displays); |
| 162 | 1 | $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 { | |
| 169 | 1 | $view_yaml = file_get_contents(__DIR__ . '/config/optional/views.view.visitors.yml'); |
| 170 | 1 | if ($view_yaml === FALSE) { |
| 171 | 0 | return; |
| 172 | } | |
| 173 | 1 | $config_data = Yaml::decode($view_yaml); |
| 174 | 1 | $storage = \Drupal::entityTypeManager()->getStorage('view'); |
| 175 | /** @var \Drupal\views\Entity\View|null $view */ | |
| 176 | 1 | $view = $storage->load('visitors'); |
| 177 | 1 | if (!$view || !isset($config_data['display'])) { |
| 178 | 0 | return; |
| 179 | } | |
| 180 | ||
| 181 | 1 | $displays = $view->get('display'); |
| 182 | 1 | foreach (['path_table', 'route_table'] as $id) { |
| 183 | 1 | if (isset($config_data['display'][$id])) { |
| 184 | 1 | $displays[$id] = $config_data['display'][$id]; |
| 185 | } | |
| 186 | } | |
| 187 | 1 | $view->set('display', $displays); |
| 188 | 1 | $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 { | |
| 195 | 1 | $view = \Drupal::entityTypeManager()->getStorage('view')->load('visitors'); |
| 196 | 1 | if (!$view) { |
| 197 | 0 | return; |
| 198 | } | |
| 199 | ||
| 200 | 1 | $path = \Drupal::service('extension.list.module')->getPath('visitors') |
| 201 | 1 | . '/config/optional/views.view.visitors.yml'; |
| 202 | 1 | $yaml = file_get_contents($path); |
| 203 | 1 | if ($yaml === FALSE) { |
| 204 | 0 | return; |
| 205 | } | |
| 206 | ||
| 207 | 1 | $default = Yaml::decode($yaml); |
| 208 | 1 | $displays = $view->get('display'); |
| 209 | 1 | $added = FALSE; |
| 210 | 1 | foreach (['bot_table', 'bot_path_table'] as $id) { |
| 211 | 1 | if (!isset($displays[$id]) && isset($default['display'][$id])) { |
| 212 | 1 | $displays[$id] = $default['display'][$id]; |
| 213 | 1 | $added = TRUE; |
| 214 | } | |
| 215 | } | |
| 216 | 1 | if (!$added) { |
| 217 | 0 | return; |
| 218 | } | |
| 219 | ||
| 220 | 1 | $view->set('display', $displays); |
| 221 | 1 | $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 { | |
| 228 | 1 | $settings = \Drupal::configFactory()->getEditable('visitors.settings'); |
| 229 | 1 | if ($settings->get('site_search_views') === NULL) { |
| 230 | 1 | $settings->set('site_search_views', [])->save(); |
| 231 | } | |
| 232 | ||
| 233 | 1 | $view = \Drupal::entityTypeManager()->getStorage('view')->load('visitors'); |
| 234 | 1 | if (!$view) { |
| 235 | 0 | return; |
| 236 | } | |
| 237 | ||
| 238 | 1 | $path = \Drupal::service('extension.list.module')->getPath('visitors') |
| 239 | 1 | . '/config/optional/views.view.visitors.yml'; |
| 240 | 1 | $yaml = file_get_contents($path); |
| 241 | 1 | if ($yaml === FALSE) { |
| 242 | 0 | return; |
| 243 | } | |
| 244 | ||
| 245 | 1 | $default = Yaml::decode($yaml); |
| 246 | 1 | $displays = $view->get('display'); |
| 247 | 1 | $added = FALSE; |
| 248 | 0 | $ids = [ |
| 249 | 0 | 'site_search_chart', |
| 250 | 0 | 'site_search_keywords', |
| 251 | 0 | 'site_search_referrers', |
| 252 | 0 | ]; |
| 253 | 1 | foreach ($ids as $id) { |
| 254 | 1 | if (!isset($displays[$id]) && isset($default['display'][$id])) { |
| 255 | 1 | $displays[$id] = $default['display'][$id]; |
| 256 | 1 | $added = TRUE; |
| 257 | } | |
| 258 | } | |
| 259 | 1 | if (!$added) { |
| 260 | 0 | return; |
| 261 | } | |
| 262 | ||
| 263 | 1 | $view->set('display', $displays); |
| 264 | 1 | $view->save(); |
| 265 | } | |
| 266 | ||
| 267 | /** | |
| 268 | * Removes the theme condition from tracking visibility. | |
| 269 | */ | |
| 270 | function visitors_post_update_remove_theme_visibility(): void { | |
| 271 | 0 | $config = \Drupal::configFactory()->getEditable('visitors.settings'); |
| 272 | 0 | $conditions = $config->get('tracking_conditions') ?: []; |
| 273 | 0 | if (!isset($conditions['current_theme'])) { |
| 274 | 0 | return; |
| 275 | } | |
| 276 | 0 | unset($conditions['current_theme']); |
| 277 | 0 | $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 { | |
| 284 | 0 | $settings = \Drupal::configFactory()->getEditable('visitors.settings'); |
| 285 | 0 | if ($settings->get('location_plugin') !== NULL) { |
| 286 | 0 | return; |
| 287 | } | |
| 288 | 0 | $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 { | |
| 295 | 0 | $view = \Drupal::entityTypeManager()->getStorage('view')->load('visitors'); |
| 296 | 0 | if (!$view) { |
| 297 | 0 | return; |
| 298 | } | |
| 299 | ||
| 300 | 0 | $path = \Drupal::service('extension.list.module')->getPath('visitors') |
| 301 | 0 | . '/config/optional/views.view.visitors.yml'; |
| 302 | 0 | $yaml = file_get_contents($path); |
| 303 | 0 | if ($yaml === FALSE) { |
| 304 | 0 | return; |
| 305 | } | |
| 306 | ||
| 307 | 0 | $default = Yaml::decode($yaml); |
| 308 | 0 | $displays = $view->get('display'); |
| 309 | 0 | if (isset($default['display']['ai_agent_table'])) { |
| 310 | 0 | $displays['ai_agent_table'] = $default['display']['ai_agent_table']; |
| 311 | } | |
| 312 | ||
| 313 | 0 | $filter = [ |
| 314 | 0 | 'id' => 'ai_agent', |
| 315 | 0 | 'table' => 'visitors_visit', |
| 316 | 0 | 'field' => 'ai_agent', |
| 317 | 0 | 'relationship' => 'none', |
| 318 | 0 | 'group_type' => 'group', |
| 319 | 0 | 'admin_label' => '', |
| 320 | 0 | 'plugin_id' => 'boolean', |
| 321 | 0 | 'operator' => '=', |
| 322 | 0 | 'value' => '0', |
| 323 | 0 | 'group' => 1, |
| 324 | 0 | 'exposed' => FALSE, |
| 325 | 0 | ]; |
| 326 | 0 | foreach ($displays as $id => &$display) { |
| 327 | 0 | if ($id === 'ai_agent_table') { |
| 328 | 0 | continue; |
| 329 | } | |
| 330 | 0 | $filters = $display['display_options']['filters'] ?? NULL; |
| 331 | 0 | if (!is_array($filters) || !isset($filters['bot'])) { |
| 332 | 0 | continue; |
| 333 | } | |
| 334 | 0 | $bot = $filters['bot']; |
| 335 | 0 | $excludes_bots = ($bot['operator'] ?? '') === '!=' |
| 336 | 0 | && (string) ($bot['value'] ?? '') === '1'; |
| 337 | 0 | if (!$excludes_bots || isset($filters['ai_agent'])) { |
| 338 | 0 | continue; |
| 339 | } | |
| 340 | 0 | $filters['ai_agent'] = $filter; |
| 341 | 0 | $display['display_options']['filters'] = $filters; |
| 342 | } | |
| 343 | 0 | unset($display); |
| 344 | ||
| 345 | 0 | $view->set('display', $displays); |
| 346 | 0 | $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 { | |
| 353 | 0 | $view = \Drupal::entityTypeManager()->getStorage('view')->load('visitors'); |
| 354 | 0 | if (!$view) { |
| 355 | 0 | return; |
| 356 | } | |
| 357 | ||
| 358 | 0 | $path = \Drupal::service('extension.list.module')->getPath('visitors') |
| 359 | 0 | . '/config/optional/views.view.visitors.yml'; |
| 360 | 0 | $yaml = file_get_contents($path); |
| 361 | 0 | if ($yaml === FALSE) { |
| 362 | 0 | return; |
| 363 | } | |
| 364 | ||
| 365 | 0 | $default = Yaml::decode($yaml); |
| 366 | 0 | $displays = $view->get('display'); |
| 367 | 0 | $ids = [ |
| 368 | 0 | 'ai_visitors_column', |
| 369 | 0 | 'ai_visitors_pie', |
| 370 | 0 | 'ai_visitors_table', |
| 371 | 0 | 'ai_entry_pages', |
| 372 | 0 | ]; |
| 373 | 0 | foreach ($ids as $id) { |
| 374 | 0 | if (isset($default['display'][$id])) { |
| 375 | 0 | $displays[$id] = $default['display'][$id]; |
| 376 | } | |
| 377 | } | |
| 378 | ||
| 379 | 0 | $view->set('display', $displays); |
| 380 | 0 | $view->save(); |
| 381 | } |