Making WordPress.org

Changeset 3449


Ignore:
Timestamp:
06/19/2016 08:46:20 AM (10 years ago)
Author:
tellyworth
Message:

Inject translated content, title, and excerpt into pseudo-meta values for search indexing.

This is intentionally limited for testing purposes to fr and es locales, and to post IDs below 200.

See #1692.

File:
1 edited

Legend:

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

    r3422 r3449  
    1010 */
    1111class Plugin_Directory {
     12
     13        /**
     14         * Local cache for translated content injected into meta
     15         */
     16        private $i18n_meta = array();
    1217
    1318        /**
    … …  
    238243                        \Jetpack_Search::instance();
    239244                }
     245
     246                // When Jetpack syncs, we want to add filters to inject additional metadata for Jetpack, so it syncs for ElasticSearch indexing.
     247                add_action( 'shutdown', array( $this, 'append_meta_for_jetpack' ), 8 );
     248
    240249        }
    241250
    … …  
    516525
    517526        /**
     527         * Shutdown action that will add a filter to inject additional postmeta containing translated content if Jetpack is syncing.
     528         *
     529         */
     530        public function append_meta_for_jetpack() {
     531                // TEMP: only do this for low numbered plugin IDs, till we're sure it works.
     532                if ( get_post()->ID > 200 )
     533                        return;
     534
     535                // Guess if a Jetpack sync is scheduled to run. It runs during shutdown at a lower priority than this action, so we can get in first.
     536                // Fetching the extra meta to inject is expensive, so we only want to do this if a sync is likely.
     537                if ( class_exists( 'Jetpack' ) && !empty(\Jetpack::init()->sync->sync) ) {
     538                        add_filter( 'wporg_plugins_custom_meta_fields', array( $this, 'filter_post_meta_i18n' ), 10, 2 );
     539                }
     540
     541        }
     542
     543        /**
     544         * Filter for wporg_plugins_custom_meta_fields to inject translated content for ES.
     545         *
     546         * @param array $meta
     547         * @param int $post_id
     548         * @return array
     549         */
     550        public function filter_post_meta_i18n( $meta, $post_id ) {
     551                // Prevent recursion and repeat runs
     552                remove_filter( 'wporg_plugins_custom_meta_fields', array( $this, 'filter_post_meta_i18n' ) );
     553
     554                if ( get_post()->ID == $post_id ) {
     555                        $locales_to_sync = array( 'fr_fr', 'es_es' ); // This should probably be a list of available translations for the plugin readme.
     556
     557                        global $locale;
     558                        $_locale = $locale;
     559                        foreach ( $locales_to_sync as $locale ) {
     560                                $this->i18n_meta[$post_id]['title_'.$locale] = $this->translate_post_title( get_the_title(), $post_id );
     561                                $this->i18n_meta[$post_id]['excerpt_'.$locale] = $this->translate_post_excerpt( get_the_excerpt() );
     562
     563                                // Split up the content to translate it in sections
     564                                $content = '';
     565                                $sections = $this->split_post_content_into_pages( get_the_content() );
     566                                foreach ( $sections as $section => $section_content )
     567                                        $content .= $this->translate_post_content( $section_content, $section );
     568                                $this->i18n_meta[$post_id]['content_'.$locale] = $content;
     569
     570                        }
     571
     572                        $locale = $_locale;
     573
     574                        $meta = array_merge( $meta, array_keys( $this->i18n_meta[$post_id] ) );
     575                }
     576
     577                add_filter( 'wporg_plugins_custom_meta_fields', array( $this, 'filter_post_meta_i18n'), 10, 2 );
     578                return $meta;
     579        }
     580
     581
     582        /**
    518583         * Filter for rest_api_allowed_post_types to enable JP syncing of the CPT
    519584         *
    … …  
    662727                                }
    663728
     729                                break;
     730                        default:
     731                                if ( isset( $this->i18n_meta[ $object_id ][ $meta_key ] ) )
     732                                        return array( $this->i18n_meta[ $object_id ][ $meta_key ] );
    664733                                break;
    665734                }
Note: See TracChangeset for help on using the changeset viewer.