Ticket #36675: 36675.4.diff
File 36675.4.diff, 11.4 KB (added by , 8 years ago) |
---|
-
src/wp-admin/includes/class-wp-ms-sites-list-table.php
83 83 $s = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST[ 's' ] ) ) : ''; 84 84 $wild = ''; 85 85 if ( false !== strpos($s, '*') ) { 86 $wild = ' %';86 $wild = '*'; 87 87 $s = trim($s, '*'); 88 88 } 89 89 … … 98 98 $_GET['order'] = $_REQUEST['order'] = 'DESC'; 99 99 } 100 100 101 $query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' "; 101 $args = array( 102 'number' => intval( $per_page ), 103 'offset' => intval( ( $pagenum - 1 ) * $per_page ), 104 'network_id' => $current_site->id, 105 ); 102 106 103 107 if ( empty($s) ) { 104 108 // Nothing to do. … … 107 111 preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) || 108 112 preg_match( '/^[0-9]{1,3}\.$/', $s ) ) { 109 113 // IPv4 address 110 $sql = $wpdb->prepare( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s", $wpdb->esc_like( $s ) . $wild);114 $sql = $wpdb->prepare( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s", $wpdb->esc_like( $s ) . ( ! empty( $wild ) ? '%' : '' ) ); 111 115 $reg_blog_ids = $wpdb->get_col( $sql ); 112 116 113 if ( !$reg_blog_ids ) 114 $reg_blog_ids = array( 0 ); 117 if ( $reg_blog_ids ) { 118 $args['site__in'] = $reg_blog_ids; 119 } 120 } elseif ( is_numeric( $s ) && empty( $wild ) ) { 121 $args['ID'] = $s; 122 } else { 123 $args['search'] = $s; 115 124 116 $query = "SELECT * 117 FROM {$wpdb->blogs} 118 WHERE site_id = '{$wpdb->siteid}' 119 AND {$wpdb->blogs}.blog_id IN (" . implode( ', ', $reg_blog_ids ) . ")"; 120 } else { 121 if ( is_numeric($s) && empty( $wild ) ) { 122 $query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.blog_id = %s )", $s ); 123 } elseif ( is_subdomain_install() ) { 124 $blog_s = str_replace( '.' . $current_site->domain, '', $s ); 125 $blog_s = $wpdb->esc_like( $blog_s ) . $wild . $wpdb->esc_like( '.' . $current_site->domain ); 126 $query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.domain LIKE %s ) ", $blog_s ); 127 } else { 128 if ( $s != trim('/', $current_site->path) ) { 129 $blog_s = $wpdb->esc_like( $current_site->path . $s ) . $wild . $wpdb->esc_like( '/' ); 130 } else { 131 $blog_s = $wpdb->esc_like( $s ); 132 } 133 $query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.path LIKE %s )", $blog_s ); 125 if ( ! is_subdomain_install() ) { 126 $args['search_columns'] = array( 'path' ); 134 127 } 135 128 } 136 129 137 130 $order_by = isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : ''; 138 if ( $order_by === 'registered') {139 $query .= ' ORDER BY registered ';140 } elseif ( $order_by === 'lastupdated') {141 $ query .= ' ORDER BY last_updated';142 } elseif ( $order_by === 'blogname') {131 if ( 'registered' === $order_by ) { 132 // registered is named properly. 133 } elseif ( 'lastupdated' === $order_by ) { 134 $order_by = 'last_updated'; 135 } elseif ( 'blogname' === $order_by ) { 143 136 if ( is_subdomain_install() ) { 144 $ query .= ' ORDER BY domain';137 $order_by = 'domain'; 145 138 } else { 146 $ query .= ' ORDER BY path';139 $order_by = 'path'; 147 140 } 148 } elseif ( $order_by === 'blog_id') {149 $ query .= ' ORDER BY blog_id';150 } else {151 $order_by = null;141 } elseif ( 'blog_id' === $order_by ) { 142 $order_by = 'id'; 143 } elseif ( ! $order_by ) { 144 $order_by = false; 152 145 } 153 146 154 if ( isset( $order_by ) ) { 155 $order = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? "DESC" : "ASC"; 156 $query .= $order; 147 $args['orderby'] = $order_by; 148 149 if ( $order_by ) { 150 $args['order'] = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? "DESC" : "ASC"; 157 151 } 158 152 159 // Don't do an unbounded count on large networks 160 if ( ! wp_is_large_network() ) 161 $total = $wpdb->get_var( str_replace( 'SELECT *', 'SELECT COUNT( blog_id )', $query ) ); 153 if ( wp_is_large_network() ) { 154 $args['no_found_rows'] = true; 155 } else { 156 $args['no_found_rows'] = false; 157 } 162 158 163 $query .= " LIMIT " . intval( ( $pagenum - 1 ) * $per_page ) . ", " . intval( $per_page ); 164 $this->items = $wpdb->get_results( $query, ARRAY_A ); 159 $_sites = get_sites( $args ); 160 if ( is_array( $_sites ) ) { 161 update_site_cache( $_sites ); 165 162 166 if ( wp_is_large_network() )167 $total = count($this->items);163 $this->items = array_slice( $_sites, 0, $per_page ); 164 } 168 165 166 $total_sites = get_sites( array_merge( $args, array( 167 'count' => true, 168 'offset' => 0, 169 'number' => 0, 170 ) ) ); 171 169 172 $this->set_pagination_args( array( 170 'total_items' => $total ,173 'total_items' => $total_sites, 171 174 'per_page' => $per_page, 172 175 ) ); 173 176 } … … 445 448 */ 446 449 public function display_rows() { 447 450 foreach ( $this->items as $blog ) { 451 $blog = $blog->to_array(); 448 452 $class = ''; 449 453 reset( $this->status_list ); 450 454 -
src/wp-includes/class-wp-site-query.php
141 141 * @type int $spam Limit results to spam sites. Accepts '1' or '0'. Default empty. 142 142 * @type int $deleted Limit results to deleted sites. Accepts '1' or '0'. Default empty. 143 143 * @type string $search Search term(s) to retrieve matching sites for. Default empty. 144 * @type array $search_columns Array of column names to be searched. Accepts 'domain' and 'path'. 145 * Default empty array. 144 146 * @type bool $update_site_cache Whether to prime the cache for found sites. Default false. 145 147 * } 146 148 */ … … 170 172 'spam' => null, 171 173 'deleted' => null, 172 174 'search' => '', 175 'search_columns' => array(), 173 176 'count' => false, 174 177 'date_query' => null, // See WP_Date_Query 175 178 'update_site_cache' => true, … … 481 484 482 485 // Falsey search strings are ignored. 483 486 if ( strlen( $this->query_vars['search'] ) ) { 484 $this->sql_clauses['where']['search'] = $this->get_search_sql( 485 $this->query_vars['search'], 486 array( 'domain', 'path' ) 487 ); 487 $search_columns = array(); 488 489 if ( $this->query_vars['search_columns'] ) { 490 $search_columns = array_intersect( $this->query_vars['search_columns'], array( 'domain', 'path' ) ); 491 } 492 493 if ( ! $search_columns ) { 494 $search_columns = array( 'domain', 'path' ); 495 } 496 497 /** 498 * Filters the columns to search in a WP_Site_Query search. 499 * 500 * The default columns include 'domain' and 'path. 501 * 502 * @since 4.6.0 503 * 504 * @param array $search_columns Array of column names to be searched. 505 * @param string $search Text being searched. 506 * @param WP_Site_Query $this The current WP_Site_Query instance. 507 */ 508 $search_columns = apply_filters( 'site_search_columns', $search_columns, $this->query_vars['search'], $this ); 509 510 $this->sql_clauses['where']['search'] = $this->get_search_sql( $this->query_vars['search'], $search_columns ); 488 511 } 489 512 490 513 $date_query = $this->query_vars['date_query']; … … 563 586 protected function get_search_sql( $string, $columns ) { 564 587 global $wpdb; 565 588 566 $like = '%' . $wpdb->esc_like( $string ) . '%'; 589 if ( false !== strpos( $string, '*' ) ) { 590 $like = '%' . implode( '%', array_map( array( $wpdb, 'esc_like' ), explode( '*', $string ) ) ) . '%'; 591 } else { 592 $like = '%' . $wpdb->esc_like( $string ) . '%'; 593 } 567 594 568 595 $searches = array(); 569 596 foreach ( $columns as $column ) { -
tests/phpunit/tests/multisite/siteQuery.php
470 470 471 471 $this->assertEquals( $expected, $found ); 472 472 } 473 474 public function test_wp_site_query_by_search_with_text_in_path_exclude_domain_from_search() { 475 $q = new WP_Site_Query(); 476 $found = $q->query( array( 477 'fields' => 'ids', 478 'search' => 'make', 479 'search_columns' => array( 'path' ), 480 ) ); 481 482 $expected = array( 483 self::$site_ids['www.w.org/make/'], 484 ); 485 486 $this->assertEquals( $expected, $found ); 487 } 488 489 public function test_wp_site_query_by_search_with_text_in_domain_exclude_path_from_search() { 490 $q = new WP_Site_Query(); 491 $found = $q->query( array( 492 'fields' => 'ids', 493 'search' => 'make', 494 'search_columns' => array( 'domain' ), 495 ) ); 496 497 $expected = array( 498 self::$site_ids['make.wordpress.org/'], 499 self::$site_ids['make.wordpress.org/foo/'], 500 ); 501 502 $this->assertEquals( $expected, $found ); 503 } 504 505 public function test_wp_site_query_by_search_with_wildcard_in_text() { 506 $q = new WP_Site_Query(); 507 $found = $q->query( array( 508 'fields' => 'ids', 509 'search' => 'm*ke', 510 ) ); 511 512 $expected = array( 513 self::$site_ids['www.w.org/make/'], 514 self::$site_ids['make.wordpress.org/'], 515 self::$site_ids['make.wordpress.org/foo/'], 516 ); 517 518 $this->assertEqualSets( $expected, $found ); 519 } 520 521 public function test_wp_site_query_by_search_with_wildcard_in_text_exclude_path_from_search() { 522 $q = new WP_Site_Query(); 523 $found = $q->query( array( 524 'fields' => 'ids', 525 'search' => 'm*ke', 526 'search_columns' => array( 'domain' ), 527 ) ); 528 529 $expected = array( 530 self::$site_ids['make.wordpress.org/'], 531 self::$site_ids['make.wordpress.org/foo/'], 532 ); 533 534 $this->assertEqualSets( $expected, $found ); 535 } 536 537 public function test_wp_site_query_by_search_with_wildcard_in_text_exclude_domain_from_search() { 538 $q = new WP_Site_Query(); 539 $found = $q->query( array( 540 'fields' => 'ids', 541 'search' => 'm*ke', 542 'search_columns' => array( 'path' ), 543 ) ); 544 545 $expected = array( 546 self::$site_ids['www.w.org/make/'], 547 ); 548 549 $this->assertEqualSets( $expected, $found ); 550 } 473 551 } 474 552 475 553 endif; -
tests/phpunit/tests/multisite/wpMSSitesListTable.php
73 73 74 74 $expected = array( 75 75 self::$site_ids['wordpress.org/foo/'], 76 self::$site_ids['wordpress.org/foo/bar/'], 77 self::$site_ids['wordpress.org/afoo/'], 76 78 self::$site_ids['make.wordpress.org/foo/'], 77 79 self::$site_ids['www.w.org/foo/'], 80 self::$site_ids['www.w.org/foo/bar/'], 78 81 ); 79 82 80 83 $this->assertEqualSets( $expected, $items ); … … 131 134 132 135 $expected = array( 133 136 self::$site_ids['test.example.org/'], 137 self::$site_ids['test2.example.org/'], 138 self::$site_ids['test3.example.org/zig/'], 139 self::$site_ids['atest.example.org/'], 134 140 ); 135 141 136 142 $this->assertEqualSets( $expected, $items ); … … 154 160 self::$site_ids['test.example.org/'], 155 161 self::$site_ids['test2.example.org/'], 156 162 self::$site_ids['test3.example.org/zig/'], 163 self::$site_ids['atest.example.org/'], 157 164 ); 158 165 159 166 $this->assertEqualSets( $expected, $items ); … … 176 183 $expected = array( 177 184 self::$site_ids['wordpress.org/foo/'], 178 185 self::$site_ids['wordpress.org/foo/bar/'], 186 self::$site_ids['wordpress.org/afoo/'], 179 187 self::$site_ids['make.wordpress.org/foo/'], 180 188 self::$site_ids['www.w.org/foo/'], 181 189 self::$site_ids['www.w.org/foo/bar/'],