post_type ) { echo $post_content; return; } $matches = array(); if ( ! preg_match_all( '/(?P(<\s{0,}(?P([^>\s]))\s{0,}>))?\s{0,}(?P((\d{1,2}:)?\d{1,2}:\d{1,2}))\s+(?P(.+))(<br\s{0,}\/?>){0,}(?P<endtag>(<\/(\s{0,})(?P<endtagname>([^>\s]))(\s{0,})>)?)/im', $post_content, $matches ) ) { echo $post_content; return; } $video_guid = get_post_meta( $post->ID, 'guid', true ); $video_details = null; if ( $video_guid ) { $video_details = videopress_hq_get_video_details( $video_guid ); } if ( ! $video_details ) { echo $post_content; return; } $post_permalink = get_post_permalink( $post->ID ); $playlist_slug = get_query_var( VideoPressHQPlaylist::TAXONOMY_NAME ); if ( $playlist_slug ) { $playlist = VideoPressHQPlaylist::get_by_slug( $playlist_slug ); if ( $playlist ) { $post_permalink = $playlist->get_post_permalink( $post->ID ); } } $patterns = $matches[0]; $timestamps = $matches['timestamp']; $titles = $matches['title']; $end_tags = $matches['endtag']; $start_tags = $matches['starttag']; $start_tag_names = $matches['starttagname']; $end_tag_names = $matches['endtagname']; $ts_size = is_countable( $timestamps ) ? count( $timestamps ) : 0; $title_size = is_countable( $titles ) ? count( $titles ) : 0; $content_offset = 0; $chapters_list = array(); for ( $i = 0; $i < $ts_size && $i < $title_size; ++$i ) { $timestamp = array_reverse( explode( ':', wp_strip_all_tags( trim( $timestamps[ $i ] ), true ) ) ); $timestamp_sec = 0; $is_invalid = false; $current_multiplier = 1; foreach ( $timestamp as $value ) { $value = trim( $value ); if ( ! is_numeric( $value ) ) { $is_invalid = true; break; } $timestamp_sec += intval( $value ) * $current_multiplier; $current_multiplier *= 60; } if ( ! $is_invalid ) { $title = wp_strip_all_tags( html_entity_decode( trim( $titles[$i] ) ), true); $chapters_list[] = array( 'timestamp' => $timestamp_sec, 'timestamp_str' => gmdate( 'H:i:s', $timestamp_sec ), 'title' => $title, ); } $replace_index = strpos( $post_content, $patterns[ $i ], $content_offset ); $replace_length = strlen( $patterns[ $i ] ); // If we don't have matching opening and closing tags, leave them in the content. if ( $start_tag_names[ $i ] != $end_tag_names[ $i ] ) { $replace_index += strlen( $start_tags[ $i ] ); $replace_length -= strlen( $start_tags[ $i ] ) + strlen( $end_tags[ $i ] ); } $post_content = substr_replace( $post_content, '', $replace_index, $replace_length ); $content_offset = $replace_index; } usort( $chapters_list, function ( $a, $b ) { return $a['timestamp'] - $b['timestamp']; }); $chapters_count = count( $chapters_list ); $js_chapters = array(); for ( $i = 0; $i < $chapters_count; ++$i ) { $chapter = $chapters_list[ $i ]; $timestamp = $chapter['timestamp']; $title = $chapter['title']; $timestamp_str = $chapter['timestamp_str']; $next_chapter_timestamp = null; $js_chapter = array( 'start' => $timestamp, 'title' => esc_html( $title ), ); if ( $i < ( $chapters_count - 1 ) ) { $next_chapter = $chapters_list[ $i + 1 ]; $next_chapter_timestamp = $next_chapter['timestamp_str']; $js_chapter['end'] = $next_chapter['timestamp']; } else { $next_chapter_timestamp = gmdate( 'H:i:s', $video_details->duration / 1000 ); $js_chapter['end'] = $video_details->duration / 1000; } $js_chapters[] = $js_chapter; $chapter_permalink = add_query_arg( 't', $timestamp, $post_permalink ); $chapters_block .= '<a href="' . esc_attr( $chapter_permalink ) . '" class="vp-chapter-link" data-timestamp="' . esc_attr( $timestamp ) . '">' . esc_html( $timestamp_str ) . ' -> ' . esc_html( $next_chapter_timestamp ) . ' : ' . esc_html( $title ) . '</a>'; } if ( ! empty( $chapters_block ) ) : ?> <script> window.chaptersList = <?php echo json_encode( $js_chapters ); ?>; </script> <div class="post-vp-chapters"> <div class="post-vp-chapters-title"><i class="dashicons dashicons-arrow-down-alt2"></i><?php esc_html_e( 'Chapters', 'videopress-hq' ); ?></div> <div class="post-vp-chapters-content"> <?php echo $chapters_block; ?> </div> </div> <?php endif; echo $post_content;