WordPress.org

Making WordPress.org

Changeset 10688


Ignore:
Timestamp:
02/17/2021 06:05:39 AM (11 months ago)
Author:
dd32
Message:

Translate: Extend the GlotPress HTML warnings to allow certain URL domain changes.

This alows for <a href="http://wordpress.org/.."> to be translated to <a href="https://test.wordpress.org/.."> without generating a warning.

See #5155.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-custom-warnings/wporg-gp-custom-warnings.php

    r9794 r10688  
    9898    }
    9999
     100    /**
     101     * Extends the GlotPress tags warning to allow some URL changes.
     102     *
     103     * @param string    $original    The original string.
     104     * @param string    $translation The translated string.
     105     * @param GP_Locale $locale      The locale.
     106     */
     107    public function warning_tags( $original, $translation, $locale ) {
     108        // Allow URL changes in `href` attributes by substituting the original URL when appropriate
     109        // if that passes the checks, assume it's okay, otherwise throw the warning with the original payload.
     110
     111        $altered_translation = $translation;
     112        foreach ( $this->allowed_domain_changes as $domain => $regex ) {
     113            if ( false === stripos( $original, '://' . $domain ) ) {
     114                continue;
     115            }
     116
     117            // Make an assumption that the first protocol for the given domain is the protocol in use.
     118            $protocol = 'https';
     119            if ( preg_match( '!(https?)://' . $regex . '!', $original, $m ) ) {
     120                $protocol = $m[1];
     121            }
     122
     123            $altered_translation = preg_replace_callback(
     124                '!(href=[\'"]?)(https?)://(' . $regex . ')!i',
     125                function( $m ) use( $protocol, $domain ) {
     126                    return $m[1] . $protocol . '://' . $domain;
     127                },
     128                $altered_translation
     129            );
     130        }
     131
     132        if ( $altered_translation !== $translation ) {
     133            $altered_warning = GP::$builtin_translation_warnings->warning_tags( $original, $altered_translation, $locale );
     134            if ( true === $altered_warning ) {
     135                return true;
     136            }
     137        }
     138
     139        // Pass through to the core GlotPress warning method.
     140        return GP::$builtin_translation_warnings->warning_tags( $original, $translation, $locale );
     141    }
    100142
    101143    /**
Note: See TracChangeset for help on using the changeset viewer.