WP_REST_Server::get_data_for_route() – Retrieves publicly-visible data for the route.

You appear to be a bot. Output may be restricted

Description

Retrieves publicly-visible data for the route.

Usage

$array|null = WP_REST_Server::get_data_for_route( $route, $callbacks, $context );

Parameters

$route
( string ) required – Route to get data for.
$callbacks
( array ) required – Callbacks to convert to data.
$context
( string ) optional default: view – Optional. Context for the data. Accepts 'view' or 'help'. Default 'view'.

Returns

array|null Data for the route, or null if no publicly-visible data.

Source

File name: wordpress/wp-includes/rest-api/class-wp-rest-server.php


Lines:

1 to 82 of 82
  public function get_data_for_route( $route, $callbacks, $context = 'view' ) {
    $data = array(
      'namespace' => '',
      'methods'   => array(),
      'endpoints' => array(),
    );

    $allow_batch = false;

    if ( isset( $this->route_options[ $route ] ) ) {
      $options = $this->route_options[ $route ];

      if ( isset( $options['namespace'] ) ) {
        $data['namespace'] = $options['namespace'];
      }

      $allow_batch = isset( $options['allow_batch'] ) ? $options['allow_batch'] : false;

      if ( isset( $options['schema'] ) && 'help' === $context ) {
        $data['schema'] = call_user_func( $options['schema'] );
      }
    }

    $allowed_schema_keywords = array_flip( rest_get_allowed_schema_keywords() );

    $route = preg_replace( '#\(\?P<(\w+?)>.*?\)#', '{$1}', $route );

    foreach ( $callbacks as $callback ) {
      // Skip to the next route if any callback is hidden.
      if ( empty( $callback['show_in_index'] ) ) {
        continue;
      }

      $data['methods'] = array_merge( $data['methods'], array_keys( $callback['methods'] ) );
      $endpoint_data   = array(
        'methods' => array_keys( $callback['methods'] ),
      );

      $callback_batch = isset( $callback['allow_batch'] ) ? $callback['allow_batch'] : $allow_batch;

      if ( $callback_batch ) {
        $endpoint_data['allow_batch'] = $callback_batch;
      }

      if ( isset( $callback['args'] ) ) {
        $endpoint_data['args'] = array();

        foreach ( $callback['args'] as $key => $opts ) {
          if ( is_string( $opts ) ) {
            $opts = array( $opts => 0 );
          } elseif ( ! is_array( $opts ) ) {
            $opts = array();
          }
          $arg_data             = array_intersect_key( $opts, $allowed_schema_keywords );
          $arg_data['required'] = ! empty( $opts['required'] );

          $endpoint_data['args'][ $key ] = $arg_data;
        }
      }

      $data['endpoints'][] = $endpoint_data;

      // For non-variable routes, generate links.
      if ( ! str_contains( $route, '{' ) ) {
        $data['_links'] = array(
          'self' => array(
            array(
              'href' => rest_url( $route ),
            ),
          ),
        );
      }
    }

    if ( empty( $data['methods'] ) ) {
      // No methods supported, hide the route.
      return null;
    }

    return $data;
  }
 

 View on GitHub View on Trac