Simple Wp Sitemap - Version 1.0.6

Version Description

(April 7, 2015) = * Made the plugin more user friendly * Added links to the sitemaps from the admin area * Added FAQ's (was about time huh) * Formatted the code a bit better

Download this release

Release Info

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

Code changes from version 1.0.5 to 1.0.6

Files changed (4) hide show
  1. readme.txt +35 -7
  2. simple-wp-sitemap.php +20 -13
  3. simpleWpMapBuilder.php +52 -52
  4. simpleWpMapOptions.php +28 -17
readme.txt CHANGED
@@ -5,19 +5,19 @@ 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.1.1
8
- Stable tag: 1.0.5
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 serves both an xml and an html sitemap to your page as static files. These 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 the sitemaps, it's easily done. You can also block pages that you don't want to be included in them.
19
 
20
- So why use this instead of the other hundreds(?) of sitemap plugins that's out there and which has thousands(?) of downloads? Well for one thing it's just an easy, fast and secure plugin that will just work for you, and it also makes the life pretty easy for search engines that wants to index your site!
21
 
22
  And yes, of course the sitemaps are mobile friendly :)
23
 
@@ -27,13 +27,35 @@ 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.
31
 
32
  3. Customize the plugin and add/block pages by hitting the "Simple Wp Sitemap" option in the settings menu.
33
 
34
- == Frequently asked questions ==
35
 
36
- None so far :)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  == Screenshots ==
39
 
@@ -43,6 +65,12 @@ None so far :)
43
 
44
  == Changelog ==
45
 
 
 
 
 
 
 
46
  = 1.0.5 (March 26, 2015) =
47
  * Fixed timezone bug
48
  * Made the sitemaps a bit lighter, they were so dark n dull
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.1.1
8
+ Stable tag: 1.0.6
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
 
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
 
34
+ == Frequently Asked Questions ==
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
+ = Are the sitemaps created "on the fly" dynamically or as static files? =
53
+
54
+ 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.
55
+
56
+ = How do I remove the sitemaps if I stop using the plugin? =
57
+
58
+ When you deactivate the plugin they get removed automatically.
59
 
60
  == Screenshots ==
61
 
65
 
66
  == Changelog ==
67
 
68
+ = 1.0.6 (April 7, 2015) =
69
+ * Made the plugin more user friendly
70
+ * Added links to the sitemaps from the admin area
71
+ * Added FAQ's (was about time huh)
72
+ * Formatted the code a bit better
73
+
74
  = 1.0.5 (March 26, 2015) =
75
  * Fixed timezone bug
76
  * Made the sitemaps a bit lighter, they were so dark n dull
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.5
8
  * Author: Webbjocke
9
  * Author URI: http://www.webbjocke.com/
10
  * License: GPLv3
@@ -15,31 +15,31 @@
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('<a href="' . esc_url(admin_url('options-general.php?page=simpleWpSitemapSettings')) . '">' . __('Settings') . '</a>');
32
  return array_merge($links, $theLink);
33
  }
34
 
35
  // Sets the menu option for admins
36
- public static function sitemapAdminSetup(){
37
  add_options_page('Simple Wp Sitemap', 'Simple Wp Sitemap', 'administrator', 'simpleWpSitemapSettings', array('SimpleWpSitemap', 'sitemapAdminArea'));
38
  add_action('admin_init', array('SimpleWpSitemap', 'sitemapAdminInit'));
39
  }
40
 
41
  // Registers settings on admin_init
42
- public static function sitemapAdminInit(){
43
  register_setting('simple_wp-sitemap-group', 'simple_wp_other_urls');
44
  register_setting('simple_wp-sitemap-group', 'simple_wp_block_urls');
45
  register_setting('simple_wp-sitemap-group', 'simple_wp_attr_link');
@@ -49,11 +49,11 @@ class SimpleWpSitemap {
49
  }
50
 
51
  // Interface for settings page, also handles initial post request when settings are changed
52
- public static function sitemapAdminArea(){
53
  require_once('simpleWpMapOptions.php');
54
  $options = new SimpleWpMapOptions();
55
 
56
- if (isset($_POST['simple_wp_other_urls'], $_POST['simple_wp_block_urls'])){
57
  $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));
58
  self::updateSitemaps();
59
  } ?>
@@ -62,7 +62,14 @@ class SimpleWpSitemap {
62
 
63
  <h1>Simple Wp Sitemap settings</h1>
64
 
65
- <p>Change and customize the sitemap</p>
 
 
 
 
 
 
 
66
 
67
  <form method="post" action="options-general.php?page=simpleWpSitemapSettings">
68
 
@@ -71,11 +78,11 @@ class SimpleWpSitemap {
71
  <table class="widefat form-table">
72
 
73
  <tr><td><strong>Add pages</strong></td></tr>
74
- <tr><td>Add pages to the sitemaps in addition to the original wordpress ones. Just paste "absolute" links in the textarea like: <b>http://www.example.com/</b>, each link on a new row.</td></tr>
75
  <tr><td><textarea rows="7" name="simple_wp_other_urls" class="large-text code"><?php echo $options->getOptions('simple_wp_other_urls'); ?></textarea></td></tr>
76
 
77
  <tr><td><strong>Block pages</strong></td></tr>
78
- <tr><td>Add pages you dont't want to show up in the sitemaps. Same as above and just paste every link on a new row. (Hint: Copy paste the whole url from the address bar on the actual pages).</td></tr>
79
  <tr><td><textarea rows="7" name="simple_wp_block_urls" class="large-text code"><?php echo $options->getOptions('simple_wp_block_urls'); ?></textarea></td></tr>
80
 
81
  <tr><td><strong>Extra sitemap includes</strong></td></tr>
@@ -85,7 +92,7 @@ class SimpleWpSitemap {
85
  <tr><td><input type="checkbox" name="simple_wp_disp_authors" id="simple_wp_authors" <?php echo $options->getOptions('simple_wp_disp_authors'); ?>></input><label for="simple_wp_authors"> Include authors</label></td></tr>
86
 
87
  <tr><td><strong>Like the plugin?</strong></td></tr>
88
- <tr><td>Show your support by rating the plugin at wordpress.org, or atleast by adding an attribution link on the sitemap.html file :)</td></tr>
89
  <tr><td><input type="checkbox" name="simple_wp_attr_link" id="simple_wp_check" <?php echo $options->getOptions('simple_wp_attr_link'); ?>></input><label for="simple_wp_check"> Add "Generated by Simple Wp Sitemap" link at bottom of sitemap.html.</label></td></tr>
90
 
91
  </table>
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.6
8
  * Author: Webbjocke
9
  * Author URI: http://www.webbjocke.com/
10
  * License: GPLv3
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')));
32
  return array_merge($links, $theLink);
33
  }
34
 
35
  // Sets the menu option for admins
36
+ public static function sitemapAdminSetup() {
37
  add_options_page('Simple Wp Sitemap', 'Simple Wp Sitemap', 'administrator', 'simpleWpSitemapSettings', array('SimpleWpSitemap', 'sitemapAdminArea'));
38
  add_action('admin_init', array('SimpleWpSitemap', 'sitemapAdminInit'));
39
  }
40
 
41
  // Registers settings on admin_init
42
+ public static function sitemapAdminInit() {
43
  register_setting('simple_wp-sitemap-group', 'simple_wp_other_urls');
44
  register_setting('simple_wp-sitemap-group', 'simple_wp_block_urls');
45
  register_setting('simple_wp-sitemap-group', 'simple_wp_attr_link');
49
  }
50
 
51
  // Interface for settings page, also handles initial post request when settings are changed
52
+ public static function sitemapAdminArea() {
53
  require_once('simpleWpMapOptions.php');
54
  $options = new SimpleWpMapOptions();
55
 
56
+ if (isset($_POST['simple_wp_other_urls'], $_POST['simple_wp_block_urls'])) {
57
  $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));
58
  self::updateSitemaps();
59
  } ?>
62
 
63
  <h1>Simple Wp Sitemap settings</h1>
64
 
65
+ <p>Your two sitemaps have been created and are active! Here you can change and customize them</p>
66
+
67
+ <p><strong>Links to your xml and html sitemap:</strong>
68
+
69
+ <ul>
70
+ <li>Xml sitemap: <a href="<?php echo $options->sitemapUrl('xml'); ?>"><?php echo $options->sitemapUrl('xml'); ?></a></li>
71
+ <li>Html sitemap: <a href="<?php echo $options->sitemapUrl('html'); ?>"><?php echo $options->sitemapUrl('html'); ?></a></li>
72
+ </ul>
73
 
74
  <form method="post" action="options-general.php?page=simpleWpSitemapSettings">
75
 
78
  <table class="widefat form-table">
79
 
80
  <tr><td><strong>Add pages</strong></td></tr>
81
+ <tr><td>Add pages to the sitemaps in addition to your original wordpress ones. Just paste "absolute" links in the textarea like: <strong>http://www.example.com/</strong>, each link on a new row. (This will affect both your xml and html sitemap)</td></tr>
82
  <tr><td><textarea rows="7" name="simple_wp_other_urls" class="large-text code"><?php echo $options->getOptions('simple_wp_other_urls'); ?></textarea></td></tr>
83
 
84
  <tr><td><strong>Block pages</strong></td></tr>
85
+ <tr><td>Add pages you don't want to show up in the sitemaps. Same as above, just paste every link on a new row. (Hint: copy paste links from one of the sitemaps to get correct urls).</td></tr>
86
  <tr><td><textarea rows="7" name="simple_wp_block_urls" class="large-text code"><?php echo $options->getOptions('simple_wp_block_urls'); ?></textarea></td></tr>
87
 
88
  <tr><td><strong>Extra sitemap includes</strong></td></tr>
92
  <tr><td><input type="checkbox" name="simple_wp_disp_authors" id="simple_wp_authors" <?php echo $options->getOptions('simple_wp_disp_authors'); ?>></input><label for="simple_wp_authors"> Include authors</label></td></tr>
93
 
94
  <tr><td><strong>Like the plugin?</strong></td></tr>
95
+ <tr><td>Show your support by rating the plugin at wordpress.org, or atleast by adding an attribution link to the sitemap.html file :)</td></tr>
96
  <tr><td><input type="checkbox" name="simple_wp_attr_link" id="simple_wp_check" <?php echo $options->getOptions('simple_wp_attr_link'); ?>></input><label for="simple_wp_check"> Add "Generated by Simple Wp Sitemap" link at bottom of sitemap.html.</label></td></tr>
97
 
98
  </table>
