Ticket #14511: 14511_offset.diff
File 14511_offset.diff, 2.9 KB (added by , 11 years ago) |
---|
-
src/wp-includes/ms-functions.php
2007 2007 * @type int 'spam' Retrieve spam or non-spam sites. Default null, for any. 2008 2008 * @type int 'deleted' Retrieve deleted or non-deleted sites. Default null, for any. 2009 2009 * @type int 'limit' Number of sites to limit the query to. Default 100. 2010 * @type int 'offset' Retrieve sites, excluding the first x sites. Used in combination with the limit parameter. Default 0. 2010 2011 * } 2011 2012 * 2012 2013 * @return array An array of site data … … 2025 2026 'spam' => null, 2026 2027 'deleted' => null, 2027 2028 'limit' => 100, 2029 'offset' => 0, 2028 2030 ); 2029 2031 2030 2032 $args = wp_parse_args( $args, $defaults ); … … 2052 2054 if ( isset( $args['deleted'] ) ) 2053 2055 $query .= $wpdb->prepare( "AND deleted = %d ", $args['deleted'] ); 2054 2056 2055 if ( isset( $args['limit'] ) ) 2056 $query .= $wpdb->prepare( "LIMIT %d ", $args['limit'] ); 2057 if ( isset( $args['limit'] ) && intval( $args['limit'] ) > 0 ) { 2058 if ( isset( $args['offset'] ) && intval( $args['offset'] ) > 0 ) { 2059 $query .= $wpdb->prepare( "LIMIT %d , %d ", $args['offset'], $args['limit'] ); 2060 } else { 2061 $query .= $wpdb->prepare( "LIMIT %d ", $args['limit'] ); 2062 } 2063 } 2057 2064 2058 2065 $site_results = $wpdb->get_results( $query, ARRAY_A ); 2059 2066 -
tests/phpunit/tests/ms.php
1033 1033 * @ticket 14511 1034 1034 */ 1035 1035 function test_wp_get_sites() { 1036 global $wpdb;1037 1036 $this->factory->blog->create_many( 2, array( 'site_id' => 2, 'meta' => array( 'public' => 1 ) ) ); 1038 1037 $this->factory->blog->create_many( 3, array( 'site_id' => 3, 'meta' => array( 'public' => 0 ) ) ); 1039 1038 … … 1069 1068 } 1070 1069 1071 1070 /** 1071 * @ticket 14511 1072 */ 1073 function test_wp_get_sites_limit_offset() { 1074 // Create 4 more sites (in addition to the default one) 1075 $this->factory->blog->create_many( 4, array( 'meta' => array( 'public' => 1 ) ) ); 1076 1077 // Expect all 5 sites when no limit/offset is specified 1078 $this->assertCount( 5, wp_get_sites() ); 1079 1080 // Expect first 2 sites when using limit 1081 $this->assertCount( 2, wp_get_sites( array( 'limit' => 2 ) ) ); 1082 1083 // Expect only the last 3 sites when using offset of 2 (limit will default to 100) 1084 $this->assertCount( 3, wp_get_sites( array( 'offset' => 2 ) ) ); 1085 1086 // Expect only the last 1 site when using offset of 4 and limit of 2 1087 $this->assertCount( 1, wp_get_sites( array( 'limit' => 2, 'offset' => 4 ) ) ); 1088 1089 // Expect 0 sites when using an offset larger than the number of sites 1090 $this->assertCount( 0, wp_get_sites( array( 'offset' => 20 ) ) ); 1091 } 1092 1093 /** 1072 1094 * Test the 'archived' argument for wp_get_sites(). 1073 1095 * 1074 1096 * archived is an ENUM, not an integer field.