Advertisement
Guest User

Untitled

a guest
Apr 26th, 2020
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. <?php
  2. use RankMath\Sitemap\Providers\Provider;
  3.  
  4. class Madara_Sitemap_Provider implements Provider {
  5. /**
  6. * Check if provider supports given item type.
  7. *
  8. * @param string $type Type string to check for.
  9. *
  10. * @return boolean
  11. */
  12. public function handles_type( $type ) {
  13. return true;
  14. }
  15.  
  16. /**
  17. * Get set of sitemaps index link data.
  18. *
  19. * @param int $max_entries Entries per sitemap.
  20. *
  21. * @return array
  22. */
  23. public function get_index_links( $max_entries ){
  24. $index = array();
  25.  
  26. global $wpdb;
  27.  
  28. $sql = "SELECT count(*) as total FROM {$wpdb->prefix}manga_chapters";
  29.  
  30. $count = $wpdb->get_var($sql);
  31. $total_page = floor($count / $max_entries) + 1;
  32.  
  33. for($current_page = 1; $current_page <= $total_page; $current_page++){
  34. if($max_entries * ($current_page - 1) < $count) {
  35. $offset = $max_entries * ($current_page - 1);
  36. $sql = "SELECT max(date_gmt) FROM {$wpdb->prefix}manga_chapters ORDER BY date_gmt DESC LIMIT {$max_entries},{$offset}";
  37. $date = $wpdb->get_var($sql);
  38.  
  39. $index[] = array(
  40. 'loc' => \RankMath\Sitemap\Router::get_base_url( 'wp-manga-chapters-sitemap'. $current_page .'.xml' ),
  41. 'lastmod' => $date,
  42. );
  43. }
  44. }
  45.  
  46. return $index;
  47. }
  48.  
  49. /**
  50. * Get set of sitemap link data.
  51. *
  52. * @param string $type Sitemap type.
  53. * @param int $max_entries Entries per sitemap.
  54. * @param int $current_page Current page of the sitemap.
  55. *
  56. * @return array
  57. */
  58. public function get_sitemap_links( $type, $max_entries, $current_page ){
  59. $urls = array();
  60.  
  61. global $wpdb, $wp_manga_functions;
  62.  
  63. $offset = ($current_page - 1) * $max_entries;
  64.  
  65. $sql = "SELECT * FROM {$wpdb->prefix}manga_chapters ORDER BY date_gmt DESC LIMIT {$offset}, {$max_entries}";
  66.  
  67. $results = $wpdb->get_results($sql);
  68.  
  69. global $_wp_manga_wpseo_sitemap;
  70. $_wp_manga_wpseo_sitemap = true;
  71. foreach($results as $result){
  72. $manga_id = $result->post_id;
  73.  
  74. // check if $manga is active
  75. if(get_post_status($manga_id) == 'publish'){
  76. $chapter_slug = $result->chapter_slug;
  77.  
  78. $link = $wp_manga_functions->build_chapter_url( $manga_id, $chapter_slug );
  79.  
  80. $urls[] = array(
  81. 'loc' => esc_url($link),
  82. 'mod' => $result->date_gmt,
  83. 'chf' => 'daily', // Deprecated, kept for backwards data compat. R.
  84. 'pri' => 1, // Deprecated, kept for backwards data compat. R.
  85. 'images' => array());
  86. }
  87. }
  88. $_wp_manga_wpseo_sitemap = false;
  89.  
  90. return $urls;
  91. }
  92. }
  93.  
  94. add_filter( 'rank_math/sitemap/providers', function( $providers ) {
  95. $providers[] = new Madara_Sitemap_Provider();
  96. return $providers;
  97. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement