Advertisement
NicholasB

class-video-provider.php

Apr 27th, 2023 (edited)
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.63 KB | None | 0 0
  1. <?php
  2. /**
  3.  * The Video Sitemap Provider
  4.  *
  5.  * @since      1.0.0
  6.  * @package    RankMath
  7.  * @subpackage RankMathPro
  8.  * @author     MyThemeShop <admin@mythemeshop.com>
  9.  */
  10.  
  11. namespace RankMathPro\Sitemap;
  12.  
  13. use RankMath\Helper;
  14. use RankMath\Sitemap\Router;
  15. use RankMath\Sitemap\Sitemap;
  16. use RankMath\Sitemap\Providers\Post_Type;
  17. use RankMath\Sitemap\Timezone;
  18.  
  19. defined( 'ABSPATH' ) || exit;
  20.  
  21. /**
  22.  * Video_Provider class.
  23.  */
  24. class Video_Provider extends Post_Type {
  25.  
  26.     /**
  27.      * Timezone.
  28.      *
  29.      * @var Timezone
  30.      */
  31.     public $timezone;
  32.  
  33.     /**
  34.      * Set up object properties.
  35.      */
  36.     public function __construct() {
  37.         $this->timezone = new Timezone();
  38.     }
  39.  
  40.     /**
  41.      * Check if provider supports given item type.
  42.      *
  43.      * @param  string $type Type string to check for.
  44.      * @return boolean
  45.      */
  46.     public function handles_type( $type ) {
  47.         return 'video' === $type;
  48.     }
  49.  
  50.     /**
  51.      * Get set of sitemaps index link data.
  52.      *
  53.      * @param  int $max_entries Entries per sitemap.
  54.      * @return array
  55.      */
  56.     public function get_index_links( $max_entries ) {
  57.         $post_types = (array) Helper::get_settings( 'sitemap.video_sitemap_post_type', [] );
  58.         if ( empty( $post_types ) ) {
  59.             return [];
  60.         }
  61.  
  62.         global $wpdb;
  63.  
  64.         $sql = "SELECT p.ID, p.post_modified_gmt FROM {$wpdb->postmeta} as pm
  65.                         INNER JOIN {$wpdb->posts} as p ON pm.post_id = p.ID
  66.                         WHERE pm.meta_key = 'rank_math_schema_VideoObject'
  67.                         AND post_type IN ( '" . join( "', '", esc_sql( $post_types ) ) . "' )
  68.                         AND post_status IN ( 'publish', 'inherit' )
  69.                         GROUP BY p.ID
  70.                         ORDER BY p.post_modified_gmt DESC";
  71.  
  72.         $posts       = $wpdb->get_results( $sql, ARRAY_A ); // phpcs:ignore
  73.         $total_count = count( $posts );
  74.         if ( 0 === $total_count ) {
  75.             return [];
  76.         }
  77.  
  78.         $max_pages = 1;
  79.         if ( $total_count > $max_entries ) {
  80.             $max_pages = (int) ceil( $total_count / $max_entries );
  81.         }
  82.  
  83.         $all_dates = array_chunk( $posts, $max_entries );
  84.         $index     = [];
  85.         for ( $page_counter = 0; $page_counter < $max_pages; $page_counter++ ) {
  86.             $current_page = ( $max_pages > 1 ) ? ( $page_counter + 1 ) : '';
  87.             $index[]      = [
  88.                 'loc'     => Router::get_base_url( 'video-sitemap' . $current_page . '.xml' ),
  89.                 'lastmod' => $all_dates[ $page_counter ][0]['post_modified_gmt'],
  90.             ];
  91.         }
  92.  
  93.         return $index;
  94.     }
  95.  
  96.     /**
  97.      * Get set of sitemap link data.
  98.      *
  99.      * @param  string $type         Sitemap type.
  100.      * @param  int    $max_entries  Entries per sitemap.
  101.      * @param  int    $current_page Current page of the sitemap.
  102.      * @return array
  103.      */
  104.     public function get_sitemap_links( $type, $max_entries, $current_page ) {
  105.         rank_math()->variables->setup();
  106.  
  107.         $post_types = (array) Helper::get_settings( 'sitemap.video_sitemap_post_type', [] );
  108.         $links      = [];
  109.         $steps      = min( 100, $max_entries );
  110.         $offset     = ( $current_page > 1 ) ? ( ( $current_page - 1 ) * $max_entries ) : 0;
  111.         $total      = ( $offset + $max_entries );
  112.         $typecount  = 0;
  113.  
  114.         $stacked_urls = [];
  115.         while ( $total > $offset ) {
  116.             $posts   = $this->get_posts( $post_types, $steps, $offset );
  117.             $offset += $steps;
  118.  
  119.             if ( empty( $posts ) ) {
  120.                 continue;
  121.             }
  122.  
  123.             foreach ( $posts as $post ) {
  124.  
  125.                 if ( ! Helper::is_post_indexable( $post->ID ) ) {
  126.                     continue;
  127.                 }
  128.  
  129.                 $url = $this->get_url( $post );
  130.                 if ( ! isset( $url['loc'] ) ) {
  131.                     continue;
  132.                 }
  133.  
  134.                 /**
  135.                  * Filter URL entry before it gets added to the sitemap.
  136.                  *
  137.                  * @param array  $url  Array of URL parts.
  138.                  * @param string $type URL type.
  139.                  * @param object $user Data object for the URL.
  140.                  */
  141.                 $url = $this->do_filter( 'sitemap/entry', $url, 'post', $post );
  142.                 if ( empty( $url ) ) {
  143.                     continue;
  144.                 }
  145.  
  146.                 $stacked_urls[] = $url['loc'];
  147.                 $links[]        = $url;
  148.             }
  149.  
  150.             unset( $post, $url );
  151.         }
  152.  
  153.         return $links;
  154.     }
  155.  
  156.     /**
  157.      * Produce array of URL parts for given post object.
  158.      *
  159.      * @param  object $post Post object to get URL parts for.
  160.      * @return array|boolean
  161.      */
  162.     protected function get_url( $post ) {
  163.         $url = [];
  164.  
  165.         /**
  166.          * Filter the URL Rank Math SEO uses in the XML sitemap.
  167.          *
  168.          * Note that only absolute local URLs are allowed as the check after this removes external URLs.
  169.          *
  170.          * @param string $url  URL to use in the XML sitemap
  171.          * @param object $post Post object for the URL.
  172.          */
  173.         $url['loc'] = $this->do_filter( 'sitemap/xml_post_url', get_permalink( $post ), $post );
  174.  
  175.         /**
  176.          * Do not include external URLs.
  177.          *
  178.          * @see https://wordpress.org/plugins/page-links-to/ can rewrite permalinks to external URLs.
  179.          */
  180.         if ( 'external' === $this->get_classifier()->classify( $url['loc'] ) ) {
  181.             return false;
  182.         }
  183.  
  184.         // Include the post modified date.
  185.         $url['modified_date'] = get_the_modified_date( '', $post );
  186.  
  187.         $canonical = Helper::get_post_meta( 'canonical', $post->ID );
  188.         if ( '' !== $canonical && $canonical !== $url['loc'] ) {
  189.             /*
  190.              * Let's assume that if a canonical is set for this page and it's different from
  191.              * the URL of this post, that page is either already in the XML sitemap OR is on
  192.              * an external site, either way, we shouldn't include it here.
  193.              */
  194.             return false;
  195.         }
  196.         unset( $canonical );
  197.         $schemas = get_post_meta( $post->ID, 'rank_math_schema_VideoObject' );
  198.         if ( empty( $schemas ) ) {
  199.             return false;
  200.         }
  201.  
  202.         if ( 'post' !== $post->post_type ) {
  203.             $url['loc'] = trailingslashit( $url['loc'] );
  204.         }
  205.  
  206.         $url['author'] = $post->post_author;
  207.         $url['videos'] = [];
  208.         foreach ( $schemas as $schema ) {
  209.             $url['videos'][] = [
  210.                 'title'             => ! empty( $schema['name'] ) ? Helper::replace_vars( $schema['name'], $post ) : '',
  211.                 'thumbnail_loc'     => ! empty( $schema['thumbnailUrl'] ) ? Helper::replace_vars( $schema['thumbnailUrl'], $post ) : '',
  212.                 'description'       => ! empty( $schema['description'] ) ? Helper::replace_vars( $schema['description'], $post ) : '',
  213.                 'publication_date'  => ! empty( $schema['uploadDate'] ) ? Helper::replace_vars( $schema['uploadDate'], $post ) : '',
  214.                 'modification_date' => $this->timezone->format_date( $post->post_modified_gmt ),
  215.                 'content_loc'       => ! empty( $schema['contentUrl'] ) ? Helper::replace_vars( $schema['contentUrl'], $post ) : '',
  216.                 'player_loc'        => ! empty( $schema['embedUrl'] ) ? Helper::replace_vars( $schema['embedUrl'], $post ) : '',
  217.                 'duration'          => ! empty( $schema['duration'] ) ? Helper::duration_to_seconds( Helper::replace_vars( $schema['duration'], $post ) ) : '',
  218.                 'tags'              => ! empty( $schema['metadata']['tags'] ) ? Helper::replace_vars( $schema['metadata']['tags'], $post ) : '',
  219.                 'family_friendly'   => ! empty( $schema['isFamilyFriendly'] ) ? 'yes' : 'no',
  220.                 'rating'            => ! empty( $schema['metadata']['rating'] ) ? $schema['metadata']['rating'] : '',
  221.             ];
  222.         }
  223.  
  224.         return $url;
  225.     }
  226.  
  227.     /**
  228.      * Retrieve set of posts with optimized query routine.
  229.      *
  230.      * @param array $post_types Post type to retrieve.
  231.      * @param int   $count      Count of posts to retrieve.
  232.      * @param int   $offset     Starting offset.
  233.      *
  234.      * @return object[]
  235.      */
  236.     protected function get_posts( $post_types, $count, $offset ) { // phpcs:ignore
  237.         global $wpdb;
  238.         $sql = "SELECT p.* FROM {$wpdb->postmeta} as pm
  239.                         INNER JOIN {$wpdb->posts} as p ON pm.post_id = p.ID
  240.                         WHERE pm.meta_key = 'rank_math_schema_VideoObject'
  241.                         AND post_type IN ( '" . join( "', '", esc_sql( $post_types ) ) . "' )
  242.                         AND post_status IN ( 'publish', 'inherit' )
  243.                         AND post_password = ''
  244.                         GROUP BY p.ID
  245.                         ORDER BY p.post_modified DESC
  246.                         LIMIT %d OFFSET %d";
  247.  
  248.         return $wpdb->get_results( $wpdb->prepare( $sql, $count, $offset ) ); // phpcs:ignore
  249.  
  250.     }
  251. }
  252.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement