Ticket #34729: 34729.4.diff
File 34729.4.diff, 3.2 KB (added by , 9 years ago) |
---|
-
src/wp-includes/rest-api/class-wp-rest-response.php
136 136 * @return array List of links. 137 137 */ 138 138 public function get_links() { 139 return $this->links; 139 140 $curies = $this->get_curies(); 141 $used_curies = array(); 142 $links = array(); 143 144 foreach ( $this->links as $rel => $items ) { 145 146 // Convert $rel URIs to their compact versions if they exist. 147 foreach ( $curies as $curie ) { 148 $href_prefix = substr( $curie['href'], 0, strpos( $curie['href'], '{rel}' ) ); 149 if ( strpos( $rel, $href_prefix ) !== 0 ) { 150 continue; 151 } 152 $used_curies[ $curie['name'] ] = $curie; 153 154 // Relation now changes from '$uri' to '$curie:$relation' 155 $rel_regex = str_replace( '\{rel\}', '([\w]+)', preg_quote( $curie['href'], '!' ) ); 156 preg_match( '!' . $rel_regex . '!', $rel, $matches ); 157 if ( $matches ) { 158 $rel = $curie['name'] . ':' . $matches[1]; 159 } 160 break; 161 } 162 163 $links[ $rel ] = array(); 164 165 foreach ( $items as $item ) { 166 $attributes = $item['attributes']; 167 $attributes['href'] = $item['href']; 168 $links[ $rel ][] = $attributes; 169 } 170 } 171 172 // Push the curies onto the start of the links array. 173 if ( $used_curies ) { 174 $links = array_merge( array( 'curies' => array_values( $used_curies ) ), $links ); 175 } 176 177 return $links; 140 178 } 141 179 142 180 /** -
src/wp-includes/rest-api/class-wp-rest-server.php
453 453 * @return array Map of link relation to list of link hashes. 454 454 */ 455 455 public static function get_response_links( $response ) { 456 $links = $response->get_links(); 457 458 if ( empty( $links ) ) { 459 return array(); 460 } 461 462 // Convert links to part of the data. 463 $data = array(); 464 $curies = $response->get_curies(); 465 $used_curies = array(); 466 467 foreach ( $links as $rel => $items ) { 468 469 // Convert $rel URIs to their compact versions if they exist. 470 foreach ( $curies as $curie ) { 471 $href_prefix = substr( $curie['href'], 0, strpos( $curie['href'], '{rel}' ) ); 472 if ( strpos( $rel, $href_prefix ) !== 0 ) { 473 continue; 474 } 475 $used_curies[ $curie['name'] ] = $curie; 476 477 // Relation now changes from '$uri' to '$curie:$relation' 478 $rel_regex = str_replace( '\{rel\}', '([\w]+)', preg_quote( $curie['href'], '!' ) ); 479 preg_match( '!' . $rel_regex . '!', $rel, $matches ); 480 if ( $matches ) { 481 $rel = $curie['name'] . ':' . $matches[1]; 482 } 483 break; 484 } 485 486 $data[ $rel ] = array(); 487 488 foreach ( $items as $item ) { 489 $attributes = $item['attributes']; 490 $attributes['href'] = $item['href']; 491 $data[ $rel ][] = $attributes; 492 } 493 } 494 495 // Push the curies onto the start of the links array. 496 if ( $used_curies ) { 497 $data = array_merge( array( 'curies' => array_values( $used_curies ) ), $data ); 498 } 499 500 return $data; 456 return $response->get_links(); 501 457 } 502 458 503 459 /**