Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions admin/migrate/class-boldgrid-backup-admin-migrate-rx-rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,33 @@ public function validate_url( $request ) {
), 200 );
}

$links = explode( ',', $headers['link'] );
$wp_json_link = array_filter( $links, function( $link ) {
return false !== strpos( $link, 'rel="https://api.w.org/"' );
} );
// Make sure we have an array of link‐headers.
$raw_links = isset( $headers['link'] )
? (array) $headers['link']
: [];

// Flatten all comma-separated links into one array.
$all_links = [];
foreach ( $raw_links as $header ) {
// explode may yield spaces around each link, so trim().
$parts = array_map( 'trim', explode( ',', $header ) );
$all_links = array_merge( $all_links, $parts );
}

// Find the first link with rel="https://api.w.org/".
$wp_json_link = null;
foreach ( $all_links as $link ) {
if ( false !== strpos( $link, 'rel="https://api.w.org/"' ) ) {
$wp_json_link = $link;
break;
}
}

if ( $wp_json_link ) {
if ( preg_match( '/<([^>]+)>/', $wp_json_link, $m ) ) {
$wp_json_url = $m[1];
}
}

$rest_api_error_response = new WP_REST_Response( array(
'error' => true,
Expand All @@ -390,10 +413,6 @@ public function validate_url( $request ) {
return $rest_api_error_response;
}

preg_match('/<([^>]+)>/', $wp_json_link[0], $matches );

$wp_json_url = $matches[1];

$wp_json_response = wp_remote_get(
$wp_json_url,
array(
Expand Down
35 changes: 29 additions & 6 deletions admin/migrate/class-boldgrid-backup-admin-migrate-util.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,14 +618,37 @@ public function get_site_rest_url( $site_url ) {
return $response;
}
$headers = $response['headers']->getAll();
$links = explode( ',', $headers['link'] );
$wp_json_link = array_filter( $links, function( $link ) {
return false !== strpos( $link, 'rel="https://api.w.org/"' );
} );
// Make sure we have an array of link‐headers
$raw_links = isset( $headers['link'] )
? (array) $headers['link']
: [];

// Flatten all comma-separated links into one array
$all_links = [];
foreach ( $raw_links as $header ) {
// explode may yield spaces around each link, so trim()
$parts = array_map( 'trim', explode( ',', $header ) );
$all_links = array_merge( $all_links, $parts );
}

// Find the first link with rel="https://api.w.org/"
$wp_json_link = null;
foreach ( $all_links as $link ) {
if ( false !== strpos( $link, 'rel="https://api.w.org/"' ) ) {
$wp_json_link = $link;
break;
}
}

if ( $wp_json_link ) {
if ( preg_match( '/<([^>]+)>/', $wp_json_link, $m ) ) {
$wp_json_url = $m[1];
}
}

preg_match('/<([^>]+)>/', $wp_json_link[0], $matches );

return $matches[1];
return $wp_json_url;
}

/**
Expand Down Expand Up @@ -1583,4 +1606,4 @@ public function format_time( $seconds ) {
// Less than 1 hour
return date_i18n( 'i:s', mktime( 0, 0, $seconds ) );
}
}
}