ダッシュボードでカテゴリを選択して新規投稿するプラグインを少し手直し(リファクタリング)しました。
具体的には、一部オブジェクト指向、一部関数だったものを、全面的にオブジェクト指向にしました。
<?php
/*
Plugin Name: Select Cateogy to Post
Plugin URI: http://plugin.php-web.net/wp/category-select
Description: Select Cateogy to Post
Author: Fumito MIZUNO
Version: 1
Author URI: http://php-web.net/
*/
$selectcategory = new selectcategory;
class selectcategory {
protected $catid ;
protected $js ;
protected $number = 10;
public function __construct() {
add_action( 'admin_head-post-new.php', array(&$this, 'catid_via_get') );
add_action('wp_dashboard_setup', array(&$this, 'category_post_dashboard_widget') );
}
public function category_post_dashboard_widget() {
wp_add_dashboard_widget('select_category_post', __('Select Category to Post'), array(&$this, 'select_category_post'));
}
public function select_category_post() {
$categories = get_terms('category', array(
'number' => $this->number,
'hide_empty' => 0,
'orderby' => 'count',
'order' => 'DESC' ));
foreach ( $categories as $catdata ) {
$cat_id = $catdata->term_id;
$catname = esc_html( apply_filters('the_category', $catdata->name));
echo '<p><a href="./post-new.php?defaultcatid='.$cat_id.'">';
echo $catname;
echo '</a></p>';
}
}
public function catid_via_get() {
if ( current_user_can('edit_posts') && is_numeric( $_GET['defaultcatid'] )){
$this->catid = (int) $_GET['defaultcatid'];
$this->catidcheck() ;
} else {
$this->catid = 0;
}
}
protected function catidcheck() {
if ( $this->catid ) {
add_action( 'admin_print_footer_scripts', array(&$this, 'output_js') );
}
}
protected function create_js() {
$this->js = <<<EOF
<script>
window.onload= getcatid;
function getcatid(){
document.getElementById('in-category-
EOF;
$this->js .= $this->catid;
$this->js .= <<<EOF
').checked = true;
} </script>
EOF;
}
public function output_js() {
if ( get_cat_name( $this->catid )){
$this->create_js();
echo $this->js;
} else {
wp_die( 'invalid catid' );
}
}
}
オブジェクトにしたので、元のオブジェクト名が被るかどうか?だけ考えれば良くなります。もし関数で書くと、それぞれの関数名が被るかどうか?を考える必要があります。