Fix PHP 8.1 deprecations
-
I want to propose some updates to reduce the number of PHP 8.1-deprecation warnings & improve code quality:
In icegram/lite/classes/ig-mobile-detect.php, the match-function should be:
public function match($regex, $userAgent = null)
{
// In PHP 8.1+, passing null as subject to preg_match is deprecated.
$subject = (false === empty($userAgent)) ? $userAgent : $this->userAgent;
if (null === $subject) {
return false;
}
$match = (bool) preg_match(sprintf('#%s#is', $regex), $subject, $matches);
// If positive match is found, store the results for debug.
if ($match) {
$this->matchingRegex = $regex;
$this->matchesArray = $matches;
}
return $match;
}in Icegram/lite/class-icegram.php, the icegram_load_data()-function should be:
// Do not index Icegram campaigns / messages...
// Not using currently - made custom post types non public...
function icegram_load_data() {
global $post;
$sanitized_get_data = array();
// FILTER_SANITIZE_STRING is deprecated in recent PHP versions.
// Use a callback to sanitize each value instead.
$get_data = filter_input_array( INPUT_GET, FILTER_DEFAULT );
if ( ! empty( $get_data ) && is_array( $get_data ) ) {
$sanitized_get_data = array_map(
static function( $value ) {
if ( is_array( $value ) ) {
return array_map( 'sanitize_text_field', $value );
}
return is_scalar( $value ) ? sanitize_text_field( (string) $value ) : $value;
},
$get_data
);
}
$icegram_pre_data['ajax_url'] = admin_url( 'admin-ajax.php' );
$icegram_pre_data['post_obj'] = $sanitized_get_data;
$icegram_pre_data['post_obj']['is_home'] = ( is_home() || is_front_page() ) ? true : false;
$icegram_pre_data['post_obj']['page_id'] = is_object( $post ) && isset( $post->ID ) ? $post->ID : 0;
$icegram_pre_data['post_obj']['action'] = 'ig_display_messages';
$icegram_pre_data['post_obj']['shortcodes'] = $this->shortcode_instances;
$icegram_pre_data['post_obj']['cache_compatibility'] = $this->cache_compatibility;
$icegram_pre_data['post_obj']['device'] = $this->get_platform();
wp_register_script( 'icegram_main_js', $this->plugin_url . '/assets/js/main.min.js', array( 'jquery' ), $this->version, true );
if ( 'yes' === $this->cache_compatibility ) {
if ( ! wp_script_is( 'icegram_main_js' ) ) {
wp_enqueue_script( 'icegram_main_js' );
}
}
wp_localize_script( 'icegram_main_js', 'icegram_pre_data', $icegram_pre_data );
}These both code-improvements will fix the issues with deprecated FILTER_SANITIZE_STRING and for using the “preg_match()”-function correctly.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
You must be logged in to reply to this topic.