Terms.php 6.2 KB

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