Simplesitemap.php 20.7 KB
Newer Older
1 2
<?php

Pawel G's avatar
Pawel G committed
3
namespace Drupal\simple_sitemap;
4

Pawel G's avatar
Pawel G committed
5 6 7
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Path\PathValidator;
Pawel G's avatar
Pawel G committed
8 9
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Datetime\DateFormatter;
10
use Drupal\Component\Datetime\Time;
11
use Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager;
12

13
/**
Pawel G's avatar
Pawel G committed
14
 * Class Simplesitemap
Pawel G's avatar
Pawel G committed
15
 * @package Drupal\simple_sitemap
16 17 18
 */
class Simplesitemap {

Pawel G's avatar
Pawel G committed
19 20 21
  /**
   * @var \Drupal\simple_sitemap\SitemapGenerator
   */
22
  protected $sitemapGenerator;
Pawel G's avatar
Pawel G committed
23 24 25 26

  /**
   * @var \Drupal\simple_sitemap\EntityHelper
   */
27
  protected $entityHelper;
Pawel G's avatar
Pawel G committed
28 29 30 31

  /**
   * @var \Drupal\Core\Config\ConfigFactory
   */
32
  protected $configFactory;
Pawel G's avatar
Pawel G committed
33 34 35 36

  /**
   * @var \Drupal\Core\Database\Connection
   */
37
  protected $db;
Pawel G's avatar
Pawel G committed
38 39 40 41

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
42
  protected $entityTypeManager;
Pawel G's avatar
Pawel G committed
43 44 45 46

  /**
   * @var \Drupal\Core\Path\PathValidator
   */
47
  protected $pathValidator;
Pawel G's avatar
Pawel G committed
48

49 50 51 52 53
  /**
   * @var \Drupal\Core\Datetime\DateFormatter
   */
  protected $dateFormatter;

54 55 56 57 58
  /**
   * @var \Drupal\Component\Datetime\Time
   */
  protected $time;

59
  /**
60
   * @var \Drupal\simple_sitemap\Batch
61 62 63 64 65 66 67 68
   */
  protected $batch;

  /**
   * @var \Drupal\Core\Extension\ModuleHandler
   */
  protected $moduleHandler;

69 70 71 72 73
  /**
   * @var \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager
   */
  protected $urlGeneratorManager;

Pawel G's avatar
Pawel G committed
74 75 76
  /**
   * @var array
   */
Pawel G's avatar
Pawel G committed
77
  protected static $allowedLinkSettings = [
78
    'entity' => ['index', 'priority', 'changefreq', 'include_images'],
79 80 81
    'custom' => ['priority', 'changefreq'],
  ];

Pawel G's avatar
Pawel G committed
82 83 84 85
  /**
   * @var array
   */
  protected static $linkSettingDefaults = [
86
    'index' => 1,
87
    'priority' => 0.5,
88
    'changefreq' => '',
89
    'include_images' => 0,
90
  ];
91

92 93 94 95 96 97
  protected static $generatorServices = [
    'simple_sitemap.custom_url_generator',
    'simple_sitemap.entity_url_generator',
    'simple_sitemap.arbitrary_url_generator',
  ];

98 99
  /**
   * Simplesitemap constructor.
Pawel G's avatar
Pawel G committed
100
   * @param \Drupal\simple_sitemap\SitemapGenerator $sitemapGenerator
101
   * @param \Drupal\simple_sitemap\EntityHelper $entityHelper
Pawel G's avatar
Pawel G committed
102 103 104 105 106
   * @param \Drupal\Core\Config\ConfigFactory $configFactory
   * @param \Drupal\Core\Database\Connection $database
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   * @param \Drupal\Core\Path\PathValidator $pathValidator
   * @param \Drupal\Core\Datetime\DateFormatter $dateFormatter
107
   * @param \Drupal\Component\Datetime\Time $time
108 109
   * @param \Drupal\simple_sitemap\Batch $batch
   * @param \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager $urlGeneratorManager
110
   */
111
  public function __construct(
Pawel G's avatar
Pawel G committed
112
    SitemapGenerator $sitemapGenerator,
113
    EntityHelper $entityHelper,
Pawel G's avatar
Pawel G committed
114
    ConfigFactory $configFactory,
Pawel G's avatar
Pawel G committed
115 116 117
    Connection $database,
    EntityTypeManagerInterface $entityTypeManager,
    PathValidator $pathValidator,
118
    DateFormatter $dateFormatter,
119 120
    Time $time,
    Batch $batch,
121
    UrlGeneratorManager $urlGeneratorManager
122 123
  ) {
    $this->sitemapGenerator = $sitemapGenerator;
124
    $this->entityHelper = $entityHelper;
Pawel G's avatar
Pawel G committed
125
    $this->configFactory = $configFactory;
126 127
    $this->db = $database;
    $this->entityTypeManager = $entityTypeManager;
128
    $this->pathValidator = $pathValidator;
129
    $this->dateFormatter = $dateFormatter;
130
    $this->time = $time;
131
    $this->batch = $batch;
132
    $this->urlGeneratorManager = $urlGeneratorManager;
133 134
  }

135
  /**
136 137
   * Returns a specific sitemap setting or a default value if setting does not
   * exist.
Pawel G's avatar
Pawel G committed
138
   *
139 140 141 142 143 144 145 146
   * @param string $name
   *   Name of the setting, like 'max_links'.
   *
   * @param mixed $default
   *   Value to be returned if the setting does not exist in the configuration.
   *
   * @return mixed
   *   The current setting from configuration or a default value.
Pawel G's avatar
Pawel G committed
147
   */
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
  public function getSetting($name, $default = FALSE) {
    $setting = $this->configFactory
      ->get('simple_sitemap.settings')
      ->get($name);
    return NULL !== $setting ? $setting : $default;
  }

