sortable.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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( 'CSF_Field_sortable' ) ) {
  11. class CSF_Field_sortable 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. echo $this->field_before();
  17. echo '<div class="csf-sortable" data-depend-id="'. esc_attr( $this->field['id'] ) .'">';
  18. $pre_sortby = array();
  19. $pre_fields = array();
  20. // Add array-keys to defined fields for sort by
  21. foreach ( $this->field['fields'] as $key => $field ) {
  22. $pre_fields[$field['id']] = $field;
  23. }
  24. // Set sort by by saved-value or default-value
  25. if ( ! empty( $this->value ) ) {
  26. foreach ( $this->value as $key => $value ) {
  27. $pre_sortby[$key] = $pre_fields[$key];
  28. }
  29. $diff = array_diff_key( $pre_fields, $this->value );
  30. if( ! empty( $diff ) ) {
  31. $pre_sortby = array_merge( $pre_sortby, $diff );
  32. }
  33. } else {
  34. foreach ( $pre_fields as $key => $value ) {
  35. $pre_sortby[$key] = $value;
  36. }
  37. }
  38. foreach ( $pre_sortby as $key => $field ) {
  39. echo '<div class="csf-sortable-item">';
  40. echo '<div class="csf-sortable-content">';
  41. $field_default = ( isset( $this->field['default'][$key] ) ) ? $this->field['default'][$key] : '';
  42. $field_value = ( isset( $this->value[$key] ) ) ? $this->value[$key] : $field_default;
  43. $unique_id = ( ! empty( $this->unique ) ) ? $this->unique .'['. $this->field['id'] .']' : $this->field['id'];
  44. Sakurairo_CSF::field( $field, $field_value, $unique_id, 'field/sortable' );
  45. echo '</div>';
  46. echo '<div class="csf-sortable-helper"><i class="fas fa-arrows-alt"></i></div>';
  47. echo '</div>';
  48. }
  49. echo '</div>';
  50. echo $this->field_after();
  51. }
  52. public function enqueue() {
  53. if ( ! wp_script_is( 'jquery-ui-sortable' ) ) {
  54. wp_enqueue_script( 'jquery-ui-sortable' );
  55. }
  56. }
  57. }
  58. }