Make WordPress Core


Ignore:
Timestamp:
07/19/2022 04:20:54 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Use consistent placement for ::prepare_links() methods.

This moves the ::prepare_links() methods in REST API classes next to ::prepare_item_for_response() where they are used, to bring some consistency across the classes and make code navigation easier.

Includes wrapping some long lines for better readability.

Follow-up to [52079], [52051], [52342], [53721], [53722].

See #55647.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-locations-controller.php

    r52079 r53724  
    209209
    210210    /**
     211     * Prepares links for the request.
     212     *
     213     * @since 5.9.0
     214     *
     215     * @param stdClass $location Menu location.
     216     * @return array Links for the given menu location.
     217     */
     218    protected function prepare_links( $location ) {
     219        $base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
     220
     221        // Entity meta.
     222        $links = array(
     223            'self'       => array(
     224                'href' => rest_url( trailingslashit( $base ) . $location->name ),
     225            ),
     226            'collection' => array(
     227                'href' => rest_url( $base ),
     228            ),
     229        );
     230
     231        $locations = get_nav_menu_locations();
     232        $menu      = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0;
     233        if ( $menu ) {
     234            $path = rest_get_route_for_term( $menu );
     235            if ( $path ) {
     236                $url = rest_url( $path );
     237
     238                $links['https://api.w.org/menu'][] = array(
     239                    'href'       => $url,
     240                    'embeddable' => true,
     241                );
     242            }
     243        }
     244
     245        return $links;
     246    }
     247
     248    /**
    211249     * Retrieves the menu location's schema, conforming to JSON Schema.
    212250     *
     
    261299        );
    262300    }
    263 
    264     /**
    265      * Prepares links for the request.
    266      *
    267      * @since 5.9.0
    268      *
    269      * @param stdClass $location Menu location.
    270      * @return array Links for the given menu location.
    271      */
    272     protected function prepare_links( $location ) {
    273         $base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
    274 
    275         // Entity meta.
    276         $links = array(
    277             'self'       => array(
    278                 'href' => rest_url( trailingslashit( $base ) . $location->name ),
    279             ),
    280             'collection' => array(
    281                 'href' => rest_url( $base ),
    282             ),
    283         );
    284 
    285         $locations = get_nav_menu_locations();
    286         $menu      = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0;
    287         if ( $menu ) {
    288             $path = rest_get_route_for_term( $menu );
    289             if ( $path ) {
    290                 $url = rest_url( $path );
    291 
    292                 $links['https://api.w.org/menu'][] = array(
    293                     'href'       => $url,
    294                     'embeddable' => true,
    295                 );
    296             }
    297         }
    298 
    299         return $links;
    300     }
    301301}
Note: See TracChangeset for help on using the changeset viewer.