array_map warning in class-shutterstock-api.php
-
Our Sentry logging has had a high number of events from the Shutterstock plugin, for this warning:
Warning: array_map(): Expected parameter 2 to be an array, null given
To reproduce, I was able to confirm that the warning happens when I go to ‘License this Image’ after adding an image to a post, and then hitting the ‘License’ button on the modal window. I short-circuited the library code after the line in question to avoid actually attempt to license any images during testing.
The warning occurs on line 157 of class-shutterstock-api.php:
$metadata = array_map(‘sanitize_text_field’, $req_body[‘metadata’]);
Per comments later in the function, the metadata parameter does indeed appear to be optional, as does the search_id parameter which is also not accessed in a safe manner. I recommend a fix similar to how the local_amount value is being checked. I tested that it prevents the warning for the metadata parameter.
Index: includes/class-shutterstock-api.php =================================================================== --- includes/class-shutterstock-api.php (revision 2529898) +++ includes/class-shutterstock-api.php (working copy) @@ -148,14 +148,19 @@ $image_description = sanitize_text_field($req_body['description']); $contributor_name = sanitize_text_field($req_body['contributorName']); $media_type = sanitize_text_field($req_body['mediaType']); - $search_id = sanitize_text_field($req_body['search_id']); $is_editorial = $media_type === 'editorial'; $width = sanitize_text_field($req_body['width']); $height = sanitize_text_field($req_body['height']); - $metadata = array_map('sanitize_text_field', $req_body['metadata']); + $search_id = isset($req_body['search_id']) + ? sanitize_text_field($req_body['search_id']) + : null; + $metadata = isset($req_body['metadata']) + ? array_map('sanitize_text_field', $req_body['metadata']) + : null; + $local_amount = isset($req_body['pricePerDownload']['local_amount']) ? sanitize_text_field($req_body['pricePerDownload']['local_amount']) : 0; @@ -167,7 +172,6 @@ : $this->api_url. '/images/licenses?subscription_id='. $subscription_id; $body_key = $is_editorial ? 'editorial' : 'images'; - $body = []; if ($is_editorial) { $country = $this->shutterstock_helper->get_editorial_country();
Additionally, PHPStorm suggested removing the redundant $body = []; line, as it is defined on either of the halves of the following if/else statement, making the initial definition redundant.
- The topic ‘array_map warning in class-shutterstock-api.php’ is closed to new replies.