simpleWpMapBuilder.php CHANGED
@@ -15,11 +15,11 @@ class SimpleWpMapBuilder {
15
  private $authors;
16
 
17
  // Constructor, the only public function this class has
18
- public function __construct($command){
19
  $this->url = esc_url(plugins_url() . '/simple-wp-sitemap');
20
  $this->homeUrl = esc_url(get_home_url() . (substr(get_home_url(), -1) === '/' ? '' : '/'));
21
 
22
- switch($command){
23
  case 'generate':
24
  $this->generateSitemaps();
25
  break;
@@ -29,7 +29,7 @@ class SimpleWpMapBuilder {
29
  }
30
 
31
  // Generates the maps
32
- private function generateSitemaps(){
33
  $this->categories = (get_option('simple_wp_disp_categories') ? array(0 => 0) : null);
34
  $this->tags = (get_option('simple_wp_disp_tags') ? array(0 => 0) : null);
35
  $this->authors = (get_option('simple_wp_disp_authors') ? array(0 => 0) : null);
@@ -42,19 +42,19 @@ class SimpleWpMapBuilder {
42
  }
43
 
44
  // Deletes the maps
45
- private function deleteSitemaps(){
46
  $this->deleteFile('xml');
47
  $this->deleteFile('html');
48
  }
49
 
50
  // Returns other urls (not standard wordpress) user has submitted
51
- private function getOtherPages(){
52
  $html = '';
53
  $xml = '';
54
 
55
- if ($options = get_option('simple_wp_other_urls')){
56
- foreach($options as $option){
57
- if ($option && is_array($option)){
58
  $url = esc_url($option['url']);
59
  $date = esc_html($option['date']);
60
  $html .= $this->getHtml($url, $date);
@@ -66,42 +66,42 @@ class SimpleWpMapBuilder {
66
  }
67
 
68
  // Sets up blocked urls into an array
69
- private function setUpBlockedUrls(){
70
  $blocked = get_option('simple_wp_block_urls');
71
- if ($blocked && is_array($blocked)){
72
  $this->blockedUrls = array();
73
 
74
- foreach($blocked as $block){
75
  $this->blockedUrls[$block['url']] = 'blocked';
76
  }
77
  }
78
- else{
79
  $this->blockedUrls = null;
80
  }
81
  }
82
 
83
  // Matches url against blocked ones that shouldn't be displayed
84
- private function isBlockedUrl($url){
85
  return $this->blockedUrls && isset($this->blockedUrls[$url]);
86
  }
87
 
88
  // Returns an html string
89
- private function getHtml($link, $date){
90
  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";
91
  }
92
 
93
  // Returns an xml string
94
- private function getXml($link, $date){
95
  return "<url>\n\t<loc>$link</loc>\n\t<lastmod>$date</lastmod>\n</url>\n";
96
  }
97
 
98
  // Returns table headers with specific names (has been changed to div)
99
- private function htmlTableH($name){
100
  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";
101
  }
102
 
103
  // Creates the actual sitemaps content, and querys the database
104
- private function getContent(){
105
  $q = new WP_Query('post_type=any&posts_per_page=-1');
106
  $name = get_bloginfo('name');
107
  $xml = sprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-stylesheet type=\"text/css\" href=\"%s/css/xml.css\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">\n", $this->url);
@@ -145,38 +145,38 @@ class SimpleWpMapBuilder {
145
  }
146
 
147
  // Displays attribution link if admin has checked the checkbox
148
- private function attributionLink(){
149
- if (get_option('simple_wp_attr_link')){
150
  return "\t<p id=\"attr\">Generated by: <a href=\"http://www.webbjocke.com/simple-wp-sitemap/\">Simple Wp Sitemap</a></p>\n";
151
  }
152
  return '';
153
  }
154
 
155
  // Gets a posts categories, tags and author, and compares for last modified date
156
- private function getCategoriesTagsAndAuthor($date){
157
- if ($this->categories){
158
- if ($postCats = get_the_category()){
159
- foreach($postCats as $category){
160
  $id = $category->term_id;
161
- if (!isset($this->categories[$id]) || $this->categories[$id] < $date){
162
  $this->categories[$id] = $date;
163
  }
164
  }
165
  }
166
  }
167
- if ($this->tags){
168
- if ($postTags = get_the_tags()){
169
- foreach($postTags as $tag){
170
  $id = $tag->term_id;
171
- if (!isset($this->tags[$id]) || $this->tags[$id] < $date){
172
  $this->tags[$id] = $date;
173
  }
174
  }
175
  }
176
  }
177
- if ($this->authors){
178
- if ($id = get_the_author_meta('ID')){
179
- if (is_int($id) && (!isset($this->authors[$id]) || $this->authors[$id] < $date)){
180
  $this->authors[$id] = $date;
181
  }
182
  }
@@ -184,11 +184,11 @@ class SimpleWpMapBuilder {
184
  }
185
 
186
  // Merges the arrays with post data into strings and gets user submitted pages, categories, tags and author pages
187
- private function mergeArraysAndGetOtherPages($posts, $pages, $homePage){
188
  $xml = '';
189
  $html = '';
190
 
191
- if (!$homePage){ // if homepage isn't found in the query add it here (for instance if it's not a real "page" it wont be found)
192
  date_default_timezone_set(get_option('timezone_string'));
193
  $date = date('Y-m-d\TH:i:sP');
194
  $xml .= $this->getXml($this->homeUrl, $date);
@@ -208,22 +208,22 @@ class SimpleWpMapBuilder {
208
  }
209
 
210
  $otherPages = $this->getOtherPages();
211
- if ($otherPages['xml']){
212
  $xml .= $otherPages['xml'];
213
  $html .= sprintf("%s\t<ul>\n%s\t</ul>\n", $this->htmlTableH('Other'), $otherPages['html']);
214
  }
215
 
216
- if ($this->categories){
217
  $locArr = $this->stringifyCatsTagsAuths('Categories');
218
  $xml .= $locArr['xml'];
219
  $html .= $locArr['html'];
220
  }
221
- if ($this->tags){
222
  $locArr = $this->stringifyCatsTagsAuths('Tags');
223
  $xml .= $locArr['xml'];
224
  $html .= $locArr['html'];
225
  }
226
- if ($this->authors){
227
  $locArr = $this->stringifyCatsTagsAuths(count($this->authors) > 2 ? 'Authors' : 'Author');
228
  $xml .= $locArr['xml'];
229
  $html .= $locArr['html'];
@@ -233,11 +233,11 @@ class SimpleWpMapBuilder {
233
  }
234
 
235
  // Returns category, tag and author links as ready xml and html strings
236
- private function stringifyCatsTagsAuths($type){
237
  $html = sprintf("%s\t<ul>\n", $this->htmlTableH($type));
238
  $xml = '';
239
 
240
- switch($type){
241
  case 'Tags':
242
  $arr = $this->tags;
243
  break;
@@ -248,10 +248,10 @@ class SimpleWpMapBuilder {
248
  $arr = $this->authors;
249
  }
250
 
251
- foreach($arr as $id => $date){
252
- if ($date){
253
  $link = esc_url($this->getLink($id, $type));
254
- if (!$this->isBlockedUrl($link)){
255
  $xml .= $this->getXml($link, $date);
256
  $html .= $this->getHtml($link, $date);
257
  }
@@ -261,8 +261,8 @@ class SimpleWpMapBuilder {
261
  }
262
 
263
  // Returns either a category, tag or an author link
264
- private function getLink($id, $type){
265
- switch($type){
266
  case 'Tags':
267
  return get_tag_link($id);
268
  case 'Categories':
@@ -273,32 +273,32 @@ class SimpleWpMapBuilder {
273
  }
274
 
275
  // Sets up file paths to home directory
276
- private function setFile($fileType){
277
  $this->file = sprintf("%s%ssitemap.%s", get_home_path(), (substr(get_home_path(), -1) === '/' ? '' : '/'), $fileType);
278
  }
279
 
280
  // Creates sitemap files and overrides old ones if there's any
281
- private function writeToFile($data, $fileType){
282
  $this->setFile($fileType);
283
- try{
284
  $fp = fopen($this->file, 'w');
285
- if (file_exists($this->file)){
286
  fwrite($fp, $data);
287
  fclose($fp);
288
  }
289
  }
290
- catch(Exception $ex){
291
  die();
292
  }
293
  }
294
 
295
  // Deletes the sitemap files
296
- private function deleteFile($fileType){
297
  $this->setFile($fileType);
298
- try{
299
  unlink($this->file);
300
  }
301
- catch(Exception $ex){
302
  die();
303
  }
304
  }
15
  private $authors;
16
 
17
  // Constructor, the only public function this class has
18
+ public function __construct($command) {
19
  $this->url = esc_url(plugins_url() . '/simple-wp-sitemap');
20
  $this->homeUrl = esc_url(get_home_url() . (substr(get_home_url(), -1) === '/' ? '' : '/'));
21
 
22
+ switch ($command) {
23
  case 'generate':
24
  $this->generateSitemaps();
25
  break;
29
  }
30
 
31
  // Generates the maps
32
+ private function generateSitemaps() {
33
  $this->categories = (get_option('simple_wp_disp_categories') ? array(0 => 0) : null);
34
  $this->tags = (get_option('simple_wp_disp_tags') ? array(0 => 0) : null);
35
  $this->authors = (get_option('simple_wp_disp_authors') ? array(0 => 0) : null);
42
  }
43
 
44
  // Deletes the maps
45
+ private function deleteSitemaps() {
46
  $this->deleteFile('xml');
47
  $this->deleteFile('html');
48
  }
49
 
50
  // Returns other urls (not standard wordpress) user has submitted
51
+ private function getOtherPages() {
52
  $html = '';
53
  $xml = '';
54
 
55
+ if ($options = get_option('simple_wp_other_urls')) {
56
+ foreach ($options as $option) {
57
+ if ($option && is_array($option)) {
58
  $url = esc_url($option['url']);
59
  $date = esc_html($option['date']);
60
  $html .= $this->getHtml($url, $date);
66
  }
67
 
68
  // Sets up blocked urls into an array
69
+ private function setUpBlockedUrls() {
70
  $blocked = get_option('simple_wp_block_urls');
71
+ if ($blocked && is_array($blocked)) {
72
  $this->blockedUrls = array();
73
 
74
+ foreach ($blocked as $block) {
75
  $this->blockedUrls[$block['url']] = 'blocked';
76
  }
77
  }
78
+ else {
79
  $this->blockedUrls = null;
80
  }
81
  }
82
 
83
  // Matches url against blocked ones that shouldn't be displayed
84
+ private function isBlockedUrl($url) {
85
  return $this->blockedUrls && isset($this->blockedUrls[$url]);
86
  }
87
 
88
  // Returns an html string
89
+ private function getHtml($link, $date) {
90
  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";
91
  }
92
 
93
  // Returns an xml string
94
+ private function getXml($link, $date) {
95
  return "<url>\n\t<loc>$link</loc>\n\t<lastmod>$date</lastmod>\n</url>\n";
96
  }
97
 
98
  // Returns table headers with specific names (has been changed to div)
99
+ private function htmlTableH($name) {
100
  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";
101
  }
102
 
103
  // Creates the actual sitemaps content, and querys the database
104
+ private function getContent() {
105
  $q = new WP_Query('post_type=any&posts_per_page=-1');
106
  $name = get_bloginfo('name');
107
  $xml = sprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-stylesheet type=\"text/css\" href=\"%s/css/xml.css\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">\n", $this->url);
145
  }
146
 
147
  // Displays attribution link if admin has checked the checkbox
148
+ private function attributionLink() {
149
+ if (get_option('simple_wp_attr_link')) {
150
  return "\t<p id=\"attr\">Generated by: <a href=\"http://www.webbjocke.com/simple-wp-sitemap/\">Simple Wp Sitemap</a></p>\n";
151
  }
152
  return '';
153
  }
154
 
155
  // Gets a posts categories, tags and author, and compares for last modified date
156
+ private function getCategoriesTagsAndAuthor($date) {
157
+ if ($this->categories) {
158
+ if ($postCats = get_the_category()) {
159
+ foreach ($postCats as $category) {
160
  $id = $category->term_id;
161
+ if (!isset($this->categories[$id]) || $this->categories[$id] < $date) {
162
  $this->categories[$id] = $date;
163
  }
164
  }
165
  }
166
  }
167
+ if ($this->tags) {
168
+ if ($postTags = get_the_tags()) {
169
+ foreach ($postTags as $tag) {
170
  $id = $tag->term_id;
171
+ if (!isset($this->tags[$id]) || $this->tags[$id] < $date) {
172
  $this->tags[$id] = $date;
173
  }
174
  }
175
  }
176
  }
177
+ if ($this->authors) {
178
+ if ($id = get_the_author_meta('ID')) {
179
+ if (is_int($id) && (!isset($this->authors[$id]) || $this->authors[$id] < $date)) {
180
  $this->authors[$id] = $date;
181
  }
182
  }
184
  }
185
 
186
  // Merges the arrays with post data into strings and gets user submitted pages, categories, tags and author pages
187
+ private function mergeArraysAndGetOtherPages($posts, $pages, $homePage) {
188
  $xml = '';
189
  $html = '';
190
 
191
+ if (!$homePage) { // if homepage isn't found in the query add it here (for instance if it's not a real "page" it wont be found)
192
  date_default_timezone_set(get_option('timezone_string'));
193
  $date = date('Y-m-d\TH:i:sP');
194
  $xml .= $this->getXml($this->homeUrl, $date);
208
  }
209
 
210
  $otherPages = $this->getOtherPages();
211
+ if ($otherPages['xml']) {
212
  $xml .= $otherPages['xml'];
213
  $html .= sprintf("%s\t<ul>\n%s\t</ul>\n", $this->htmlTableH('Other'), $otherPages['html']);
214
  }
215
 
216
+ if ($this->categories) {
217
  $locArr = $this->stringifyCatsTagsAuths('Categories');
218
  $xml .= $locArr['xml'];
219
  $html .= $locArr['html'];
220
  }
221
+ if ($this->tags) {
222
  $locArr = $this->stringifyCatsTagsAuths('Tags');
223
  $xml .= $locArr['xml'];
224
  $html .= $locArr['html'];
225
  }
226
+ if ($this->authors) {
227
  $locArr = $this->stringifyCatsTagsAuths(count($this->authors) > 2 ? 'Authors' : 'Author');
228
  $xml .= $locArr['xml'];
229
  $html .= $locArr['html'];
233
  }
234
 
235
  // Returns category, tag and author links as ready xml and html strings
236
+ private function stringifyCatsTagsAuths($type) {
237
  $html = sprintf("%s\t<ul>\n", $this->htmlTableH($type));
238
  $xml = '';
239
 
240
+ switch ($type) {
241
  case 'Tags':
242
  $arr = $this->tags;
243
  break;
248
  $arr = $this->authors;
249
  }
250
 
251
+ foreach ($arr as $id => $date) {
252
+ if ($date) {
253
  $link = esc_url($this->getLink($id, $type));
254
+ if (!$this->isBlockedUrl($link)) {
255
  $xml .= $this->getXml($link, $date);
256
  $html .= $this->getHtml($link, $date);
257
  }
261
  }
262
 
263
  // Returns either a category, tag or an author link
264
+ private function getLink($id, $type) {
265
+ switch ($type) {
266
  case 'Tags':
267
  return get_tag_link($id);
268
  case 'Categories':
273
  }
274
 
275
  // Sets up file paths to home directory
276
+ private function setFile($fileType) {
277
  $this->file = sprintf("%s%ssitemap.%s", get_home_path(), (substr(get_home_path(), -1) === '/' ? '' : '/'), $fileType);
278
  }
279
 
280
  // Creates sitemap files and overrides old ones if there's any
281
+ private function writeToFile($data, $fileType) {
282
  $this->setFile($fileType);
283
+ try {
284
  $fp = fopen($this->file, 'w');
285
+ if (file_exists($this->file)) {
286
  fwrite($fp, $data);
287
  fclose($fp);
288
  }
289
  }
290
+ catch (Exception $ex) {
291
  die();
292
  }
293
  }
294
 
295
  // Deletes the sitemap files
296
+ private function deleteFile($fileType) {
297
  $this->setFile($fileType);
298
+ try {
299
  unlink($this->file);
300
  }
301
+ catch (Exception $ex) {
302
  die();
303
  }
304
  }
simpleWpMapOptions.php CHANGED
@@ -4,9 +4,20 @@
4
  * Class that handles all admin settings
5
  */
6
  class SimpleWpMapOptions {
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  // Updates the settings/options
9
- public function setOptions($otherUrls, $blockUrls, $attrLink, $categories, $tags, $authors){
10
  date_default_timezone_set(get_option('timezone_string'));
11
  update_option('simple_wp_other_urls', $this->addUrls($otherUrls, get_option('simple_wp_other_urls')));
12
  update_option('simple_wp_block_urls', $this->addUrls($blockUrls));
@@ -17,20 +28,20 @@ class SimpleWpMapOptions {
17
  }
18
 
19
  // Returns the options as strings to be displayed in textareas, and checkbox values
20
- public function getOptions($val){
21
- if ($val === 'simple_wp_other_urls' || $val === 'simple_wp_block_urls'){
22
  $val = get_option($val);
23
  }
24
- elseif ($val === 'simple_wp_attr_link' || $val === 'simple_wp_disp_categories' || $val === 'simple_wp_disp_tags' || $val === 'simple_wp_disp_authors'){
25
  return get_option($val) ? 'checked' : ''; // return checkbox checked values right here and dont bother with the loop below
26
  }
27
- else{
28
  $val = null;
29
  }
30
 
31
- if (!$this->isNullOrWhiteSpace($val)){
32
  $str = '';
33
- foreach($val as $sArr){
34
  $str .= $this->sanitizeUrl($sArr['url']) . "\n";
35
  }
36
  return trim($str);
@@ -39,38 +50,38 @@ class SimpleWpMapOptions {
39
  }
40
 
41
  // Checks if string/array is empty
42
- private function isNullOrWhiteSpace($word){
43
- if (is_array($word)){
44
  return false;
45
  }
46
  return ($word === null || $word === false || trim($word) === '');
47
  }
48
 
49
  // Sanitizes urls with esc_url and trim
50
- private function sanitizeUrl($url){
51
  return esc_url(trim($url));
52
  }
53
 
54
  // Adds new urls to the sitemaps
55
- private function addUrls($urls, $oldUrls=null){
56
  $arr = array();
57
 
58
- if (!$this->isNullOrWhiteSpace($urls)){
59
  $urls = explode("\n", $urls);
60
 
61
  foreach ($urls as $u){
62
- if (!$this->isNullOrWhiteSpace($u)){
63
  $u = $this->sanitizeUrl($u);
64
  $b = false;
65
- if ($oldUrls && is_array($oldUrls)){
66
- foreach ($oldUrls as $o){
67
- if ($o['url'] === $u && !$b){
68
  $arr[] = $o;
69
  $b = true;
70
  }
71
  }
72
  }
73
- if (!$b && strlen($u) < 500){
74
  $arr[] = array('url' => $u, 'date' => date('Y-m-d\TH:i:sP'));
75
  }
76
  }
4
  * Class that handles all admin settings
5
  */
6
  class SimpleWpMapOptions {
7
+ private $homeUrl;
8
+
9
+ // sets homeUrl with trailing slash
10
+ public function __construct() {
11
+ $this->homeUrl = esc_url(get_home_url() . (substr(get_home_url(), -1) === '/' ? '' : '/'));
12
+ }
13
+
14
+ // Returns a sitemap url
15
+ public function sitemapUrl($format) {
16
+ return sprintf('%ssitemap.%s', $this->homeUrl, $format);
17
+ }
18
 
19
  // Updates the settings/options
20
+ public function setOptions($otherUrls, $blockUrls, $attrLink, $categories, $tags, $authors) {
21
  date_default_timezone_set(get_option('timezone_string'));
22
  update_option('simple_wp_other_urls', $this->addUrls($otherUrls, get_option('simple_wp_other_urls')));
23
  update_option('simple_wp_block_urls', $this->addUrls($blockUrls));
28
  }
29
 
30
  // Returns the options as strings to be displayed in textareas, and checkbox values
31
+ public function getOptions($val) {
32
+ if ($val === 'simple_wp_other_urls' || $val === 'simple_wp_block_urls') {
33
  $val = get_option($val);
34
  }
35
+ elseif ($val === 'simple_wp_attr_link' || $val === 'simple_wp_disp_categories' || $val === 'simple_wp_disp_tags' || $val === 'simple_wp_disp_authors') {
36
  return get_option($val) ? 'checked' : ''; // return checkbox checked values right here and dont bother with the loop below
37
  }
38
+ else {
39
  $val = null;
40
  }
41
 
42
+ if (!$this->isNullOrWhiteSpace($val)) {
43
  $str = '';
44
+ foreach ($val as $sArr){
45
  $str .= $this->sanitizeUrl($sArr['url']) . "\n";
46
  }
47
  return trim($str);
50
  }
51
 
52
  // Checks if string/array is empty
53
+ private function isNullOrWhiteSpace($word) {
54
+ if (is_array($word)) {
55
  return false;
56
  }
57
  return ($word === null || $word === false || trim($word) === '');
58
  }
59
 
60
  // Sanitizes urls with esc_url and trim
61
+ private function sanitizeUrl($url) {
62
  return esc_url(trim($url));
63
  }
64
 
65
  // Adds new urls to the sitemaps
66
+ private function addUrls($urls, $oldUrls=null) {
67
  $arr = array();
68
 
69
+ if (!$this->isNullOrWhiteSpace($urls)) {
70
  $urls = explode("\n", $urls);
71
 
72
  foreach ($urls as $u){
73
+ if (!$this->isNullOrWhiteSpace($u)) {
74
  $u = $this->sanitizeUrl($u);
75
  $b = false;
76
+ if ($oldUrls && is_array($oldUrls)) {
77
+ foreach ($oldUrls as $o) {
78
+ if ($o['url'] === $u && !$b) {
79
  $arr[] = $o;
80
  $b = true;
81
  }
82
  }
83
  }
84
+ if (!$b && strlen($u) < 500) {
85
  $arr[] = array('url' => $u, 'date' => date('Y-m-d\TH:i:sP'));
86
  }
87
  }