| 1 |
<?php if ( ! defined( 'ABSPATH' ) ) { |
| 2 |
die; } // Cannot access directly. |
| 3 |
/** |
| 4 |
* |
| 5 |
* Field: group |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
* @version 1.0.0 |
| 9 |
*/ |
| 10 |
if ( ! class_exists( 'DRK_LITE_Field_group' ) ) { |
| 11 |
class DRK_LITE_Field_group extends DRK_LITE_Fields { |
| 12 |
|
| 13 |
public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { |
| 14 |
parent::__construct( $field, $value, $unique, $where, $parent ); |
| 15 |
} |
| 16 |
|
| 17 |
public function render() { |
| 18 |
|
| 19 |
$args = wp_parse_args( |
| 20 |
$this->field, |
| 21 |
array( |
| 22 |
'max' => 0, |
| 23 |
'min' => 0, |
| 24 |
'fields' => array(), |
| 25 |
'button_title' => esc_html__( 'Add New', 'darkify' ), |
| 26 |
'accordion_title_prefix' => '', |
| 27 |
'accordion_title_number' => false, |
| 28 |
'accordion_title_auto' => true, |
| 29 |
'accordion_title_by' => array(), |
| 30 |
'accordion_title_by_prefix' => ' ', |
| 31 |
) |
| 32 |
); |
| 33 |
|
| 34 |
$title_prefix = ( ! empty( $args['accordion_title_prefix'] ) ) ? $args['accordion_title_prefix'] : ''; |
| 35 |
$title_number = ( ! empty( $args['accordion_title_number'] ) ) ? true : false; |
| 36 |
$title_auto = ( ! empty( $args['accordion_title_auto'] ) ) ? true : false; |
| 37 |
$title_first = ( isset( $this->field['fields'][0]['id'] ) ) ? $this->field['fields'][0]['id'] : $this->field['fields'][1]['id']; |
| 38 |
$title_by = ( is_array( $args['accordion_title_by'] ) ) ? $args['accordion_title_by'] : (array) $args['accordion_title_by']; |
| 39 |
$title_by = ( empty( $title_by ) ) ? array( $title_first ) : $title_by; |
| 40 |
$title_by_prefix = ( ! empty( $args['accordion_title_by_prefix'] ) ) ? $args['accordion_title_by_prefix'] : ''; |
| 41 |
|
| 42 |
if ( preg_match( '/' . preg_quote( '[' . $this->field['id'] . ']' ) . '/', $this->unique ) ) { |
| 43 |
|
| 44 |
echo '<div class="drk_lite-notice drk_lite-notice-danger">' . esc_html__( 'Error: Field ID conflict.', 'darkify' ) . '</div>'; |
| 45 |
|
| 46 |
} else { |
| 47 |
|
| 48 |
echo wp_kses_post( $this->field_before() ); |
| 49 |
|
| 50 |
echo '<div class="drk_lite-cloneable-item drk_lite-cloneable-hidden" data-depend-id="' . esc_attr( $this->field['id'] ) . '">'; |
| 51 |
|
| 52 |
echo '<div class="drk_lite-cloneable-helper">'; |
| 53 |
echo '<i class="drk_lite-cloneable-sort fas fa-arrows-alt"></i>'; |
| 54 |
echo '<i class="drk_lite-cloneable-clone far fa-clone"></i>'; |
| 55 |
echo '<i class="drk_lite-cloneable-remove drk_lite-confirm fas fa-times" data-confirm="' . esc_html__( 'Are you sure to delete this item?', 'darkify' ) . '"></i>'; |
| 56 |
echo '</div>'; |
| 57 |
|
| 58 |
echo '<h4 class="drk_lite-cloneable-title">'; |
| 59 |
echo '<span class="drk_lite-cloneable-text">'; |
| 60 |
echo ( $title_number ) ? '<span class="drk_lite-cloneable-title-number"></span>' : ''; |
| 61 |
echo ( $title_prefix ) ? '<span class="drk_lite-cloneable-title-prefix">' . esc_attr( $title_prefix ) . '</span>' : ''; |
| 62 |
echo ( $title_auto ) ? '<span class="drk_lite-cloneable-value"><span class="drk_lite-cloneable-placeholder"></span></span>' : ''; |
| 63 |
echo '</span>'; |
| 64 |
echo '</h4>'; |
| 65 |
|
| 66 |
echo '<div class="drk_lite-cloneable-content">'; |
| 67 |
foreach ( $this->field['fields'] as $field ) { |
| 68 |
|
| 69 |
$field_default = ( isset( $field['default'] ) ) ? $field['default'] : ''; |
| 70 |
$field_unique = ( ! empty( $this->unique ) ) ? $this->unique . '[' . $this->field['id'] . '][0]' : $this->field['id'] . '[0]'; |
| 71 |
|
| 72 |
DRK_LITE::field( $field, $field_default, '___' . $field_unique, 'field/group' ); |
| 73 |
|
| 74 |
} |
| 75 |
echo '</div>'; |
| 76 |
|
| 77 |
echo '</div>'; |
| 78 |
|
| 79 |
echo '<div class="drk_lite-cloneable-wrapper drk_lite-data-wrapper" data-title-by="' . esc_attr( wp_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'] ) . '">'; |
| 80 |
|
| 81 |
if ( ! empty( $this->value ) ) { |
| 82 |
|
| 83 |
$num = 0; |
| 84 |
|
| 85 |
foreach ( $this->value as $value ) { |
| 86 |
|
| 87 |
$title = ''; |
| 88 |
|
| 89 |
if ( ! empty( $title_by ) ) { |
| 90 |
|
| 91 |
$titles = array(); |
| 92 |
|
| 93 |
foreach ( $title_by as $title_key ) { |
| 94 |
if ( isset( $value[ $title_key ] ) ) { |
| 95 |
$titles[] = $value[ $title_key ]; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
$title = join( $title_by_prefix, $titles ); |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
$title = ( is_array( $title ) ) ? reset( $title ) : $title; |
| 104 |
|
| 105 |
echo '<div class="drk_lite-cloneable-item">'; |
| 106 |
|
| 107 |
echo '<div class="drk_lite-cloneable-helper">'; |
| 108 |
echo '<i class="drk_lite-cloneable-sort fas fa-arrows-alt"></i>'; |
| 109 |
echo '<i class="drk_lite-cloneable-clone far fa-clone"></i>'; |
| 110 |
echo '<i class="drk_lite-cloneable-remove drk_lite-confirm fas fa-times" data-confirm="' . esc_html__( 'Are you sure to delete this item?', 'darkify' ) . '"></i>'; |
| 111 |
echo '</div>'; |
| 112 |
|
| 113 |
echo '<h4 class="drk_lite-cloneable-title">'; |
| 114 |
echo '<span class="drk_lite-cloneable-text">'; |
| 115 |
echo ( $title_number ) ? '<span class="drk_lite-cloneable-title-number">' . esc_attr( $num + 1 ) . '.</span>' : ''; |
| 116 |
echo ( $title_prefix ) ? '<span class="drk_lite-cloneable-title-prefix">' . esc_attr( $title_prefix ) . '</span>' : ''; |
| 117 |
echo ( $title_auto ) ? '<span class="drk_lite-cloneable-value">' . esc_attr( $title ) . '</span>' : ''; |
| 118 |
echo '</span>'; |
| 119 |
echo '</h4>'; |
| 120 |
|
| 121 |
echo '<div class="drk_lite-cloneable-content">'; |
| 122 |
|
| 123 |
foreach ( $this->field['fields'] as $field ) { |
| 124 |
|
| 125 |
$field_unique = ( ! empty( $this->unique ) ) ? $this->unique . '[' . $this->field['id'] . '][' . $num . ']' : $this->field['id'] . '[' . $num . ']'; |
| 126 |
$field_value = ( isset( $field['id'] ) && isset( $value[ $field['id'] ] ) ) ? $value[ $field['id'] ] : ''; |
| 127 |
|
| 128 |
DRK_LITE::field( $field, $field_value, $field_unique, 'field/group' ); |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
echo '</div>'; |
| 133 |
|
| 134 |
echo '</div>'; |
| 135 |
|
| 136 |
++$num; |
| 137 |
|
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
echo '</div>'; |
| 142 |
|
| 143 |
echo '<div class="drk_lite-cloneable-alert drk_lite-cloneable-max">' . esc_html__( 'You cannot add more.', 'darkify' ) . '</div>'; |
| 144 |
echo '<div class="drk_lite-cloneable-alert drk_lite-cloneable-min">' . esc_html__( 'You cannot remove more.', 'darkify' ) . '</div>'; |
| 145 |
echo '<a href="#" class="button button-primary drk_lite-cloneable-add">' . esc_html( $args['button_title'] ) . '</a>'; |
| 146 |
|
| 147 |
echo wp_kses_post( $this->field_after() ); |
| 148 |
|
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
public function enqueue() { |
| 153 |
|
| 154 |
if ( ! wp_script_is( 'jquery-ui-accordion' ) ) { |
| 155 |
wp_enqueue_script( 'jquery-ui-accordion' ); |
| 156 |
} |
| 157 |
|
| 158 |
if ( ! wp_script_is( 'jquery-ui-sortable' ) ) { |
| 159 |
wp_enqueue_script( 'jquery-ui-sortable' ); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|