Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active September 7, 2015 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurozumi/4a91f8a53d82ab8b9d7a to your computer and use it in GitHub Desktop.
Save kurozumi/4a91f8a53d82ab8b9d7a to your computer and use it in GitHub Desktop.
【ワードプレス】シンプルなCSVエクスポートプラグイン
<?php
/*
Plugin Name: Simple CSV Exporter
Version: 0.1-alpha
Description: simple csv expoeter
Author: kurozumi
Author URI: http://a-zuim.net
Plugin URI: http://a-zuim.net
Text Domain: simple-csv-exporter
Domain Path: /languages
*/
$simple_csv_exporter = new Simple_CSV_Exporter;
$simple_csv_exporter->register();
class Simple_CSV_Exporter {
public function register()
{
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
}
public function plugins_loaded()
{
add_action('admin_menu', function(){
add_menu_page('CSVエクスポート', 'CSVエクスポート', 'manage_options', __FILE__, array($this, 'show_options_page'));
});
}
public function show_options_page()
{
?>
<div class="wrap">
<div id="icon-options-general" class="icon32"><br /></div><h2>CSVエクスポート</h2>
<form action="" method="post">
<?php wp_nonce_field('csv_export');?>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="inputtext">投稿タイプを選択</label></th>
<td>
<select name="post_type" class="regular-text">
<option value="post"><?php _e( 'Posts' ); ?></option>
<option value="page"><?php _e( 'Pages' ); ?></option>
<?php foreach ( get_post_types( array( '_builtin' => false, 'can_export' => true ), 'objects' ) as $post_type ) : ?>
<option value="<?php echo esc_attr( $post_type->name ); ?>"><?php echo esc_attr( $post_type->label ); ?></option>
<?php endforeach;?>
</select>
</td>
</tr>
</table>
<p class="submit"><input type="submit" class="button-primary" value="エクスポート" /></p>
</form>
<?php $this->result(); ?>
</div>
<?php
}
public function result()
{
if(isset($_POST['post_type']))
{
echo "<hr />";
try {
$result = $this->export();
printf("<p>「%s」記事のCSVを作成しました。</p>", $result['post_type']);
printf('<p><a href="%s" target="_blank">右クリックでファイルを保存して下さい。</a></p>', $result['download_url']);
} catch (Exception $ex) {
printf("<p>%s</p>", $ex->getMessage());
}
}
}
public function export()
{
global $wpdb;
check_admin_referer('csv_export');
$post_type = get_post_type_object($_POST['post_type']);
if(!$post_type)
throw new Exception("post_typeが正しくありません。");
// wp_postsテーブルから指定したpost_typeの公開記事を取得
$query = <<< __EOS__
SELECT
ID as post_id,
post_name,
post_type,
post_status,
post_title,
post_content
FROM {$wpdb->posts}
WHERE post_status LIKE 'publish' AND
post_type LIKE '%s'
__EOS__;
$prepare = $wpdb->prepare($query, array($post_type->name));
$results = $wpdb->get_results($prepare, ARRAY_A);
if(!$results)
throw new Exception(sprintf("%sの記事が見つかりませんでした。", $post_type->label));
// カテゴリとタグのslugを追加
$results = array_map(function($result){
// カテゴリ追加
$cats = implode(',', array_map(function($cat){
return $cat->slug;
}, get_the_category($result['post_id'])));
// タグがあれば追加
if($tags = get_the_tags($result['post_id'])){
$tags = implode(',', array_map(function($tag){
return $tag->slug;
}, $tags));
}else{
$tags = "";
}
return array_merge($result, array(
'post_category' => $cats,
'post_tags' => $tags));
}, $results);
// 項目名を取得
$head[] = array_keys(current($results));
// 先頭に項目名を追加
$list = array_merge($head, $results);
// ファイルの保存場所を設定
$filename = "export.csv";
$filepath = dirname(__FILE__) . '/' . $filename;
$fp = fopen($filepath, 'w');
// 配列をカンマ区切りにしてファイルに書き込み
foreach($list as $fields){
//mb_convert_variables('SJIS', 'UTF-8', $fields);
fputcsv($fp, $fields);
}
fclose($fp);
return array(
'post_type' => $post_type->label,
'download_url' => plugins_url( '', __FILE__ ) . '/' . $filename
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment