Making WordPress.org

Changeset 4328


Ignore:
Timestamp:
11/04/2016 11:53:08 AM (8 years ago)
Author:
ocean90
Message:

WP i18n teams: Use the term_link filter for linking #locale to the team page.

Avoids yet another content parsing which is already done by o2.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wp-i18n-teams/wp-i18n-teams.php

    r4249 r4328  
    1111
    1212class WP_I18n_Teams {
     13    const TEAM_PAGE = 'https://make.wordpress.org/polyglots/teams/?locale=%s';
     14
    1315    public function __construct() {
    1416        add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
     
    1820     * Attaches hooks and registers shortcodes once plugins are loasded.
    1921     */
    20     function plugins_loaded() {
     22    public function plugins_loaded() {
    2123        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
    2224        add_shortcode( 'wp-locales',      array( $this, 'wp_locales' ) );
    2325
    24         add_filter( 'the_content',  array( $this, 'link_locales' ), 0 );
    25         add_filter( 'comment_text', array( $this, 'link_locales' ), 0 );
     26        add_filter( 'term_link', array( $this, 'link_locales' ), 10, 3 );
    2627    }
    2728
     
    2930     * Links #locale to the teams page.
    3031     *
    31      * @param string $content Post or comment content.
    32      * @return string Filtered post or comment content.
    33      */
    34     function link_locales( $content ) {
     32     * @param string $termlink Term link URL.
     33     * @param object $term     Term object.
     34     * @param string $taxonomy Taxonomy slug.
     35     * @return string URL to teams page of a locale.
     36     */
     37    public function link_locales( $termlink, $term, $taxonomy ) {
     38        if ( 'post_tag' !== $taxonomy ) {
     39            return $termlink;
     40        }
     41
    3542        static $available_locales;
    3643
     
    3845            $available_locales = self::get_locales();
    3946            $available_locales = wp_list_pluck( $available_locales, 'wp_locale' );
    40         }
    41 
    42         $regex = '/\B#([\w]+)\b/';
    43         if ( ! preg_match_all( $regex, $content, $matches ) ) {
    44             return $content;
    45         }
    46 
    47         $possible_locales = $matches[1];
    48         $possible_locales = array_unique( $possible_locales );
    49         $locales = array_intersect( $available_locales, $possible_locales );
    50 
    51         foreach ( $locales as $locale ) {
    52             $url         = esc_url( 'https://make.wordpress.org/polyglots/teams/?locale=' . $locale );
    53             $replacement = "<a href='$url'>#$locale</a>";
    54             $content     = preg_replace( "/#$locale\b/", $replacement, $content );
    55         }
    56 
    57         return $content;
     47            $available_locales = array_flip( $available_locales );
     48        }
     49
     50        if ( isset( $available_locales[ $term->name ] ) || isset( $available_locales[ $term->slug ] ) ) {
     51            return sprintf( self::TEAM_PAGE, $term->slug );
     52        }
     53
     54        return $termlink;
    5855    }
    5956
Note: See TracChangeset for help on using the changeset viewer.