Permalink
Browse files

add support for post type unique

  • Loading branch information...
1 parent d187113 commit cfba11209a1412bed715bca79e2e7d6d139e9396 @hunk hunk committed Oct 8, 2012
Showing with 53 additions and 2 deletions.
  1. +26 −0 admin/mf_menu.php
  2. +15 −0 admin/mf_posttype.php
  3. +4 −0 main.php
  4. +8 −2 mf_register.php
View
@@ -0,0 +1,26 @@
+<?php
+class mf_menu
+{
+ public function unique_post_type(){
+ global $submenu,$menu,$mf_pt_unique,$wpdb;
+
+ if (count($mf_pt_unique)) {
+ foreach ($submenu as $key => $value) {
+ if (in_array($key, $mf_pt_unique)) {
+ unset($submenu[$key][5]);
+
+ //exist element crated for this post type?
+ $name = preg_replace('/edit.php\?post_type\=/', '', $key);
+ $has_posts = get_posts( array( 'post_type' => $name, ) );
+ if ($has_posts) {
+ //replace url and text
+ $submenu[$key][10][0] = preg_replace('/Add/', 'Edit', $submenu[$key][10][0]);
+ $submenu[$key][10][2] = "post.php?post=".$has_posts[0]->ID."&action=edit";
+ }
+ }
+
+ }
+ }
+ }
+
+}
View
@@ -88,6 +88,14 @@ public function fields_form() {
'description' => __( 'A short descriptive summary of what the post type is.', $mf_domain ),
'class' => '',
'div_class' => ''
+ ),
+ 'quantity' => array(
+ 'id' => 'posttype-quantity',
+ 'type' => 'checkbox',
+ 'label' => __( 'Quantity', $mf_domain ),
+ 'name' => 'mf_posttype[core][quantity]',
+ 'value' => 0,
+ 'description' => __( 'mark true if you want your post type only has one element.', $mf_domain )
)
),
'posttype_support' => array(),
@@ -364,6 +372,7 @@ public function edit_post_type() {
}else{
$data = $this->fields_form();
+
$post_type_support = array();
if( isset($post_type['support']) ){
foreach($post_type['support'] as $k => $v){
@@ -388,11 +397,13 @@ public function edit_post_type() {
}
}
}
+
$this->form_post_type($data);
}
}
function form_post_type($data){
+
global $mf_domain;
$supports = array(
@@ -427,6 +438,10 @@ function form_post_type($data){
<div class="form-field mf_form <?php echo $core['div_class']; ?>">
<?php mf_form_text($core); ?>
</div>
+ <?php elseif($core['type'] == 'checkbox'): ?>
+ <div class="form-field mf_form <?php echo $core['div_class']; ?>">
+ <?php mf_form_checkbox($core); ?>
+ </div>
<?php endif; ?>
<?php endforeach; ?>
<!-- / core -->
View
@@ -31,6 +31,7 @@
global $mf_domain,$mf_pt_register;
$mf_domain = 'magic_fields';
$mf_pt_register = array();
+$mf_pt_unique = array();
/**
* Constants
@@ -106,6 +107,9 @@ function mf_enqueue_scripts() {
wp_enqueue_style( 'mf_admin_css' );
}
+ //unique post type calll
+ add_action('admin_menu', array('mf_menu', 'unique_post_type'));
+
// Settings Page
add_action( 'admin_menu', 'mf_menu' );
function mf_menu( ) {
View
@@ -14,7 +14,7 @@ function __construct(){
// register post type
public function mf_register_post_types(){
- global $mf_pt_register;
+ global $mf_pt_register,$mf_pt_unique;
$post_types = $this->_get_post_types();
@@ -69,9 +69,15 @@ public function mf_register_post_types(){
//description
$option['description'] = $p['core']['description'];
register_post_type($name,$option);
+
+ //add unique post type
+ if ($p['core']['quantity']) {
+ array_push($mf_pt_unique, "edit.php?post_type=".$name);
+ }
+
}
-
+
}
public function _get_cap($name){

0 comments on commit cfba112

Please sign in to comment.