1
0

group.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
  2. /**
  3. *
  4. * Field: group
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. if ( ! class_exists( 'CSF_Field_group' ) ) {
  11. class CSF_Field_group extends CSF_Fields {
  12. public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) {
  13. parent::__construct( $field, $value, $unique, $where, $parent );
  14. }
  15. public function render() {
  16. $args = wp_parse_args( $this->field, array(
  17. 'max' => 0,
  18. 'min' => 0,
  19. 'fields' => array(),
  20. 'button_title' => esc_html__( 'Add New', 'sakurairo_csf' ),
  21. 'accordion_title_prefix' => '',
  22. 'accordion_title_number' => false,
  23. 'accordion_title_auto' => true,
  24. 'accordion_title_by' => array(),
  25. 'accordion_title_by_prefix' => ' ',
  26. ) );
  27. $title_prefix = ( ! empty( $args['accordion_title_prefix'] ) ) ? $args['accordion_title_prefix'] : '';
  28. $title_number = ( ! empty( $args['accordion_title_number'] ) ) ? true : false;
  29. $title_auto = ( ! empty( $args['accordion_title_auto'] ) ) ? true : false;
  30. $title_first = ( isset( $this->field['fields'][0]['id'] ) ) ? $this->field['fields'][0]['id'] : $this->field['fields'][1]['id'];
  31. $title_by = ( is_array( $args['accordion_title_by'] ) ) ? $args['accordion_title_by'] : (array) $args['accordion_title_by'];
  32. $title_by = ( empty( $title_by ) ) ? array( $title_first ) : $title_by;
  33. $title_by_prefix = ( ! empty( $args['accordion_title_by_prefix'] ) ) ? $args['accordion_title_by_prefix'] : '';
  34. if ( preg_match( '/'. preg_quote( '['. $this->field['id'] .']' ) .'/', $this->unique ) ) {
  35. echo '<div class="csf-notice csf-notice-danger">'. esc_html__( 'Error: Field ID conflict.', 'sakurairo_csf' ) .'</div>';
  36. } else {
  37. echo $this->field_before();
  38. echo '<div class="csf-cloneable-item csf-cloneable-hidden" data-depend-id="'. esc_attr( $this->field['id'] ) .'">';
  39. echo '<div class="csf-cloneable-helper">';
  40. echo '<i class="csf-cloneable-sort fas fa-arrows-alt"></i>';
  41. echo '<i class="csf-cloneable-clone far fa-clone"></i>';
  42. echo '<i class="csf-cloneable-remove csf-confirm fas fa-times" data-confirm="'. esc_html__( 'Are you sure to delete this item?', 'sakurairo_csf' ) .'"></i>';
  43. echo '</div>';
  44. echo '<h4 class="csf-cloneable-title">';
  45. echo '<span class="csf-cloneable-text">';
  46. echo ( $title_number ) ? '<span class="csf-cloneable-title-number"></span>' : '';
  47. echo ( $title_prefix ) ? '<span class="csf-cloneable-title-prefix">'. esc_attr( $title_prefix ) .'</span>' : '';
  48. echo ( $title_auto ) ? '<span class="csf-cloneable-value"><span class="csf-cloneable-placeholder"></span></span>' : '';
  49. echo '</span>';
  50. echo '</h4>';
  51. echo '<div class="csf-cloneable-content">';
  52. foreach ( $this->field['fields'] as $field ) {
  53. $field_default = ( isset( $field['default'] ) ) ? $field['default'] : '';
  54. $field_unique = ( ! empty( $this->unique ) ) ? $this->unique .'['. $this->field['id'] .'][0]' : $this->field['id'] .'[0]';
  55. Sakurairo_CSF::field( $field, $field_default, '___'. $field_unique, 'field/group' );
  56. }
  57. echo '</div>';
  58. echo '</div>';
  59. echo '<div class="csf-cloneable-wrapper csf-data-wrapper" data-title-by="'. esc_attr( json_encode( $title_by ) ) .'" data-title-by-prefix="'. esc_attr( $title_by_prefix ) .'" data-title-number="'. esc_attr( $title_number ) .'" data-field-id="['. esc_attr( $this->field['id'] ) .']" data-max="'. esc_attr( $args['max'] ) .'" data-min="'. esc_attr( $args['min'] ) .'">';
  60. if ( ! empty( $this->value ) ) {
  61. $num = 0;
  62. foreach ( $this->value as $value ) {
  63. $title = '';
  64. if ( ! empty( $title_by ) ) {
  65. $titles = array();
  66. foreach ( $title_by as $title_key ) {
  67. if ( isset( $value[ $title_key ] ) ) {
  68. $titles[] = $value[ $title_key ];
  69. }
  70. }
  71. $title = join( $title_by_prefix, $titles );
  72. }
  73. $title = ( is_array( $title ) ) ? reset( $title ) : $title;
  74. echo '<div class="csf-cloneable-item">';
  75. echo '<div class="csf-cloneable-helper">';
  76. echo '<i class="csf-cloneable-sort fas fa-arrows-alt"></i>';
  77. echo '<i class="csf-cloneable-clone far fa-clone"></i>';
  78. echo '<i class="csf-cloneable-remove csf-confirm fas fa-times" data-confirm="'. esc_html__( 'Are you sure to delete this item?', 'sakurairo_csf' ) .'"></i>';
  79. echo '</div>';
  80. echo '<h4 class="csf-cloneable-title">';
  81. echo '<span class="csf-cloneable-text">';
  82. echo ( $title_number ) ? '<span class="csf-cloneable-title-number">'. esc_attr( $num+1 ) .'.</span>' : '';
  83. echo ( $title_prefix ) ? '<span class="csf-cloneable-title-prefix">'. esc_attr( $title_prefix ) .'</span>' : '';
  84. echo ( $title_auto ) ? '<span class="csf-cloneable-value">' . esc_attr( $title ) .'</span>' : '';
  85. echo '</span>';
  86. echo '</h4>';
  87. echo '<div class="csf-cloneable-content">';
  88. foreach ( $this->field['fields'] as $field ) {
  89. $field_unique = ( ! empty( $this->unique ) ) ? $this->unique .'['. $this->field['id'] .']['. $num .']' : $this->field['id'] .'['. $num .']';
  90. $field_value = ( isset( $field['id'] ) && isset( $value[$field['id']] ) ) ? $value[$field['id']] : '';
  91. Sakurairo_CSF::field( $field, $field_value, $field_unique, 'field/group' );
  92. }
  93. echo '</div>';
  94. echo '</div>';
  95. $num++;
  96. }
  97. }
  98. echo '</div>';
  99. echo '<div class="csf-cloneable-alert csf-cloneable-max">'. esc_html__( 'You cannot add more.', 'sakurairo_csf' ) .'</div>';
  100. echo '<div class="csf-cloneable-alert csf-cloneable-min">'. esc_html__( 'You cannot remove more.', 'sakurairo_csf' ) .'</div>';
  101. echo '<a href="#" class="button button-primary csf-cloneable-add">'. $args['button_title'] .'</a>';
  102. echo $this->field_after();
  103. }
  104. }
  105. public function enqueue() {
  106. if ( ! wp_script_is( 'jquery-ui-accordion' ) ) {
  107. wp_enqueue_script( 'jquery-ui-accordion' );
  108. }
  109. if ( ! wp_script_is( 'jquery-ui-sortable' ) ) {
  110. wp_enqueue_script( 'jquery-ui-sortable' );
  111. }
  112. }
  113. }
  114. }