Skip to content

Commit 91ea97e

Browse files
committed
Stricter typing
1 parent d9ff3aa commit 91ea97e

File tree

2 files changed

+109
-58
lines changed

2 files changed

+109
-58
lines changed

xml-sitemap-config-sample.ini

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
[basics]
55
; The directory to check.
6-
; Make sure the directory matches the sitemap directory URL below, otherwise the links to files will be broken!
6+
; Make sure the DIR ends ups in the Sitemap Dir URL below, otherwise the links to files will be broken!
77
directory = "./"
88

99
; With trailing slash!
@@ -13,24 +13,25 @@ directory_url = "https://example.com/"
1313
recursive = true
1414

1515
; The file types you want to add to your XML sitemap.
16+
filetypes[] = html
17+
filetypes[] = php
1618
filetypes[] = pdf
17-
filetypes[] = doc
1819

1920
[advanced]
2021
; The replace array, this works as file => replacement, so 'index.php' => '', would make the index.php be listed as just /
21-
; Note that in the default settings, index.php would never be matched.
2222
replacements[index.php] = ""
2323

2424
; The XSL file used for styling the sitemap output, make sure this path is relative to the root of the site.
2525
xsl = "xml-sitemap.xsl"
2626

2727
; The Change Frequency for files, should probably not be 'never', unless you know for sure you'll never change them again.
28-
; Leave blank to not ouput a change frequency (recommended).
28+
; Leave blank to not output a change frequency (recommended).
2929
changefreq = ""
3030

3131
; The Priority Frequency for files, between 0 and 1.
32-
; Leave blank to not ouput a priority (recommended).
33-
priority = ""
32+
; Set to 0 to not output a priority (recommended).
33+
priority = 0
3434

3535
; Ignore array, all files in this array will be: ignored!
3636
ignore[] = xml-sitemap.php
37+

xml-sitemap.php

+102-52
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,64 @@
66
*/
77

