Advertisement
NicholasB

class-video-sitemap.php

Apr 27th, 2023
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.08 KB | None | 0 0
  1. /**
  2.      * Build the `<url>` tag for a given URL.
  3.      *
  4.      * @param  array    $url      Array of parts that make up this entry.
  5.      * @param  Renderer $renderer Sitemap renderer class object.
  6.      * @return string
  7.      */
  8.     public function sitemap_url( $url, $renderer ) {
  9.         $output  = $renderer->newline( '<url>', 1 );
  10.         $output .= $renderer->newline( '<loc>' . $renderer->encode_url_rfc3986( htmlspecialchars( $url['loc'] ) ) . '</loc>', 2 );
  11.  
  12.         if ( ! empty( $url['videos'] ) ) {
  13.             foreach ( $url['videos'] as $video ) {
  14.                 $date = null;
  15.                 if ( ! empty( $video['publication_date'] ) ) {
  16.                     // Create a DateTime object date in the correct timezone.
  17.                     $date = $renderer->timezone->format_date( $video['publication_date'] );
  18.                 }
  19.  
  20.                 $output .= $renderer->newline( '<video:video>', 2 );
  21.  
  22.                 $output .= $renderer->add_cdata( $video['title'], 'video:title', 3 );
  23.                 $output .= empty( $date ) ? '' : $renderer->newline( '<video:publication_date>' . htmlspecialchars( $date ) . '</video:publication_date>', 3 );
  24.                 $output .= $renderer->add_cdata( $video['description'], 'video:description', 3 );
  25.  
  26.                 if ( ! empty( $video['player_loc'] ) ) {
  27.                     $output .= $renderer->newline( '<video:player_loc>' . esc_url( $video['player_loc'] ) . '</video:player_loc>', 3 );
  28.                 }
  29.  
  30.                 foreach ( [ 'thumbnail_loc', 'content_loc' ] as $prop ) {
  31.                     if ( empty( $video[ $prop ] ) ) {
  32.                         continue;
  33.                     }
  34.  
  35.                     /**
  36.                      * Filter the video content and thumbnail location:
  37.                      * - rank_math/sitemap/video/thumbnail_loc
  38.                      * - rank_math/sitemap/video/content_loc
  39.                      */
  40.                     $value = $this->do_filter( "sitemap/video/{$prop}", $video[ $prop ] );
  41.  
  42.                     $output .= $renderer->newline( "<video:{$prop}>" . esc_url( $value ) . "</video:{$prop}>", 3 );
  43.                 }
  44.  
  45.                 if ( ! empty( $video['tags'] ) ) {
  46.                     $tags = explode( ', ', $video['tags'] );
  47.                     foreach ( $tags as $tag ) {
  48.                         $output .= $renderer->add_cdata( $tag, 'video:tag', 3 );
  49.                     }
  50.                 }
  51.                 $output .= $renderer->add_cdata( $video['modification_date'], 'video:modification_date', 3 );
  52.  
  53.                 if ( ! empty( $video['rating'] ) ) {
  54.                     $output .= $renderer->newline( '<video:rating>' . absint( $video['rating'] ) . '</video:rating>', 3 );
  55.                 }
  56.  
  57.                 if ( ! empty( $video['duration'] ) ) {
  58.                     $output .= $renderer->newline( '<video:duration>' . esc_html( $video['duration'] ) . '</video:duration>', 3 );
  59.                 }
  60.  
  61.                 $output .= $renderer->newline( '<video:family_friendly>' . $video['family_friendly'] . '</video:family_friendly>', 3 );
  62.                 $output .= $renderer->newline( '<video:uploader info="' . get_author_posts_url( $url['author'] ) . '">' . ent2ncr( esc_html( get_the_author_meta( 'display_name', $url['author'] ) ) ) . '</video:uploader>', 3 );
  63.                 $output .= $renderer->newline( '</video:video>', 2 );
  64.             }
  65.         }
  66.         $output .= $renderer->newline( '</url>', 1 );
  67.  
  68.         /**
  69.          * Filters the output for the sitemap url tag.
  70.          *
  71.          * @param string $output The output for the sitemap url tag.
  72.          * @param array  $url    The sitemap url array on which the output is based.
  73.          */
  74.         return $this->do_filter( 'sitemap_url', $output, $url );
  75.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement