Simple Wp Sitemap - Version 1.0.8

Version Description

(April 29, 2015) = * Now generates dynamic sitemaps instead of static files * Tested with caching plugins (the popular ones) and works * Compressed html sitemap to reduce filesize/load time * Changed mouse hover effect in html sitemap * Fixed couple bugs/collisions with other plugins * Updated descriptions

Download this release

Release Info

Developer Webbjocke
Plugin Icon 128x128 Simple Wp Sitemap
Version 1.0.8
Comparing to
See all releases

Code changes from version 1.0.7 to 1.0.8

Files changed (4) hide show
  1. css/html.css +1 -0
  2. readme.txt +23 -15
  3. simple-wp-sitemap.php +73 -19
  4. simpleWpMapBuilder.php +76 -98
css/html.css CHANGED
@@ -23,6 +23,7 @@ a{
23
  }
24
  a:hover{
25
  background: #f8f8f8;
 
26
  }
27
  a:active, a:focus{
28
  color: #0000ff;
23
  }
24
  a:hover{
25
  background: #f8f8f8;
26
+ text-decoration: underline;
27
  }
28
  a:active, a:focus{
29
  color: #0000ff;
readme.txt CHANGED
@@ -4,22 +4,22 @@ License: GPLv3
4
  License URI: http://www.gnu.org/licenses/gpl.html
5
  Tags: sitemap, google sitemap, xml, simple sitemap, html, xml sitemap, html sitemap, seo, seo sitemap
6
  Requires at least: 4.0
7
- Tested up to: 4.2
8
- Stable tag: 1.0.7
9
 
10
  An easy, fast and secure plugin that adds both an xml and an html sitemap to your site, which updates and maintains themselves so you dont have to!
11
 
12
  == Description ==
13
 
14
- Simple Wp Sitemap is a plugin that creates and adds both an xml and an html sitemap to your page as static files. These two are updated automatically everytime a post or page is created, edited or deleted, and makes sure they're easily indexed. What this means you only have to install and activate the plugin once, and it will just work for you without you ever having to worry.
15
 
16
- Reason the sitemaps are created as static files and aren't generated everytime someone's visiting them, is because of the awesome performance that comes with not having to do database queries and php rendering all day. Sure you have to wait a couple milliseconds extra when you create a new or edit a post, but in my opinion that's totally worth it!
17
 
18
- Also supports the option to add pages to the sitemaps that aren't part of your original wordpress site. For instance if you create a little html file and upload to your server and want it to be included in them, it's easily done. You can also block pages that you don't want to be included.
19
 
20
- So what the plugin actually does is creating two files, one sitemap.xml and one sitemap.html. These two becomes available directly on your site at like yourpage.com/sitemap.xml and yourpage.com/sitemap.html.
21
 
22
- And yes, of course the sitemaps are mobile friendly :)
23
 
24
  == Installation ==
25
 
@@ -27,7 +27,7 @@ And yes, of course the sitemaps are mobile friendly :)
27
  2. Either search for "simple-wp-sitemap" and click install, or hit "upload plugin" and upload the zip file.
28
  3. Another way is by just uploading the "simple-wp-sitemap" folder via ftp to the /wp-content/plugins/ directory.
29
 
30
- 2. Activate the plugin and thats it, done. The two sitemaps have now been created and can be found at like yourpage.com/sitemap.xml and yourpage.com/sitemap.html.
31
 
32
  3. Customize the plugin and add/block pages by hitting the "Simple Wp Sitemap" option in the settings menu.
33
 
@@ -35,19 +35,23 @@ And yes, of course the sitemaps are mobile friendly :)
35
 
36
  = I have installed the plugin, where can I find the sitemaps? =
37
 
38
- They are located in your sites so called "home directory", you can find them at like yourpage.com/sitemap.xml and yourpage.com/sitemap.html. There's also links to them from the plugins customization page in your admin area.
 
 
 
 
39
 
40
  = Where can I find the customization or admin page for the plugin? =
41
 
42
  Click the link called "Simple Wp Sitemap" in your admin areas settings menu and it will take you there. Theres also a link from the plugins page, where you activate and deactivate them etc.
43
 
44
- = Does it create both an xml and an html sitemap? =
45
 
46
  Yes sir, it does.
47
 
48
  = Is it possible to add the sitemaps anywhere else on my site? Like on a page with a shortcode or something? =
49
 
50
- Sorry no, not at the moment it isn't.
51
 
52
  = Which one of the sitemaps should I submit to google and to other search engines? =
53
 
@@ -57,10 +61,6 @@ The sitemap.xml one.
57
 
58
  Not really, search engines usually finds them automatically when they visit your site. However if you have webmaster tools at google or bing etc, that could be a good place to do it to get statistics over indexed pages and stuff.
59
 
60
- = Are the sitemaps created "on the fly" dynamically or as static files? =
61
-
62
- As static files. They get updated everytime you create, edit or delete a post or page. And also when changes are made in the admin area.
63
-
64
  = How do I remove the sitemaps if I stop using the plugin? =
