我的感受osc的博客不是很好用,主要是編輯器不是很符合個人習慣,容許上傳圖片圖片過小,並且很多人有本身的博客,只是想給osc上作個文章的備份。 因而我決定寫一個插件,實現如下功能:先在Wordpress寫好文章,而後在發佈文章時自動同步到osc的博客,而且發佈一條動彈(可選)。有兩種方式:一種是用模擬登陸,另外一種是使用osc 的open api。模擬登陸最大的問題是不穩定,保不許osc哪天改版,模擬登陸又要重寫,維護很是麻煩. open api 相對而言比較穩定和簡單,而且有 詳細的文檔:但缺點是:php
把需求和步驟想清楚了,作起來就不會以爲太複雜了,我分爲四個部分:git
首先寫插件的基本框架, 咱們使用面向對象的寫法。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 } }
好了,這就是設置頁的基本內容. 咱們簡單說一下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; }
若是填寫錯誤,會出現一條錯誤提示: 其餘的api函數如:add_settings_section,add_settings_field,do_settings_sections可參考註釋和gist: https://gist.github.com/annalinneajohansson/5290405 接下來咱們會講一下獲取osc oauth受權。安全