repeater.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
  2. /**
  3. *
  4. * Field: repeater
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. if ( ! class_exists( 'CSF_Field_repeater' ) ) {
  11. class CSF_Field_repeater 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. 'button_title' => '<i class="fas fa-plus-circle"></i>',
  20. ) );
  21. if ( preg_match( '/'. preg_quote( '['. $this->field['id'] .']' ) .'/', $this->unique ) ) {
  22. echo '<div class="csf-notice csf-notice-danger">'. esc_html__( 'Error: Field ID conflict.', 'sakurairo_csf' ) .'</div>';
  23. } else {
  24. echo $this->field_before();
  25. echo '<div class="csf-repeater-item csf-repeater-hidden" data-depend-id="'. esc_attr( $this->field['id'] ) .'">';
  26. echo '<div class="csf-repeater-content">';
  27. foreach ( $this->field['fields'] as $field ) {
  28. $field_default = ( isset( $field['default'] ) ) ? $field['default'] : '';
  29. $field_unique = ( ! empty( $this->unique ) ) ? $this->unique .'['. $this->field['id'] .'][0]' : $this->field['id'] .'[0]';
  30. Sakurairo_CSF::field( $field, $field_default, '___'. $field_unique, 'field/repeater' );
  31. }
  32. echo '</div>';
  33. echo '<div class="csf-repeater-helper">';
  34. echo '<div class="csf-repeater-helper-inner">';
  35. echo '<i class="csf-repeater-sort fas fa-arrows-alt"></i>';
  36. echo '<i class="csf-repeater-clone far fa-clone"></i>';
  37. echo '<i class="csf-repeater-remove csf-confirm fas fa-times" data-confirm="'. esc_html__( 'Are you sure to delete this item?', 'sakurairo_csf' ) .'"></i>';
  38. echo '</div>';
  39. echo '</div>';
  40. echo '</div>';
  41. echo '<div class="csf-repeater-wrapper csf-data-wrapper" data-field-id="['. esc_attr( $this->field['id'] ) .']" data-max="'. esc_attr( $args['max'] ) .'" data-min="'. esc_attr( $args['min'] ) .'">';
  42. if ( ! empty( $this->value ) && is_array( $this->value ) ) {
  43. $num = 0;
  44. foreach ( $this->value as $key => $value ) {
  45. echo '<div class="csf-repeater-item">';
  46. echo '<div class="csf-repeater-content">';
  47. foreach ( $this->field['fields'] as $field ) {
  48. $field_unique = ( ! empty( $this->unique ) ) ? $this->unique .'['. $this->field['id'] .']['. $num .']' : $this->field['id'] .'['. $num .']';
  49. $field_value = ( isset( $field['id'] ) && isset( $this->value[$key][$field['id']] ) ) ? $this->value[$key][$field['id']] : '';
  50. Sakurairo_CSF::field( $field, $field_value, $field_unique, 'field/repeater' );
  51. }
  52. echo '</div>';
  53. echo '<div class="csf-repeater-helper">';
  54. echo '<div class="csf-repeater-helper-inner">';
  55. echo '<i class="csf-repeater-sort fas fa-arrows-alt"></i>';
  56. echo '<i class="csf-repeater-clone far fa-clone"></i>';
  57. echo '<i class="csf-repeater-remove csf-confirm fas fa-times" data-confirm="'. esc_html__( 'Are you sure to delete this item?', 'sakurairo_csf' ) .'"></i>';
  58. echo '</div>';
  59. echo '</div>';
  60. echo '</div>';
  61. $num++;
  62. }
  63. }
  64. echo '</div>';
  65. echo '<div class="csf-repeater-alert csf-repeater-max">'. esc_html__( 'You cannot add more.', 'sakurairo_csf' ) .'</div>';
  66. echo '<div class="csf-repeater-alert csf-repeater-min">'. esc_html__( 'You cannot remove more.', 'sakurairo_csf' ) .'</div>';
  67. echo '<a href="#" class="button button-primary csf-repeater-add">'. $args['button_title'] .'</a>';
  68. echo $this->field_after();
  69. }
  70. }
  71. public function enqueue() {
  72. if ( ! wp_script_is( 'jquery-ui-sortable' ) ) {
  73. wp_enqueue_script( 'jquery-ui-sortable' );
  74. }
  75. }
  76. }
  77. }