: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
public $custom_post_query_vars = [];
public $taxonomies_query_vars = [];
private $active_languages;
public function __construct() {
if ( did_action( 'init' ) ) {
$this->init_query_vars();
// init_query_vars is using $wp_taxonomies.
// We have to change priority of our action
// to make sure that all custom taxonomies are already registered.
add_action( 'init', [ $this, 'init_query_vars' ], 1000 );
public function init_query_vars() {
global $wp_post_types, $wp_taxonomies;
// Custom posts query vars.
foreach ( $wp_post_types as $k => $v ) {
if ( 'post' === $k || 'page' === $k ) {
$this->custom_post_query_vars[ $k ] = $v->query_var;
// Taxonomies query vars.
foreach ( $wp_taxonomies as $k => $v ) {
if ( 'category' === $k ) {
if ( 'post_tag' === $k && ! $v->query_var ) {
$tag_base = get_option( 'tag_base', 'tag' );
$v->query_var = $tag_base;
$this->taxonomies_query_vars[ $k ] = $v->query_var;
public function _process_generic_text( $source_text, &$alp_broken_links ) {
global $wpdb, $wp_rewrite, $sitepress, $sitepress_settings;
$sitepress_settings = $sitepress->get_settings();
$default_language = $sitepress->get_default_language();
$current_language = $sitepress->get_current_language();
md5( implode( '', $alp_broken_links ) ),
$cache_key = md5( wp_json_encode( $cache_key_args ) );
$cache_group = '_process_generic_text';
$text = WPML_Non_Persistent_Cache::get( $cache_key, $cache_group, $found );
$filtered_icl_post_language = filter_input( INPUT_POST, 'icl_post_language', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$block_protector = new \WPML\AbsoluteLinks\BlockProtector();
$text = $block_protector->protect( $source_text );
// We need to loop over each language so we create sticky links for all languages.
$this->active_languages = array_keys( $sitepress->get_active_languages() );
$current_language = empty( $filtered_icl_post_language ) ? $current_language : $filtered_icl_post_language;
if ( ! empty( $current_language ) ) {
$key = array_search( $current_language, $this->active_languages, true );
unset( $this->active_languages[ $key ] );
array_unshift( $this->active_languages, $current_language );
$blacklist_requests = new WPML_Absolute_Links_Blacklist(
apply_filters( 'wpml_sl_blacklist_requests', [], $sitepress )
foreach ( $this->active_languages as $test_language ) {
$rewrite = $this->initialize_rewrite( $current_language, $default_language, $sitepress );
$home_url = $sitepress->language_url( $test_language );
if ( 3 === $sitepress_settings['language_negotiation_type'] ) {
$home_url = preg_replace( '#\?lang=([a-z-]+)#i', '', $home_url );
$home_url = str_replace( '?', '\?', $home_url );
if ( $sitepress_settings['urls']['directory_for_default_language'] && $test_language === $default_language ) {
$home_url = str_replace( $default_language . '/', '', $home_url );
$int1 = preg_match_all( '@<a([^>]*)href="((' . rtrim( $home_url, '/' ) . ')?/([^"^>^\[^\]]+))"([^>]*)>@i', $text, $alp_matches1 );
$int2 = preg_match_all( '@<a([^>]*)href=\'((' . rtrim( $home_url, '/' ) . ')?/([^\'^>^\[^\]]+))\'([^>]*)>@i', $text, $alp_matches2 );
for ( $i = 0; $i < 6; $i ++ ) {
$alp_matches[ $i ] = array_merge( (array) $alp_matches1[ $i ], (array) $alp_matches2[ $i ] );
$url_parts = wp_parse_url( $this->get_home_url_with_no_lang_directory() );
$url_parts['path'] = isset( $url_parts['path'] ) ? $url_parts['path'] : '';
foreach ( $alp_matches[4] as $k => $dir_path ) {
if ( 0 === strpos( $dir_path, 'wp-content' ) ) {
list( $lang, $dir_path ) = $this->extract_lang_from_path( $sitepress_settings, $default_language, $dir_path );
$req_uri = '/' . $dir_path;
$req_uri_array = explode( '?', $req_uri );
$req_uri = $req_uri_array[0];
if ( isset( $req_uri_array[1] ) ) {
$req_uri_params = $req_uri_array[1];
$req_uri_array = explode( '#', $req_uri );
$req_uri = $req_uri_array[0];
$anchor_output = isset( $req_uri_array[1] ) ? '#' . $req_uri_array[1] : '';
$home_path = wp_parse_url( get_home_url() );
if ( isset( $home_path['path'] ) ) {
$home_path = $home_path['path'];
$home_path = trim( $home_path, '/' );
$req_uri = str_replace( $pathinfo, '', rawurldecode( $req_uri ) );
$req_uri = trim( $req_uri, '/' );
$req_uri = preg_replace( "|^$home_path|", '', $req_uri );
$req_uri = trim( $req_uri, '/' );
$pathinfo = trim( $pathinfo, '/' );
$pathinfo = preg_replace( "|^$home_path|", '', $pathinfo );
$pathinfo = trim( $pathinfo, '/' );
if ( ! empty( $pathinfo ) && ! preg_match( '|^.*' . $wp_rewrite->index . '$|', $pathinfo ) ) {
// If the request uri is the index, blank it out so that we don't try to match it against a rule.
if ( $req_uri === $wp_rewrite->index ) {
if ( ! $request || $blacklist_requests->is_blacklisted( $request ) ) {
$request_match = $request;
$permalink_query_vars = [];
foreach ( (array) $rewrite as $match => $query ) {
// If the requesting file is the anchor of the match, prepend it to the path info.
if ( ( ! empty( $req_uri ) ) && ( strpos( $match, $req_uri ) === 0 ) && ( $req_uri !== $request ) ) {
$request_match = $req_uri . '/' . $request;
if ( preg_match( "!^$match!", $request_match, $matches ) || preg_match( "!^$match!", urldecode( $request_match ), $matches ) ) {
// Trim the query of everything up to the '?'.
$query = preg_replace( '!^.+\?!', '', $query );
// Substitute the substring matches into the query.
$query = addslashes( WP_MatchesMapRegex::apply( $query, $matches ) );
parse_str( $query, $permalink_query_vars );
if ( isset( $permalink_query_vars['pagename'] ) ) {
$get_page_by_path = new WPML_Get_Page_By_Path( $wpdb, $sitepress, new WPML_Debug_BackTrace( null, 7 ) );
$page_by_path = $get_page_by_path->get( $permalink_query_vars['pagename'], $test_language );
$post_name = $permalink_query_vars['pagename'];
if ( ! empty( $page_by_path->post_type ) ) {
} elseif ( isset( $permalink_query_vars['name'] ) ) {
$post_name = $permalink_query_vars['name'];
} elseif ( isset( $permalink_query_vars['category_name'] ) ) {
$category_name = $permalink_query_vars['category_name'];
} elseif ( isset( $permalink_query_vars['p'] ) ) { // Case or /archives/%post_id.
list( $post_type, $post_name ) = $wpdb->get_row(
$wpdb->prepare( "SELECT post_type, post_name FROM {$wpdb->posts} WHERE id=%d", $permalink_query_vars['p'] ),
if ( empty( $this->custom_post_query_vars ) || empty( $this->taxonomies_query_vars ) ) {
$this->init_query_vars();
foreach ( $this->custom_post_query_vars as $query_vars_key => $query_vars_value ) {
if ( isset( $permalink_query_vars[ $query_vars_value ] ) ) {
$post_name = $permalink_query_vars[ $query_vars_value ];
$post_type = $query_vars_key;
foreach ( $this->taxonomies_query_vars as $query_vars_value ) {
if ( isset( $permalink_query_vars[ $query_vars_value ] ) ) {
$tax_name = $permalink_query_vars[ $query_vars_value ];
$tax_type = $query_vars_value;
if ( $post_name && isset( $post_type ) ) {
$get_page_by_path = new WPML_Get_Page_By_Path( $wpdb, $sitepress, new WPML_Debug_BackTrace( null, 7 ) );
$p = $get_page_by_path->get( $post_name, $test_language, OBJECT, $post_type );
if ( empty( $p ) ) { // Fail safe.
$post_id = url_to_postid( $home_path . '/' . $post_name );
$p = get_post( $post_id );
$offsite_url = get_post_meta( $p->ID, '_cms_nav_offsite_url', true );
if ( 'page' === $p->post_type && $offsite_url ) {
$def_url = $this->get_regex_replacement_offline(
$sitepress_settings['language_negotiation_type'],
$def_url = $this->get_regex_replacement(
'page' === $p->post_type ? 'page_id' : 'p',
$sitepress_settings['language_negotiation_type'],
$alp_broken_links[ $alp_matches[2][ $k ] ] = [];
$name = wpml_like_escape( $post_name );
$p = $this->_get_ids_and_post_types( $name );
foreach ( $p as $post_suggestion ) {
if ( 'page' === $post_suggestion->post_type ) {
$alp_broken_links[ $alp_matches[2][ $k ] ]['suggestions'][] = [
'absolute' => '/' . ltrim( $url_parts['path'], '/' ) . '?' . $qvid . '=' . $post_suggestion->ID,
'perma' => '/' . ltrim( str_replace( site_url(), '', get_permalink( $post_suggestion->ID ) ), '/' ),
} elseif ( $category_name ) {
if ( false !== strpos( $category_name, '/' ) ) {
$splits = explode( '/', $category_name );
$category_name = array_pop( $splits );
$category_parent = array_pop( $splits );
$category_parent_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM {$wpdb->terms} WHERE slug=%s", $category_parent ) );
$c = $wpdb->get_row( $wpdb->prepare( "SELECT t.term_id FROM {$wpdb->terms} t JOIN {$wpdb->term_taxonomy} x ON x.term_id=t.term_id AND x.taxonomy='category' AND x.parent=%d AND t.slug=%s", $category_parent_id, $category_name ) );
$c = $wpdb->get_row( $wpdb->prepare( "SELECT term_id FROM {$wpdb->terms} WHERE slug=%s", $category_name ) );
$def_url = $this->get_regex_replacement(
$sitepress_settings['language_negotiation_type'],
} elseif ( isset( $name ) ) {
$alp_broken_links[ $alp_matches[2][ $k ] ] = [];
$wpdb->prepare( "SELECT term_id FROM {$wpdb->terms} WHERE slug LIKE %s", [ $name . '%' ] )
foreach ( $c as $cat_suggestion ) {
$perma = '/' . ltrim( str_replace( get_home_url(), '', get_category_link( $cat_suggestion->term_id ) ), '/' );
$alp_broken_links[ $alp_matches[2][ $k ] ]['suggestions'][] = [
'absolute' => '?cat_ID=' . $cat_suggestion->term_id,
} elseif ( $tax_name && isset( $tax_type ) ) {
$def_url = $this->get_regex_replacement(
$sitepress_settings['language_negotiation_type'],
if ( ! empty( $def_url ) ) {
$text = preg_replace( array_keys( $def_url ), array_values( $def_url ), $text );
$tx_qvs = ! empty( $this->taxonomies_query_vars ) && is_array( $this->taxonomies_query_vars ) ? '|' . join( '|', $this->taxonomies_query_vars ) : '';
$post_qvs = ! empty( $this->custom_posts_query_vars ) && is_array( $this->custom_posts_query_vars ) ? '|' . join( '|', $this->custom_posts_query_vars ) : '';
$int = preg_match_all( '@href=[\'"](' . rtrim( get_home_url(), '/' ) . '/?\?(p|page_id' . $tx_qvs . $post_qvs . ')=([0-9a-z-]+)(#.+)?)[\'"]@i', $text, $matches2 );
$url_parts = wp_parse_url( rtrim( get_home_url(), '/' ) . '/' );
$text = preg_replace( '@href=[\'"](' . rtrim( get_home_url(), '/' ) . '/?\?(p|page_id' . $tx_qvs . $post_qvs . ')=([0-9a-z-]+)(#.+)?)[\'"]@i', 'href="/' . ltrim( $url_parts['path'], '/' ) . '?$2=$3$4"', $text );
$text = $block_protector->unProtect( $text );
WPML_Non_Persistent_Cache::set( $cache_key, $text, $cache_group );
private function get_home_url_with_no_lang_directory() {
global $sitepress, $sitepress_settings;
$sitepress_settings = $sitepress->get_settings();
$home_url = rtrim( get_home_url(), '/' );
if ( 1 === $sitepress_settings['language_negotiation_type'] ) {
// Strip lang directory from end if it's there.
$exp = explode( '/', $home_url );
if ( $this->does_lang_exist( $lang ) ) {
$home_url = substr( $home_url, 0, strlen( $home_url ) - strlen( $lang ) );
private function does_lang_exist( $lang ) {
return in_array( $lang, $this->active_languages, true );
public function _get_ids_and_post_types( $name ) {
$name = rawurlencode( $name );
if ( ! isset( $cache[ $name ] ) ) {
$cache[ $name ] = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_type FROM {$wpdb->posts} WHERE post_name LIKE %s AND post_type IN('post','page')", $name . '%' ) );
private function initialize_rewrite( $current_language, $default_language, $sitepress ) {
$key = $current_language . $default_language;
$rewrite = WPML_Non_Persistent_Cache::get( $key, __CLASS__, $found );
if ( ! isset( $wp_rewrite ) ) {
require_once ABSPATH . WPINC . '/rewrite.php';
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$wp_rewrite = new WP_Rewrite();
if ( $current_language === $default_language ) {
$rewrite = $wp_rewrite->wp_rewrite_rules();
remove_filter( 'option_rewrite_rules', [ $sitepress, 'rewrite_rules_filter' ] );
if ( class_exists( 'WPML_Slug_Translation' ) ) {
remove_filter( 'option_rewrite_rules', [ 'WPML_Slug_Translation', 'rewrite_rules_filter' ], 1 );
$rewrite = $wp_rewrite->wp_rewrite_rules();
if ( class_exists( 'WPML_Slug_Translation' ) ) {
add_filter( 'option_rewrite_rules', [ 'WPML_Slug_Translation', 'rewrite_rules_filter' ], 1, 1 );
$rewrite = $this->all_rewrite_rules( $rewrite );
WPML_Non_Persistent_Cache::set( $key, $rewrite, __CLASS__ );
public function all_rewrite_rules( $rewrite ) {
if ( ! class_exists( 'WPML\ST\SlugTranslation\Hooks\Hooks' ) ) {
$active_languages = $sitepress->get_active_languages();
$current_language = $sitepress->get_current_language();
$default_language = $sitepress->get_default_language();
$cache_keys = [ $current_language, $default_language ];
$cache_keys[] = md5( wp_json_encode( $active_languages ) );
$cache_keys[] = md5( wp_json_encode( $rewrite ) );
$cache_key = implode( ':', $cache_keys );
$cache_group = 'all_rewrite_rules';
$final_rules = WPML_Non_Persistent_Cache::get( $cache_key, $cache_group, $cache_found );
foreach ( $active_languages as $next_language ) {
if ( $next_language['code'] === $default_language ) {
$sitepress->switch_lang( $next_language['code'] );
$translated_rules = ( new \WPML\ST\SlugTranslation\Hooks\HooksFactory() )->create()->filter( $final_rules );
if ( is_array( $translated_rules ) && is_array( $final_rules ) ) {
$new_rules = array_diff_assoc( $translated_rules, $final_rules );
$final_rules = array_merge( $new_rules, $final_rules );
$sitepress->switch_lang( $current_language );
WPML_Non_Persistent_Cache::set( $cache_key, $final_rules, $cache_group );
private function get_regex_replacement(