2016-12-03 3 views
0

私の考えは、カテゴリのリストを取得することですが、必要なフィールドのみを使用します。リクエストパラメータ(exclude、include、order、per_pageなど)は、REST APIを使用する場合と同じように動作する必要があります。WP REST API既存のコントローラを拡張する

私はWP_REST_Terms_Controller.phpコントローラを拡張しようとしています。ここで私が持っているコードは、これまでです:

<?php 

if (! class_exists('WP_REST_Controller')) { 
    require_once ABSPATH. 'wp-content/plugins/rest-api/lib/endpoints/' . '/class-wp-rest-controller.php'; 
} 
if (! class_exists('WP_REST_Taxonomies_Controller')) { 
    require_once ABSPATH. 'wp-content/plugins/rest-api/lib/endpoints/' . '/class-wp-rest-terms-controller.php'; 
} 


class MyPlugin_Categories_Controller extends WP_REST_Terms_Controller 
{ 

    public function __construct() { 
     parent::__construct('category'); 
    } 

    public function register_routes() { 
     $version = '1'; 
     $namespace = 'myplugin-api/v' . $version; 
     $base = 'categories'; 
     register_rest_route($namespace, '/' . $base, array(
      array(
       'methods'   => WP_REST_Server::READABLE, 
       'callback'  => array($this, 'get_items'), 
       'args'   => array(

       ), 
      ), 
     )); 
    } 

    /** 
    * Get a collection of items 
    * 
    * @param WP_REST_Request $request Full data about the request. 
    * @return WP_Error|WP_REST_Response 
    */ 
    public function get_items($request) { 

     var_dump($request); 
     $items = parent::get_items($request); 
     $data = array(); 
     foreach($items as $item) { 
      $itemdata = $this->prepare_item_for_response($item, $request); 
      $data[] = $this->prepare_response_for_collection($itemdata); 
     } 

     return $data; 
    } 

} 

のgetItem()メソッドを実行すると、次のエラーが発生します(!)

警告: でのゼロによる除算E:\ wamp64 \ WWW \ワードプレス\ WP-コンテンツ\プラグイン\休ま-API \ libにする\エンドポイントクラス-WP-残り-用語-controller.php \

181行に

これは、の実行に関連している:$アイテム=親:: get_items($リクエスト);

問題は、デフォルトのプロパティ(exclude、include、order、per_pageなど)が含まれていないため、$ requestオブジェクトのようです。ここで

は$要求のダンプです:私は間違っ

E:\wamp64\www\wordpress\wp-content\plugins\myplugin-api\class-myplugin-categories-controller.php:46: object(WP_REST_Request)[65] protected 'method' => string 'GET' (length=3) protected 'params' => 
    array (size=6) 
     'URL' => 
     array (size=0) 
      empty 
     'GET' => 
     array (size=0) 
      empty 
     'POST' => 
     array (size=0) 
      empty 
     'FILES' => 
     array (size=0) 
      empty 
     'JSON' => null 
     'defaults' => 
     array (size=0) 
      empty protected 'headers' => 
    array (size=8) 
     'host' => 
     array (size=1) 
      0 => string 'localhost:8080' (length=14) 
     'connection' => 
     array (size=1) 
      0 => string 'keep-alive' (length=10) 
     'cache_control' => 
     array (size=1) 
      0 => string 'no-cache' (length=8) 
     'user_agent' => 
     array (size=1) 
      0 => string 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36' (length=114) 
     'postman_token' => 
     array (size=1) 
      0 => string '9c1726a9-b4ee-1d69-630f-c05bf1e85557' (length=36) 
     'accept' => 
     array (size=1) 
      0 => string '*/*' (length=3) 
     'accept_encoding' => 
     array (size=1) 
      0 => string 'gzip, deflate, sdch, br' (length=23) 
     'accept_language' => 
     array (size=1) 
      0 => string 'en-US,en;q=0.8,pt-PT;q=0.6,pt;q=0.4' (length=35) protected 'body' => string '' (length=0) protected 'route' => string '/myplugin-api/v1/categories' (length=27) protected 'attributes' => 
    array (size=6) 
     'methods' => 
     array (size=1) 
      'GET' => boolean true 
     'accept_json' => boolean false 
     'accept_raw' => boolean false 
     'show_in_index' => boolean true 
     'args' => 
     array (size=0) 
      empty 
     'callback' => 
     array (size=2) 
      0 => 
      object(MyPlugin_Categories_Controller)[67] 
       ... 
      1 => string 'get_items' (length=9) protected 'parsed_json' => boolean false protected 'parsed_body' => boolean false 

何をしているのですか?

私はPHPで新しいですので、おそらく私は何かが明らかに欠けている。

+0

あなたは単に残り-API/libに/エンドポイント/ ''でこのファイルを入れていましたし、ちょうどそれが始まっていることが好きワーキング? – Jacksonkr

+0

@Jacksonkrいいえ、あなたは既存のプラグインに何かを配置する必要はありません。独自のプラグインを作成する必要があります。 https://codex.wordpress.org/Writing_a_Pluginこれは良い出発点です。 – amp

答えて

0

はこれで少し苦労した後、私は私が望んでいたように、結果を送信するために管理:

<?php 

if (! class_exists('WP_REST_Controller')) { 
    require_once ABSPATH. 'wp-content/plugins/rest-api/lib/endpoints/' . '/class-wp-rest-controller.php'; 
} 
if (! class_exists('WP_REST_Taxonomies_Controller')) { 
    require_once ABSPATH. 'wp-content/plugins/rest-api/lib/endpoints/' . '/class-wp-rest-terms-controller.php'; 
} 


class MyPlugin_Categories_Controller extends WP_REST_Terms_Controller 
{ 

    public function __construct() { 
     parent::__construct('category'); 
    } 

    public function register_routes() { 
     $version = '1'; 
     $namespace = 'myplugin-api/v' . $version; 
     $base = 'categories'; 
     register_rest_route($namespace, '/' . $base, array(
      array(
       'methods'   => WP_REST_Server::READABLE, 
       'callback'  => array($this, 'get_items'), 
       'args'   => parent::get_collection_params(), 
      ), 
     )); 
     register_rest_route($namespace, '/' . $base . '/schema', array(
      'methods'   => WP_REST_Server::READABLE, 
      'callback'  => array($this, 'get_public_item_schema'), 
     )); 
    } 

    /** 
    * Get a collection of items 
    * 
    * @param WP_REST_Request $request Full data about the request. 
    * @return WP_Error|WP_REST_Response 
    */ 
    public function get_items($request) { 

     $items = parent::get_items($request); 

     if (is_wp_error($items)) { 
      return $items; 
     } 

     $response = array(); 
     foreach($items->get_data() as $item) { 
      $itemdata = $this->get_item_for_response($item, $request); 
      array_push($response,$itemdata); 
     } 

     $to_return = new WP_REST_Response($response,200); 
     $to_return->set_headers($items->get_headers()); 
     return $to_return; 
    } 

    public function get_item_for_response($item) { 
     $smalleritem = array(); 
     $smalleritem["id"] = $item["id"]; 
     $smalleritem["name"] = $item["name"]; 
     $smalleritem["count"] = $item["count"]; 
     return $smalleritem; 
    } 

} 
関連する問題