sitemap-builder.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. <?php
  2. /**
  3. * Build the sitemap tree.
  4. *
  5. * @package Jetpack
  6. * @since 4.8.0
  7. * @author Automattic
  8. */
  9. require_once dirname( __FILE__ ) . '/sitemap-constants.php';
  10. require_once dirname( __FILE__ ) . '/sitemap-buffer.php';
  11. if ( ! class_exists( 'DOMDocument' ) ) {
  12. require_once dirname( __FILE__ ) . '/sitemap-buffer-fallback.php';
  13. require_once dirname( __FILE__ ) . '/sitemap-buffer-image-fallback.php';
  14. require_once dirname( __FILE__ ) . '/sitemap-buffer-master-fallback.php';
  15. require_once dirname( __FILE__ ) . '/sitemap-buffer-news-fallback.php';
  16. require_once dirname( __FILE__ ) . '/sitemap-buffer-page-fallback.php';
  17. require_once dirname( __FILE__ ) . '/sitemap-buffer-video-fallback.php';
  18. } else {
  19. require_once dirname( __FILE__ ) . '/sitemap-buffer-image.php';
  20. require_once dirname( __FILE__ ) . '/sitemap-buffer-master.php';
  21. require_once dirname( __FILE__ ) . '/sitemap-buffer-news.php';
  22. require_once dirname( __FILE__ ) . '/sitemap-buffer-page.php';
  23. require_once dirname( __FILE__ ) . '/sitemap-buffer-video.php';
  24. }
  25. require_once dirname( __FILE__ ) . '/sitemap-librarian.php';
  26. require_once dirname( __FILE__ ) . '/sitemap-finder.php';
  27. require_once dirname( __FILE__ ) . '/sitemap-state.php';
  28. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
  29. require_once dirname( __FILE__ ) . '/sitemap-logger.php';
  30. }
  31. /**
  32. * The Jetpack_Sitemap_Builder object handles the construction of
  33. * all sitemap files (except the XSL files, which are handled by
  34. * Jetpack_Sitemap_Stylist.) Other than the constructor, there are
  35. * only two public functions: build_all_sitemaps and news_sitemap_xml.
  36. *
  37. * @since 4.8.0
  38. */
  39. class Jetpack_Sitemap_Builder {
  40. /**
  41. * Librarian object for storing and retrieving sitemap data.
  42. *
  43. * @access private
  44. * @since 4.8.0
  45. * @var $librarian Jetpack_Sitemap_Librarian
  46. */
  47. private $librarian;
  48. /**
  49. * Logger object for reporting debug messages.
  50. *
  51. * @access private
  52. * @since 4.8.0
  53. * @var $logger Jetpack_Sitemap_Logger
  54. */
  55. private $logger = false;
  56. /**
  57. * Finder object for dealing with sitemap URIs.
  58. *
  59. * @access private
  60. * @since 4.8.0
  61. * @var $finder Jetpack_Sitemap_Finder
  62. */
  63. private $finder;
  64. /**
  65. * Construct a new Jetpack_Sitemap_Builder object.
  66. *
  67. * @access public
  68. * @since 4.8.0
  69. */
  70. public function __construct() {
  71. $this->librarian = new Jetpack_Sitemap_Librarian();
  72. $this->finder = new Jetpack_Sitemap_Finder();
  73. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
  74. $this->logger = new Jetpack_Sitemap_Logger();
  75. }
  76. update_option(
  77. 'jetpack_sitemap_post_types',
  78. /**
  79. * The array of post types to be included in the sitemap.
  80. *
  81. * Add your custom post type name to the array to have posts of
  82. * that type included in the sitemap. The default array includes
  83. * 'page' and 'post'.
  84. *
  85. * The result of this filter is cached in an option, 'jetpack_sitemap_post_types',
  86. * so this filter only has to be applied once per generation.
  87. *
  88. * @since 4.8.0
  89. */
  90. apply_filters(
  91. 'jetpack_sitemap_post_types',
  92. array( 'post', 'page' )
  93. )
  94. );
  95. }
  96. /**
  97. * Update the sitemap.
  98. *
  99. * All we do here is call build_next_sitemap_file a bunch of times.
  100. *
  101. * @since 4.8.0
  102. */
  103. public function update_sitemap() {
  104. if ( $this->logger ) {
  105. $this->logger->report( '-- Updating...' );
  106. if ( ! class_exists( 'DOMDocument' ) ) {
  107. $this->logger->report(
  108. __(
  109. '-- WARNING: Jetpack can not load necessary XML manipulation libraries. '
  110. . 'This can happen if XML support in PHP is not enabled on your server. '
  111. . 'XML support is highly recommended for WordPress and Jetpack, please enable '
  112. . 'it or contact your hosting provider about it.',
  113. 'jetpack'
  114. ),
  115. true
  116. );
  117. }
  118. }
  119. for ( $i = 1; $i <= JP_SITEMAP_UPDATE_SIZE; $i++ ) {
  120. if ( true === $this->build_next_sitemap_file() ) {
  121. break; // All finished!
  122. }
  123. }
  124. if ( $this->logger ) {
  125. $this->logger->report( '-- ...done for now.' );
  126. $this->logger->time();
  127. }
  128. }
  129. /**
  130. * Generate the next sitemap file.
  131. *
  132. * Reads the most recent state of the sitemap generation phase,
  133. * constructs the next file, and updates the state.
  134. *
  135. * @since 4.8.0
  136. *
  137. * @return bool True when finished.
  138. */
  139. private function build_next_sitemap_file() {
  140. $finished = false; // Initialize finished flag.
  141. // Get the most recent state, and lock the state.
  142. $state = Jetpack_Sitemap_State::check_out();
  143. // Do nothing if the state was locked.
  144. if ( false === $state ) {
  145. return false;
  146. }
  147. // Otherwise, branch on the sitemap-type key of $state.
  148. switch ( $state['sitemap-type'] ) {
  149. case JP_PAGE_SITEMAP_TYPE:
  150. $this->build_next_sitemap_of_type(
  151. JP_PAGE_SITEMAP_TYPE,
  152. array( $this, 'build_one_page_sitemap' ),
  153. $state
  154. );
  155. break;
  156. case JP_PAGE_SITEMAP_INDEX_TYPE:
  157. $this->build_next_sitemap_index_of_type(
  158. JP_PAGE_SITEMAP_INDEX_TYPE,
  159. JP_IMAGE_SITEMAP_TYPE,
  160. $state
  161. );
  162. break;
  163. case JP_IMAGE_SITEMAP_TYPE:
  164. $this->build_next_sitemap_of_type(
  165. JP_IMAGE_SITEMAP_TYPE,
  166. array( $this, 'build_one_image_sitemap' ),
  167. $state
  168. );
  169. break;
  170. case JP_IMAGE_SITEMAP_INDEX_TYPE:
  171. $this->build_next_sitemap_index_of_type(
  172. JP_IMAGE_SITEMAP_INDEX_TYPE,
  173. JP_VIDEO_SITEMAP_TYPE,
  174. $state
  175. );
  176. break;
  177. case JP_VIDEO_SITEMAP_TYPE:
  178. $this->build_next_sitemap_of_type(
  179. JP_VIDEO_SITEMAP_TYPE,
  180. array( $this, 'build_one_video_sitemap' ),
  181. $state
  182. );
  183. break;
  184. case JP_VIDEO_SITEMAP_INDEX_TYPE:
  185. $this->build_next_sitemap_index_of_type(
  186. JP_VIDEO_SITEMAP_INDEX_TYPE,
  187. JP_MASTER_SITEMAP_TYPE,
  188. $state
  189. );
  190. break;
  191. case JP_MASTER_SITEMAP_TYPE:
  192. $this->build_master_sitemap( $state['max'] );
  193. // Reset the state and quit.
  194. Jetpack_Sitemap_State::reset(
  195. JP_PAGE_SITEMAP_TYPE
  196. );
  197. if ( $this->logger ) {
  198. $this->logger->report( '-- Finished.' );
  199. $this->logger->time();
  200. }
  201. $finished = true;
  202. break;
  203. default:
  204. Jetpack_Sitemap_State::reset(
  205. JP_PAGE_SITEMAP_TYPE
  206. );
  207. $finished = true;
  208. break;
  209. } // End switch().
  210. // Unlock the state.
  211. Jetpack_Sitemap_State::unlock();
  212. return $finished;
  213. }
  214. /**
  215. * Build the next sitemap of a given type and update the sitemap state.
  216. *
  217. * @since 4.8.0
  218. *
  219. * @param string $sitemap_type The type of the sitemap being generated.
  220. * @param callback $build_one A callback which builds a single sitemap file.
  221. * @param array $state A sitemap state.
  222. */
  223. private function build_next_sitemap_of_type( $sitemap_type, $build_one, $state ) {
  224. $index_type = jp_sitemap_index_type_of( $sitemap_type );
  225. // Try to build a sitemap.
  226. $result = call_user_func_array(
  227. $build_one,
  228. array(
  229. $state['number'] + 1,
  230. $state['last-added'],
  231. )
  232. );
  233. if ( false === $result ) {
  234. // If no sitemap was generated, advance to the next type.
  235. Jetpack_Sitemap_State::check_in( array(
  236. 'sitemap-type' => $index_type,
  237. 'last-added' => 0,
  238. 'number' => 0,
  239. 'last-modified' => '1970-01-01 00:00:00',
  240. ) );
  241. if ( $this->logger ) {
  242. $this->logger->report( "-- Cleaning Up $sitemap_type" );
  243. }
  244. // Clean up old files.
  245. $this->librarian->delete_numbered_sitemap_rows_after(
  246. $state['number'], $sitemap_type
  247. );
  248. return;
  249. }
  250. // Otherwise, update the state.
  251. Jetpack_Sitemap_State::check_in( array(
  252. 'sitemap-type' => $state['sitemap-type'],
  253. 'last-added' => $result['last_id'],
  254. 'number' => $state['number'] + 1,
  255. 'last-modified' => $result['last_modified'],
  256. ) );
  257. if ( true === $result['any_left'] ) {
  258. // If there's more work to be done with this type, return.
  259. return;
  260. }
  261. // Otherwise, advance state to the next sitemap type.
  262. Jetpack_Sitemap_State::check_in( array(
  263. 'sitemap-type' => $index_type,
  264. 'last-added' => 0,
  265. 'number' => 0,
  266. 'last-modified' => '1970-01-01 00:00:00',
  267. ) );
  268. if ( $this->logger ) {
  269. $this->logger->report( "-- Cleaning Up $sitemap_type" );
  270. }
  271. // Clean up old files.
  272. $this->librarian->delete_numbered_sitemap_rows_after(
  273. $state['number'] + 1, $sitemap_type
  274. );
  275. }
  276. /**
  277. * Build the next sitemap index of a given type and update the state.
  278. *
  279. * @since 4.8.0
  280. *
  281. * @param string $index_type The type of index being generated.
  282. * @param string $next_type The next type to generate after this one.
  283. * @param array $state A sitemap state.
  284. */
  285. private function build_next_sitemap_index_of_type( $index_type, $next_type, $state ) {
  286. $sitemap_type = jp_sitemap_child_type_of( $index_type );
  287. // If only 0 or 1 sitemaps were built, advance to the next type and return.
  288. if ( 1 >= $state['max'][ $sitemap_type ]['number'] ) {
  289. Jetpack_Sitemap_State::check_in( array(
  290. 'sitemap-type' => $next_type,
  291. 'last-added' => 0,
  292. 'number' => 0,
  293. 'last-modified' => '1970-01-01 00:00:00',
  294. ) );
  295. if ( $this->logger ) {
  296. $this->logger->report( "-- Cleaning Up $index_type" );
  297. }
  298. // There are no indices of this type.
  299. $this->librarian->delete_numbered_sitemap_rows_after(
  300. 0, $index_type
  301. );
  302. return;
  303. }
  304. // Otherwise, try to build a sitemap index.
  305. $result = $this->build_one_sitemap_index(
  306. $state['number'] + 1,
  307. $state['last-added'],
  308. $state['last-modified'],
  309. $index_type
  310. );
  311. // If no index was built, advance to the next type and return.
  312. if ( false === $result ) {
  313. Jetpack_Sitemap_State::check_in( array(
  314. 'sitemap-type' => $next_type,
  315. 'last-added' => 0,
  316. 'number' => 0,
  317. 'last-modified' => '1970-01-01 00:00:00',
  318. ) );
  319. if ( $this->logger ) {
  320. $this->logger->report( "-- Cleaning Up $index_type" );
  321. }
  322. // Clean up old files.
  323. $this->librarian->delete_numbered_sitemap_rows_after(
  324. $state['number'], $index_type
  325. );
  326. return;
  327. }
  328. // Otherwise, check in the state.
  329. Jetpack_Sitemap_State::check_in( array(
  330. 'sitemap-type' => $index_type,
  331. 'last-added' => $result['last_id'],
  332. 'number' => $state['number'] + 1,
  333. 'last-modified' => $result['last_modified'],
  334. ) );
  335. // If there are still sitemaps left to index, return.
  336. if ( true === $result['any_left'] ) {
  337. return;
  338. }
  339. // Otherwise, advance to the next type.
  340. Jetpack_Sitemap_State::check_in( array(
  341. 'sitemap-type' => $next_type,
  342. 'last-added' => 0,
  343. 'number' => 0,
  344. 'last-modified' => '1970-01-01 00:00:00',
  345. ) );
  346. if ( $this->logger ) {
  347. $this->logger->report( "-- Cleaning Up $index_type" );
  348. }
  349. // We're done generating indices of this type.
  350. $this->librarian->delete_numbered_sitemap_rows_after(
  351. $state['number'] + 1, $index_type
  352. );
  353. return;
  354. }
  355. /**
  356. * Builds the master sitemap index.
  357. *
  358. * @param array $max Array of sitemap types with max index and datetime.
  359. *
  360. * @since 4.8.0
  361. */
  362. private function build_master_sitemap( $max ) {
  363. if ( $this->logger ) {
  364. $this->logger->report( '-- Building Master Sitemap.' );
  365. }
  366. $buffer = new Jetpack_Sitemap_Buffer_Master(
  367. JP_SITEMAP_MAX_ITEMS,
  368. JP_SITEMAP_MAX_BYTES
  369. );
  370. if ( 0 < $max[ JP_PAGE_SITEMAP_TYPE ]['number'] ) {
  371. if ( 1 === $max[ JP_PAGE_SITEMAP_TYPE ]['number'] ) {
  372. $page['filename'] = jp_sitemap_filename( JP_PAGE_SITEMAP_TYPE, 1 );
  373. $page['last_modified'] = jp_sitemap_datetime( $max[ JP_PAGE_SITEMAP_TYPE ]['lastmod'] );
  374. } else {
  375. $page['filename'] = jp_sitemap_filename(
  376. JP_PAGE_SITEMAP_INDEX_TYPE,
  377. $max[ JP_PAGE_SITEMAP_INDEX_TYPE ]['number']
  378. );
  379. $page['last_modified'] = jp_sitemap_datetime( $max[ JP_PAGE_SITEMAP_INDEX_TYPE ]['lastmod'] );
  380. }
  381. $buffer->append(
  382. array(
  383. 'sitemap' => array(
  384. 'loc' => $this->finder->construct_sitemap_url( $page['filename'] ),
  385. 'lastmod' => $page['last_modified'],
  386. ),
  387. )
  388. );
  389. }
  390. if ( 0 < $max[ JP_IMAGE_SITEMAP_TYPE ]['number'] ) {
  391. if ( 1 === $max[ JP_IMAGE_SITEMAP_TYPE ]['number'] ) {
  392. $image['filename'] = jp_sitemap_filename( JP_IMAGE_SITEMAP_TYPE, 1 );
  393. $image['last_modified'] = jp_sitemap_datetime( $max[ JP_IMAGE_SITEMAP_TYPE ]['lastmod'] );
  394. } else {
  395. $image['filename'] = jp_sitemap_filename(
  396. JP_IMAGE_SITEMAP_INDEX_TYPE,
  397. $max[ JP_IMAGE_SITEMAP_INDEX_TYPE ]['number']
  398. );
  399. $image['last_modified'] = jp_sitemap_datetime( $max[ JP_IMAGE_SITEMAP_INDEX_TYPE ]['lastmod'] );
  400. }
  401. $buffer->append(
  402. array(
  403. 'sitemap' => array(
  404. 'loc' => $this->finder->construct_sitemap_url( $image['filename'] ),
  405. 'lastmod' => $image['last_modified'],
  406. ),
  407. )
  408. );
  409. }
  410. if ( 0 < $max[ JP_VIDEO_SITEMAP_TYPE ]['number'] ) {
  411. if ( 1 === $max[ JP_VIDEO_SITEMAP_TYPE ]['number'] ) {
  412. $video['filename'] = jp_sitemap_filename( JP_VIDEO_SITEMAP_TYPE, 1 );
  413. $video['last_modified'] = jp_sitemap_datetime( $max[ JP_VIDEO_SITEMAP_TYPE ]['lastmod'] );
  414. } else {
  415. $video['filename'] = jp_sitemap_filename(
  416. JP_VIDEO_SITEMAP_INDEX_TYPE,
  417. $max[ JP_VIDEO_SITEMAP_INDEX_TYPE ]['number']
  418. );
  419. $video['last_modified'] = jp_sitemap_datetime( $max[ JP_VIDEO_SITEMAP_INDEX_TYPE ]['lastmod'] );
  420. }
  421. $buffer->append(
  422. array(
  423. 'sitemap' => array(
  424. 'loc' => $this->finder->construct_sitemap_url( $video['filename'] ),
  425. 'lastmod' => $video['last_modified'],
  426. ),
  427. )
  428. );
  429. }
  430. $this->librarian->store_sitemap_data(
  431. 0,
  432. JP_MASTER_SITEMAP_TYPE,
  433. $buffer->contents(),
  434. ''
  435. );
  436. }
  437. /**
  438. * Build and store a single page sitemap. Returns false if no sitemap is built.
  439. *
  440. * Side effect: Create/update a sitemap row.
  441. *
  442. * @access private
  443. * @since 4.8.0
  444. *
  445. * @param int $number The number of the current sitemap.
  446. * @param int $from_id The greatest lower bound of the IDs of the posts to be included.
  447. *
  448. * @return bool|array @args {
  449. * @type int $last_id The ID of the last item to be successfully added to the buffer.
  450. * @type bool $any_left 'true' if there are items which haven't been saved to a sitemap, 'false' otherwise.
  451. * @type string $last_modified The most recent timestamp to appear on the sitemap.
  452. * }
  453. */
  454. public function build_one_page_sitemap( $number, $from_id ) {
  455. $last_post_id = $from_id;
  456. $any_posts_left = true;
  457. if ( $this->logger ) {
  458. $debug_name = jp_sitemap_filename( JP_PAGE_SITEMAP_TYPE, $number );
  459. $this->logger->report( "-- Building $debug_name" );
  460. }
  461. $buffer = new Jetpack_Sitemap_Buffer_Page(
  462. JP_SITEMAP_MAX_ITEMS,
  463. JP_SITEMAP_MAX_BYTES
  464. );
  465. // Add entry for the main page (only if we're at the first one).
  466. if ( 1 === $number ) {
  467. $item_array = array(
  468. 'url' => array(
  469. 'loc' => home_url(),
  470. ),
  471. );
  472. /**
  473. * Filter associative array with data to build <url> node
  474. * and its descendants for site home.
  475. *
  476. * @module sitemaps
  477. *
  478. * @since 3.9.0
  479. *
  480. * @param array $blog_home Data to build parent and children nodes for site home.
  481. */
  482. $item_array = apply_filters( 'jetpack_sitemap_url_home', $item_array );
  483. $buffer->append( $item_array );
  484. }
  485. // Add as many items to the buffer as possible.
  486. while ( $last_post_id >= 0 && false === $buffer->is_full() ) {
  487. $posts = $this->librarian->query_posts_after_id(
  488. $last_post_id, JP_SITEMAP_BATCH_SIZE
  489. );
  490. if ( null == $posts ) { // WPCS: loose comparison ok.
  491. $any_posts_left = false;
  492. break;
  493. }
  494. foreach ( $posts as $post ) {
  495. $current_item = $this->post_to_sitemap_item( $post );
  496. if ( true === $buffer->append( $current_item['xml'] ) ) {
  497. $last_post_id = $post->ID;
  498. $buffer->view_time( $current_item['last_modified'] );
  499. } else {
  500. break;
  501. }
  502. }
  503. }
  504. // Handle other page sitemap URLs.
  505. if ( false === $any_posts_left || $last_post_id < 0 ) {
  506. // Negative IDs are used to track URL indexes.
  507. $last_post_id = min( 0, $last_post_id );
  508. $any_posts_left = true; // Reinitialize.
  509. /**
  510. * Filter other page sitemap URLs.
  511. *
  512. * @module sitemaps
  513. *
  514. * @since 6.1.0
  515. *
  516. * @param array $urls An array of other URLs.
  517. */
  518. $other_urls = apply_filters( 'jetpack_page_sitemap_other_urls', array() );
  519. if ( $other_urls ) { // Start with index [1].
  520. $other_urls = array_values( $other_urls );
  521. array_unshift( $other_urls, $other_urls[0] );
  522. unset( $other_urls[0] );
  523. }
  524. // Add as many items to the buffer as possible.
  525. while ( false === $buffer->is_full() ) {
  526. $last_post_id_index = abs( $last_post_id );
  527. $start_from_post_id_index = $last_post_id_index ? $last_post_id_index + 1 : 0;
  528. $urls = array_slice(
  529. $other_urls,
  530. $start_from_post_id_index,
  531. JP_SITEMAP_BATCH_SIZE,
  532. true
  533. );
  534. if ( ! $urls ) {
  535. $any_posts_left = false;
  536. break;
  537. }
  538. foreach ( $urls as $index => $url ) {
  539. if ( ! is_array( $url ) ) {
  540. $url = array( 'loc' => $url );
  541. }
  542. $item = array( 'xml' => compact( 'url' ) );
  543. if ( true === $buffer->append( $item['xml'] ) ) {
  544. $last_post_id = -$index;
  545. } else {
  546. break;
  547. }
  548. }
  549. }
  550. }
  551. // If no items were added, return false.
  552. if ( true === $buffer->is_empty() ) {
  553. return false;
  554. }
  555. /**
  556. * Filter sitemap before rendering it as XML.
  557. *
  558. * @module sitemaps
  559. *
  560. * @since 3.9.0
  561. * @since 5.3.0 returns an element of DOMDocument type instead of SimpleXMLElement
  562. *
  563. * @param DOMDocument $doc Data tree for sitemap.
  564. * @param string $last_modified Date of last modification.
  565. */
  566. $tree = apply_filters(
  567. 'jetpack_print_sitemap',
  568. $buffer->get_document(),
  569. $buffer->last_modified()
  570. );
  571. // Store the buffer as the content of a sitemap row.
  572. $this->librarian->store_sitemap_data(
  573. $number,
  574. JP_PAGE_SITEMAP_TYPE,
  575. $buffer->contents(),
  576. $buffer->last_modified()
  577. );
  578. /*
  579. * Now report back with the ID of the last post ID to be
  580. * successfully added and whether there are any posts left.
  581. */
  582. return array(
  583. 'last_id' => $last_post_id,
  584. 'any_left' => $any_posts_left,
  585. 'last_modified' => $buffer->last_modified(),
  586. );
  587. }
  588. /**
  589. * Build and store a single image sitemap. Returns false if no sitemap is built.
  590. *
  591. * Side effect: Create/update an image sitemap row.
  592. *
  593. * @access private
  594. * @since 4.8.0
  595. *
  596. * @param int $number The number of the current sitemap.
  597. * @param int $from_id The greatest lower bound of the IDs of the posts to be included.
  598. *
  599. * @return bool|array @args {
  600. * @type int $last_id The ID of the last item to be successfully added to the buffer.
  601. * @type bool $any_left 'true' if there are items which haven't been saved to a sitemap, 'false' otherwise.
  602. * @type string $last_modified The most recent timestamp to appear on the sitemap.
  603. * }
  604. */
  605. public function build_one_image_sitemap( $number, $from_id ) {
  606. $last_post_id = $from_id;
  607. $any_posts_left = true;
  608. if ( $this->logger ) {
  609. $debug_name = jp_sitemap_filename( JP_IMAGE_SITEMAP_TYPE, $number );
  610. $this->logger->report( "-- Building $debug_name" );
  611. }
  612. $buffer = new Jetpack_Sitemap_Buffer_Image(
  613. JP_SITEMAP_MAX_ITEMS,
  614. JP_SITEMAP_MAX_BYTES
  615. );
  616. // Add as many items to the buffer as possible.
  617. while ( false === $buffer->is_full() ) {
  618. $posts = $this->librarian->query_images_after_id(
  619. $last_post_id, JP_SITEMAP_BATCH_SIZE
  620. );
  621. if ( null == $posts ) { // WPCS: loose comparison ok.
  622. $any_posts_left = false;
  623. break;
  624. }
  625. foreach ( $posts as $post ) {
  626. $current_item = $this->image_post_to_sitemap_item( $post );
  627. if ( true === $buffer->append( $current_item['xml'] ) ) {
  628. $last_post_id = $post->ID;
  629. $buffer->view_time( $current_item['last_modified'] );
  630. } else {
  631. break;
  632. }
  633. }
  634. }
  635. // If no items were added, return false.
  636. if ( true === $buffer->is_empty() ) {
  637. return false;
  638. }
  639. // Store the buffer as the content of a jp_sitemap post.
  640. $this->librarian->store_sitemap_data(
  641. $number,
  642. JP_IMAGE_SITEMAP_TYPE,
  643. $buffer->contents(),
  644. $buffer->last_modified()
  645. );
  646. /*
  647. * Now report back with the ID of the last post to be
  648. * successfully added and whether there are any posts left.
  649. */
  650. return array(
  651. 'last_id' => $last_post_id,
  652. 'any_left' => $any_posts_left,
  653. 'last_modified' => $buffer->last_modified(),
  654. );
  655. }
  656. /**
  657. * Build and store a single video sitemap. Returns false if no sitemap is built.
  658. *
  659. * Side effect: Create/update an video sitemap row.
  660. *
  661. * @access private
  662. * @since 4.8.0
  663. *
  664. * @param int $number The number of the current sitemap.
  665. * @param int $from_id The greatest lower bound of the IDs of the posts to be included.
  666. *
  667. * @return bool|array @args {
  668. * @type int $last_id The ID of the last item to be successfully added to the buffer.
  669. * @type bool $any_left 'true' if there are items which haven't been saved to a sitemap, 'false' otherwise.
  670. * @type string $last_modified The most recent timestamp to appear on the sitemap.
  671. * }
  672. */
  673. public function build_one_video_sitemap( $number, $from_id ) {
  674. $last_post_id = $from_id;
  675. $any_posts_left = true;
  676. if ( $this->logger ) {
  677. $debug_name = jp_sitemap_filename( JP_VIDEO_SITEMAP_TYPE, $number );
  678. $this->logger->report( "-- Building $debug_name" );
  679. }
  680. $buffer = new Jetpack_Sitemap_Buffer_Video(
  681. JP_SITEMAP_MAX_ITEMS,
  682. JP_SITEMAP_MAX_BYTES
  683. );
  684. // Add as many items to the buffer as possible.
  685. while ( false === $buffer->is_full() ) {
  686. $posts = $this->librarian->query_videos_after_id(
  687. $last_post_id, JP_SITEMAP_BATCH_SIZE
  688. );
  689. if ( null == $posts ) { // WPCS: loose comparison ok.
  690. $any_posts_left = false;
  691. break;
  692. }
  693. foreach ( $posts as $post ) {
  694. $current_item = $this->video_post_to_sitemap_item( $post );
  695. if ( true === $buffer->append( $current_item['xml'] ) ) {
  696. $last_post_id = $post->ID;
  697. $buffer->view_time( $current_item['last_modified'] );
  698. } else {
  699. break;
  700. }
  701. }
  702. }
  703. // If no items were added, return false.
  704. if ( true === $buffer->is_empty() ) {
  705. return false;
  706. }
  707. if ( false === $buffer->is_empty() ) {
  708. $this->librarian->store_sitemap_data(
  709. $number,
  710. JP_VIDEO_SITEMAP_TYPE,
  711. $buffer->contents(),
  712. $buffer->last_modified()
  713. );
  714. }
  715. /*
  716. * Now report back with the ID of the last post to be
  717. * successfully added and whether there are any posts left.
  718. */
  719. return array(
  720. 'last_id' => $last_post_id,
  721. 'any_left' => $any_posts_left,
  722. 'last_modified' => $buffer->last_modified(),
  723. );
  724. }
  725. /**
  726. * Build and store a single page sitemap index. Return false if no index is built.
  727. *
  728. * Side effect: Create/update a sitemap index row.
  729. *
  730. * @access private
  731. * @since 4.8.0
  732. *
  733. * @param int $number The number of the current sitemap index.
  734. * @param int $from_id The greatest lower bound of the IDs of the sitemaps to be included.
  735. * @param string $datetime Datetime of previous sitemap in 'YYYY-MM-DD hh:mm:ss' format.
  736. * @param string $index_type Sitemap index type.
  737. *
  738. * @return bool|array @args {
  739. * @type int $last_id The ID of the last item to be successfully added to the buffer.
  740. * @type bool $any_left 'true' if there are items which haven't been saved to a sitemap, 'false' otherwise.
  741. * @type string $last_modified The most recent timestamp to appear on the sitemap.
  742. * }
  743. */
  744. private function build_one_sitemap_index( $number, $from_id, $datetime, $index_type ) {
  745. $last_sitemap_id = $from_id;
  746. $any_sitemaps_left = true;
  747. // Check the datetime format.
  748. $datetime = jp_sitemap_datetime( $datetime );
  749. $sitemap_type = jp_sitemap_child_type_of( $index_type );
  750. if ( $this->logger ) {
  751. $index_debug_name = jp_sitemap_filename( $index_type, $number );
  752. $this->logger->report( "-- Building $index_debug_name" );
  753. }
  754. $buffer = new Jetpack_Sitemap_Buffer_Master(
  755. JP_SITEMAP_MAX_ITEMS,
  756. JP_SITEMAP_MAX_BYTES,
  757. $datetime
  758. );
  759. // Add pointer to the previous sitemap index (unless we're at the first one).
  760. if ( 1 !== $number ) {
  761. $i = $number - 1;
  762. $prev_index_url = $this->finder->construct_sitemap_url(
  763. jp_sitemap_filename( $index_type, $i )
  764. );
  765. $item_array = array(
  766. 'sitemap' => array(
  767. 'loc' => $prev_index_url,
  768. 'lastmod' => $datetime,
  769. ),
  770. );
  771. $buffer->append( $item_array );
  772. }
  773. // Add as many items to the buffer as possible.
  774. while ( false === $buffer->is_full() ) {
  775. // Retrieve a batch of posts (in order).
  776. $posts = $this->librarian->query_sitemaps_after_id(
  777. $sitemap_type, $last_sitemap_id, JP_SITEMAP_BATCH_SIZE
  778. );
  779. // If there were no posts to get, make a note.
  780. if ( null == $posts ) { // WPCS: loose comparison ok.
  781. $any_sitemaps_left = false;
  782. break;
  783. }
  784. // Otherwise, loop through each post in the batch.
  785. foreach ( $posts as $post ) {
  786. // Generate the sitemap XML for the post.
  787. $current_item = $this->sitemap_row_to_index_item( (array) $post );
  788. // Try adding this item to the buffer.
  789. if ( true === $buffer->append( $current_item['xml'] ) ) {
  790. $last_sitemap_id = $post['ID'];
  791. $buffer->view_time( $current_item['last_modified'] );
  792. } else {
  793. // Otherwise stop looping through posts.
  794. break;
  795. }
  796. }
  797. }
  798. // If no items were added, return false.
  799. if ( true === $buffer->is_empty() ) {
  800. return false;
  801. }
  802. $this->librarian->store_sitemap_data(
  803. $number,
  804. $index_type,
  805. $buffer->contents(),
  806. $buffer->last_modified()
  807. );
  808. /*
  809. * Now report back with the ID of the last sitemap post ID to
  810. * be successfully added, whether there are any sitemap posts
  811. * left, and the most recent modification time seen.
  812. */
  813. return array(
  814. 'last_id' => $last_sitemap_id,
  815. 'any_left' => $any_sitemaps_left,
  816. 'last_modified' => $buffer->last_modified(),
  817. );
  818. }
  819. /**
  820. * Construct the sitemap index url entry for a sitemap row.
  821. *
  822. * @link http://www.sitemaps.org/protocol.html#sitemapIndex_sitemap
  823. *
  824. * @access private
  825. * @since 4.8.0
  826. *
  827. * @param array $row The sitemap data to be processed.
  828. *
  829. * @return string An XML fragment representing the post URL.
  830. */
  831. private function sitemap_row_to_index_item( $row ) {
  832. $url = $this->finder->construct_sitemap_url( $row['post_title'] );
  833. $item_array = array(
  834. 'sitemap' => array(
  835. 'loc' => $url,
  836. 'lastmod' => jp_sitemap_datetime( $row['post_date'] ),
  837. ),
  838. );
  839. return array(
  840. 'xml' => $item_array,
  841. 'last_modified' => $row['post_date'],
  842. );
  843. }
  844. /**
  845. * Build and return the news sitemap xml. Note that the result of this
  846. * function is cached in the transient 'jetpack_news_sitemap_xml'.
  847. *
  848. * @access public
  849. * @since 4.8.0
  850. *
  851. * @return string The news sitemap xml.
  852. */
  853. public function news_sitemap_xml() {
  854. $the_stored_news_sitemap = get_transient( 'jetpack_news_sitemap_xml' );
  855. if ( false === $the_stored_news_sitemap ) {
  856. if ( $this->logger ) {
  857. $this->logger->report( 'Beginning news sitemap generation.' );
  858. }
  859. /**
  860. * Filter limit of entries to include in news sitemap.
  861. *
  862. * @module sitemaps
  863. *
  864. * @since 3.9.0
  865. *
  866. * @param int $count Number of entries to include in news sitemap.
  867. */
  868. $item_limit = apply_filters(
  869. 'jetpack_sitemap_news_sitemap_count',
  870. JP_NEWS_SITEMAP_MAX_ITEMS
  871. );
  872. $buffer = new Jetpack_Sitemap_Buffer_News(
  873. min( $item_limit, JP_NEWS_SITEMAP_MAX_ITEMS ),
  874. JP_SITEMAP_MAX_BYTES
  875. );
  876. $posts = $this->librarian->query_most_recent_posts( JP_NEWS_SITEMAP_MAX_ITEMS );
  877. foreach ( $posts as $post ) {
  878. $current_item = $this->post_to_news_sitemap_item( $post );
  879. if ( false === $buffer->append( $current_item['xml'] ) ) {
  880. break;
  881. }
  882. }
  883. if ( $this->logger ) {
  884. $this->logger->time( 'End news sitemap generation.' );
  885. }
  886. $the_stored_news_sitemap = $buffer->contents();
  887. set_transient(
  888. 'jetpack_news_sitemap_xml',
  889. $the_stored_news_sitemap,
  890. JP_NEWS_SITEMAP_INTERVAL
  891. );
  892. } // End if().
  893. return $the_stored_news_sitemap;
  894. }
  895. /**
  896. * Construct the sitemap url entry for a WP_Post.
  897. *
  898. * @link http://www.sitemaps.org/protocol.html#urldef
  899. * @access private
  900. * @since 4.8.0
  901. *
  902. * @param WP_Post $post The post to be processed.
  903. *
  904. * @return array An array representing the post URL.
  905. */
  906. private function post_to_sitemap_item( $post ) {
  907. /**
  908. * Filter condition to allow skipping specific posts in sitemap.
  909. *
  910. * @module sitemaps
  911. *
  912. * @since 3.9.0
  913. *
  914. * @param bool $skip Current boolean. False by default, so no post is skipped.
  915. * @param WP_POST $post Current post object.
  916. */
  917. if ( true === apply_filters( 'jetpack_sitemap_skip_post', false, $post ) ) {
  918. return array(
  919. 'xml' => null,
  920. 'last_modified' => null,
  921. );
  922. }
  923. $url = esc_url( get_permalink( $post ) );
  924. /*
  925. * Spec requires the URL to be <=2048 bytes.
  926. * In practice this constraint is unlikely to be violated.
  927. */
  928. if ( 2048 < strlen( $url ) ) {
  929. $url = home_url() . '/?p=' . $post->ID;
  930. }
  931. $last_modified = $post->post_modified_gmt;
  932. // Check for more recent comments.
  933. // Note that 'Y-m-d h:i:s' strings sort lexicographically.
  934. if ( 0 < $post->comment_count ) {
  935. $last_modified = max(
  936. $last_modified,
  937. $this->librarian->query_latest_approved_comment_time_on_post( $post->ID )
  938. );
  939. }
  940. $item_array = array(
  941. 'url' => array(
  942. 'loc' => $url,
  943. 'lastmod' => jp_sitemap_datetime( $last_modified ),
  944. ),
  945. );
  946. /**
  947. * Filter sitemap URL item before rendering it as XML.
  948. *
  949. * @module sitemaps
  950. *
  951. * @since 3.9.0
  952. *
  953. * @param array $tree Associative array representing sitemap URL element.
  954. * @param int $post_id ID of the post being processed.
  955. */
  956. $item_array = apply_filters( 'jetpack_sitemap_url', $item_array, $post->ID );
  957. return array(
  958. 'xml' => $item_array,
  959. 'last_modified' => $last_modified,
  960. );
  961. }
  962. /**
  963. * Construct the image sitemap url entry for a WP_Post of image type.
  964. *
  965. * @link http://www.sitemaps.org/protocol.html#urldef
  966. *
  967. * @access private
  968. * @since 4.8.0
  969. *
  970. * @param WP_Post $post The image post to be processed.
  971. *
  972. * @return string An XML fragment representing the post URL.
  973. */
  974. private function image_post_to_sitemap_item( $post ) {
  975. /**
  976. * Filter condition to allow skipping specific image posts in the sitemap.
  977. *
  978. * @module sitemaps
  979. *
  980. * @since 4.8.0
  981. *
  982. * @param bool $skip Current boolean. False by default, so no post is skipped.
  983. * @param WP_POST $post Current post object.
  984. */
  985. if ( apply_filters( 'jetpack_sitemap_image_skip_post', false, $post ) ) {
  986. return array(
  987. 'xml' => null,
  988. 'last_modified' => null,
  989. );
  990. }
  991. $url = wp_get_attachment_url( $post->ID );
  992. $parent_url = get_permalink( get_post( $post->post_parent ) );
  993. if ( '' == $parent_url ) { // WPCS: loose comparison ok.
  994. $parent_url = get_permalink( $post );
  995. }
  996. $item_array = array(
  997. 'url' => array(
  998. 'loc' => $parent_url,
  999. 'lastmod' => jp_sitemap_datetime( $post->post_modified_gmt ),
  1000. 'image:image' => array(
  1001. 'image:loc' => $url,
  1002. ),
  1003. ),
  1004. );
  1005. $item_array['url']['image:image']['image:title'] = $post->post_title;
  1006. $item_array['url']['image:image']['image:caption'] = $post->post_excerpt;
  1007. /**
  1008. * Filter associative array with data to build <url> node
  1009. * and its descendants for current post in image sitemap.
  1010. *
  1011. * @module sitemaps
  1012. *
  1013. * @since 4.8.0
  1014. *
  1015. * @param array $item_array Data to build parent and children nodes for current post.
  1016. * @param int $post_id Current image post ID.
  1017. */
  1018. $item_array = apply_filters(
  1019. 'jetpack_sitemap_image_sitemap_item',
  1020. $item_array,
  1021. $post->ID
  1022. );
  1023. return array(
  1024. 'xml' => $item_array,
  1025. 'last_modified' => $post->post_modified_gmt,
  1026. );
  1027. }
  1028. /**
  1029. * Construct the video sitemap url entry for a WP_Post of video type.
  1030. *
  1031. * @link http://www.sitemaps.org/protocol.html#urldef
  1032. * @link https://developers.google.com/webmasters/videosearch/sitemaps
  1033. *
  1034. * @access private
  1035. * @since 4.8.0
  1036. *
  1037. * @param WP_Post $post The video post to be processed.
  1038. *
  1039. * @return string An XML fragment representing the post URL.
  1040. */
  1041. private function video_post_to_sitemap_item( $post ) {
  1042. /**
  1043. * Filter condition to allow skipping specific image posts in the sitemap.
  1044. *
  1045. * @module sitemaps
  1046. *
  1047. * @since 4.8.0
  1048. *
  1049. * @param bool $skip Current boolean. False by default, so no post is skipped.
  1050. * @param WP_POST $post Current post object.
  1051. */
  1052. if ( apply_filters( 'jetpack_sitemap_video_skip_post', false, $post ) ) {
  1053. return array(
  1054. 'xml' => null,
  1055. 'last_modified' => null,
  1056. );
  1057. }
  1058. $parent_url = esc_url( get_permalink( get_post( $post->post_parent ) ) );
  1059. if ( '' == $parent_url ) { // WPCS: loose comparison ok.
  1060. $parent_url = esc_url( get_permalink( $post ) );
  1061. }
  1062. // Prepare the content like get_the_content_feed().
  1063. $content = $post->post_content;
  1064. /** This filter is already documented in core/wp-includes/post-template.php */
  1065. $content = apply_filters( 'the_content', $content );
  1066. /** This filter is already documented in core/wp-includes/feed.php */
  1067. $content = apply_filters( 'the_content_feed', $content, 'rss2' );
  1068. $item_array = array(
  1069. 'url' => array(
  1070. 'loc' => $parent_url,
  1071. 'lastmod' => jp_sitemap_datetime( $post->post_modified_gmt ),
  1072. 'video:video' => array(
  1073. /** This filter is already documented in core/wp-includes/feed.php */
  1074. 'video:title' => apply_filters( 'the_title_rss', $post->post_title ),
  1075. 'video:thumbnail_loc' => '',
  1076. 'video:description' => $content,
  1077. 'video:content_loc' => esc_url( wp_get_attachment_url( $post->ID ) ),
  1078. ),
  1079. ),
  1080. );
  1081. // TODO: Integrate with VideoPress here.
  1082. // cf. video:player_loc tag in video sitemap spec.
  1083. /**
  1084. * Filter associative array with data to build <url> node
  1085. * and its descendants for current post in video sitemap.
  1086. *
  1087. * @module sitemaps
  1088. *
  1089. * @since 4.8.0
  1090. *
  1091. * @param array $item_array Data to build parent and children nodes for current post.
  1092. * @param int $post_id Current video post ID.
  1093. */
  1094. $item_array = apply_filters(
  1095. 'jetpack_sitemap_video_sitemap_item',
  1096. $item_array,
  1097. $post->ID
  1098. );
  1099. return array(
  1100. 'xml' => $item_array,
  1101. 'last_modified' => $post->post_modified_gmt,
  1102. );
  1103. }
  1104. /**
  1105. * Construct the news sitemap url entry for a WP_Post.
  1106. *
  1107. * @link http://www.sitemaps.org/protocol.html#urldef
  1108. *
  1109. * @access private
  1110. * @since 4.8.0
  1111. *
  1112. * @param WP_Post $post The post to be processed.
  1113. *
  1114. * @return string An XML fragment representing the post URL.
  1115. */
  1116. private function post_to_news_sitemap_item( $post ) {
  1117. /**
  1118. * Filter condition to allow skipping specific posts in news sitemap.
  1119. *
  1120. * @module sitemaps
  1121. *
  1122. * @since 3.9.0
  1123. *
  1124. * @param bool $skip Current boolean. False by default, so no post is skipped.
  1125. * @param WP_POST $post Current post object.
  1126. */
  1127. if ( apply_filters( 'jetpack_sitemap_news_skip_post', false, $post ) ) {
  1128. return array(
  1129. 'xml' => null,
  1130. );
  1131. }
  1132. $url = get_permalink( $post );
  1133. /*
  1134. * Spec requires the URL to be <=2048 bytes.
  1135. * In practice this constraint is unlikely to be violated.
  1136. */
  1137. if ( 2048 < strlen( $url ) ) {
  1138. $url = home_url() . '/?p=' . $post->ID;
  1139. }
  1140. /*
  1141. * Trim the locale to an ISO 639 language code as required by Google.
  1142. * Special cases are zh-cn (Simplified Chinese) and zh-tw (Traditional Chinese).
  1143. * @link http://www.loc.gov/standards/iso639-2/php/code_list.php
  1144. */
  1145. $language = strtolower( get_locale() );
  1146. if ( in_array( $language, array( 'zh_tw', 'zh_cn' ), true ) ) {
  1147. $language = str_replace( '_', '-', $language );
  1148. } else {
  1149. $language = preg_replace( '/(_.*)$/i', '', $language );
  1150. }
  1151. $item_array = array(
  1152. 'url' => array(
  1153. 'loc' => $url,
  1154. 'lastmod' => jp_sitemap_datetime( $post->post_modified_gmt ),
  1155. 'news:news' => array(
  1156. 'news:publication' => array(
  1157. 'news:name' => get_bloginfo( 'name' ),
  1158. 'news:language' => $language,
  1159. ),
  1160. /** This filter is already documented in core/wp-includes/feed.php */
  1161. 'news:title' => apply_filters( 'the_title_rss', $post->post_title ),
  1162. 'news:publication_date' => jp_sitemap_datetime( $post->post_date_gmt ),
  1163. 'news:genres' => 'Blog',
  1164. ),
  1165. ),
  1166. );
  1167. /**
  1168. * Filter associative array with data to build <url> node
  1169. * and its descendants for current post in news sitemap.
  1170. *
  1171. * @module sitemaps
  1172. *
  1173. * @since 3.9.0
  1174. *
  1175. * @param array $item_array Data to build parent and children nodes for current post.
  1176. * @param int $post_id Current post ID.
  1177. */
  1178. $item_array = apply_filters(
  1179. 'jetpack_sitemap_news_sitemap_item',
  1180. $item_array,
  1181. $post->ID
  1182. );
  1183. return array(
  1184. 'xml' => $item_array,
  1185. );
  1186. }
  1187. }