<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[10463] sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/functions.php: WordPress.TV: Add initial implementation of `ld+json` schema.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/10463">10463</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/10463","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dd32</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2020-11-25 04:42:42 +0000 (Wed, 25 Nov 2020)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>WordPress.TV: Add initial implementation of `ld+json` schema.

See <a href="http://meta.trac.wordpress.org/ticket/5512">#5512</a>, <a href="http://meta.trac.wordpress.org/ticket/1156">#1156</a> (For the Slug => Locale expansion)</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpresstvpublic_htmlwpcontentthemeswptv2functionsphp">sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/functions.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpresstvpublic_htmlwpcontentthemeswptv2functionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/functions.php        2020-11-25 03:18:28 UTC (rev 10462)
+++ sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/functions.php  2020-11-25 04:42:42 UTC (rev 10463)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -55,6 +55,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'wp_head', array( $this, 'rel_canonical' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'wp_head', array( $this, 'archive_link_rel_prev_next' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_action( 'wp_head', array( $this, 'json_ld' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -164,6 +165,77 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        function json_ld() {
+               $post = get_post();
+               $data = [];
+
+               if ( is_singular() ) {
+
+                       $video = $this->get_the_video_details();
+
+                       $lang = false;
+                       if ( $lang_terms = get_the_terms( $post, 'language' ) ) {
+                               if ( ! is_wp_error( $lang_terms ) ) {
+                                       $lang = $this->locale_to_wp_locale( $lang_terms[0]->slug );
+                                       $lang = str_replace( '_', '-', $lang ); // TODO Might not always be correct.
+                               }
+                       }
+                       if ( ! $lang ) {
+                               $lang = 'en';
+                       }
+
+                       $video_data = [
+                               '@type'            => 'VideoObject',
+                               'actor'            => [],
+                               'name'             => get_the_title(),
+                               'url'              => get_the_permalink(),
+                               'contentUrl'       => $this->get_the_video_urls(),
+                               'description'      => get_the_excerpt(),
+                               'duration'         => gmdate( '\P0\D\TH\Hi\Ms\S', $video->duration ), // 1970-01-01 + duration
+                               'height'           => $vid->height ?? 530,
+                               'inLanguage'       => $lang,
+                               'isFamilyFriendly' => ( 'G' === $video->rating || 'PG' === $video->rating ),
+                               'recordedAt'       => [],
+                               'thumbnailUrl'     => $this->get_the_video_image(),
+                               'uploadDate'       => gmdate( 'Y-m-d', strtotime( get_the_date() ) ),
+                               'width'            => $vid->width ?? 940,
+                       ];
+
+                       foreach ( get_the_terms( $post, 'speakers' ) as $speaker ) {
+                               $video_data['actor'][] = [
+                                       '@type' => 'Person',
+                                       'name'  => $speaker->name,
+                                       'url'   => get_term_link( $speaker ),
+                               ];
+                       }
+
+                       foreach ( get_the_terms( $post, 'event' ) as $event ) {
+                               $video_data['recordedAt'][] = [
+                                       '@type' => 'Event',
+                                       'name'  => $event->name,
+                                       'url'   => get_term_link( $event ),
+                               ];
+                       }
+
+                       $data[] = array_filter( $video_data, function( $item ) { 
+                               return '' !== $item && [] !== $item;
+                       } );
+               }
+
+               if ( $data ) {
+                       printf(
+                               "\n" . '<script type="application/ld+json">%s</script>' . "\n",
+                               wp_json_encode(
+                                       [
+                                               '@context' => 'https://schema.org',
+                                               '@graph'   => $data
+                                       ],
+                                       JSON_PRETTY_PRINT
+                               )
+                       );
+               }
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Different posts_per_page settings for different views. Runs during pre_get_posts.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -614,17 +686,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @param bool $html_code
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        function the_video_image( $h = 196, $w = 400, $arrow = true, $html_code = true ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $ret = '';
-               global $post;
-               remove_filter( 'the_content', array( $this, 'remove_shortcodes' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $ret = $this->get_the_video_image();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                preg_match_all( '/\[wpvideo +([a-zA-Z0-9,\#,\&,\/,;,",=, ]*?)\]/i', $post->post_content, $matches );
-               foreach ( $matches[1] as $key => $code ) {
-                       preg_match( '/([0-9A-Za-z]+)/i', $code, $m );
-                       $guid = $m[1];
-                       $ret = video_image_url_by_guid( $guid, 'fmt_dvd' );
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 if ( $arrow ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        ?><a href="<?php the_permalink() ?>" class="showarrow arrow"><?php the_title(); ?></a><?php
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -632,8 +695,76 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $ret = '<img src="' . $ret . '" alt="' . esc_attr( $post->post_title ) . '" />';
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">                echo $ret;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Retrieves the Video thumbnail image for a post.
+        */
+       function get_the_video_image( $post = null ) {
+               $ret = false;
+
+               $guid = $this->get_the_video_guid( $post );
+
+               if ( $guid && function_exists( 'video_get_highest_resolution_image_url' ) ) {
+                       $ret = video_get_highest_resolution_image_url( $guid );
+               }
+
+               return $ret;
+       }
+
+       /**
+        * Retrieve the video files for a given video guid.
+        */
+       function get_the_video_urls( $post = null ) {
+               $details = $this->get_the_video_details( $post );
+               if ( ! $details ) {
+                       return;
+               }
+
+               $urls = [];
+               foreach ( [ 'fmt_hd', 'fmt_dvd', 'fmt_std', 'fmt1_ogg' ] as $format ) {
+                       $fmt_url = video_url_by_format( $details, $format );
+                       if ( $fmt_url ) {
+                               $urls[] = $fmt_url;
+                       }
+               }
+
+               return $urls;
+       }
+
+       /**
+        * Retrieve the Video details for a given guid.
+        */
+       function get_the_video_details( $post = null ) {
+               $guid = $this->get_the_video_guid( $post );
+
+               $ret = false;
+               if ( $guid && function_exists( 'video_get_info_by_guid' ) ) {
+                       $ret = video_get_info_by_guid( $guid );
+               }
+
+               return $ret;
+       }
+
+       /**
+        * Retrieves the guid for the wpvideo video for a given post.
+        */
+       function get_the_video_guid( $post = null ) {
+               $post = get_post( $post );
+
+               remove_filter( 'the_content', array( $this, 'remove_shortcodes' ) );
+
+               $guid = false;
+
+               preg_match_all( '/\[wpvideo +([a-zA-Z0-9,\#,\&,\/,;,",=, ]*?)\]/i', $post->post_content, $matches );
+               foreach ( $matches[1] as $key => $code ) {
+                       preg_match( '/([0-9A-Za-z]+)/i', $code, $m );
+                       $guid = $m[1];
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 add_filter( 'the_content', array( $this, 'remove_shortcodes' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               return $guid;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -735,6 +866,71 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                bump_stats_extras( 'wptv-activity', 'publish-video' );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * Convert a WPTV Locale to a WP Locale code.
+        * 
+        * This isn't all correct, and many of these need to be combined.
+        * 
+        * See https://meta.trac.wordpress.org/ticket/1156
+        */
+       function locale_to_wp_locale( $locale ) {
+               static $locales = [
+                       'bengali' => 'bn_BD',
+                       'bulgarianбългарски-език' => 'bg_BG',
+                       // 'cantonese-廣東話' => '',
+                       'catalan' => 'ca',
+                       'croatianhrvatski' => 'hr',
+                       'czechcestina' => 'cs_CZ',
+                       'danishdansk' => 'da_DK',
+                       'dutchnederlands' => 'nl_NL',
+                       'english' => 'en',
+                       'english-swahili' => 'sw',
+                       'english-and-dutch' => 'nl_NL',
+                       // 'euskera' => '',
+                       'finnishsuomi' => 'fi',
+                       'frenchfrancais' => 'fr_FR',
+                       // 'galician-galego' => '',
+                       'germandeutsch' => 'de_DE',
+                       'greek-ελληνικά' => 'el',
+                       'gujarati' => 'gu',
+                       'hebrewעברית' => 'he_IL',
+                       'hindi' => 'hi_IN',
+                       'indonesian' => 'id_ID',
+                       'italianitaliano' => 'it_IT',
+                       'ελληνικά' => 'el',
+                       'japanese' => 'ja',
+                       'japanese-and-english' => 'ja',
+                       'japanese日本語' => 'ja',
+                       'kananda' => 'kn',
+                       'lithuanian' => 'lt_LT',
+                       'malay' => 'ms_MY',
+                       'malay-bahasa-melayu' => 'ms_MY',
+                       'malayalam' => 'ms_MY',
+                       'marathi' => 'mr',
+                       'maori' => 'mri',
+                       'nepaliनेपाली' => 'ne_NP',
+                       'norwegiannorsk' => 'nb_NO',
+                       'persian-farsi' => 'fa_IR',
+                       'polishpolski' => 'pl_PL',
+                       'portugueseportugues' => 'pt_BR',
+                       'romanianromana' => 'ro_RO',
+                       'russianрусский' => 'ru_RU',
+                       'serbianсрпски' => 'sr_RS',
+                       'slovakslovencina' => 'sk_SK',
+                       //'span' => '',
+                       'spanishespanol' => 'es_ES',
+                       'swedishsvenska' => 'sv_SE',
+                       'thai' => 'th',
+                       'traditional-chinese' => 'zh_CN',
+                       'ukrainianукраїнська' => 'uk',
+                       'urdu' => 'ur',
+                       'urdu-and-english' => 'ur',
+                       'vietnamese' => 'vi',
+               ];
+
+               return $locales[ $locale ] ?? false;
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> global $wptv;
</span></span></pre>
</div>
</div>

</body>
</html>