Ticket #44895: 44895.diff
File 44895.diff, 16.6 KB (added by , 6 years ago) |
---|
-
src/wp-admin/includes/schema.php
383 383 } 384 384 385 385 // If WP_DEFAULT_THEME doesn't exist, fall back to the latest core default theme. 386 $stylesheet = $template = WP_DEFAULT_THEME; 386 $stylesheet = WP_DEFAULT_THEME; 387 $template = WP_DEFAULT_THEME; 387 388 $theme = wp_get_theme( WP_DEFAULT_THEME ); 388 389 if ( ! $theme->exists() ) { 389 390 $theme = WP_Theme::get_core_default_theme(); … … 402 403 * or a valid timezone string (America/New_York). See https://secure.php.net/manual/en/timezones.php 403 404 * for all timezone strings supported by PHP. 404 405 */ 405 $offset_or_tz = _x( '0', 'default GMT offset or timezone string' ); 406 $offset_or_tz = _x( '0', 'default GMT offset or timezone string' ); // phpcs:ignore WordPress.WP.I18n.NoEmptyStrings 406 407 if ( is_numeric( $offset_or_tz ) ) { 407 408 $gmt_offset = $offset_or_tz; 408 409 } elseif ( $offset_or_tz && in_array( $offset_or_tz, timezone_identifiers_list() ) ) { … … 562 563 $fat_options = array( 'moderation_keys', 'recently_edited', 'blacklist_keys', 'uninstall_plugins' ); 563 564 564 565 $keys = "'" . implode( "', '", array_keys( $options ) ) . "'"; 565 $existing_options = $wpdb->get_col( "SELECT option_name FROM $wpdb->options WHERE option_name in ( $keys )" ); 566 $existing_options = $wpdb->get_col( "SELECT option_name FROM $wpdb->options WHERE option_name in ( $keys )" ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared 566 567 567 568 $insert = ''; 568 569 foreach ( $options as $option => $value ) { … … 585 586 } 586 587 587 588 if ( ! empty( $insert ) ) { 588 $wpdb->query( "INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES " . $insert ); 589 $wpdb->query( "INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES " . $insert ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared 589 590 } 590 591 591 592 // In case it is set, but blank, update "home". … … 960 961 * 961 962 * @global wpdb $wpdb 962 963 * @global object $current_site 963 * @global int $wp_db_version964 964 * @global WP_Rewrite $wp_rewrite 965 965 * 966 966 * @param int $network_id ID of network to populate. … … 974 974 * so the error code must be checked) or failure. 975 975 */ 976 976 function populate_network( $network_id = 1, $domain = '', $email = '', $site_name = '', $path = '/', $subdomain_install = false ) { 977 global $wpdb, $current_site, $wp_ db_version, $wp_rewrite;977 global $wpdb, $current_site, $wp_rewrite; 978 978 979 979 $errors = new WP_Error(); 980 980 if ( '' == $domain ) { … … 1004 1004 return $errors; 1005 1005 } 1006 1006 1007 if ( 1 == $network_id ) { 1008 $wpdb->insert( 1009 $wpdb->site, 1010 array( 1011 'domain' => $domain, 1012 'path' => $path, 1013 ) 1014 ); 1015 $network_id = $wpdb->insert_id; 1016 } else { 1017 $wpdb->insert( 1018 $wpdb->site, 1019 array( 1020 'domain' => $domain, 1021 'path' => $path, 1022 'id' => $network_id, 1023 ) 1024 ); 1025 } 1026 1027 populate_network_meta( 1028 $network_id, 1029 array( 1030 'admin_email' => $email, 1031 'site_name' => $site_name, 1032 'subdomain_install' => $subdomain_install, 1033 ) 1034 ); 1035 1036 $site_user = get_userdata( (int) $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", 'admin_user_id', $network_id ) ) ); 1037 1038 /* 1039 * When upgrading from single to multisite, assume the current site will 1040 * become the main site of the network. When using populate_network() 1041 * to create another network in an existing multisite environment, skip 1042 * these steps since the main site of the new network has not yet been 1043 * created. 1044 */ 1045 if ( ! is_multisite() ) { 1046 $current_site = new stdClass; 1047 $current_site->domain = $domain; 1048 $current_site->path = $path; 1049 $current_site->site_name = ucfirst( $domain ); 1050 $wpdb->insert( 1051 $wpdb->blogs, 1052 array( 1053 'site_id' => $network_id, 1054 'blog_id' => 1, 1055 'domain' => $domain, 1056 'path' => $path, 1057 'registered' => current_time( 'mysql' ), 1058 ) 1059 ); 1060 $current_site->blog_id = $wpdb->insert_id; 1061 update_user_meta( $site_user->ID, 'source_domain', $domain ); 1062 update_user_meta( $site_user->ID, 'primary_blog', $current_site->blog_id ); 1063 1064 if ( $subdomain_install ) { 1065 $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); 1066 } else { 1067 $wp_rewrite->set_permalink_structure( '/blog/%year%/%monthnum%/%day%/%postname%/' ); 1068 } 1069 1070 flush_rewrite_rules(); 1071 1072 if ( ! $subdomain_install ) { 1073 return true; 1074 } 1075 1076 $vhost_ok = false; 1077 $errstr = ''; 1078 $hostname = substr( md5( time() ), 0, 6 ) . '.' . $domain; // Very random hostname! 1079 $page = wp_remote_get( 1080 'http://' . $hostname, 1081 array( 1082 'timeout' => 5, 1083 'httpversion' => '1.1', 1084 ) 1085 ); 1086 if ( is_wp_error( $page ) ) { 1087 $errstr = $page->get_error_message(); 1088 } elseif ( 200 == wp_remote_retrieve_response_code( $page ) ) { 1089 $vhost_ok = true; 1090 } 1091 1092 if ( ! $vhost_ok ) { 1093 $msg = '<p><strong>' . __( 'Warning! Wildcard DNS may not be configured correctly!' ) . '</strong></p>'; 1094 1095 $msg .= '<p>' . sprintf( 1096 /* translators: %s: host name */ 1097 __( 'The installer attempted to contact a random hostname (%s) on your domain.' ), 1098 '<code>' . $hostname . '</code>' 1099 ); 1100 if ( ! empty( $errstr ) ) { 1101 /* translators: %s: error message */ 1102 $msg .= ' ' . sprintf( __( 'This resulted in an error message: %s' ), '<code>' . $errstr . '</code>' ); 1103 } 1104 $msg .= '</p>'; 1105 1106 $msg .= '<p>' . sprintf( 1107 /* translators: %s: asterisk symbol (*) */ 1108 __( 'To use a subdomain configuration, you must have a wildcard entry in your DNS. This usually means adding a %s hostname record pointing at your web server in your DNS configuration tool.' ), 1109 '<code>*</code>' 1110 ) . '</p>'; 1111 1112 $msg .= '<p>' . __( 'You can still use your site but any subdomain you create may not be accessible. If you know your DNS is correct, ignore this message.' ) . '</p>'; 1113 1114 return new WP_Error( 'no_wildcard_dns', $msg ); 1115 } 1116 } 1117 1118 return true; 1119 } 1120 1121 /** 1122 * Creates WordPress network meta and sets the default values. 1123 * 1124 * @since 5.0.0 1125 * 1126 * @global wpdb $wpdb WordPress database abstraction object. 1127 * @global int $wp_db_version WordPress database version. 1128 * 1129 * @param int $network_id Network ID to populate meta for. 1130 * @param array $meta Optional. Custom meta $key => $value pairs to use. Default empty array. 1131 */ 1132 function populate_network_meta( $network_id, array $meta = array() ) { 1133 global $wpdb, $wp_db_version; 1134 1135 $network_id = (int) $network_id; 1136 1137 $email = ! empty( $meta['admin_email'] ) ? $meta['admin_email'] : ''; 1138 $subdomain_install = isset( $meta['subdomain_install'] ) ? (int) $meta['subdomain_install'] : 0; 1139 1007 1140 // If a user with the provided email does not exist, default to the current user as the new network admin. 1008 $site_user = get_user_by( 'email', $email );1141 $site_user = ! empty( $email ) ? get_user_by( 'email', $email ) : false; 1009 1142 if ( false === $site_user ) { 1010 1143 $site_user = wp_get_current_user(); 1011 1144 } 1012 1145 1013 // Set up site tables. 1146 if ( empty( $email ) ) { 1147 $email = $site_user->user_email; 1148 } 1149 1014 1150 $template = get_option( 'template' ); 1015 1151 $stylesheet = get_option( 'stylesheet' ); 1016 1152 $allowed_themes = array( $stylesheet => true ); … … 1025 1161 1026 1162 // If WP_DEFAULT_THEME doesn't exist, also whitelist the latest core default theme. 1027 1163 if ( ! wp_get_theme( WP_DEFAULT_THEME )->exists() ) { 1028 if ( $core_default = WP_Theme::get_core_default_theme() ) { 1164 $core_default = WP_Theme::get_core_default_theme(); 1165 if ( $core_default ) { 1029 1166 $allowed_themes[ $core_default->get_stylesheet() ] = true; 1030 1167 } 1031 1168 } 1032 1169 1033 if ( 1 == $network_id ) {1034 $wpdb->insert(1035 $wpdb->site,1036 array(1037 'domain' => $domain,1038 'path' => $path,1039 )1040 );1041 $network_id = $wpdb->insert_id;1042 } else {1043 $wpdb->insert(1044 $wpdb->site,1045 array(1046 'domain' => $domain,1047 'path' => $path,1048 'id' => $network_id,1049 )1050 );1051 }1052 1053 1170 wp_cache_delete( 'networks_have_paths', 'site-options' ); 1054 1171 1055 1172 if ( ! is_multisite() ) { … … 1089 1206 --The Team @ SITE_NAME' 1090 1207 ); 1091 1208 1092 $misc_exts 1209 $misc_exts = array( 1093 1210 // Images. 1094 1211 'jpg', 1095 1212 'jpeg', … … 1122 1239 $upload_filetypes = array_unique( array_merge( $misc_exts, $audio_exts, $video_exts ) ); 1123 1240 1124 1241 $sitemeta = array( 1125 'site_name' => $site_name,1242 'site_name' => __( 'My Network' ), 1126 1243 'admin_email' => $email, 1127 1244 'admin_user_id' => $site_user->ID, 1128 1245 'registration' => 'none', … … 1140 1257 'siteurl' => get_option( 'siteurl' ) . '/', 1141 1258 'add_new_users' => '0', 1142 1259 'upload_space_check_disabled' => is_multisite() ? get_site_option( 'upload_space_check_disabled' ) : '1', 1143 'subdomain_install' => intval( $subdomain_install ),1260 'subdomain_install' => $subdomain_install, 1144 1261 'global_terms_enabled' => global_terms_enabled() ? '1' : '0', 1145 1262 'ms_files_rewriting' => is_multisite() ? get_site_option( 'ms_files_rewriting' ) : '0', 1146 1263 'initial_db_version' => get_option( 'initial_db_version' ), … … 1151 1268 $sitemeta['illegal_names'][] = 'blog'; 1152 1269 } 1153 1270 1271 $sitemeta = wp_parse_args( $meta, $sitemeta ); 1272 1154 1273 /** 1155 1274 * Filters meta for a network on creation. 1156 1275 * … … 1171 1290 } 1172 1291 $insert .= $wpdb->prepare( '( %d, %s, %s)', $network_id, $meta_key, $meta_value ); 1173 1292 } 1174 $wpdb->query( "INSERT INTO $wpdb->sitemeta ( site_id, meta_key, meta_value ) VALUES " . $insert ); 1175 1176 /* 1177 * When upgrading from single to multisite, assume the current site will 1178 * become the main site of the network. When using populate_network() 1179 * to create another network in an existing multisite environment, skip 1180 * these steps since the main site of the new network has not yet been 1181 * created. 1182 */ 1183 if ( ! is_multisite() ) { 1184 $current_site = new stdClass; 1185 $current_site->domain = $domain; 1186 $current_site->path = $path; 1187 $current_site->site_name = ucfirst( $domain ); 1188 $wpdb->insert( 1189 $wpdb->blogs, 1190 array( 1191 'site_id' => $network_id, 1192 'blog_id' => 1, 1193 'domain' => $domain, 1194 'path' => $path, 1195 'registered' => current_time( 'mysql' ), 1196 ) 1197 ); 1198 $current_site->blog_id = $blog_id = $wpdb->insert_id; 1199 update_user_meta( $site_user->ID, 'source_domain', $domain ); 1200 update_user_meta( $site_user->ID, 'primary_blog', $blog_id ); 1201 1202 if ( $subdomain_install ) { 1203 $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); 1204 } else { 1205 $wp_rewrite->set_permalink_structure( '/blog/%year%/%monthnum%/%day%/%postname%/' ); 1206 } 1207 1208 flush_rewrite_rules(); 1209 1210 if ( ! $subdomain_install ) { 1211 return true; 1212 } 1213 1214 $vhost_ok = false; 1215 $errstr = ''; 1216 $hostname = substr( md5( time() ), 0, 6 ) . '.' . $domain; // Very random hostname! 1217 $page = wp_remote_get( 1218 'http://' . $hostname, 1219 array( 1220 'timeout' => 5, 1221 'httpversion' => '1.1', 1222 ) 1223 ); 1224 if ( is_wp_error( $page ) ) { 1225 $errstr = $page->get_error_message(); 1226 } elseif ( 200 == wp_remote_retrieve_response_code( $page ) ) { 1227 $vhost_ok = true; 1228 } 1229 1230 if ( ! $vhost_ok ) { 1231 $msg = '<p><strong>' . __( 'Warning! Wildcard DNS may not be configured correctly!' ) . '</strong></p>'; 1232 1233 $msg .= '<p>' . sprintf( 1234 /* translators: %s: host name */ 1235 __( 'The installer attempted to contact a random hostname (%s) on your domain.' ), 1236 '<code>' . $hostname . '</code>' 1237 ); 1238 if ( ! empty( $errstr ) ) { 1239 /* translators: %s: error message */ 1240 $msg .= ' ' . sprintf( __( 'This resulted in an error message: %s' ), '<code>' . $errstr . '</code>' ); 1241 } 1242 $msg .= '</p>'; 1243 1244 $msg .= '<p>' . sprintf( 1245 /* translators: %s: asterisk symbol (*) */ 1246 __( 'To use a subdomain configuration, you must have a wildcard entry in your DNS. This usually means adding a %s hostname record pointing at your web server in your DNS configuration tool.' ), 1247 '<code>*</code>' 1248 ) . '</p>'; 1249 1250 $msg .= '<p>' . __( 'You can still use your site but any subdomain you create may not be accessible. If you know your DNS is correct, ignore this message.' ) . '</p>'; 1251 1252 return new WP_Error( 'no_wildcard_dns', $msg ); 1253 } 1254 } 1255 1256 return true; 1293 $wpdb->query( "INSERT INTO $wpdb->sitemeta ( site_id, meta_key, meta_value ) VALUES " . $insert ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared 1257 1294 } -
tests/phpunit/tests/admin/includesSchema.php
1 1 <?php 2 // phpcs:ignoreFile WordPress.WP.PreparedSQL.NotPrepared 2 3 3 4 /** 4 5 * @group admin … … 6 7 class Tests_Admin_Includes_Schema extends WP_UnitTestCase { 7 8 8 9 private static $options; 10 private static $sitemeta; 9 11 10 12 /** 11 13 * Make sure the schema code is loaded before the tests are run. … … 14 16 global $wpdb; 15 17 16 18 self::$options = 'testprefix_options'; 19 self::$sitemeta = 'testprefix_sitemeta'; 17 20 18 $options = self::$options; 21 $options = self::$options; 22 $sitemeta = self::$sitemeta; 19 23 20 24 require_once( ABSPATH . 'wp-admin/includes/schema.php' ); 21 25 … … 34 38 ) {$charset_collate} 35 39 " 36 40 ); 41 $wpdb->query( 42 " 43 CREATE TABLE {$sitemeta} ( 44 meta_id bigint(20) unsigned NOT NULL auto_increment, 45 site_id bigint(20) unsigned NOT NULL default '0', 46 meta_key varchar(255) default NULL, 47 meta_value longtext, 48 PRIMARY KEY (meta_id), 49 KEY meta_key (meta_key({$max_index_length})), 50 KEY site_id (site_id) 51 ) {$charset_collate} 52 " 53 ); 37 54 } 38 55 39 56 /** … … 42 59 public static function wpTearDownAfterClass() { 43 60 global $wpdb; 44 61 45 $options = self::$options; 62 $options = self::$options; 63 $sitemeta = self::$sitemeta; 46 64 47 65 $wpdb->query( "DROP TABLE IF EXISTS {$options}" ); 66 $wpdb->query( "DROP TABLE IF EXISTS {$sitemeta}" ); 48 67 } 49 68 50 69 /** … … 54 73 function test_populate_options( $options, $expected ) { 55 74 global $wpdb; 56 75 57 remove_all_filters( 'option_admin_email' );58 remove_all_filters( 'pre_option_admin_email' );59 remove_all_filters( 'default_option_admin_email' );60 61 76 $orig_options = $wpdb->options; 62 77 $wpdb->options = self::$options; 63 78 … … 92 107 ), 93 108 array( 94 109 array( 95 'posts_per_rss' 96 'rss_use_excerpt' 110 'posts_per_rss' => 7, 111 'rss_use_excerpt' => 1, 97 112 ), 98 113 array( 99 114 // Random options to check. … … 129 144 ), 130 145 array( 131 146 array( 132 'rss_0123456789abcdef0123456789abcdef' 147 'rss_0123456789abcdef0123456789abcdef' => '1', 133 148 'rss_0123456789abcdef0123456789abcdef_ts' => '1', 134 149 ), 135 150 array( 136 151 // These options would be obsolete magpie cache data and should never exist. 137 'rss_0123456789abcdef0123456789abcdef' 152 'rss_0123456789abcdef0123456789abcdef' => false, 138 153 'rss_0123456789abcdef0123456789abcdef_ts' => false, 139 154 ), 140 155 ), 141 156 ); 142 157 } 158 159 /** 160 * @ticket 44895 161 * @dataProvider data_populate_network_meta 162 */ 163 function test_populate_network_meta( $meta, $expected ) { 164 global $wpdb; 165 166 $orig_sitemeta = $wpdb->sitemeta; 167 $wpdb->sitemeta = self::$sitemeta; 168 169 populate_network_meta( 42, $meta ); 170 171 $results = array(); 172 foreach ( $expected as $meta_key => $value ) { 173 if ( is_multisite() ) { 174 $results[ $meta_key ] = get_network_option( 42, $meta_key ); 175 } else { 176 $results[ $meta_key ] = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $meta_key, 42 ) ); 177 } 178 } 179 180 $wpdb->query( "TRUNCATE TABLE {$wpdb->sitemeta}" ); 181 182 $wpdb->sitemeta = $orig_sitemeta; 183 184 $this->assertEquals( $expected, $results ); 185 } 186 187 public function data_populate_network_meta() { 188 return array( 189 array( 190 array(), 191 array( 192 // Random meta to check. 193 'registration' => 'none', 194 'blog_upload_space' => 100, 195 'fileupload_maxk' => 1500, 196 ), 197 ), 198 array( 199 array( 200 'site_name' => 'My Great Network', 201 'WPLANG' => 'fr_FR', 202 ), 203 array( 204 // Random meta to check. 205 'site_name' => 'My Great Network', 206 'registration' => 'none', 207 'blog_upload_space' => 100, 208 'fileupload_maxk' => 1500, 209 'WPLANG' => 'fr_FR', 210 ), 211 ), 212 array( 213 array( 214 'custom_meta' => '1', 215 ), 216 array( 217 // Random meta to check. 218 'custom_meta' => '1', 219 'registration' => 'none', 220 'blog_upload_space' => 100, 221 'fileupload_maxk' => 1500, 222 ), 223 ), 224 ); 225 } 143 226 }