Make WordPress Core

Changeset 43629


Ignore:
Timestamp:
09/05/2018 11:01:36 AM (8 years ago)
Author:
flixos90
Message:

Upgrade/Install: Introduce populate_site_meta().

Fixes #44896. See #41333.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/schema.php

    r43628 r43629  
    12931293        $wpdb->query( "INSERT INTO $wpdb->sitemeta ( site_id, meta_key, meta_value ) VALUES " . $insert ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
    12941294}
     1295
     1296/**
     1297 * Creates WordPress site meta and sets the default values.
     1298 *
     1299 * @since 5.0.0
     1300 *
     1301 * @global wpdb $wpdb WordPress database abstraction object.
     1302 *
     1303 * @param int   $site_id Site ID to populate meta for.
     1304 * @param array $meta    Optional. Custom meta $key => $value pairs to use. Default empty array.
     1305 */
     1306function populate_site_meta( $site_id, array $meta = array() ) {
     1307        global $wpdb;
     1308
     1309        $site_id = (int) $site_id;
     1310
     1311        if ( ! is_site_meta_supported() ) {
     1312                return;
     1313        }
     1314
     1315        if ( empty( $meta ) ) {
     1316                return;
     1317        }
     1318
     1319        $insert = '';
     1320        foreach ( $meta as $meta_key => $meta_value ) {
     1321                if ( is_array( $meta_value ) ) {
     1322                        $meta_value = serialize( $meta_value );
     1323                }
     1324                if ( ! empty( $insert ) ) {
     1325                        $insert .= ', ';
     1326                }
     1327                $insert .= $wpdb->prepare( '( %d, %s, %s)', $site_id, $meta_key, $meta_value );
     1328        }
     1329
     1330        $wpdb->query( "INSERT INTO $wpdb->blogmeta ( blog_id, meta_key, meta_value ) VALUES " . $insert ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
     1331
     1332        wp_cache_set( 'last_changed', microtime(), 'sites' );
     1333}
  • trunk/tests/phpunit/tests/admin/includesSchema.php

    r43628 r43629  
    88
    99        private static $options;
     10        private static $blogmeta;
    1011        private static $sitemeta;
    1112
     
    1718
    1819                self::$options  = 'testprefix_options';
     20                self::$blogmeta = 'testprefix_blogmeta';
    1921                self::$sitemeta = 'testprefix_sitemeta';
    2022
    2123                $options  = self::$options;
     24                $blogmeta = self::$blogmeta;
    2225                $sitemeta = self::$sitemeta;
    2326
     
    3639                                PRIMARY KEY  (option_id),
    3740                                UNIQUE KEY option_name (option_name)
     41                        ) {$charset_collate}
     42                        "
     43                );
     44                $wpdb->query(
     45                        "
     46                        CREATE TABLE {$blogmeta} (
     47                                meta_id bigint(20) unsigned NOT NULL auto_increment,
     48                                blog_id bigint(20) unsigned NOT NULL default '0',
     49                                meta_key varchar(255) default NULL,
     50                                meta_value longtext,
     51                                PRIMARY KEY  (meta_id),
     52                                KEY meta_key (meta_key({$max_index_length})),
     53                                KEY blog_id (blog_id)
    3854                        ) {$charset_collate}
    3955                        "
     
    6177
    6278                $options  = self::$options;
     79                $blogmeta = self::$blogmeta;
    6380                $sitemeta = self::$sitemeta;
    6481
    6582                $wpdb->query( "DROP TABLE IF EXISTS {$options}" );
     83                $wpdb->query( "DROP TABLE IF EXISTS {$blogmeta}" );
    6684                $wpdb->query( "DROP TABLE IF EXISTS {$sitemeta}" );
    6785        }
     
    158176
    159177        /**
     178         * @ticket 44896
     179         * @group multisite
     180         * @group ms-required
     181         * @dataProvider data_populate_site_meta
     182         */
     183        function test_populate_site_meta( $meta, $expected ) {
     184                global $wpdb;
     185
     186                $orig_blogmeta  = $wpdb->blogmeta;
     187                $wpdb->blogmeta = self::$blogmeta;
     188
     189                populate_site_meta( 42, $meta );
     190
     191                $results = array();
     192                foreach ( $expected as $meta_key => $value ) {
     193                        $results[ $meta_key ] = get_site_meta( 42, $meta_key, true );
     194                }
     195
     196                $wpdb->query( "TRUNCATE TABLE {$wpdb->blogmeta}" );
     197
     198                $wpdb->blogmeta = $orig_blogmeta;
     199
     200                $this->assertEquals( $expected, $results );
     201        }
     202
     203        public function data_populate_site_meta() {
     204                return array(
     205                        array(
     206                                array(),
     207                                array(
     208                                        'unknown_value' => '',
     209                                ),
     210                        ),
     211                        array(
     212                                array(
     213                                        'custom_meta' => '1',
     214                                ),
     215                                array(
     216                                        'custom_meta' => '1',
     217                                ),
     218                        ),
     219                );
     220        }
     221
     222        /**
    160223         * @ticket 44895
     224         * @group multisite
    161225         * @dataProvider data_populate_network_meta
    162226         */
Note: See TracChangeset for help on using the changeset viewer.