| 1 |
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly. |
| 2 |
// phpcs:ignoreFile WordPress.Security.EscapeOutput.OutputNotEscaped |
| 3 |
/** |
| 4 |
* |
| 5 |
* Field: sortable |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
* @version 1.0.0 |
| 9 |
* |
| 10 |
*/ |
| 11 |
if ( ! class_exists( 'CSF_Field_sortable' ) ) { |
| 12 |
class CSF_Field_sortable extends CSF_Fields { |
| 13 |
|
| 14 |
public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { |
| 15 |
parent::__construct( $field, $value, $unique, $where, $parent ); |
| 16 |
} |
| 17 |
|
| 18 |
public function render() { |
| 19 |
|
| 20 |
echo $this->field_before(); |
| 21 |
|
| 22 |
echo '<div class="csf-sortable" data-depend-id="'. esc_attr( $this->field['id'] ) .'">'; |
| 23 |
|
| 24 |
$pre_sortby = array(); |
| 25 |
$pre_fields = array(); |
| 26 |
|
| 27 |
// Add array-keys to defined fields for sort by |
| 28 |
foreach ( $this->field['fields'] as $key => $field ) { |
| 29 |
$pre_fields[$field['id']] = $field; |
| 30 |
} |
| 31 |
|
| 32 |
// Set sort by by saved-value or default-value |
| 33 |
if ( ! empty( $this->value ) ) { |
| 34 |
|
| 35 |
foreach ( $this->value as $key => $value ) { |
| 36 |
$pre_sortby[$key] = $pre_fields[$key]; |
| 37 |
} |
| 38 |
|
| 39 |
$diff = array_diff_key( $pre_fields, $this->value ); |
| 40 |
|
| 41 |
if( ! empty( $diff ) ) { |
| 42 |
$pre_sortby = array_merge( $pre_sortby, $diff ); |
| 43 |
} |
| 44 |
|
| 45 |
} else { |
| 46 |
|
| 47 |
foreach ( $pre_fields as $key => $value ) { |
| 48 |
$pre_sortby[$key] = $value; |
| 49 |
} |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
foreach ( $pre_sortby as $key => $field ) { |
| 54 |
|
| 55 |
echo '<div class="csf-sortable-item">'; |
| 56 |
|
| 57 |
echo '<div class="csf-sortable-content">'; |
| 58 |
|
| 59 |
$field_default = ( isset( $this->field['default'][$key] ) ) ? $this->field['default'][$key] : ''; |
| 60 |
$field_value = ( isset( $this->value[$key] ) ) ? $this->value[$key] : $field_default; |
| 61 |
$unique_id = ( ! empty( $this->unique ) ) ? $this->unique .'['. $this->field['id'] .']' : $this->field['id']; |
| 62 |
|
| 63 |
CSF::field( $field, $field_value, $unique_id, 'field/sortable' ); |
| 64 |
|
| 65 |
echo '</div>'; |
| 66 |
|
| 67 |
echo '<div class="csf-sortable-helper"><i class="fas fa-arrows-alt"></i></div>'; |
| 68 |
|
| 69 |
echo '</div>'; |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
echo '</div>'; |
| 74 |
|
| 75 |
echo $this->field_after(); |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
public function enqueue() { |
| 80 |
|
| 81 |
if ( ! wp_script_is( 'jquery-ui-sortable' ) ) { |
| 82 |
wp_enqueue_script( 'jquery-ui-sortable' ); |
| 83 |
} |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
} |
| 88 |
} |
| 89 |
|