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

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

5
use Drupal\simple_sitemap\Form\FormHelper;
Pawel G's avatar
Pawel G committed
6 7 8
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Path\PathValidator;
Pawel G's avatar
Pawel G committed
9 10 11
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Datetime\DateFormatter;
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
  private $sitemapGenerator;
Pawel G's avatar
Pawel G committed
23 24 25 26

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

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

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

  /**
   * @var \Drupal\Core\Entity\Query\QueryFactory
   */
42
  private $entityQuery;
Pawel G's avatar
Pawel G committed
43 44 45 46

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
47
  private $entityTypeManager;
Pawel G's avatar
Pawel G committed
48 49 50 51

  /**
   * @var \Drupal\Core\Path\PathValidator
   */
52
  private $pathValidator;
Pawel G's avatar
Pawel G committed
53 54 55 56

  /**
   * @var array
   */
57 58
  private static $allowed_link_settings = [
    'entity' => ['index', 'priority'],
Pawel G's avatar
Pawel G committed
59
    'custom' => ['priority'],
60
  ];
61

62 63
  /**
   * Simplesitemap constructor.
Pawel G's avatar
Pawel G committed
64
   * @param \Drupal\simple_sitemap\SitemapGenerator $sitemapGenerator
65
   * @param \Drupal\simple_sitemap\EntityHelper $entityHelper
Pawel G's avatar
Pawel G committed
66 67 68 69 70 71
   * @param \Drupal\Core\Config\ConfigFactory $configFactory
   * @param \Drupal\Core\Database\Connection $database
   * @param \Drupal\Core\Entity\Query\QueryFactory $entityQuery
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   * @param \Drupal\Core\Path\PathValidator $pathValidator
   * @param \Drupal\Core\Datetime\DateFormatter $dateFormatter
72
   */
73
  public function __construct(
Pawel G's avatar
Pawel G committed
74
    SitemapGenerator $sitemapGenerator,
75
    EntityHelper $entityHelper,
Pawel G's avatar
Pawel G committed
76
    ConfigFactory $configFactory,
Pawel G's avatar
Pawel G committed
77
    Connection $database,
Pawel G's avatar
Pawel G committed
78
    QueryFactory $entityQuery,
Pawel G's avatar
Pawel G committed
79 80
    EntityTypeManagerInterface $entityTypeManager,
    PathValidator $pathValidator,
81
    DateFormatter $dateFormatter
82 83
  ) {
    $this->sitemapGenerator = $sitemapGenerator;
84
    $this->entityHelper = $entityHelper;
Pawel G's avatar
Pawel G committed
85
    $this->configFactory = $configFactory;
86
    $this->db = $database;
87
    $this->entityQuery = $entityQuery;
88
    $this->entityTypeManager = $entityTypeManager;
89
    $this->pathValidator = $pathValidator;
90
    $this->dateFormatter = $dateFormatter;
91 92
  }

93
  /**
94
   * Fetches all sitemap chunks indexed by chunk ID.
Pawel G's avatar
Pawel G committed
95
   *
96
   * @return string
Pawel G's avatar
Pawel G committed
97
   */
Pawel G's avatar
Pawel G committed
98
  private function fetchSitemapChunks() {
99
    return $this->db
100 101 102 103
      ->query("SELECT * FROM {simple_sitemap}")
      ->fetchAllAssoc('id');
  }

104 105
  /**
   * Enables sitemap support for an entity type. Enabled entity types show
106 107
   * sitemap settings on their bundle setting forms. If an enabled entity type
   * features bundles (e.g. 'node'), it needs to be set up with
108 109 110
   * setBundleSettings() as well.
   *
   * @param string $entity_type_id
Pawel G's avatar
Pawel G committed
111
   *   Entity type id like 'node'.
112
   *
Pawel G's avatar
Pawel G committed
113
   * @return $this
114 115
   */
  public function enableEntityType($entity_type_id) {
116 117 118 119
    $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);
120
    }
Pawel G's avatar
Pawel G committed
121
    return $this;
