Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 188 lines (149 sloc) 6.168 kb
2ce01ce @benbalter initial commit
authored
1 <?php
2 /*
3 Plugin Name: Type Custom Document Field
4 Plugin URI:
5 Description: Creates type custom taxonomy for use with WP Document Revisions
6 Version: 0.1
7 Author: Benjamin J. Balter
8 Author URI: http://ben.balter.com
9 License: GPL2
10 */
11
12 /**
13 * Callback to register Type custom document taxonomy
14 */
15 function wp_document_revisions_register_type_ct() {
16
17 $labels = array(
18 'name' => _x( 'Types', 'taxonomy general name' ),
19 'singular_name' => _x( 'Type', 'taxonomy singular name' ),
20 'search_items' => __( 'Search Types' ),
21 'all_items' => __( 'All Types' ),
22 'parent_item' => __( 'Parent Type' ),
23 'parent_item_colon' => __( 'Parent Type:' ),
24 'edit_item' => __( 'Edit Type' ),
25 'update_item' => __( 'Update Type' ),
26 'add_new_item' => __( 'Add New Type' ),
27 'new_item_name' => __( 'New Type Name' ),
28 'menu_name' => __( 'Type' ),
29 );
30
31 register_taxonomy('document_type',array('document'), array(
32 'hierarchical' => true,
33 'labels' => $labels,
34 'show_ui' => true,
35 'public' => true,
2f742ef @benbalter rewrites
authored
36 'rewrite' => array( 'slug' => 'type' ),
2ce01ce @benbalter initial commit
authored
37 ));
38
39 }
40
41 //add action hook
42 add_action( 'init', 'wp_document_revisions_register_type_ct');
43
44
45 /**
46 * Callback to register Type metabox
47 */
48 function wp_document_revisions_register_type_metabox() {
49
50 //remove default metabox
51 remove_meta_box( 'document_typediv', 'document', 'side');
52
53 //add custom metabox
54 add_meta_box( 'document_type', 'Type', 'wp_document_revisions_type_cb', 'document', 'side');
55
56 }
57
58 //add action hook
59 add_action( 'add_meta_boxes', 'wp_document_revisions_register_type_metabox' );
60
61 /**
62 * Generates the Type taxonomy radio inputs
63 * @params object $post the post object WP passes
64 * @params object $box the meta box object WP passes (with our arg stuffed in there)
65 */
66 function wp_document_revisions_type_cb( $post ) {
67
68 //get the taxonomy and labels
69 $taxonomy = get_taxonomy( 'document_type' );
70
71 //grab an array of all terms within our custom taxonomy, including empty terms
72 $terms = get_terms( 'document_type', array( 'hide_empty' => false ) );
73
74 //garb the current selected term where applicable so we can select it
75 $current = wp_get_object_terms( $post->ID, 'document_type' );
76
77 //loop through the terms
78 foreach ($terms as $term) {
79
80 //build the radio box with the term_id as its value
81 echo '<input type="radio" name="document_type" value="'.$term->term_id.'" id="'.$term->slug.'"';
82
83 //if the post is already in this taxonomy, select it
84 if ( isset( $current[0]->term_id ) )
85 checked( $term->term_id, $current[0]->term_id );
86
87 //build the label
88 echo '> <label for="'.$term->slug.'">' . $term->name . '</label><br />'. "\r\n";
89 }
90 echo '<input type="radio" name="document_type" value="" id="none" ';
91 checked( empty($current[0]->term_id) );
92 echo '/> <label for="none">None</label><br />'. "\r\n";
93 ?>
94 <a href="#" id="add_document_type_toggle">+ <?php echo $taxonomy->labels->add_new_item; ?></a>
95 <div id="add_document_type_div" style="display:none">
96 <label for="new_document_type"><?php echo $taxonomy->labels->singular_name; ?>:</label>
97 <input type="text" name="new_document_type" id="new_document_type" /><br />
98 <input type="button" value="Add New" id="add_document_type_button" />
99 <img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" id="document_type-ajax-loading" style="display:none;" alt="" />
100 </div>
101 <script>
102 jQuery(document).ready(function($){
103 $('#add_document_type_toggle').click(function(event){
104 $('#add_document_type_div').toggle();
105 event.preventDefault();
106 });
107 $('#add_document_type_button').click(function() {
108 $('#document_type-ajax-loading').show();
109 $.post('admin-ajax.php?action=add_document_type', $('#new_document_type, #new_document_type_location, #_ajax_nonce-add-document_type, #post_ID').serialize(), function(data) {
110 $('#document_type .inside').html(data);
111 });
112 });
113 });
114 </script>
115 <?php
116 //nonce is a funny word
117 wp_nonce_field( 'add_document_type', '_ajax_nonce-add-document_type' );
118 wp_nonce_field( 'document_type', 'document_type_nonce');
119 }
120
121 /**
122 * Processes AJAX request to add new Type terms
123 * @since 1.2
124 */
125 function wp_document_revisions_type_ajax_add() {
126
127 //pull the taxonomy from the action query var
128 $type = substr($_GET['action'],4);
129
130 //pull up the taxonomy details
131 $taxonomy = get_taxonomy($type);
132
133 //check the nonce
134 check_ajax_referer( $_GET['action'] , '_ajax_nonce-add-document_type' );
135
136 //check user capabilities
137 if ( !current_user_can( $taxonomy->cap->edit_terms ) )
138 die('-1');
139
140 //insert term
141 $term = wp_insert_term( $_POST['new_document_type'], 'document_type' );
142
143 //get updated post to send to taxonomy box
144 $post = get_post( $_POST['post_ID'] );
145
146 //return the HTML of the updated metabox back to the user so they can use the new term
147 wp_document_revisions_type_cb( $post );
148 exit();
149 }
150
151 add_action('wp_ajax_add_document_type', 'wp_document_revisions_type_ajax_add');
152
153 /**
154 * Callback to store Type custom taxonomy
155 */
156 function wp_document_revisions_save_type( $post_id ) {
157
158 //verify this is not an autosave
159 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
160 return;
161
162 //verify nonce
163 if ( !isset( $_POST['document_type_nonce'] ) ||
164 !wp_verify_nonce( $_POST['document_type_nonce'], 'document_type' ) )
165 return;
166
167 //verify permissions
168 if ( !current_user_can( 'edit_post', $post_id ) )
169 return;
170
171 //associate taxonomy with parent, not revision
172 if ( wp_is_post_revision( $post_id ) )
173 $post_id = wp_is_post_revision( $post_id );
174
175 //store the data
176 wp_set_post_terms( $post_id, $_POST['document_type'], 'document_type', false);
177
178 }
179
180 add_action( 'save_post', 'wp_document_revisions_save_type', 10, 1 );
181
182
183
184 /***********************************
185 WP Document Revisions Custom Field
186 a:4:{s:4:"name";s:4:"Type";s:11:"name_plural";s:5:"Types";s:4:"slug";s:4:"type";s:4:"type";s:9:"exclusive";}
187 ************************************/
188 ?>
Something went wrong with that request. Please try again.