Skip to content

Commit

Permalink
Stats: Remove usage of create_function() in stats_convert_image_urls() (
Browse files Browse the repository at this point in the history
#8240)

* Remove usage of create_function from stats_convert_image_urls

* Make the callback be prefixed and documented
  • Loading branch information
oskosk authored and dereksmart committed Nov 27, 2017
1 parent 3a743c2 commit f933eae
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions modules/stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,19 @@ function stats_convert_image_urls( $html ) {
return $html;
}

/**
* Callback for preg_replace_callback used in stats_convert_chart_urls()
*
* @since 5.6.0
*
* @param array $matches The matches resulting from the preg_replace_callback call.
* @return string The admin url for the chart.
*/
function jetpack_stats_convert_chart_urls_callback( $matches ) {
// If there is a query string, change the beginning '?' to a '&' so it fits into the middle of this query string.
return 'admin.php?page=stats&noheader&chart=' . $matches[1] . str_replace( '?', '&', $matches[2] );
}

/**
* Stats Convert Chart URLs.
*
Expand All @@ -662,13 +675,11 @@ function stats_convert_image_urls( $html ) {
* @return string
*/
function stats_convert_chart_urls( $html ) {
$html = preg_replace_callback( '|https?://[-.a-z0-9]+/wp-includes/charts/([-.a-z0-9]+).php(\??)|',
create_function(
'$matches',
// If there is a query string, change the beginning '?' to a '&' so it fits into the middle of this query string.
'return "admin.php?page=stats&noheader&chart=" . $matches[1] . str_replace( "?", "&", $matches[2] );'
),
$html );
$html = preg_replace_callback(
'|https?://[-.a-z0-9]+/wp-includes/charts/([-.a-z0-9]+).php(\??)|',
'jetpack_stats_convert_chart_urls_callback',
$html
);
return $html;
}

Expand Down

0 comments on commit f933eae

Please sign in to comment.