88
class Joost_XML_Sitemap_PHP {
9-
/**
10-
* Holds our configuration.
11-
*/
12-
private array $config = [];
9+
const CONFIG_FILE = 'xml-sitemap-config.ini';
1310

1411
/**
1512
* Holds the output.
1613
*/
1714
private string $output = '';
1815

1916
/**
20-
* Class constructor.
17+
* Holds the path.
18+
*/
19+
private string $path;
20+
21+
/**
22+
* Holds the base URL.
23+
*/
24+
private string $url;
25+
26+
/**
27+
* Array of filetypes we're adding to the XML sitemap.
28+
*/
29+
private array $filetypes;
30+
31+
/**
32+
* Relative path to the XSL file.
33+
*/
34+
private string $xsl = 'xml-sitemap.xsl';
35+
36+
/**
37+
* Files we ignore in our output.
38+
*/
39+
private array $ignore = [];
40+
41+
/**
42+
* The change frequency of the files.
2143
*/
22-
public function __construct() {
44+
private string $changefreq = '';
45+
46+
/**
47+
* The priority of the file, between 0.1 and 1.
48+
*/
49+
private float $priority = 0;
50+
51+
/**
52+
* Determines if we recurse directories or not.
53+
*/
54+
private bool $recursive = true;
55+
56+
/**
57+
* URLs we replace with something else.
58+
*/
59+
private array $replacements;
60+
61+
/**
62+
* Generates our XML sitemap.
63+
*
64+
* @return void
65+
*/
66+
public function generate(): void {
2367
$this->read_ini();
2468
$this->read_dir();
2569
$this->output();
@@ -30,44 +74,34 @@ public function __construct() {
3074
*
3175
* @return void
3276
*/
33-
private function read_ini() {
34-
if ( file_exists( './xml-sitemap-config.ini' ) ) {
35-
$this->config = parse_ini_file( './xml-sitemap-config.ini' );
36-
}
37-
else if ( file_exists( '../xml-sitemap-config.ini' ) ) {
38-
$this->config = parse_ini_file( '../xml-sitemap-config.ini' );
39-
}
40-
else {
41-
echo "Error: unable to load config.php, please copy config-sample.php to config.php and adjust." . PHP_EOL;
77+
private function read_ini(): void {
78+
if ( file_exists( './' . self::CONFIG_FILE ) ) {
79+
$config = parse_ini_file( './' . self::CONFIG_FILE );
80+
} elseif ( file_exists( '../' . self::CONFIG_FILE ) ) {
81+
$config = parse_ini_file( '../' . self::CONFIG_FILE );
82+
} else {
83+
printf( 'Error: unable to load %1$s, please copy xml-sitemap-config-sample.ini to %1$s and adjust.' . PHP_EOL, self::CONFIG_FILE );
4284
die( 1 );
4385
}
44-
}
4586

46-
/**
47-
* Kick off the script reading the dir provided in config.
48-
*
49-
* @return void
50-
*/
51-
private function read_dir() {
52-
$this->parse_dir( $this->config['directory'], $this->config['directory_url'] );
87+
$this->changefreq = (string) $config['changefreq'];
88+
$this->path = (string) $config['directory'];
89+
$this->url = (string) $config['directory_url'];
90+
$this->filetypes = (array) $config['filetypes'];
91+
$this->ignore = array_merge( $config['ignore'], [ '.', '..', 'xml-sitemap.php' ] );
92+
$this->priority = (float) $config['priority'];
93+
$this->recursive = (bool) $config['recursive'];
94+
$this->replacements = (array) $config['replacements'];
95+
$this->xsl = (string) $config['xsl'];
5396
}
5497

5598
/**
56-
* Output our XML sitemap.
99+
* Kick off the script reading the dir provided in config.
57100
*
58101
* @return void
59102
*/
60-
private function output() {
61-
// Sent the correct header so browsers display properly, with or without XSL.
62-
header( 'Content-Type: application/xml' );
63-
64-
echo '<?xml version="1.0" encoding="utf-8"?>' . PHP_EOL;
65-
if ( isset( $this->config['xsl'] ) && ! empty( $this->config['xsl'] ) ) {
66-
echo '<?xml-stylesheet type="text/xsl" href="' . $this->config['directory_url'] . $this->config['xsl'] . '"?>' . PHP_EOL;
67-
}
68-
echo '<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL;
69-
echo $this->output;
70-
echo '</urlset>' . PHP_EOL;
103+
private function read_dir(): void {
104+
$this->parse_dir( $this->path, $this->url );
71105
}
72106

73107
/**
@@ -78,25 +112,22 @@ private function output() {
78112
*
79113
* @return void
80114
*/
81-
private function parse_dir( string $dir, string $url ) {
82-
$ignore = array_merge( $this->config['ignore'], array( '.', '..', 'xml-sitemap.php' ) );
83-
$filetypes = $this->config['filetypes'];
84-
115+
private function parse_dir( string $dir, string $url ): void {
85116
$handle = opendir( $dir );
86117

87118
while ( false !== ( $file = readdir( $handle ) ) ) {
88119
// Check if this file needs to be ignored, if so, skip it.
89-
if ( in_array( utf8_encode( $file ), $ignore ) ) {
120+
if ( in_array( utf8_encode( $file ), $this->ignore ) ) {
90121
continue;
91122
}
92123

93-
if ( $this->config['recursive'] && is_dir( $dir . $file ) ) {
124+
if ( $this->recursive && is_dir( $dir . $file ) ) {
94125
$this->parse_dir( $dir . $file . '/', $url . $file . '/' );
95126
}
96127

97128
// Check whether the file has on of the extensions allowed for this XML sitemap.
98129
$extension = pathinfo( $dir . $file, PATHINFO_EXTENSION );
99-
if ( empty( $extension ) || ! in_array( $extension, $filetypes ) ) {
130+
if ( empty( $extension ) || ! in_array( $extension, $this->filetypes ) ) {
100131
continue;
101132
}
102133

@@ -109,19 +140,19 @@ private function parse_dir( string $dir, string $url ) {
109140
$mod = date( 'c', $file_mod_time );
110141

111142
// Replace the file with its replacement from the settings, if needed.
112-
if ( isset( $this->config['replacements'][ $file ] ) ) {
113-
$file = $this->config['replacements'][ $file ];
143+
if ( isset( $this->replacements[ $file ] ) ) {
144+
$file = $this->replacements[ $file ];
114145
}
115146

116147
// Start creating the output
117-
$output = '<url>' . PHP_EOL;
118-
$output .= "\t" . '<loc>' . $url . rawurlencode( $file ) . '</loc>' . PHP_EOL;
119-
$output .= "\t" . '<lastmod>' . $mod . '</lastmod>' . PHP_EOL;
120-
if ( ! empty( $this->config['changefreq'] ) ) {
121-
$output .= "\t" . '<changefreq>' . $this->config['changefreq'] . '</changefreq>' . PHP_EOL;
148+
$output = '<url>' . PHP_EOL;
149+
$output .= "\t" . '<loc>' . $url . rawurlencode( $file ) . '</loc>' . PHP_EOL;
150+
$output .= "\t" . '<lastmod>' . $mod . '</lastmod>' . PHP_EOL;
151+
if ( ! empty( $this->changefreq ) ) {
152+
$output .= "\t" . '<changefreq>' . $this->changefreq . '</changefreq>' . PHP_EOL;
122153
}
123-
if ( ! empty( $this->config['priority'] ) ) {
124-
$output .= "\t" . '<priority>' . $this->config['priority'] . '</priority>' . PHP_EOL;
154+
if ( $this->priority !== 0 && $this->priority <= 1 ) {
155+
$output .= "\t" . '<priority>' . $this->priority . '</priority>' . PHP_EOL;
125156
}
126157
$output .= '</url>' . PHP_EOL;
127158

@@ -130,6 +161,25 @@ private function parse_dir( string $dir, string $url ) {
130161
}
131162
closedir( $handle );
132163
}
164+
165+
/**
166+
* Output our XML sitemap.
167+
*
168+
* @return void
169+
*/
170+
private function output(): void {
171+
// Sent the correct header so browsers display properly, with or without XSL.
172+
header( 'Content-Type: application/xml' );
173+
174+
echo '<?xml version="1.0" encoding="utf-8"?>' . PHP_EOL;
175+
if ( ! empty( $this->xsl ) ) {
176+
echo '<?xml-stylesheet type="text/xsl" href="' . $this->url . $this->xsl . '"?>' . PHP_EOL;
177+
}
178+
echo '<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL;
179+
echo $this->output;
180+
echo '</urlset>' . PHP_EOL;
181+
}
133182
}
134183

135-
new Joost_XML_Sitemap_PHP();
184+
$joost_xml = new Joost_XML_Sitemap_PHP();
185+
$joost_xml->generate();

0 commit comments

Comments
 (0)