* @version 1.0 */ class ACF_Taxonomy_Field extends acf_Field { /** * Base directory * @var string */ private $base_dir; /** * Relative Uri from the WordPress ABSPATH constant * @var string */ private $base_uri_rel; /** * Absolute Uri * * This is used to create urls to CSS and JavaScript files. * @var string */ private $base_uri_abs; /** * WordPress Localization Text Domain * * The textdomain for the field is controlled by the helper class. * @var string */ private $l10n_domain; /** * Class Constructor - Instantiates a new Taxonomy Field * @param Acf $parent Parent Acf class */ public function __construct( $parent ) { //Call parent constructor parent::__construct( $parent ); //Get the textdomain from the Helper class $this->l10n_domain = ACF_Taxonomy_Field_Helper::L10N_DOMAIN; //Base directory of this field $this->base_dir = rtrim( dirname( realpath( __FILE__ ) ), '/' ); //Build the base relative uri by searching backwards until we encounter the wordpress ABSPATH $root = array_pop( explode( '/', rtrim( ABSPATH, '/' ) ) ); $path_parts = explode( '/', $this->base_dir ); $parts = array(); while( $part = array_pop( $path_parts ) ) { if( $part == $root ) break; array_unshift( $parts, $part ); } $this->base_uri_rel = '/' . implode( '/', $parts ); $this->base_uri_abs = get_site_url( null, $this->base_uri_rel ); $this->name = 'taxonomy-field'; $this->title = __( 'Taxonomy', $this->l10n_doamin ); add_action( 'admin_print_scripts', array( &$this, 'admin_print_scripts' ), 12, 0 ); add_action( 'admin_print_styles', array( &$this, 'admin_print_styles' ), 12, 0 ); } /** * Registers and enqueues necessary CSS * * This method is called by ACF when rendering a post add or edit screen. * We also call this method on the Acf Field Options screen as well in order * to style our Field options * * @see acf_Field::admin_print_styles() */ public function admin_print_styles() { global $pagenow; // wp_register_style( 'acf-taxonomy-field', $this->base_uri_abs . '/taxonomy-field.css' ); if( in_array( $pagenow, array( 'post.php', 'post-new.php' ) ) ) { // wp_enqueue_style( 'acf-taxonomy-field' ); } } /** * Registers and enqueues necessary JavaScript * * This method is called by ACF when rendering a post add or edit screen. * We also call this method on the Acf Field Options screen as well in order * to add the necessary JavaScript for taxonomy selection. * * @see acf_Field::admin_print_scripts() */ public function admin_print_scripts() { global $pagenow; // wp_register_script( 'acf-taxonomy-field', $this->base_uri_abs . '/taxonomy-field.js', array( 'jquery' ) ); if( in_array( $pagenow, array( 'post.php', 'post-new.php' ) ) ) { // wp_enqueue_script( 'acf-taxonomy-field' ); } } /** * Populates the fields array with defaults for this field type * * @param array $field * @return array */ private function set_field_defaults( &$field ) { $field[ 'taxonomy' ] = ( array_key_exists( 'taxonomy', $field ) && isset( $field[ 'taxonomy' ] ) ) ? $field[ 'taxonomy' ] : 'category'; $field[ 'input_type' ] = ( array_key_exists( 'input_type', $field ) && isset( $field[ 'input_type' ] ) ) ? $field[ 'input_type' ] : 'select'; $field[ 'input_size' ] = ( array_key_exists( 'input_size', $field ) && isset( $field[ 'input_size' ] ) ) ? (int) $field[ 'input_size' ] : 5; // $field[ 'allow_new_terms' ] = ( array_key_exists( 'allow_new_terms', $field ) && isset( $field[ 'allow_new_terms' ] ) ) ? (int) $field[ 'allow_new_terms' ] : 0; //false $field[ 'set_post_terms' ] = ( array_key_exists( 'set_post_terms', $field ) && isset( $field[ 'set_post_terms' ] ) ) ? (int) $field[ 'set_post_terms' ] : 1; //true return $field; } /** * Creates the taxonomy field for inside post metaboxes * * @see acf_Field::create_field() */ public function create_field( $field ) { $this->set_field_defaults( $field ); $terms = get_terms( $field['taxonomy'], array( 'hide_empty' => false ) ); $value = $field[ 'value' ]; if( in_array( $field[ 'input_type' ], array( 'select', 'multiselect' ) ) ) : ?> set_field_defaults( $field ); $taxonomies = get_taxonomies( array(), 'objects' ); ksort( $taxonomies ); $tax_choices = array(); foreach( $taxonomies as $tax ) $tax_choices[ $tax->name ] = $tax->label; ?>

l10n_domain ); ?>

parent->create_field( array( 'type' => 'select', 'name' => "fields[{$key}][taxonomy]", 'value' => $field[ 'taxonomy' ], 'choices' => $tax_choices, ) ); ?>

l10n_domain ); ?>