  /**
   * Stores a specific sitemap setting in configuration.
   *
   * @param string $name
   *   Setting name, like 'max_links'.
   * @param mixed $setting
   *   The setting to be saved.
   *
   * @return $this
   */
  public function saveSetting($name, $setting) {
    $this->configFactory->getEditable("simple_sitemap.settings")
      ->set($name, $setting)->save();
    return $this;
  }

  /**
   * Returns the whole sitemap, a requested sitemap chunk,
   * or the sitemap index file.
   *
   * @param int $chunk_id
   *
   * @return string|false
   *   If no sitemap id provided, either a sitemap index is returned, or the
   *   whole sitemap, if the amount of links does not exceed the max links
   *   setting. If a sitemap id is provided, a sitemap chunk is returned. False
   *   if sitemap is not retrievable from the database.
   */
  public function getSitemap($chunk_id = NULL) {
    $chunk_info = $this->fetchSitemapChunkInfo();

    if (NULL === $chunk_id || !isset($chunk_info[$chunk_id])) {

      if (count($chunk_info) > 1) {
        // Return sitemap index, if there are multiple sitemap chunks.
        return $this->getSitemapIndex($chunk_info);
      }
      else {
        // Return sitemap if there is only one chunk.
        return count($chunk_info) === 1
        && isset($chunk_info[SitemapGenerator::FIRST_CHUNK_INDEX])
          ? $this->fetchSitemapChunk(SitemapGenerator::FIRST_CHUNK_INDEX)
            ->sitemap_string
          : FALSE;
      }
    }
    else {
      // Return specific sitemap chunk.
      return $this->fetchSitemapChunk($chunk_id)->sitemap_string;
    }
  }

  /**
   * Fetches all sitemap chunk timestamps keyed by chunk ID.
   *
   * @return array
   *   An array containing chunk creation timestamps keyed by chunk ID.
   */
  protected function fetchSitemapChunkInfo() {
214
    return $this->db
215
      ->query("SELECT id, sitemap_created FROM {simple_sitemap}")
216 217 218
      ->fetchAllAssoc('id');
  }

219 220 221 222 223 224 225 226 227
  /**
   * Fetches a single sitemap chunk by ID.
   *
   * @param int $id
   *   The chunk ID.
   *
   * @return object
   *   A sitemap chunk object.
   */
228
  protected function fetchSitemapChunk($id) {
229 230 231 232 233
    return $this->db->query('SELECT * FROM {simple_sitemap} WHERE id = :id',
      [':id' => $id])->fetchObject();
  }

