wp/wp-includes/ms-site.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
   112 		// WPLANG was passed with `$meta` to the `wpmu_new_blog` hook prior to 5.1.0.
   112 		// WPLANG was passed with `$meta` to the `wpmu_new_blog` hook prior to 5.1.0.
   113 		if ( ! array_key_exists( 'WPLANG', $meta ) ) {
   113 		if ( ! array_key_exists( 'WPLANG', $meta ) ) {
   114 			$meta['WPLANG'] = get_network_option( $new_site->network_id, 'WPLANG' );
   114 			$meta['WPLANG'] = get_network_option( $new_site->network_id, 'WPLANG' );
   115 		}
   115 		}
   116 
   116 
   117 		// Rebuild the data expected by the `wpmu_new_blog` hook prior to 5.1.0 using allowed keys.
   117 		/*
   118 		// The `$allowed_data_fields` matches the one used in `wpmu_create_blog()`.
   118 		 * Rebuild the data expected by the `wpmu_new_blog` hook prior to 5.1.0 using allowed keys.
       
   119 		 * The `$allowed_data_fields` matches the one used in `wpmu_create_blog()`.
       
   120 		 */
   119 		$allowed_data_fields = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
   121 		$allowed_data_fields = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
   120 		$meta                = array_merge( array_intersect_key( $data, array_flip( $allowed_data_fields ) ), $meta );
   122 		$meta                = array_merge( array_intersect_key( $data, array_flip( $allowed_data_fields ) ), $meta );
   121 
   123 
   122 		/**
   124 		/**
   123 		 * Fires immediately after a new site is created.
   125 		 * Fires immediately after a new site is created.
   337 /**
   339 /**
   338  * Adds any sites from the given IDs to the cache that do not already exist in cache.
   340  * Adds any sites from the given IDs to the cache that do not already exist in cache.
   339  *
   341  *
   340  * @since 4.6.0
   342  * @since 4.6.0
   341  * @since 5.1.0 Introduced the `$update_meta_cache` parameter.
   343  * @since 5.1.0 Introduced the `$update_meta_cache` parameter.
   342  * @access private
   344  * @since 6.1.0 This function is no longer marked as "private".
       
   345  * @since 6.3.0 Use wp_lazyload_site_meta() for lazy-loading of site meta.
   343  *
   346  *
   344  * @see update_site_cache()
   347  * @see update_site_cache()
   345  * @global wpdb $wpdb WordPress database abstraction object.
   348  * @global wpdb $wpdb WordPress database abstraction object.
   346  *
   349  *
   347  * @param array $ids               ID list.
   350  * @param array $ids               ID list.
   352 
   355 
   353 	$non_cached_ids = _get_non_cached_ids( $ids, 'sites' );
   356 	$non_cached_ids = _get_non_cached_ids( $ids, 'sites' );
   354 	if ( ! empty( $non_cached_ids ) ) {
   357 	if ( ! empty( $non_cached_ids ) ) {
   355 		$fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
   358 		$fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
   356 
   359 
   357 		update_site_cache( $fresh_sites, $update_meta_cache );
   360 		update_site_cache( $fresh_sites, false );
   358 	}
   361 	}
       
   362 
       
   363 	if ( $update_meta_cache ) {
       
   364 		wp_lazyload_site_meta( $ids );
       
   365 	}
       
   366 }
       
   367 
       
   368 /**
       
   369  * Queue site meta for lazy-loading.
       
   370  *
       
   371  * @since 6.3.0
       
   372  *
       
   373  * @param array $site_ids List of site IDs.
       
   374  */
       
   375 function wp_lazyload_site_meta( array $site_ids ) {
       
   376 	if ( empty( $site_ids ) ) {
       
   377 		return;
       
   378 	}
       
   379 	$lazyloader = wp_metadata_lazyloader();
       
   380 	$lazyloader->queue_objects( 'blog', $site_ids );
   359 }
   381 }
   360 
   382 
   361 /**
   383 /**
   362  * Updates sites in cache.
   384  * Updates sites in cache.
   363  *
   385  *
   500  * @return array Normalized site data.
   522  * @return array Normalized site data.
   501  */
   523  */
   502 function wp_normalize_site_data( $data ) {
   524 function wp_normalize_site_data( $data ) {
   503 	// Sanitize domain if passed.
   525 	// Sanitize domain if passed.
   504 	if ( array_key_exists( 'domain', $data ) ) {
   526 	if ( array_key_exists( 'domain', $data ) ) {
   505 		$data['domain'] = trim( $data['domain'] );
   527 		$data['domain'] = preg_replace( '/[^a-z0-9\-.:]+/i', '', $data['domain'] );
   506 		$data['domain'] = preg_replace( '/\s+/', '', sanitize_user( $data['domain'], true ) );
       
   507 		if ( is_subdomain_install() ) {
       
   508 			$data['domain'] = str_replace( '@', '', $data['domain'] );
       
   509 		}
       
   510 	}
   528 	}
   511 
   529 
   512 	// Sanitize path if passed.
   530 	// Sanitize path if passed.
   513 	if ( array_key_exists( 'path', $data ) ) {
   531 	if ( array_key_exists( 'path', $data ) ) {
   514 		$data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) );
   532 		$data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) );
   820 	/**
   838 	/**
   821 	 * Filters the upload base directory to delete when the site is deleted.
   839 	 * Filters the upload base directory to delete when the site is deleted.
   822 	 *
   840 	 *
   823 	 * @since MU (3.0.0)
   841 	 * @since MU (3.0.0)
   824 	 *
   842 	 *
   825 	 * @param string $basedir Uploads path without subdirectory. @see wp_upload_dir()
   843 	 * @param string $basedir Uploads path without subdirectory. See {@see wp_upload_dir()}.
   826 	 * @param int    $site_id The site ID.
   844 	 * @param int    $site_id The site ID.
   827 	 */
   845 	 */
   828 	$dir     = apply_filters( 'wpmu_delete_blog_upload_dir', $uploads['basedir'], $site->id );
   846 	$dir     = apply_filters( 'wpmu_delete_blog_upload_dir', $uploads['basedir'], $site->id );
   829 	$dir     = rtrim( $dir, DIRECTORY_SEPARATOR );
   847 	$dir     = rtrim( $dir, DIRECTORY_SEPARATOR );
   830 	$top_dir = $dir;
   848 	$top_dir = $dir;
   853 
   871 
   854 				$file = @readdir( $dh );
   872 				$file = @readdir( $dh );
   855 			}
   873 			}
   856 			@closedir( $dh );
   874 			@closedir( $dh );
   857 		}
   875 		}
   858 		$index++;
   876 		++$index;
   859 	}
   877 	}
   860 
   878 
   861 	$stack = array_reverse( $stack ); // Last added directories are deepest.
   879 	$stack = array_reverse( $stack ); // Last added directories are deepest.
   862 	foreach ( (array) $stack as $dir ) {
   880 	foreach ( (array) $stack as $dir ) {
   863 		if ( $dir != $top_dir ) {
   881 		if ( $dir !== $top_dir ) {
   864 			@rmdir( $dir );
   882 			@rmdir( $dir );
   865 		}
   883 		}
   866 	}
   884 	}
   867 
   885 
   868 	// phpcs:enable WordPress.PHP.NoSilencedErrors.Discouraged
   886 	// phpcs:enable WordPress.PHP.NoSilencedErrors.Discouraged
   986 	 * @param WP_Site $blog            Site object.
  1004 	 * @param WP_Site $blog            Site object.
   987 	 * @param string  $domain_path_key md5 hash of domain and path.
  1005 	 * @param string  $domain_path_key md5 hash of domain and path.
   988 	 */
  1006 	 */
   989 	do_action( 'clean_site_cache', $blog_id, $blog, $domain_path_key );
  1007 	do_action( 'clean_site_cache', $blog_id, $blog, $domain_path_key );
   990 
  1008 
   991 	wp_cache_set( 'last_changed', microtime(), 'sites' );
  1009 	wp_cache_set_sites_last_changed();
   992 
  1010 
   993 	/**
  1011 	/**
   994 	 * Fires after the blog details cache is cleared.
  1012 	 * Fires after the blog details cache is cleared.
   995 	 *
  1013 	 *
   996 	 * @since 3.4.0
  1014 	 * @since 3.4.0
  1106 	if ( null === $old_site ) {
  1124 	if ( null === $old_site ) {
  1107 		wp_maybe_update_network_site_counts( $new_site->network_id );
  1125 		wp_maybe_update_network_site_counts( $new_site->network_id );
  1108 		return;
  1126 		return;
  1109 	}
  1127 	}
  1110 
  1128 
  1111 	if ( $new_site->network_id != $old_site->network_id ) {
  1129 	if ( $new_site->network_id !== $old_site->network_id ) {
  1112 		wp_maybe_update_network_site_counts( $new_site->network_id );
  1130 		wp_maybe_update_network_site_counts( $new_site->network_id );
  1113 		wp_maybe_update_network_site_counts( $old_site->network_id );
  1131 		wp_maybe_update_network_site_counts( $old_site->network_id );
  1114 	}
  1132 	}
  1115 }
  1133 }
  1116 
  1134 
  1129 	// Use the default values for a site if no previous state is given.
  1147 	// Use the default values for a site if no previous state is given.
  1130 	if ( ! $old_site ) {
  1148 	if ( ! $old_site ) {
  1131 		$old_site = new WP_Site( new stdClass() );
  1149 		$old_site = new WP_Site( new stdClass() );
  1132 	}
  1150 	}
  1133 
  1151 
  1134 	if ( $new_site->spam != $old_site->spam ) {
  1152 	if ( $new_site->spam !== $old_site->spam ) {
  1135 		if ( 1 == $new_site->spam ) {
  1153 		if ( '1' === $new_site->spam ) {
  1136 
  1154 
  1137 			/**
  1155 			/**
  1138 			 * Fires when the 'spam' status is added to a site.
  1156 			 * Fires when the 'spam' status is added to a site.
  1139 			 *
  1157 			 *
  1140 			 * @since MU (3.0.0)
  1158 			 * @since MU (3.0.0)
  1153 			 */
  1171 			 */
  1154 			do_action( 'make_ham_blog', $site_id );
  1172 			do_action( 'make_ham_blog', $site_id );
  1155 		}
  1173 		}
  1156 	}
  1174 	}
  1157 
  1175 
  1158 	if ( $new_site->mature != $old_site->mature ) {
  1176 	if ( $new_site->mature !== $old_site->mature ) {
  1159 		if ( 1 == $new_site->mature ) {
  1177 		if ( '1' === $new_site->mature ) {
  1160 
  1178 
  1161 			/**
  1179 			/**
  1162 			 * Fires when the 'mature' status is added to a site.
  1180 			 * Fires when the 'mature' status is added to a site.
  1163 			 *
  1181 			 *
  1164 			 * @since 3.1.0
  1182 			 * @since 3.1.0
  1177 			 */
  1195 			 */
  1178 			do_action( 'unmature_blog', $site_id );
  1196 			do_action( 'unmature_blog', $site_id );
  1179 		}
  1197 		}
  1180 	}
  1198 	}
  1181 
  1199 
  1182 	if ( $new_site->archived != $old_site->archived ) {
  1200 	if ( $new_site->archived !== $old_site->archived ) {
  1183 		if ( 1 == $new_site->archived ) {
  1201 		if ( '1' === $new_site->archived ) {
  1184 
  1202 
  1185 			/**
  1203 			/**
  1186 			 * Fires when the 'archived' status is added to a site.
  1204 			 * Fires when the 'archived' status is added to a site.
  1187 			 *
  1205 			 *
  1188 			 * @since MU (3.0.0)
  1206 			 * @since MU (3.0.0)
  1201 			 */
  1219 			 */
  1202 			do_action( 'unarchive_blog', $site_id );
  1220 			do_action( 'unarchive_blog', $site_id );
  1203 		}
  1221 		}
  1204 	}
  1222 	}
  1205 
  1223 
  1206 	if ( $new_site->deleted != $old_site->deleted ) {
  1224 	if ( $new_site->deleted !== $old_site->deleted ) {
  1207 		if ( 1 == $new_site->deleted ) {
  1225 		if ( '1' === $new_site->deleted ) {
  1208 
  1226 
  1209 			/**
  1227 			/**
  1210 			 * Fires when the 'deleted' status is added to a site.
  1228 			 * Fires when the 'deleted' status is added to a site.
  1211 			 *
  1229 			 *
  1212 			 * @since 3.5.0
  1230 			 * @since 3.5.0
  1225 			 */
  1243 			 */
  1226 			do_action( 'make_undelete_blog', $site_id );
  1244 			do_action( 'make_undelete_blog', $site_id );
  1227 		}
  1245 		}
  1228 	}
  1246 	}
  1229 
  1247 
  1230 	if ( $new_site->public != $old_site->public ) {
  1248 	if ( $new_site->public !== $old_site->public ) {
  1231 
  1249 
  1232 		/**
  1250 		/**
  1233 		 * Fires after the current blog's 'public' setting is updated.
  1251 		 * Fires after the current blog's 'public' setting is updated.
  1234 		 *
  1252 		 *
  1235 		 * @since MU (3.0.0)
  1253 		 * @since MU (3.0.0)
  1236 		 *
  1254 		 *
  1237 		 * @param int    $site_id Site ID.
  1255 		 * @param int    $site_id   Site ID.
  1238 		 * @param string $value   The value of the site status.
  1256 		 * @param string $is_public Whether the site is public. A numeric string,
       
  1257 		 *                          for compatibility reasons. Accepts '1' or '0'.
  1239 		 */
  1258 		 */
  1240 		do_action( 'update_blog_public', $site_id, $new_site->public );
  1259 		do_action( 'update_blog_public', $site_id, $new_site->public );
  1241 	}
  1260 	}
  1242 }
  1261 }
  1243 
  1262 
  1245  * Cleans the necessary caches after specific site data has been updated.
  1264  * Cleans the necessary caches after specific site data has been updated.
  1246  *
  1265  *
  1247  * @since 5.1.0
  1266  * @since 5.1.0
  1248  *
  1267  *
  1249  * @param WP_Site $new_site The site object after the update.
  1268  * @param WP_Site $new_site The site object after the update.
  1250  * @param WP_Site $old_site The site obejct prior to the update.
  1269  * @param WP_Site $old_site The site object prior to the update.
  1251  */
  1270  */
  1252 function wp_maybe_clean_new_site_cache_on_update( $new_site, $old_site ) {
  1271 function wp_maybe_clean_new_site_cache_on_update( $new_site, $old_site ) {
  1253 	if ( $old_site->domain !== $new_site->domain || $old_site->path !== $new_site->path ) {
  1272 	if ( $old_site->domain !== $new_site->domain || $old_site->path !== $new_site->path ) {
  1254 		clean_blog_cache( $new_site );
  1273 		clean_blog_cache( $new_site );
  1255 	}
  1274 	}
  1258 /**
  1277 /**
  1259  * Updates the `blog_public` option for a given site ID.
  1278  * Updates the `blog_public` option for a given site ID.
  1260  *
  1279  *
  1261  * @since 5.1.0
  1280  * @since 5.1.0
  1262  *
  1281  *
  1263  * @param int    $site_id Site ID.
  1282  * @param int    $site_id   Site ID.
  1264  * @param string $public  The value of the site status.
  1283  * @param string $is_public Whether the site is public. A numeric string,
  1265  */
  1284  *                          for compatibility reasons. Accepts '1' or '0'.
  1266 function wp_update_blog_public_option_on_site_update( $site_id, $public ) {
  1285  */
       
  1286 function wp_update_blog_public_option_on_site_update( $site_id, $is_public ) {
  1267 
  1287 
  1268 	// Bail if the site's database tables do not exist (yet).
  1288 	// Bail if the site's database tables do not exist (yet).
  1269 	if ( ! wp_is_site_initialized( $site_id ) ) {
  1289 	if ( ! wp_is_site_initialized( $site_id ) ) {
  1270 		return;
  1290 		return;
  1271 	}
  1291 	}
  1272 
  1292 
  1273 	update_blog_option( $site_id, 'blog_public', $public );
  1293 	update_blog_option( $site_id, 'blog_public', $is_public );
  1274 }
  1294 }
  1275 
  1295 
  1276 /**
  1296 /**
  1277  * Sets the last changed time for the 'sites' cache group.
  1297  * Sets the last changed time for the 'sites' cache group.
  1278  *
  1298  *
  1279  * @since 5.1.0
  1299  * @since 5.1.0
  1280  */
  1300  */
  1281 function wp_cache_set_sites_last_changed() {
  1301 function wp_cache_set_sites_last_changed() {
  1282 	wp_cache_set( 'last_changed', microtime(), 'sites' );
  1302 	wp_cache_set_last_changed( 'sites' );
  1283 }
  1303 }
  1284 
  1304 
  1285 /**
  1305 /**
  1286  * Aborts calls to site meta if it is not supported.
  1306  * Aborts calls to site meta if it is not supported.
  1287  *
  1307  *