parent->create_field( array( 'type' => 'select', 'name' => "fields[{$key}][input_type]", 'value' => $field[ 'input_type' ], 'class' => 'taxonomy_input_type', 'choices' => array( 'select' => 'Select', 'multiselect' => 'Multi-Select', //'token' => 'Input Tokenizer', ), ) ); ?>

l10n_domain ); ?>

parent->create_field( array( 'type' => 'true_false', 'name' => "fields[{$key}][set_post_terms]", 'value' => $field[ 'set_post_terms' ], ) ); ?>

l10n_domain ); ?>

parent->create_field( array( 'type' => 'select', 'name' => "fields[{$key}][input_size]", 'value' => $field[ 'input_size' ], 'choices' => array_combine( range( 3, 15, 2 ), range( 3, 15, 2 ) ), ) ); ?> set_field_defaults( $field ); if( $field[ 'set_post_terms' ] ) { $terms = array(); foreach( (array) $value as $item ) { if( intval( $item ) > 0 ) $terms[] = intval( $item ); else $terms[] = strval( $item ); } $value = wp_set_object_terms( $post_id, $terms, $field[ 'taxonomy' ], false ); } parent::update_value( $post_id, $field, $value ); } /** * Returns the values of the field * * @see acf_Field::get_value() * @param int $post_id * @param array $field * @return mixed */ public function get_value( $post_id, $field ) { $value = (array) parent::get_value( $post_id, $field ); return $value; } /** * Returns the value of the field for the advanced custom fields API * * @see acf_Field::get_value_for_api() * @param int $post_id * @param array $field * @return string */ public function get_value_for_api( $post_id, $field ) { return parent::get_value_for_api($post_id, $field); } } endif; //class_exists 'ACF_Taxonomy_Field' if( !class_exists( 'ACF_Taxonomy_Field_Helper' ) ) : /** * Advanced Custom Fields - Taxonomy Field Helper * * @author Brian Zoetewey */ class ACF_Taxonomy_Field_Helper { /** * Singleton instance * @var ACF_Taxonomy_Field_Helper */ private static $instance; /** * Returns the ACF_Taxonomy_Field_Helper singleton * * $obj = ACF_Taxonomy_Field_Helper::singleton(); * @return ACF_Taxonomy_Field_Helper */ public static function singleton() { if( !isset( self::$instance ) ) { $class = __CLASS__; self::$instance = new $class(); } return self::$instance; } /** * Prevent cloning of the ACF_Taxonomy_Field_Helper object * @internal */ private function __clone() { } /** * WordPress Localization Text Domain * * Used in wordpress localization and translation methods. * @var string */ const L10N_DOMAIN = 'acf-taxonomy-field'; /** * Language directory path * * Used to build the path for WordPress localization files. * @var string */ private $lang_dir; /** * Constructor */ private function __construct() { $this->lang_dir = rtrim( dirname( realpath( __FILE__ ) ), '/' ) . '/languages'; add_action( 'init', array( &$this, 'register_field' ), 5, 0 ); add_action( 'init', array( &$this, 'load_textdomain' ), 2, 0 ); } /** * Registers the Field with Advanced Custom Fields */ public function register_field() { if( function_exists( 'register_field' ) ) { register_field( 'ACF_Taxonomy_Field', __FILE__ ); } } /** * Loads the textdomain for the current locale if it exists */ public function load_textdomain() { $locale = get_locale(); $mofile = $this->lang_dir . '/' . self::L10N_DOMAIN . '-' . $locale . '.mo'; load_textdomain( self::L10N_DOMAIN, $mofile ); } } endif; //class_exists 'ACF_Taxonomy_Field_Helper' //Instantiate the Addon Helper class ACF_Taxonomy_Field_Helper::singleton();