Coding Standards: Use static closures when not using $this.

When a closure does not use `$this`, it can be made `static` for improved performance.

Static closures are supported in PHP since PHP 5.4. ​

Props jrf, hellofromTonya, swissspidy, SergeyBiryukov.
See #53359.
Built from https://develop.svn.wordpress.org/trunk@51657


git-svn-id: http://core.svn.wordpress.org/trunk@51263 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
hellofromTonya 2021-08-26 12:59:02 +00:00
parent 622985b69c
commit c7d3e267b8
23 changed files with 27 additions and 27 deletions

View File

@ -31,7 +31,7 @@ $current_screen->is_block_editor( true );
// Default to is-fullscreen-mode to avoid jumps in the UI.
add_filter(
'admin_body_class',
function( $classes ) {
static function( $classes ) {
return "$classes is-fullscreen-mode";
}
);

View File

@ -474,7 +474,7 @@ class WP_Community_Events {
$future_wordcamps = array_filter(
$future_events,
function( $wordcamp ) {
static function( $wordcamp ) {
return 'wordcamp' === $wordcamp['type'];
}
);

View File

@ -41,7 +41,7 @@ class WP_Site_Health_Auto_Updates {
$tests = array_filter( $tests );
$tests = array_map(
function( $test ) {
static function( $test ) {
$test = (object) $test;
if ( empty( $test->severity ) ) {

View File

@ -1658,7 +1658,7 @@ function _upgrade_422_find_genericons_files_in_folder( $directory ) {
$dirs = glob( $directory . '*', GLOB_ONLYDIR );
$dirs = array_filter(
$dirs,
function( $dir ) {
static function( $dir ) {
// Skip any node_modules directories.
return false === strpos( $dir, 'node_modules' );
}

View File

@ -20,7 +20,7 @@ if ( isset( $_GET['tab'] ) && 'policyguide' === $_GET['tab'] ) {
add_filter(
'admin_body_class',
function( $body_class ) {
static function( $body_class ) {
$body_class .= ' privacy-settings ';
return $body_class;

View File

@ -19,7 +19,7 @@ if ( ! class_exists( 'WP_Privacy_Policy_Content' ) ) {
add_filter(
'admin_body_class',
function( $body_class ) {
static function( $body_class ) {
$body_class .= ' privacy-settings ';
return $body_class;

View File

@ -186,7 +186,7 @@ if ( ! class_exists( 'TwentyTwenty_Customize' ) ) {
'settings' => 'accent_hue',
'description' => __( 'Apply a custom color for links, buttons, featured images.', 'twentytwenty' ),
'mode' => 'hue',
'active_callback' => function() use ( $wp_customize ) {
'active_callback' => static function() use ( $wp_customize ) {
return ( 'custom' === $wp_customize->get_setting( 'accent_hue_active' )->value() );
},
)

View File

@ -92,7 +92,7 @@ if ( ! class_exists( 'Twenty_Twenty_One_Customize' ) ) {
array(
'capability' => 'edit_theme_options',
'default' => 'excerpt',
'sanitize_callback' => function( $value ) {
'sanitize_callback' => static function( $value ) {
return 'excerpt' === $value || 'full' === $value ? $value : 'excerpt';
},
)

View File

@ -153,7 +153,7 @@ class Twenty_Twenty_One_Dark_Mode {
array(
'section' => 'colors',
'priority' => 100,
'active_callback' => function() {
'active_callback' => static function() {
return 127 >= Twenty_Twenty_One_Custom_Colors::get_relative_luminance_from_hex( get_theme_mod( 'background_color', 'D1E4DD' ) );
},
)
@ -165,7 +165,7 @@ class Twenty_Twenty_One_Dark_Mode {
array(
'capability' => 'edit_theme_options',
'default' => false,
'sanitize_callback' => function( $value ) {
'sanitize_callback' => static function( $value ) {
return (bool) $value;
},
)
@ -188,7 +188,7 @@ class Twenty_Twenty_One_Dark_Mode {
'label' => esc_html__( 'Dark Mode support', 'twentytwentyone' ),
'priority' => 110,
'description' => $description,
'active_callback' => function( $value ) {
'active_callback' => static function( $value ) {
return 127 < Twenty_Twenty_One_Custom_Colors::get_relative_luminance_from_hex( get_theme_mod( 'background_color', 'D1E4DD' ) );
},
)

View File

@ -361,7 +361,7 @@ function wp_render_duotone_support( $block_content, $block ) {
$selectors = explode( ',', $duotone_support );
$selectors_scoped = array_map(
function ( $selector ) use ( $duotone_id ) {
static function ( $selector ) use ( $duotone_id ) {
return '.' . $duotone_id . ' ' . trim( $selector );
},
$selectors

View File

@ -131,7 +131,7 @@ function wp_restore_group_inner_container( $block_content, $block ) {
$replace_regex = '/(^\s*<div\b[^>]*wp-block-group[^>]*>)(.*)(<\/div>\s*$)/ms';
$updated_content = preg_replace_callback(
$replace_regex,
function( $matches ) {
static function( $matches ) {
return $matches[1] . '<div class="wp-block-group__inner-container">' . $matches[2] . '</div>' . $matches[3];
},
$block_content

View File

@ -124,7 +124,7 @@ function resolve_block_template( $template_type, $template_hierarchy ) {
usort(
$templates,
function ( $template_a, $template_b ) use ( $slug_priorities ) {
static function ( $template_a, $template_b ) use ( $slug_priorities ) {
return $slug_priorities[ $template_a->slug ] - $slug_priorities[ $template_b->slug ];
}
);

View File

@ -636,7 +636,7 @@ class WP_Theme_JSON {
$declaration_block = array_reduce(
$declarations,
function ( $carry, $element ) {
static function ( $carry, $element ) {
return $carry .= $element['name'] . ': ' . $element['value'] . ';'; },
''
);

View File

@ -3113,7 +3113,7 @@ function wp_rel_nofollow( $text ) {
$text = stripslashes( $text );
$text = preg_replace_callback(
'|<a (.+?)>|i',
function( $matches ) {
static function( $matches ) {
return wp_rel_callback( $matches, 'nofollow' );
},
$text
@ -3147,7 +3147,7 @@ function wp_rel_ugc( $text ) {
$text = stripslashes( $text );
$text = preg_replace_callback(
'|<a (.+?)>|i',
function( $matches ) {
static function( $matches ) {
return wp_rel_callback( $matches, 'nofollow ugc' );
},
$text

View File

@ -3227,7 +3227,7 @@ function rest_get_endpoint_args_for_schema( $schema, $method = WP_REST_Server::C
function rest_convert_error_to_response( $error ) {
$status = array_reduce(
$error->get_all_error_data(),
function ( $status, $error_data ) {
static function ( $status, $error_data ) {
return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status;
},
500

View File

@ -597,7 +597,7 @@ abstract class WP_REST_Controller {
// Return the list of all requested fields which appear in the schema.
return array_reduce(
$requested_fields,
function( $response_fields, $field ) use ( $fields ) {
static function( $response_fields, $field ) use ( $fields ) {
if ( in_array( $field, $fields, true ) ) {
$response_fields[] = $field;
return $response_fields;

View File

@ -381,7 +381,7 @@ class WP_REST_Plugins_Controller extends WP_REST_Controller {
$installed_locales = apply_filters( 'plugins_update_check_locales', $installed_locales );
$language_packs = array_map(
function( $item ) {
static function( $item ) {
return (object) $item;
},
$api->language_packs
@ -389,7 +389,7 @@ class WP_REST_Plugins_Controller extends WP_REST_Controller {
$language_packs = array_filter(
$language_packs,
function( $pack ) use ( $installed_locales ) {
static function( $pack ) use ( $installed_locales ) {
return in_array( $pack->language, $installed_locales, true );
}
);

View File

@ -85,7 +85,7 @@ class WP_REST_Widget_Types_Controller extends WP_REST_Controller {
'form_data' => array(
'description' => __( 'Serialized widget form data to encode into instance settings.' ),
'type' => 'string',
'sanitize_callback' => function( $string ) {
'sanitize_callback' => static function( $string ) {
$array = array();
wp_parse_str( $string, $array );
return $array;

View File

@ -792,7 +792,7 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
'type' => 'string',
'context' => array(),
'arg_options' => array(
'sanitize_callback' => function( $string ) {
'sanitize_callback' => static function( $string ) {
$array = array();
wp_parse_str( $string, $array );
return $array;

View File

@ -2692,7 +2692,7 @@ function wp_maybe_inline_styles() {
// Reorder styles array based on size.
usort(
$styles,
function( $a, $b ) {
static function( $a, $b ) {
return ( $a['size'] <= $b['size'] ) ? -1 : 1;
}
);

View File

@ -517,7 +517,7 @@ function wp_update_plugins( $extra_stats = array() ) {
}
}
$sanitize_plugin_update_payload = function( &$item ) {
$sanitize_plugin_update_payload = static function( &$item ) {
$item = (object) $item;
unset( $item->translations, $item->compatibility );

View File

@ -3459,7 +3459,7 @@ function wp_user_personal_data_exporter( $email_address ) {
// Remove items that use reserved names.
$extra_data = array_filter(
$_extra_data,
function( $item ) use ( $reserved_names ) {
static function( $item ) use ( $reserved_names ) {
return ! in_array( $item['name'], $reserved_names, true );
}
);

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.9-alpha-51655';
$wp_version = '5.9-alpha-51657';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.