<!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>[3319] sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory: Display translated readme content on the front-end.</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 { 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/3319">3319</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/3319","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>tellyworth</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-06-08 02:55:55 +0000 (Wed, 08 Jun 2016)</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'>Display translated readme content on the front-end.
This does on-the-fly translation rather than storing the data. It could and probably should eventually be changed to store that content in postmeta, in a way that is compatible with ElasticSearch indexing.
See <a href="http://meta.trac.wordpress.org/ticket/1691">#1691</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryclassplugindirectoryphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryclassplugini18nphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-i18n.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryclassplugindirectoryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php 2016-06-07 20:28:06 UTC (rev 3318)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php 2016-06-08 02:55:55 UTC (rev 3319)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -216,6 +216,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 'en_US' != get_locale() ) {
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'get_term', array( __NAMESPACE__ . '\i18n', 'translate_term' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ add_filter( 'the_content', array( $this, 'translate_post_content' ), 1, 2 );
+ add_filter( 'the_title', array( $this, 'translate_post_title' ), 1, 2 );
+ add_filter( 'get_the_excerpt', array( $this, 'translate_post_excerpt' ), 1 );
</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"> // Instantiate our copy of the Jetpack_Search class.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -441,6 +444,42 @@
</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">+ * Returns the requested page's content, translated.
+ *
+ * @param string $content
+ * @return string
+ */
+ public function translate_post_content( $content, $section = null ) {
+ if ( is_null( $section ) ) {
+ return $content;
+ }
+ return Plugin_i18n::instance()->translate( $section, $content );
+ }
+
+ /**
+ * Returns the requested page's content, translated.
+ *
+ * @param string $content
+ * @return string
+ */
+ public function translate_post_title( $title, $post_id ) {
+ if ( $post_id === get_post()->ID ) {
+ return Plugin_i18n::instance()->translate( 'title', $title );
+ }
+ return $title;
+ }
+
+ /**
+ * Returns the requested page's excerpt, translated.
+ *
+ * @param string $content
+ * @return string
+ */
+ public function translate_post_excerpt( $excerpt ) {
+ return Plugin_i18n::instance()->translate( 'excerpt', $excerpt );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Filter for rest_api_allowed_post_types to enable JP syncing of the CPT
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $allowed_post_types
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryclassplugini18nphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-i18n.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-i18n.php (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-i18n.php 2016-06-08 02:55:55 UTC (rev 3319)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,381 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+namespace WordPressdotorg\Plugin_Directory;
+
+/**
+ * Translation for plugin content.
+ *
+ * @package WordPressdotorg\Plugin_Directory
+ */
+class Plugin_I18n {
+
+ /**
+ * @var string Global cache group for related caching
+ */
+ var $i18n_cache_group = 'plugins-i18n';
+
+ public static $use_cache = true;
+ public static $set_cache = true;
+
+ /**
+ * Fetch the instance of the Plugin_I18n class.
+ */
+ public static function instance() {
+ static $instance = null;
+
+ global $wpdb;
+ return ! is_null( $instance ) ? $instance : $instance = new Plugin_I18n( $wpdb );
+ }
+
+ function __construct( $db, $tracker = null ) {
+ if ( !empty( $db ) && is_object( $db ) ) {
+ $this->db = $db;
+ }
+
+ wp_cache_add_global_groups( $this->i18n_cache_group );
+ }
+
+ /**
+ * Generates and returns a standard cache key format, for consistency
+ *
+ * @param string $slug Plugin slug
+ * @param string $branch dev|stable
+ * @param string $suffix Arbitrary cache key suffix, if needed for uniqueness
+ *
+ * @return string Cache key
+ */
+ function cache_key( $slug, $branch, $suffix = null ) {
+ // EG keys
+ // plugin:press-this:stable-readme:originals
+ // plugin:press-this:stable-readme:original:title
+ // plugin:press-this:stable-readme:fr:title
+ $key = "{$this->master_project}:{$slug}:{$branch}";
+ if ( !empty( $suffix ) ) {
+ $key .= ":{$suffix}";
+ }
+ return $key;
+ }
+
+ /**
+ * Cache getting, with proper global cache group
+ *
+ * @param string $slug Plugin slug
+ * @param string $branch dev|stable
+ * @param string $suffix Arbitrary cache key suffix, if needed for uniqueness
+ *
+ * @return bool|mixed As returned by wp_cache_set()
+ */
+ function cache_get( $slug, $branch, $suffix = null ) {
+ if ( ! self::$use_cache ) {
+ return false;
+ }
+
+ $key = $this->cache_key( $slug, $branch, $suffix );
+ return wp_cache_get( $key, $this->i18n_cache_group );
+ }
+
+ /**
+ * Cache setting, with proper global cache group
+ *
+ * @param string $slug Plugin slug
+ * @param string $branch dev|stable
+ * @param string $suffix Arbitrary cache key suffix, if needed for uniqueness
+ *
+ * @return bool As returned by wp_cache_set()
+ */
+ function cache_set( $slug, $branch, $content, $suffix = null ) {
+ if ( ! self::$set_cache ) {
+ return false;
+ }
+
+ $key = $this->cache_key( $slug, $branch, $suffix );
+ return wp_cache_set( $key, $content, $this->i18n_cache_group );
+ }
+
+ /**
+ * Gets a GlotPress branch ID
+ *
+ * @param string $slug Plugin slug
+ * @param string $branch dev|stable
+ *
+ * @return bool|int|mixed
+ */
+ function get_gp_branch_id( $slug, $branch ) {
+ $cache_suffix = "branch_id";
+
+ if ( false !== ( $branch_id = $this->cache_get( $slug, $branch, $cache_suffix ) ) ) {
+ return $branch_id;
+ }
+
+ $branch_id = $this->db->get_var( $this->db->prepare(
+ 'SELECT id FROM '.GLOTPRESS_TABLE_PREFIX.'_projects WHERE path = %s',
+ "wp-plugins/{$slug}/{$branch}"
+ ) );
+
+ if ( empty( $branch_id ) ) {
+ $branch_id = 0;
+ }
+
+ $this->cache_set( $slug, $branch, $branch_id, $cache_suffix );
+
+ return $branch_id;
+ }
+
+ /**
+ * Gets GlotPress "originals" based on passed parameters
+ *
+ * @param string $slug Plugin slug
+ * @param string $branch dev|stable
+ * @param string $key Unique key
+ * @param string $str String to match in GP
+ *
+ * @return array|bool|mixed|null
+ */
+ function get_gp_originals( $slug, $branch, $key, $str ) {
+ // Try to get a single original with the whole content first (title, etc), if passed, or get them all otherwise.
+ if ( !empty( $key ) && !empty( $str ) ) {
+ $originals = $this->search_gp_original( $slug, $branch, $key, $str );
+ if ( !empty( $originals ) ) {
+ return array( $originals );
+ }
+ // Do not cache this as originals, search_gp_original() does its own caching
+ }
+
+ $cache_suffix = 'originals';
+
+ if ( false !== ( $originals = $this->cache_get( $slug, $branch, $cache_suffix ) ) ) {
+ return $originals;
+ }
+
+ $branch_id = $this->get_gp_branch_id( $slug, $branch );
+
+ if ( empty( $branch_id ) ) {
+ return array();
+ }
+
+ $originals = $this->db->get_results( $this->db->prepare(
+ 'SELECT id, singular, comment FROM '.GLOTPRESS_TABLE_PREFIX.'_originals WHERE project_id = %d AND status = %s ORDER BY CHAR_LENGTH(singular) DESC',
+ $branch_id, '+active'
+ ) );
+
+ if ( empty( $originals ) ) {
+ $originals = array(); // still cache if empty, but as array, never false
+ }
+
+ $this->cache_set( $slug, $branch, $originals, $cache_suffix );
+
+ return $originals;
+ }
+
+ /**
+ * Get GlotPress translation set ID based on passed params
+ *
+ * @param string $slug Plugin slug
+ * @param string $branch dev|stable
+ * @param string $locale EG: fr
+ *
+ * @return bool|int|mixed
+ */
+ function get_gp_translation_set_id( $slug, $branch, $locale ) {
+ $cache_suffix = "{$locale}:translation_set_id";
+
+ if ( false !== ( $translation_set_id = $this->cache_get( $slug, $branch, $cache_suffix ) ) ) {
+ return $translation_set_id;
+ }
+
+ $branch_id = $this->get_gp_branch_id( $slug, $branch );
+
+ if ( empty( $branch_id ) ) {
+ return 0;
+ }
+
+ $translation_set_id = $this->db->get_var( $this->db->prepare(
+ 'SELECT id FROM '.GLOTPRESS_TABLE_PREFIX.'_translation_sets WHERE project_id = %d AND locale = %s',
+ $branch_id, $locale ) );
+
+ if ( empty( $translation_set_id ) ) {
+ // Don't give up yet. Might be given fr_FR, which actually exists as locale=fr in GP.
+ $translation_set_id = $this->db->get_var( $this->db->prepare(
+ 'SELECT id FROM '.GLOTPRESS_TABLE_PREFIX.'_translation_sets WHERE project_id = %d AND locale = %s',
+ $branch_id, preg_replace( '/^([^-]+)(-.+)?$/', '\1', $locale ) ) );
+ }
+
+ if ( empty( $translation_set_id ) ) {
+ $translation_set_id = 0;
+ }
+
+ $this->cache_set( $slug, $branch, $translation_set_id, $cache_suffix );
+
+ return $translation_set_id;
+ }
+
+ /**
+ * Searches GlotPress "originals" for the passed string
+ *
+ * @param string $slug Plugin slug
+ * @param string $branch dev|stable
+ * @param string $key Unique key
+ * @param string $str String to be searched for
+ *
+ * @return bool|mixed|null
+ */
+ function search_gp_original( $slug, $branch, $key, $str ) {
+ $cache_suffix = "original:{$key}";
+
+ if ( false !== ( $original = $this->cache_get( $slug, $branch, $cache_suffix ) ) ) {
+ return $original;
+ }
+
+ $branch_id = $this->get_gp_branch_id( $slug, $branch );
+
+ if ( empty( $branch_id ) ) {
+ return false;
+ }
+
+ $original = $this->db->get_row( $this->db->prepare(
+ 'SELECT id, singular, comment FROM '.GLOTPRESS_TABLE_PREFIX.'_originals WHERE project_id = %d AND status = %s AND singular = %s',
+ $branch_id, '+active', $str
+ ) );
+
+ if ( empty( $original ) ) {
+ $original = null;
+ }
+
+ $this->cache_set( $slug, $branch, $original, $cache_suffix );
+
+ return $original;
+ }
+
+ /**
+ * Somewhat emulated equivalent of __() for content translation drawn directly from the GlotPress DB
+ *
+ * @param string $key Unique key, used for caching
+ * @param string $content Content to be translated
+ * @param array $args Misc arguments, such as BBPress topic id (otherwise acquired from global $topic_id)
+ *
+ * @return mixed
+ */
+ function translate( $key, $content, $args = array() ) {
+ if ( empty( $key ) || empty( $content ) ) {
+ return $content;
+ }
+
+ if ( !empty( $args['post_id'] ) && is_numeric( $args['post_id'] ) ) {
+ $topic = get_post( $args['post_id'] );
+ } else {
+ global $post;
+ }
+
+ if ( empty( $post ) ) {
+ return $content;
+ }
+
+ if ( !empty( $args['locale'] ) ) {
+ $wp_locale = $args['locale'];
+ } else {
+ $wp_locale = get_locale();
+ }
+
+ $server_name = strtolower( $_SERVER['SERVER_NAME'] );
+ if ( 'api.wordpress.org' == $server_name ) {
+ // Support formats like fr, haz, and en_GB
+ if ( ! empty( $_REQUEST['locale'] ) ) {
+ $wp_locale = preg_replace( '/[^a-zA-Z_]/', '', $_REQUEST['locale'] );
+ } else if ( ! empty( $_REQUEST['request'] ) ) {
+ $request = maybe_unserialize( $_REQUEST['request'] );
+ if ( ! empty( $request ) && ! empty( $request->locale ) ) {
+ $wp_locale = preg_replace( '/[^a-zA-Z_]/', '', $request->locale );
+ }
+ }
+ }
+
+ if ( ! $wp_locale ) {
+ return $content;
+ }
+
+ require_once GLOTPRESS_LOCALES_PATH;
+ $gp_locale = \GP_Locales::by_field( 'wp_locale', $wp_locale );
+
+ if ( ! $gp_locale || 'en' === $gp_locale->slug ) {
+ return $content;
+ }
+
+ $locale = $gp_locale->slug; // The slug is the locale of a translation set.
+ $slug = $post->post_name;
+
+ $post->stable_tag = get_post_meta( $post->ID, 'stable_tag', true );
+
+ if ( empty( $slug ) ) {
+ return $content;
+ }
+
+ $branch = ( empty( $post->stable_tag ) || 'trunk' === $post->stable_tag ) ? 'dev' : 'stable';
+
+ if ( empty( $args['code_i18n'] ) || true !== $args['code_i18n'] ) {
+ $branch .= '-readme';
+ }
+
+ $cache_suffix = "{$locale}:{$key}";
+
+ // Try the cache
+ if ( false !== ( $cache = $this->cache_get( $slug, $branch, $cache_suffix ) ) ) {
+ // DEBUG
+ // var_dump( array( $slug, $branch, $cache_suffix, $cache ) );
+ return $cache;
+ }
+
+ $originals = $this->get_gp_originals( $slug, $branch, $key, $content );
+
+ if ( empty( $originals ) ) {
+ return $content;
+ }
+
+ $translation_set_id = $this->get_gp_translation_set_id( $slug, $branch, $locale );
+
+ if ( empty( $translation_set_id ) ) {
+ return $content;
+ }
+
+ foreach ( $originals as $original ) {
+ if ( empty( $original->id ) ) {
+ continue;
+ }
+
+ $translation = $this->db->get_var( $this->db->prepare(
+ 'SELECT translation_0 FROM '.GLOTPRESS_TABLE_PREFIX.'_translations WHERE original_id = %d AND translation_set_id = %d AND status = %s',
+ $original->id, $translation_set_id, 'current'
+ ) );
+
+ if ( empty( $translation ) ) {
+ continue;
+ }
+
+ $content = $this->translate_gp_original( $original->singular, $translation, $content );
+ }
+
+ $this->cache_set( $slug, $branch, $content, $cache_suffix );
+
+ return $content;
+ }
+
+ /**
+ * Takes content, searches for $original, and replaces it by $translation
+ *
+ * @param string $original English string
+ * @param string $translation Translation
+ * @param string $content Content to be searched
+ *
+ * @return mixed
+ */
+ function translate_gp_original( $original, $translation, $content ) {
+ if ( false === strpos( $content, '<' ) ) {
+ $content = str_replace( $original, $translation, $content );
+ } else {
+ $original = preg_quote( $original, '/' );
+ $content = preg_replace( "/(<([a-z0-9]*)\b[^>]*>){$original}(<\/\\2>)/m", "\\1{$translation}\\3", $content );
+ }
+
+ return $content;
+ }
+
+}
+
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-i18n.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>
</body>
</html>