Skip to content

Commit

Permalink
Add taxonomy_single_term_metabox_bottom and taxonomy_single_term_meta…
Browse files Browse the repository at this point in the history
…box_top hooks
  • Loading branch information
jtsternberg committed Jul 28, 2017
1 parent aa3156d commit df167e3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Taxonomy_Single_Term
==================

Version: 0.2.2
Version: 0.2.3

Taxonomies in WordPress are super powerful. The purpose of taxonomies is to create relationships among post types. Unfortunately the UI doesn't effectively enforce limiting to a single term.

Expand Down Expand Up @@ -50,6 +50,9 @@ $custom_tax_mb->set( 'default', 123 ); // default term_id
```

#### Change Log
**0.2.3**
* Add hooks to the metabox, `taxonomy_single_term_metabox_bottom` and `taxonomy_single_term_metabox_top`.

**0.2.2**
* Add default to options. Props [@BinaryKitten](https://github.com/BinaryKitten)
* Add default setter
Expand Down
27 changes: 13 additions & 14 deletions class.taxonomy-single-term.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @link http://codex.wordpress.org/Function_Reference/add_meta_box#Parameters
* @link https://github.com/WebDevStudios/Taxonomy_Single_Term/blob/master/README.md
* @version 0.2.2
* @version 0.2.3
*/
class Taxonomy_Single_Term {

Expand Down Expand Up @@ -112,7 +112,7 @@ class Taxonomy_Single_Term {
* @var boolean
*/
protected $allow_new_terms = false;

/**
* Default for the selector
* @since 0.2.2
Expand Down Expand Up @@ -144,10 +144,10 @@ public function __construct( $tax_slug, $post_types = array(), $type = 'radio',
$this->bulk_edit_handler();
}
}

/**
* Process default value for settings
*
*
* @param array $default
*
* @return array
Expand All @@ -172,8 +172,6 @@ protected function process_default( $default = array() ) {

return array_filter( $default );
}



/**
* Removes and replaces the built-in taxonomy metabox with our own.
Expand Down Expand Up @@ -202,6 +200,7 @@ public function add_input_element() {
* @todo Abstract inline javascript to it's own file and localize it
*/
public function input_element() {
do_action( 'taxonomy_single_term_metabox_top', $this );

// uses same noncename as default box so no save_post hook needed
wp_nonce_field( 'taxonomy_'. $this->slug, 'taxonomy_noncename' );
Expand All @@ -227,6 +226,7 @@ public function input_element() {
<div style="clear:both;"></div>
</div>
<?php
do_action( 'taxonomy_single_term_metabox_bottom', $this );
}

/**
Expand Down Expand Up @@ -296,11 +296,11 @@ public function term_fields_list() {
get_the_ID(),
$this->slug
);

if ( is_wp_error( $default ) ) {
$default = array();
}

$default[] = $this->default;
$default = (array) current( $default );

Expand Down Expand Up @@ -370,9 +370,9 @@ function ajax_add_term() {
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
$term_name = isset( $_POST['term_name'] ) ? sanitize_text_field( $_POST['term_name'] ) : false;
$taxonomy = isset( $_POST['taxonomy'] ) ? sanitize_text_field( $_POST['taxonomy'] ) : false;

$friendly_taxonomy = $this->taxonomy()->labels->singular_name;

// Ensure user is allowed to add new terms
if( !$this->allow_new_terms ) {
wp_send_json_error( __( "New $friendly_taxonomy terms are not allowed" ) );
Expand Down Expand Up @@ -402,7 +402,6 @@ function ajax_add_term() {
wp_send_json_error();
}


$field_name = $taxonomy == 'category'
? 'post_category'
: 'tax_input[' . $taxonomy . ']';
Expand Down Expand Up @@ -638,12 +637,12 @@ public function set( $property, $value ) {
* @return mixed Property requested.
*/
public function __get( $property ) {

if ( property_exists( $this, $property ) ) {
return $this->{$property};
}
throw new Exception( 'Invalid '. __CLASS__ .' property: ' . $property );

throw new Exception( 'Invalid '. __CLASS__ .' property: ' . $property );
}

}
Expand Down

0 comments on commit df167e3

Please sign in to comment.