* @version 1.2 */ 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__ ) ), DIRECTORY_SEPARATOR ); //Build the base relative uri by searching backwards until we encounter the wordpress ABSPATH //This may not work if the $base_dir contains a symlink outside of the WordPress ABSPATH $root = array_pop( explode( DIRECTORY_SEPARATOR, rtrim( realpath( ABSPATH ), '/' ) ) ); $path_parts = explode( DIRECTORY_SEPARATOR, $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_domain ); 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 $field[ 'return_value_type' ] = isset( $field[ 'return_value_type' ] ) ? $field[ 'return_value_type' ] : 'link'; 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 = is_array( $field[ 'value' ] ) ? $field[ 'value' ] : array(); if( in_array( $field[ 'input_type' ], array( 'select', 'multiselect' ) ) ) : ?>
l10n_domain ); ?>
l10n_domain ); ?>
l10n_domain ); ?>
l10n_domain ); ?>
l10n_domain ); ?>
$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();