├── CHANGELOG.md ├── LICENSE ├── composer.json ├── core-sitemaps.php ├── docs ├── CONTRIBUTING.md ├── README.md ├── SETUP.md └── TESTING.md ├── inc ├── class-wp-sitemaps-index.php ├── class-wp-sitemaps-provider.php ├── class-wp-sitemaps-registry.php ├── class-wp-sitemaps-renderer.php ├── class-wp-sitemaps-stylesheet.php ├── class-wp-sitemaps.php ├── functions.php └── providers │ ├── class-wp-sitemaps-posts.php │ ├── class-wp-sitemaps-taxonomies.php │ └── class-wp-sitemaps-users.php ├── phpstan.neon.dist └── readme.txt /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | ## [Unreleased] 6 | 7 | ## [0.4.3] 8 | 9 | - Ensure compatibility with WordPress 5.5 Beta 3 ([#226](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/226)) 10 | 11 | ## [0.4.2] 12 | 13 | - Rename `wp_sitemaps_is_enabled` filter to `wp_sitemaps_enabled` to match WordPress core ([#218](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/218)) 14 | 15 | ## [0.4.1] 16 | 17 | - Add new `wp_sitemaps_posts_show_on_front_entry` filter ([#207](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/207)) 18 | 19 | ## [0.4.0] 20 | 21 | - Various updates to inline documentation ([#175](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/175)) 22 | - Improve logic when there are no sitemap entries ([#190](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/190)) 23 | - Remove `core_` prefix of functions and filters ([#182](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/182)) 24 | - Add new filters for individual sitemap entries ([#191](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/191)) 25 | - Add new filters to modify query arguments ([#183](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/183)) 26 | - Introduce `esc_xml` helper function ([#192](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/192), [#203](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/203)) 27 | - Add new `wp_sitemaps_index_entry` filter ([#184](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/184)) 28 | 29 | ## [0.3.0] 30 | 31 | - Disable sitemaps for private sites discouraging search engines ([#138](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/138)) 32 | - Remove `lastmod` field from sitemaps ([#145](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/145)) 33 | - Redirect `sitemap.xml` to `wp-sitemap.xml` if possible ([#149](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/149)) 34 | - Prevents adding trailing slashes when requesting stylesheets ([#160](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/160)) 35 | - Exclude private posts from sitemap even for logged-in users ([#165](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/165)) 36 | - Fix rewrites for object subtypes containing underscores ([#168](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/168)) 37 | - Various code refactorings ([#150](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/150), [#170](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/170), [#171](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/171), [#172](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/172), [#174](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/174)) 38 | - Various documentation improvements ([#173](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/173), [#176](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/176), [#179](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/179)) 39 | 40 | ## [0.2.0] 41 | 42 | - Fail gracefully when `SimpleXML` extension is not available ([#142](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/142)) 43 | - Fix XSL stylesheets when not using pretty permalinks ([#141](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/141)) 44 | - Flush rewrite rules upon plugin activation and deactivation ([#136](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/136)) 45 | - Prefix all sitemaps with `wp-` ([#135](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/135)) 46 | - Added missing translatable strings ([#117](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/117)) 47 | - Ensure correct type conversion for URL counts ([#120](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/120)) 48 | - Documentation improvements ([#130](https://github.com/GoogleChromeLabs/wp-sitemaps/pull/130)) 49 | 50 | ## [0.1.0] 51 | 52 | - Initial release 53 | 54 | [unreleased]: https://github.com/GoogleChromeLabs/wp-sitemaps/compare/v0.4.3...HEAD 55 | [0.4.3]: https://github.com/GoogleChromeLabs/wp-sitemaps/releases/tag/v0.4.3 56 | [0.4.2]: https://github.com/GoogleChromeLabs/wp-sitemaps/releases/tag/v0.4.2 57 | [0.4.1]: https://github.com/GoogleChromeLabs/wp-sitemaps/releases/tag/v0.4.1 58 | [0.4.0]: https://github.com/GoogleChromeLabs/wp-sitemaps/releases/tag/v0.4.0 59 | [0.3.0]: https://github.com/GoogleChromeLabs/wp-sitemaps/releases/tag/v0.3.0 60 | [0.2.0]: https://github.com/GoogleChromeLabs/wp-sitemaps/releases/tag/v0.2.0 61 | [0.1.0]: https://github.com/GoogleChromeLabs/wp-sitemaps/releases/tag/v0.1.0 62 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "googlechromelabs/wp-sitemaps", 3 | "description": "Core Sitemaps project", 4 | "type": "wordpress-plugin", 5 | "license": "GPL-2.0-or-later", 6 | "config": { 7 | "preferred-install": "dist", 8 | "sort-packages": true, 9 | "autoloader-suffix": "csm", 10 | "platform": { 11 | "php": "5.6" 12 | }, 13 | "process-timeout": 600, 14 | "vendor-dir": "vendor" 15 | }, 16 | "repositories": [ 17 | { 18 | "type": "composer", 19 | "url": "https://wpackagist.org" 20 | } 21 | ], 22 | "scripts": { 23 | "post-install-cmd": [ 24 | "@setup" 25 | ], 26 | "post-update-cmd": [ 27 | "@setup" 28 | ], 29 | "setup": [ 30 | "@composer run-script --list" 31 | ], 32 | "local:flush": [ 33 | "wp @local rewrite flush" 34 | ], 35 | "local:tests": [ 36 | "@test:phpcs", 37 | "@local:phpunit" 38 | ], 39 | "local:phpunit": [ 40 | "wp @local db query --dbuser=root --dbpass=password \"CREATE DATABASE IF NOT EXISTS wordpress_test DEFAULT CHARSET utf8\"", 41 | "vagrant ssh -c \"cd /vagrant/content/plugins/core-sitemaps && WP_TESTS_DB_PASS=password composer run test:phpunit\"" 42 | ], 43 | "test:phpcs": [ 44 | "phpcs" 45 | ], 46 | "test:phpunit": [ 47 | "vendor/bin/phpunit --colors=always" 48 | ] 49 | }, 50 | "scripts-descriptions": { 51 | "setup": "Sets up the development environment.", 52 | "local:flush": "Flush rewrite rules (local).", 53 | "local:phpunit": "Run PHPUnit tests (local).", 54 | "test:phpunit": "Run PHPUnit tests.", 55 | "test:phpcs": "Runs the PHP code sniffer." 56 | }, 57 | "require": { 58 | "php": ">=5.6.0", 59 | "composer/installers": "~1.0", 60 | "oomphinc/composer-installers-extender": "^1.1", 61 | "ext-simplexml": "*" 62 | }, 63 | "require-dev": { 64 | "ext-dom": "*", 65 | "ext-libxml": "*", 66 | "ext-xsl": "*", 67 | "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0", 68 | "phpcompatibility/phpcompatibility-wp": "^2.1", 69 | "phpunit/phpunit": "^5.7 || ^6.5 || ^7.5", 70 | "roave/security-advisories": "dev-master", 71 | "roots/wordpress": "dev-master", 72 | "sirbrillig/phpcs-variable-analysis": "^2.8", 73 | "wp-cli/wp-cli": "^2.2", 74 | "wp-cli/wp-cli-bundle": "^2.2", 75 | "wp-coding-standards/wpcs": "^2.2", 76 | "wp-phpunit/wp-phpunit": "^5.3" 77 | }, 78 | "extra": { 79 | "installer-types": [ 80 | "library" 81 | ], 82 | "wordpress-install-dir": "tests/app/www" 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /core-sitemaps.php: -------------------------------------------------------------------------------- 1 | register_rewrites(); 59 | flush_rewrite_rules( false ); 60 | } 61 | 62 | register_activation_hook( __FILE__, 'wp_sitemaps_plugin_activation' ); 63 | 64 | /** 65 | * Plugin deactivation hook. 66 | * 67 | * Adds and flushes rewrite rules. 68 | */ 69 | function wp_sitemaps_plugin_deactivation() { 70 | $sitemaps = new WP_Sitemaps(); 71 | $sitemaps->unregister_rewrites(); 72 | flush_rewrite_rules( false ); 73 | } 74 | 75 | register_deactivation_hook( __FILE__, 'wp_sitemaps_plugin_deactivation' ); 76 | -------------------------------------------------------------------------------- /docs/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Thank you for thinking about contributing to the Core Sitemaps project! Contributions can either be made in the form of code or through input on tickets. 4 | We appreciate all contributions as they help to move the project forward. 5 | 6 | ## Contributions to Issues 7 | 8 | Tickets will often need input from multiple points of view. We invite you to share your POV by adding a comment to the relevant ticket. 9 | 10 | If a ticket does not yet exist, you can create a new ticket and add it to the back log, but please do read: https://make.wordpress.org/core/2019/06/12/xml-sitemaps-feature-project-proposal/ and double check that what you are proposing is in line with what we are trying to achieve within this project's scope. 11 | 12 | ### Creating an Issue 13 | 14 | - In Github, under 'Issues' click the green 'new issue' button. 15 | - Give the issue a short but meaningful title. This should be descriptive, summarize the issue in 5-10 words at most. 16 | - Give us much detail about the issue as possible in the description box. 17 | 18 | ## Developer Contributions 19 | 20 | When contributing through code, each feature should be developed in a seperate branch. 21 | 22 | - Create a new branch, forked from `master`. 23 | - Each branch should be prefixed with `feature/` and the issue number, followed by a short-description of the task. For example: Issue 3: Index Sitemap would become `feature/3-index-sitemap`. 24 | - Regularly commit your work and push it to your remote branch on Github. 25 | 26 | ### Submitting Pull Requests ### 27 | 28 | Once you are ready to submit your code for review, you need to create a pull request (PR). 29 | 30 | - Under 'Pull Requests', click the green 'New pull request' button. 31 | - When comparing changes, ensure that the base is set to `master` and compare is set to your feature branch. 32 | - Give the PR a title. This should be descriptive, summarize what your request is about in 5-10 words at most. 33 | - Add a description, outlining what you have done, and what problem this solves. Include screenshots if possible to help visualize what changes have been introduced, and reference any open 34 | issues if one exists. 35 | 36 | When submitting a PR there are some items you should take note of: 37 | - Does the code follow the [WordPress Coding Standards](https://make.wordpress.org/core/handbook/best-practices/coding-standards/)? 38 | - Did you include unit tests (if applicable)? 39 | - Was your local copy recently pulled from `master`, so it's a clean patch? 40 | 41 | ## Contribute to the Documentation 42 | 43 | All of the documentation for this plugin lives at [/docs/](/docs/README.md) 44 | 45 | To contribute to the documentation either follow the Contributions to Issues workflow above and leave a comment about your suggested change. Or, follow the Developer Contributions workflow, and create a pull request with your suggested changes in. 46 | 47 | ## Contributor License Agreement 48 | 49 | Contributions to this project must be accompanied by a Contributor License 50 | Agreement. You (or your employer) retain the copyright to your contribution; 51 | this simply gives us permission to use and redistribute your contributions as 52 | part of the project. Head over to to see 53 | your current agreements on file or to sign a new one. 54 | 55 | You generally only need to submit a CLA once, so if you've already submitted one 56 | (even if it was for a different project), you probably don't need to do it 57 | again. 58 | 59 | ## Reporting Security Issues 60 | 61 | If you find a security issue, please do not post about it publicly anywhere. Even if there’s a report filed on one of the official security tracking sites, bringing more awareness to the security issue tends to increase people being hacked, and rarely speeds up the fixing. 62 | 63 | Please email plugins@wordpress.org with a clear and concise description of the issue. It greatly helps if you can provide us with how you verified this is an exploit (links to the plugin listing on sites like secunia.com are perfect). 64 | 65 | ## Community Guidelines 66 | 67 | This project follows 68 | [Google's Open Source Community Guidelines](https://opensource.google.com/conduct/). 69 | -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | # Sitemaps Documentation 2 | 3 | See for a project overview: https://make.wordpress.org/core/2019/06/12/xml-sitemaps-feature-project-proposal/ 4 | 5 | ## Getting Set up 6 | 7 | - Local Setup 8 | - Coding Standards 9 | 10 | Please see the [Local Setup Documentation Section](/docs/SETUP.md). 11 | 12 | ## Contributing 13 | 14 | - Input Documentation 15 | - Developer Documentation 16 | - Contribute to the Documentation 17 | 18 | Please see the [Contributing Documentation Section](/docs/CONTRIBUTING.md). 19 | 20 | 21 | ## Testing 22 | 23 | - Unit Tests 24 | 25 | Please see the [Testing Documentation Section](/docs/TESTING.md). 26 | 27 | 28 | ## Reporting Security Issues 29 | 30 | Please see [CONTRIBUTING.md](/docs/CONTRIBUTING.md#reporting-security-issues). 31 | -------------------------------------------------------------------------------- /docs/SETUP.md: -------------------------------------------------------------------------------- 1 | # Local Setup 2 | 3 | To get a local environment set up locally we would recommend cloning the [core-sitemaps-quickstart](https://github.com/humanmade/core-sitemaps-quickstart) repo and following the installation instructions there. 4 | 5 | ## Plugin Installation 6 | 7 | - Once you have a local environment configured, clone or download this repo. 8 | - If you have downloaded the plugin, extract the plugin from the zip file. 9 | - Place the plugin in the plugins folder in your local WordPress install. Usually `wp-content/plugins/` 10 | - Activate the plugin on the plugin screen. 11 | -------------------------------------------------------------------------------- /docs/TESTING.md: -------------------------------------------------------------------------------- 1 | # TESTING 2 | 3 | [TODO: Add this] 4 | 5 | If your site is not running under http://sitemaps.local then you can run the functional tests against the 6 | site as follows: 7 | 8 | ```bash 9 | #hardcode it 10 | export WP_SITEURL=http://sitemaps.local 11 | composer run test:behat-local 12 | 13 | #Any vagrant install with wp cli 14 | export WP_SITEURL=$(vagrant ssh -c "cd /vagrant && wp option get home") 15 | composer run test:behat-local 16 | ``` 17 | -------------------------------------------------------------------------------- /inc/class-wp-sitemaps-index.php: -------------------------------------------------------------------------------- 1 | registry = $registry; 37 | } 38 | 39 | /** 40 | * Gets a sitemap list for the index. 41 | * 42 | * @since 5.5.0 43 | * 44 | * @return array List of all sitemaps. 45 | */ 46 | public function get_sitemap_list() { 47 | $sitemaps = array(); 48 | 49 | $providers = $this->registry->get_sitemaps(); 50 | /* @var WP_Sitemaps_Provider $provider */ 51 | foreach ( $providers as $provider ) { 52 | $sitemap_entries = $provider->get_sitemap_entries(); 53 | 54 | // Prevent issues with array_push and empty arrays on PHP < 7.3. 55 | if ( ! $sitemap_entries ) { 56 | continue; 57 | } 58 | 59 | // Using array_push is more efficient than array_merge in a loop. 60 | array_push( $sitemaps, ...$sitemap_entries ); 61 | } 62 | 63 | return $sitemaps; 64 | } 65 | 66 | /** 67 | * Builds the URL for the sitemap index. 68 | * 69 | * @since 5.5.0 70 | * 71 | * @return string The sitemap index url. 72 | */ 73 | public function get_index_url() { 74 | /* @var WP_Rewrite $wp_rewrite */ 75 | global $wp_rewrite; 76 | 77 | $url = home_url( '/wp-sitemap.xml' ); 78 | 79 | if ( ! $wp_rewrite->using_permalinks() ) { 80 | $url = add_query_arg( 'sitemap', 'index', home_url( '/' ) ); 81 | } 82 | 83 | return $url; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /inc/class-wp-sitemaps-provider.php: -------------------------------------------------------------------------------- 1 | get_object_subtypes(); 71 | 72 | // If there are no object subtypes, include a single sitemap for the 73 | // entire object type. 74 | if ( empty( $object_subtypes ) ) { 75 | $sitemap_data[] = array( 76 | 'name' => '', 77 | 'pages' => $this->get_max_num_pages(), 78 | ); 79 | return $sitemap_data; 80 | } 81 | 82 | // Otherwise, include individual sitemaps for every object subtype. 83 | foreach ( $object_subtypes as $object_subtype_name => $data ) { 84 | $object_subtype_name = (string) $object_subtype_name; 85 | 86 | $sitemap_data[] = array( 87 | 'name' => $object_subtype_name, 88 | 'pages' => $this->get_max_num_pages( $object_subtype_name ), 89 | ); 90 | } 91 | 92 | return $sitemap_data; 93 | } 94 | 95 | /** 96 | * Lists sitemap pages exposed by this provider. 97 | * 98 | * The returned data is used to populate the sitemap entries of the index. 99 | * 100 | * @since 5.5.0 101 | * 102 | * @return array List of sitemaps. 103 | */ 104 | public function get_sitemap_entries() { 105 | $sitemaps = array(); 106 | 107 | $sitemap_types = $this->get_sitemap_type_data(); 108 | 109 | foreach ( $sitemap_types as $type ) { 110 | for ( $page = 1; $page <= $type['pages']; $page ++ ) { 111 | $sitemap_entry = array( 112 | 'loc' => $this->get_sitemap_url( $type['name'], $page ), 113 | ); 114 | 115 | /** 116 | * Filters the sitemap entry for the sitemap index. 117 | * 118 | * @since 5.5.0 119 | * 120 | * @param array $sitemap_entry Sitemap entry for the post. 121 | * @param string $object_type Object empty name. 122 | * @param string $object_subtype Object subtype name. 123 | * Empty string if the object type does not support subtypes. 124 | * @param string $page Page of results. 125 | */ 126 | $sitemap_entry = apply_filters( 'wp_sitemaps_index_entry', $sitemap_entry, $this->object_type, $type['name'], $page ); 127 | 128 | $sitemaps[] = $sitemap_entry; 129 | } 130 | } 131 | 132 | return $sitemaps; 133 | } 134 | 135 | /** 136 | * Gets the URL of a sitemap entry. 137 | * 138 | * @since 5.5.0 139 | * 140 | * @param string $name The name of the sitemap. 141 | * @param int $page The page of the sitemap. 142 | * @return string The composed URL for a sitemap entry. 143 | */ 144 | public function get_sitemap_url( $name, $page ) { 145 | /* @var WP_Rewrite $wp_rewrite */ 146 | global $wp_rewrite; 147 | 148 | if ( ! $wp_rewrite->using_permalinks() ) { 149 | return add_query_arg( 150 | // Accounts for cases where name is not included, ex: sitemaps-users-1.xml. 151 | array_filter( 152 | array( 153 | 'sitemap' => $this->name, 154 | 'sitemap-sub-type' => $name, 155 | 'paged' => $page, 156 | ) 157 | ), 158 | home_url( '/' ) 159 | ); 160 | } 161 | 162 | $basename = sprintf( 163 | '/wp-sitemap-%1$s.xml', 164 | implode( 165 | '-', 166 | // Accounts for cases where name is not included, ex: sitemaps-users-1.xml. 167 | array_filter( 168 | array( 169 | $this->name, 170 | $name, 171 | (string) $page, 172 | ) 173 | ) 174 | ) 175 | ); 176 | 177 | return home_url( $basename ); 178 | } 179 | 180 | /** 181 | * Returns the list of supported object subtypes exposed by the provider. 182 | * 183 | * @since 5.5.0 184 | * 185 | * @return array List of object subtypes objects keyed by their name. 186 | */ 187 | public function get_object_subtypes() { 188 | return array(); 189 | } 190 | } 191 | -------------------------------------------------------------------------------- /inc/class-wp-sitemaps-registry.php: -------------------------------------------------------------------------------- 1 | sitemaps[ $name ] ) ) { 38 | return false; 39 | } 40 | 41 | $this->sitemaps[ $name ] = $provider; 42 | 43 | return true; 44 | } 45 | 46 | /** 47 | * Returns a single registered sitemaps provider. 48 | * 49 | * @since 5.5.0 50 | * 51 | * @param string $name Sitemap provider name. 52 | * @return WP_Sitemaps_Provider|null Sitemaps provider if it exists, null otherwise. 53 | */ 54 | public function get_sitemap( $name ) { 55 | if ( ! isset( $this->sitemaps[ $name ] ) ) { 56 | return null; 57 | } 58 | 59 | return $this->sitemaps[ $name ]; 60 | } 61 | 62 | /** 63 | * Lists all registered sitemaps. 64 | * 65 | * @since 5.5.0 66 | * 67 | * @return array List of sitemaps. 68 | */ 69 | public function get_sitemaps() { 70 | $total_sitemaps = count( $this->sitemaps ); 71 | 72 | if ( $total_sitemaps > WP_SITEMAPS_MAX_SITEMAPS ) { 73 | return array_slice( $this->sitemaps, 0, WP_SITEMAPS_MAX_SITEMAPS, true ); 74 | } 75 | 76 | return $this->sitemaps; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /inc/class-wp-sitemaps-renderer.php: -------------------------------------------------------------------------------- 1 | get_sitemap_stylesheet_url(); 43 | if ( $stylesheet_url ) { 44 | $this->stylesheet = ''; 45 | } 46 | $stylesheet_index_url = $this->get_sitemap_index_stylesheet_url(); 47 | if ( $stylesheet_index_url ) { 48 | $this->stylesheet_index = ''; 49 | } 50 | } 51 | 52 | /** 53 | * Gets the URL for the sitemap stylesheet. 54 | * 55 | * @since 5.5.0 56 | * 57 | * @return string The sitemap stylesheet url. 58 | */ 59 | public function get_sitemap_stylesheet_url() { 60 | /* @var WP_Rewrite $wp_rewrite */ 61 | global $wp_rewrite; 62 | 63 | $sitemap_url = home_url( '/wp-sitemap.xsl' ); 64 | 65 | if ( ! $wp_rewrite->using_permalinks() ) { 66 | $sitemap_url = add_query_arg( 'sitemap-stylesheet', 'sitemap', home_url( '/' ) ); 67 | } 68 | 69 | /** 70 | * Filters the URL for the sitemap stylesheet. 71 | * 72 | * If a falsy value is returned, no stylesheet will be used and 73 | * the "raw" XML of the sitemap will be displayed. 74 | * 75 | * @since 5.5.0 76 | * 77 | * @param string $sitemap_url Full URL for the sitemaps xsl file. 78 | */ 79 | return apply_filters( 'wp_sitemaps_stylesheet_url', $sitemap_url ); 80 | } 81 | 82 | /** 83 | * Gets the URL for the sitemap index stylesheet. 84 | * 85 | * @since 5.5.0 86 | * 87 | * @return string The sitemap index stylesheet url. 88 | */ 89 | public function get_sitemap_index_stylesheet_url() { 90 | /* @var WP_Rewrite $wp_rewrite */ 91 | global $wp_rewrite; 92 | 93 | $sitemap_url = home_url( '/wp-sitemap-index.xsl' ); 94 | 95 | if ( ! $wp_rewrite->using_permalinks() ) { 96 | $sitemap_url = add_query_arg( 'sitemap-stylesheet', 'index', home_url( '/' ) ); 97 | } 98 | 99 | /** 100 | * Filters the URL for the sitemap index stylesheet. 101 | * 102 | * If a falsy value is returned, no stylesheet will be used and 103 | * the "raw" XML of the sitemap index will be displayed. 104 | * 105 | * @since 5.5.0 106 | * 107 | * @param string $sitemap_url Full URL for the sitemaps index xsl file. 108 | */ 109 | return apply_filters( 'wp_sitemaps_stylesheet_index_url', $sitemap_url ); 110 | } 111 | 112 | /** 113 | * Renders a sitemap index. 114 | * 115 | * @since 5.5.0 116 | * 117 | * @param array $sitemaps Array of sitemap URLs. 118 | */ 119 | public function render_index( $sitemaps ) { 120 | header( 'Content-type: application/xml; charset=UTF-8' ); 121 | 122 | $this->check_for_simple_xml_availability(); 123 | 124 | $index_xml = $this->get_sitemap_index_xml( $sitemaps ); 125 | 126 | if ( ! empty( $index_xml ) ) { 127 | // All output is escaped within get_sitemap_index_xml(). 128 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 129 | echo $index_xml; 130 | } 131 | } 132 | 133 | /** 134 | * Gets XML for a sitemap index. 135 | * 136 | * @since 5.5.0 137 | * 138 | * @param array $sitemaps Array of sitemap URLs. 139 | * @return string|false A well-formed XML string for a sitemap index. False on error. 140 | */ 141 | public function get_sitemap_index_xml( $sitemaps ) { 142 | $sitemap_index = new SimpleXMLElement( 143 | sprintf( 144 | '%1$s%2$s%3$s', 145 | '', 146 | $this->stylesheet_index, 147 | '' 148 | ) 149 | ); 150 | 151 | foreach ( $sitemaps as $entry ) { 152 | $sitemap = $sitemap_index->addChild( 'sitemap' ); 153 | 154 | // Add each element as a child node to the entry. 155 | foreach ( $entry as $name => $value ) { 156 | if ( 'loc' === $name ) { 157 | $sitemap->addChild( $name, esc_url( $value ) ); 158 | } elseif ( 'lastmod' === $name ) { 159 | $sitemap->addChild( $name, esc_xml( $value ) ); 160 | } else { 161 | _doing_it_wrong( 162 | __METHOD__, 163 | /* translators: %s: list of element names */ 164 | sprintf( 165 | __( 'Fields other than %s are not currently supported for the sitemap index.', 'core-sitemaps' ), 166 | implode( ',', array( 'loc', 'lastmod' ) ) 167 | ), 168 | '5.5.0' 169 | ); 170 | } 171 | } 172 | } 173 | 174 | return $sitemap_index->asXML(); 175 | } 176 | 177 | /** 178 | * Renders a sitemap. 179 | * 180 | * @since 5.5.0 181 | * 182 | * @param array $url_list Array of URLs for a sitemap. 183 | */ 184 | public function render_sitemap( $url_list ) { 185 | header( 'Content-type: application/xml; charset=UTF-8' ); 186 | 187 | $this->check_for_simple_xml_availability(); 188 | 189 | $sitemap_xml = $this->get_sitemap_xml( $url_list ); 190 | 191 | if ( ! empty( $sitemap_xml ) ) { 192 | // All output is escaped within get_sitemap_xml(). 193 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 194 | echo $sitemap_xml; 195 | } 196 | } 197 | 198 | /** 199 | * Gets XML for a sitemap. 200 | * 201 | * @since 5.5.0 202 | * 203 | * @param array $url_list Array of URLs for a sitemap. 204 | * @return string|false A well-formed XML string for a sitemap index. False on error. 205 | */ 206 | public function get_sitemap_xml( $url_list ) { 207 | $urlset = new SimpleXMLElement( 208 | sprintf( 209 | '%1$s%2$s%3$s', 210 | '', 211 | $this->stylesheet, 212 | '' 213 | ) 214 | ); 215 | 216 | foreach ( $url_list as $url_item ) { 217 | $url = $urlset->addChild( 'url' ); 218 | 219 | // Add each element as a child node to the entry. 220 | foreach ( $url_item as $name => $value ) { 221 | if ( 'loc' === $name ) { 222 | $url->addChild( $name, esc_url( $value ) ); 223 | } elseif ( in_array( $name, array( 'lastmod', 'changefreq', 'priority' ), true ) ) { 224 | $url->addChild( $name, esc_xml( $value ) ); 225 | } else { 226 | _doing_it_wrong( 227 | __METHOD__, 228 | /* translators: %s: list of element names */ 229 | sprintf( 230 | __( 'Fields other than %s are not currently supported for sitemaps.', 'core-sitemaps' ), 231 | implode( ',', array( 'loc', 'lastmod', 'changefreq', 'priority' ) ) 232 | ), 233 | '5.5.0' 234 | ); 235 | } 236 | } 237 | } 238 | 239 | return $urlset->asXML(); 240 | } 241 | 242 | /** 243 | * Checks for the availability of the SimpleXML extension and errors if missing. 244 | * 245 | * @since 5.5.0 246 | */ 247 | private function check_for_simple_xml_availability() { 248 | if ( ! class_exists( 'SimpleXMLElement' ) ) { 249 | add_filter( 250 | 'wp_die_handler', 251 | static function () { 252 | return '_xml_wp_die_handler'; 253 | } 254 | ); 255 | 256 | wp_die( 257 | sprintf( 258 | /* translators: %s: SimpleXML */ 259 | esc_xml( __( 'Could not generate XML sitemap due to missing %s extension', 'core-sitemaps' ) ), 260 | 'SimpleXML' 261 | ), 262 | esc_xml( __( 'WordPress › Error', 'core-sitemaps' ) ), 263 | array( 264 | 'response' => 501, // "Not implemented". 265 | ) 266 | ); 267 | } 268 | } 269 | } 270 | -------------------------------------------------------------------------------- /inc/class-wp-sitemaps-stylesheet.php: -------------------------------------------------------------------------------- 1 | get_sitemap_stylesheet(); 29 | } 30 | 31 | if ( 'index' === $type ) { 32 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- All content escaped below. 33 | echo $this->get_sitemap_index_stylesheet(); 34 | } 35 | 36 | exit; 37 | } 38 | 39 | /** 40 | * Returns the escaped xsl for all sitemaps, except index. 41 | * 42 | * @since 5.5.0 43 | */ 44 | public function get_sitemap_stylesheet() { 45 | $css = $this->get_stylesheet_css(); 46 | $title = esc_xml( __( 'XML Sitemap', 'core-sitemaps' ) ); 47 | $description = esc_xml( __( 'This XML Sitemap is generated by WordPress to make your content more visible for search engines.', 'core-sitemaps' ) ); 48 | $learn_more = sprintf( 49 | '%s', 50 | esc_url( __( 'https://www.sitemaps.org/', 'core-sitemaps' ) ), 51 | esc_xml( __( 'Learn more about XML sitemaps.', 'core-sitemaps' ) ) 52 | ); 53 | 54 | $text = sprintf( 55 | /* translators: %s: number of URLs. */ 56 | esc_xml( __( 'Number of URLs in this XML Sitemap: %s.', 'core-sitemaps' ) ), 57 | '' 58 | ); 59 | 60 | $lang = get_language_attributes( 'html' ); 61 | $url = esc_xml( __( 'URL', 'core-sitemaps' ) ); 62 | $lastmod = esc_xml( __( 'Last Modified', 'core-sitemaps' ) ); 63 | $changefreq = esc_xml( __( 'Change Frequency', 'core-sitemaps' ) ); 64 | $priority = esc_xml( __( 'Priority', 'core-sitemaps' ) ); 65 | 66 | $xsl_content = << 68 | 74 | 75 | 76 | 77 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | {$title} 89 | 90 | 91 | 92 |
93 |