122 123 124 125 126 127 128 129
  }

  /**
   * 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
130
   *  Entity type id like 'node'.
131
   *
Pawel G's avatar
Pawel G committed
132
   * @return $this
133 134
   */
  public function disableEntityType($entity_type_id) {
135 136 137

    // Updating settings.
    $enabled_entity_types = $this->getSetting('enabled_entity_types');
138
    if (FALSE !== ($key = array_search($entity_type_id, $enabled_entity_types))) {
139
      unset ($enabled_entity_types[$key]);
140
      $this->saveSetting('enabled_entity_types', array_values($enabled_entity_types));
141 142 143 144 145 146
    }

    // Deleting inclusion settings.
    $config_names = $this->configFactory->listAll("simple_sitemap.bundle_settings.$entity_type_id");
    foreach($config_names as $config_name) {
      $this->configFactory->getEditable($config_name)->delete();
147
    }
148 149 150

    // Deleting entity overrides.
    $this->removeEntityInstanceSettings($entity_type_id);
Pawel G's avatar
Pawel G committed
151
    return $this;
152 153 154
  }

  /**
Pawel G's avatar
Pawel G committed
155
   * Sets sitemap settings for a non-bundle entity type (e.g. user) or a bundle
156 157 158
   * of an entity type (e.g. page).
   *
   * @param string $entity_type_id
Pawel G's avatar
Pawel G committed
159
   *   Entity type id like 'node' the bundle belongs to.
160
   * @param string $bundle_name
Pawel G's avatar
Pawel G committed
161
   *   Name of the bundle. NULL if entity type has no bundles.
162
   * @param array $settings
Pawel G's avatar
Pawel G committed
163 164
   *   An array of sitemap settings for this bundle/entity type.
   *   Example: ['index' => TRUE, 'priority' => 0.5].
Pawel G's avatar
Pawel G committed
165 166
   *
   * @return $this
167 168
   */
  public function setBundleSettings($entity_type_id, $bundle_name = NULL, $settings) {
169

170
    $bundle_name = empty($bundle_name) ? $entity_type_id : $bundle_name;
171

172 173 174 175 176 177 178 179 180 181
    foreach($settings as $setting_key => $setting) {
      if ($setting_key == 'index') {
        $setting = intval($setting);
      }
      $this->configFactory
        ->getEditable("simple_sitemap.bundle_settings.$entity_type_id.$bundle_name")
        ->set($setting_key, $setting)
        ->save();
    }
    //todo: Use addLinkSettings()?
182 183

    // Delete entity overrides which are identical to new bundle setting.
184
    $sitemap_entity_types = $this->entityHelper->getSitemapEntityTypes();
185 186 187 188 189 190
    if (isset($sitemap_entity_types[$entity_type_id])) {
      $entity_type = $sitemap_entity_types[$entity_type_id];
      $keys = $entity_type->getKeys();
      $keys['bundle'] = $entity_type_id == 'menu_link_content' ? 'menu_name' : $keys['bundle'];

      $query = $this->entityQuery->get($entity_type_id);
191
      if (!$this->entityHelper->entityTypeIsAtomic($entity_type_id)) {
192
        $query->condition($keys['bundle'], $bundle_name);
193
      }
194 195 196 197 198 199 200 201 202 203 204 205
      $entity_ids = $query->execute();

      $bundle_settings = $this->configFactory
        ->get("simple_sitemap.bundle_settings.$entity_type_id.$bundle_name");

      $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');
      }

206
      foreach($query->execute()->fetchAll() as $result) {
207 208 209 210 211 212 213 214 215 216 217 218 219
        $delete = TRUE;
        $instance_settings = unserialize($result->inclusion_settings);
        foreach ($instance_settings as $setting_key => $instance_setting) {
          if ($instance_setting != $bundle_settings->get($setting_key)) {
            $delete = FALSE;
            break;
          }
        }
        if ($delete) {
          $this->db->delete('simple_sitemap_entity_overrides')
            ->condition('id', $result->id)
            ->execute();
        }
220 221
      }
    }
222 223 224
    else {
      //todo: log error
    }
Pawel G's avatar
Pawel G committed
225
    return $this;
226 227
  }

228
  /**
229 230
   * Gets sitemap settings for an entity bundle, a non-bundle entity type or for
   * all entity types and their bundles.
231
   *
232 233 234
   * @param string|null $entity_type_id
   *  If set to null, sitemap settings for all entity types and their bundles
   *  are fetched.
235 236 237
   * @param string|null $bundle_name
   *
   * @return array|false
Pawel G's avatar
Pawel G committed
238 239
   *  Array of sitemap settings for an entity bundle, a non-bundle entity type
   *  or for all entity types and their bundles.
240
   *  False if entity type does not exist.
241
   */
