diff --git a/wp-admin/includes/class-wp-ms-sites-list-table.php b/wp-admin/includes/class-wp-ms-sites-list-table.php index 74a961b8e6..43652e68a6 100644 --- a/wp-admin/includes/class-wp-ms-sites-list-table.php +++ b/wp-admin/includes/class-wp-ms-sites-list-table.php @@ -38,8 +38,6 @@ class WP_MS_Sites_List_Table extends WP_List_Table { $s = trim($s, '*'); } - $like_s = esc_sql( like_escape( $s ) ); - // If the network is large and a search is not being performed, show only the latest blogs with no paging in order // to avoid expensive count queries. if ( !$s && wp_is_large_network() ) { @@ -58,7 +56,8 @@ class WP_MS_Sites_List_Table extends WP_List_Table { preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) || preg_match( '/^[0-9]{1,3}\.$/', $s ) ) { // IPv4 address - $reg_blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE ( '{$like_s}$wild' )" ); + $sql = $wpdb->prepare( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s", $wpdb->esc_like( $s ) . $wild ); + $reg_blog_ids = $wpdb->get_col( $sql ); if ( !$reg_blog_ids ) $reg_blog_ids = array( 0 ); @@ -69,17 +68,18 @@ class WP_MS_Sites_List_Table extends WP_List_Table { AND {$wpdb->blogs}.blog_id IN (" . implode( ', ', $reg_blog_ids ) . ")"; } else { if ( is_numeric($s) && empty( $wild ) ) { - $query .= " AND ( {$wpdb->blogs}.blog_id = '{$like_s}' )"; + $query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.blog_id = %s )", $s ); } elseif ( is_subdomain_install() ) { - $blog_s = str_replace( '.' . $current_site->domain, '', $like_s ); - $blog_s .= $wild . '.' . $current_site->domain; - $query .= " AND ( {$wpdb->blogs}.domain LIKE '$blog_s' ) "; + $blog_s = str_replace( '.' . $current_site->domain, '', $s ); + $blog_s = $wpdb->esc_like( $blog_s ) . $wild . $wpdb->esc_like( '.' . $current_site->domain ); + $query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.domain LIKE %s ) ", $blog_s ); } else { - if ( $like_s != trim('/', $current_site->path) ) - $blog_s = $current_site->path . $like_s . $wild . '/'; - else - $blog_s = $like_s; - $query .= " AND ( {$wpdb->blogs}.path LIKE '$blog_s' )"; + if ( $s != trim('/', $current_site->path) ) { + $blog_s = $wpdb->esc_like( $current_site->path . $s ) . $wild . $wpdb->esc_like( '/' ); + } else { + $blog_s = $wpdb->esc_like( $s ); + } + $query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.path LIKE %s )", $blog_s ); } } diff --git a/wp-admin/includes/schema.php b/wp-admin/includes/schema.php index 70ae6a96b9..b88a7ab6f9 100644 --- a/wp-admin/includes/schema.php +++ b/wp-admin/includes/schema.php @@ -553,19 +553,21 @@ function populate_options() { // The multi-table delete syntax is used to delete the transient record from table a, // and the corresponding transient_timeout record from table b. $time = time(); - $wpdb->query("DELETE a, b FROM $wpdb->options a, $wpdb->options b WHERE - a.option_name LIKE '\_transient\_%' AND - a.option_name NOT LIKE '\_transient\_timeout\_%' AND - b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) ) - AND b.option_value < $time"); + $sql = "DELETE a, b FROM $wpdb->options a, $wpdb->options b + WHERE a.option_name LIKE %s + AND a.option_name NOT LIKE %s + AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) ) + AND b.option_value < %d"; + $wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( '_transient_' ) . '%', $wpdb->esc_like( '_transient_timeout_' ) . '%', $time ) ); if ( is_main_site() && is_main_network() ) { - $wpdb->query("DELETE a, b FROM $wpdb->options a, $wpdb->options b WHERE - a.option_name LIKE '\_site\_transient\_%' AND - a.option_name NOT LIKE '\_site\_transient\_timeout\_%' AND - b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) ) - AND b.option_value < $time"); - } + $sql = "DELETE a, b FROM $wpdb->options a, $wpdb->options b + WHERE a.option_name LIKE %s + AND a.option_name NOT LIKE %s + AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) ) + AND b.option_value < %d"; + $wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( '_site_transient_' ) . '%', $wpdb->esc_like( '_site_transient_timeout_' ) . '%', $time ) ); + } } /** diff --git a/wp-admin/includes/template.php b/wp-admin/includes/template.php index f43b0b6c83..98bba48de0 100644 --- a/wp-admin/includes/template.php +++ b/wp-admin/includes/template.php @@ -632,14 +632,14 @@ function meta_form( $post = null ) { * * @param int $limit Number of custom fields to retrieve. Default 30. */ - $limit = (int) apply_filters( 'postmeta_form_limit', 30 ); - $keys = $wpdb->get_col( " - SELECT meta_key + $limit = apply_filters( 'postmeta_form_limit', 30 ); + $sql = "SELECT meta_key FROM $wpdb->postmeta GROUP BY meta_key - HAVING meta_key NOT LIKE '\_%' + HAVING meta_key NOT LIKE %s ORDER BY meta_key - LIMIT $limit" ); + LIMIT %d"; + $keys = $wpdb->get_col( $wpdb->prepare( $sql, $wpdb->esc_like( '_' ) . '%', $limit ) ); if ( $keys ) { natcasesort( $keys ); $meta_key_input_id = 'metakeyselect'; diff --git a/wp-admin/includes/upgrade.php b/wp-admin/includes/upgrade.php index 558b88e60b..72c67b4e9d 100644 --- a/wp-admin/includes/upgrade.php +++ b/wp-admin/includes/upgrade.php @@ -465,9 +465,11 @@ function upgrade_100() { } } - $wpdb->query("UPDATE $wpdb->options SET option_value = REPLACE(option_value, 'wp-links/links-images/', 'wp-images/links/') - WHERE option_name LIKE 'links_rating_image%' - AND option_value LIKE 'wp-links/links-images/%'"); + $sql = "UPDATE $wpdb->options + SET option_value = REPLACE(option_value, 'wp-links/links-images/', 'wp-images/links/') + WHERE option_name LIKE %s + AND option_value LIKE %s"; + $wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( 'links_rating_image' ) . '%', $wpdb->esc_like( 'wp-links/links-images/' ) . '%' ) ); $done_ids = $wpdb->get_results("SELECT DISTINCT post_id FROM $wpdb->post2cat"); if ($done_ids) : @@ -1100,9 +1102,28 @@ function upgrade_300() { // 3.0 screen options key name changes. if ( is_main_site() && !defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) { - $prefix = like_escape($wpdb->base_prefix); - $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key LIKE '{$prefix}%meta-box-hidden%' OR meta_key LIKE '{$prefix}%closedpostboxes%' OR meta_key LIKE '{$prefix}%manage-%-columns-hidden%' OR meta_key LIKE '{$prefix}%meta-box-order%' OR meta_key LIKE '{$prefix}%metaboxorder%' OR meta_key LIKE '{$prefix}%screen_layout%' - OR meta_key = 'manageedittagscolumnshidden' OR meta_key='managecategoriescolumnshidden' OR meta_key = 'manageedit-tagscolumnshidden' OR meta_key = 'manageeditcolumnshidden' OR meta_key = 'categories_per_page' OR meta_key = 'edit_tags_per_page'" ); + $sql = "DELETE FROM $wpdb->usermeta + WHERE meta_key LIKE %s + OR meta_key LIKE %s + OR meta_key LIKE %s + OR meta_key LIKE %s + OR meta_key LIKE %s + OR meta_key LIKE %s + OR meta_key = 'manageedittagscolumnshidden' + OR meta_key = 'managecategoriescolumnshidden' + OR meta_key = 'manageedit-tagscolumnshidden' + OR meta_key = 'manageeditcolumnshidden' + OR meta_key = 'categories_per_page' + OR meta_key = 'edit_tags_per_page'"; + $prefix = $wpdb->esc_like( $wpdb->base_prefix ); + $wpdb->query( $wpdb->prepare( $sql, + $prefix . '%' . $wpdb->esc_like( 'meta-box-hidden' ) . '%', + $prefix . '%' . $wpdb->esc_like( 'closedpostboxes' ) . '%', + $prefix . '%' . $wpdb->esc_like( 'manage-' ) . '%' . $wpdb->esc_like( '-columns-hidden' ) . '%', + $prefix . '%' . $wpdb->esc_like( 'meta-box-order' ) . '%', + $prefix . '%' . $wpdb->esc_like( 'metaboxorder' ) . '%', + $prefix . '%' . $wpdb->esc_like( 'screen_layout' ) . '%' + ) ); } } @@ -1284,11 +1305,12 @@ function upgrade_network() { // The multi-table delete syntax is used to delete the transient record from table a, // and the corresponding transient_timeout record from table b. $time = time(); - $wpdb->query("DELETE a, b FROM $wpdb->sitemeta a, $wpdb->sitemeta b WHERE - a.meta_key LIKE '\_site\_transient\_%' AND - a.meta_key NOT LIKE '\_site\_transient\_timeout\_%' AND - b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) ) - AND b.meta_value < $time"); + $sql = "DELETE a, b FROM $wpdb->sitemeta a, $wpdb->sitemeta b + WHERE a.meta_key LIKE %s + AND a.meta_key NOT LIKE %s + AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) ) + AND b.meta_value < %d"; + $wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( '_site_transient_' ) . '%', $wpdb->esc_like ( '_site_transient_timeout_' ) . '%', $time ) ); } // 2.8 @@ -1382,13 +1404,18 @@ function upgrade_network() { */ function maybe_create_table($table_name, $create_ddl) { global $wpdb; - if ( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name ) + + $query = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $table_name ) ); + + if ( $wpdb->get_var( $query ) == $table_name ) { return true; + } //didn't find it try to create it. $wpdb->query($create_ddl); // we cannot directly tell that whether this succeeded! - if ( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name ) + if ( $wpdb->get_var( $query ) == $table_name ) { return true; + } return false; } diff --git a/wp-admin/install.php b/wp-admin/install.php index d363e2db7d..fdfcbdd62d 100644 --- a/wp-admin/install.php +++ b/wp-admin/install.php @@ -74,7 +74,9 @@ function display_header() { */ function display_setup_form( $error = null ) { global $wpdb; - $user_table = ( $wpdb->get_var("SHOW TABLES LIKE '$wpdb->users'") != null ); + + $sql = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $wpdb->users ) ); + $user_table = ( $wpdb->get_var( $sql ) != null ); // Ensure that Blogs appear in search engines by default $blog_public = 1; diff --git a/wp-admin/maint/repair.php b/wp-admin/maint/repair.php index 361229f555..db40a7aae7 100644 --- a/wp-admin/maint/repair.php +++ b/wp-admin/maint/repair.php @@ -36,8 +36,10 @@ if ( ! defined( 'WP_ALLOW_REPAIR' ) ) { $tables = $wpdb->tables(); // Sitecategories may not exist if global terms are disabled. - if ( is_multisite() && ! $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->sitecategories'" ) ) + $query = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $wpdb->sitecategories ) ); + if ( is_multisite() && ! $wpdb->get_var( $query ) ) { unset( $tables['sitecategories'] ); + } /** * Filter additional database tables to repair. diff --git a/wp-admin/network.php b/wp-admin/network.php index 27acf98fe5..e848009cb5 100644 --- a/wp-admin/network.php +++ b/wp-admin/network.php @@ -39,8 +39,11 @@ foreach ( $wpdb->tables( 'ms_global' ) as $table => $prefixed_table ) */ function network_domain_check() { global $wpdb; - if ( $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->site'" ) ) + + $sql = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $wpdb->site ) ); + if ( $wpdb->get_var( $sql ) ) { return $wpdb->get_var( "SELECT domain FROM $wpdb->site ORDER BY id ASC LIMIT 1" ); + } return false; } diff --git a/wp-admin/network/site-settings.php b/wp-admin/network/site-settings.php index aeca875e50..1decdcbec1 100644 --- a/wp-admin/network/site-settings.php +++ b/wp-admin/network/site-settings.php @@ -113,7 +113,14 @@ if ( ! empty( $messages ) ) {