Changeset 10463
- Timestamp:
- 11/25/2020 04:42:42 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/functions.php
r10000 r10463 56 56 add_action( 'wp_head', array( $this, 'rel_canonical' ) ); 57 57 add_action( 'wp_head', array( $this, 'archive_link_rel_prev_next' ) ); 58 add_action( 'wp_head', array( $this, 'json_ld' ) ); 58 59 } 59 60 … … 161 162 '<link rel="next" href="%s">' . "\n", 162 163 esc_url( get_pagenum_link( $nextpage ) ) 164 ); 165 } 166 } 167 168 function json_ld() { 169 $post = get_post(); 170 $data = []; 171 172 if ( is_singular() ) { 173 174 $video = $this->get_the_video_details(); 175 176 $lang = false; 177 if ( $lang_terms = get_the_terms( $post, 'language' ) ) { 178 if ( ! is_wp_error( $lang_terms ) ) { 179 $lang = $this->locale_to_wp_locale( $lang_terms[0]->slug ); 180 $lang = str_replace( '_', '-', $lang ); // TODO Might not always be correct. 181 } 182 } 183 if ( ! $lang ) { 184 $lang = 'en'; 185 } 186 187 $video_data = [ 188 '@type' => 'VideoObject', 189 'actor' => [], 190 'name' => get_the_title(), 191 'url' => get_the_permalink(), 192 'contentUrl' => $this->get_the_video_urls(), 193 'description' => get_the_excerpt(), 194 'duration' => gmdate( '\P0\D\TH\Hi\Ms\S', $video->duration ), // 1970-01-01 + duration 195 'height' => $vid->height ?? 530, 196 'inLanguage' => $lang, 197 'isFamilyFriendly' => ( 'G' === $video->rating || 'PG' === $video->rating ), 198 'recordedAt' => [], 199 'thumbnailUrl' => $this->get_the_video_image(), 200 'uploadDate' => gmdate( 'Y-m-d', strtotime( get_the_date() ) ), 201 'width' => $vid->width ?? 940, 202 ]; 203 204 foreach ( get_the_terms( $post, 'speakers' ) as $speaker ) { 205 $video_data['actor'][] = [ 206 '@type' => 'Person', 207 'name' => $speaker->name, 208 'url' => get_term_link( $speaker ), 209 ]; 210 } 211 212 foreach ( get_the_terms( $post, 'event' ) as $event ) { 213 $video_data['recordedAt'][] = [ 214 '@type' => 'Event', 215 'name' => $event->name, 216 'url' => get_term_link( $event ), 217 ]; 218 } 219 220 $data[] = array_filter( $video_data, function( $item ) { 221 return '' !== $item && [] !== $item; 222 } ); 223 } 224 225 if ( $data ) { 226 printf( 227 "\n" . '<script type="application/ld+json">%s</script>' . "\n", 228 wp_json_encode( 229 [ 230 '@context' => 'https://schema.org', 231 '@graph' => $data 232 ], 233 JSON_PRETTY_PRINT 234 ) 163 235 ); 164 236 } … … 615 687 */ 616 688 function the_video_image( $h = 196, $w = 400, $arrow = true, $html_code = true ) { 617 $ret = ''; 618 global $post; 689 $ret = $this->get_the_video_image(); 690 691 if ( $arrow ) { 692 ?><a href="<?php the_permalink() ?>" class="showarrow arrow"><?php the_title(); ?></a><?php 693 } 694 if ( $html_code ) { 695 $ret = '<img src="' . $ret . '" alt="' . esc_attr( $post->post_title ) . '" />'; 696 } 697 echo $ret; 698 } 699 700 /** 701 * Retrieves the Video thumbnail image for a post. 702 */ 703 function get_the_video_image( $post = null ) { 704 $ret = false; 705 706 $guid = $this->get_the_video_guid( $post ); 707 708 if ( $guid && function_exists( 'video_get_highest_resolution_image_url' ) ) { 709 $ret = video_get_highest_resolution_image_url( $guid ); 710 } 711 712 return $ret; 713 } 714 715 /** 716 * Retrieve the video files for a given video guid. 717 */ 718 function get_the_video_urls( $post = null ) { 719 $details = $this->get_the_video_details( $post ); 720 if ( ! $details ) { 721 return; 722 } 723 724 $urls = []; 725 foreach ( [ 'fmt_hd', 'fmt_dvd', 'fmt_std', 'fmt1_ogg' ] as $format ) { 726 $fmt_url = video_url_by_format( $details, $format ); 727 if ( $fmt_url ) { 728 $urls[] = $fmt_url; 729 } 730 } 731 732 return $urls; 733 } 734 735 /** 736 * Retrieve the Video details for a given guid. 737 */ 738 function get_the_video_details( $post = null ) { 739 $guid = $this->get_the_video_guid( $post ); 740 741 $ret = false; 742 if ( $guid && function_exists( 'video_get_info_by_guid' ) ) { 743 $ret = video_get_info_by_guid( $guid ); 744 } 745 746 return $ret; 747 } 748 749 /** 750 * Retrieves the guid for the wpvideo video for a given post. 751 */ 752 function get_the_video_guid( $post = null ) { 753 $post = get_post( $post ); 754 619 755 remove_filter( 'the_content', array( $this, 'remove_shortcodes' ) ); 756 757 $guid = false; 620 758 621 759 preg_match_all( '/\[wpvideo +([a-zA-Z0-9,\#,\&,\/,;,",=, ]*?)\]/i', $post->post_content, $matches ); … … 623 761 preg_match( '/([0-9A-Za-z]+)/i', $code, $m ); 624 762 $guid = $m[1]; 625 $ret = video_image_url_by_guid( $guid, 'fmt_dvd' ); 626 } 627 628 if ( $arrow ) { 629 ?><a href="<?php the_permalink() ?>" class="showarrow arrow"><?php the_title(); ?></a><?php 630 } 631 if ( $html_code ) { 632 $ret = '<img src="' . $ret . '" alt="' . esc_attr( $post->post_title ) . '" />'; 633 } 634 echo $ret; 763 } 635 764 636 765 add_filter( 'the_content', array( $this, 'remove_shortcodes' ) ); 766 767 return $guid; 637 768 } 638 769 … … 735 866 736 867 bump_stats_extras( 'wptv-activity', 'publish-video' ); 868 } 869 870 /** 871 * Convert a WPTV Locale to a WP Locale code. 872 * 873 * This isn't all correct, and many of these need to be combined. 874 * 875 * See https://meta.trac.wordpress.org/ticket/1156 876 */ 877 function locale_to_wp_locale( $locale ) { 878 static $locales = [ 879 'bengali' => 'bn_BD', 880 'bulgarianбългарски-език' => 'bg_BG', 881 // 'cantonese-廣東話' => '', 882 'catalan' => 'ca', 883 'croatianhrvatski' => 'hr', 884 'czechcestina' => 'cs_CZ', 885 'danishdansk' => 'da_DK', 886 'dutchnederlands' => 'nl_NL', 887 'english' => 'en', 888 'english-swahili' => 'sw', 889 'english-and-dutch' => 'nl_NL', 890 // 'euskera' => '', 891 'finnishsuomi' => 'fi', 892 'frenchfrancais' => 'fr_FR', 893 // 'galician-galego' => '', 894 'germandeutsch' => 'de_DE', 895 'greek-ελληνικά' => 'el', 896 'gujarati' => 'gu', 897 'hebrewעברית' => 'he_IL', 898 'hindi' => 'hi_IN', 899 'indonesian' => 'id_ID', 900 'italianitaliano' => 'it_IT', 901 'ελληνικά' => 'el', 902 'japanese' => 'ja', 903 'japanese-and-english' => 'ja', 904 'japanese日本語' => 'ja', 905 'kananda' => 'kn', 906 'lithuanian' => 'lt_LT', 907 'malay' => 'ms_MY', 908 'malay-bahasa-melayu' => 'ms_MY', 909 'malayalam' => 'ms_MY', 910 'marathi' => 'mr', 911 'maori' => 'mri', 912 'nepaliनेपाली' => 'ne_NP', 913 'norwegiannorsk' => 'nb_NO', 914 'persian-farsi' => 'fa_IR', 915 'polishpolski' => 'pl_PL', 916 'portugueseportugues' => 'pt_BR', 917 'romanianromana' => 'ro_RO', 918 'russianрусский' => 'ru_RU', 919 'serbianсрпски' => 'sr_RS', 920 'slovakslovencina' => 'sk_SK', 921 //'span' => '', 922 'spanishespanol' => 'es_ES', 923 'swedishsvenska' => 'sv_SE', 924 'thai' => 'th', 925 'traditional-chinese' => 'zh_CN', 926 'ukrainianукраїнська' => 'uk', 927 'urdu' => 'ur', 928 'urdu-and-english' => 'ur', 929 'vietnamese' => 'vi', 930 ]; 931 932 return $locales[ $locale ] ?? false; 737 933 } 738 934 }
Note: See TracChangeset
for help on using the changeset viewer.