Standing on the Shoulder of Linus

Home / 2010 / 8月 / 24 / Select Cateogy to Post改定

Select Cateogy to Post改定

ダッシュボードでカテゴリを選択して新規投稿するプラグインを少し手直し(リファクタリング)しました。

具体的には、一部オブジェクト指向、一部関数だったものを、全面的にオブジェクト指向にしました。

<?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' );
		}
	}

}

オブジェクトにしたので、元のオブジェクト名が被るかどうか?だけ考えれば良くなります。もし関数で書くと、それぞれの関数名が被るかどうか?を考える必要があります。

関連

← WordBench名古屋をUSTで閲覧 カスタム投稿タイプとカスタム分類 at 第2回WordBench神戸 →

アーカイブ

人気の投稿とページ

  • キンドル本を印刷する(PDFに変換する)方法
  • 名古屋駅から国際センターまでの道のり(徒歩)
  • AGPL ライセンス(GPLとは似ているが違いもある)
  • 6年使ったイーモバイル(Y!mobile)を解約手続。店頭でSIM返却
  • JP-Secure SiteGuard WP Pluginは不正ログイン防止に役立つか

プロフィール

水野史土:月70万PVホームページ制作会社のレスキューワーク株式会社で、PHPソフトウェアのサポートを行っている。concrete5コミュニティリーダー、Novius OSコアコード貢献者でもある。 詳しくは管理者詳細参照。
大好評WordPress書籍「WordPressユーザーのためのPHP入門 はじめから、ていねいに。」サポートページ

Copyright © 2015 Standing on the Shoulder of Linus.