Skip to content

Commit a12cbc1

Browse files
Moved CustomPostType, CustomTaxonomy, CustomMetaBox, and FormTablePresenter to lib/posttypes/ wrote wrapper classes NIB_PostType, NIB_Taxonomy, and NIB_MetaBox; refactored CustomMetaBox to require fewer arguments for construction.
1 parent 911d9cb commit a12cbc1

10 files changed

+769
-16
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.buildpath
2+
.project
3+
.settings

lib/NIB_MetaBox.class.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
abstract class NIB_MetaBox extends NIB_PropertiesWrapper {
3+
4+
protected $_metaBoxObj;
5+
6+
protected
7+
$id,
8+
$title,
9+
$context = 'main',
10+
$priority = 'high',
11+
$postTypes = array(),
12+
$fields,
13+
$tableClass = 'form-table'
14+
;
15+
16+
public function __construct() {
17+
$this->_metaBoxObj = new CustomMetaBox($this->id,$this->title,$this->postTypes);
18+
$this->_metaBoxObj->setContext($this->context);
19+
$this->_metaBoxObj->setPriority($this->priority);
20+
if(!is_null($this->fields)) {
21+
foreach($this->fields as $field) {
22+
$this->_metaBoxObj->addField($field);
23+
}
24+
}
25+
$presenter = new FormTablePresenter($this->fields,'post',$this->tableClass);
26+
$this->_metaBoxObj->setPresenter($presenter);
27+
}
28+
29+
public function obj() {
30+
return $this->_metaBoxObj;
31+
}
32+
}

lib/NIB_PostType.class.php

+24-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
2-
abstract class NIB_PostType {
2+
abstract class NIB_PostType extends NIB_PropertiesWrapper {
33

44
protected $_typeObj;
5-
protected $_metaBoxObjs;
6-
protected $_taxonomyObjs;
75

86
// You should specify the $_name in your child class
97
protected $_name; // slug
@@ -37,19 +35,33 @@ abstract class NIB_PostType {
3735
$_edit_link = 'post.php?post=%d'
3836
;
3937

40-
private function getProperties() {
41-
$args = get_class_vars(get_called_class());
42-
43-
unset($args['_typeObj']);
44-
unset($args['_metaBoxObjs']);
45-
unset($args['_taxonomyObjs']);
46-
unset($args['_name']);
47-
48-
return $args;
38+
public static function property($id) {
39+
global $post;
40+
if(!is_null($post)) {
41+
return get_post_meta($post->ID,$id,TRUE);
42+
}
43+
return null;
4944
}
5045

5146
public function __construct() {
5247
$this->_typeObj = new CustomPostType($this->_name,$this->getProperties());
5348
}
5449

50+
public function addTaxonomy($obj) {
51+
if($obj instanceof NIB_Taxonomy) {
52+
$this->_typeObj->addTaxonomy($obj->obj());
53+
}
54+
if($obj instanceof CustomTaxonomy) {
55+
$this->_typeObj->addTaxonomy($obj);
56+
}
57+
}
58+
59+
public function addMetaBox($obj) {
60+
if($obj instanceof NIB_MetaBox) {
61+
$this->_typeObj->addMetaBox($obj->obj());
62+
}
63+
if($obj instanceof CustomMetaBox) {
64+
$this->_typeObj->addMetaBox($obj);
65+
}
66+
}
5567
}

lib/NIB_PropertiesWrapper.class.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
abstract class NIB_PropertiesWrapper {
3+
4+
protected function getProperties() {
5+
$args = get_class_vars(get_called_class());
6+
7+
unset($args['_typeObj']);
8+
unset($args['_metaBoxObjs']);
9+
unset($args['_taxonomyObjs']);
10+
unset($args['_taxObj']);
11+
unset($args['_metaBoxObj']);
12+
unset($args['_name']);
13+
14+
return $args;
15+
}
16+
17+
}

lib/NIB_Taxonomy.class.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
abstract class NIB_Taxonomy extends NIB_PropertiesWrapper {
3+
4+
protected $_taxObj;
5+
6+
// You need to overwrite the following:
7+
protected $_name; //slug
8+
9+
protected
10+
//$label,
11+
//$labels,
12+
$public = TRUE,
13+
$show_in_nav_menus = TRUE,
14+
$show_ui = TRUE,
15+
$show_tagclound = TRUE,
16+
$hierarchical = FALSE,
17+
//$update_count_callback,
18+
//$rewrite,
19+
$query_var = TRUE,
20+
//$capabilities,
21+
$_builtin = FALSE
22+
;
23+
24+
public function __construct() {
25+
$this->_taxObj = new CustomTaxonomy($this->_name,$this->getProperties());
26+
}
27+
28+
public function &obj() {
29+
return $this->_taxObj;
30+
}
31+
32+
}

lib/posttypes/CustomMetaBox.class.php

+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
<?php
2+
require_once('FormTablePresenter.class.php');
3+
4+
if(!class_exists('CustomMetaBox')) {
5+
/**
6+
* CustomMetaBox class
7+
*
8+
* Implements WordPress's custom meta boxes as objects that are capable
9+
* of self-registering with a given post type. Designed to integrate with
10+
* the CustomPostType class, but can be used without it via register()
11+
*
12+
* @package CustomMetaBox
13+
* @version 0.2
14+
* @author David Beveridge <[email protected]>
15+
*
16+
*/
17+
class CustomMetaBox {
18+
19+
/**
20+
* @var string The ID string for the meta box.
21+
*/
22+
protected $id;
23+
24+
/**
25+
* @var string The Meta box's title. Appears at the top of the Meta Box on the edit page.
26+
*/
27+
protected $title;
28+
29+
/**
30+
* @var array An array of post types with which the meta box will register.
31+
*/
32+
protected $postTypes;
33+
34+
/**
35+
* @var string The part of the edit page where the meta box will appear. Valid values include 'normal','advanced', or 'side'
36+
*/
37+
protected $context = 'normal';
38+
39+
/**
40+
* @var string The priority within the contex where the boxes should show. Valid options include 'high' or 'low'
41+
*/
42+
protected $priority = 'high';
43+
44+
/**
45+
* @var array A multi-dimensional associative array of fields. Each field must contain values for, at least, id and type. Most (if not all) should contain more fields.
46+
*/
47+
protected $fields = array();
48+
49+
/**
50+
* @var formTablePresenter Presenter object for generating the form
51+
*/
52+
protected $_presenter;
53+
54+
/**
55+
*
56+
* @param string $id ID/slug
57+
* @param string $title Title to display
58+
* @param array $postTypes Array of post type slugs to associate with.
59+
* @param string $context Default is 'normal'
60+
* @param string $priority Default is 'high'
61+
* @param array $fields Array of fields, corresponding to the fields used by FormTablePresenter.
62+
*/
63+
public function __construct($id,$title,$postTypes=array(),$fields=null) {
64+
$this->id = $id;
65+
$this->title = $title;
66+
if(isset($postTypes) && is_array($postTypes)) {
67+
$this->postTypes = $postTypes;
68+
}
69+
else {
70+
$this->postTypes = array();
71+
}
72+
if(!is_null($fields)) {
73+
$this->fields = $fields;
74+
}
75+
76+
add_action('save_post',array(&$this,'saveData'));
77+
add_action('admin_menu',array(&$this,'registerMetaBox'));
78+
79+
add_action('admin_init',array(&$this,'loadThickBox'));
80+
}
81+
82+
/**
83+
* Generates the Meta Box's internal code. Uses a FormTablePresenter object.
84+
* Should not be called externally, but must be public in order to be accessible to
85+
* the WordPress Plugin API.
86+
*/
87+
public function display() {
88+
if(!is_null($this->_presenter)) {
89+
$this->_nonce(); //Generate nonce for form authentication
90+
$this->_presenter->printOutput();
91+
}
92+
}
93+
94+
/**
95+
* Saves the meta box's information using WordPress' custom fields.
96+
* Should not be called externally, but must be public in order to be accessible to
97+
* the WordPress Plugin API.
98+
*
99+
* @param int $postID The ID of the post with which to associate data.
100+
*/
101+
public function saveData($postID) {
102+
// Verify nonce
103+
if(@!wp_verify_nonce($_POST[$this->id.'_meta_box_nonce'], basename(__FILE__))) {
104+
return $postID;
105+
}
106+
// Don't save if this is an autosave.
107+
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
108+
return $postID;
109+
}
110+
111+
// Permissions check:
112+
113+
// Check page permissions
114+
if('page' == $_POST['post_type']) {
115+
if(!current_user_can('edit_page',$postID)) {
116+
return $postID;
117+
}
118+
}
119+
//Check post permissions
120+
elseif(!current_user_can('edit_post',$postID)) {
121+
return $postID;
122+
}
123+
124+
// Update each field, one at a time.
125+
foreach($this->fields as $field) {
126+
$oldValue = get_post_meta($postID,$field['id'],TRUE);
127+
@$newValue = $_POST[$field['id']];
128+
if($newValue && $newValue != $oldValue) {
129+
update_post_meta($postID,$field['id'],$newValue);
130+
}
131+
elseif('' == $newValue && $oldValue) {
132+
delete_post_meta($postID,$field['id'],$oldValue);
133+
}
134+
}
135+
}
136+
137+
/**
138+
* Adds a post type with which to register on init.
139+
*
140+
* @param string $postType The post type's slug.
141+
* @return CustomMetaBox $this Enables chaining.
142+
*/
143+
public function &addPostType($postType) {
144+
if(!in_array($postType,$this->postTypes)) {
145+
$this->postTypes[] = $postType;
146+
}
147+
return $this;
148+
}
149+
150+
/**
151+
* Removes an associated post type by slug.
152+
* @param string $postType The post type's slug.
153+
* @return CustomMetaBox $this Enables chaining.
154+
*/
155+
public function &removePostType($postType) {
156+
if(($key = array_search($postType,$this->postTypes)) !== FALSE) {
157+
unset($this->postTypes[$key]);
158+
}
159+
return $this;
160+
}
161+
162+
/**
163+
* Registers the Meta Box with the WordPress Plugin API. Should not be called externally,
164+
* but must be public to be accessible to the API.
165+
*/
166+
public function registerMetaBox() {
167+
foreach($this->postTypes as $postType) {
168+
add_meta_box(
169+
$this->id, //id
170+
$this->title, //title
171+
array(&$this,'display'), //callback
172+
$postType, //page
173+
$this->context, //context
174+
$this->priority //priority
175+
);
176+
}
177+
}
178+
179+
/**
180+
* Loads the Thickbox JavaScript library for the 'image' input type. Should not be called
181+
* externally, but must be public to be accessible to the WordPress Plugin API.
182+
*/
183+
public function loadThickbox() {
184+
wp_enqueue_script('thickbox');
185+
wp_enqueue_style('thickbox');
186+
}
187+
188+
public function addField($field) {
189+
$this->fields[] = $field;
190+
}
191+
192+
public function setContext($context) {
193+
$this->context = $context;
194+
}
195+
public function setPriority($priority) {
196+
$this->priority = $priority;
197+
}
198+
199+
public function setPresenter(&$presenter) {
200+
$this->_presenter =& $presenter;
201+
}
202+
203+
/**
204+
* Simple nonce generator for WordPress' form validation system.
205+
*/
206+
protected function _nonce() {
207+
echo '<input type="hidden" name="'.$this->id.'_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
208+
}
209+
210+
}
211+
}

0 commit comments

Comments
 (0)