Wordpress REST API,如何获取/发布架构?

时间:2018-10-08 19:50:34

标签: wordpress wordpress-rest-api

我创建了一个自定义终结点,该终结点基本上只是从每个类别中抓取一些不同的帖子并返回它。该端点工作正常,但是返回的每个帖子的架构与您单击默认的内置/ 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:

默认/ posts模式

_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:

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

尽管这个问题比较老,但是我很难找到自己获取模式的答案,所以我想分享我发现的东西。

  • 简短答案(以获取架构信息):对路由请求使用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') );

如果您真正想要的是使用自定义表(或posts表的另一个端点)创建新的路由和端点,则应执行以下操作:

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') );

当然,这只是一个基本概述。这是一个警告:

这些是非常有用的链接,可以最终将所有内容整合在一起: