Wordpress REST API,如何获取/post schema?

问题描述 投票:0回答:1

我创建了一个自定义端点,它基本上只是从每个类别中获取一些不同的帖子并将其返回。此端点工作正常,但返回的每个帖子的架构与您刚刚点击默认的内置 /posts 端点时不同。我必须做什么才能保持架构一致?

我感觉 get_posts 是问题所在,但我一直在进行文档爬行,而且我似乎找不到任何使用与 /posts 相同架构的内容。

// How the endpoint is built.
function anon_content_api_posts($category) {
  $posts = get_posts(
    array(
      'posts_per_page' => 3,
      'tax_query' => array(
          array(
              'taxonomy' => 'content_category',
              'field' => 'term_id',
              'terms' => $category->term_id,
          )
      )
    )
  );
  $posts = array_map('get_extra_post_data', $posts); // just me appending more data to each post.
  return $posts;
}

function anon_content_api_resources() {
  $data = array();

  $categories = get_categories(
    array(
      'taxonomy' => 'content_category',
    )
  );

  foreach($categories as $category) {
    $category->posts = anon_content_api_posts($category);
    array_push($data, $category);
  }

  return $data;
}

自定义端点架构

ID: 
author: 
comment_count: 
comment_status: 
featured_image_url: 
filter: 
guid: 
menu_order: 
ping_status: 
pinged: 
post_author: 
post_content: 
post_content_filtered:
post_date: 
post_date_gmt:
post_excerpt:
post_mime_type:
post_modified:
post_modified_gmt:
post_name:
post_parent:
post_password:
post_status:
post_title:
post_type:
to_ping:

默认/帖子架构

_links:
author:
categories:
comment_status:
content:
date:
date_gmt:
excerpt:
featured_image_url:
featured_media:
format:
guid:
id:
link:
meta:
modified:
modified_gmt:
ping_status:
slug:
status:
sticky:
task_category:
template:
title:
type:

如有任何帮助,我们将不胜感激!

php wordpress wordpress-rest-api
1个回答
1
投票

虽然这个问题比较老,但我自己很难找到获取模式的答案,所以我想分享我发现的内容。

  • 简短回答(获取架构信息):在路由请求上使用 OPTIONS 方法

您正在处理已经存在的端点 /wp/v2/posts,因此您可能想要修改现有路由的响应,您可以使用 register_rest_field() 来完成(这应该为所有公开的帖子列保留适当的模式/字段,但也允许您修改现在公开的字段的架构):

类似这样的:

function demo_plugin_extend_route_rest_api()
{
register_rest_field( 
    'post', 
    'unexposed_column_in_wp_posts_table', 
    array(
        'get_callback' => function( $post_arr ) {
            $post_obj = get_post( $post_arr['id'] );
            return $post_obj->unexposed_column_in_wp_posts_table;
        },
        'update_callback' => function( $unexposed_column_in_wp_posts_table, $post_obj ) {
            $ret = wp_update_post( 
                array(
                    // ID is the name of the column in the posts table
                    // unexposed_column_in_wp_posts_table should be replaced throughout with the unexposed column in the posts table
                    'ID'    => $post_obj->ID,
                    'unexposed_column_in_wp_posts_table' => $unexposed_column_in_wp_posts_table
                ) 
            );
            if ( false === $ret ) 
            {
                return new WP_Error(
                    'rest_post_unexposed_column_in_wp_posts_table_failed',
                    __( 'Failed to update unexposed_column_in_wp_posts_table.' ),
                    array( 'status' => 500 )
                );
            }
            return true;
        },
        'schema' => array(
            'description' => __( 'Post unexposed_column_in_wp_posts_table' ),
            'type'        => 'integer'
            ),
        ) 
    );
}

// Not-in-class call (use only this add_action or the one below, but not both)
add_action( 'rest_api_init', 'demo_plugin_extend_route_rest_api' );

// In-class call
add_action( 'rest_api_init', array($this, 'demo_plugin_extend_route_rest_api') );

如果您真正想要的是使用自定义表(或帖子表的另一个端点)创建新的路线和端点,则类似这样的操作应该有效:

function demo_plugin_custom_rest_api()
{
//  Adding Custom Endpoints (add tables and fields not currently exposed)
//      register_rest_route()
//          $namespace (string) (Required) The first URL segment after core prefix. 
//                  Should be unique to your package/plugin.
//          $route (string) (Required) The base URL for route you are adding.
//          $args (array) (Optional) Either an array of options for the endpoint, or an 
//                  array of arrays for multiple methods.  Default value: array()
//              array:  If using schema element to define the schema, or multiple methods, 
//                      then wrap the 'methods', 'args', and 'permission_callback' in an array, 
//                      otherwise they do not need to be wrapped in an array.  Best practice 
//                      would be to wrap them in an array though
//                  'methods' (array | string):  GET, POST, DELETE, PUT, PATCH, etc.
//                  'args' (array) 
//                      '<schema property name>' (array) (ie. parameter name - if not including a schema 
//                              then can include field names as valid parameters as a way to 
//                              describe them):
//                          'default':  Used as the default value for the argument, if none is supplied 
//                              Note:  (if defined, then it will validate and sanitize regardless of if 
//                              the parameter is passed in query).
//                          'required':  If defined as true, and no value is passed for that argument, an 
//                              error will be returned. No effect if a default value is set, as the argument 
//                              will always have a value.
//                          'description':  Field Description
//                          'type':  Data Type
//                          'validate_callback' (function):  Used to pass a function that will be passed the 
//                              value of the argument. That function should return true if the value is valid, 
//                              and false if not.
//                          'sanitize_callback' (function):  Used to pass a function that is used to sanitize 
//                              the value of the argument before passing it to the main callback.           
//                  'permission_callback' (function):  Checks if the user can perform the 
//                      action (reading, updating, etc) before the real callback is called 
//              'schema' (callback function) (optional):  Defines the schema.  
//                      NOTE:  Can view this schema information by making OPTIONS method request.
//          $override (bool) (Optional) If the route already exists, should we override it? True overrides, 
//              false merges (with newer overriding if duplicate keys exist).  Default value: false
//
//  View your describe page at:  /wp-json/demo-plugin/v1
//  View your JSON data at: /wp-json/demo-plugin/v1/demo-plugin_options
//  View your schema at (with OPTIONS method) at:  /wp-json/demo-plugin/v1/demo-plugin_options
//      Note:  For a browser method to see OPTIONS Request in Firefox:
//              Inspect the JSON data endpoint (goto endpoint and click F12) 
//              > goto Network 
//              > find a GET request 
//              > click it 
//              > goto headers section 
//              > click Edit and Resend 
//              > change Method to OPTIONS 
//              > click Send 
//              > double click on last OPTIONS request 
//              > goto Response (the JSON data returned shows your schema)

register_rest_route( 
    'demo-plugin/v1', 
    '/demo-plugin_options/',                
    array(
        //  GET array options
        array(
            'methods' => array('GET'),
            'callback' => function ( WP_REST_Request $request ){
                // Get Data (here we are getting from options, but could be any data retrieval)
                $options_data = get_option('demo_option_name');  

                // Set $param
                $param = $request->get_params();

                // Do Other things based upon Params

                return $options_data;                       
            },
            'args' => array(    
                // Valid Parameters 
                'element_1' => array(                           
                    'description'=> 'Element text field',
                    'type'=> 'string',
                ),
                'element_color' => array(
                    'description'=> 'Element color select box',
                    'type'=> 'string',
                    )
                )
            ),
        //  POST array options
        array(
            'methods' => array('POST'),
            'callback' => function ( WP_REST_Request $request ){
                // Get Data (here we are getting from options, but could be any data retrieval)
                $options_data = get_option('demo_option_name');  

                // Set $param
                $param = $request->get_params();

                // Do Other things based upon Params

                if (is_array($param) && isset($param))
                {
                    foreach ($param as $k=>$v)
                    {
                        // $param is in an array($key => array($key => $value), ...)
                        if (is_array($v) && array_key_exists($k, $options_data) && array_key_exists($k, $v))
                        {
                            $options_data[$k] = $v;
                        }
                    }
                }

                update_option('demo_option_name', $options_data);

                return $options_data;                       
            },
            'args' => array(                        
                'element_1' => array(
                    'default' => '',
                    'required' => false,
                    'description'=> 'Element text field',
                    'type'=> 'string',
                    'validate_callback' => function($param, $request, $key) { //validation function },                          
                    'sanitize_callback' => function($param, $request, $key) { //sanitization function }
                ),          
                'element_color' => array(
                    'default' => 'red',
                    'required' => true,
                    'description'=> 'Element color select box',
                    'type'=> 'integer',
                    'validate_callback' => function($param, $request, $key) {
                        $colors = array('red', 'blue');

                        return in_array($param, $colors);                       
                    },
                    'sanitize_callback' => function($param, $request, $key) {  
                        // If it includes a default, and sanitize callback for other properties above are set, it seems to need it here as well
                        return true;
                    })
                ),
            'permission_callback' => function () {

                // See Capabilities here:  https://wordpress.org/support/article/roles-and-capabilities/

                $approved = current_user_can( 'activate_plugins' );

                return $approved;
            }
        ),
        'schema' => function() {
            $schema = array(
                // This tells the spec of JSON Schema we are using which is draft 4.
                '$schema' => 'http://json-schema.org/draft-04/schema#',
                // The title property marks the identity of the resource.
                'title' => 'demo-plugin_options',
                'type' => 'object',
                // In JSON Schema you can specify object properties in the properties attribute.
                'properties' => array(
                    'element_1' => array(
                        'description' => esc_html__( 'Element text field', 'demo-plugin' ),
                        'type' => 'string',
                        'context' => array( 'view', 'edit', 'embed' ),
                        'readonly' => false,
                        ),                  
                    'element_color' => array(
                        'description' => esc_html__( 'Element color select box', 'demo-plugin' ),
                        'type' => 'string',
                        'readonly' => false,
                        ),
                    ),
                );

            return $schema;
        })              
    );
}
// Not-in-class call (use only this add_action or the one below, but not both)
add_action( 'rest_api_init', 'demo_plugin_custom_rest_api' );

// In-class call
add_action( 'rest_api_init', array($this, 'demo_plugin_custom_rest_api') );

当然,这只是一个基本轮廓。这里有一个警告:

这些是非常有帮助的链接,最终为我自己把所有这些放在一起:

© www.soinside.com 2019 - 2024. All rights reserved.