@@ -471,9 +471,22 @@ public function replace_url($url, $current_mapping = null) {
471471 $ current_mapping = $ this ->current_mapping ;
472472 }
473473
474+ // If we don't have a valid mapping, return the original URL
475+ if (!$ current_mapping ) {
476+ return $ url ;
477+ }
478+
479+ // Get the site associated with the mapping
480+ $ site = $ current_mapping ->get_site ();
481+
482+ // If we don't have a valid site, return the original URL
483+ if (!$ site ) {
484+ return $ url ;
485+ }
486+
474487 // Replace the domain
475488 $ domain_base = wp_parse_url ($ url , PHP_URL_HOST );
476- $ domain = rtrim ($ domain_base . '/ ' . $ current_mapping -> get_site () ->get_path (), '/ ' );
489+ $ domain = rtrim ($ domain_base . '/ ' . $ site ->get_path (), '/ ' );
477490 $ regex = '#^(\w+://) ' . preg_quote ($ domain , '# ' ) . '#i ' ;
478491 $ mangled = preg_replace ($ regex , '${1} ' . $ current_mapping ->get_domain (), $ url );
479492
@@ -508,10 +521,16 @@ public function mangle_url($url, $path = '/', $orig_scheme = '', $site_id = 0) {
508521
509522 $ current_mapping = $ this ->current_mapping ;
510523
524+ // Check if we have a valid mapping for this site
511525 if (empty ($ current_mapping ) || $ current_mapping ->get_site_id () !== $ site_id ) {
512526 return $ url ;
513527 }
514528
529+ // Check if the site exists
530+ if (!$ current_mapping ->get_site ()) {
531+ return $ url ;
532+ }
533+
515534 return $ this ->replace_url ($ url );
516535 }
517536
@@ -524,6 +543,11 @@ public function mangle_url($url, $path = '/', $orig_scheme = '', $site_id = 0) {
524543 */
525544 public function fix_srcset ($ sources ) {
526545
546+ // Check if we have a valid mapping
547+ if (empty ($ this ->current_mapping ) || !$ this ->current_mapping ->get_site ()) {
548+ return $ sources ;
549+ }
550+
527551 foreach ($ sources as &$ source ) {
528552 $ sources [ $ source ['value ' ] ]['url ' ] = $ this ->replace_url ($ sources [ $ source ['value ' ] ]['url ' ]);
529553 }
0 commit comments