242
  public function getBundleSettings($entity_type_id = NULL, $bundle_name = NULL) {
Pawel G's avatar
Pawel G committed
243
    if (null !== $entity_type_id) {
244 245 246 247 248
      $bundle_name = empty($bundle_name) ? $entity_type_id : $bundle_name;
      $settings = $this->configFactory
        ->get("simple_sitemap.bundle_settings.$entity_type_id.$bundle_name")
        ->get();
      $bundle_settings = !empty($settings) ? $settings : FALSE;
249
    }
250 251 252 253 254 255 256 257 258 259
    else {
      $config_names = $this->configFactory->listAll("simple_sitemap.bundle_settings");
      $bundle_settings = [];
      foreach($config_names as $config_name) {
        $config_name_parts = explode('.', $config_name);
        $bundle_settings[$config_name_parts[2]][$config_name_parts[3]]
          = $this->configFactory->get($config_name)->get();
      }
    }
    return $bundle_settings;
260 261
  }

Pawel G's avatar
Pawel G committed
262 263 264
  /**
   * Overrides entity bundle/entity type sitemap settings for a single entity.
   *
Pawel G's avatar
Pawel G committed
265 266 267
   * @param string $entity_type_id
   * @param int $id
   * @param array $settings
Pawel G's avatar
Pawel G committed
268
   *
Pawel G's avatar
Pawel G committed
269 270
   * @return $this
   */
271
  public function setEntityInstanceSettings($entity_type_id, $id, $settings) {
272

273
    $entity = $this->entityTypeManager->getStorage($entity_type_id)->load($id);
274
    $bundle_name = $this->entityHelper->getEntityInstanceBundleName($entity);
275 276 277 278 279
    $bundle_settings = $this->configFactory
      ->get("simple_sitemap.bundle_settings.$entity_type_id.$bundle_name")
      ->get();

    if (!empty($bundle_settings)) {
280 281 282 283

      // Check if overrides are different from bundle setting before saving.
      $override = FALSE;
      foreach ($settings as $key => $setting) {
284
        if ($setting != $bundle_settings[$key]) {
285 286 287 288
          $override = TRUE;
          break;
        }
      }
Pawel G's avatar
Pawel G committed
289 290
      // Save overrides for this entity if something is different.
      if ($override) {
291 292 293 294 295 296 297 298 299 300
        $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,
            'inclusion_settings' => serialize($settings),
          ])
          ->execute();
301
      }
Pawel G's avatar
Pawel G committed
302 303
      // Else unset override.
      else {
304
        $this->removeEntityInstanceSettings($entity_type_id, $id);
305
      }
306 307 308
    }
    else {
      //todo: log error
309
    }
Pawel G's avatar
Pawel G committed
310
    return $this;
311 312
  }

Pawel G's avatar
Pawel G committed
313
  /**
314
   * Gets sitemap settings for an entity instance which overrides the sitemap
315
   * settings of its bundle, or bundle settings, if they are not overridden.
Pawel G's avatar
Pawel G committed
316
   *
Pawel G's avatar
Pawel G committed
317
   * @param string $entity_type_id
318
   * @param int $id
Pawel G's avatar
Pawel G committed
319
   *
320
   * @return array|false
Pawel G's avatar
Pawel G committed
321
   */
322 323 324 325 326 327 328 329 330 331 332 333
  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)) {
      return unserialize($results);
    }
    else {
334 335 336 337 338 339
      $entity = $this->entityTypeManager->getStorage($entity_type_id)
        ->load($id);
      return $this->getBundleSettings(
        $entity_type_id,
        $this->entityHelper->getEntityInstanceBundleName($entity)
      );
340 341 342
    }
  }

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
  /**
   * 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);
    if (!is_null($entity_ids)) {
      $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
363 364 365 366
  /**
   * Checks if an entity bundle (or a non-bundle entity type) is set to be
   * indexed in the sitemap settings.
   *
367 368
   * @param string $entity_type_id
   * @param string|null $bundle_name
Pawel G's avatar
Pawel G committed
369
   *
Pawel G's avatar
Pawel G committed
370 371
   * @return bool
   */
372
  public function bundleIsIndexed($entity_type_id, $bundle_name = NULL) {
373 374 375 376
    $settings = $this->getBundleSettings($entity_type_id, $bundle_name);
    return !empty($settings['index']);
  }

Pawel G's avatar
Pawel G committed
377 378 379
  /**
   * Checks if an entity type is enabled in the sitemap settings.
   *
380
   * @param string $entity_type_id
Pawel G's avatar
Pawel G committed
381
   *
Pawel G's avatar
Pawel G committed
382 383
   * @return bool
   */
384
  public function entityTypeIsEnabled($entity_type_id) {
385
    return in_array($entity_type_id, $this->getSetting('enabled_entity_types', []));
386 387
  }

