WP REST API (WP API) 是一個 WordPress 插件,其用途是為 WordPress 核心添加一個 JSON REST API , 以便於像移動應用之類的應用與 WordPress 進行交互。

WP-API 是可擴展的,並且具有齊備的文檔,如果你使用 WP REST API (WP API) ,你很可能會被授權問題所困擾。

WP
REST API (WP API)
能讓你創建、編輯獲取文章 (各種 WordPress 內置的文章類型的文章以及自定義類型的文章) 、創建、編輯和獲取用户等,因此,它在某些情形下需要認證
(授權),比如創建和編輯操作,就絕對需要授權才行,否則,處理申請會被 WordPress 拒絕。

據 WP REST API (WP API) 的認證 (授權) 文檔來看,認證方式有三種:cookie 、 oauth 和簡單認證。本文記錄如何實現自定義認證。

據 WordPress 官方開發記錄顯示:這個插件很可能會在 2015 年 4 月 22 日發佈的 WordPress 4.2 版本中加入到 WordPress 核心,那樣的話,授權方式可能會有所改變,但不會大變。

舉個簡單的例子: 某個用户通過手機拍了一張照片,想上傳到某個啓用了 WP REST API (WP API) 的 WordPress 企業網站源碼,那麼,就需要認證了吧,那麼,怎麼做呢?

下面將説一種用於此種情形的認證方式。

用自定義 filter hook

在該插件目錄 lib 下有個類文件 class-wp-json-server.php , 其中有這段兒:

  1. /** 
  2.  * Check the authentication headers if supplied 
  3.  * 
  4.  * @return WP_Error|null WP_Error indicates unsuccessful login, null indicates successful or no authentication provided 
  5.  */ 
  6. public function check_authentication() { 
  7.     /** 
  8.      * Pass an authentication error to the API 
  9.      * 
  10.      * This is used to pass a {@see WP_Error} from an authentication method 
  11.      * back to the API. 
  12.      * 
  13.      * Authentication methods should check first if they're being used, as 
  14.      * multiple authentication methods can be enabled on a site (cookies, 
  15.      * HTTP basic auth, OAuth). If the authentication method hooked in is 
  16.      * not actually being attempted, null should be returned to indicate 
  17.      * another authentication method should check instead. Similarly, 
  18.      * callbacks should ensure the value is `null` before checking for 
  19.      * errors. 
  20.      * 
  21.      * A {@see WP_Error} instance can be returned if an error occurs, and 
  22.      * this should match the format used by API methods internally (that is, 
  23.      * the `status` data should be used). A callback can return `true` to 
  24.      * indicate that the authentication method was used, and it succeeded. 
  25.      * 
  26.      * @param WP_Error|null|boolean WP_Error if authentication error, null if authentication method wasn't used, true if authentication succeeded 
  27.      */ 
  28.     return apply_filters( 'json_authentication_errors', null ); 
  29. }

基於上面的這個函數以及其被調用位置,我們可以加進去一個 hook,以確認認證是否成功:

  1. /** 
  2. * WP JSON API 認證檢查 
  3. * @param  null 
  4. * @return boolean     是否認證成功 
  5. * @author suifengtec  coolwp.com 
  6. */ 
  7. function coolwp_rest_api_auth_check( $result ){ 
  8.  
  9.    if( 
  10.         !isset($_GET['id']) 
  11.        // ||!isset($_GET['app_key']) 
  12.         ||!isset($_GET['app_token']) 
  13.         ||empty($_GET['id']) 
  14.        // ||empty($_GET['app_key']) 
  15.         ||empty($_GET['app_token']) 
  16.  
  17.  
  18.     ){ 
  19.          return false; 
  20.     } 
  21.  
  22.     //獲取從應用 GET 過來的用户 id 、 app_key 和 app_token, 當然了,你也可以只用一個去 app_key 和 app_token 中的任何一個去檢查 
  23.     $user_id =  (int)$_GET['id']; 
  24.    // $app_key = sanitize_text_field($_GET['app_key']); 
  25.     $app_token = sanitize_text_field($_GET['app_token']); 
  26.  
  27.     //查詢 app_key 和 app_token, 當然了, 你也可以自定義一種算法, 
  28.     //$wp_key = get_user_meta( $user_id, 'app_key', true); 
  29.     $wp_token = get_user_meta( $user_id, 'app_token', true); 
  30.  
  31.  
  32.     //將從應用客户端獲取到的值與數據庫存儲的值進行對比 
  33.     if( 
  34.         ( $wp_token == $app_token ) 
  35.        // &&( $wp_key == $app_key ) 
  36.  
  37.     ){ 
  38.  
  39.        return true; 
  40.     } 
  41.  
  42.     return false; 
  43.  
  44. } 
  45. add_filter('json_authentication_errors', 'coolwp_rest_api_auth_check');

結論

加入 rest api 的 WordPress 甚至可以讓你做一個在線支付網站,有了這組 api ,基於 WordPress 的原生安卓應用和 IOS 應用可以更好的與 WordPress 站點進行交互。