  /**
Pawel G's avatar
Pawel G committed
234
   * Generates the XML sitemap and saves it to the db.
235 236
   *
   * @param string $from
Pawel G's avatar
Pawel G committed
237
   *   Can be 'form', 'backend', 'drush' or 'nobatch'.
238 239 240
   *   This decides how the batch process is to be run.
   */
  public function generateSitemap($from = 'form') {
241

Pawel G's avatar
Pawel G committed
242
    $this->batch->setBatchSettings([
243 244 245 246 247 248 249 250 251
      'base_url' => $this->getSetting('base_url', ''),
      'batch_process_limit' => $this->getSetting('batch_process_limit', NULL),
      'max_links' => $this->getSetting('max_links', 2000),
      'skip_untranslated' => $this->getSetting('skip_untranslated', FALSE),
      'remove_duplicates' => $this->getSetting('remove_duplicates', TRUE),
      'excluded_languages' => $this->getSetting('excluded_languages', []),
      'from' => $from,
    ]);

252 253
    foreach ($this->urlGeneratorManager->getDefinitions() as $plugin) {
      $this->batch->addOperation($plugin['id']);
254 255 256
    }

    $this->batch->start();
257 258 259 260 261
  }

  /**
   * Generates and returns the sitemap index as string.
   *
Pawel G's avatar
Pawel G committed
262 263
   * @param array $chunk_info
   *   Array containing chunk creation timestamps keyed by chunk ID.
264 265 266
   *
   * @return string
   *   The sitemap index.
267 268
   *
   * @todo Need to make sure response is cached.
269
   */
Pawel G's avatar
Pawel G committed
270
  protected function getSitemapIndex($chunk_info) {
271
    return $this->sitemapGenerator
Pawel G's avatar
Pawel G committed
272
      ->setSettings(['base_url' => $this->getSetting('base_url', '')])
Pawel G's avatar
Pawel G committed
273
      ->generateSitemapIndex($chunk_info);
274 275 276 277 278 279 280 281 282 283 284 285
  }

  /**
   * Returns a 'time ago' string of last timestamp generation.
   *
   * @return string|false
   *   Formatted timestamp of last sitemap generation, otherwise FALSE.
   */
  public function getGeneratedAgo() {
    $chunks = $this->fetchSitemapChunkInfo();
    if (isset($chunks[SitemapGenerator::FIRST_CHUNK_INDEX]->sitemap_created)) {
      return $this->dateFormatter
286
        ->formatInterval($this->time->getRequestTime() - $chunks[SitemapGenerator::FIRST_CHUNK_INDEX]
287 288 289 290 291
            ->sitemap_created);
    }
    return FALSE;
  }

292 293
  /**
   * Enables sitemap support for an entity type. Enabled entity types show
294 295
   * sitemap settings on their bundle setting forms. If an enabled entity type
   * features bundles (e.g. 'node'), it needs to be set up with
296 297 298
   * setBundleSettings() as well.
   *
   * @param string $entity_type_id
Pawel G's avatar
Pawel G committed
299
   *   Entity type id like 'node'.
300
   *
Pawel G's avatar
Pawel G committed
301
   * @return $this
302 303
   */
  public function enableEntityType($entity_type_id) {
304 305 306 307
    $enabled_entity_types = $this->getSetting('enabled_entity_types');
    if (!in_array($entity_type_id, $enabled_entity_types)) {
      $enabled_entity_types[] = $entity_type_id;
      $this->saveSetting('enabled_entity_types', $enabled_entity_types);
308
    }
Pawel G's avatar
Pawel G committed
309
    return $this;
310 311 312 313 314 315 316 317
  }

