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