Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (8 years ago)
Author:
pento
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/update.php

    r41967 r42343  
    3232    $php_version = phpversion();
    3333
    34     $current = get_site_transient( 'update_core' );
     34    $current      = get_site_transient( 'update_core' );
    3535    $translations = wp_get_installed_translations( 'core' );
    3636
    3737    // Invalidate the transient when $wp_version changes
    38     if ( is_object( $current ) && $wp_version != $current->version_checked )
     38    if ( is_object( $current ) && $wp_version != $current->version_checked ) {
    3939        $current = false;
    40 
    41     if ( ! is_object($current) ) {
    42         $current = new stdClass;
    43         $current->updates = array();
     40    }
     41
     42    if ( ! is_object( $current ) ) {
     43        $current                  = new stdClass;
     44        $current->updates         = array();
    4445        $current->version_checked = $wp_version;
    4546    }
    4647
    47     if ( ! empty( $extra_stats ) )
     48    if ( ! empty( $extra_stats ) ) {
    4849        $force_check = true;
     50    }
    4951
    5052    // Wait 60 seconds between multiple version check requests
    51     $timeout = 60;
     53    $timeout          = 60;
    5254    $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
    5355    if ( ! $force_check && $time_not_changed ) {
     
    6870    set_site_transient( 'update_core', $current );
    6971
    70     if ( method_exists( $wpdb, 'db_version' ) )
    71         $mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version());
    72     else
     72    if ( method_exists( $wpdb, 'db_version' ) ) {
     73        $mysql_version = preg_replace( '/[^0-9.].*/', '', $wpdb->db_version() );
     74    } else {
    7375        $mysql_version = 'N/A';
     76    }
    7477
    7578    if ( is_multisite() ) {
    76         $user_count = get_user_count();
    77         $num_blogs = get_blog_count();
    78         $wp_install = network_site_url();
     79        $user_count        = get_user_count();
     80        $num_blogs         = get_blog_count();
     81        $wp_install        = network_site_url();
    7982        $multisite_enabled = 1;
    8083    } else {
    81         $user_count = count_users();
    82         $user_count = $user_count['total_users'];
     84        $user_count        = count_users();
     85        $user_count        = $user_count['total_users'];
    8386        $multisite_enabled = 0;
    84         $num_blogs = 1;
    85         $wp_install = home_url( '/' );
     87        $num_blogs         = 1;
     88        $wp_install        = home_url( '/' );
    8689    }
    8790
     
    107110     *
    108111     * @param array $query {
    109      *     Version check query arguments. 
     112     *     Version check query arguments.
    110113     *
    111114     *     @type string $version            WordPress version number.
     
    126129    );
    127130
    128     if ( is_array( $extra_stats ) )
     131    if ( is_array( $extra_stats ) ) {
    129132        $post_body = array_merge( $post_body, $extra_stats );
     133    }
    130134
    131135    $url = $http_url = 'http://api.wordpress.org/core/version-check/1.7/?' . http_build_query( $query, null, '&' );
    132     if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
     136    if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) {
    133137        $url = set_url_scheme( $url, 'https' );
     138    }
    134139
    135140    $doing_cron = wp_doing_cron();
    136141
    137142    $options = array(
    138         'timeout' => $doing_cron ? 30 : 3,
     143        'timeout'    => $doing_cron ? 30 : 3,
    139144        'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
    140         'headers' => array(
     145        'headers'    => array(
    141146            'wp_install' => $wp_install,
    142             'wp_blog' => home_url( '/' )
     147            'wp_blog'    => home_url( '/' ),
    143148        ),
    144         'body' => $post_body,
     149        'body'       => $post_body,
    145150    );
    146151
     
    173178    foreach ( $offers as &$offer ) {
    174179        foreach ( $offer as $offer_key => $value ) {
    175             if ( 'packages' == $offer_key )
    176                 $offer['packages'] = (object) array_intersect_key( array_map( 'esc_url', $offer['packages'] ),
    177                     array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' ) );
    178             elseif ( 'download' == $offer_key )
     180            if ( 'packages' == $offer_key ) {
     181                $offer['packages'] = (object) array_intersect_key(
     182                    array_map( 'esc_url', $offer['packages'] ),
     183                    array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' )
     184                );
     185            } elseif ( 'download' == $offer_key ) {
    179186                $offer['download'] = esc_url( $value );
    180             else
     187            } else {
    181188                $offer[ $offer_key ] = esc_html( $value );
    182         }
    183         $offer = (object) array_intersect_key( $offer, array_fill_keys( array( 'response', 'download', 'locale',
    184             'packages', 'current', 'version', 'php_version', 'mysql_version', 'new_bundled', 'partial_version', 'notify_email', 'support_email', 'new_files' ), '' ) );
    185     }
    186 
    187     $updates = new stdClass();
    188     $updates->updates = $offers;
    189     $updates->last_checked = time();
     189            }
     190        }
     191        $offer = (object) array_intersect_key(
     192            $offer, array_fill_keys(
     193                array(
     194                    'response',
     195                    'download',
     196                    'locale',
     197                    'packages',
     198                    'current',
     199                    'version',
     200                    'php_version',
     201                    'mysql_version',
     202                    'new_bundled',
     203                    'partial_version',
     204                    'notify_email',
     205                    'support_email',
     206                    'new_files',
     207                ), ''
     208            )
     209        );
     210    }
     211
     212    $updates                  = new stdClass();
     213    $updates->updates         = $offers;
     214    $updates->last_checked    = time();
    190215    $updates->version_checked = $wp_version;
    191216
    192     if ( isset( $body['translations'] ) )
     217    if ( isset( $body['translations'] ) ) {
    193218        $updates->translations = $body['translations'];
     219    }
    194220
    195221    set_site_transient( 'update_core', $updates );
     
    230256
    231257    // If running blog-side, bail unless we've not checked in the last 12 hours
    232     if ( !function_exists( 'get_plugins' ) )
     258    if ( ! function_exists( 'get_plugins' ) ) {
    233259        require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    234 
    235     $plugins = get_plugins();
     260    }
     261
     262    $plugins      = get_plugins();
    236263    $translations = wp_get_installed_translations( 'plugins' );
    237264
    238265    $active  = get_option( 'active_plugins', array() );
    239266    $current = get_site_transient( 'update_plugins' );
    240     if ( ! is_object($current) )
     267    if ( ! is_object( $current ) ) {
    241268        $current = new stdClass;
    242 
    243     $new_option = new stdClass;
     269    }
     270
     271    $new_option               = new stdClass;
    244272    $new_option->last_checked = time();
    245273
     
    248276    // Check for update on a different schedule, depending on the page.
    249277    switch ( current_filter() ) {
    250         case 'upgrader_process_complete' :
     278        case 'upgrader_process_complete':
    251279            $timeout = 0;
    252280            break;
    253         case 'load-update-core.php' :
     281        case 'load-update-core.php':
    254282            $timeout = MINUTE_IN_SECONDS;
    255283            break;
    256         case 'load-plugins.php' :
    257         case 'load-update.php' :
     284        case 'load-plugins.php':
     285        case 'load-update.php':
    258286            $timeout = HOUR_IN_SECONDS;
    259287            break;
    260         default :
     288        default:
    261289            if ( $doing_cron ) {
    262290                $timeout = 0;
     
    273301            $new_option->checked[ $file ] = $p['Version'];
    274302
    275             if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
     303            if ( ! isset( $current->checked[ $file ] ) || strval( $current->checked[ $file ] ) !== strval( $p['Version'] ) ) {
    276304                $plugin_changed = true;
    277         }
    278 
    279         if ( isset ( $current->response ) && is_array( $current->response ) ) {
     305            }
     306        }
     307
     308        if ( isset( $current->response ) && is_array( $current->response ) ) {
    280309            foreach ( $current->response as $plugin_file => $update_details ) {
    281                 if ( ! isset($plugins[ $plugin_file ]) ) {
     310                if ( ! isset( $plugins[ $plugin_file ] ) ) {
    282311                    $plugin_changed = true;
    283312                    break;
     
    319348
    320349    $options = array(
    321         'timeout' => $timeout,
    322         'body' => array(
     350        'timeout'    => $timeout,
     351        'body'       => array(
    323352            'plugins'      => wp_json_encode( $to_send ),
    324353            'translations' => wp_json_encode( $translations ),
     
    326355            'all'          => wp_json_encode( true ),
    327356        ),
    328         'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' )
     357        'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
    329358    );
    330359
     
    334363
    335364    $url = $http_url = 'http://api.wordpress.org/plugins/update-check/1.1/';
    336     if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
     365    if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) {
    337366        $url = set_url_scheme( $url, 'https' );
     367    }
    338368
    339369    $raw_response = wp_remote_post( $url, $options );
     
    371401
    372402    if ( is_array( $response ) ) {
    373         $new_option->response = $response['plugins'];
     403        $new_option->response     = $response['plugins'];
    374404        $new_option->translations = $response['translations'];
    375405        // TODO: Perhaps better to store no_update in a separate transient with an expiry?
    376406        $new_option->no_update = $response['no_update'];
    377407    } else {
    378         $new_option->response = array();
     408        $new_option->response     = array();
    379409        $new_option->translations = array();
    380         $new_option->no_update = array();
     410        $new_option->no_update    = array();
    381411    }
    382412
     
    404434
    405435    $installed_themes = wp_get_themes();
    406     $translations = wp_get_installed_translations( 'themes' );
     436    $translations     = wp_get_installed_translations( 'themes' );
    407437
    408438    $last_update = get_site_transient( 'update_themes' );
    409     if ( ! is_object($last_update) )
     439    if ( ! is_object( $last_update ) ) {
    410440        $last_update = new stdClass;
     441    }
    411442
    412443    $themes = $checked = $request = array();
     
    416447
    417448    foreach ( $installed_themes as $theme ) {
    418         $checked[ $theme->get_stylesheet() ] = $theme->get('Version');
     449        $checked[ $theme->get_stylesheet() ] = $theme->get( 'Version' );
    419450
    420451        $themes[ $theme->get_stylesheet() ] = array(
    421             'Name'       => $theme->get('Name'),
    422             'Title'      => $theme->get('Name'),
    423             'Version'    => $theme->get('Version'),
    424             'Author'     => $theme->get('Author'),
    425             'Author URI' => $theme->get('AuthorURI'),
     452            'Name'       => $theme->get( 'Name' ),
     453            'Title'      => $theme->get( 'Name' ),
     454            'Version'    => $theme->get( 'Version' ),
     455            'Author'     => $theme->get( 'Author' ),
     456            'Author URI' => $theme->get( 'AuthorURI' ),
    426457            'Template'   => $theme->get_template(),
    427458            'Stylesheet' => $theme->get_stylesheet(),
     
    433464    // Check for update on a different schedule, depending on the page.
    434465    switch ( current_filter() ) {
    435         case 'upgrader_process_complete' :
     466        case 'upgrader_process_complete':
    436467            $timeout = 0;
    437468            break;
    438         case 'load-update-core.php' :
     469        case 'load-update-core.php':
    439470            $timeout = MINUTE_IN_SECONDS;
    440471            break;
    441         case 'load-themes.php' :
    442         case 'load-update.php' :
     472        case 'load-themes.php':
     473        case 'load-update.php':
    443474            $timeout = HOUR_IN_SECONDS;
    444475            break;
    445         default :
     476        default:
    446477            $timeout = $doing_cron ? 0 : 12 * HOUR_IN_SECONDS;
    447478    }
     
    452483        $theme_changed = false;
    453484        foreach ( $checked as $slug => $v ) {
    454             if ( !isset( $last_update->checked[ $slug ] ) || strval($last_update->checked[ $slug ]) !== strval($v) )
     485            if ( ! isset( $last_update->checked[ $slug ] ) || strval( $last_update->checked[ $slug ] ) !== strval( $v ) ) {
    455486                $theme_changed = true;
    456         }
    457 
    458         if ( isset ( $last_update->response ) && is_array( $last_update->response ) ) {
     487            }
     488        }
     489
     490        if ( isset( $last_update->response ) && is_array( $last_update->response ) ) {
    459491            foreach ( $last_update->response as $slug => $update_details ) {
    460                 if ( ! isset($checked[ $slug ]) ) {
     492                if ( ! isset( $checked[ $slug ] ) ) {
    461493                    $theme_changed = true;
    462494                    break;
     
    498530
    499531    $options = array(
    500         'timeout' => $timeout,
    501         'body' => array(
     532        'timeout'    => $timeout,
     533        'body'       => array(
    502534            'themes'       => wp_json_encode( $request ),
    503535            'translations' => wp_json_encode( $translations ),
    504536            'locale'       => wp_json_encode( $locales ),
    505537        ),
    506         'user-agent'    => 'WordPress/' . $wp_version . '; ' . home_url( '/' )
     538        'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
    507539    );
    508540
     
    512544
    513545    $url = $http_url = 'http://api.wordpress.org/themes/update-check/1.1/';
    514     if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
     546    if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) {
    515547        $url = set_url_scheme( $url, 'https' );
     548    }
    516549
    517550    $raw_response = wp_remote_post( $url, $options );
     
    532565    }
    533566
    534     $new_update = new stdClass;
     567    $new_update               = new stdClass;
    535568    $new_update->last_checked = time();
    536     $new_update->checked = $checked;
     569    $new_update->checked      = $checked;
    537570
    538571    $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
     
    567600 */
    568601function wp_get_translation_updates() {
    569     $updates = array();
    570     $transients = array( 'update_core' => 'core', 'update_plugins' => 'plugin', 'update_themes' => 'theme' );
     602    $updates    = array();
     603    $transients = array(
     604        'update_core'    => 'core',
     605        'update_plugins' => 'plugin',
     606        'update_themes'  => 'theme',
     607    );
    571608    foreach ( $transients as $transient => $type ) {
    572609        $transient = get_site_transient( $transient );
    573         if ( empty( $transient->translations ) )
     610        if ( empty( $transient->translations ) ) {
    574611            continue;
     612        }
    575613
    576614        foreach ( $transient->translations as $translation ) {
     
    589627 */
    590628function wp_get_update_data() {
    591     $counts = array( 'plugins' => 0, 'themes' => 0, 'wordpress' => 0, 'translations' => 0 );
     629    $counts = array(
     630        'plugins'      => 0,
     631        'themes'       => 0,
     632        'wordpress'    => 0,
     633        'translations' => 0,
     634    );
    592635
    593636    if ( $plugins = current_user_can( 'update_plugins' ) ) {
    594637        $update_plugins = get_site_transient( 'update_plugins' );
    595         if ( ! empty( $update_plugins->response ) )
     638        if ( ! empty( $update_plugins->response ) ) {
    596639            $counts['plugins'] = count( $update_plugins->response );
     640        }
    597641    }
    598642
    599643    if ( $themes = current_user_can( 'update_themes' ) ) {
    600644        $update_themes = get_site_transient( 'update_themes' );
    601         if ( ! empty( $update_themes->response ) )
     645        if ( ! empty( $update_themes->response ) ) {
    602646            $counts['themes'] = count( $update_themes->response );
     647        }
    603648    }
    604649
    605650    if ( ( $core = current_user_can( 'update_core' ) ) && function_exists( 'get_core_updates' ) ) {
    606         $update_wordpress = get_core_updates( array('dismissed' => false) );
    607         if ( ! empty( $update_wordpress ) && ! in_array( $update_wordpress[0]->response, array('development', 'latest') ) && current_user_can('update_core') )
     651        $update_wordpress = get_core_updates( array( 'dismissed' => false ) );
     652        if ( ! empty( $update_wordpress ) && ! in_array( $update_wordpress[0]->response, array( 'development', 'latest' ) ) && current_user_can( 'update_core' ) ) {
    608653            $counts['wordpress'] = 1;
    609     }
    610 
    611     if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() )
     654        }
     655    }
     656
     657    if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() ) {
    612658        $counts['translations'] = 1;
     659    }
    613660
    614661    $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
    615     $titles = array();
     662    $titles          = array();
    616663    if ( $counts['wordpress'] ) {
    617664        /* translators: 1: Number of updates available to WordPress */
    618         $titles['wordpress'] = sprintf( __( '%d WordPress Update'), $counts['wordpress'] );
     665        $titles['wordpress'] = sprintf( __( '%d WordPress Update' ), $counts['wordpress'] );
    619666    }
    620667    if ( $counts['plugins'] ) {
     
    632679    $update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
    633680
    634     $update_data = array( 'counts' => $counts, 'title' => $update_title );
     681    $update_data = array(
     682        'counts' => $counts,
     683        'title'  => $update_title,
     684    );
    635685    /**
    636686     * Filters the returned array of update data for plugins, themes, and WordPress core.
     
    681731function _maybe_update_plugins() {
    682732    $current = get_site_transient( 'update_plugins' );
    683     if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
    684         return;
     733    if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) ) {
     734        return;
     735    }
    685736    wp_update_plugins();
    686737}
     
    697748function _maybe_update_themes() {
    698749    $current = get_site_transient( 'update_themes' );
    699     if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
    700         return;
     750    if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) ) {
     751        return;
     752    }
    701753    wp_update_themes();
    702754}
     
    708760 */
    709761function wp_schedule_update_checks() {
    710     if ( ! wp_next_scheduled( 'wp_version_check' ) && ! wp_installing() )
    711         wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
    712 
    713     if ( ! wp_next_scheduled( 'wp_update_plugins' ) && ! wp_installing() )
    714         wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
    715 
    716     if ( ! wp_next_scheduled( 'wp_update_themes' ) && ! wp_installing() )
    717         wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
     762    if ( ! wp_next_scheduled( 'wp_version_check' ) && ! wp_installing() ) {
     763        wp_schedule_event( time(), 'twicedaily', 'wp_version_check' );
     764    }
     765
     766    if ( ! wp_next_scheduled( 'wp_update_plugins' ) && ! wp_installing() ) {
     767        wp_schedule_event( time(), 'twicedaily', 'wp_update_plugins' );
     768    }
     769
     770    if ( ! wp_next_scheduled( 'wp_update_themes' ) && ! wp_installing() ) {
     771        wp_schedule_event( time(), 'twicedaily', 'wp_update_themes' );
     772    }
    718773}
    719774
     
    752807add_action( 'wp_update_themes', 'wp_update_themes' );
    753808
    754 add_action( 'update_option_WPLANG', 'wp_clean_update_cache' , 10, 0 );
     809add_action( 'update_option_WPLANG', 'wp_clean_update_cache', 10, 0 );
    755810
    756811add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
Note: See TracChangeset for help on using the changeset viewer.