Commit 4d8e92b4 authored by Mike Rockétt's avatar Mike Rockétt 💬
Browse files

Chore: Formatting

parent 3e81275d
......@@ -4,4 +4,4 @@
* Add applicable namespaces to the ProcessWire classLoader.
*/
wire('classLoader')->addNamespace('Rockett\Sitemap', __DIR__ . '/src/Sitemap');
wire('classLoader')->addNamespace('Rockett\Traits', __DIR__ . '/src/Traits');
\ No newline at end of file
wire('classLoader')->addNamespace('Rockett\Traits', __DIR__ . '/src/Traits');
This diff is collapsed.
......@@ -16,170 +16,170 @@ use Rockett\Traits\FieldsTrait as BuildsFields;
class MarkupSitemapConfig extends ModuleConfig
{
use BuildsFields;
/**
* Get default condifguration, automatically passed to input fields.
* @return array
*/
public function getDefaults()
{
return [
// This has been turned on by default due to the fact that
// the default XML output is not rendered properly (not sure why)
'sitemap_stylesheet' => true,
];
use BuildsFields;
/**
* Get default condifguration, automatically passed to input fields.
* @return array
*/
public function getDefaults()
{
return [
// This has been turned on by default due to the fact that
// the default XML output is not rendered properly (not sure why)
'sitemap_stylesheet' => true,
];
}
/**
* Render input fields on config Page.
* @return string
*/
public function getInputFields()
{
// Gather a list of templates
$allTemplates = $this->templates;
foreach ($allTemplates as $template) {
// Exclude system templates
if ($template->flags & Template::flagSystem) {
continue;
}
$templates[] = $template;
}
/**
* Render input fields on config Page.
* @return string
*/
public function getInputFields()
{
// Gather a list of templates
$allTemplates = $this->templates;
foreach ($allTemplates as $template) {
// Exclude system templates
if ($template->flags & Template::flagSystem) {
continue;
}
$templates[] = $template;
}
// If saving, remove the sitemap cache to effect
// possible changes in configuration.
$this->input->post->submit_save_module &&
$this->removeSitemapCache() &&
$this->message($this->_('Removed sitemap cache'));
// Start inputfields
$inputfields = parent::getInputfields();
// Add the template-selector field
$includeTemplatesField = $this->buildInputField('AsmSelect', [
'name+id' => 'sitemap_include_templates',
'label' => $this->_('Templates with sitemap options'),
'description' => $this->_('Select which templates (and, therefore, all pages assigned to those templates) can have individual sitemap options. These options (shown in the Settings tab of the page editor) allow you to set which pages and, optionally, their children should be excluded from the sitemap when it is rendered; define which page’s images should not be included in the sitemap (provided that image fields have been added below); and, lastly, set an optional priority for each page.'),
'notes' => $this->_("**Removal/Restoration:** Removing a template from this list will not delete any page options applicable to it. However, they will also not be read when rendering the sitemap. As such, when restoring a template to this list after having removed it, any previous options saved for a page that uses this template will be used when rendering the sitemap. The only time sitemap options are deleted is when either the page in question is completely deleted after having been trashed, or when the module is uninstalled.\n\n**A note about the home page: ** This page cannot be excluded from the sitemap. As such, the applicable exclusion options will not be available when editing it."),
'icon' => 'cubes',
]);
foreach ($templates as $template) {
$includeTemplatesField->addOption($template->name, $template->get('label|name'));
}
$inputfields->add($includeTemplatesField);
// Add the template-selector field that disables template access to the module.
// The home template cannot be added to the exclusions list.
$excludeTemplatesField = $this->buildInputField('AsmSelect', [
'name+id' => 'sitemap_exclude_templates',
'label' => $this->_('Templates without sitemap access'),
'description' => $this->_('Select which templates (and, therefore, all pages assigned to those templates) should not have sitemap access.'),
'notes' => $this->_('**Note:** Adding a template to this list overrides template-level functionality defined above. If a template is listed here, its pages will not have access to any sitemap functionality, including options, and will not be included in the rendered sitemap. However, the template will not be removed from the options list above, in the case that you wish to easily restore it. As such, this is a non-destructive configuration option.'),
'icon' => 'remove',
'collapsed' => Inputfield::collapsedBlank,
]);
foreach ($templates as $template) {
if ($template->id !== 1) {
$excludeTemplatesField->addOption($template->name, $template->get('label|name'));
}
}
$inputfields->add($excludeTemplatesField);
// Add the image-field-selector field if image fields exist
if ($imageFields = $this->fields->find('type=FieldtypeImage|FieldtypeCroppableImage3') and $imageFields->count) {
$imageFieldsField = $this->buildInputField('AsmSelect', [
'name+id' => 'sitemap_image_fields',
'label' => $this->_('Image fields'),
'description' => $this->_('If you’d like to include images in your sitemap (for somewhat enhanced Google Images support), specify the image fields you’d like MarkupSitemap to traverse and include. The sitemap will include images for every page that uses the field(s) you select below, except for pages that are set to not have their images included.'),
'icon' => 'image',
'collapsed' => Inputfield::collapsedBlank,
]);
foreach ($imageFields as $field) {
$imageFieldsField->addOption($field->name, "{$field->get('label|name')} (used in {$field->numFieldgroups()} templates)");
}
$inputfields->add($imageFieldsField);
}
// Add the default-language iso text field
if ($this->siteUsesLanguageSupportPageNames()) {
$inputfields->add($this->buildInputField('Text', [
'name+id' => 'sitemap_default_iso',
'label' => $this->_('ISO code for default language'),
'description' => $this->_('If you’ve set your home page to not include a language ISO (default language name) via LanguageSupportPageNames **and** your home page’s default language name is empty, then you can set an ISO code here for the default language that will appear in the sitemap. This will prevent the sitemap from containing `hreflang="home"` for all default-language URLs.'),
'notes' => $this->_('Note that if your home page has a name for the default language, then this option will not take any effect.'),
'placeholder' => $this->_('Example: en'),
'icon' => 'language',
'collapsed' => Inputfield::collapsedBlank,
]));
}
// Create the stylesheet fieldset
$stylesheetFieldset = $this->buildInputField('Fieldset', [
'label' => $this->_('Stylesheet'),
'collapsed' => Inputfield::collapsedBlank,
'icon' => 'css3',
]);
// Add the stylesheet checkbox
$stylesheetFieldset->add($this->buildInputField('Checkbox', [
'name+id' => 'sitemap_stylesheet',
// 'label' => $this->_('Sitemap Stylesheet'),
'label' => $this->_('Add a stylesheet to the sitemap'),
]));
// Add the custom stylesheet text field
$stylesheetFieldset->add($this->buildInputField('Text', [
'name+id' => 'sitemap_stylesheet_custom',
'label' => $this->_('Custom Stylesheet'),
'description' => $this->_('If you would like to use your own stylesheet, enter the absolute URL to its file here.'),
'placeholder' => $this->_('Example: https://example.tld/assets/sitemap-stylesheet.xsl'),
'showIf' => 'sitemap_stylesheet=1',
'notes' => $this->_('The default stylesheet is located at **assets/sitemap-stylesheet.xsl** in the module’s directory. If you leave this field blank or your input is not a valid URL, the default will be used.'),
'icon' => 'file-o',
'collapsed' => Inputfield::collapsedBlank,
]));
// Add the stylesheet fieldset to the inputfields
$inputfields->add($stylesheetFieldset);
// Add the support-development markup field
$supportText = $this->wire('sanitizer')->entitiesMarkdown($this->_('Sitemap is proudly [open-source](http://opensource.com/resources/what-open-source) and is [free to use](https://en.wikipedia.org/wiki/Free_software) for personal and commercial projects. Please consider [making a small donation](https://rockett.pw/donate) in support of the development of MarkupSitemap and other modules.'), ['fullMarkdown' => true]);
$inputfields->add($this->buildInputField('Markup', [
'id' => 'support_development',
'label' => $this->_('Support Development'),
'value' => $supportText,
'icon' => 'paypal',
'collapsed' => Inputfield::collapsedYes,
]));
$this->config->scripts->add($this->urls->httpSiteModules . 'MarkupSitemap/assets/scripts/config.js');
return $inputfields;
// If saving, remove the sitemap cache to effect
// possible changes in configuration.
$this->input->post->submit_save_module &&
$this->removeSitemapCache() &&
$this->message($this->_('Removed sitemap cache'));
// Start inputfields
$inputfields = parent::getInputfields();
// Add the template-selector field
$includeTemplatesField = $this->buildInputField('AsmSelect', [
'name+id' => 'sitemap_include_templates',
'label' => $this->_('Templates with sitemap options'),
'description' => $this->_('Select which templates (and, therefore, all pages assigned to those templates) can have individual sitemap options. These options (shown in the Settings tab of the page editor) allow you to set which pages and, optionally, their children should be excluded from the sitemap when it is rendered; define which page’s images should not be included in the sitemap (provided that image fields have been added below); and, lastly, set an optional priority for each page.'),
'notes' => $this->_("**Removal/Restoration:** Removing a template from this list will not delete any page options applicable to it. However, they will also not be read when rendering the sitemap. As such, when restoring a template to this list after having removed it, any previous options saved for a page that uses this template will be used when rendering the sitemap. The only time sitemap options are deleted is when either the page in question is completely deleted after having been trashed, or when the module is uninstalled.\n\n**A note about the home page: ** This page cannot be excluded from the sitemap. As such, the applicable exclusion options will not be available when editing it."),
'icon' => 'cubes',
]);
foreach ($templates as $template) {
$includeTemplatesField->addOption($template->name, $template->get('label|name'));
}
$inputfields->add($includeTemplatesField);
// Add the template-selector field that disables template access to the module.
// The home template cannot be added to the exclusions list.
$excludeTemplatesField = $this->buildInputField('AsmSelect', [
'name+id' => 'sitemap_exclude_templates',
'label' => $this->_('Templates without sitemap access'),
'description' => $this->_('Select which templates (and, therefore, all pages assigned to those templates) should not have sitemap access.'),
'notes' => $this->_('**Note:** Adding a template to this list overrides template-level functionality defined above. If a template is listed here, its pages will not have access to any sitemap functionality, including options, and will not be included in the rendered sitemap. However, the template will not be removed from the options list above, in the case that you wish to easily restore it. As such, this is a non-destructive configuration option.'),
'icon' => 'remove',
'collapsed' => Inputfield::collapsedBlank,
]);
foreach ($templates as $template) {
if ($template->id !== 1) {
$excludeTemplatesField->addOption($template->name, $template->get('label|name'));
}
}
$inputfields->add($excludeTemplatesField);
// Add the image-field-selector field if image fields exist
if ($imageFields = $this->fields->find('type=FieldtypeImage|FieldtypeCroppableImage3') and $imageFields->count) {
$imageFieldsField = $this->buildInputField('AsmSelect', [
'name+id' => 'sitemap_image_fields',
'label' => $this->_('Image fields'),
'description' => $this->_('If you’d like to include images in your sitemap (for somewhat enhanced Google Images support), specify the image fields you’d like MarkupSitemap to traverse and include. The sitemap will include images for every page that uses the field(s) you select below, except for pages that are set to not have their images included.'),
'icon' => 'image',
'collapsed' => Inputfield::collapsedBlank,
]);
foreach ($imageFields as $field) {
$imageFieldsField->addOption($field->name, "{$field->get('label|name')} (used in {$field->numFieldgroups()} templates)");
}
$inputfields->add($imageFieldsField);
}
/**
* Remove the sitemap cache
* @return bool
*/
protected function removeSitemapCache()
{
try {
$cachePath = $this->config->paths->cache . 'MarkupCache/MarkupSitemap';
$removed = (bool) CacheFile::removeAll($cachePath, true);
} catch (\Exception $e) {
$removed = false;
}
return $removed;
// Add the default-language iso text field
if ($this->siteUsesLanguageSupportPageNames()) {
$inputfields->add($this->buildInputField('Text', [
'name+id' => 'sitemap_default_iso',
'label' => $this->_('ISO code for default language'),
'description' => $this->_('If you’ve set your home page to not include a language ISO (default language name) via LanguageSupportPageNames **and** your home page’s default language name is empty, then you can set an ISO code here for the default language that will appear in the sitemap. This will prevent the sitemap from containing `hreflang="home"` for all default-language URLs.'),
'notes' => $this->_('Note that if your home page has a name for the default language, then this option will not take any effect.'),
'placeholder' => $this->_('Example: en'),
'icon' => 'language',
'collapsed' => Inputfield::collapsedBlank,
]));
}
/**
* Determine if the site uses the LanguageSupportPageNames module.
* @return bool
*/
protected function siteUsesLanguageSupportPageNames()
{
return $this->modules->isInstalled('LanguageSupportPageNames');
// Create the stylesheet fieldset
$stylesheetFieldset = $this->buildInputField('Fieldset', [
'label' => $this->_('Stylesheet'),
'collapsed' => Inputfield::collapsedBlank,
'icon' => 'css3',
]);
// Add the stylesheet checkbox
$stylesheetFieldset->add($this->buildInputField('Checkbox', [
'name+id' => 'sitemap_stylesheet',
// 'label' => $this->_('Sitemap Stylesheet'),
'label' => $this->_('Add a stylesheet to the sitemap'),
]));
// Add the custom stylesheet text field
$stylesheetFieldset->add($this->buildInputField('Text', [
'name+id' => 'sitemap_stylesheet_custom',
'label' => $this->_('Custom Stylesheet'),
'description' => $this->_('If you would like to use your own stylesheet, enter the absolute URL to its file here.'),
'placeholder' => $this->_('Example: https://example.tld/assets/sitemap-stylesheet.xsl'),
'showIf' => 'sitemap_stylesheet=1',
'notes' => $this->_('The default stylesheet is located at **assets/sitemap-stylesheet.xsl** in the module’s directory. If you leave this field blank or your input is not a valid URL, the default will be used.'),
'icon' => 'file-o',
'collapsed' => Inputfield::collapsedBlank,
]));
// Add the stylesheet fieldset to the inputfields
$inputfields->add($stylesheetFieldset);
// Add the support-development markup field
$supportText = $this->wire('sanitizer')->entitiesMarkdown($this->_('Sitemap is proudly [open-source](http://opensource.com/resources/what-open-source) and is [free to use](https://en.wikipedia.org/wiki/Free_software) for personal and commercial projects. Please consider [making a small donation](https://rockett.pw/donate) in support of the development of MarkupSitemap and other modules.'), ['fullMarkdown' => true]);
$inputfields->add($this->buildInputField('Markup', [
'id' => 'support_development',
'label' => $this->_('Support Development'),
'value' => $supportText,
'icon' => 'paypal',
'collapsed' => Inputfield::collapsedYes,
]));
$this->config->scripts->add($this->urls->httpSiteModules . 'MarkupSitemap/assets/scripts/config.js');
return $inputfields;
}
/**
* Remove the sitemap cache
* @return bool
*/
protected function removeSitemapCache()
{
try {
$cachePath = $this->config->paths->cache . 'MarkupCache/MarkupSitemap';
$removed = (bool) CacheFile::removeAll($cachePath, true);
} catch (\Exception $e) {
$removed = false;
}
return $removed;
}
/**
* Determine if the site uses the LanguageSupportPageNames module.
* @return bool
*/
protected function siteUsesLanguageSupportPageNames()
{
return $this->modules->isInstalled('LanguageSupportPageNames');
}
}
......@@ -18,213 +18,214 @@ use Rockett\Sitemap\SubElements\Link;
trait BuilderTrait
{
/**
* Current UrlSet
*
* @var Urlset
*/
protected $urlSet;
/**
* Add alternative languges, including current.
* @param Page $page
* @param Url $url
*/
protected function addAltLanguages($page, $url)
{
foreach ($this->languages as $altLanguage) {
if ($this->pageLanguageInvalid($altLanguage, $page)) {
continue;
}
if ($altLanguage->isDefault()
&& $this->pages->get(1)->name === 'home'
&& !$this->modules->LanguageSupportPageNames->useHomeSegment
&& !empty($this->sitemap_default_iso)) {
$languageIsoName = $this->sitemap_default_iso;
} else {
$languageIsoName = $this->pages->get(1)->localName($altLanguage);
}
$url->addSubElement(new Link($languageIsoName, $page->localHttpUrl($altLanguage)));
}
/**
* Current UrlSet
*
* @var Urlset
*/
protected $urlSet;
/**
* Add alternative languges, including current.
* @param Page $page
* @param Url $url
*/
protected function addAltLanguages($page, $url)
{
foreach ($this->languages as $altLanguage) {
if ($this->pageLanguageInvalid($altLanguage, $page)) {
continue;
}
if ($altLanguage->isDefault()
&& $this->pages->get(1)->name === 'home'
&& !$this->modules->LanguageSupportPageNames->useHomeSegment
&& !empty($this->sitemap_default_iso)) {
$languageIsoName = $this->sitemap_default_iso;
} else {
$languageIsoName = $this->pages->get(1)->localName($altLanguage);
}
$url->addSubElement(new Link($languageIsoName, $page->localHttpUrl($altLanguage)));
}
/**
* Generate an image tag for the current image in the loop
* @param Pageimage $image
* @param Language $language
* @return Image
*/
protected function addImage($image, $language = null)
{
$locImage = new Image($image->httpUrl);
foreach (self::IMAGE_FIELDS as $imageMetaMethod => $imageMetaValues) {
foreach (explode('|', $imageMetaValues) as $imageMetaValue) {
if ($language != null && !$language->isDefault() && $image->{"$imageMetaValue{$language->id}"}) {
$imageMetaValue .= $language->id;
}
if ($image->$imageMetaValue) {
if ($imageMetaMethod === 'License') {
// Skip invalid licence URLs
if (!filter_var($image->$imageMetaValue, FILTER_VALIDATE_URL)) {
continue;
}
}
$locImage->{"set{$imageMetaMethod}"}($image->$imageMetaValue);
}
}
/**
* Generate an image tag for the current image in the loop
* @param Pageimage $image
* @param Language $language
* @return Image
*/
protected function addImage($image, $language = null)
{
$locImage = new Image($image->httpUrl);
foreach (self::IMAGE_FIELDS as $imageMetaMethod => $imageMetaValues) {
foreach (explode('|', $imageMetaValues) as $imageMetaValue) {
if ($language != null && !$language->isDefault() && $image->{"$imageMetaValue{$language->id}"}) {
$imageMetaValue .= $language->id;
}
if ($image->$imageMetaValue) {
if ($imageMetaMethod === 'License') {
// Skip invalid licence URLs
if (!filter_var($image->$imageMetaValue, FILTER_VALIDATE_URL)) {
continue;
}
}
$locImage->{"set{$imageMetaMethod}"}($image->$imageMetaValue);
}
return $locImage;
}
}
/**
* Add images to the current Url
* @param Url $url
* @param Language $language
*/
protected function addImages($page, $url, $language = null)
{
// Loop through declared image fields and skip non image fields
if ($this->sitemap_image_fields) {
foreach ($this->sitemap_image_fields as $imageFieldName) {
$page->of(false);
$imageField = $page->$imageFieldName;
if ($imageField) {
foreach ($imageField as $image) {
if ($image instanceof Pageimage || $image instanceof \ProcessWire\Pageimage) {
$url->addSubElement($this->addImage($image, $language));
}
}
}
return $locImage;
}
/**
* Add images to the current Url
* @param Url $url
* @param Language $language
*/
protected function addImages($page, $url, $language = null)
{
// Loop through declared image fields and skip non image fields
if ($this->sitemap_image_fields) {
foreach ($this->sitemap_image_fields as $imageFieldName) {
$page->of(false);
$imageField = $page->$imageFieldName;
if ($imageField) {
foreach ($imageField as $image) {
if ($image instanceof Pageimage || $image instanceof \ProcessWire\Pageimage) {
$url->addSubElement($this->addImage($image, $language));
}
}
}
}
}
}
/**
* Determine if a page can be included in the sitemap
* @param $page
* @param $options
* @return bool
*/
public function pageIsIncludible($page, $options)
{
// If it's the home page, it's always includible.
if ($page->id === 1) {
return true;
}
/**
* Determine if a page can be included in the sitemap
* @param $page
* @param $options
* @return bool
*/
public function pageIsIncludible($page, $options)
{
// If it's the home page, it's always includible.
if ($page->id === 1) {
return true;
}
// If the page's template is excluded from accessing Sitemap,
// then it's not includible.
if (in_array($page->template->name, $this->sitemap_exclude_templates)) {
return false;
}
// If the page's template is excluded from accessing Sitemap,
// then it's not includible.
if (in_array($page->template->name, $this->sitemap_exclude_templates)) {
return false;
}
// Otherwise, check to see if the page itself has been excluded
// via Sitemap options.
return !$options['excludes']['page'];
// Otherwise, check to see if the page itself has been excluded
// via Sitemap options.
return !$options['excludes']['page'];
}
/**
* Recursively add pages in each language with
* alternate language and image sub-elements.
* @param $page
*/
protected function addPages($page)
{
// Get the saved options for this page
$pageSitemapOptions = $this->modules->getConfig($this, "o{$page->id}");
// If the template that this page belongs to is not using sitemap options
// (per the module's current configuration), then we need to revert the keys
// in $pageSitemapOptions to their defaults so as to prevent their
// saved options from being used in this cycle.
if ($this->sitemap_include_templates !== null
&& !in_array($page->template->name, $this->sitemap_include_templates)
&& is_array($pageSitemapOptions)) {
array_walk_recursive($pageSitemapOptions, function (&$value) {
$value = false;
});
}
/**
* Recursively add pages in each language with
* alternate language and image sub-elements.
* @param $page
*/
protected function addPages($page)
{
// Get the saved options for this page
$pageSitemapOptions = $this->modules->getConfig($this, "o{$page->id}");
// If the template that this page belongs to is not using sitemap options
// (per the module's current configuration), then we need to revert the keys
// in $pageSitemapOptions to their defaults so as to prevent their
// saved options from being used in this cycle.
if ($this->sitemap_include_templates !== null
&& !in_array($page->template->name, $this->sitemap_include_templates)
&& is_array($pageSitemapOptions)) {
array_walk_recursive($pageSitemapOptions, function (&$value) {
$value = false;
});
// If the page is viewable and not excluded or we’re working with the root page,
// begin generating the sitemap by adding pages recursively. (Root is always added.)
if ($page->viewable() && $this->pageIsIncludible($page, $pageSitemapOptions)) {
// If language support is enabled, then we need to loop through each language
// to generate <loc> for each language with all alternates, including the
// current language. Then add image references with multi-language support.
if ($this->siteUsesLanguageSupportPageNames()) {