WordPress.org

Making WordPress.org

Changeset 4419


Ignore:
Timestamp:
11/25/2016 05:44:18 AM (3 years ago)
Author:
tellyworth
Message:

Plugin directory: replace on-the-fly virtual postmeta translations with a function to store translated content in real postmeta.

The old method no longer works with Jetpack sync; this should. The function is not yet called, that should be done async, or triggered by translation updates.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php

    r4418 r4419  
    1010 */
    1111class Plugin_Directory {
    12 
    13     /**
    14      * Local cache for translated content injected into meta.
    15      *
    16      * @access private
    17      *
    18      * @var array
    19      */
    20     private $i18n_meta = array();
    2112
    2213    /**
     
    334325        }
    335326
    336         // When Jetpack syncs, we want to add filters to inject additional metadata for Jetpack, so it syncs for ElasticSearch indexing.
    337         if ( defined( 'DOING_CRON' ) )
    338             $this->append_meta_for_jetpack();
    339         else
    340             add_action( 'shutdown', array( $this, 'append_meta_for_jetpack' ), 8 );
    341 
    342327    }
    343328
     
    500485     */
    501486    public function filter_rel_nofollow( $content ) {
    502         if ( get_post_type() == 'plugin' )
     487        if ( get_post_type() == 'plugin' ) {
    503488            // regex copied from wp_rel_nofollow(). Not calling that function because it messes with slashes.
    504489            $content = preg_replace_callback('|<a (.+?)>|i', 'wp_rel_nofollow_callback', $content);
     490        }
    505491        return $content;
    506492    }
     
    708694
    709695    /**
    710      * Shutdown action that will add a filter to inject additional postmeta containing translated content if Jetpack
    711      * is syncing.
    712      */
    713     public function append_meta_for_jetpack() {
    714 
    715         /*
    716          * Guess if a Jetpack sync is scheduled to run. It runs during shutdown at a lower priority than this action,
    717          * so we can get in first.Fetching the extra meta to inject is expensive, so we only want to do this if a sync
    718          * is likely.
    719          * As of Jetpack 4.4, sync can also run in a cron job, so filter the meta there too.
    720          */
    721         if ( class_exists( 'Jetpack' ) && ( defined( 'DOING_CRON' ) || ! empty( \Jetpack::$instance->sync->sync ) ) ) {
    722             // Attempt to work around the problem with options cache poisoning from subdomains
    723             refresh_blog_details();
    724             add_filter( 'wporg_plugins_custom_meta_fields', array( $this, 'filter_post_meta_i18n' ), 10, 2 );
    725         }
    726     }
    727 
    728     /**
    729      * Filter for wporg_plugins_custom_meta_fields to inject translated content for ES.
     696     * Fetch all translated content for a given post, and push it into postmeta.
    730697     *
    731698     * @global string $locale Current locale.
    732699     *
    733      * @param array $meta
    734      * @param int   $post_id
     700     * @param int   $post_id    Post ID to update.
     701     * @param int   $min_translated Translations below this % threshold will not be synced to meta, to save space.
    735702     * @return array
    736703     */
    737     public function filter_post_meta_i18n( $meta, $post_id ) {
    738 
    739         // Prevent recursion and repeat runs.
    740         remove_filter( 'wporg_plugins_custom_meta_fields', array( $this, 'filter_post_meta_i18n' ) );
    741 
    742         if ( empty( $this->i18n_meta[ $post_id ] ) ) {
    743 
    744             $locales_to_sync = array(
    745                 // Default locales to translate, just in case we can't determine the available ones
    746                 'fr_FR',
    747                 'es_ES',
    748             );
    749             $post = get_post( $post_id );
    750             if ( $post ) {
    751                 $translations = Plugin_I18n::find_all_translations_for_plugin( $post->post_name, 'stable-readme', 10 ); // at least 10% translated
    752                 if ( $translations )
    753                     $locales_to_sync = $translations;
     704    public function sync_all_translations_to_meta( $post_id, $min_translated = 60, $skip_pfx = array('en_') ) {
     705
     706        $locales_to_sync = array();
     707        $post = get_post( $post_id );
     708        if ( $post ) {
     709            $translations = Plugin_I18n::find_all_translations_for_plugin( $post->post_name, 'stable-readme', $min_translated ); // at least $min_translated % translated
     710            if ( $translations ) {
     711                // Eliminate translations that start with unwanted prefixes, so we don't waste space on near-duplicates like en_AU, en_CA etc.
     712                foreach ( $translations as $i => $_locale ) {
     713                    foreach ( $skip_pfx as $pfx )
     714                        if ( substr( $_locale, 0, strlen( $pfx ) ) === $pfx )
     715                            unset( $translations[ $i ] );
     716                }
     717                $locales_to_sync = $translations;
    754718            }
    755 
    756             global $locale;
    757             $_locale = $locale;
    758 
     719        }
     720
     721        if ( count($locales_to_sync) > 0 ) {
    759722            foreach ( $locales_to_sync as $locale ) {
    760                 $the_title = Plugin_I18n::instance()->translate( 'title', get_the_title( $post_id ), [ 'post_id' => $post_id ] );
    761                 if ( $the_title && $the_title != get_the_title( $post_id ) ) {
    762                     $this->i18n_meta[ $post_id ][ 'title_' . $locale ] = $the_title;
    763                 }
    764 
    765                 $the_excerpt = $this->translate_post_excerpt( get_the_excerpt( $post_id ), $post_id );
    766                 if ( $the_excerpt && $the_excerpt != get_the_excerpt( $post_id ) ) {
    767                     $this->i18n_meta[ $post_id ][ 'excerpt_' . $locale ] = $the_excerpt;
    768                 }
    769 
    770                 // Split up the content to translate it in sections.
    771                 $the_content = array();
    772                 $sections = $this->split_post_content_into_pages( get_the_content( $post_id ) );
    773                 foreach ( $sections as $section => $section_content ) {
    774                     $translated_section = $this->translate_post_content( $section_content, $section, $post_id );
    775                     if ( $translated_section && $translated_section != $section_content ) {
    776                         $the_content[] = $translated_section;
    777                     }
    778                 }
    779                 if ( !empty( $the_content ) )
    780                     $this->i18n_meta[ $post_id ][ 'content_' . $locale ] = implode( $the_content );
     723                $this->sync_translation_to_meta( $post_id, $locale );
    781724            }
    782 
    783             $locale = $_locale;
    784 
    785         }
    786 
    787         if ( is_array( $this->i18n_meta[ $post_id ] ) )
    788             $meta = array_merge( $meta, array_keys( $this->i18n_meta[ $post_id ] ) );
    789 
    790         add_filter( 'wporg_plugins_custom_meta_fields', array( $this, 'filter_post_meta_i18n' ), 10, 2 );
    791 
    792         return $meta;
     725        }
     726
     727        return $locales_to_sync;
     728    }
     729
     730    /**
     731     * Fetch translated content for a given post and locale, and push it into postmeta.
     732     *
     733     * @global string $locale Current locale.
     734     *
     735     * @param int   $post_id    Post ID to update.
     736     * @param string   $locale  Locale to translate.
     737     */
     738    public function sync_translation_to_meta( $post_id, $_locale ) {
     739        global $locale;
     740        $old_locale = $locale;
     741        $locale = $_locale;
     742
     743        // Update postmeta values for the translated title, excerpt, and content, if they are available and different from the originals.
     744        // There is a bug here, in that no attempt is made to remove old meta values for translations that do not have new translations.
     745
     746        $the_title = Plugin_I18n::instance()->translate( 'title', get_the_title( $post_id ), [ 'post_id' => $post_id ] );
     747        if ( $the_title && $the_title != get_the_title( $post_id ) ) {
     748            update_post_meta( $post_id, 'title_' . $locale, $the_title );
     749        }
     750
     751        $the_excerpt = $this->translate_post_excerpt( get_the_excerpt( $post_id ), $post_id );
     752        if ( $the_excerpt && $the_excerpt != get_the_excerpt( $post_id ) ) {
     753            update_post_meta( $post_id, 'excerpt_' . $locale, $the_excerpt );
     754        }
     755
     756        // Split up the content to translate it in sections.
     757        $the_content = array();
     758        $sections = $this->split_post_content_into_pages( get_the_content( $post_id ) );
     759        foreach ( $sections as $section => $section_content ) {
     760            $translated_section = $this->translate_post_content( $section_content, $section, $post_id );
     761            if ( $translated_section && $translated_section != $section_content ) {
     762                $the_content[] = $translated_section;
     763            }
     764        }
     765        if ( !empty( $the_content ) )
     766            update_post_meta( $post_id, 'content_' . $locale, implode( $the_content ) );
     767
     768        $locale = $old_locale;
    793769    }
    794770
Note: See TracChangeset for help on using the changeset viewer.