Make WordPress Core

Ticket #36917: 36917.diff

File 36917.diff, 3.1 KB (added by pento, 7 years ago)
  • src/wp-includes/wp-db.php

     
    735735         */
    736736        public function init_charset() {
    737737                if ( function_exists('is_multisite') && is_multisite() ) {
    738                         $this->charset = 'utf8';
     738                        $charset = 'utf8';
    739739                        if ( defined( 'DB_COLLATE' ) && DB_COLLATE ) {
    740                                 $this->collate = DB_COLLATE;
     740                                $collate = DB_COLLATE;
    741741                        } else {
    742                                 $this->collate = 'utf8_general_ci';
     742                                $collate = 'utf8_general_ci';
    743743                        }
    744744                } elseif ( defined( 'DB_COLLATE' ) ) {
    745                         $this->collate = DB_COLLATE;
     745                        $collate = DB_COLLATE;
    746746                }
    747747
    748748                if ( defined( 'DB_CHARSET' ) ) {
    749                         $this->charset = DB_CHARSET;
     749                        $charset = DB_CHARSET;
    750750                }
    751751
     752                list( $this->charset, $this->collate ) = $this->determine_charset( $charset, $collate );
     753        }
     754
     755        /**
     756         * Given a charset and collation, determine the best charset and collation to use.
     757         *
     758         * For example, when able, utf8mb4 should be used instead of utf8.
     759         *
     760         * @since 4.6.0
     761         *
     762         * @param  string $charset The character set to check.
     763         * @param  string $collate The collation to check.
     764         */
     765        public function determine_charset( $charset, $collate ) {
    752766                if ( ( $this->use_mysqli && ! ( $this->dbh instanceof mysqli ) ) || empty( $this->dbh ) ) {
    753                         return;
     767                        return array( $charset, $collate );
    754768                }
    755769
    756                 if ( 'utf8' === $this->charset && $this->has_cap( 'utf8mb4' ) ) {
    757                         $this->charset = 'utf8mb4';
     770                if ( 'utf8' === $charset && $this->has_cap( 'utf8mb4' ) ) {
     771                        $charset = 'utf8mb4';
    758772                }
    759773
    760                 if ( 'utf8mb4' === $this->charset ) {
     774                if ( 'utf8mb4' === $charset ) {
    761775                        // _general_ is outdated, so we can upgrade it to _unicode_, instead.
    762                         if ( ! $this->collate || 'utf8_general_ci' === $this->collate ) {
    763                                 $this->collate = 'utf8mb4_unicode_ci';
     776                        if ( ! $collate || 'utf8_general_ci' === $collate ) {
     777                                $collate = 'utf8mb4_unicode_ci';
    764778                        } else {
    765                                 $this->collate = str_replace( 'utf8_', 'utf8mb4_', $this->collate );
     779                                $collate = str_replace( 'utf8_', 'utf8mb4_', $collate );
    766780                        }
    767781                }
    768782
    769783                // _unicode_520_ is a better collation, we should use that when it's available.
    770                 if ( $this->has_cap( 'utf8mb4_520' ) && 'utf8mb4_unicode_ci' === $this->collate ) {
    771                         $this->collate = 'utf8mb4_unicode_520_ci';
     784                if ( $this->has_cap( 'utf8mb4_520' ) && 'utf8mb4_unicode_ci' === $collate ) {
     785                        $collate = 'utf8mb4_unicode_520_ci';
    772786                }
     787
     788                return array( $charset, $collate );
    773789        }
    774790
    775791        /**
  • tests/phpunit/tests/db.php

     
    955955
    956956                $wpdb->check_connection();
    957957        }
     958
     959        /**
     960         * @ticket 36917
     961         */
     962        function test_charset_not_determined_when_disconnected() {
     963                global $wpdb;
     964
     965                $charset = 'utf8';
     966                $collate = 'this_isnt_a_collation';
     967
     968                $wpdb->close();
     969
     970                $result = $wpdb->determine_charset( $charset, $collate );
     971
     972                $this->assertSame( array( $charset, $collate ), $result );
     973
     974                $wpdb->check_connection();
     975        }
    958976}