WordPress插件開發: 文章同步到OSC博客插件(OscPress) (一)

我的感受osc的博客不是很好用,主要是編輯器不是很符合個人習慣,容許上傳圖片圖片過小,並且很多人有本身的博客,只是想給osc上作個文章的備份。 因而我決定寫一個插件,實現如下功能:先在Wordpress寫好文章,而後在發佈文章時自動同步到osc的博客,而且發佈一條動彈(可選)。有兩種方式:一種是用模擬登陸,另外一種是使用osc 的open api。模擬登陸最大的問題是不穩定,保不許osc哪天改版,模擬登陸又要重寫,維護很是麻煩. open api 相對而言比較穩定和簡單,而且有 詳細的文檔:但缺點是:php

  1. 調用次數的限制,未經審覈的應用2000次(每日?);
  2. 是未經審覈的應用refresh token 時有些問題,須要私信給osc相關人員申請,詳情請見: OSC 的open API在刷新token時是否是有問題
  3. 我詳細閱讀了open api之後發現跟博客相關的,只有發佈接口,並無編輯,刪除的接口,也就是說這個api不是完整的,但願osc的相關工做人員以後能完善一下。

把需求和步驟想清楚了,作起來就不會以爲太複雜了,我分爲四個部分:git

  1. 向osc申請一個應用:http://www.oschina.net/openapi/client (略)
  2. 插件設置頁面,好比設置應用id,secret,同步的相關選項,如是否發佈到動彈,容許文章前置/後置的插入相關內容等
  3. osc open api oauth受權,保存用戶access token,refresh token等。
  4. 使用api同步文章到osc,主要調用osc博客接口和動彈接口

首先寫插件的基本框架, 咱們使用面向對象的寫法。github

<?php
/*
Plugin Name: OscPress
Plugin URI: https://www.cellmean.com/oscpress
Description: 將Wordprss站點更新文章時, 將內容同步到osc博客和動彈
Version: 1.0
Author: Falcon
Author URI: https://www.cellmean.com
*/

$oscPress = new OscPress();

class OscPress{

    /**
     * 初始化
     */
    public function __construct(){

        $this->api_site = 'https://www.oschina.net'; // osc api的基本前綴

        $this->dir = __DIR__; // 當前插件的物理地址

        $this->state = __CLASS__ . '_state'; // 用於oauth 受權的安全鑑權的參數

        $this->callback_url = add_query_arg('callback', 'oscpress', site_url());


    }

    
}

注意:這裏的註釋部分不要省略,不然wordpress沒法識別和發現咱們的插件。 咱們先完成菜單的設置,先提供appid和appsecret的設置項,使用 wordpress內置的 settings api完成,固然也能夠本身使用常規的表單完成,不過要注意安全相關的內容,好比csrf. 代碼完成後以下:api

<?php
/*
Plugin Name: OscPress
Plugin URI: https://www.cellmean.com/oscpress
Description: Wordprss站點更新文章時, 將內容自動發佈到osc博客和動彈
Version: 1.0
Author: Falcon
Author URI: https://www.cellmean.com
*/
$oscPress = new OscPress();

class OscPress{

    /**
     * 初始化
     */
    public function __construct(){

        $this->api_site = 'https://www.oschina.net'; // osc api的基本前綴

        $this->dir = __DIR__; // 當前插件的物理地址

        $this->state = __CLASS__ . '_state'; // 用於oauth 受權的安全鑑權的參數

        $this->callback_url = add_query_arg('callback', 'oscpress', site_url());

        add_action( 'admin_menu', array( $this,'admin_menu'));

        add_action( 'admin_init', array($this,'admin_init') );

    }

    public function admin_menu() {
        add_options_page('OscPress','OscPress','manage_options',"oscpress_admin_settings",array($this,'admin_setting'));
        // 添加設置菜單的另外一種寫法
        // add_submenu_page('options-general.php', 'OscPress','OscPress', 10, __FILE__, array($this,'admin_setting')); 
    }