65
 
66
  When you deactivate the plugin they get removed automatically.
@@ -73,6 +73,14 @@ When you deactivate the plugin they get removed automatically.
73
 
74
  == Changelog ==
75
 
 
 
 
 
 
 
 
 
76
  = 1.0.7 (April 18, 2015) =
77
  * Added option to specify own display order
78
  * Excluded drafts, private and password protected pages
4
  License URI: http://www.gnu.org/licenses/gpl.html
5
  Tags: sitemap, google sitemap, xml, simple sitemap, html, xml sitemap, html sitemap, seo, seo sitemap
6
  Requires at least: 4.0
7
+ Tested up to: 4.2.1
8
+ Stable tag: 1.0.8
9
 
10
  An easy, fast and secure plugin that adds both an xml and an html sitemap to your site, which updates and maintains themselves so you dont have to!
11
 
12
  == Description ==
13
 
14
+ Simple Wp Sitemap is a plugin that generates both an xml and an html sitemap to your site dynamically. These two are updated automatically everytime someone's visiting them, and makes sure they're easily indexed. What this means you only have to install and activate the plugin once, and it will just work for you without you ever having to worry.
15
 
16
+ Reason the sitemaps are generated dynamically instead of being created as actual files as they were in the beginning, is because it's much faster and more solid. No problems with having to access, create and delete files, instead they're just presented when needed to. It's now suddenly as lightweight, simple and fast as one can expect!
17
 
18
+ Also supports the option to add pages to the sitemaps that aren't part of your original wordpress site. For instance if you create a little html file and upload to your server and want it to be included in them, it's easily done. You can also block pages that you don't want to be included, and much more.
19
 
20
+ So what the plugin actually does is making one sitemap.xml and one sitemap.html available directly on your site. These aren't actually real files that can be found in a folder or something, instead they get generated when visited and can be found at like yourpage.com/sitemap.xml and yourpage.com/sitemap.html.
21
 
22
+ And yes, of course the sitemaps are mobile friendly. They also work well with caching plugins :)
23
 
24
  == Installation ==
25
 
27
  2. Either search for "simple-wp-sitemap" and click install, or hit "upload plugin" and upload the zip file.
28
  3. Another way is by just uploading the "simple-wp-sitemap" folder via ftp to the /wp-content/plugins/ directory.
29
 
30
+ 2. Activate the plugin and thats it, done. The two sitemaps will be generated when visited, and can be found at like yourpage.com/sitemap.xml and yourpage.com/sitemap.html.
31
 
32
  3. Customize the plugin and add/block pages by hitting the "Simple Wp Sitemap" option in the settings menu.
33
 
35
 
36
  = I have installed the plugin, where can I find the sitemaps? =
37
 
38
+ You can find them at like yourpage.com/sitemap.xml and yourpage.com/sitemap.html. There's also links to them from the plugins customization page in your admin area.
39
+
40
+ = Are the sitemaps created "on the fly" dynamically or as static files? =
41
+
42
+ Dynamically! From version 1.0.8 they now get generated when someone's visiting them, rather than being created as actual files.
43
 
44
  = Where can I find the customization or admin page for the plugin? =
45
 
46
  Click the link called "Simple Wp Sitemap" in your admin areas settings menu and it will take you there. Theres also a link from the plugins page, where you activate and deactivate them etc.
47
 
48
+ = Does it provide both an xml and an html sitemap? =
49
 
50
  Yes sir, it does.
51
 
52
  = Is it possible to add the sitemaps anywhere else on my site? Like on a page with a shortcode or something? =
53
 
54
+ Sorry no, not at the moment it isn't but maybe in the future.
55
 
56
  = Which one of the sitemaps should I submit to google and to other search engines? =
57
 
61
 
62
  Not really, search engines usually finds them automatically when they visit your site. However if you have webmaster tools at google or bing etc, that could be a good place to do it to get statistics over indexed pages and stuff.
63
 
 
 
 
 
64
  = How do I remove the sitemaps if I stop using the plugin? =
65
 
66
  When you deactivate the plugin they get removed automatically.
73
 
74
  == Changelog ==
75
 
76
+ = 1.0.8 (April 29, 2015) =
77
+ * Now generates dynamic sitemaps instead of static files
78
+ * Tested with caching plugins (the popular ones) and works
79
+ * Compressed html sitemap to reduce filesize/load time
80
+ * Changed mouse hover effect in html sitemap
81
+ * Fixed couple bugs/collisions with other plugins
82
+ * Updated descriptions
83
+
84
  = 1.0.7 (April 18, 2015) =
85
  * Added option to specify own display order
86
  * Excluded drafts, private and password protected pages
simple-wp-sitemap.php CHANGED
@@ -4,7 +4,7 @@
4
  * Plugin Name: Simple Wp Sitemap
5
  * Plugin URI: http://www.webbjocke.com/simple-wp-sitemap/
6
  * Description: An easy, fast and secure plugin that adds both an xml and an html sitemap to your site, which updates and maintains themselves so you dont have to!
7
- * Version: 1.0.7
8
  * Author: Webbjocke
9
  * Author URI: http://www.webbjocke.com/
10
  * License: GPLv3
@@ -13,19 +13,34 @@
13
  // Main class
14
 
15
  class SimpleWpSitemap {
 
16
 
17
- // Updates the sitemaps
18
- public static function updateSitemaps() {
19
- require_once('simpleWpMapBuilder.php');
20
- new SimpleWpMapBuilder('generate');
21
- }
22
-
23
- // Delete the files sitemap.xml and sitemap.html on deactivate
24
- public static function removeSitemaps() {
25
  require_once('simpleWpMapBuilder.php');
26
  new SimpleWpMapBuilder('delete');
27
  }
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  // Adds a link to settings from the plugins page
30
  public static function pluginSettingsLink($links) {
31
  $theLink = array(sprintf('<a href="%s">%s</a>', esc_url(admin_url('options-general.php?page=simpleWpSitemapSettings')), __('Settings')));
@@ -48,6 +63,43 @@ class SimpleWpSitemap {
48
  register_setting('simple_wp-sitemap-group', 'simple_wp_disp_tags');
49
  register_setting('simple_wp-sitemap-group', 'simple_wp_disp_authors');
50
  register_setting('simple_wp-sitemap-group', 'simple_wp_disp_sitemap_order');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  }
52
 
53
  // Add custom scripts and styles to the plugins customization page in admin area
@@ -60,23 +112,22 @@ class SimpleWpSitemap {
60
  }
61
 
62
  // Interface for settings page, also handles initial post request when settings are changed
63
- public static function sitemapAdminArea() {
64
  require_once('simpleWpMapOptions.php');
65
  $options = new SimpleWpMapOptions();
66
 
67
  if (isset($_POST['simple_wp_other_urls'], $_POST['simple_wp_block_urls'], $_POST['simple_wp_home_n'], $_POST['simple_wp_posts_n'], $_POST['simple_wp_pages_n'], $_POST['simple_wp_other_n'], $_POST['simple_wp_categories_n'], $_POST['simple_wp_tags_n'], $_POST['simple_wp_authors_n'])) {
68
 
69
  $options->setOptions($_POST['simple_wp_other_urls'], $_POST['simple_wp_block_urls'], (isset($_POST['simple_wp_attr_link']) ? 1 : 0), (isset($_POST['simple_wp_disp_categories']) ? 1 : 0), (isset($_POST['simple_wp_disp_tags']) ? 1 : 0), (isset($_POST['simple_wp_disp_authors']) ? 1 : 0), array('Home' => $_POST['simple_wp_home_n'], 'Posts' => $_POST['simple_wp_posts_n'], 'Pages' => $_POST['simple_wp_pages_n'], 'Other' => $_POST['simple_wp_other_n'], 'Categories' => $_POST['simple_wp_categories_n'], 'Tags' => $_POST['simple_wp_tags_n'], 'Authors' => $_POST['simple_wp_authors_n']));
70
- self::updateSitemaps();
71
  } ?>
72
 
73
  <div class="wrap">
74
 
75
  <h2>Simple Wp Sitemap settings</h2>
76
 
77
- <p>Your two sitemaps have been created and are active! Here you can change and customize them</p>
78
 
79
- <p><strong>Links to your xml and html sitemap:</strong>
80
 
81
  <ul>
82
  <li>Xml sitemap: <a href="<?php echo $options->sitemapUrl('xml'); ?>"><?php echo $options->sitemapUrl('xml'); ?></a></li>
@@ -118,7 +169,7 @@ class SimpleWpSitemap {
118
 
119
  <table id="sitemap-table-hide" class="widefat form-table">
120
 
121
- <tr><td><strong>Change display order</td></tr>
122
  <tr><td>If you want to change the display order in your sitemaps, click the arrows to move sections up or down. They will be displayed as ordered below, highest up is displayed first and lowest down last.</td></tr>
123
  <tr><td>
124
 
@@ -144,17 +195,20 @@ class SimpleWpSitemap {
144
  <tr><td id="sitemap-defaults" title="Restore the default display order">Restore default order</td></tr>
145
 
146
  </table>
147
-
148
  <p class="submit"><input type="submit" class="button-primary" value="<?php _e('Save Changes'); ?>"></p>
149
 
 
 
150
  </form>
151
 
152
  </div>
153
  <?php }
154
  }
 
 
155
  add_action('admin_menu', array('SimpleWpSitemap', 'sitemapAdminSetup'));
156
- add_action('deleted_post', array('SimpleWpSitemap', 'updateSitemaps'));
157
- add_action('save_post', array('SimpleWpSitemap', 'updateSitemaps'));
158
- register_activation_hook(__FILE__, array('SimpleWpSitemap', 'updateSitemaps'));
159
- register_deactivation_hook(__FILE__, array('SimpleWpSitemap', 'removeSitemaps'));
160
  add_filter("plugin_action_links_" . plugin_basename(__FILE__), array('SimpleWpSitemap', 'pluginSettingsLink'));
4
  * Plugin Name: Simple Wp Sitemap
5
  * Plugin URI: http://www.webbjocke.com/simple-wp-sitemap/
6
  * Description: An easy, fast and secure plugin that adds both an xml and an html sitemap to your site, which updates and maintains themselves so you dont have to!
7
+ * Version: 1.0.8
8
  * Author: Webbjocke
9
  * Author URI: http://www.webbjocke.com/
10
  * License: GPLv3
13
  // Main class
14
 
15
  class SimpleWpSitemap {
16
+ private static $version = 8;
17
 
18
+ // Activates the plugin
19
+ public static function activateSitemaps() {
20
+ self::rewriteRules();
21
+ flush_rewrite_rules();
22
+
23
+ update_option('simple_wp_sitemap_version', self::$version);
24
+
25
+ // deletes files sitemap.xml and .html from old versions of the plugin
26
  require_once('simpleWpMapBuilder.php');
27
  new SimpleWpMapBuilder('delete');
28
  }
29
 
30
+ // Deactivates the plugin
31
+ public static function deactivateSitemaps() {
32
+ flush_rewrite_rules();
33
+ }
34
+
35
+ // Updates the plugin (calls activateSitemaps)
36
+ public static function updateCheck() {
37
+ $current = get_option('simple_wp_sitemap_version');
38
+
39
+ if (!$current || $current < self::$version) {
40
+ self::activateSitemaps();
41
+ }
42
+ }
43
+
44
  // Adds a link to settings from the plugins page
45
  public static function pluginSettingsLink($links) {
46
  $theLink = array(sprintf('<a href="%s">%s</a>', esc_url(admin_url('options-general.php?page=simpleWpSitemapSettings')), __('Settings')));
63
  register_setting('simple_wp-sitemap-group', 'simple_wp_disp_tags');
64
  register_setting('simple_wp-sitemap-group', 'simple_wp_disp_authors');
65
  register_setting('simple_wp-sitemap-group', 'simple_wp_disp_sitemap_order');
66
+ self::updateCheck();
67
+ }
68
+
69
+ // Rewrite rules for sitemaps
70
+ public static function rewriteRules() {
71
+ add_rewrite_rule('sitemap\.xml$', 'index.php?thesimplewpsitemap=xml', 'top');
72
+ add_rewrite_rule('sitemap\.html$', 'index.php?thesimplewpsitemap=html', 'top');
73
+ }
74
+
75
+ // Add custom query
76
+ public static function addSitemapQuery($vars) {
77
+ $vars[] = 'thesimplewpsitemap';
78
+ return $vars;
79
+ }
80
+
81
+ // Generates the content
82
+ public static function generateSitemapContent() {
83
+ global $wp_query;
84
+
85
+ if (isset($wp_query->query_vars['thesimplewpsitemap'])) {
86
+ $q = $wp_query->query_vars['thesimplewpsitemap'];
87
+
88
+ if (!empty($q) && ($q === 'xml' || $q === 'html')) {
89
+ $wp_query->is_404 = false;
90
+ require_once('simpleWpMapBuilder.php');
91
+
92
+ if ($q === 'xml') {
93
+ $builder = new SimpleWpMapBuilder('xml');
94
+ header('Content-type: application/xml; charset=utf-8');
95
+ }
96
+ else {
97
+ $builder = new SimpleWpMapBuilder('html');
98
+ }
99
+ echo $builder->getContent();
100
+ exit;
101
+ }
102
+ }
103
  }
104
 
105
  // Add custom scripts and styles to the plugins customization page in admin area
112
  }
113
 
114
  // Interface for settings page, also handles initial post request when settings are changed
115
+ public static function sitemapAdminArea() {
116
  require_once('simpleWpMapOptions.php');
117
  $options = new SimpleWpMapOptions();
118
 
119
  if (isset($_POST['simple_wp_other_urls'], $_POST['simple_wp_block_urls'], $_POST['simple_wp_home_n'], $_POST['simple_wp_posts_n'], $_POST['simple_wp_pages_n'], $_POST['simple_wp_other_n'], $_POST['simple_wp_categories_n'], $_POST['simple_wp_tags_n'], $_POST['simple_wp_authors_n'])) {
120
 
121
  $options->setOptions($_POST['simple_wp_other_urls'], $_POST['simple_wp_block_urls'], (isset($_POST['simple_wp_attr_link']) ? 1 : 0), (isset($_POST['simple_wp_disp_categories']) ? 1 : 0), (isset($_POST['simple_wp_disp_tags']) ? 1 : 0), (isset($_POST['simple_wp_disp_authors']) ? 1 : 0), array('Home' => $_POST['simple_wp_home_n'], 'Posts' => $_POST['simple_wp_posts_n'], 'Pages' => $_POST['simple_wp_pages_n'], 'Other' => $_POST['simple_wp_other_n'], 'Categories' => $_POST['simple_wp_categories_n'], 'Tags' => $_POST['simple_wp_tags_n'], 'Authors' => $_POST['simple_wp_authors_n']));
 
122
  } ?>
123
 
124
  <div class="wrap">
125
 
126
  <h2>Simple Wp Sitemap settings</h2>
127
 
128
+ <p>Your two sitemaps are active! Here you can change and customize them.</p>
129
 
130
+ <p><strong>Links to your xml and html sitemap:</strong></p>
131
 
132
  <ul>
133
  <li>Xml sitemap: <a href="<?php echo $options->sitemapUrl('xml'); ?>"><?php echo $options->sitemapUrl('xml'); ?></a></li>
169
 
170
  <table id="sitemap-table-hide" class="widefat form-table">
171
 
172
+ <tr><td><strong>Change display order</strong></td></tr>
173
  <tr><td>If you want to change the display order in your sitemaps, click the arrows to move sections up or down. They will be displayed as ordered below, highest up is displayed first and lowest down last.</td></tr>
174
  <tr><td>
175
 
195
  <tr><td id="sitemap-defaults" title="Restore the default display order">Restore default order</td></tr>
196
 
197
  </table>
198
+
199
  <p class="submit"><input type="submit" class="button-primary" value="<?php _e('Save Changes'); ?>"></p>
200
 
201
+ <p>(If you have a caching plugin, you might have to clear cache before changes will be shown in the sitemaps)</p>
202
+
203
  </form>
204
 
205
  </div>
206
  <?php }
207
  }
208
+ register_activation_hook(__FILE__, array('SimpleWpSitemap', 'activateSitemaps'));
209
+ register_deactivation_hook(__FILE__, array('SimpleWpSitemap', 'deactivateSitemaps'));
210
  add_action('admin_menu', array('SimpleWpSitemap', 'sitemapAdminSetup'));
211
+ add_action('init', array('SimpleWpSitemap', 'rewriteRules'), 1);
212
+ add_filter('query_vars', array('SimpleWpSitemap', 'addSitemapQuery'), 1);
213
+ add_filter('template_redirect', array('SimpleWpSitemap', 'generateSitemapContent'), 1);
 
214
  add_filter("plugin_action_links_" . plugin_basename(__FILE__), array('SimpleWpSitemap', 'pluginSettingsLink'));
simpleWpMapBuilder.php CHANGED
@@ -4,73 +4,67 @@
4
  * The sitemap creating class
5
  */
6
  class SimpleWpMapBuilder {
7
- private $xml;
8
- private $html;
9
- private $file;
 
 
 
10
  private $url;
11
- private $homeUrl;
12
- private $blockedUrls;
13
- private $home;
14
- private $posts;
15
- private $pages;
16
- private $categories;
17
  private $tags;
18
- private $authors;
19
  private $order;
 
 
 
 
20
 
21
- // Constructor, the only public function this class has
22
  public function __construct($command) {
23
  $this->url = esc_url(plugins_url() . '/simple-wp-sitemap');
24
  $this->homeUrl = esc_url(get_home_url() . (substr(get_home_url(), -1) === '/' ? '' : '/'));
25
 
26
  switch ($command) {
27
- case 'generate':
 
 
 
 
 
28
  $this->generateSitemaps();
29
  break;
30
  case 'delete':
31
- $this->deleteSitemaps();
32
  }
33
  }
34
 
 
 
 
 
 
35
  // Generates the maps
36
  private function generateSitemaps() {
37
  $this->categories = (get_option('simple_wp_disp_categories') ? array(0 => 0) : false);
38
  $this->tags = (get_option('simple_wp_disp_tags') ? array(0 => 0) : false);
39
  $this->authors = (get_option('simple_wp_disp_authors') ? array(0 => 0) : false);
40
  $this->order = get_option('simple_wp_disp_sitemap_order');
41
- $this->posts = array('xml' => '', 'html' => '');
42
- $this->pages = array('xml' => '', 'html' => '');
43
- $this->home = null;
44
 
45
  $this->setUpBlockedUrls();
46
- $this->getContent();
47
-
48
- $this->writeToFile($this->xml, 'xml');
49
- $this->writeToFile($this->html, 'html');
50
- }
51
-
52
- // Deletes the maps
53
- private function deleteSitemaps() {
54
- $this->deleteFile('xml');
55
- $this->deleteFile('html');
56
  }
57
 
58
  // Returns other urls user has submitted
59
  private function getOtherPages() {
60
- $html = '';
61
  $xml = '';
62
 
63
  if ($options = get_option('simple_wp_other_urls')) {
64
  foreach ($options as $option) {
65
  if ($option && is_array($option)) {
66
- $url = esc_url($option['url']);
67
- $date = esc_html($option['date']);
68
- $html .= $this->getHtml($url, $date);
69
- $xml .= $this->getXml($url, $date);
70
  }
71
  }
72
  }
73
- return array('xml' => $xml, 'html' => $html);
74
  }
75
 
76
  // Sets up blocked urls into an array
@@ -93,28 +87,26 @@ class SimpleWpMapBuilder {
93
  return $this->blockedUrls && isset($this->blockedUrls[$url]);
94
  }
95
 
96
- // Returns an html string
97
- private function getHtml($link, $date) {
98
- return "\t\t<li>\n\t\t\t<a title=\"$link\" href=\"$link\">$link</a>\n\t\t\t<span class=\"date\">$date</span>\n\t\t</li>\n";
99
- }
100
 
101
- // Returns an xml string
102
  private function getXml($link, $date) {
103
- return "<url>\n\t<loc>$link</loc>\n\t<lastmod>$date</lastmod>\n</url>\n";
 
 
 
 
 
104
  }
105
 
106
  // Returns table headers with specific names (has been changed to div)
107
  private function htmlTableH($name) {
108
- return "\t<div class=\"header\">\n\t\t<p class=\"header-txt\">$name:</p>\n\t\t<p class=\"header-date\">Last modified:</p>\n\t</div>\n";
109
  }
110
 
111
- // Creates the actual sitemaps content, and querys the database. Might be long strings in one line.. I have a big screen
112
- private function getContent() {
113
  $q = new WP_Query(array('post_type' => 'any', 'post_status' => 'publish', 'posts_per_page' => -1, 'has_password' => false));
114
- $name = get_bloginfo('name');
115
- $this->xml = sprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-stylesheet type=\"text/css\" href=\"%s/css/xml.css\"?>\n<urlset xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9\n\thttp://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\" xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n", $this->url);
116
- $this->html = sprintf("<!doctype html>\n<html>\n<head>\n\t<meta charset=\"utf-8\">\n\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n\t<title>%s Html Sitemap</title>\n\t<link rel=\"stylesheet\" href=\"%s/css/html.css\">\n</head>\n<body>\n<div id=\"wrapper\">\n\n\t<h1>%s Html Sitemap</h1>\n\n", $name, $this->url, $name);
117
-
118
  global $post;
119
  $localPost = $post;
120
 
@@ -129,15 +121,13 @@ class SimpleWpMapBuilder {
129
 
130
  if (!$this->isBlockedUrl($link)) {
131
  if (!$this->home && $link === $this->homeUrl) {
132
- $this->home = array('xml' => $this->getXml($link, $date), 'html' => $this->getHtml($link, $date));
133
  }
134
  elseif ('page' === get_post_type()) {
135
- $this->pages['xml'] .= $this->getXml($link, $date);
136
- $this->pages['html'] .= $this->getHtml($link, $date);
137
  }
138
  else { // posts (also all custom post types are added here)
139
- $this->posts['xml'] .= $this->getXml($link, $date);
140
- $this->posts['html'] .= $this->getHtml($link, $date);
141
  }
142
  }
143
  }
@@ -177,33 +167,36 @@ class SimpleWpMapBuilder {
177
  // Merges the arrays with post data into strings and gets user submitted pages, categories, tags and author pages
178
  private function mergeArraysAndGetOtherPages() {
179
  $xml = '';
180
- $html = '';
181
  $sections = $this->getSortedArray();
182
 
183
  foreach ($sections as $title => $content) {
184
  if ($content) {
185
  if ($title === 'Categories' || $title === 'Tags' || $title === 'Authors') {
186
- $content = $this->stringifyCatsTagsAuths($title, $content);
187
- if ($title === 'Authors' && count($this->authors) <= 2) { // only one author (<= 2 cause one extra item is added to the array earlier)
188
  $title = 'Author';
189
  }
190
  }
191
 
192
- if ($content['xml']) {
193
- $xml .= $content['xml'];
194
- $html .= sprintf("%s\t<ul>\n%s\t</ul>\n", $this->htmlTableH($title), $content['html']);
195
  }
196
  }
197
  }
198
-
199
- $this->xml = sprintf("%s%s</urlset>", $this->xml, $xml);
200
- $this->html = sprintf("%s%s%s</div>\n</body>\n</html>", $this->html, $html, $this->attributionLink());
 
 
 
 
201
  }
202
 
203
  // Displays attribution link if admin has checked the checkbox
204
  private function attributionLink() {
205
  if (get_option('simple_wp_attr_link')) {
206
- return "\t<p id=\"attr\">Generated by: <a href=\"http://www.webbjocke.com/simple-wp-sitemap/\">Simple Wp Sitemap</a></p>\n";
207
  }
208
  return '';
209
  }
@@ -216,24 +209,23 @@ class SimpleWpMapBuilder {
216
 
217
  if (!$this->home) { // if homepage isn't found in the query (for instance if it's not a real "page" it wont be found)
218
  @date_default_timezone_set(get_option('timezone_string'));
219
- $date = date('Y-m-d\TH:i:sP');
220
- $this->home = array('xml' => $this->getXml($this->homeUrl, $date), 'html' => $this->getHtml($this->homeUrl, $date));
221
  }
222
 
223
- $arr['Home'] = $this->home;
224
- $arr['Posts'] = $this->posts;
225
- $arr['Pages'] = $this->pages;
 
 
 
 
226
  $arr['Other'] = $this->getOtherPages();
227
- $arr['Categories'] = $this->categories;
228
- $arr['Tags'] = $this->tags;
229
- $arr['Authors'] = $this->authors;
230
 
231
  return $arr;
232
  }
233
 
234
  // Returns category, tag and author links as ready xml and html strings
235
  private function stringifyCatsTagsAuths($type, $content) {
236
- $html = '';
237
  $xml = '';
238
 
239
  foreach ($content as $id => $date) {
@@ -241,11 +233,10 @@ class SimpleWpMapBuilder {
241
  $link = esc_url($this->getLink($id, $type));
242
  if (!$this->isBlockedUrl($link)) {
243
  $xml .= $this->getXml($link, $date);
244
- $html .= $this->getHtml($link, $date);
245
  }
246
  }
247
  }
248
- return array('xml' => $xml, 'html' => $html);
249
  }
250
 
251
  // Returns either a category, tag or an author link
@@ -257,34 +248,21 @@ class SimpleWpMapBuilder {
257
  }
258
  }
259
 
260
- // Sets up file paths to home directory
261
- private function setFile($fileType) {
262
- $this->file = sprintf("%s%ssitemap.%s", get_home_path(), (substr(get_home_path(), -1) === '/' ? '' : '/'), $fileType);
263
- }
264
-
265
- // Creates sitemap files and overrides old ones if there's any
266
- private function writeToFile($data, $fileType) {
267
- $this->setFile($fileType);
268
- try {
269
- $fp = fopen($this->file, 'w');
270
- if (file_exists($this->file)) {
271
- fwrite($fp, $data);
272
- fclose($fp);
 
273
  }
274
- }
275
- catch (Exception $ex) {
276
- die();
277
- }
278
- }
279
-
280
- // Deletes the sitemap files
281
- private function deleteFile($fileType) {
282
- $this->setFile($fileType);
283
- try {
284
- unlink($this->file);
285
- }
286
- catch (Exception $ex) {
287
- die();
288
  }
289
  }
290
  }
4
  * The sitemap creating class
5
  */
6
  class SimpleWpMapBuilder {
7
+ private $home = null;
8
+ private $xml = false;
9
+ private $html = false;
10
+ private $posts = '';
11
+ private $pages = '';
12
+ private $content = '';
13
  private $url;
 
 
 
 
 
 
14
  private $tags;
 
15
  private $order;
16
+ private $homeUrl;
17
+ private $authors;
18
+ private $categories;
19
+ private $blockedUrls;
20
 
21
+ // Constructor
22
  public function __construct($command) {
23
  $this->url = esc_url(plugins_url() . '/simple-wp-sitemap');
24
  $this->homeUrl = esc_url(get_home_url() . (substr(get_home_url(), -1) === '/' ? '' : '/'));
25
 
26
  switch ($command) {
27
+ case 'xml':
28
+ $this->xml = true;
29
+ $this->generateSitemaps();
30
+ break;
31
+ case 'html':
32
+ $this->html = true;
33
  $this->generateSitemaps();
34
  break;
35
  case 'delete':
36
+ $this->deleteFiles();
37
  }
38
  }
39
 
40
+ // Get method for content
41
+ public function getContent() {
42
+ return $this->content;
43
+ }
44
+
45
  // Generates the maps
46
  private function generateSitemaps() {
47
  $this->categories = (get_option('simple_wp_disp_categories') ? array(0 => 0) : false);
48
  $this->tags = (get_option('simple_wp_disp_tags') ? array(0 => 0) : false);
49
  $this->authors = (get_option('simple_wp_disp_authors') ? array(0 => 0) : false);
50
  $this->order = get_option('simple_wp_disp_sitemap_order');
 
 
 
51
 
52
  $this->setUpBlockedUrls();
53
+ $this->generateContent();
 
 
 
 
 
 
 
 
 
54
  }
55
 
56
  // Returns other urls user has submitted
57
  private function getOtherPages() {
 
58
  $xml = '';
59
 
60
  if ($options = get_option('simple_wp_other_urls')) {
61
  foreach ($options as $option) {
62
  if ($option && is_array($option)) {
63
+ $xml .= $this->getXml(esc_url($option['url']), esc_html($option['date']));
 
 
 
64
  }
65
  }
66
  }
67
+ return $xml;
68
  }
69
 
70
  // Sets up blocked urls into an array
87
  return $this->blockedUrls && isset($this->blockedUrls[$url]);
88
  }
89
 
 
 
 
 
90
 
91
+ // Returns an xml or html string
92
  private function getXml($link, $date) {
93
+ if ($this->xml) {
94
+ return "<url>\n\t<loc>$link</loc>\n\t<lastmod>$date</lastmod>\n</url>\n";
95
+ }
96
+ else{
97
+ return "<li><a title=\"$link\" href=\"$link\">$link</a><span class=\"date\">$date</span></li>";
98
+ }
99
  }
100
 
101
  // Returns table headers with specific names (has been changed to div)
102
  private function htmlTableH($name) {
103
+ return '<div class="header"><p class="header-txt">' . $name . ':</p><p class="header-date">Last modified:</p></div>';
104
  }
105
 
106
+ // Gets the actual sitemaps content, and querys the database
107
+ private function generateContent() {
108
  $q = new WP_Query(array('post_type' => 'any', 'post_status' => 'publish', 'posts_per_page' => -1, 'has_password' => false));
109
+
 
 
 
110
  global $post;
111
  $localPost = $post;
112
 
121
 
122
  if (!$this->isBlockedUrl($link)) {
123
  if (!$this->home && $link === $this->homeUrl) {
124
+ $this->home = $this->getXml($link, $date);
125
  }
126
  elseif ('page' === get_post_type()) {
127
+ $this->pages .= $this->getXml($link, $date);
 
128
  }
129
  else { // posts (also all custom post types are added here)
130
+ $this->posts .= $this->getXml($link, $date);
 
131
  }
132
  }
133
  }
167
  // Merges the arrays with post data into strings and gets user submitted pages, categories, tags and author pages
168
  private function mergeArraysAndGetOtherPages() {
169
  $xml = '';
170
+ $name = get_bloginfo('name');
171
  $sections = $this->getSortedArray();
172
 
173
  foreach ($sections as $title => $content) {
174
  if ($content) {
175
  if ($title === 'Categories' || $title === 'Tags' || $title === 'Authors') {
176
+ $content = $this->stringifyCatsTagsAuths($title, $content);
177
+ if ($title === 'Authors' && count($this->authors) <= 2) { // only one author
178
  $title = 'Author';
179
  }
180
  }
181
 
182
+ if ($content) {
183
+ $xml .= $this->xml ? $content : $this->htmlTableH($title) . "<ul>$content</ul>";
 
184
  }
185
  }
186
  }
187
+
188
+ if ($this->xml) {
189
+ $this->content = sprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-stylesheet type=\"text/css\" href=\"%s/css/xml.css\"?>\n<urlset xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9\n\thttp://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\" xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n%s</urlset>", $this->url, $xml);
190
+ }
191
+ else {
192
+ $this->content = sprintf('<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>%s Html Sitemap</title><link rel="stylesheet" href="%s/css/html.css"></head><body><div id="wrapper"><h1>%s Html Sitemap</h1>%s%s</div></body></html>', $name, $this->url, $name, $xml, $this->attributionLink());
193
+ }
194
  }
195
 
196
  // Displays attribution link if admin has checked the checkbox
197
  private function attributionLink() {
198
  if (get_option('simple_wp_attr_link')) {
199
+ return '<p id="attr">Generated by: <a href="http://www.webbjocke.com/simple-wp-sitemap/">Simple Wp Sitemap</a></p>';
200
  }
201
  return '';
202
  }
209
 
210
  if (!$this->home) { // if homepage isn't found in the query (for instance if it's not a real "page" it wont be found)
211
  @date_default_timezone_set(get_option('timezone_string'));
212
+ $this->home = $this->getXml($this->homeUrl, date('Y-m-d\TH:i:sP'));
 
213
  }
214
 
215
+ // copy to array and also clear some memory (some sites have a huge amount of posts)
216
+ $arr['Home'] = $this->home; $this->home = null;
217
+ $arr['Posts'] = $this->posts; $this->posts = null;
218
+ $arr['Pages'] = $this->pages; $this->pages = null;
219
+ $arr['Categories'] = $this->categories; $this->categories = null;
220
+ $arr['Tags'] = $this->tags; $this->tags = null;
221
+ $arr['Authors'] = $this->authors; $this->authors = null;
222
  $arr['Other'] = $this->getOtherPages();
 
 
 
223
 
224
  return $arr;
225
  }
226
 
227
  // Returns category, tag and author links as ready xml and html strings
228
  private function stringifyCatsTagsAuths($type, $content) {
 
229
  $xml = '';
230
 
231
  foreach ($content as $id => $date) {
233
  $link = esc_url($this->getLink($id, $type));
234
  if (!$this->isBlockedUrl($link)) {
235
  $xml .= $this->getXml($link, $date);
 
236
  }
237
  }
238
  }
239
+ return $xml;
240
  }
241
 
242
  // Returns either a category, tag or an author link
248
  }
249
  }
250
 
251
+ // Deletes the sitemap files from old versions of the plugin
252
+ private function deleteFiles() {
253
+ if (function_exists('get_home_path')) {
254
+ $path = sprintf('%s%ssitemap', get_home_path(), (substr(get_home_path(), -1) === '/' ? '' : '/'));
255
+ try {
256
+ if (file_exists($path . '.xml')) {
257
+ unlink($path . '.xml');
258
+ }
259
+ if (file_exists($path . '.html')) {
260
+ unlink($path . '.html');
261
+ }
262
+ }
263
+ catch (Exception $ex) {
264
+ return;
265
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
  }
267
  }
268
  }