Commit 544f094e authored by gbyte.co's avatar gbyte.co Committed by gbyte.co

Search engine submission clean up

parent d0e208c0
This module adds additional functionality to the Simple XML Sitemap module
(https://www.drupal.org/project/simple_sitemap), providing the ability to
automatically submit generated sitemaps to search engines. Similar submission
functionality exists in the XML Sitemap module, but this module provides the
ability to take advantage of the multilingual capabilities of Simple XML Sitemap
and still be able to submit sitemaps automatically.
Settings for which search engines to submit to and submission frequency may be
found at admin/config/search/simplesitemap/engines.
......@@ -25,6 +25,6 @@ simple_sitemap_engines.settings:
enabled:
type: boolean
label: 'Sitemap submission enabled'
frequency:
submission_interval:
type: integer
label: 'Sitemap submission frequency'
name: 'Simple XML Sitemap - Search engines'
name: 'Simple XML Sitemap (Search engines)'
type: module
description: 'Submits sitemaps to search engines.'
configure: simple_sitemap_engines.settings
package: SEO
core: 8.x
dependencies:
......
simple_sitemap_engines.settings:
route_name: simple_sitemap_engines.settings
simple_sitemap_engines:
route_name: entity.simple_sitemap_engine.status
title: 'Search engines'
base_route: simple_sitemap.settings
weight: 5
simple_sitemap_engines.status:
route_name: entity.simple_sitemap_engine.status
title: 'Status'
parent_id: simple_sitemap_engines
weight: 1
simple_sitemap_engines.settings:
route_name: simple_sitemap_engines.settings
title: 'Settings'
parent_id: simple_sitemap_engines
weight: 2
......@@ -3,9 +3,6 @@
/**
* @file
* Submits sitemaps to search engines.
*
* (c) 2019 Brad Greco.
* This module may be distributed under the terms of GPL version 2.0 or later.
*/
/**
......@@ -17,21 +14,27 @@
* @see Drupal\simple_sitemap_engines\Plugin\QueueWorker\SitemapSubmitter
*/
function simple_sitemap_engines_cron() {
/** @var \Drupal\Core\Config\Config $config */
$config = \Drupal::config('simple_sitemap_engines.settings');
if ($config->get('enabled')) {
$submit_frequency = $config->get('frequency');
$last_submitted = \Drupal::state()->get('simple_sitemap_engines_last_submitted', 0);
$interval = (int) $config->get('submission_interval') * 60 * 60;
$request_time = \Drupal::service('datetime.time')->getRequestTime();
$state = \Drupal::state();
if ($interval === 0
|| $state->get('simple_sitemap_engines_last_submitted', 0) + $interval <= $request_time) {
/** @var \Drupal\Core\Queue\QueueInterface $queue */
$queue = \Drupal::queue('simple_sitemap_engine_submit');
if ($submit_frequency > 0 && $last_submitted + $submit_frequency < time()) {
// Load only the enabled search engines.
$engines = \Drupal::entityTypeManager()
$state->set('simple_sitemap_engines_last_submitted', $request_time);
foreach (\Drupal::entityTypeManager()
->getStorage('simple_sitemap_engine')
->loadByProperties(['status' => TRUE]);
// Add them to the submission queue.
foreach ($engines as $id => $engine) {
\Drupal::queue('simple_sitemap_engine_submit')->createItem($id);
->loadByProperties(['status' => TRUE]) as $id => $engine) {
$queue->createItem($id);
}
\Drupal::state()->set('simple_sitemap_engines_last_submitted', time());
}
}
}
simple_sitemap_engines.settings:
entity.simple_sitemap_engine.status:
path: '/admin/config/search/simplesitemap/engines'
defaults:
_form: '\Drupal\simple_sitemap_engines\Form\SimplesitemapEnginesForm'
_entity_list: 'simple_sitemap_engine'
_title: 'Simple XML Sitemap Settings'
requirements:
_permission: 'administer sitemap settings'
entity.simple_sitemap_engine.list:
path: '/admin/config/search/simplesitemap/engines/list'
simple_sitemap_engines.settings:
path: '/admin/config/search/simplesitemap/engines/settings'
defaults:
_entity_list: simple_sitemap_engine
_title: 'Search engines'
_form: '\Drupal\simple_sitemap_engines\Form\SimplesitemapEnginesForm'
_title: 'Simple XML Sitemap Settings'
requirements:
_permission: 'administer sitemap settings'
......@@ -7,6 +7,7 @@ use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\simple_sitemap\Form\FormHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -74,4 +75,13 @@ class SearchEngineListBuilder extends ConfigEntityListBuilder {
return $row;
}
public function render() {
return ['simple_sitemap_engines' => [
'#prefix' => FormHelper::getDonationText(),
'#title' => $this->t('Submission status'),
'#type' => 'fieldset',
'table' => parent::render(),
]];
}
}
......@@ -7,7 +7,7 @@ use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\simple_sitemap\Form\FormHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -80,31 +80,16 @@ class SimplesitemapEnginesForm extends ConfigFormBase {
$engine_statuses = array_column($engines, 'status', 'id');
$enabled_engines = array_keys(array_filter($engine_statuses));
// Build the frequency options, in the form [interval => label].
$frequency_options = array_flip([
10800,
21600,
43200,
86400,
259200,
604800,
]);
foreach ($frequency_options as $frequency => &$label) {
$label = $this->dateFormatter->formatInterval($frequency);
}
$form['engine_group'] = [
'#type' => 'fieldset',
'#title' => $this->t('Search engines'),
'#prefix' => FormHelper::getDonationText(),
];
$form['engine_group']['engines'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Submit the sitemap to the following engines'),
'#options' => $engines,
'#default_value' => $enabled_engines,
'#description' => $this->t('Details about each search engine can be seen <a href="@url">here</a>.', [
'@url' => Url::fromRoute('entity.simple_sitemap_engine.list')->toString(),
]),
];
$form['settings'] = [
......@@ -113,15 +98,16 @@ class SimplesitemapEnginesForm extends ConfigFormBase {
];
$form['settings']['enabled'] = [
'#type' => 'checkbox',
'#title' => $this->t('Submit the sitemap to search engines every:'),
'#title' => $this->t('Submit the sitemap to search engines'),
'#default_value' => $config->get('enabled'),
];
$form['settings']['frequency'] = [
$form['settings']['submission_interval'] = [
'#type' => 'select',
'#options' => $frequency_options,
'#default_value' => $config->get('frequency'),
'#title' => $this->t('Submission interval'),
'#options' => FormHelper::getCronIntervalOptions(),
'#default_value' => $config->get('submission_interval'),
'#states' => [
'enabled' => [':input[name="enabled"]' => ['checked' => TRUE]],
'visible' => [':input[name="enabled"]' => ['checked' => TRUE]],
],
];
......@@ -139,7 +125,7 @@ class SimplesitemapEnginesForm extends ConfigFormBase {
}
$config = $this->config('simple_sitemap_engines.settings');
$config->set('enabled', $form_state->getValue('enabled'));
$config->set('frequency', $form_state->getValue('frequency'));
$config->set('submission_interval', $form_state->getValue('submission_interval'));
$config->save();
}
......
This module uses PHPUnit functional tests to follow drupal.org standards.
To run tests, execute the command below in the web root of your Drupal site,
where 'www-data' is the name of the user your web server runs as, and
'http://localhost/drupal8/web' is the URL to the root of your site.
sudo -u www-data php core/scripts/run-tests.sh --verbose --sqlite /tmp/test.sqlite --url http://localhost/drupal8/web simple_sitemap_engines
If you receive an error that sqlite is missing, install the php-sqlite3 Debian
package.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment