Advertisement
phpface

Untitled

Jun 28th, 2024 (edited)
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.22 KB | None | 0 0
  1. /**
  2.  *
  3.  * Filter subtitles
  4.  *
  5.  * @param array $tracks
  6.  * @param int $post_id
  7.  *
  8.  */
  9. add_filter( 'streamtube/core/post/text_tracks', function( $tracks, $post_id ){
  10.  
  11.     // Return tracks if they have been manually uploaded through the form.
  12.     // Otherwise, remove these two lines.
  13.  
  14.     if( $tracks ){
  15.         return $tracks;
  16.     }
  17.  
  18.     $_tracks = array();
  19.  
  20.     // Get the video URL meta value
  21.     $video_url = get_post_meta( $post_id, 'video_url', true );
  22.  
  23.     if( ! $video_url ){
  24.         return false;
  25.     }
  26.  
  27.     // Extract the YouTube ID from the video URL
  28.     preg_match('/(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/', $video_url, $matches);
  29.  
  30.     if( ! $matches || ! isset( $matches[1] ) ){
  31.         return false;
  32.     }
  33.  
  34.     $youtube_id = $matches[1];
  35.  
  36.     // Directory where subtitle files are stored
  37.     $subtitle_dir = WP_CONTENT_DIR . '/uploads/subtitles/' . $youtube_id;
  38.  
  39.     // Check if directory exists
  40.     $subtitle_files = array();
  41.     if( is_dir( $subtitle_dir ) ){
  42.         // Scan the directory for .vtt files
  43.         $subtitle_files = glob( $subtitle_dir . '/*.vtt' );
  44.     }
  45.  
  46.     // Get the text tracks from post meta
  47.     $tracks = get_post_meta( $post_id, 'text_tracks', true );
  48.  
  49.     if( $tracks && is_array( $tracks ) && array_key_exists( 'languages', $tracks ) && is_array( $tracks['languages'] ) ){
  50.         for ( $i = 0; $i < count( $tracks['languages'] ); $i++) {
  51.             if( $tracks['languages'][$i] && $tracks['sources'][$i] ){
  52.                 $file_type = wp_check_filetype( $tracks['sources'][$i] );
  53.                 if( array_key_exists( 'ext', $file_type ) && in_array( strtolower( $file_type['ext'] ), apply_filters( 'streamtube/core/text_track_format',  array( 'vtt' ) ) ) ){
  54.                     $_tracks[] = array(
  55.                         'language'  =>  $tracks['languages'][$i],
  56.                         'source'    =>  $tracks['sources'][$i]
  57.                     );
  58.                 }
  59.             }
  60.         }
  61.     }
  62.  
  63.     // Add local subtitle files if not already added
  64.     if( $subtitle_files ){
  65.         foreach ( $subtitle_files as $subtitle_file ) {
  66.             $file_name = basename( $subtitle_file, '.vtt' );
  67.             $file_parts = explode('.', $file_name);
  68.  
  69.             if( count( $file_parts ) >= 2 && $file_parts[0] === $youtube_id ){
  70.                 // Extract language code before the hyphen if present
  71.                 $language = explode('-', $file_parts[1])[0];
  72.                 $source = content_url( '/uploads/subtitles/' . $youtube_id . '/' . basename( $subtitle_file ) );
  73.  
  74.                 // Check if this language is already added from meta
  75.                 $language_exists = false;
  76.                 foreach( $_tracks as $track ){
  77.                     if( $track['language'] === $language ){
  78.                         $language_exists = true;
  79.                         break;
  80.                     }
  81.                 }
  82.  
  83.                 if( ! $language_exists ){
  84.                     $_tracks[] = array(
  85.                         'language' => $language,
  86.                         'source'   => $source
  87.                     );
  88.                 }
  89.             }
  90.         }
  91.     }
  92.  
  93.     return $_tracks;
  94.  
  95. }, 10, 2 );
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement