Simple Wp Sitemap - Version 1.0.1

Version Description

Download this release

Release Info

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

Code changes from version 1.0.0 to 1.0.1

{css → tags/1.0.0/css}/html.css RENAMED
File without changes
{css → tags/1.0.0/css}/index.php RENAMED
File without changes
{css → tags/1.0.0/css}/xml.css RENAMED
File without changes
index.php → tags/1.0.0/index.php RENAMED
File without changes
readme.txt → tags/1.0.0/readme.txt RENAMED
File without changes
simple-wp-sitemap.php → tags/1.0.0/simple-wp-sitemap.php RENAMED
File without changes
simpleWpMapBuilder.php → tags/1.0.0/simpleWpMapBuilder.php RENAMED
File without changes
simpleWpMapOptions.php → tags/1.0.0/simpleWpMapOptions.php RENAMED
File without changes
trunk/css/html.css ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *{
2
+ margin: 0;
3
+ padding: 0;
4
+ }
5
+ html{
6
+ background: #edf4fa;
7
+ }
8
+ body{
9
+ max-width: 960px;
10
+ margin: 20px auto;
11
+ padding: 20px 4%;
12
+ box-shadow: 1px 1px 5px #d1d1d1;
13
+ font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif;
14
+ background: white;
15
+ border-radius: 5px;
16
+ }
17
+ h1{
18
+ margin: 10px 0 20px 5px;
19
+ }
20
+ p{
21
+ float: left;
22
+ word-break: break-all;
23
+ }
24
+ p:nth-child(2n){
25
+ float: right;
26
+ }
27
+ a{
28
+ text-decoration: none;
29
+ color: black;
30
+ padding: 5px;
31
+ transition: all 0.3s;
32
+ }
33
+ a:hover{
34
+ background: #b2cde0;
35
+ color: white;
36
+ }
37
+ div{
38
+ overflow: auto;
39
+ padding: 5px 5px 5px 0;
40
+ border-left: 1px solid #0f669c;
41
+ border-right: 1px solid #0f669c;
42
+ border-bottom: 1px solid #0f669c;
43
+ }
44
+ div:nth-child(2n){
45
+ background: #edf4fa;
46
+ }
47
+ div:nth-child(2){
48
+ border-top: 1px solid #0f669c;
49
+ }
50
+ div.header{
51
+ background: #b2cde0;
52
+ padding-left: 5px;
53
+ }
54
+ div:last-child{
55
+ margin-bottom: 20px;
56
+ }
57
+ #attr{
58
+ float: none;
59
+ margin: 25px 0 0 0;
60
+ text-align: right;
61
+ font-size: 0.8em;
62
+ color: gray;
63
+ }
64
+ #attr a{
65
+ color: gray;
66
+ }
67
+ #attr a:hover{
68
+ color: #b2cde0;
69
+ background: none;
70
+ }
71
+ @media screen and (max-width: 960px){
72
+ a{
73
+ padding: 5px 0;
74
+ }
75
+ p{
76
+ width: 100%;
77
+ margin-left: 5px;
78
+ }
79
+ p:nth-child(2){
80
+ float: left;
81
+ margin-left: 5px;
82
+ }
83
+ }
trunk/css/index.php ADDED
@@ -0,0 +1 @@
 
1
+ <?php // silence is golden
trunk/css/xml.css ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ urlset{
2
+ max-width: 960px;
3
+ margin: 10px auto;
4
+ font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif;
5
+ }
6
+ url{
7
+ display: block;
8
+ overflow: auto;
9
+ padding: 5px;
10
+ font-size: 0.9em;
11
+ border-top: 1px solid gray;
12
+ border-right: 1px solid gray;
13
+ border-left: 1px solid gray;
14
+ word-break: break-all;
15
+ }
16
+ url:nth-child(2n+1){
17
+ background: whitesmoke;
18
+ }
19
+ url:last-child{
20
+ border-bottom: 1px solid gray;
21
+ }
22
+ loc{
23
+ float: left;
24
+ }
25
+ lastmod{
26
+ float: right;
27
+ }
28
+ @media screen and (max-width: 800px){
29
+ loc, lastmod{
30
+ width: 100%;
31
+ float: left;
32
+ }
33
+ }
trunk/index.php ADDED
@@ -0,0 +1 @@
 