  /**
   * Disables sitemap support for an entity type. Disabling support for an
   * entity type deletes its sitemap settings permanently and removes sitemap
   * settings from entity forms.
   *
   * @param string $entity_type_id
318
   *  Entity type id like 'node'.
319
   *
Pawel G's avatar
Pawel G committed
320
   * @return $this
321 322
   */
  public function disableEntityType($entity_type_id) {
323 324 325

    // Updating settings.
    $enabled_entity_types = $this->getSetting('enabled_entity_types');
326
    if (FALSE !== ($key = array_search($entity_type_id, $enabled_entity_types))) {
327
      unset ($enabled_entity_types[$key]);
328
      $this->saveSetting('enabled_entity_types', array_values($enabled_entity_types));
329 330 331
    }

    // Deleting inclusion settings.
332
    $config_names = $this->configFactory->listAll("simple_sitemap.bundle_settings.$entity_type_id.");
333 334
    foreach($config_names as $config_name) {
      $this->configFactory->getEditable($config_name)->delete();
335
    }
336 337 338

    // Deleting entity overrides.
    $this->removeEntityInstanceSettings($entity_type_id);
Pawel G's avatar
Pawel G committed
339
    return $this;
340 341 342
  }

  /**
Pawel G's avatar
Pawel G committed
343
   * Sets sitemap settings for a non-bundle entity type (e.g. user) or a bundle
344 345 346
   * of an entity type (e.g. page).
   *
   * @param string $entity_type_id
Pawel G's avatar
Pawel G committed
347
   *   Entity type id like 'node' the bundle belongs to.
348
   * @param string $bundle_name
Pawel G's avatar
Pawel G committed
349
   *   Name of the bundle. NULL if entity type has no bundles.
350
   * @param array $settings
Pawel G's avatar
Pawel G committed
351
   *   An array of sitemap settings for this bundle/entity type.
352
   *   Example: ['index' => TRUE, 'priority' => 0.5, 'changefreq' => 'never', 'include_images' => FALSE].
Pawel G's avatar
Pawel G committed
353 354
   *
   * @return $this
355 356
   *
   * @todo: enableEntityType automatically
357
   */
358
  public function setBundleSettings($entity_type_id, $bundle_name = NULL, $settings = []) {
359
    $bundle_name = empty($bundle_name) ? $entity_type_id : $bundle_name;
360

361 362 363 364 365
    $old_settings = $this->getBundleSettings($entity_type_id, $bundle_name);
    $settings = !empty($old_settings) ? array_merge($old_settings, $settings) : $this->supplementDefaultSettings('entity', $settings);

    $bundle_settings = $this->configFactory
      ->getEditable("simple_sitemap.bundle_settings.$entity_type_id.$bundle_name");
366
    foreach($settings as $setting_key => $setting) {
367
      if ($setting_key === 'index') {
368 369
        $setting = intval($setting);
      }
370
      $bundle_settings->set($setting_key, $setting);
371
    }
372
    $bundle_settings->save();
373 374

    // Delete entity overrides which are identical to new bundle setting.
Pawel G's avatar
Pawel G committed
375
    $sitemap_entity_types = $this->entityHelper->getSupportedEntityTypes();
376 377 378
    if (isset($sitemap_entity_types[$entity_type_id])) {
      $entity_type = $sitemap_entity_types[$entity_type_id];
      $keys = $entity_type->getKeys();
379 380

      // Menu fix.
381 382
      $keys['bundle'] = $entity_type_id == 'menu_link_content' ? 'menu_name' : $keys['bundle'];

383
      $query = $this->entityTypeManager->getStorage($entity_type_id)->getQuery();
384
      if (!$this->entityHelper->entityTypeIsAtomic($entity_type_id)) {
385
        $query->condition($keys['bundle'], $bundle_name);
386
      }
387 388 389 390 391 392 393 394 395
      $entity_ids = $query->execute();

      $query = $this->db->select('simple_sitemap_entity_overrides', 'o')
        ->fields('o', ['id', 'inclusion_settings'])
        ->condition('o.entity_type', $entity_type_id);
      if (!empty($entity_ids)) {
        $query->condition('o.entity_id', $entity_ids, 'IN');
      }

396
      $delete_instances = [];
397
      foreach($query->execute()->fetchAll() as $result) {
398 399 400
        $delete = TRUE;
        $instance_settings = unserialize($result->inclusion_settings);
        foreach ($instance_settings as $setting_key => $instance_setting) {
401
          if ($instance_setting != $settings[$setting_key]) {
402 403 404 405 406
            $delete = FALSE;
            break;
          }
        }
        if ($delete) {
407
          $delete_instances[] = $result->id;
408
        }
409
      }
410 411 412 413 414
      if (!empty($delete_instances)) {
        $this->db->delete('simple_sitemap_entity_overrides')
          ->condition('id', $delete_instances, 'IN')
          ->execute();
      }
415
    }
416 417 418
    else {
      //todo: log error
    }
Pawel G's avatar
Pawel G committed
419
    return $this;
420 421
  }

422
  /**
423 424
   * Gets sitemap settings for an entity bundle, a non-bundle entity type or for
   * all entity types and their bundles.
425
   *
426 427 428
   * @param string|null $entity_type_id
   *  If set to null, sitemap settings for all entity types and their bundles
   *  are fetched.
429 430 431
   * @param string|null $bundle_name
   *
   * @return array|false
Pawel G's avatar
Pawel G committed
432 433
   *  Array of sitemap settings for an entity bundle, a non-bundle entity type
   *  or for all entity types and their bundles.
434
   *  False if entity type does not exist.
435
   */
436
  public function getBundleSettings($entity_type_id = NULL, $bundle_name = NULL) {
437
    if (NULL !== $entity_type_id) {
438
      $bundle_name = empty($bundle_name) ? $entity_type_id : $bundle_name;
439
      $bundle_settings = $this->configFactory
440 441
        ->get("simple_sitemap.bundle_settings.$entity_type_id.$bundle_name")
        ->get();
442
      return !empty($bundle_settings) ? $bundle_settings : FALSE;
443
    }
444
    else {
445
      $config_names = $this->configFactory->listAll("simple_sitemap.bundle_settings.");
446
      $all_settings = [];
447 448
      foreach($config_names as $config_name) {
        $config_name_parts = explode('.', $config_name);
449
        $all_settings[$config_name_parts[2]][$config_name_parts[3]] = $this->configFactory->get($config_name)->get();
450 451 452 453 454 455
      }
      return $all_settings;
    }
  }

