Ticket #37923: 37923.3.diff
File 37923.3.diff, 25.9 KB (added by , 7 years ago) |
---|
-
src/wp-admin/includes/ms.php
127 127 $wpdb->query( "DROP TABLE IF EXISTS `$table`" ); 128 128 } 129 129 130 $blog_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->blogmeta WHERE blog_id = %d ", $blog_id ) ); 131 foreach ( $blog_meta_ids as $mid ) { 132 delete_metadata_by_mid( 'blog', $mid ); 133 } 134 130 135 $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ) ); 131 136 132 137 /** -
src/wp-admin/includes/schema.php
265 265 PRIMARY KEY (blog_id), 266 266 KEY db_version (db_version) 267 267 ) $charset_collate; 268 CREATE TABLE $wpdb->blogmeta ( 269 meta_id bigint(20) NOT NULL auto_increment, 270 blog_id bigint(20) NOT NULL default '0', 271 meta_key varchar(255) default NULL, 272 meta_value longtext, 273 PRIMARY KEY (meta_id), 274 KEY meta_key (meta_key($max_index_length)), 275 KEY blog_id (blog_id) 276 ) $charset_collate; 268 277 CREATE TABLE $wpdb->registration_log ( 269 278 ID bigint(20) NOT NULL auto_increment, 270 279 email varchar(255) NOT NULL default '', -
src/wp-admin/includes/upgrade.php
1881 1881 } 1882 1882 } 1883 1883 } 1884 1885 // 4.9 1886 if ( $wp_current_db_version < 40001 ) { 1887 update_site_option( 'site_meta_supported', '1'); 1888 } 1884 1889 } 1885 1890 1886 1891 // -
src/wp-includes/class-wp-site-query.php
39 39 'limits' => '', 40 40 ); 41 41 42 42 43 /** 43 44 * Date query container. 44 45 * … … 136 137 * @type string $search Search term(s) to retrieve matching sites for. Default empty. 137 138 * @type array $search_columns Array of column names to be searched. Accepts 'domain' and 'path'. 138 139 * Default empty array. 139 * @type bool $update_site_cache Whether to prime the cache for found sites. Default false. 140 * @type bool $update_site_cache Whether to prime the cache for found sites. Default true. 141 * @type bool $update_site_meta_cache Whether to prime the metadata cache for found sites. 142 * Default true. 140 143 * } 141 144 */ 142 145 public function __construct( $query = '' ) { … … 172 175 'count' => false, 173 176 'date_query' => null, // See WP_Date_Query 174 177 'update_site_cache' => true, 178 'update_site_meta_cache' => true, 175 179 ); 176 180 177 181 if ( ! empty( $query ) ) { … … 224 228 * 225 229 * @since 4.6.0 226 230 * 231 * @global wpdb $wpdb WordPress database abstraction object. 232 * 227 233 * @return array|int List of sites, or number of sites when 'count' is passed as a query var. 228 234 */ 229 230 $this->parse_query();235 public function get_sites() { 236 global $wpdb; 231 237 232 233 234 235 236 237 238 238 /** 239 * Fires before sites are retrieved. 240 * 241 * @since 4.6.0 242 * 243 * @param WP_Site_Query &$this Current instance of WP_Site_Query, passed by reference. 244 */ 239 245 do_action_ref_array( 'pre_get_sites', array( &$this ) ); 246 $this->parse_query(); 247 240 248 241 249 // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. 242 250 $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); … … 286 294 287 295 // Prime site network caches. 288 296 if ( $this->query_vars['update_site_cache'] ) { 289 _prime_site_caches( $site_ids);297 _prime_site_caches( $site_ids, $this->query_vars['update_site_meta_cache'] ); 290 298 } 291 299 292 300 // Fetch full site objects from the primed cache. … … 365 373 366 374 $orderby = implode( ', ', $orderby_array ); 367 375 } else { 368 $orderby = " blog_id $order";376 $orderby = "$wpdb->blogs.blog_id $order"; 369 377 } 370 378 371 379 $number = absint( $this->query_vars['number'] ); … … 382 390 if ( $this->query_vars['count'] ) { 383 391 $fields = 'COUNT(*)'; 384 392 } else { 385 $fields = 'blog_id';393 $fields = "$wpdb->blogs.blog_id"; 386 394 } 387 395 388 396 // Parse site IDs for an IN clause. 389 397 $site_id = absint( $this->query_vars['ID'] ); 390 398 if ( ! empty( $site_id ) ) { 391 $this->sql_clauses['where']['ID'] = $wpdb->prepare( 'blog_id = %d', $site_id );399 $this->sql_clauses['where']['ID'] = $wpdb->prepare( "$wpdb->blogs.blog_id = %d", $site_id ); 392 400 } 393 401 394 402 // Parse site IDs for an IN clause. 395 403 if ( ! empty( $this->query_vars['site__in'] ) ) { 396 $this->sql_clauses['where']['site__in'] = " blog_id IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__in'] ) ) . ' )';404 $this->sql_clauses['where']['site__in'] = "$wpdb->blogs.blog_id IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__in'] ) ) . ' )'; 397 405 } 398 406 399 407 // Parse site IDs for a NOT IN clause. 400 408 if ( ! empty( $this->query_vars['site__not_in'] ) ) { 401 $this->sql_clauses['where']['site__not_in'] = " blog_id NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__not_in'] ) ) . ' )';409 $this->sql_clauses['where']['site__not_in'] = "$wpdb->blogs.blog_id NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__not_in'] ) ) . ' )'; 402 410 } 403 411 404 412 $network_id = absint( $this->query_vars['network_id'] ); … … 670 678 $parsed = 'CHAR_LENGTH(path)'; 671 679 break; 672 680 case 'id': 673 $parsed = 'blog_id';681 $parsed = "$wpdb->blogs.blog_id"; 674 682 break; 675 683 } 676 684 -
src/wp-includes/load.php
517 517 } 518 518 519 519 if ( function_exists( 'wp_cache_add_global_groups' ) ) { 520 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'site-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites' ) );520 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'site-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'blog_meta' ) ); 521 521 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); 522 522 } 523 523 } -
src/wp-includes/ms-blogs.php
465 465 wp_cache_delete( $domain_path_key, 'blog-id-cache' ); 466 466 wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' ); 467 467 wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' ); 468 wp_cache_delete( $blog_id, 'blog_meta' ); 468 469 469 470 /** 470 471 * Fires immediately after a site has been removed from the object cache. … … 541 542 * Adds any sites from the given ids to the cache that do not already exist in cache. 542 543 * 543 544 * @since 4.6.0 545 * @since 4.9.0 The $update_meta_cache parameter was added. 544 546 * @access private 545 547 * 546 548 * @see update_site_cache() 549 * @see update_sitemeta_cache() 550 * 547 551 * @global wpdb $wpdb WordPress database abstraction object. 548 552 * 549 * @param array $ids ID list. 553 * @param array $ids ID list. 554 * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. 550 555 */ 551 function _prime_site_caches( $ids ) {556 function _prime_site_caches( $ids, $update_meta_cache = true ) { 552 557 global $wpdb; 553 558 554 559 $non_cached_ids = _get_non_cached_ids( $ids, 'sites' ); 555 560 if ( ! empty( $non_cached_ids ) ) { 556 561 $fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) ); 557 562 558 update_site_cache( $fresh_sites );563 update_site_cache( $fresh_sites, $update_meta_cache ); 559 564 } 560 565 } 561 566 … … 566 571 * 567 572 * @param array $sites Array of site objects. 568 573 */ 569 function update_site_cache( $sites ) {574 function update_site_cache( $sites, $update_meta_cache = true ) { 570 575 if ( ! $sites ) { 571 576 return; 572 577 } 573 578 $site_ids = array(); 574 579 foreach ( $sites as $site ) { 575 wp_cache_add( $site->blog_id, $site, 'sites' ); 580 $site_ids[] = $site->blog_id; 581 wp_cache_add( $site->blog_id, $site, 'sites' ); 576 582 wp_cache_add( $site->blog_id . 'short', $site, 'blog-details' ); 577 583 } 584 585 if ( $update_meta_cache ) { 586 update_sitemeta_cache( $site_ids ); 587 } 588 } 589 590 /** 591 * Updates metadata cache for list of site IDs. 592 * 593 * Performs SQL query to retrieve all metadata for the sites matching `$site_ids` and stores them in the cache. 594 * Subsequent calls to `get_site_meta()` will not need to query the database. 595 * 596 * @since 4.9.0 597 * 598 * @param array $site_ids List of site IDs. 599 * @return array|false Returns false if there is nothing to update. Returns an array of metadata on success. 600 */ 601 function update_sitemeta_cache( $site_ids ) { 602 return update_meta_cache( 'blog', $site_ids ); 578 603 } 579 604 580 605 /** … … 638 663 return $query->query( $args ); 639 664 } 640 665 666 641 667 /** 642 668 * Retrieve option value for a given blog id based on name of option. 643 669 * … … 842 868 if ( is_array( $global_groups ) ) { 843 869 wp_cache_add_global_groups( $global_groups ); 844 870 } else { 845 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );871 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) ); 846 872 } 847 873 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); 848 874 } … … 916 942 if ( is_array( $global_groups ) ) { 917 943 wp_cache_add_global_groups( $global_groups ); 918 944 } else { 919 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );945 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) ); 920 946 } 921 947 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); 922 948 } -
src/wp-includes/option.php
225 225 if ( empty($site_id) ) 226 226 $site_id = $wpdb->siteid; 227 227 228 $core_options = array('site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'can_compress_scripts', 'global_terms_enabled', 'ms_files_rewriting' );228 $core_options = array('site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'can_compress_scripts', 'global_terms_enabled', 'ms_files_rewriting', 'site_meta_supported' ); 229 229 230 230 $core_options_in = "'" . implode("', '", $core_options) . "'"; 231 231 $options = $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->sitemeta WHERE meta_key IN ($core_options_in) AND site_id = %d", $site_id) ); … … 2059 2059 2060 2060 return $registered[ $option ]['default']; 2061 2061 } 2062 2063 2064 /** 2065 * Add meta data field to a site. 2066 * 2067 * Site meta data is called "Custom Fields" on the Administration Screen. 2068 * 2069 * @since 4.9.0 2070 * 2071 * @param int $site_id Site ID. 2072 * @param string $meta_key Metadata name. 2073 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 2074 * @param bool $unique Optional. Whether the same key should not be added. 2075 * Default false. 2076 * @return int|false Meta ID on success, false on failure. 2077 */ 2078 function add_site_meta( $site_id, $meta_key, $meta_value, $unique = false ) { 2079 if( !is_multisite() ){ 2080 return false; 2081 } 2082 // If site meta not installed, quit early 2083 if( !get_site_option('site_meta_supported') ){ 2084 return false; 2085 } 2086 2087 $added = add_metadata( 'blog', $site_id, $meta_key, $meta_value, $unique ); 2088 2089 // Bust site query cache. 2090 if ( $added ) { 2091 wp_cache_set( 'last_changed', microtime(), 'sites' ); 2092 } 2093 2094 return $added; 2095 } 2096 2097 /** 2098 * Remove metadata matching criteria from a site. 2099 * 2100 * You can match based on the key, or key and value. Removing based on key and 2101 * value, will keep from removing duplicate metadata with the same key. It also 2102 * allows removing all metadata matching key, if needed. 2103 * 2104 * @since 4.9.0 2105 * 2106 * @param int $site_id Site ID. 2107 * @param string $meta_key Metadata name. 2108 * @param mixed $meta_value Optional. Metadata value. Must be serializable if 2109 * non-scalar. Default empty. 2110 * @return bool True on success, false on failure. 2111 */ 2112 function delete_site_meta( $site_id, $meta_key, $meta_value = '' ) { 2113 if( !is_multisite() ){ 2114 return false; 2115 } 2116 // If site meta not installed, quit early 2117 if( !get_site_option('site_meta_supported') ){ 2118 return false; 2119 } 2120 2121 $deleted = delete_metadata( 'blog', $site_id, $meta_key, $meta_value ); 2122 2123 // Bust site query cache. 2124 if ( $deleted ) { 2125 wp_cache_set( 'last_changed', microtime(), 'sites' ); 2126 } 2127 2128 return $deleted; 2129 } 2130 2131 /** 2132 * Retrieve site meta field for a site. 2133 * 2134 * @since 4.9.0 2135 * 2136 * @param int $site_id Site ID. 2137 * @param string $key Optional. The meta key to retrieve. By default, returns 2138 * data for all keys. Default empty. 2139 * @param bool $single Optional. Whether to return a single value. Default false. 2140 * @return mixed Will be an array if $single is false. Will be value of meta data 2141 * field if $single is true. 2142 */ 2143 function get_site_meta( $site_id, $key = '', $single = false ) { 2144 if( !is_multisite() ){ 2145 return false; 2146 } 2147 // If site meta not installed, quit early 2148 if( !get_site_option('site_meta_supported') ){ 2149 return false; 2150 } 2151 return get_metadata( 'blog', $site_id, $key, $single ); 2152 } 2153 2154 /** 2155 * Update site meta field based on site ID. 2156 * 2157 * Use the $prev_value parameter to differentiate between meta fields with the 2158 * same key and site ID. 2159 * 2160 * If the meta field for the site does not exist, it will be added. 2161 * 2162 * @since 4.9.0 2163 * 2164 * @param int $site_id Site ID. 2165 * @param string $meta_key Metadata key. 2166 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 2167 * @param mixed $prev_value Optional. Previous value to check before removing. 2168 * Default empty. 2169 * @return int|bool Meta ID if the key didn't exist, true on successful update, 2170 * false on failure. 2171 */ 2172 function update_site_meta( $site_id, $meta_key, $meta_value, $prev_value = '' ) { 2173 if( !is_multisite() ){ 2174 return false; 2175 } 2176 // If site meta not installed, quit early 2177 if( !get_site_option('site_meta_supported') ){ 2178 return false; 2179 } 2180 2181 $updated = update_metadata( 'blog', $site_id, $meta_key, $meta_value, $prev_value ); 2182 2183 // Bust site query cache. 2184 if ( $updated ) { 2185 wp_cache_set( 'last_changed', microtime(), 'sites' ); 2186 } 2187 2188 return $updated; 2189 } 2190 2191 /** 2192 * Delete everything from site meta matching meta key. 2193 * 2194 * @since 4.9.0 2195 * 2196 * @param string $meta_key Metadata key to search for when deleting. 2197 * @return bool Whether the site meta key was deleted from the database. 2198 */ 2199 function delete_site_meta_by_key( $meta_key ) { 2200 if( !is_multisite() ){ 2201 return false; 2202 } 2203 // If site meta not installed, quit early 2204 if( !get_site_option('site_meta_supported') ){ 2205 return false; 2206 } 2207 2208 return delete_metadata( 'blog', null, $meta_key, '', true ); 2209 } 2210 No newline at end of file -
src/wp-includes/version.php
11 11 * 12 12 * @global int $wp_db_version 13 13 */ 14 $wp_db_version = 38590;14 $wp_db_version = 40001; 15 15 16 16 /** 17 17 * Holds the TinyMCE version -
src/wp-includes/wp-db.php
271 271 * @see wpdb::tables() 272 272 * @var array 273 273 */ 274 var $ms_global_tables = array( 'blogs', ' signups', 'site', 'sitemeta',274 var $ms_global_tables = array( 'blogs', 'blogmeta', 'signups', 'site', 'sitemeta', 275 275 'sitecategories', 'registration_log', 'blog_versions' ); 276 276 277 277 /** … … 383 383 public $blogs; 384 384 385 385 /** 386 * Multisite Blog Metadata table 387 * 388 * @since 4.9.0 389 * @access public 390 * @var string 391 */ 392 public $blogmeta; 393 394 /** 386 395 * Multisite Blog Versions table 387 396 * 388 397 * @since 3.0.0 -
src/wp-settings.php
95 95 // Load early WordPress files. 96 96 require( ABSPATH . WPINC . '/compat.php' ); 97 97 require( ABSPATH . WPINC . '/class-wp-list-util.php' ); 98 require( ABSPATH . WPINC . '/formatting.php' ); 99 require( ABSPATH . WPINC . '/meta.php' ); 98 100 require( ABSPATH . WPINC . '/functions.php' ); 99 101 require( ABSPATH . WPINC . '/class-wp-matchesmapregex.php' ); 100 102 require( ABSPATH . WPINC . '/class-wp.php' ); … … 142 144 // Load most of WordPress. 143 145 require( ABSPATH . WPINC . '/class-wp-walker.php' ); 144 146 require( ABSPATH . WPINC . '/class-wp-ajax-response.php' ); 145 require( ABSPATH . WPINC . '/formatting.php' ); 147 146 148 require( ABSPATH . WPINC . '/capabilities.php' ); 147 149 require( ABSPATH . WPINC . '/class-wp-roles.php' ); 148 150 require( ABSPATH . WPINC . '/class-wp-role.php' ); … … 157 159 require( ABSPATH . WPINC . '/class-wp-user-query.php' ); 158 160 require( ABSPATH . WPINC . '/class-wp-session-tokens.php' ); 159 161 require( ABSPATH . WPINC . '/class-wp-user-meta-session-tokens.php' ); 160 require( ABSPATH . WPINC . '/meta.php' );161 162 require( ABSPATH . WPINC . '/class-wp-meta-query.php' ); 162 163 require( ABSPATH . WPINC . '/class-wp-metadata-lazyloader.php' ); 163 164 require( ABSPATH . WPINC . '/general-template.php' ); -
tests/phpunit/includes/testcase.php
323 323 $wp_object_cache->__remoteset(); 324 324 } 325 325 wp_cache_flush(); 326 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );326 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) ); 327 327 wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) ); 328 328 } 329 329 -
tests/phpunit/tests/multisite/siteMeta.php
1 <?php 2 3 if ( is_multisite() ) : 4 /** 5 * @group ms-site 6 * @group multisite 7 * @group meta 8 * @ticket 37923 9 */ 10 class Tests_Multisite_Site_Meta extends WP_UnitTestCase { 11 protected static $site_id; 12 protected static $site_id2; 13 14 function setUp() { 15 global $wpdb; 16 parent::setUp(); 17 $this->suppress = $wpdb->suppress_errors(); 18 19 update_site_option( 'site_meta_supported', 1 ); 20 } 21 22 function tearDown() { 23 global $wpdb; 24 25 update_site_option( 'site_meta_supported', 0 ); 26 $wpdb->suppress_errors( $this->suppress ); 27 28 parent::tearDown(); 29 } 30 31 public static function wpSetUpBeforeClass( $factory ) { 32 self::$site_id = $factory->blog->create( array( 'domain' => 'wordpress.org', 'path' => '/' ) ); 33 self::$site_id2 = $factory->blog->create( array( 'domain' => 'wordpress.org', 'path' => '/foo/' ) ); 34 } 35 36 public static function wpTearDownAfterClass() { 37 wpmu_delete_blog( self::$site_id, true ); 38 wpmu_delete_blog( self::$site_id2, true ); 39 40 wp_update_network_site_counts(); 41 delete_site_option( 'site_meta_supported'); 42 } 43 44 public function test_add() { 45 $this->assertNotEmpty( add_site_meta( self::$site_id, 'foo', 'bar' ) ); 46 } 47 48 public function test_add_unique() { 49 $this->assertNotEmpty( add_site_meta( self::$site_id, 'foo', 'bar' ) ); 50 $this->assertFalse( add_site_meta( self::$site_id, 'foo', 'bar', true ) ); 51 } 52 53 public function test_delete() { 54 add_site_meta( self::$site_id, 'foo', 'bar' ); 55 56 $this->assertTrue( delete_site_meta( self::$site_id, 'foo' ) ); 57 } 58 59 public function test_delete_with_invalid_meta_key_should_return_false() { 60 $this->assertFalse( delete_site_meta( self::$site_id, 'foo' ) ); 61 } 62 63 public function test_delete_should_respect_meta_value() { 64 add_site_meta( self::$site_id, 'foo', 'bar' ); 65 add_site_meta( self::$site_id, 'foo', 'baz' ); 66 67 $this->assertTrue( delete_site_meta( self::$site_id, 'foo', 'bar' ) ); 68 69 $metas = get_site_meta( self::$site_id, 'foo', false ); 70 $this->assertSame( array( 'baz' ), $metas ); 71 } 72 73 public function test_get_with_no_key_should_fetch_all_keys() { 74 add_site_meta( self::$site_id, 'foo', 'bar' ); 75 add_site_meta( self::$site_id, 'foo1', 'baz' ); 76 77 $found = get_site_meta( self::$site_id ); 78 $expected = array( 79 'foo' => array( 'bar' ), 80 'foo1' => array( 'baz' ), 81 ); 82 83 $this->assertEqualSets( $expected, $found ); 84 } 85 86 public function test_get_with_key_should_fetch_all_for_key() { 87 add_site_meta( self::$site_id, 'foo', 'bar' ); 88 add_site_meta( self::$site_id, 'foo', 'baz' ); 89 add_site_meta( self::$site_id, 'foo1', 'baz' ); 90 91 $found = get_site_meta( self::$site_id, 'foo' ); 92 $expected = array( 'bar', 'baz' ); 93 94 $this->assertEqualSets( $expected, $found ); 95 } 96 97 public function test_get_should_respect_single_true() { 98 add_site_meta( self::$site_id, 'foo', 'bar' ); 99 add_site_meta( self::$site_id, 'foo', 'baz' ); 100 101 $found = get_site_meta( self::$site_id, 'foo', true ); 102 $this->assertEquals( 'bar', $found ); 103 } 104 105 public function test_update_should_pass_to_add_when_no_value_exists_for_key() { 106 $actual = update_site_meta( self::$site_id, 'foo', 'bar' ); 107 $this->assertInternalType( 'int', $actual ); 108 $this->assertNotEmpty( $actual ); 109 110 $meta = get_site_meta( self::$site_id, 'foo', true ); 111 $this->assertSame( 'bar', $meta ); 112 } 113 114 public function test_update_should_return_true_when_updating_existing_value_for_key() { 115 add_site_meta( self::$site_id, 'foo', 'bar' ); 116 117 $actual = update_site_meta( self::$site_id, 'foo', 'baz' ); 118 $this->assertTrue( $actual ); 119 120 $meta = get_site_meta( self::$site_id, 'foo', true ); 121 $this->assertSame( 'baz', $meta ); 122 } 123 124 public function test_delete_by_key() { 125 add_site_meta( self::$site_id, 'unique_delete_by_key', 'value', true ); 126 add_site_meta( self::$site_id2, 'unique_delete_by_key', 'value', true ); 127 128 $this->assertEquals( 'value', get_site_meta( self::$site_id, 'unique_delete_by_key', true ) ); 129 $this->assertEquals( 'value', get_site_meta( self::$site_id2, 'unique_delete_by_key', true ) ); 130 131 $this->assertTrue( delete_site_meta_by_key( 'unique_delete_by_key' ) ); 132 133 $this->assertEquals( '', get_site_meta( self::$site_id, 'unique_delete_by_key', true ) ); 134 $this->assertEquals( '', get_site_meta( self::$site_id2, 'unique_delete_by_key', true ) ); 135 } 136 137 public function test_site_meta_should_be_deleted_when_site_is_deleted() { 138 $site_id = self::factory()->blog->create( array( 'domain' => 'foo.org', 'path' => '/' ) ); 139 140 add_site_meta( $site_id, 'foo', 'bar' ); 141 add_site_meta( $site_id, 'foo1', 'bar' ); 142 143 $this->assertSame( 'bar', get_site_meta( $site_id, 'foo', true ) ); 144 $this->assertSame( 'bar', get_site_meta( $site_id, 'foo1', true ) ); 145 146 wpmu_delete_blog( $site_id, true ); 147 148 $this->assertSame( '', get_site_meta( $site_id, 'foo', true ) ); 149 $this->assertSame( '', get_site_meta( $site_id, 'foo1', true ) ); 150 } 151 152 public function test_disabled_site_meta_by_filter(){ 153 $site_id = self::factory()->blog->create( array( 'domain' => 'foo.org', 'path' => '/' ) ); 154 add_filter( 'pre_site_option_site_meta_supported', '__return_null' ); 155 156 $this->assertSame( null, get_site_option('site_meta_supported') ); 157 $this->assertSame( false, add_site_meta( $site_id, 'wibble', 'test1' ) ); 158 $this->assertSame( false, update_site_meta( $site_id, 'wibble', 'test2' ) ); 159 $this->assertSame( false, get_site_meta( $site_id, 'wibble' ) ); 160 $this->assertSame( false, delete_site_meta( $site_id, 'wibble' ) ); 161 162 remove_filter( 'pre_site_option_site_meta_supported', '__return_null' ); 163 } 164 } 165 166 endif;