Code is Poetry.

WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.


Built from https://develop.svn.wordpress.org/trunk@42343


git-svn-id: http://core.svn.wordpress.org/trunk@42172 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Gary Pendergast
2017-11-30 23:11:00 +00:00
parent 62be7ad218
commit aaf99e6913
610 changed files with 55731 additions and 43711 deletions

View File

@@ -58,11 +58,13 @@ function get_blogaddress_by_id( $blog_id ) {
*/
function get_blogaddress_by_name( $blogname ) {
if ( is_subdomain_install() ) {
if ( $blogname == 'main' )
if ( $blogname == 'main' ) {
$blogname = 'www';
}
$url = rtrim( network_home_url(), '/' );
if ( !empty( $blogname ) )
$url = preg_replace( '|^([^\.]+://)|', "\${1}" . $blogname . '.', $url );
if ( ! empty( $blogname ) ) {
$url = preg_replace( '|^([^\.]+://)|', '${1}' . $blogname . '.', $url );
}
} else {
$url = network_home_url( $blogname );
}
@@ -80,22 +82,24 @@ function get_blogaddress_by_name( $blogname ) {
*/
function get_id_from_blogname( $slug ) {
$current_network = get_network();
$slug = trim( $slug, '/' );
$slug = trim( $slug, '/' );
if ( is_subdomain_install() ) {
$domain = $slug . '.' . preg_replace( '|^www\.|', '', $current_network->domain );
$path = $current_network->path;
$path = $current_network->path;
} else {
$domain = $current_network->domain;
$path = $current_network->path . $slug . '/';
$path = $current_network->path . $slug . '/';
}
$site_ids = get_sites( array(
'number' => 1,
'fields' => 'ids',
'domain' => $domain,
'path' => $path,
) );
$site_ids = get_sites(
array(
'number' => 1,
'fields' => 'ids',
'domain' => $domain,
'path' => $path,
)
);
if ( empty( $site_ids ) ) {
return null;
@@ -120,39 +124,41 @@ function get_id_from_blogname( $slug ) {
function get_blog_details( $fields = null, $get_all = true ) {
global $wpdb;
if ( is_array($fields ) ) {
if ( isset($fields['blog_id']) ) {
if ( is_array( $fields ) ) {
if ( isset( $fields['blog_id'] ) ) {
$blog_id = $fields['blog_id'];
} elseif ( isset($fields['domain']) && isset($fields['path']) ) {
$key = md5( $fields['domain'] . $fields['path'] );
$blog = wp_cache_get($key, 'blog-lookup');
if ( false !== $blog )
} elseif ( isset( $fields['domain'] ) && isset( $fields['path'] ) ) {
$key = md5( $fields['domain'] . $fields['path'] );
$blog = wp_cache_get( $key, 'blog-lookup' );
if ( false !== $blog ) {
return $blog;
}
if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
$nowww = substr( $fields['domain'], 4 );
$blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) AND path = %s ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'], $fields['path'] ) );
$blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) AND path = %s ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'], $fields['path'] ) );
} else {
$blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
}
if ( $blog ) {
wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
wp_cache_set( $blog->blog_id . 'short', $blog, 'blog-details' );
$blog_id = $blog->blog_id;
} else {
return false;
}
} elseif ( isset($fields['domain']) && is_subdomain_install() ) {
$key = md5( $fields['domain'] );
$blog = wp_cache_get($key, 'blog-lookup');
if ( false !== $blog )
} elseif ( isset( $fields['domain'] ) && is_subdomain_install() ) {
$key = md5( $fields['domain'] );
$blog = wp_cache_get( $key, 'blog-lookup' );
if ( false !== $blog ) {
return $blog;
}
if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
$nowww = substr( $fields['domain'], 4 );
$blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
$blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
} else {
$blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
}
if ( $blog ) {
wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
wp_cache_set( $blog->blog_id . 'short', $blog, 'blog-details' );
$blog_id = $blog->blog_id;
} else {
return false;
@@ -161,17 +167,18 @@ function get_blog_details( $fields = null, $get_all = true ) {
return false;
}
} else {
if ( ! $fields )
if ( ! $fields ) {
$blog_id = get_current_blog_id();
elseif ( ! is_numeric( $fields ) )
} elseif ( ! is_numeric( $fields ) ) {
$blog_id = get_id_from_blogname( $fields );
else
} else {
$blog_id = $fields;
}
}
$blog_id = (int) $blog_id;
$all = $get_all == true ? '' : 'short';
$all = $get_all == true ? '' : 'short';
$details = wp_cache_get( $blog_id . $all, 'blog-details' );
if ( $details ) {
@@ -181,7 +188,7 @@ function get_blog_details( $fields = null, $get_all = true ) {
} else {
// Clear old pre-serialized objects. Cache clients do better with that.
wp_cache_delete( $blog_id . $all, 'blog-details' );
unset($details);
unset( $details );
}
} else {
return $details;
@@ -201,7 +208,7 @@ function get_blog_details( $fields = null, $get_all = true ) {
} else {
// Clear old pre-serialized objects. Cache clients do better with that.
wp_cache_delete( $blog_id, 'blog-details' );
unset($details);
unset( $details );
}
} else {
return $details;
@@ -209,7 +216,7 @@ function get_blog_details( $fields = null, $get_all = true ) {
}
}
if ( empty($details) ) {
if ( empty( $details ) ) {
$details = WP_Site::get_instance( $blog_id );
if ( ! $details ) {
// Set the full cache.
@@ -282,23 +289,26 @@ function refresh_blog_details( $blog_id = 0 ) {
function update_blog_details( $blog_id, $details = array() ) {
global $wpdb;
if ( empty($details) )
if ( empty( $details ) ) {
return false;
}
if ( is_object($details) )
$details = get_object_vars($details);
if ( is_object( $details ) ) {
$details = get_object_vars( $details );
}
$current_details = get_site( $blog_id );
if ( empty($current_details) )
if ( empty( $current_details ) ) {
return false;
}
$current_details = get_object_vars($current_details);
$current_details = get_object_vars( $current_details );
$details = array_merge($current_details, $details);
$details['last_updated'] = current_time('mysql', true);
$details = array_merge( $current_details, $details );
$details['last_updated'] = current_time( 'mysql', true );
$update_details = array();
$fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
$fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) {
if ( 'path' === $field ) {
$details[ $field ] = trailingslashit( '/' . trim( $details[ $field ], '/' ) );
@@ -307,10 +317,11 @@ function update_blog_details( $blog_id, $details = array() ) {
$update_details[ $field ] = $details[ $field ];
}
$result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
$result = $wpdb->update( $wpdb->blogs, $update_details, array( 'blog_id' => $blog_id ) );
if ( false === $result )
if ( false === $result ) {
return false;
}
// If spam status changed, issue actions.
if ( $details['spam'] != $current_details['spam'] ) {
@@ -436,27 +447,29 @@ function clean_blog_cache( $blog ) {
}
$blog_id = $blog;
$blog = get_site( $blog_id );
$blog = get_site( $blog_id );
if ( ! $blog ) {
if ( ! is_numeric( $blog_id ) ) {
return;
}
// Make sure a WP_Site object exists even when the site has been deleted.
$blog = new WP_Site( (object) array(
'blog_id' => $blog_id,
'domain' => null,
'path' => null,
) );
$blog = new WP_Site(
(object) array(
'blog_id' => $blog_id,
'domain' => null,
'path' => null,
)
);
}
$blog_id = $blog->blog_id;
$blog_id = $blog->blog_id;
$domain_path_key = md5( $blog->domain . $blog->path );
wp_cache_delete( $blog_id, 'sites' );
wp_cache_delete( $blog_id, 'site-details' );
wp_cache_delete( $blog_id, 'blog-details' );
wp_cache_delete( $blog_id . 'short' , 'blog-details' );
wp_cache_delete( $blog_id . 'short', 'blog-details' );
wp_cache_delete( $domain_path_key, 'blog-lookup' );
wp_cache_delete( $domain_path_key, 'blog-id-cache' );
wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' );
@@ -559,7 +572,7 @@ function _prime_site_caches( $ids ) {
$non_cached_ids = _get_non_cached_ids( $ids, 'sites' );
if ( ! empty( $non_cached_ids ) ) {
$fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
$fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", join( ',', array_map( 'intval', $non_cached_ids ) ) ) );
update_site_cache( $fresh_sites );
}
@@ -665,11 +678,13 @@ function get_sites( $args = array() ) {
function get_blog_option( $id, $option, $default = false ) {
$id = (int) $id;
if ( empty( $id ) )
if ( empty( $id ) ) {
$id = get_current_blog_id();
}
if ( get_current_blog_id() == $id )
if ( get_current_blog_id() == $id ) {
return get_option( $option, $default );
}
switch_to_blog( $id );
$value = get_option( $option, $default );
@@ -710,11 +725,13 @@ function get_blog_option( $id, $option, $default = false ) {
function add_blog_option( $id, $option, $value ) {
$id = (int) $id;
if ( empty( $id ) )
if ( empty( $id ) ) {
$id = get_current_blog_id();
}
if ( get_current_blog_id() == $id )
if ( get_current_blog_id() == $id ) {
return add_option( $option, $value );
}
switch_to_blog( $id );
$return = add_option( $option, $value );
@@ -735,11 +752,13 @@ function add_blog_option( $id, $option, $value ) {
function delete_blog_option( $id, $option ) {
$id = (int) $id;
if ( empty( $id ) )
if ( empty( $id ) ) {
$id = get_current_blog_id();
}
if ( get_current_blog_id() == $id )
if ( get_current_blog_id() == $id ) {
return delete_option( $option );
}
switch_to_blog( $id );
$return = delete_option( $option );
@@ -762,11 +781,13 @@ function delete_blog_option( $id, $option ) {
function update_blog_option( $id, $option, $value, $deprecated = null ) {
$id = (int) $id;
if ( null !== $deprecated )
if ( null !== $deprecated ) {
_deprecated_argument( __FUNCTION__, '3.1.0' );
}
if ( get_current_blog_id() == $id )
if ( get_current_blog_id() == $id ) {
return update_option( $option, $value );
}
switch_to_blog( $id );
$return = update_option( $option, $value );
@@ -829,8 +850,8 @@ function switch_to_blog( $new_blog, $deprecated = null ) {
$wpdb->set_blog_id( $new_blog );
$GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
$prev_blog_id = $blog_id;
$GLOBALS['blog_id'] = $new_blog;
$prev_blog_id = $blog_id;
$GLOBALS['blog_id'] = $new_blog;
if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
wp_cache_switch_to_blog( $new_blog );
@@ -883,7 +904,7 @@ function restore_current_blog() {
return false;
}
$blog = array_pop( $GLOBALS['_wp_switched_stack'] );
$blog = array_pop( $GLOBALS['_wp_switched_stack'] );
$blog_id = get_current_blog_id();
if ( $blog_id == $blog ) {
@@ -895,8 +916,8 @@ function restore_current_blog() {
}
$wpdb->set_blog_id( $blog );
$prev_blog_id = $blog_id;
$GLOBALS['blog_id'] = $blog;
$prev_blog_id = $blog_id;
$GLOBALS['blog_id'] = $blog;
$GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
@@ -974,7 +995,7 @@ function ms_is_switched() {
* @return string Whether the blog is archived or not
*/
function is_archived( $id ) {
return get_blog_status($id, 'archived');
return get_blog_status( $id, 'archived' );
}
/**
@@ -987,7 +1008,7 @@ function is_archived( $id ) {
* @return string $archived
*/
function update_archived( $id, $archived ) {
update_blog_status($id, 'archived', $archived);
update_blog_status( $id, 'archived', $archived );
return $archived;
}
@@ -1007,16 +1028,24 @@ function update_archived( $id, $archived ) {
function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
global $wpdb;
if ( null !== $deprecated )
if ( null !== $deprecated ) {
_deprecated_argument( __FUNCTION__, '3.1.0' );
}
if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) ) {
return $value;
}
$result = $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
$result = $wpdb->update(
$wpdb->blogs, array(
$pref => $value,
'last_updated' => current_time( 'mysql', true ),
), array( 'blog_id' => $blog_id )
);
if ( false === $result )
if ( false === $result ) {
return false;
}
clean_blog_cache( $blog_id );
@@ -1060,7 +1089,7 @@ function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
*
* @param int $blog_id Blog ID.
* @param string $value The value of blog status.
*/
*/
do_action( 'update_blog_public', $blog_id, $value ); // Moved here from update_blog_public().
}
@@ -1082,10 +1111,11 @@ function get_blog_status( $id, $pref ) {
global $wpdb;
$details = get_site( $id );
if ( $details )
if ( $details ) {
return $details->$pref;
}
return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
return $wpdb->get_var( $wpdb->prepare( "SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id ) );
}
/**
@@ -1103,8 +1133,9 @@ function get_blog_status( $id, $pref ) {
function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
global $wpdb;
if ( ! empty( $deprecated ) )
if ( ! empty( $deprecated ) ) {
_deprecated_argument( __FUNCTION__, 'MU' ); // never used
}
return $wpdb->get_results( $wpdb->prepare( "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated != '0000-00-00 00:00:00' ORDER BY last_updated DESC limit %d, %d", get_current_network_id(), $start, $quantity ), ARRAY_A );
}
@@ -1232,8 +1263,8 @@ function _prime_network_caches( $network_ids ) {
global $wpdb;
$non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' );
if ( !empty( $non_cached_ids ) ) {
$fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->site WHERE id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
if ( ! empty( $non_cached_ids ) ) {
$fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->site WHERE id IN (%s)", join( ',', array_map( 'intval', $non_cached_ids ) ) ) );
update_network_cache( $fresh_networks );
}