<?php | |
/* | |
Plugin Name: My API | |
Version: 0.1-alpha | |
Description: 記事取得用 API | |
Author: wokamoto@digitalcube | |
Author URI: https://digitalcube.jp | |
Plugin URI: https://digitalcube.jp | |
Text Domain: my-api | |
Domain Path: /languages | |
Thx! http://firegoby.jp/archives/5309 | |
License: | |
Released under the GPL license | |
http://www.gnu.org/copyleft/gpl.html | |
Copyright 2014 wokamoto (email : wokamoto1973@gmail.com) | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation; either version 2 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ | |
// プラグインの有効化時/無効化時の処理を登録 | |
register_activation_hook( __FILE__ , 'My_API::activation' ); | |
register_deactivation_hook( __FILE__ , 'My_API::deactivation' ); | |
// フックの登録 | |
$my_api = My_API::get_instance(); | |
$my_api->init(); | |
class My_API { | |
private static $instance; | |
const OPTION_NAME_ACTIVATED = 'my-api_activated'; | |
const API_ENDPOINT = 'api'; | |
const API_VIEWS = 'api-template'; | |
private function __construct() {} | |
public static function get_instance() { | |
if( !isset( self::$instance ) ) { | |
$c = __CLASS__; | |
self::$instance = new $c(); | |
} | |
return self::$instance; | |
} | |
// フックの登録 | |
public function init(){ | |
// rewrite rule の追加 | |
add_action('init', array($this, 'add_rewrite_endpoint')); | |
// 他のプラグインや管理者の操作によってflush_rewrite_rules()が発火した際にこのプラグイン用のrewrite ruleを再登録する | |
add_action('delete_option', array($this, 'delete_option'), 10, 1 ); | |
// API 表示用 | |
add_filter('query_vars', array($this, 'query_vars')); | |
add_action('template_redirect', array($this, 'template_redirect')); | |
} | |
// 有効化時の処理 | |
static public function activation(){ | |
update_option( self::OPTION_NAME_ACTIVATED, true ); | |
flush_rewrite_rules(); | |
} | |
// 無効化時の処理 | |
static public function deactivation(){ | |
delete_option( self::OPTION_NAME_ACTIVATED ); | |
flush_rewrite_rules(); | |
} | |
// delete_optionフックのコールバック関数 | |
public function delete_option($option){ | |
if ( 'rewrite_rules' === $option && get_option(self::OPTION_NAME_ACTIVATED) ) { | |
$this->add_rewrite_endpoint(); | |
} | |
} | |
// rewrite rule の追加 | |
public function add_rewrite_endpoint() { | |
add_rewrite_endpoint(self::API_ENDPOINT, EP_ROOT); | |
} | |
// query_vars の追加 | |
public function query_vars($vars) { | |
$vars[] = self::API_ENDPOINT; | |
return $vars; | |
} | |
// API | |
public function template_redirect() { | |
global $wp_query; | |
if (is_object($wp_query) && isset($wp_query->query[self::API_ENDPOINT])) { | |
$api = preg_replace('/[^a-z0-9\-]*/', '', strtolower($wp_query->query[self::API_ENDPOINT])); | |
if (empty($api)) | |
$api = 'default'; | |
$api_template = sprintf('%s/%s/%s.php', dirname(__FILE__), self::API_VIEWS, $api); | |
nocache_headers(); | |
header('Content-Type: application/json; charset='.get_option('charset')); | |
if ( file_exists($api_template) ) { | |
load_template($api_template); | |
} else { | |
status_header('404'); | |
echo json_encode(array('meta' => array('code' => "404", "message" => "API Not Found"))); | |
} | |
exit; | |
} | |
} | |
} |
API が実際に返すデータはプラグインディレクトリのサブディレクトリ api-template の中に {api名}.php ってファイル書いて置いておいてね。
たとえば http://example.com/api/get-posts ってリクエストが来たら api-template/get-posts.php を読み込みます。