Pawel G's avatar
Pawel G committed
388
  /**
389
   * Stores a custom path along with its sitemap settings to configuration.
Pawel G's avatar
Pawel G committed
390
   *
Pawel G's avatar
Pawel G committed
391 392
   * @param string $path
   * @param array $settings
Pawel G's avatar
Pawel G committed
393
   *
Pawel G's avatar
Pawel G committed
394
   * @return $this
Pawel G's avatar
Pawel G committed
395
   */
396
  public function addCustomLink($path, $settings) {
Pawel G's avatar
Pawel G committed
397 398 399 400 401 402 403 404
    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
405

406
    $custom_links = $this->getCustomLinks();
Pawel G's avatar
Pawel G committed
407
    foreach ($custom_links as $key => $link) {
408 409 410 411 412 413 414
      if ($link['path'] == $path) {
        $link_key = $key;
        break;
      }
    }
    $link_key = isset($link_key) ? $link_key : count($custom_links);
    $custom_links[$link_key]['path'] = $path;
415 416
    $this->addLinkSettings('custom', $settings, $custom_links[$link_key]); //todo: dirty
    $this->configFactory->getEditable("simple_sitemap.custom")
417
      ->set('links', $custom_links)->save();
Pawel G's avatar
Pawel G committed
418
    return $this;
419 420
  }

421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
  /**
   *
   */
  private function addLinkSettings($type, $settings, &$target) {
    foreach ($settings as $setting_key => $setting) {
      if (in_array($setting_key, self::$allowed_link_settings[$type])) {
        switch ($setting_key) {
          case 'priority':
            if (!FormHelper::isValidPriority($setting)) {
              // todo: log error.
              continue;
            }
            break;

          // todo: add index check.
        }
        $target[$setting_key] = $setting;
      }
    }
  }

  /**
   * Returns an array of custom paths and their sitemap settings.
   *
   * @return array
   */
  public function getCustomLinks() {
    $custom_links = $this->configFactory
      ->get('simple_sitemap.custom')
450
      ->get('links');
451
    return $custom_links !== NULL ? $custom_links : [];
452 453
  }

Pawel G's avatar
Pawel G committed
454 455 456
  /**
   * Returns settings for a custom path added to the sitemap settings.
   *
Pawel G's avatar
Pawel G committed
457
   * @param string $path
Pawel G's avatar
Pawel G committed
458
   *
Pawel G's avatar
Pawel G committed
459
   * @return array|false
Pawel G's avatar
Pawel G committed
460
   */
461
  public function getCustomLink($path) {
462
    foreach ($this->getCustomLinks() as $key => $link) {
463
      if ($link['path'] == $path) {
464
        return $link;
465 466 467 468 469
      }
    }
    return FALSE;
  }

Pawel G's avatar
Pawel G committed
470 471 472
  /**
   * Removes a custom path from the sitemap settings.
   *
Pawel G's avatar
Pawel G committed
473
   * @param string $path
Pawel G's avatar
Pawel G committed
474
   *
Pawel G's avatar
Pawel G committed
475 476
   * @return $this
   */
477
  public function removeCustomLink($path) {
478
    $custom_links = $this->getCustomLinks();
Pawel G's avatar
Pawel G committed
479
    foreach ($custom_links as $key => $link) {
480 481 482
      if ($link['path'] == $path) {
        unset($custom_links[$key]);
        $custom_links = array_values($custom_links);
483
        $this->configFactory->getEditable("simple_sitemap.custom")
484
          ->set('links', $custom_links)->save();
485
        break;
486 487
      }
    }
Pawel G's avatar
Pawel G committed
488
    return $this;
489 490
  }

Pawel G's avatar
Pawel G committed
491 492
  /**
   * Removes all custom paths from the sitemap settings.
Pawel G's avatar
Pawel G committed
493 494
   *
   * @return $this
Pawel G's avatar
Pawel G committed
495
   */
496
  public function removeCustomLinks() {
497
    $this->configFactory->getEditable("simple_sitemap.custom")
498
      ->set('links', [])->save();
Pawel G's avatar
Pawel G committed
499
    return $this;
500 501
  }

502 503 504 505
  /**
   * Returns the whole sitemap, a requested sitemap chunk,
   * or the sitemap index file.
   *
Pawel G's avatar
Pawel G committed
506
   * @param int $chunk_id
507
   *
Pawel G's avatar
Pawel G committed
508
   * @return string|false
Pawel G's avatar
Pawel G committed
509 510 511 512
   *   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.
513
   */
