/includes/Fields/Terms.php

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