1
+ <?php // silence is golden
trunk/readme.txt ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Simple Wp Sitemap ===
2
+ Contributors: Webbjocke
3
+ 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.1.1
8
+ Stable tag: 1.0.1
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 are 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
+ == Installation ==
23
+
24
+ 1. 1. Go to the plugins page in your wordpress admin area and hit "add new".
25
+ 2. Either search for "simple-wp-sitemap" and click install, or hit "upload plugin" and upload the zip file.
26
+ 3. Another way is by just uploading the "simple-wp-sitemap" folder via ftp to the /wp-content/plugins/ directory.
27
+
28
+ 2. Activate the plugin and thats it, done.
29
+
30
+ 3. Customize the plugin and add/block pages by hitting the "Simple Wp Sitemap" option in the settings menu.
31
+
32
+ == Frequently asked questions ==
33
+
34
+ None so far :)
35
+
36
+ == Screenshots ==
37
+
38
+ 1. Settings page
39
+
40
+ == Changelog ==
41
+
42
+ = 1.0.1 =
43
+ * Added link to settings from the plugins page
44
+ * Now also escapes output for user added urls
45
+ * Added max-length for user added urls
46
+
47
+ = 1.0.0 (March 14, 2015) =
48
+ * Initial public release
trunk/screenshot-1.png ADDED
Binary file
trunk/simple-wp-sitemap.php ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php if (!defined( 'ABSPATH' )){ die(); }
2
+
3
+ /*
4
+ * Plugin Name: Simple Wp Sitemap
5
+ * Plugin URI: https://wordpress.org/plugins/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.1
8
+ * Author: Webbjocke
9
+ * Author URI: http://www.webbjocke.com/
10
+ * License: GPLv3
11
+ */
12
+
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('<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');
46
+ }
47
+
48
+ // Interface for settings page, also handles initial post request when settings are changed
49
+ public static function sitemapAdminArea(){
50
+ require_once('simpleWpMapOptions.php');
51
+ $options = new SimpleWpMapOptions();
52
+
53
+ if (isset($_POST['simple_wp_other_urls'], $_POST['simple_wp_block_urls'])){
54
+ $options->setOptions($_POST['simple_wp_other_urls'], $_POST['simple_wp_block_urls'], isset($_POST['simple_wp_attr_link']) ? 1 : 0);
55
+ self::updateSitemaps();
56
+ } ?>
57
+
58
+ <div class="wrap">
59
+ <h1>Simple Wp Sitemap settings</h1>
60
+
61
+ <p>Change and customize the sitemap</p>
62
+
63
+ <form method="post" action="options-general.php?page=simpleWpSitemapSettings">
64
+
65
+ <?php settings_fields('simple_wp-sitemap-group'); ?>
66
+
67
+ <table class="widefat form-table">
68
+
69
+ <tr><td><b>Add pages</b></td></tr>
70
+ <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>
71
+ <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>
72
+
73
+ <tr><td><b>Block pages</b></td></tr>
74
+ <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>
75
+ <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>
76
+
77
+ <tr><td><b>Like the plugin?</b></td></tr>
78
+ <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>
79
+ <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 to bottom of sitemap.html.</label></td></tr>
80
+
81
+ </table>
82
+
83
+ <p class="submit"><input type="submit" class="button-primary" value="<?php _e('Save Changes'); ?>"></p>
84
+
85
+ </form>
86
+
87
+ </div>
88
+ <?php }
89
+ }
90
+
91
+ add_action('admin_menu', array('SimpleWpSitemap', 'sitemapAdminSetup'));
92
+ add_action('deleted_post', array('SimpleWpSitemap', 'updateSitemaps'));
93
+ add_action('save_post', array('SimpleWpSitemap', 'updateSitemaps'));
94
+ register_activation_hook(__FILE__, array('SimpleWpSitemap', 'updateSitemaps'));
95
+ register_deactivation_hook(__FILE__, array('SimpleWpSitemap', 'removeSitemaps'));
96
+ add_filter("plugin_action_links_" . plugin_basename(__FILE__), array('SimpleWpSitemap', 'pluginSettingsLink'));
trunk/simpleWpMapBuilder.php ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php if (!defined( 'ABSPATH' )){ die(); }
2
+
3
+ /*
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
+
14
+ // Constructor, the only public function this class has
15
+ public function __construct($command){
16
+ $this->url = esc_url(plugins_url() . '/simple-wp-sitemap');
17
+ $this->homeUrl = esc_url(get_home_url() . (substr(get_home_url(), -1) === '/' ? '' : '/'));
18
+
19
+ switch($command){
20
+ case 'generate':
21
+ $this->generateSitemaps();
22
+ break;
23
+ case 'delete':
24
+ $this->deleteSitemaps();
25
+ break;
26
+ }
27
+ }
28
+
29
+ // Generates the maps
30
+ private function generateSitemaps(){
31
+ $this->setUpBlockedUrls();
32
+ $this->getContent();
33
+
34
+ $this->writeToFile($this->xml, 'xml');
35
+ $this->writeToFile($this->html, 'html');
36
+ }
37
+
38
+ // Deletes the maps
39
+ private function deleteSitemaps(){
40
+ $this->deleteFile('xml');
41
+ $this->deleteFile('html');
42
+ }
43
+
44
+ // Returns other urls (not standard wordpress) user has submitted
45
+ private function getOtherPages(){
46
+ $options = get_option('simple_wp_other_urls');
47
+ $html = '';
48
+ $xml = '';
49
+
50
+ if ($options){
51
+ foreach($options as $option){
52
+ if ($option && is_array($option)){
53
+ $url = esc_url($option['url']);
54
+ $date = esc_html($option['date']);
55
+ $html .= $this->getHtml($url, $date);
56
+ $xml .= $this->getXml($url, $date);
57
+ }
58
+ }
59
+ }
60
+ return array('xml' => $xml, 'html' => $html);
61
+ }
62
+
63
+ // Sets up blocked urls into an array
64
+ private function setUpBlockedUrls(){
65
+ $blocked = get_option('simple_wp_block_urls');
66
+ if ($blocked && is_array($blocked)){
67
+ $this->blockedUrls = array();
68
+
69
+ foreach($blocked as $block){
70
+ $this->blockedUrls[] = $block['url'];
71
+ }
72
+ }
73
+ else{
74
+ $this->blockedUrls = null;
75
+ }
76
+ }
77
+
78
+ // Matches url against all blocked ones that shouldn't be displayed
79
+ private function isBlockedUrl($url){
80
+ return $this->blockedUrls && in_array($url, $this->blockedUrls);
81
+ }
82
+
83
+ // Returns an html string
84
+ private function getHtml($link, $date){
85
+ return "\t<div>\n\t\t<p><a title=\"$link\" href=\"$link\">$link</a></p>\n\t\t<p>$date</p>\n\t</div>\n";
86
+ }
87
+
88
+ // Returns an xml string
89
+ private function getXml($link, $date){
90
+ return "<url>\n\t<loc>$link</loc>\n\t<lastmod>$date</lastmod>\n</url>\n";
91
+ }
92
+
93
+ // Returns table headers with specific names (has been changed to div)
94
+ private function htmlTableH($name){
95
+ return "\t<div class=\"header\">\n\t\t<p>$name:</p>\n\t\t<p>Last modified:</p>\n\t</div>\n";
96
+ }
97
+
98
+ // Creates the actual sitemaps content, and querys the database
99
+ private function getContent(){
100
+ $q = new WP_Query('post_type=any&posts_per_page=-1');
101
+ $name = get_bloginfo('name');
102
+ $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);
103
+ $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\t<h1>%s Html Sitemap</h1>\n%s", $name, $this->url, $name, $this->htmlTableH('Home'));
104
+ $posts = array('xml' => '', 'html' => '');
105
+ $pages = array('xml' => '', 'html' => '');
106
+ $others = array('xml' => '', 'html' => '');
107
+ $homePage = false;
108
+
109
+ if ($q->have_posts()) {
110
+ while ($q->have_posts()) {
111
+ $q->the_post();
112
+
113
+ $link = esc_url(get_permalink());
114
+ $date = esc_html(get_the_modified_date('Y-m-d\TH:i:sP'));
115
+
116
+ if (!$this->isBlockedUrl($link)){
117
+ if ($link === $this->homeUrl){
118
+ $xml .= $this->getXml($link, $date);
119
+ $html .= $this->getHtml($link, $date);
120
+ $homePage = true;
121
+ }
122
+ elseif ('post' === get_post_type()){
123
+ $posts['xml'] .= $this->getXml($link, $date);
124
+ $posts['html'] .= $this->getHtml($link, $date);
125
+ }
126
+ elseif ('page' === get_post_type()){
127
+ $pages['xml'] .= $this->getXml($link, $date);
128
+ $pages['html'] .= $this->getHtml($link, $date);
129
+ }
130
+ else{
131
+ $others['xml'] .= $this->getXml($link, $date);
132
+ $others['html'] .= $this->getHtml($link, $date);
133
+ }
134
+ }
135
+ }
136
+ }
137
+
138
+ $localArr = $this->mergeArraysAndGetOtherPages($posts, $pages, $others, $homePage);
139
+
140
+ $this->xml = sprintf("%s%s</urlset>", $xml, $localArr['xml']);
141
+ $this->html = sprintf("%s%s\n\t%s</body>\n</html>", $html, $localArr['html'], $this->attributionLink());
142
+ wp_reset_postdata();
143
+ }
144
+
145
+ // Displays attribution link. Not default, user has to check a checkbox for this to be displayed (returns an empty string as default)
146
+ private function attributionLink(){
147
+ if (get_option('simple_wp_attr_link')){
148
+ return "<p id=\"attr\">Generated by: <a href=\"http://www.webbjocke.com/\">Simple Wp Sitemap</a></p>"; // will be changed to like webbjocke.com/simple-wp-sitemap or plugin page on wordpress.org if the plugin gets accepted
149
+ }
150
+ return '';
151
+ }
152
+
153
+ // Merges the arrays with post data into strings and gets user submitted pages
154
+ private function mergeArraysAndGetOtherPages($posts, $pages, $others, $homePage){
155
+ $xml = '';
156
+ $html = '';
157
+
158
+ 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)
159
+ $date = date('Y-m-d\TH:i:sP');
160
+ $xml .= $this->getXml($this->homeUrl, $date);
161
+ $html .= $this->getHtml($this->homeUrl, $date);
162
+ }
163
+
164
+ if ($posts['xml']) {
165
+ $xml .= $posts['xml'];
166
+ $html .= $this->htmlTableH('Posts') . $posts['html'];
167
+ }
168
+ if ($pages['xml']) {
169
+ $xml .= $pages['xml'];
170
+ $html .= $this->htmlTableH('Pages') . $pages['html'];
171
+ }
172
+ if ($others['xml']) {
173
+ $xml .= $others['xml'];
174
+ $html .= $this->htmlTableH('Other') . $others['html'];
175
+ }
176
+
177
+ $otherPages = $this->getOtherPages();
178
+ if ($otherPages['xml']){
179
+ $xml .= $otherPages['xml'];
180
+ $html .= (!$others['html'] ? $this->htmlTableH('Other') : '') . $otherPages['html'];
181
+ }
182
+
183
+ return array('xml' => $xml, 'html' => $html);
184
+ }
185
+
186
+ // Sets up file paths to home directory
187
+ private function setFile($fileType){
188
+ $this->file = sprintf("%s%ssitemap.%s", get_home_path(), (substr(get_home_path(), -1) === '/' ? '' : '/'), $fileType);
189
+ }
190
+
191
+ // Creates sitemap files and overrides old ones if there are any
192
+ private function writeToFile($data, $fileType){
193
+ $this->setFile($fileType);
194
+ try{
195
+ $fp = fopen($this->file, 'w');
196
+ if (file_exists($this->file)){
197
+ fwrite($fp, $data);
198
+ fclose($fp);
199
+ }
200
+ }
201
+ catch(Exception $ex){
202
+ die();
203
+ }
204
+ }
205
+
206
+ // Deletes the sitemap files
207
+ private function deleteFile($fileType){
208
+ $this->setFile($fileType);
209
+ try{
210
+ unlink($this->file);
211
+ }
212
+ catch(Exception $ex){
213
+ die();
214
+ }
215
+ }
216
+ }
trunk/simpleWpMapOptions.php ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php if (!defined( 'ABSPATH' )){ die(); }
2
+
3
+ /*
4
+ * Class that handles all admin settings
5
+ */
6
+ class SimpleWpMapOptions {
7
+
8
+ // Updates the settings/options
9
+ public function setOptions($otherUrls, $blockUrls, $attrLink){
10
+ update_option('simple_wp_other_urls', $this->addUrls($otherUrls, get_option('simple_wp_other_urls')));
11
+ update_option('simple_wp_block_urls', $this->addUrls($blockUrls));
12
+ update_option('simple_wp_attr_link', $attrLink);
13
+ }
14
+
15
+ // Returns the options as strings to be displayed in textareas, and checkbox values
16
+ public function getOptions($val){
17
+ switch($val){
18
+ case 'simple_wp_other_urls':
19
+ $val = get_option('simple_wp_other_urls');
20
+ break;
21
+ case 'simple_wp_block_urls':
22
+ $val = get_option('simple_wp_block_urls');
23
+ break;
24
+ case 'simple_wp_attr_link':
25
+ $val = get_option('simple_wp_attr_link');
26
+ return $val ? 'checked' : ''; // return checkbox checked value right here and dont bother with the loop below
27
+ default:
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);
37
+ }
38
+ return '';
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
+ }
77
+ }
78
+ }
79
+ return !empty($arr) ? $arr : '';
80
+ }
81
+ }