{$title}

94 |

{$description}

95 |

{$learn_more}

96 |
97 |
98 |

{$text}

99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 |
{$url}{$lastmod}{$changefreq}{$priority}
131 |
132 | 133 | 134 |
135 |
136 | 137 | XSL; 138 | 139 | /** 140 | * Filters the content of the sitemap stylesheet. 141 | * 142 | * @since 5.5.0 143 | * 144 | * @param string $xsl Full content for the xml stylesheet. 145 | */ 146 | return apply_filters( 'wp_sitemaps_stylesheet_content', $xsl_content ); 147 | } 148 | 149 | /** 150 | * Returns the escaped xsl for the index sitemaps. 151 | * 152 | * @since 5.5.0 153 | */ 154 | public function get_sitemap_index_stylesheet() { 155 | $css = $this->get_stylesheet_css(); 156 | $title = esc_xml( __( 'XML Sitemap', 'core-sitemaps' ) ); 157 | $description = esc_xml( __( 'This XML Sitemap is generated by WordPress to make your content more visible for search engines.', 'core-sitemaps' ) ); 158 | $learn_more = sprintf( 159 | '%s', 160 | esc_url( __( 'https://www.sitemaps.org/', 'core-sitemaps' ) ), 161 | esc_xml( __( 'Learn more about XML sitemaps.', 'core-sitemaps' ) ) 162 | ); 163 | 164 | $text = sprintf( 165 | /* translators: %s: number of URLs. */ 166 | esc_xml( __( 'Number of URLs in this XML Sitemap: %s.', 'core-sitemaps' ) ), 167 | '' 168 | ); 169 | 170 | $lang = get_language_attributes( 'html' ); 171 | $url = esc_xml( __( 'URL', 'core-sitemaps' ) ); 172 | $lastmod = esc_xml( __( 'Last Modified', 'core-sitemaps' ) ); 173 | 174 | $xsl_content = << 176 | 182 | 183 | 184 | 185 | 189 | 190 | 191 | 192 | 193 | 194 | {$title} 195 | 196 | 197 | 198 |
199 |

{$title}

200 |

{$description}

201 |

{$learn_more}

202 |
203 |
204 |

{$text}

205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 |
{$url}{$lastmod}
225 |
226 | 227 | 228 |
229 |
230 | 231 | XSL; 232 | 233 | /** 234 | * Filters the content of the sitemap index stylesheet. 235 | * 236 | * @since 5.5.0 237 | * 238 | * @param string $xsl Full content for the xml stylesheet. 239 | */ 240 | return apply_filters( 'wp_sitemaps_stylesheet_index_content', $xsl_content ); 241 | } 242 | 243 | /** 244 | * Gets the CSS to be included in sitemap XSL stylesheets. 245 | * 246 | * @since 5.5.0 247 | * 248 | * @return string The CSS. 249 | */ 250 | public function get_stylesheet_css() { 251 | $css = ' 252 | body { 253 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; 254 | color: #444; 255 | } 256 | 257 | #sitemap__table { 258 | border: solid 1px #ccc; 259 | border-collapse: collapse; 260 | } 261 | 262 | #sitemap__table tr th { 263 | text-align: left; 264 | } 265 | 266 | #sitemap__table tr td, 267 | #sitemap__table tr th { 268 | padding: 10px; 269 | } 270 | 271 | #sitemap__table tr:nth-child(odd) td { 272 | background-color: #eee; 273 | } 274 | 275 | a:hover { 276 | text-decoration: none; 277 | }'; 278 | 279 | /** 280 | * Filters the css only for the sitemap stylesheet. 281 | * 282 | * @since 5.5.0 283 | * 284 | * @param string $css CSS to be applied to default xsl file. 285 | */ 286 | return apply_filters( 'wp_sitemaps_stylesheet_css', $css ); 287 | } 288 | } 289 | -------------------------------------------------------------------------------- /inc/class-wp-sitemaps.php: -------------------------------------------------------------------------------- 1 | registry = new WP_Sitemaps_Registry(); 52 | $this->renderer = new WP_Sitemaps_Renderer(); 53 | $this->index = new WP_Sitemaps_Index( $this->registry ); 54 | } 55 | 56 | /** 57 | * Initiates all sitemap functionality. 58 | * 59 | * @since 5.5.0 60 | */ 61 | public function init() { 62 | // These will all fire on the init hook. 63 | $this->register_sitemaps(); 64 | 65 | // Add additional action callbacks. 66 | add_action( 'wp_sitemaps_init', array( $this, 'register_rewrites' ) ); 67 | add_action( 'template_redirect', array( $this, 'render_sitemaps' ) ); 68 | add_action( 'wp_loaded', array( $this, 'maybe_flush_rewrites' ) ); 69 | add_filter( 'pre_handle_404', array( $this, 'redirect_sitemapxml' ), 10, 2 ); 70 | add_filter( 'robots_txt', array( $this, 'add_robots' ), 0, 2 ); 71 | add_filter( 'redirect_canonical', array( $this, 'redirect_canonical' ) ); 72 | } 73 | 74 | /** 75 | * Registers and sets up the functionality for all supported sitemaps. 76 | * 77 | * @since 5.5.0 78 | */ 79 | public function register_sitemaps() { 80 | /** 81 | * Filters the list of registered sitemap providers. 82 | * 83 | * @since 5.5.0 84 | * 85 | * @param array $providers { 86 | * Array of WP_Sitemaps_Provider objects keyed by their name. 87 | * 88 | * @type object $posts The WP_Sitemaps_Posts object. 89 | * @type object $taxonomies The WP_Sitemaps_Taxonomies object. 90 | * @type object $users The WP_Sitemaps_Users object. 91 | * } 92 | */ 93 | $providers = apply_filters( 94 | 'wp_sitemaps_register_providers', 95 | array( 96 | 'posts' => new WP_Sitemaps_Posts(), 97 | 'taxonomies' => new WP_Sitemaps_Taxonomies(), 98 | 'users' => new WP_Sitemaps_Users(), 99 | ) 100 | ); 101 | 102 | // Register each supported provider. 103 | /* @var WP_Sitemaps_Provider $provider */ 104 | foreach ( $providers as $name => $provider ) { 105 | $this->registry->add_sitemap( $name, $provider ); 106 | } 107 | } 108 | 109 | /** 110 | * Registers sitemap rewrite tags and routing rules. 111 | * 112 | * @since 5.5.0 113 | */ 114 | public function register_rewrites() { 115 | // Add rewrite tags. 116 | add_rewrite_tag( '%sitemap%', '([^?]+)' ); 117 | add_rewrite_tag( '%sitemap-sub-type%', '([^?]+)' ); 118 | 119 | // Register index route. 120 | add_rewrite_rule( '^wp-sitemap\.xml$', 'index.php?sitemap=index', 'top' ); 121 | 122 | // Register rewrites for the XSL stylesheet. 123 | add_rewrite_tag( '%sitemap-stylesheet%', '([^?]+)' ); 124 | add_rewrite_rule( '^wp-sitemap\.xsl$', 'index.php?sitemap-stylesheet=sitemap', 'top' ); 125 | add_rewrite_rule( '^wp-sitemap-index\.xsl$', 'index.php?sitemap-stylesheet=index', 'top' ); 126 | 127 | // Register routes for providers. 128 | add_rewrite_rule( 129 | '^wp-sitemap-([a-z]+?)-([a-z\d_-]+?)-(\d+?)\.xml$', 130 | 'index.php?sitemap=$matches[1]&sitemap-sub-type=$matches[2]&paged=$matches[3]', 131 | 'top' 132 | ); 133 | add_rewrite_rule( 134 | '^wp-sitemap-([a-z]+?)-(\d+?)\.xml$', 135 | 'index.php?sitemap=$matches[1]&paged=$matches[2]', 136 | 'top' 137 | ); 138 | } 139 | 140 | /** 141 | * Un-registers sitemap rewrite tags and routing rules. 142 | * 143 | * @since 5.5.0 144 | */ 145 | public function unregister_rewrites() { 146 | /* @var WP_Rewrite $wp_rewrite */ 147 | global $wp_rewrite; 148 | 149 | // Unregister index route. 150 | unset( $wp_rewrite->extra_rules_top['^wp-sitemap\.xml$'] ); 151 | 152 | // Unregister rewrites for the XSL stylesheet. 153 | unset( $wp_rewrite->extra_rules_top['^wp-sitemap\.xsl$'] ); 154 | unset( $wp_rewrite->extra_rules_top['^wp-sitemap-index\.xsl$'] ); 155 | 156 | // Unregister routes for providers. 157 | unset( $wp_rewrite->extra_rules_top['^wp-sitemap-([a-z]+?)-([a-z\d-]+?)-(\d+?)\.xml$'] ); 158 | unset( $wp_rewrite->extra_rules_top['^wp-sitemap-([a-z]+?)-(\d+?)\.xml$'] ); 159 | } 160 | 161 | /** 162 | * Flushes rewrite rules if developers updated them. 163 | * 164 | * @since 5.5.0 165 | */ 166 | public function maybe_flush_rewrites() { 167 | if ( update_option( 'wp_sitemaps_rewrite_version', WP_SITEMAPS_REWRITE_VERSION ) ) { 168 | flush_rewrite_rules( false ); 169 | } 170 | } 171 | 172 | /** 173 | * Renders sitemap templates based on rewrite rules. 174 | * 175 | * @since 5.5.0 176 | */ 177 | public function render_sitemaps() { 178 | global $wp_query; 179 | 180 | $sitemap = sanitize_text_field( get_query_var( 'sitemap' ) ); 181 | $object_subtype = sanitize_text_field( get_query_var( 'sitemap-sub-type' ) ); 182 | $stylesheet_type = sanitize_text_field( get_query_var( 'sitemap-stylesheet' ) ); 183 | $paged = absint( get_query_var( 'paged' ) ); 184 | 185 | // Bail early if this isn't a sitemap or stylesheet route. 186 | if ( ! ( $sitemap || $stylesheet_type ) ) { 187 | return; 188 | } 189 | 190 | // Render stylesheet if this is stylesheet route. 191 | if ( $stylesheet_type ) { 192 | $stylesheet = new WP_Sitemaps_Stylesheet(); 193 | 194 | $stylesheet->render_stylesheet( $stylesheet_type ); 195 | exit; 196 | } 197 | 198 | // Render the index. 199 | if ( 'index' === $sitemap ) { 200 | $sitemap_list = $this->index->get_sitemap_list(); 201 | 202 | $this->renderer->render_index( $sitemap_list ); 203 | exit; 204 | } 205 | 206 | $provider = $this->registry->get_sitemap( $sitemap ); 207 | 208 | if ( ! $provider ) { 209 | return; 210 | } 211 | 212 | if ( empty( $paged ) ) { 213 | $paged = 1; 214 | } 215 | 216 | $url_list = $provider->get_url_list( $paged, $object_subtype ); 217 | 218 | // Force a 404 and bail early if no URLs are present. 219 | if ( empty( $url_list ) ) { 220 | $wp_query->set_404(); 221 | return; 222 | } 223 | 224 | $this->renderer->render_sitemap( $url_list ); 225 | exit; 226 | } 227 | 228 | /** 229 | * Redirects a URL to the wp-sitemap.xml 230 | * 231 | * @since 5.5.0 232 | * 233 | * @param bool $bypass Pass-through of the pre_handle_404 filter value. 234 | * @param WP_Query $query The WP_Query object. 235 | * @return bool Bypass value. 236 | */ 237 | public function redirect_sitemapxml( $bypass, $query ) { 238 | // If a plugin has already utilized the pre_handle_404 function, return without action to avoid conflicts. 239 | if ( $bypass ) { 240 | return $bypass; 241 | } 242 | 243 | // 'pagename' is for most permalink types, name is for when the %postname% is used as a top-level field. 244 | if ( 'sitemap-xml' === $query->get( 'pagename' ) || 245 | 'sitemap-xml' === $query->get( 'name' ) ) { 246 | wp_safe_redirect( $this->index->get_index_url() ); 247 | exit(); 248 | } 249 | 250 | return $bypass; 251 | } 252 | 253 | /** 254 | * Adds the sitemap index to robots.txt. 255 | * 256 | * @since 5.5.0 257 | * 258 | * @param string $output robots.txt output. 259 | * @param bool $public Whether the site is public or not. 260 | * @return string The robots.txt output. 261 | */ 262 | public function add_robots( $output, $public ) { 263 | if ( $public ) { 264 | $output .= "\nSitemap: " . esc_url( $this->index->get_index_url() ) . "\n"; 265 | } 266 | 267 | return $output; 268 | } 269 | 270 | /** 271 | * Prevent trailing slashes. 272 | * 273 | * @since 5.5.0 274 | * 275 | * @param string $redirect The redirect URL currently determined. 276 | * @return bool|string $redirect The canonical redirect URL. 277 | */ 278 | public function redirect_canonical( $redirect ) { 279 | if ( get_query_var( 'sitemap' ) || get_query_var( 'sitemap-stylesheet' ) ) { 280 | return false; 281 | } 282 | 283 | return $redirect; 284 | } 285 | } 286 | -------------------------------------------------------------------------------- /inc/functions.php: -------------------------------------------------------------------------------- 1 | init(); 49 | 50 | /** 51 | * Fires when initializing the Sitemaps object. 52 | * 53 | * Additional sitemaps should be registered on this hook. 54 | * 55 | * @since 5.5.0 56 | * 57 | * @param WP_Sitemaps $sitemaps Server object. 58 | */ 59 | do_action( 'wp_sitemaps_init', $wp_sitemaps ); 60 | } 61 | 62 | return $wp_sitemaps; 63 | } 64 | 65 | /** 66 | * Gets a list of sitemap providers. 67 | * 68 | * @since 5.5.0 69 | * 70 | * @return array $sitemaps A list of registered sitemap providers. 71 | */ 72 | function wp_get_sitemaps() { 73 | $sitemaps = wp_sitemaps_get_server(); 74 | 75 | if ( ! $sitemaps ) { 76 | return array(); 77 | } 78 | 79 | return $sitemaps->registry->get_sitemaps(); 80 | } 81 | 82 | /** 83 | * Registers a new sitemap provider. 84 | * 85 | * @since 5.5.0 86 | * 87 | * @param string $name Unique name for the sitemap provider. 88 | * @param WP_Sitemaps_Provider $provider The `Sitemaps_Provider` instance implementing the sitemap. 89 | * @return bool Returns true if the sitemap was added. False on failure. 90 | */ 91 | function wp_register_sitemap( $name, WP_Sitemaps_Provider $provider ) { 92 | $sitemaps = wp_sitemaps_get_server(); 93 | 94 | if ( ! $sitemaps ) { 95 | return false; 96 | } 97 | 98 | return $sitemaps->registry->add_sitemap( $name, $provider ); 99 | } 100 | 101 | /** 102 | * Gets the maximum number of URLs for a sitemap. 103 | * 104 | * @since 5.5.0 105 | * 106 | * @param string $object_type Object type for sitemap to be filtered (e.g. 'post', 'term', 'user'). 107 | * @return int The maximum number of URLs. 108 | */ 109 | function wp_sitemaps_get_max_urls( $object_type ) { 110 | /** 111 | * Filters the maximum number of URLs displayed on a sitemap. 112 | * 113 | * @since 5.5.0 114 | * 115 | * @param int $max_urls The maximum number of URLs included in a sitemap. Default 2000. 116 | * @param string $object_type Object type for sitemap to be filtered (e.g. 'post', 'term', 'user'). 117 | */ 118 | return apply_filters( 'wp_sitemaps_max_urls', WP_SITEMAPS_MAX_URLS, $object_type ); 119 | } 120 | 121 | if ( ! function_exists( 'esc_xml' ) ) : 122 | /** 123 | * Escaping for XML blocks. 124 | * 125 | * @since 5.5.0 126 | * 127 | * @param string $text Text to escape. 128 | * @return string 129 | */ 130 | function esc_xml( $text ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals 131 | $safe_text = wp_check_invalid_utf8( $text ); 132 | 133 | $cdata_regex = '\<\!\[CDATA\[.*?\]\]\>'; 134 | $regex = <<(.*?)) # the "anything" matched by the lookahead 138 | (?({$cdata_regex})) # the CDATA Section matched by the lookahead 139 | 140 | | # alternative 141 | 142 | (?(.*)) # non-CDATA Section 143 | /sx 144 | EOF; 145 | $safe_text = (string) preg_replace_callback( 146 | $regex, 147 | function( $matches ) { 148 | if ( ! $matches[0] ) { 149 | return ''; 150 | } elseif ( ! empty( $matches['non_cdata'] ) ) { 151 | // escape HTML entities in the non-CDATA Section. 152 | return _esc_xml_non_cdata_section( $matches['non_cdata'] ); 153 | } 154 | 155 | // Return the CDATA Section unchanged, escape HTML entities in the rest. 156 | return _esc_xml_non_cdata_section( $matches['non_cdata_followed_by_cdata'] ) . $matches['cdata']; 157 | }, 158 | $safe_text 159 | ); 160 | 161 | /** 162 | * Filters a string cleaned and escaped for output in XML. 163 | * 164 | * Text passed to esc_xml() is stripped of invalid or special characters 165 | * before output. HTML named character references are converted to their 166 | * equivalent code points. 167 | * 168 | * @since 5.5.0 169 | * 170 | * @param string $safe_text The text after it has been escaped. 171 | * @param string $text The text prior to being escaped. 172 | */ 173 | return apply_filters( 'esc_xml', $safe_text, $text ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals 174 | } 175 | endif; 176 | 177 | if ( ! function_exists( '_esc_xml_non_cdata_section' ) ) : 178 | /** 179 | * Escaping for non-CDATA Section XML blocks. 180 | * 181 | * @access private 182 | * @since 5.5.0 183 | * 184 | * @param string $text Text to escape. 185 | * @return string 186 | */ 187 | function _esc_xml_non_cdata_section( $text ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals 188 | global $allowedentitynames; 189 | 190 | $safe_text = _wp_specialchars( $text, ENT_QUOTES ); 191 | // Replace HTML entities with their Unicode codepoints, 192 | // without doing the same for the 5 XML entities. 193 | $html_only_entities = array_diff( $allowedentitynames, array( 'amp', 'lt', 'gt', 'apos', 'quot' ) ); 194 | $safe_text = (string) preg_replace_callback( 195 | '/&(' . implode( '|', $html_only_entities ) . ');/', 196 | function( $matches ) { 197 | return html_entity_decode( $matches[0], ENT_HTML5 ); 198 | }, 199 | $safe_text 200 | ); 201 | 202 | return $safe_text; 203 | } 204 | endif; 205 | -------------------------------------------------------------------------------- /inc/providers/class-wp-sitemaps-posts.php: -------------------------------------------------------------------------------- 1 | name = 'posts'; 25 | $this->object_type = 'post'; 26 | } 27 | 28 | /** 29 | * Returns the public post types, which excludes nav_items and similar types. 30 | * Attachments are also excluded. This includes custom post types with public = true. 31 | * 32 | * @since 5.5.0 33 | * 34 | * @return array Map of registered post type objects (WP_Post_Type) keyed by their name. 35 | */ 36 | public function get_object_subtypes() { 37 | $post_types = get_post_types( array( 'public' => true ), 'objects' ); 38 | unset( $post_types['attachment'] ); 39 | 40 | /** 41 | * Filters the list of post object subtypes available within the sitemap. 42 | * 43 | * @since 5.5.0 44 | * 45 | * @param array $post_types Map of registered post type objects (WP_Post_Type) keyed by their name. 46 | */ 47 | return apply_filters( 'wp_sitemaps_post_types', $post_types ); 48 | } 49 | 50 | /** 51 | * Gets a URL list for a post type sitemap. 52 | * 53 | * @since 5.5.0 54 | * 55 | * @param int $page_num Page of results. 56 | * @param string $post_type Optional. Post type name. Default empty. 57 | * @return array $url_list Array of URLs for a sitemap. 58 | */ 59 | public function get_url_list( $page_num, $post_type = '' ) { 60 | // Bail early if the queried post type is not supported. 61 | $supported_types = $this->get_object_subtypes(); 62 | 63 | if ( ! isset( $supported_types[ $post_type ] ) ) { 64 | return array(); 65 | } 66 | 67 | /** 68 | * Filters the posts URL list before it is generated. 69 | * 70 | * Passing a non-null value will effectively short-circuit the generation, 71 | * returning that value instead. 72 | * 73 | * @since 5.5.0 74 | * 75 | * @param array $url_list The URL list. Default null. 76 | * @param string $post_type Post type name. 77 | * @param int $page_num Page of results. 78 | */ 79 | $url_list = apply_filters( 80 | 'wp_sitemaps_posts_pre_url_list', 81 | null, 82 | $post_type, 83 | $page_num 84 | ); 85 | 86 | if ( null !== $url_list ) { 87 | return $url_list; 88 | } 89 | 90 | $args = $this->get_posts_query_args( $post_type ); 91 | $args['paged'] = $page_num; 92 | 93 | $query = new WP_Query( $args ); 94 | 95 | /** 96 | * Returns an array of posts. 97 | * 98 | * @var array $posts 99 | */ 100 | $posts = $query->get_posts(); 101 | 102 | $url_list = array(); 103 | 104 | /* 105 | * Add a URL for the homepage in the pages sitemap. 106 | * Shows only on the first page if the reading settings are set to display latest posts. 107 | */ 108 | if ( 'page' === $post_type && 1 === $page_num && 'posts' === get_option( 'show_on_front' ) ) { 109 | // Extract the data needed for home URL to add to the array. 110 | $sitemap_entry = array( 111 | 'loc' => home_url(), 112 | ); 113 | 114 | /** 115 | * Filters the sitemap entry for the home page when the 'show_on_front' option equals 'posts'. 116 | * 117 | * @since 5.5.0 118 | * 119 | * @param array $sitemap_entry Sitemap entry for the home page. 120 | */ 121 | $sitemap_entry = apply_filters( 'wp_sitemaps_posts_show_on_front_entry', $sitemap_entry ); 122 | $url_list[] = $sitemap_entry; 123 | } 124 | 125 | foreach ( $posts as $post ) { 126 | $sitemap_entry = array( 127 | 'loc' => get_permalink( $post ), 128 | ); 129 | 130 | /** 131 | * Filters the sitemap entry for an individual post. 132 | * 133 | * @since 5.5.0 134 | * 135 | * @param array $sitemap_entry Sitemap entry for the post. 136 | * @param WP_Post $post Post object. 137 | * @param string $post_type Name of the post_type. 138 | */ 139 | $sitemap_entry = apply_filters( 'wp_sitemaps_posts_entry', $sitemap_entry, $post, $post_type ); 140 | $url_list[] = $sitemap_entry; 141 | } 142 | 143 | return $url_list; 144 | } 145 | 146 | /** 147 | * Gets the max number of pages available for the object type. 148 | * 149 | * @since 5.5.0 150 | * 151 | * @param string $post_type Optional. Post type name. Default empty. 152 | * @return int Total number of pages. 153 | */ 154 | public function get_max_num_pages( $post_type = '' ) { 155 | if ( empty( $post_type ) ) { 156 | return 0; 157 | } 158 | 159 | /** 160 | * Filters the max number of pages before it is generated. 161 | * 162 | * Passing a non-null value will effectively short-circuit the generation, 163 | * returning that value instead. 164 | * 165 | * @since 5.5.0 166 | * 167 | * @param int $max_num_pages The maximum number of pages. Default null. 168 | * @param string $post_type Post type name. 169 | */ 170 | $max_num_pages = apply_filters( 'wp_sitemaps_posts_pre_max_num_pages', null, $post_type ); 171 | 172 | if ( null !== $max_num_pages ) { 173 | return $max_num_pages; 174 | } 175 | 176 | $args = $this->get_posts_query_args( $post_type ); 177 | $args['fields'] = 'ids'; 178 | $args['no_found_rows'] = false; 179 | 180 | $query = new WP_Query( $args ); 181 | 182 | return isset( $query->max_num_pages ) ? $query->max_num_pages : 1; 183 | } 184 | 185 | /** 186 | * Returns the query args for retrieving posts to list in the sitemap. 187 | * 188 | * @since 5.5.0 189 | * 190 | * @param string $post_type Post type name. 191 | * @return array $args Array of WP_Query arguments. 192 | */ 193 | protected function get_posts_query_args( $post_type ) { 194 | /** 195 | * Filters the query arguments for post type sitemap queries. 196 | * 197 | * @see WP_Query for a full list of arguments. 198 | * 199 | * @since 5.5.0 200 | * 201 | * @param array $args Array of WP_Query arguments. 202 | * @param string $post_type Post type name. 203 | */ 204 | $args = apply_filters( 205 | 'wp_sitemaps_posts_query_args', 206 | array( 207 | 'orderby' => 'ID', 208 | 'order' => 'ASC', 209 | 'post_type' => $post_type, 210 | 'posts_per_page' => wp_sitemaps_get_max_urls( $this->object_type ), 211 | 'post_status' => array( 'publish' ), 212 | 'no_found_rows' => true, 213 | 'update_post_term_cache' => false, 214 | 'update_post_meta_cache' => false, 215 | ), 216 | $post_type 217 | ); 218 | 219 | return $args; 220 | } 221 | } 222 | -------------------------------------------------------------------------------- /inc/providers/class-wp-sitemaps-taxonomies.php: -------------------------------------------------------------------------------- 1 | name = 'taxonomies'; 25 | $this->object_type = 'term'; 26 | } 27 | 28 | /** 29 | * Returns all public, registered taxonomies. 30 | * 31 | * @since 5.5.0 32 | * 33 | * @return array Map of registered taxonomy objects keyed by their name. 34 | */ 35 | public function get_object_subtypes() { 36 | $taxonomies = get_taxonomies( array( 'public' => true ), 'objects' ); 37 | 38 | /** 39 | * Filter the list of taxonomy object subtypes available within the sitemap. 40 | * 41 | * @since 5.5.0 42 | * 43 | * @param array $taxonomies Map of registered taxonomy objects keyed by their name. 44 | */ 45 | return apply_filters( 'wp_sitemaps_taxonomies', $taxonomies ); 46 | } 47 | 48 | /** 49 | * Gets a URL list for a taxonomy sitemap. 50 | * 51 | * @since 5.5.0 52 | * 53 | * @param int $page_num Page of results. 54 | * @param string $taxonomy Optional. Taxonomy name. Default empty. 55 | * @return array $url_list Array of URLs for a sitemap. 56 | */ 57 | public function get_url_list( $page_num, $taxonomy = '' ) { 58 | $supported_types = $this->get_object_subtypes(); 59 | 60 | // Bail early if the queried taxonomy is not supported. 61 | if ( ! isset( $supported_types[ $taxonomy ] ) ) { 62 | return array(); 63 | } 64 | 65 | /** 66 | * Filters the taxonomies URL list before it is generated. 67 | * 68 | * Passing a non-null value will effectively short-circuit the generation, 69 | * returning that value instead. 70 | * 71 | * @since 5.5.0 72 | * 73 | * @param array $url_list The URL list. Default null. 74 | * @param string $taxonomy Taxonomy name. 75 | * @param int $page_num Page of results. 76 | */ 77 | $url_list = apply_filters( 78 | 'wp_sitemaps_taxonomies_pre_url_list', 79 | null, 80 | $taxonomy, 81 | $page_num 82 | ); 83 | 84 | if ( null !== $url_list ) { 85 | return $url_list; 86 | } 87 | 88 | $url_list = array(); 89 | 90 | // Offset by how many terms should be included in previous pages. 91 | $offset = ( $page_num - 1 ) * wp_sitemaps_get_max_urls( $this->object_type ); 92 | 93 | $args = $this->get_taxonomies_query_args( $taxonomy ); 94 | $args['offset'] = $offset; 95 | 96 | $taxonomy_terms = new WP_Term_Query( $args ); 97 | 98 | if ( ! empty( $taxonomy_terms->terms ) ) { 99 | foreach ( $taxonomy_terms->terms as $term ) { 100 | $sitemap_entry = array( 101 | 'loc' => get_term_link( $term ), 102 | ); 103 | 104 | /** 105 | * Filters the sitemap entry for an individual term. 106 | * 107 | * @since 5.5.0 108 | * 109 | * @param array $sitemap_entry Sitemap entry for the term. 110 | * @param WP_Term $term Term object. 111 | * @param string $taxonomy Taxonomy name. 112 | */ 113 | $sitemap_entry = apply_filters( 'wp_sitemaps_taxonomies_entry', $sitemap_entry, $term, $taxonomy ); 114 | $url_list[] = $sitemap_entry; 115 | } 116 | } 117 | 118 | return $url_list; 119 | } 120 | 121 | /** 122 | * Gets the max number of pages available for the object type. 123 | * 124 | * @since 5.5.0 125 | * 126 | * @param string $taxonomy Taxonomy name. 127 | * @return int Total number of pages. 128 | */ 129 | public function get_max_num_pages( $taxonomy = '' ) { 130 | if ( empty( $taxonomy ) ) { 131 | return 0; 132 | } 133 | 134 | /** 135 | * Filters the max number of pages before it is generated. 136 | * 137 | * Passing a non-null value will effectively short-circuit the generation, 138 | * returning that value instead. 139 | * 140 | * @since 5.5.0 141 | * 142 | * @param int $max_num_pages The maximum number of pages. Default null. 143 | * @param string $taxonomy Taxonomy name. 144 | */ 145 | $max_num_pages = apply_filters( 'wp_sitemaps_taxonomies_pre_max_num_pages', null, $taxonomy ); 146 | 147 | if ( null !== $max_num_pages ) { 148 | return $max_num_pages; 149 | } 150 | 151 | $term_count = wp_count_terms( $taxonomy, $this->get_taxonomies_query_args( $taxonomy ) ); 152 | 153 | return (int) ceil( $term_count / wp_sitemaps_get_max_urls( $this->object_type ) ); 154 | } 155 | 156 | /** 157 | * Returns the query args for retrieving taxonomy terms to list in the sitemap. 158 | * 159 | * @since 5.5.0 160 | * 161 | * @param string $taxonomy Taxonomy name. 162 | * @return array $args Array of WP_Term_Query arguments. 163 | */ 164 | protected function get_taxonomies_query_args( $taxonomy ) { 165 | /** 166 | * Filters the taxonomy terms query arguments. 167 | * 168 | * Allows modification of the taxonomy query arguments before querying. 169 | * 170 | * @see WP_Term_Query for a full list of arguments 171 | * 172 | * @since 5.5.0 173 | * 174 | * @param array $args Array of WP_Term_Query arguments. 175 | * @param string $taxonomy Taxonomy name. 176 | */ 177 | $args = apply_filters( 178 | 'wp_sitemaps_taxonomies_query_args', 179 | array( 180 | 'fields' => 'ids', 181 | 'taxonomy' => $taxonomy, 182 | 'orderby' => 'term_order', 183 | 'number' => wp_sitemaps_get_max_urls( $this->object_type ), 184 | 'hide_empty' => true, 185 | 'hierarchical' => false, 186 | 'update_term_meta_cache' => false, 187 | ), 188 | $taxonomy 189 | ); 190 | 191 | return $args; 192 | } 193 | } 194 | -------------------------------------------------------------------------------- /inc/providers/class-wp-sitemaps-users.php: -------------------------------------------------------------------------------- 1 | name = 'users'; 25 | $this->object_type = 'user'; 26 | } 27 | 28 | /** 29 | * Gets a URL list for a user sitemap. 30 | * 31 | * @since 5.5.0 32 | * 33 | * @param int $page_num Page of results. 34 | * @param string $object_subtype Optional. Not applicable for Users but 35 | * required for compatibility with the parent 36 | * provider class. Default empty. 37 | * @return array $url_list Array of URLs for a sitemap. 38 | */ 39 | public function get_url_list( $page_num, $object_subtype = '' ) { 40 | /** 41 | * Filters the users URL list before it is generated. 42 | * 43 | * Passing a non-null value will effectively short-circuit the generation, 44 | * returning that value instead. 45 | * 46 | * @since 5.5.0 47 | * 48 | * @param array $url_list The URL list. Default null. 49 | * @param int $page_num Page of results. 50 | */ 51 | $url_list = apply_filters( 52 | 'wp_sitemaps_users_pre_url_list', 53 | null, 54 | $page_num 55 | ); 56 | 57 | if ( null !== $url_list ) { 58 | return $url_list; 59 | } 60 | 61 | $args = $this->get_users_query_args(); 62 | $args['paged'] = $page_num; 63 | 64 | $query = new WP_User_Query( $args ); 65 | $users = $query->get_results(); 66 | $url_list = array(); 67 | 68 | foreach ( $users as $user ) { 69 | $sitemap_entry = array( 70 | 'loc' => get_author_posts_url( $user->ID ), 71 | ); 72 | 73 | /** 74 | * Filters the sitemap entry for an individual user. 75 | * 76 | * @since 5.5.0 77 | * 78 | * @param array $sitemap_entry Sitemap entry for the user. 79 | * @param WP_User $user User object. 80 | */ 81 | $sitemap_entry = apply_filters( 'wp_sitemaps_users_entry', $sitemap_entry, $user ); 82 | $url_list[] = $sitemap_entry; 83 | } 84 | 85 | return $url_list; 86 | } 87 | 88 | /** 89 | * Gets the max number of pages available for the object type. 90 | * 91 | * @since 5.5.0 92 | * 93 | * @see WP_Sitemaps_Provider::max_num_pages 94 | * 95 | * @param string $object_subtype Optional. Not applicable for Users but 96 | * required for compatibility with the parent 97 | * provider class. Default empty. 98 | * @return int Total page count. 99 | */ 100 | public function get_max_num_pages( $object_subtype = '' ) { 101 | /** 102 | * Filters the max number of pages before it is generated. 103 | * 104 | * Passing a non-null value will effectively short-circuit the generation, 105 | * returning that value instead. 106 | * 107 | * @since 5.5.0 108 | * 109 | * @param int $max_num_pages The maximum number of pages. Default null. 110 | */ 111 | $max_num_pages = apply_filters( 'wp_sitemaps_users_pre_max_num_pages', null ); 112 | 113 | if ( null !== $max_num_pages ) { 114 | return $max_num_pages; 115 | } 116 | 117 | $args = $this->get_users_query_args(); 118 | $query = new WP_User_Query( $args ); 119 | 120 | $total_users = $query->get_total(); 121 | 122 | return (int) ceil( $total_users / wp_sitemaps_get_max_urls( $this->object_type ) ); 123 | } 124 | 125 | /** 126 | * Returns the query args for retrieving users to list in the sitemap. 127 | * 128 | * @since 5.5.0 129 | * 130 | * @return array $args Array of WP_User_Query arguments. 131 | */ 132 | protected function get_users_query_args() { 133 | $public_post_types = get_post_types( 134 | array( 135 | 'public' => true, 136 | ) 137 | ); 138 | 139 | // We're not supporting sitemaps for author pages for attachments. 140 | unset( $public_post_types['attachment'] ); 141 | 142 | /** 143 | * Filters the query arguments for authors with public posts. 144 | * 145 | * Allows modification of the authors query arguments before querying. 146 | * 147 | * @see WP_User_Query for a full list of arguments 148 | * 149 | * @since 5.5.0 150 | * 151 | * @param array $args Array of WP_User_Query arguments. 152 | */ 153 | $args = apply_filters( 154 | 'wp_sitemaps_users_query_args', 155 | array( 156 | 'has_published_posts' => array_keys( $public_post_types ), 157 | 'number' => wp_sitemaps_get_max_urls( $this->object_type ), 158 | ) 159 | ); 160 | 161 | return $args; 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /phpstan.neon.dist: -------------------------------------------------------------------------------- 1 | includes: 2 | - vendor/szepeviktor/phpstan-wordpress/extension.neon 3 | parameters: 4 | level: max 5 | inferPrivatePropertyTypeFromConstructor: true 6 | # TODO Add array types & shapes to PHPDoc blocks. 7 | checkMissingIterableValueType: false 8 | paths: 9 | - inc/ 10 | bootstrapFiles: 11 | - core-sitemaps.php 12 | ignoreErrors: 13 | # Uses func_get_args() 14 | - '#^Function apply_filters(_ref_array)? invoked with [34567] parameters, 2 required\.$#' 15 | # PHP 5.6 16 | - '# has no return typehint specified\.$#' 17 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === Core Sitemaps === 2 | Contributors: joemcgill, pacifika, kburgoine, tweetythierry, swissspidy, pbiron 3 | Tags: seo, sitemaps 4 | Requires at least: 5.4 5 | Tested up to: 5.5 6 | Requires PHP: 5.6 7 | Stable tag: 0.4.3 8 | License: GPLv2 or later 9 | License URI: https://www.gnu.org/licenses/gpl-2.0.html 10 | 11 | A feature plugin to integrate basic XML Sitemaps in WordPress Core. 12 | 13 | == Description == 14 | 15 | **Note: This feature has been integrated into WordPress 5.5. If you run WordPress 5.5, you can freely disable this plugin.** 16 | 17 | As [originally proposed in June 2019](https://make.wordpress.org/core/2019/06/12/xml-sitemaps-feature-project-proposal/), this feature plugin seeks to integrate basic XML Sitemaps functionality in WordPress Core. 18 | 19 | A short explanation of how this plugin works can be found on [this make/core blog post](https://make.wordpress.org/core/2020/01/27/feature-plugin-xml-sitemaps/). 20 | 21 | Interested in contributing to this plugin? Feel free to [join us on GitHub](https://github.com/GoogleChromeLabs/wp-sitemaps) and the [#core-sitemaps](https://wordpress.slack.com/archives/CTKTGNJJW) Slack channel. 22 | 23 | === Available Hooks and Filters === 24 | 25 | **General:** 26 | 27 | * `wp_sitemaps_enabled` - Filters whether XML Sitemaps are enabled or not. 28 | * `wp_sitemaps_max_urls` - Filters the maximum number of URLs displayed on a sitemap. 29 | * `wp_sitemaps_register_providers` - Filters the list of registered sitemap providers. 30 | * `wp_sitemaps_init` - Fires when initializing sitemaps. 31 | * `wp_sitemaps_index_entry` - Filters the sitemap entry for the sitemap index. 32 | 33 | **Providers:** 34 | 35 | * `wp_sitemaps_post_types` - Filters the list of post types to include in the sitemaps. 36 | * `wp_sitemaps_posts_entry` - Filters the sitemap entry for an individual post. 37 | * `wp_sitemaps_posts_show_on_front_entry` - Filters the sitemap entry for the home page when the 'show_on_front' option equals 'posts'. 38 | * `wp_sitemaps_posts_query_args` - Filters the query arguments for post type sitemap queries. 39 | * `wp_sitemaps_posts_pre_url_list` - Filters the posts URL list before it is generated (short-circuit). 40 | * `wp_sitemaps_posts_pre_max_num_pages` - Filters the max number of pages before it is generated (short-circuit). 41 | * `wp_sitemaps_taxonomies` - Filters the list of taxonomies to include in the sitemaps. 42 | * `wp_sitemaps_taxonomies_entry` - Filters the sitemap entry for an individual term. 43 | * `wp_sitemaps_taxonomies_query_args` - Filters the query arguments for taxonomy terms sitemap queries. 44 | * `wp_sitemaps_taxonomies_pre_url_list` - Filters the taxonomies URL list before it is generated (short-circuit). 45 | * `wp_sitemaps_taxonomies_pre_max_num_pages` - Filters the max number of pages before it is generated (short-circuit). 46 | * `wp_sitemaps_users_entry` - Filters the sitemap entry for an individual user. 47 | * `wp_sitemaps_users_query_args` - Filters the query arguments for user sitemap queries. 48 | * `wp_sitemaps_users_pre_url_list` - Filters the users URL list before it is generated (short-circuit). 49 | * `wp_sitemaps_users_pre_max_num_pages` - Filters the max number of pages before it is generated (short-circuit). 50 | 51 | **Stylesheets:** 52 | 53 | * `wp_sitemaps_stylesheet_css` - Filters the CSS for the sitemap stylesheet. 54 | * `wp_sitemaps_stylesheet_url` - Filters the URL for the sitemap stylesheet. 55 | * `wp_sitemaps_stylesheet_content` - Filters the content of the sitemap stylesheet. 56 | * `wp_sitemaps_stylesheet_index_url` - Filters the URL for the sitemap index stylesheet. 57 | * `wp_sitemaps_stylesheet_index_content` - Filters the content of the sitemap index stylesheet. 58 | 59 | == Installation == 60 | 61 | = Installation from within WordPress = 62 | 63 | 1. Visit **Plugins > Add New**. 64 | 2. Search for **Core Sitemaps**. 65 | 3. Install and activate the Core Sitemaps plugin. 66 | 67 | = Manual installation = 68 | 69 | 1. Upload the entire `core-sitemaps` folder to the `/wp-content/plugins/` directory. 70 | 2. Visit **Plugins**. 71 | 3. Activate the Core Sitemaps plugin. 72 | 73 | == Frequently Asked Questions == 74 | 75 | = How can I fully disable sitemap generation? = 76 | 77 | If you update the WordPress settings to discourage search engines from indexing your site, sitemaps will be disabled. 78 | Alternatively, use the `wp_sitemaps_enabled` filter, or use `remove_action( 'init', 'wp_sitemaps_get_server' );` to disable initialization of any sitemap functionality. 79 | 80 | = How can I disable sitemaps for a certain object type? = 81 | 82 | You can use the `wp_sitemaps_register_providers` filter to disable sitemap generation for posts, users, or taxonomies. 83 | 84 | = How can I disable sitemaps for a certain post type or taxonomy? = 85 | 86 | You can use the `wp_sitemaps_post_types` filter to disable sitemap generation for posts of a certain post type. 87 | 88 | By default, only public posts will be represented in the sitemap. 89 | 90 | Similarly, the `wp_sitemaps_taxonomies` filter can be used to disable sitemap generation for certain taxonomies. 91 | 92 | **Example: Disabling sitemaps for the "page" post type** 93 | 94 | ` 95 | add_filter( 96 | 'wp_sitemaps_post_types', 97 | function( $post_types ) { 98 | unset( $post_types['page'] ); 99 | return $post_types; 100 | } 101 | ); 102 | ` 103 | 104 | **Example: Disabling sitemaps for the "post_tag" taxonomy** 105 | 106 | ` 107 | add_filter( 108 | 'wp_sitemaps_taxonomies', 109 | function( $taxonomies ) { 110 | unset( $taxonomies['post_tag'] ); 111 | return $taxonomies; 112 | } 113 | ); 114 | ` 115 | 116 | = How can I exclude certain posts / taxonomies / users from the sitemap or add custom ones? = 117 | 118 | The `wp_sitemaps_posts_query_args`, `wp_sitemaps_taxonomies_query_args`, and `wp_sitemaps_users_query_args` filters can be used to modify the underlying queries. Using these queries, certain items can be excluded. 119 | 120 | **Example: Ensuring the page with ID 42 is not included** 121 | 122 | ` 123 | add_filter( 124 | 'wp_sitemaps_posts_query_args', 125 | function( $args ) { 126 | $args['post__not_in'] = isset( $args['post__not_in'] ) ? $args['post__not_in'] : array(); 127 | $args['post__not_in'][] = 42; 128 | return $args; 129 | } 130 | ); 131 | ` 132 | 133 | **Example: Ensuring the category with ID 7 is not included** 134 | 135 | ` 136 | add_filter( 137 | 'wp_sitemaps_taxonomies_query_args', 138 | function( $args ) { 139 | $args['exclude'] = isset( $args['exclude'] ) ? $args['exclude'] : array(); 140 | $args['exclude'][] = 7; 141 | return $args; 142 | } 143 | ); 144 | ` 145 | 146 | **Example: Ensuring the user with ID 1 is not included** 147 | 148 | ` 149 | add_filter( 150 | 'wp_sitemaps_users_query_args', 151 | function( $args ) { 152 | $args['exclude'] = isset( $args['exclude'] ) ? $args['exclude'] : array(); 153 | $args['exclude'][] = 1; 154 | return $args; 155 | } 156 | ); 157 | ` 158 | 159 | = How can I add `changefreq`, `priority`, or `lastmod` to a sitemap? = 160 | 161 | You can use the `wp_sitemaps_posts_entry` / `wp_sitemaps_users_entry` / `wp_sitemaps_taxonomies_entry` filters to add additional attributes like `changefreq`, `priority`, or `lastmod` to single item in the sitemap. 162 | 163 | **Example: Adding the last modified date for posts** 164 | 165 | ` 166 | add_filter( 167 | 'wp_sitemaps_posts_entry', 168 | function( $entry, $post ) { 169 | $entry['lastmod'] = $post->post_modified_gmt; 170 | return $entry; 171 | }, 172 | 10, 173 | 2 174 | ); 175 | ` 176 | 177 | Similarly, you can use the `wp_sitemaps_index_entry` filter to add `lastmod` on the sitemap index. Note: `changefreq` and `priority` are not supported on the sitemap index. 178 | 179 | = How can I add image sitemaps? = 180 | 181 | Adding image sitemaps are not supported yet, but support will be added in the future so that plugin developers can add them if needed. 182 | 183 | = How can I change the number of URLs per sitemap? = 184 | 185 | Use the `wp_sitemaps_max_urls` filter to adjust the maximum number of URLs included in a sitemap. The default value is 2000 URLs. 186 | 187 | = How can I change the appearance of the XML sitemaps in the browser using XSL? = 188 | 189 | A variety of filters exist to allow you to adjust the styling: 190 | 191 | * `wp_sitemaps_stylesheet_url` - Filter the URL for the sitemap stylesheet. 192 | * `wp_sitemaps_stylesheet_index_url` - Filter the URL for the sitemap index stylesheet. 193 | * `wp_sitemaps_stylesheet_content` - Filter the content of the sitemap stylesheet. 194 | * `wp_sitemaps_index_stylesheet_content` - Filter the content of the sitemap index stylesheet. 195 | * `wp_sitemaps_stylesheet_css` - Filter the CSS only for the sitemap stylesheet. 196 | 197 | = Does this plugin support `changefreq` and `priority` attributes for sitemaps? = 198 | 199 | No. Those are optional fields in the sitemaps protocol and not typically consumed by search engines. Developers can still add those fields if they really want to. 200 | 201 | = Why is there no last modified date shown in the sitemap? = 202 | 203 | XML sitemaps are first and foremost a discovery mechanism for content. Exposing the date the content was last modified is not needed for the majority of sites. 204 | 205 | == Changelog == 206 | 207 | For the plugin's changelog, please check out the full list of changes [on GitHub](https://github.com/GoogleChromeLabs/wp-sitemaps/blob/master/CHANGELOG.md). 208 | --------------------------------------------------------------------------------