wp/wp-includes/ms-site.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    67 
    67 
    68 	if ( false === $wpdb->insert( $wpdb->blogs, $prepared_data ) ) {
    68 	if ( false === $wpdb->insert( $wpdb->blogs, $prepared_data ) ) {
    69 		return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error );
    69 		return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error );
    70 	}
    70 	}
    71 
    71 
    72 	$new_site = get_site( $wpdb->insert_id );
    72 	$site_id = (int) $wpdb->insert_id;
       
    73 
       
    74 	clean_blog_cache( $site_id );
       
    75 
       
    76 	$new_site = get_site( $site_id );
    73 
    77 
    74 	if ( ! $new_site ) {
    78 	if ( ! $new_site ) {
    75 		return new WP_Error( 'get_site_error', __( 'Could not retrieve site data.' ) );
    79 		return new WP_Error( 'get_site_error', __( 'Could not retrieve site data.' ) );
    76 	}
    80 	}
    77 
       
    78 	clean_blog_cache( $new_site );
       
    79 
    81 
    80 	/**
    82 	/**
    81 	 * Fires once a site has been inserted into the database.
    83 	 * Fires once a site has been inserted into the database.
    82 	 *
    84 	 *
    83 	 * @since 5.1.0
    85 	 * @since 5.1.0
   110 		// 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.
   111 		if ( ! array_key_exists( 'WPLANG', $meta ) ) {
   113 		if ( ! array_key_exists( 'WPLANG', $meta ) ) {
   112 			$meta['WPLANG'] = get_network_option( $new_site->network_id, 'WPLANG' );
   114 			$meta['WPLANG'] = get_network_option( $new_site->network_id, 'WPLANG' );
   113 		}
   115 		}
   114 
   116 
   115 		// Rebuild the data expected by the `wpmu_new_blog` hook prior to 5.1.0 using whitelisted keys.
   117 		// Rebuild the data expected by the `wpmu_new_blog` hook prior to 5.1.0 using allowed keys.
   116 		// The `$site_data_whitelist` matches the one used in `wpmu_create_blog()`.
   118 		// The `$allowed_data_fields` matches the one used in `wpmu_create_blog()`.
   117 		$site_data_whitelist = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
   119 		$allowed_data_fields = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
   118 		$meta                = array_merge( array_intersect_key( $data, array_flip( $site_data_whitelist ) ), $meta );
   120 		$meta                = array_merge( array_intersect_key( $data, array_flip( $allowed_data_fields ) ), $meta );
   119 
   121 
   120 		/**
   122 		/**
   121 		 * Fires immediately after a new site is created.
   123 		 * Fires immediately after a new site is created.
   122 		 *
   124 		 *
   123 		 * @since MU (3.0.0)
   125 		 * @since MU (3.0.0)
   124 		 * @deprecated 5.1.0 Use wp_insert_site
   126 		 * @deprecated 5.1.0 Use {@see 'wp_insert_site'} instead.
   125 		 *
   127 		 *
   126 		 * @param int    $site_id    Site ID.
   128 		 * @param int    $site_id    Site ID.
   127 		 * @param int    $user_id    User ID.
   129 		 * @param int    $user_id    User ID.
   128 		 * @param string $domain     Site domain.
   130 		 * @param string $domain     Site domain.
   129 		 * @param string $path       Site path.
   131 		 * @param string $path       Site path.
   130 		 * @param int    $network_id Network ID. Only relevant on multi-network installations.
   132 		 * @param int    $network_id Network ID. Only relevant on multi-network installations.
   131 		 * @param array  $meta       Meta data. Used to set initial site options.
   133 		 * @param array  $meta       Meta data. Used to set initial site options.
   132 		 */
   134 		 */
   133 		do_action_deprecated( 'wpmu_new_blog', array( $new_site->id, $user_id, $new_site->domain, $new_site->path, $new_site->network_id, $meta ), '5.1.0', 'wp_insert_site' );
   135 		do_action_deprecated(
       
   136 			'wpmu_new_blog',
       
   137 			array( $new_site->id, $user_id, $new_site->domain, $new_site->path, $new_site->network_id, $meta ),
       
   138 			'5.1.0',
       
   139 			'wp_insert_site'
       
   140 		);
   134 	}
   141 	}
   135 
   142 
   136 	return (int) $new_site->id;
   143 	return (int) $new_site->id;
   137 }
   144 }
   138 
   145 
   236 	 *
   243 	 *
   237 	 * @since MU (3.0.0)
   244 	 * @since MU (3.0.0)
   238 	 * @deprecated 5.1.0
   245 	 * @deprecated 5.1.0
   239 	 *
   246 	 *
   240 	 * @param int  $site_id The site ID.
   247 	 * @param int  $site_id The site ID.
   241 	 * @param bool $drop    True if site's table should be dropped. Default is false.
   248 	 * @param bool $drop    True if site's table should be dropped. Default false.
   242 	 */
   249 	 */
   243 	do_action_deprecated( 'delete_blog', array( $old_site->id, true ), '5.1.0' );
   250 	do_action_deprecated( 'delete_blog', array( $old_site->id, true ), '5.1.0' );
   244 
   251 
   245 	/**
   252 	/**
   246 	 * Fires when a site's uninitialization routine should be executed.
   253 	 * Fires when a site's uninitialization routine should be executed.
   278 	 *
   285 	 *
   279 	 * @since 4.8.0
   286 	 * @since 4.8.0
   280 	 * @deprecated 5.1.0
   287 	 * @deprecated 5.1.0
   281 	 *
   288 	 *
   282 	 * @param int  $site_id The site ID.
   289 	 * @param int  $site_id The site ID.
   283 	 * @param bool $drop    True if site's tables should be dropped. Default is false.
   290 	 * @param bool $drop    True if site's tables should be dropped. Default false.
   284 	 */
   291 	 */
   285 	do_action_deprecated( 'deleted_blog', array( $old_site->id, true ), '5.1.0' );
   292 	do_action_deprecated( 'deleted_blog', array( $old_site->id, true ), '5.1.0' );
   286 
   293 
   287 	return $old_site;
   294 	return $old_site;
   288 }
   295 }
   326 
   333 
   327 	return $_site;
   334 	return $_site;
   328 }
   335 }
   329 
   336 
   330 /**
   337 /**
   331  * Adds any sites from the given ids to the cache that do not already exist in cache.
   338  * Adds any sites from the given IDs to the cache that do not already exist in cache.
   332  *
   339  *
   333  * @since 4.6.0
   340  * @since 4.6.0
   334  * @since 5.1.0 Introduced the `$update_meta_cache` parameter.
   341  * @since 5.1.0 Introduced the `$update_meta_cache` parameter.
   335  * @access private
   342  * @access private
   336  *
   343  *
   383  * Subsequent calls to `get_site_meta()` will not need to query the database.
   390  * Subsequent calls to `get_site_meta()` will not need to query the database.
   384  *
   391  *
   385  * @since 5.1.0
   392  * @since 5.1.0
   386  *
   393  *
   387  * @param array $site_ids List of site IDs.
   394  * @param array $site_ids List of site IDs.
   388  * @return array|false Returns false if there is nothing to update. Returns an array of metadata on success.
   395  * @return array|false An array of metadata on success, false if there is nothing to update.
   389  */
   396  */
   390 function update_sitemeta_cache( $site_ids ) {
   397 function update_sitemeta_cache( $site_ids ) {
   391 	// Ensure this filter is hooked in even if the function is called early.
   398 	// Ensure this filter is hooked in even if the function is called early.
   392 	if ( ! has_filter( 'update_blog_metadata_cache', 'wp_check_site_meta_support_prefilter' ) ) {
   399 	if ( ! has_filter( 'update_blog_metadata_cache', 'wp_check_site_meta_support_prefilter' ) ) {
   393 		add_filter( 'update_blog_metadata_cache', 'wp_check_site_meta_support_prefilter' );
   400 		add_filter( 'update_blog_metadata_cache', 'wp_check_site_meta_support_prefilter' );
   446  *     @type string       $search            Search term(s) to retrieve matching sites for. Default empty.
   453  *     @type string       $search            Search term(s) to retrieve matching sites for. Default empty.
   447  *     @type array        $search_columns    Array of column names to be searched. Accepts 'domain' and 'path'.
   454  *     @type array        $search_columns    Array of column names to be searched. Accepts 'domain' and 'path'.
   448  *                                           Default empty array.
   455  *                                           Default empty array.
   449  *     @type bool         $update_site_cache Whether to prime the cache for found sites. Default true.
   456  *     @type bool         $update_site_cache Whether to prime the cache for found sites. Default true.
   450  * }
   457  * }
   451  * @return array|int List of WP_Site objects, a list of site ids when 'fields' is set to 'ids',
   458  * @return array|int List of WP_Site objects, a list of site IDs when 'fields' is set to 'ids',
   452  *                   or the number of sites when 'count' is passed as a query var.
   459  *                   or the number of sites when 'count' is passed as a query var.
   453  */
   460  */
   454 function get_sites( $args = array() ) {
   461 function get_sites( $args = array() ) {
   455 	$query = new WP_Site_Query();
   462 	$query = new WP_Site_Query();
   456 
   463 
   488 	 * @param array $data Associative array of site data passed to the respective function.
   495 	 * @param array $data Associative array of site data passed to the respective function.
   489 	 *                    See {@see wp_insert_site()} for the possibly included data.
   496 	 *                    See {@see wp_insert_site()} for the possibly included data.
   490 	 */
   497 	 */
   491 	$data = apply_filters( 'wp_normalize_site_data', $data );
   498 	$data = apply_filters( 'wp_normalize_site_data', $data );
   492 
   499 
   493 	$whitelist = array( 'domain', 'path', 'network_id', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
   500 	$allowed_data_fields = array( 'domain', 'path', 'network_id', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
   494 	$data      = array_intersect_key( wp_parse_args( $data, $defaults ), array_flip( $whitelist ) );
   501 	$data                = array_intersect_key( wp_parse_args( $data, $defaults ), array_flip( $allowed_data_fields ) );
   495 
   502 
   496 	$errors = new WP_Error();
   503 	$errors = new WP_Error();
   497 
   504 
   498 	/**
   505 	/**
   499 	 * Fires when data should be validated for a site prior to inserting or updating in the database.
   506 	 * Fires when data should be validated for a site prior to inserting or updating in the database.
   686 
   693 
   687 	$args = wp_parse_args(
   694 	$args = wp_parse_args(
   688 		$args,
   695 		$args,
   689 		array(
   696 		array(
   690 			'user_id' => 0,
   697 			'user_id' => 0,
   691 			/* translators: %d: site ID */
   698 			/* translators: %d: Site ID. */
   692 			'title'   => sprintf( __( 'Site %d' ), $site->id ),
   699 			'title'   => sprintf( __( 'Site %d' ), $site->id ),
   693 			'options' => array(),
   700 			'options' => array(),
   694 			'meta'    => array(),
   701 			'meta'    => array(),
   695 		)
   702 		)
   696 	);
   703 	);
   715 	if ( get_current_blog_id() !== $site->id ) {
   722 	if ( get_current_blog_id() !== $site->id ) {
   716 		$switch = true;
   723 		$switch = true;
   717 		switch_to_blog( $site->id );
   724 		switch_to_blog( $site->id );
   718 	}
   725 	}
   719 
   726 
   720 	require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
   727 	require_once ABSPATH . 'wp-admin/includes/upgrade.php';
   721 
   728 
   722 	// Set up the database tables.
   729 	// Set up the database tables.
   723 	make_db_current_silent( 'blog' );
   730 	make_db_current_silent( 'blog' );
   724 
   731 
   725 	$home_scheme    = 'http';
   732 	$home_scheme    = 'http';
   759 	// Populate metadata for the site.
   766 	// Populate metadata for the site.
   760 	populate_site_meta( $site->id, $args['meta'] );
   767 	populate_site_meta( $site->id, $args['meta'] );
   761 
   768 
   762 	// Remove all permissions that may exist for the site.
   769 	// Remove all permissions that may exist for the site.
   763 	$table_prefix = $wpdb->get_blog_prefix();
   770 	$table_prefix = $wpdb->get_blog_prefix();
   764 	delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true ); // delete all
   771 	delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true );   // Delete all.
   765 	delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // delete all
   772 	delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // Delete all.
   766 
   773 
   767 	// Install default site content.
   774 	// Install default site content.
   768 	wp_install_defaults( $args['user_id'] );
   775 	wp_install_defaults( $args['user_id'] );
   769 
   776 
   770 	// Set the site administrator.
   777 	// Set the site administrator.
   861 	$top_dir = $dir;
   868 	$top_dir = $dir;
   862 	$stack   = array( $dir );
   869 	$stack   = array( $dir );
   863 	$index   = 0;
   870 	$index   = 0;
   864 
   871 
   865 	while ( $index < count( $stack ) ) {
   872 	while ( $index < count( $stack ) ) {
   866 		// Get indexed directory from stack
   873 		// Get indexed directory from stack.
   867 		$dir = $stack[ $index ];
   874 		$dir = $stack[ $index ];
   868 
   875 
   869 		// phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
   876 		// phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
   870 		$dh = @opendir( $dir );
   877 		$dh = @opendir( $dir );
   871 		if ( $dh ) {
   878 		if ( $dh ) {
   887 			@closedir( $dh );
   894 			@closedir( $dh );
   888 		}
   895 		}
   889 		$index++;
   896 		$index++;
   890 	}
   897 	}
   891 
   898 
   892 	$stack = array_reverse( $stack ); // Last added dirs are deepest
   899 	$stack = array_reverse( $stack ); // Last added directories are deepest.
   893 	foreach ( (array) $stack as $dir ) {
   900 	foreach ( (array) $stack as $dir ) {
   894 		if ( $dir != $top_dir ) {
   901 		if ( $dir != $top_dir ) {
   895 			@rmdir( $dir );
   902 			@rmdir( $dir );
   896 		}
   903 		}
   897 	}
   904 	}
   930 	 * Returning a non-null value will effectively short-circuit the function, returning
   937 	 * Returning a non-null value will effectively short-circuit the function, returning
   931 	 * that value instead.
   938 	 * that value instead.
   932 	 *
   939 	 *
   933 	 * @since 5.1.0
   940 	 * @since 5.1.0
   934 	 *
   941 	 *
   935 	 * @param bool|null $pre     The value to return, if not null.
   942 	 * @param bool|null $pre     The value to return instead. Default null
       
   943 	 *                           to continue with the check.
   936 	 * @param int       $site_id The site ID that is being checked.
   944 	 * @param int       $site_id The site ID that is being checked.
   937 	 */
   945 	 */
   938 	$pre = apply_filters( 'pre_wp_is_site_initialized', null, $site_id );
   946 	$pre = apply_filters( 'pre_wp_is_site_initialized', null, $site_id );
   939 	if ( null !== $pre ) {
   947 	if ( null !== $pre ) {
   940 		return (bool) $pre;
   948 		return (bool) $pre;
  1003 	wp_cache_delete( $blog_id, 'site-details' );
  1011 	wp_cache_delete( $blog_id, 'site-details' );
  1004 	wp_cache_delete( $blog_id, 'blog-details' );
  1012 	wp_cache_delete( $blog_id, 'blog-details' );
  1005 	wp_cache_delete( $blog_id . 'short', 'blog-details' );
  1013 	wp_cache_delete( $blog_id . 'short', 'blog-details' );
  1006 	wp_cache_delete( $domain_path_key, 'blog-lookup' );
  1014 	wp_cache_delete( $domain_path_key, 'blog-lookup' );
  1007 	wp_cache_delete( $domain_path_key, 'blog-id-cache' );
  1015 	wp_cache_delete( $domain_path_key, 'blog-id-cache' );
  1008 	wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' );
       
  1009 	wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' );
       
  1010 	wp_cache_delete( $blog_id, 'blog_meta' );
  1016 	wp_cache_delete( $blog_id, 'blog_meta' );
  1011 
  1017 
  1012 	/**
  1018 	/**
  1013 	 * Fires immediately after a site has been removed from the object cache.
  1019 	 * Fires immediately after a site has been removed from the object cache.
  1014 	 *
  1020 	 *
  1024 
  1030 
  1025 	/**
  1031 	/**
  1026 	 * Fires after the blog details cache is cleared.
  1032 	 * Fires after the blog details cache is cleared.
  1027 	 *
  1033 	 *
  1028 	 * @since 3.4.0
  1034 	 * @since 3.4.0
  1029 	 * @deprecated 4.9.0 Use clean_site_cache
  1035 	 * @deprecated 4.9.0 Use {@see 'clean_site_cache'} instead.
  1030 	 *
  1036 	 *
  1031 	 * @param int $blog_id Blog ID.
  1037 	 * @param int $blog_id Blog ID.
  1032 	 */
  1038 	 */
  1033 	do_action_deprecated( 'refresh_blog_details', array( $blog_id ), '4.9.0', 'clean_site_cache' );
  1039 	do_action_deprecated( 'refresh_blog_details', array( $blog_id ), '4.9.0', 'clean_site_cache' );
  1034 }
  1040 }
  1058  *
  1064  *
  1059  * @since 5.1.0
  1065  * @since 5.1.0
  1060  *
  1066  *
  1061  * @param int    $site_id    Site ID.
  1067  * @param int    $site_id    Site ID.
  1062  * @param string $meta_key   Metadata name.
  1068  * @param string $meta_key   Metadata name.
  1063  * @param mixed  $meta_value Optional. Metadata value. Must be serializable if
  1069  * @param mixed  $meta_value Optional. Metadata value. If provided,
  1064  *                           non-scalar. Default empty.
  1070  *                           rows will only be removed that match the value.
       
  1071  *                           Must be serializable if non-scalar. Default empty.
  1065  * @return bool True on success, false on failure.
  1072  * @return bool True on success, false on failure.
  1066  */
  1073  */
  1067 function delete_site_meta( $site_id, $meta_key, $meta_value = '' ) {
  1074 function delete_site_meta( $site_id, $meta_key, $meta_value = '' ) {
  1068 	return delete_metadata( 'blog', $site_id, $meta_key, $meta_value );
  1075 	return delete_metadata( 'blog', $site_id, $meta_key, $meta_value );
  1069 }
  1076 }
  1072  * Retrieves metadata for a site.
  1079  * Retrieves metadata for a site.
  1073  *
  1080  *
  1074  * @since 5.1.0
  1081  * @since 5.1.0
  1075  *
  1082  *
  1076  * @param int    $site_id Site ID.
  1083  * @param int    $site_id Site ID.
  1077  * @param string $key     Optional. The meta key to retrieve. By default, returns
  1084  * @param string $key     Optional. The meta key to retrieve. By default,
  1078  *                        data for all keys. Default empty.
  1085  *                        returns data for all keys. Default empty.
  1079  * @param bool   $single  Optional. Whether to return a single value. Default false.
  1086  * @param bool   $single  Optional. Whether to return a single value.
  1080  * @return mixed Will be an array if $single is false. Will be value of meta data
  1087  *                        This parameter has no effect if $key is not specified.
  1081  *               field if $single is true.
  1088  *                        Default false.
       
  1089  * @return mixed An array if $single is false. The value of meta data field
       
  1090  *               if $single is true. False for an invalid $site_id.
  1082  */
  1091  */
  1083 function get_site_meta( $site_id, $key = '', $single = false ) {
  1092 function get_site_meta( $site_id, $key = '', $single = false ) {
  1084 	return get_metadata( 'blog', $site_id, $key, $single );
  1093 	return get_metadata( 'blog', $site_id, $key, $single );
  1085 }
  1094 }
  1086 
  1095 
  1095  * @since 5.1.0
  1104  * @since 5.1.0
  1096  *
  1105  *
  1097  * @param int    $site_id    Site ID.
  1106  * @param int    $site_id    Site ID.
  1098  * @param string $meta_key   Metadata key.
  1107  * @param string $meta_key   Metadata key.
  1099  * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
  1108  * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
  1100  * @param mixed  $prev_value Optional. Previous value to check before removing.
  1109  * @param mixed  $prev_value Optional. Previous value to check before updating.
  1101  *                           Default empty.
  1110  *                           If specified, only update existing metadata entries with
       
  1111  *                           this value. Otherwise, update all entries. Default empty.
  1102  * @return int|bool Meta ID if the key didn't exist, true on successful update,
  1112  * @return int|bool Meta ID if the key didn't exist, true on successful update,
  1103  *                  false on failure.
  1113  *                  false on failure or if the value passed to the function
       
  1114  *                  is the same as the one that is already in the database.
  1104  */
  1115  */
  1105 function update_site_meta( $site_id, $meta_key, $meta_value, $prev_value = '' ) {
  1116 function update_site_meta( $site_id, $meta_key, $meta_value, $prev_value = '' ) {
  1106 	return update_metadata( 'blog', $site_id, $meta_key, $meta_value, $prev_value );
  1117 	return update_metadata( 'blog', $site_id, $meta_key, $meta_value, $prev_value );
  1107 }
  1118 }
  1108 
  1119 
  1317  * @param mixed $check Skip-value for whether to proceed site meta function execution.
  1328  * @param mixed $check Skip-value for whether to proceed site meta function execution.
  1318  * @return mixed Original value of $check, or false if site meta is not supported.
  1329  * @return mixed Original value of $check, or false if site meta is not supported.
  1319  */
  1330  */
  1320 function wp_check_site_meta_support_prefilter( $check ) {
  1331 function wp_check_site_meta_support_prefilter( $check ) {
  1321 	if ( ! is_site_meta_supported() ) {
  1332 	if ( ! is_site_meta_supported() ) {
  1322 		/* translators: %s: database table name */
  1333 		/* translators: %s: Database table name. */
  1323 		_doing_it_wrong( __FUNCTION__, sprintf( __( 'The %s table is not installed. Please run the network database upgrade.' ), $GLOBALS['wpdb']->blogmeta ), '5.1.0' );
  1334 		_doing_it_wrong( __FUNCTION__, sprintf( __( 'The %s table is not installed. Please run the network database upgrade.' ), $GLOBALS['wpdb']->blogmeta ), '5.1.0' );
  1324 		return false;
  1335 		return false;
  1325 	}
  1336 	}
  1326 
  1337 
  1327 	return $check;
  1338 	return $check;