  /**
456 457
   * Supplements all missing link setting with default values.
   *
458 459 460 461 462
   * @param string $type
   * @param array $settings
   * @return array
   */
  protected function supplementDefaultSettings($type, $settings) {
Pawel G's avatar
Pawel G committed
463
    foreach(self::$allowedLinkSettings[$type] as $allowed_link_setting) {
464
      if (!isset($settings[$allowed_link_setting])
Pawel G's avatar
Pawel G committed
465 466
        && isset(self::$linkSettingDefaults[$allowed_link_setting])) {
        $settings[$allowed_link_setting] = self::$linkSettingDefaults[$allowed_link_setting];
467 468
      }
    }
469
    return $settings;
470 471
  }

Pawel G's avatar
Pawel G committed
472 473 474
  /**
   * Overrides entity bundle/entity type sitemap settings for a single entity.
   *
Pawel G's avatar
Pawel G committed
475 476 477
   * @param string $entity_type_id
   * @param int $id
   * @param array $settings
Pawel G's avatar
Pawel G committed
478
   *
Pawel G's avatar
Pawel G committed
479 480
   * @return $this
   */
481
  public function setEntityInstanceSettings($entity_type_id, $id, $settings) {
482
    $entity = $this->entityTypeManager->getStorage($entity_type_id)->load($id);
483 484 485
    $bundle_settings = $this->getBundleSettings(
      $entity_type_id, $this->entityHelper->getEntityInstanceBundleName($entity)
    );
486
    if (!empty($bundle_settings)) {
487 488 489 490

      // Check if overrides are different from bundle setting before saving.
      $override = FALSE;
      foreach ($settings as $key => $setting) {
491
        if (!isset($bundle_settings[$key]) || $setting != $bundle_settings[$key]) {
492 493 494 495
          $override = TRUE;
          break;
        }
      }
Pawel G's avatar
Pawel G committed
496 497
      // Save overrides for this entity if something is different.
      if ($override) {
498 499 500 501 502 503 504
        $this->db->merge('simple_sitemap_entity_overrides')
          ->key([
            'entity_type' => $entity_type_id,
            'entity_id' => $id])
          ->fields([
            'entity_type' => $entity_type_id,
            'entity_id' => $id,
505
            'inclusion_settings' => serialize(array_merge($bundle_settings, $settings)),])
506
          ->execute();
507
      }
Pawel G's avatar
Pawel G committed
508 509
      // Else unset override.
      else {
510
        $this->removeEntityInstanceSettings($entity_type_id, $id);
511
      }
512 513 514
    }
    else {
      //todo: log error
515
    }
Pawel G's avatar
Pawel G committed
516
    return $this;
517 518
  }

Pawel G's avatar
Pawel G committed
519
  /**
520
   * Gets sitemap settings for an entity instance which overrides the sitemap
521
   * settings of its bundle, or bundle settings, if they are not overridden.
Pawel G's avatar
Pawel G committed
522
   *
Pawel G's avatar
Pawel G committed
523
   * @param string $entity_type_id
524
   * @param int $id
Pawel G's avatar
Pawel G committed
525
   *
526
   * @return array|false
Pawel G's avatar
Pawel G committed
527
   */
528 529 530 531 532 533 534 535 536
  public function getEntityInstanceSettings($entity_type_id, $id) {
    $results = $this->db->select('simple_sitemap_entity_overrides', 'o')
      ->fields('o', ['inclusion_settings'])
      ->condition('o.entity_type', $entity_type_id)
      ->condition('o.entity_id', $id)
      ->execute()
      ->fetchField();

    if (!empty($results)) {
537
      return unserialize($results);
538 539
    }
    else {
540 541 542 543 544 545
      $entity = $this->entityTypeManager->getStorage($entity_type_id)
        ->load($id);
      return $this->getBundleSettings(
        $entity_type_id,
        $this->entityHelper->getEntityInstanceBundleName($entity)
      );
546 547 548
    }
  }

549 550 551 552 553 554 555 556 557 558 559 560
  /**
   * Removes sitemap settings for an entity that overrides the sitemap settings
   * of its bundle.
   *
   * @param string $entity_type_id
   * @param string|null $entity_ids
   *
   * @return $this
   */
  public function removeEntityInstanceSettings($entity_type_id, $entity_ids = NULL) {
    $query = $this->db->delete('simple_sitemap_entity_overrides')
      ->condition('entity_type', $entity_type_id);
561
    if (NULL !== $entity_ids) {
562 563 564 565 566 567 568
      $entity_ids = !is_array($entity_ids) ? [$entity_ids] : $entity_ids;
      $query->condition('entity_id', $entity_ids, 'IN');
    }
    $query->execute();
    return $this;
  }

Pawel G's avatar
Pawel G committed
569 570 571 572
  /**
   * Checks if an entity bundle (or a non-bundle entity type) is set to be
   * indexed in the sitemap settings.
   *
573 574
   * @param string $entity_type_id
   * @param string|null $bundle_name
Pawel G's avatar
Pawel G committed
575
   *
Pawel G's avatar
Pawel G committed
576 577
   * @return bool
   */
578
  public function bundleIsIndexed($entity_type_id, $bundle_name = NULL) {
579 580 581 582
    $settings = $this->getBundleSettings($entity_type_id, $bundle_name);
    return !empty($settings['index']);
  }

Pawel G's avatar
Pawel G committed
583 584 585
  /**
   * Checks if an entity type is enabled in the sitemap settings.
   *
586
   * @param string $entity_type_id
Pawel G's avatar
Pawel G committed
587
   *
Pawel G's avatar
Pawel G committed
588 589
   * @return bool
   */
590
  public function entityTypeIsEnabled($entity_type_id) {
591
    return in_array($entity_type_id, $this->getSetting('enabled_entity_types', []));
592 593
  }

Pawel G's avatar
Pawel G committed
594
  /**
595
   * Stores a custom path along with its sitemap settings to configuration.
Pawel G's avatar
Pawel G committed
596
   *
Pawel G's avatar
Pawel G committed
597 598
   * @param string $path
   * @param array $settings
Pawel G's avatar
Pawel G committed
599
   *
Pawel G's avatar
Pawel G committed
600
   * @return $this
601 602
   *
   * @todo Validate $settings and throw exceptions
Pawel G's avatar
Pawel G committed
603
   */
604
  public function addCustomLink($path, $settings = []) {
Pawel G's avatar
Pawel G committed
605 606 607 608 609 610 611 612
    if (!$this->pathValidator->isValid($path)) {
      // todo: log error.
      return $this;
    }
    if ($path[0] != '/') {
      // todo: log error.
      return $this;
    }
Pawel G's avatar
Pawel G committed
613

614
    $custom_links = $this->getCustomLinks(FALSE);
Pawel G's avatar
Pawel G committed
615
    foreach ($custom_links as $key => $link) {
616
      if ($link['path'] === $path) {
617 618 619 620 621
        $link_key = $key;
        break;
      }
    }
    $link_key = isset($link_key) ? $link_key : count($custom_links);
622
    $custom_links[$link_key] = ['path' => $path] + $settings;
623
    $this->configFactory->getEditable("simple_sitemap.custom")
624
      ->set('links', $custom_links)->save();
Pawel G's avatar
Pawel G committed
625
    return $this;
626 627
  }

628 629 630
  /**
   * Returns an array of custom paths and their sitemap settings.
   *
631
   * @param bool $supplement_default_settings
632 633
   * @return array
   */
634
  public function getCustomLinks($supplement_default_settings = TRUE) {
635 636
    $custom_links = $this->configFactory
      ->get('simple_sitemap.custom')
637
      ->get('links');
638 639 640 641 642 643 644

    if ($supplement_default_settings) {
      foreach ($custom_links as $i => $link_settings) {
        $custom_links[$i] = $this->supplementDefaultSettings('custom', $link_settings);
      }
    }

645
    return $custom_links !== NULL ? $custom_links : [];
646 647
  }

Pawel G's avatar
Pawel G committed
648 649 650
  /**
   * Returns settings for a custom path added to the sitemap settings.
   *
Pawel G's avatar
Pawel G committed
651
   * @param string $path
Pawel G's avatar
Pawel G committed
652
   *
Pawel G's avatar
Pawel G committed
653
   * @return array|false
Pawel G's avatar
Pawel G committed
654
   */
655
  public function getCustomLink($path) {
656
    foreach ($this->getCustomLinks() as $key => $link) {
657
      if ($link['path'] === $path) {
658
        return $link;
659 660 661 662 663
      }
    }
    return FALSE;
  }

Pawel G's avatar
Pawel G committed
664 665 666
  /**
   * Removes a custom path from the sitemap settings.
   *
Pawel G's avatar
Pawel G committed
667
   * @param string $path
Pawel G's avatar
Pawel G committed
668
   *
Pawel G's avatar
Pawel G committed
669 670
   * @return $this
   */
671
  public function removeCustomLink($path) {
672
    $custom_links = $this->getCustomLinks(FALSE);
Pawel G's avatar
Pawel G committed
673
    foreach ($custom_links as $key => $link) {
674
      if ($link['path'] === $path) {
675 676
        unset($custom_links[$key]);
        $custom_links = array_values($custom_links);
677
        $this->configFactory->getEditable("simple_sitemap.custom")
678
          ->set('links', $custom_links)->save();
679
        break;
680 681
      }
    }
Pawel G's avatar
Pawel G committed
682
    return $this;
683 684
  }

Pawel G's avatar
Pawel G committed
685 686
  /**
   * Removes all custom paths from the sitemap settings.
Pawel G's avatar
Pawel G committed
687 688
   *
   * @return $this
Pawel G's avatar
Pawel G committed
689
   */
690
  public function removeCustomLinks() {
691
    $this->configFactory->getEditable("simple_sitemap.custom")
692
      ->set('links', [])->save();
Pawel G's avatar
Pawel G committed
693
    return $this;
694
  }
695
}