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

@@ -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++;
}