    public function admin_init() {

        register_setting( 'oscpress_settings_group', 'oscpress_settings',array($this,'sanitize_callback') );

        /*
      This creates a 「section」 of settings.
      The first argument is simply a unique id for the section.
      The second argument is the title or name of the section (to be output on the page).
      The third is a function callback to display the guts of the section itself.
      The fourth is a page name. This needs to match the text we gave to the do_settings_sections function call.
       */
        add_settings_section('oscpress_settings', 'OscPress設置', array($this,'settings_section_text'), 'oscpress');

        /*
         The first argument is simply a unique id for the field.
        The second is a title for the field.
        The third is a function callback, to display the input box.
        The fourth is the page name that this is attached to (same as the do_settings_sections function call).
        The fifth is the id of the settings section that this goes into (same as the first argument to  add_settings_section).
        第六個參數是自定義傳入回調函數的參數,數組類型
        */
        add_settings_field('appid', '應用ID', array($this,'input_text'), 'oscpress', 'oscpress_settings',array(
            "field" =>"appid", "label"=>"應用ID","placeholder"=>"請輸入應用ID"
        ));
        add_settings_field('appsecret', '應用私鑰', array($this,'input_text'), 'oscpress', 'oscpress_settings',array(
            "field" =>"appsecret", "label"=>"應用私鑰","placeholder"=>"請輸入應用私鑰"
        ));
    }

    public function input_text($arr){

        $settings = (array) get_option( 'oscpress_settings' );
        $field = $arr['field'];
        $value = esc_attr( $settings[$field] );

        echo "<label class=\"oscpress_settings\" for=\"{$arr['field']}\"><input type='text' id='{$arr['field']}' name='oscpress_settings[$field]' value='$value' placeholder=\"{$arr['placeholder']}\" /></label>";


    }

    /**
     * 驗證和潔淨化函數
     */
    public function sanitize_callback($inputs){
        return $inputs;
    }

    public function settings_section_text(){
        echo "<hr/>";
        echo "<em>請填寫app id及私鑰</em>";

    }

    public function admin_setting(){
?>

        <div class="wrap">
            <form action="options.php" method="POST">
                <?php settings_fields('oscpress_settings_group'); ?>
                <?php do_settings_sections('oscpress'); ?>
                <?php submit_button(); ?>
            </form>
        </div>

    <?php
    }


}

屏幕截圖 2016-07-08 03.08.10 好了,這就是設置頁的基本內容. 咱們簡單說一下Wordpress的這套setting api吧,其中 register_setting 是註冊一組選項。 第一個參數是 oscpress_settings_group ,這個是分組的名稱,要跟settings_fields('oscpress_settings_group'); 的參數一致,第二個參數是 oscpress_settings ,即存儲在options表裏的meta_name,咱們能夠經過 get_option( 'oscpress_settings' ) 獲取設置的內容。若是在設置頁提交了表單,這個值是一個關聯數組。 register_setting 的第三個參數,是對設置頁表單字段進行驗證和潔淨化的函數,在這裏即 sanitize_callback方法,這個函數接收一個參數,就是表單post過來的數組,能夠理解爲$_POST,好比說咱們須要驗證appid字段必須爲20個字符,能夠這樣寫數組

public function sanitize_callback($inputs){

    if( strlen($inputs['appid']) !== 20 ) {
        add_settings_error('oscpress_settings', 'oscpress_failed', "應用ID必須爲20位", 'error');
        $settings = (array) get_option( 'oscpress_settings' );
        $inputs['appid'] = isset($settings['appid']) ? $settings['appid'] : "";
    }

    return $inputs;

}

若是填寫錯誤,會出現一條錯誤提示: 屏幕截圖 2016-07-08 11.55.58 其餘的api函數如:add_settings_section,add_settings_field,do_settings_sections可參考註釋和gist: https://gist.github.com/annalinneajohansson/5290405 接下來咱們會講一下獲取osc oauth受權。安全

相關文章
相關標籤/搜索