General: Use static on closures whenever $this is not used to avoid memory leaks.

Props westonruter, jrf, spacedmonkey.
Fixes #58323.


git-svn-id: https://develop.svn.wordpress.org/trunk@55822 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter 2023-05-17 22:44:21 +00:00
parent 5db31259ec
commit d5792c7a88
21 changed files with 29 additions and 29 deletions

View File

@ -2685,7 +2685,7 @@ function wp_opcache_invalidate_directory( $dir ) {
* with sub-directories represented as nested arrays.
* @param string $path Absolute path to the directory.
*/
$invalidate_directory = function( $dirlist, $path ) use ( &$invalidate_directory ) {
$invalidate_directory = static function( $dirlist, $path ) use ( &$invalidate_directory ) {
$path = trailingslashit( $path );
foreach ( $dirlist as $name => $details ) {

View File

@ -485,7 +485,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
*
* @return string Returns the block content.
*/
$settings['render_callback'] = function( $attributes, $content, $block ) use ( $template_path ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$settings['render_callback'] = static function( $attributes, $content, $block ) use ( $template_path ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
ob_start();
require $template_path;
return ob_get_clean();

View File

@ -4826,7 +4826,7 @@ class WP_Query {
* $value is passed by reference to allow it to be modified.
* array_walk_recursive() does not return an array.
*/
function ( &$value ) use ( $wpdb, $placeholder ) {
static function ( &$value ) use ( $wpdb, $placeholder ) {
if ( is_string( $value ) && str_contains( $value, $placeholder ) ) {
$value = $wpdb->remove_placeholder_escape( $value );
}

View File

@ -1177,7 +1177,7 @@ function wp_kses_attr( $element, $attr, $allowed_html, $allowed_protocols ) {
// Check if there are attributes that are required.
$required_attrs = array_filter(
$allowed_html[ $element_low ],
function( $required_attr_limits ) {
static function( $required_attr_limits ) {
return isset( $required_attr_limits['required'] ) && true === $required_attr_limits['required'];
}
);

View File

@ -792,7 +792,7 @@ class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
),
'context' => array( 'view', 'edit', 'embed' ),
'arg_options' => array(
'sanitize_callback' => function ( $value ) {
'sanitize_callback' => static function ( $value ) {
return array_map( 'sanitize_html_class', wp_parse_list( $value ) );
},
),
@ -873,7 +873,7 @@ class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
),
'context' => array( 'view', 'edit', 'embed' ),
'arg_options' => array(
'sanitize_callback' => function ( $value ) {
'sanitize_callback' => static function ( $value ) {
return array_map( 'sanitize_html_class', wp_parse_list( $value ) );
},
),

View File

@ -534,7 +534,7 @@ class WP_REST_Menus_Controller extends WP_REST_Terms_Controller {
),
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'validate_callback' => function ( $locations, $request, $param ) {
'validate_callback' => static function ( $locations, $request, $param ) {
$valid = rest_validate_request_arg( $locations, $request, $param );
if ( true !== $valid ) {

View File

@ -2405,7 +2405,7 @@ function wp_common_block_scripts_and_styles() {
function wp_filter_out_block_nodes( $nodes ) {
return array_filter(
$nodes,
function( $node ) {
static function( $node ) {
return ! in_array( 'blocks', $node['path'], true );
},
ARRAY_FILTER_USE_BOTH
@ -2635,7 +2635,7 @@ function enqueue_block_styles_assets() {
if ( wp_should_load_separate_core_block_assets() ) {
add_filter(
'render_block',
function( $html, $block ) use ( $block_name, $style_properties ) {
static function( $html, $block ) use ( $block_name, $style_properties ) {
if ( $block['blockName'] === $block_name ) {
wp_enqueue_style( $style_properties['style_handle'] );
}

View File

@ -2,7 +2,7 @@
add_action(
'template_redirect',
function() {
static function() {
global $timestart;
@ -15,7 +15,7 @@ add_action(
add_action(
'shutdown',
function() use ( $server_timing_values, $template_start ) {
static function() use ( $server_timing_values, $template_start ) {
global $timestart;

View File

@ -421,7 +421,7 @@ class Tests_Admin_wpSiteHealth extends WP_UnitTestCase {
// Set thresholds so high they should never be exceeded.
add_filter(
'site_status_persistent_object_cache_thresholds',
function() {
static function() {
return array(
'alloptions_count' => PHP_INT_MAX,
'alloptions_bytes' => PHP_INT_MAX,
@ -472,7 +472,7 @@ class Tests_Admin_wpSiteHealth extends WP_UnitTestCase {
public function test_object_cache_thresholds( $threshold, $count ) {
add_filter(
'site_status_persistent_object_cache_thresholds',
function ( $thresholds ) use ( $threshold, $count ) {
static function ( $thresholds ) use ( $threshold, $count ) {
return array_merge( $thresholds, array( $threshold => $count ) );
}
);

View File

@ -302,7 +302,7 @@ class Tests_Blocks_Editor extends WP_UnitTestCase {
// Force the return value of wp_max_upload_size() to be 500.
add_filter(
'upload_size_limit',
function() {
static function() {
return 500;
}
);

View File

@ -436,7 +436,7 @@ END
)
);
$commenter_filter = function () {
$commenter_filter = static function () {
return array(
'comment_author_email' => 'unapproved@example.org',
);

View File

@ -693,7 +693,7 @@ class Tests_Blocks_wpBlock extends WP_UnitTestCase {
$this->registry->register(
'core/outer',
array(
'render_callback' => function( $block_attributes, $content ) {
'render_callback' => static function( $block_attributes, $content ) {
return $content;
},
)
@ -702,7 +702,7 @@ class Tests_Blocks_wpBlock extends WP_UnitTestCase {
$this->registry->register(
'core/inner',
array(
'render_callback' => function() {
'render_callback' => static function() {
return 'b';
},
)

View File

@ -1473,7 +1473,7 @@ JS;
wp_set_script_translations( 'common' );
$print_scripts = get_echo(
function() {
static function() {
wp_print_scripts();
_print_scripts();
}

View File

@ -14,7 +14,7 @@ class Tests_General_wpPreloadResources extends WP_UnitTestCase {
* @ticket 42438
*/
public function test_preload_resources( $expected, $preload_resources ) {
$callback = function () use ( $preload_resources ) {
$callback = static function () use ( $preload_resources ) {
return $preload_resources;
};

View File

@ -1923,7 +1923,7 @@ HTML;
);
return array_map(
function ( $datum ) {
static function ( $datum ) {
$datum[] = array(
'p' => array(
'dir' => array(

View File

@ -2348,7 +2348,7 @@ EOF;
add_filter(
'wp_content_img_tag',
function( $filtered_image ) {
static function( $filtered_image ) {
return "<span>$filtered_image</span>";
}
);
@ -2372,7 +2372,7 @@ EOF;
add_filter(
'wp_content_img_tag',
function( $filtered_image ) {
static function( $filtered_image ) {
return "<span>$filtered_image</span>";
}
);
@ -3761,7 +3761,7 @@ EOF;
add_filter( 'wp_img_tag_add_decoding_attr', '__return_false' );
add_filter(
'wp_get_attachment_image_attributes',
function( $attr ) {
static function( $attr ) {
unset( $attr['srcset'], $attr['sizes'], $attr['decoding'] );
return $attr;
}

View File

@ -538,7 +538,7 @@ class Tests_Pluggable_wpMail extends WP_UnitTestCase {
* Tests that AltBody is reset between each wp_mail call.
*/
public function test_wp_mail_resets_properties() {
$wp_mail_set_text_message = function ( $phpmailer ) {
$wp_mail_set_text_message = static function ( $phpmailer ) {
$phpmailer->AltBody = 'user1';
};

View File

@ -136,7 +136,7 @@ class Tests_Query_Stickies extends WP_UnitTestCase {
$post_ids = self::factory()->post->create_many( $sticky_count, array( 'post_date' => $post_date ) );
add_filter(
'pre_option_sticky_posts',
function () use ( $post_ids ) {
static function () use ( $post_ids ) {
return $post_ids;
}
);

View File

@ -937,7 +937,7 @@ class Tests_REST_Server extends WP_Test_REST_TestCase {
* @ticket 56566
*/
public function test_link_embedding_returning_wp_error() {
$return_wp_error = function() {
$return_wp_error = static function() {
return new WP_Error( 'some-error', 'This is not valid!' );
};
add_filter( 'rest_pre_dispatch', $return_wp_error );
@ -2188,7 +2188,7 @@ class Tests_REST_Server extends WP_Test_REST_TestCase {
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => function() {
'callback' => static function() {
return new \WP_REST_Response( INF );
},
'permission_callback' => '__return_true',

View File

@ -1384,7 +1384,7 @@ class WP_Test_REST_Themes_Controller extends WP_Test_REST_Controller_Testcase {
'wp/v2',
sprintf( '/themes/(?P<stylesheet>%s)//test', WP_REST_Themes_Controller::PATTERN ),
array(
'callback' => function ( WP_REST_Request $request ) {
'callback' => static function ( WP_REST_Request $request ) {
return $request['stylesheet'];
},
'permission_callback' => '__return_true',

View File

@ -600,7 +600,7 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
$global_styles_query_count = 0;
add_filter(
'query',
function( $query ) use ( &$global_styles_query_count ) {
static function( $query ) use ( &$global_styles_query_count ) {
if ( preg_match( '#post_type = \'wp_global_styles\'#', $query ) ) {
$global_styles_query_count++;
}