Pawel G's avatar
Pawel G committed
514 515
  public function getSitemap($chunk_id = NULL) {
    $chunks = $this->fetchSitemapChunks();
516
    if (is_null($chunk_id) || !isset($chunks[$chunk_id])) {
517 518

      // Return sitemap index, if there are multiple sitemap chunks.
Pawel G's avatar
Pawel G committed
519 520
      if (count($chunks) > 1) {
        return $this->getSitemapIndex($chunks);
521
      }
Pawel G's avatar
Pawel G committed
522 523
      // Return sitemap if there is only one chunk.
      else {
Pawel G's avatar
Pawel G committed
524 525
        if (isset($chunks[1])) {
          return $chunks[1]->sitemap_string;
526 527
        }
        return FALSE;
528 529
      }
    }
Pawel G's avatar
Pawel G committed
530 531
    // Return specific sitemap chunk.
    else {
Pawel G's avatar
Pawel G committed
532
      return $chunks[$chunk_id]->sitemap_string;
533
    }
534 535
  }

536 537
  /**
   * Generates the sitemap for all languages and saves it to the db.
538 539
   *
   * @param string $from
Pawel G's avatar
Pawel G committed
540 541
   *   Can be 'form', 'cron', 'drush' or 'nobatch'.
   *   This decides how the batch process is to be run.
542
   */
543
  public function generateSitemap($from = 'form') {
544 545 546 547
    $this->sitemapGenerator
      ->setGenerator($this)
      ->setGenerateFrom($from)
      ->startGeneration();
548 549
  }

550
  /**
551 552
   * Generates and returns the sitemap index as string.
   *
Pawel G's avatar
Pawel G committed
553
   * @param array $chunks
Pawel G's avatar
Pawel G committed
554
   *   Sitemap chunks which to generate the index from.
555
   *
556
   * @return string
Pawel G's avatar
Pawel G committed
557
   *   The sitemap index.
558
   */
Pawel G's avatar
Pawel G committed
559
  private function getSitemapIndex($chunks) {
560 561
    return $this->sitemapGenerator
      ->setGenerator($this)
Pawel G's avatar
Pawel G committed
562
      ->generateSitemapIndex($chunks);
Pawel G's avatar
Pawel G committed
563 564
  }

565
  /**
566 567
   * Returns a specific sitemap setting or a default value if setting does not
   * exist.
568 569
   *
   * @param string $name
Pawel G's avatar
Pawel G committed
570
   *   Name of the setting, like 'max_links'.
571
   *
572
   * @param mixed $default
Pawel G's avatar
Pawel G committed
573
   *   Value to be returned if the setting does not exist in the configuration.
574
   *
575
   * @return mixed
576
   *   The current setting from configuration or a default value.
577
   */
578
  public function getSetting($name, $default = FALSE) {
579 580 581 582
    $setting = $this->configFactory
      ->get('simple_sitemap.settings')
      ->get($name);
    return !is_null($setting) ? $setting : $default;
583
  }
584

585
  /**
586
   * Stores a specific sitemap setting in configuration.
587
   *
Pawel G's avatar
Pawel G committed
588
   * @param string $name
Pawel G's avatar
Pawel G committed
589
   *   Setting name, like 'max_links'.
Pawel G's avatar
Pawel G committed
590
   * @param mixed $setting
Pawel G's avatar
Pawel G committed
591
   *   The setting to be saved.
Pawel G's avatar
Pawel G committed
592 593
   *
   * @return $this
594
   */
595
  public function saveSetting($name, $setting) {
596 597
    $this->configFactory->getEditable("simple_sitemap.settings")
      ->set($name, $setting)->save();
Pawel G's avatar
Pawel G committed
598
    return $this;
599
  }
600

Pawel G's avatar
Pawel G committed
601 602
  /**
   * Returns a 'time ago' string of last timestamp generation.
603
   *
Pawel G's avatar
Pawel G committed
604
   * @return string|false
Pawel G's avatar
Pawel G committed
605
   *   Formatted timestamp of last sitemap generation, otherwise FALSE.
Pawel G's avatar
Pawel G committed
606
   */
607
  public function getGeneratedAgo() {
608
    $chunks = $this->fetchSitemapChunks();
Pawel G's avatar
Pawel G committed
609
    if (isset($chunks[1]->sitemap_created)) {
610
      return $this->dateFormatter
611
        ->formatInterval(REQUEST_TIME - $chunks[1]->sitemap_created);
612 613 614
    }
    return FALSE;
  }
615
}