NF_Fields_Terms

Class NF_Fields_Terms.

Defined (1)

The class is defined in the following location(s).

/includes/Fields/Terms.php  
  1. class NF_Fields_Terms extends NF_Fields_ListCheckbox 
  2. protected $_name = 'terms'; 
  3. protected $_type = 'terms'; 
  4.  
  5. protected $_nicename = 'Terms List'; 
  6.  
  7. protected $_section = ''; 
  8.  
  9. protected $_icon = 'tags'; 
  10.  
  11. protected $_templates = array( 'terms', 'listcheckbox' ); 
  12.  
  13. protected $_settings = array( 'taxonomy', 'add_new_terms' ); 
  14.  
  15. protected $_settings_exclude = array( 'required' ); 
  16.  
  17. protected $_excluded_taxonomies = array( 
  18. 'post_format' 
  19. ); 
  20.  
  21. public function __construct() 
  22. parent::__construct(); 
  23.  
  24. $this->_nicename = __( 'Terms List', 'ninja-forms' ); 
  25.  
  26. add_action( 'admin_init', array( $this, 'init_settings' ) ); 
  27.  
  28. add_filter( 'ninja_forms_display_field', array( $this, 'active_taxonomy_field_check' ) ); 
  29. add_filter( 'ninja_forms_localize_field_' . $this->_type, array( $this, 'add_term_options' ) ); 
  30. add_filter( 'ninja_forms_localize_field_' . $this->_type . '_preview', array( $this, 'add_term_options' ) ); 
  31.  
  32. $this->_settings[ 'options' ][ 'group' ] = ''; 
  33.  
  34. public function process( $field, $data ) 
  35. return $data; 
  36.  
  37. public function init_settings() 
  38. $term_settings = array(); 
  39. $taxonomies = get_taxonomies( array( 'public' => true ), 'objects' ); 
  40. foreach( $taxonomies as $name => $taxonomy ) { 
  41.  
  42. $tax_term_settings = array(); 
  43.  
  44. if( in_array( $name, $this->_excluded_taxonomies ) ) continue; 
  45.  
  46. $this->_settings[ 'taxonomy' ][ 'options' ][] = array( 
  47. 'label' => $taxonomy->labels->name,  
  48. 'value' => $name 
  49. ); 
  50.  
  51. $terms = get_terms( $name, array( 'hide_empty' => false ) ); 
  52.  
  53. foreach( $terms as $term ) { 
  54.  
  55. if( 1 == $term->term_id ) continue; 
  56.  
  57. $tax_term_settings[] = array( 
  58. 'name' => 'taxonomy_term_' . $term->term_id,  
  59. 'type' => 'toggle',  
  60. 'label' => $term->name . ' (' . $term->count .')',  
  61. 'width' => 'one-third',  
  62. 'deps' => array( 
  63. 'taxonomy' => $name 
  64. ),  
  65. ); 
  66.  
  67. if( empty( $tax_term_settings ) ) { 
  68. $tax_term_settings[] = array( 
  69. 'name' => $name . '_no_terms',  
  70. 'type' => 'html',  
  71. 'width' => 'full',  
  72. 'value' => sprintf( __( 'No available terms for this taxonomy. %sAdd a term%s', 'ninja-forms' ), '<a href="' . admin_url( "edit-tags.php?taxonomy=$name" ) . '">', '</a>' ),  
  73. 'deps' => array( 
  74. 'taxonomy' => $name 
  75. ); 
  76.  
  77. $term_settings = array_merge( $term_settings, $tax_term_settings ); 
  78.  
  79.  
  80. $term_settings[] = array( 
  81. 'name' => '_no_taxonomy',  
  82. 'type' => 'html',  
  83. 'width' => 'full',  
  84. 'value' => __( 'No taxonomy selected.', 'ninja-forms' ),  
  85. 'deps' => array( 
  86. 'taxonomy' => '' 
  87. ); 
  88.  
  89. $this->_settings[ 'taxonomy_terms' ] = array( 
  90. 'name' => 'taxonomy_terms',  
  91. 'type' => 'fieldset',  
  92. 'label' => __( 'Available Terms' ),  
  93. 'width' => 'full',  
  94. 'group' => 'primary',  
  95. 'settings' => $term_settings 
  96. ); 
  97.  
  98. public function active_taxonomy_field_check( $field ) 
  99. if( $this->_type != $field->get_setting( 'type' ) ) return $field; 
  100.  
  101. $taxonomy = $field->get_setting( 'taxonomy' ); 
  102.  
  103. if( ! taxonomy_exists( $taxonomy ) ) return FALSE; 
  104.  
  105. return $field; 
  106.  
  107. public function add_term_options( $field ) 
  108. $settings = ( is_object( $field ) ) ? $field->get_settings() : $field[ 'settings' ]; 
  109.  
  110. $settings[ 'options' ] = array(); 
  111.  
  112. if( isset( $settings[ 'taxonomy' ] ) && $settings[ 'taxonomy' ] ) { 
  113.  
  114. $terms = get_terms( $settings[ 'taxonomy' ], array( 'hide_empty' => false ) ); 
  115.  
  116. if( ! is_wp_error( $terms ) ) { 
  117. foreach( $terms as $term ) { 
  118.  
  119. if( ! isset( $settings[ 'taxonomy_term_' . $term->term_id ] ) ) continue; 
  120. if( ! $settings[ 'taxonomy_term_' . $term->term_id ] ) continue; 
  121.  
  122. $settings['options'][] = array( 
  123. 'label' => $term->name,  
  124. 'value' => $term->term_id,  
  125. 'calc' => '',  
  126. 'selected' => 0,  
  127. 'order' => 0 
  128. ); 
  129.  
  130. if( is_object( $field ) ) { 
  131. $field->update_settings( $settings ); 
  132. } else { 
  133. $field[ 'settings' ] = $settings; 
  134.  
  135. return $field; 
  136.  
  137. public function get_parent_type() 
  138. return 'listcheckbox';