Blog

Really Simple CSV Importerプラグインのアドオンを作る

Posted by admin at 8:32 日時 2013/10/28

Really Simple CSV Importerプラグインでは、フィルターフックが仕込んであります。フックを使えば、CSVデータの解析とインポート処理の間に任意の処理を挟み込むことができます。この処理をプラグインとしてサイトにインストールしておくことで、プラグインを直接書き換えなくても、お使いのサイトに合ったオリジナルのCSVインポートプラグインを作ることができます。

一例として、インポート画面に投稿、カスタムフィールド、カスタムタクソノミーのデータをダンプするデバッグ用のアドオンプラグインの例を掲載しておきます。

<?php
/*
Plugin Name: Really Simple CSV Importer Debugger add-on
Description: Enables to dry-run-testing with Really Simple CSV Importer. When this add-on plugin activated, csv data will not imported, just displayed on dashboard.
Author: Takuro Hishikawa
Version: 0.2
*/
class rscsvimporter_debug {
// singleton instance
private static $instance;
public static function instance() {
if ( isset( self::$instance ) )
return self::$instance;
self::$instance = new rscsvimporter_debug;
self::$instance->run_init();
return self::$instance;
}
private function __construct() {
/** Do nothing **/
}
protected function run_init() {
add_action( 'init', array( $this, 'add_filter' ) );
}
public function add_filter() {
add_filter( 'really_simple_csv_importer_dry_run', '__return_true' ); // dry-run
add_filter( 'really_simple_csv_importer_save_post', array( $this, 'debug_save_post'), 50, 2 );
add_filter( 'really_simple_csv_importer_save_meta', array( $this, 'debug_save_meta'), 50, 3 );
add_filter( 'really_simple_csv_importer_save_tax', array( $this, 'debug_save_tax'), 50, 3 );
}
public function debug_save_post($post, $is_update) {
$this->var_dump($is_update,'$is_update');
$this->var_dump($post,'$post');
return $post;
}
public function debug_save_meta($meta, $post, $is_update) {
$this->var_dump($meta,'$meta');
return $meta;
}
public function debug_save_tax($tax, $post, $is_update) {
$this->var_dump($tax,'$tax');
return $tax;
}
private function var_dump($var, $label) {
if (isset($label) && !empty($label))
echo $label . ':';
echo '<pre>';
var_dump($var);
echo '</pre>';
}
}
$rscsvimporter_debug = rscsvimporter_debug::instance();

これを応用すれば、特定のフィールドを無視したり、CSVデータにない情報を追加したりといったカスタマイズができます。アドオンとしてプラグイン本体から切り離すことで、プラグインのバグ修正アップデートをいち早く導入していただけます。

各フィルターの詳細はプラグインページの「Other Notes」をご覧ください。

余談ですがWordPressプラグインはこんな感じでシングルトンで書くのがマイブームです。


Share this entry