sorter.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
  2. /**
  3. *
  4. * Field: sorter
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. if ( ! class_exists( 'CSF_Field_sorter' ) ) {
  11. class CSF_Field_sorter 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. 'disabled' => true,
  18. 'enabled_title' => esc_html__( 'Enabled', 'sakurairo_csf' ),
  19. 'disabled_title' => esc_html__( 'Disabled', 'sakurairo_csf' ),
  20. ) );
  21. echo $this->field_before();
  22. $this->value = ( ! empty( $this->value ) ) ? $this->value : $this->field['default'];
  23. $enabled_options = ( ! empty( $this->value['enabled'] ) ) ? $this->value['enabled'] : array();
  24. $disabled_options = ( ! empty( $this->value['disabled'] ) ) ? $this->value['disabled'] : array();
  25. echo '<div class="csf-sorter" data-depend-id="'. esc_attr( $this->field['id'] ) .'"></div>';
  26. echo ( $args['disabled'] ) ? '<div class="csf-modules">' : '';
  27. echo ( ! empty( $args['enabled_title'] ) ) ? '<div class="csf-sorter-title">'. esc_attr( $args['enabled_title'] ) .'</div>' : '';
  28. echo '<ul class="csf-enabled">';
  29. if ( ! empty( $enabled_options ) ) {
  30. foreach ( $enabled_options as $key => $value ) {
  31. echo '<li><input type="hidden" name="'. esc_attr( $this->field_name( '[enabled]['. $key .']' ) ) .'" value="'. esc_attr( $value ) .'"/><label>'. esc_attr( $value ) .'</label></li>';
  32. }
  33. }
  34. echo '</ul>';
  35. // Check for hide/show disabled section
  36. if ( $args['disabled'] ) {
  37. echo '</div>';
  38. echo '<div class="csf-modules">';
  39. echo ( ! empty( $args['disabled_title'] ) ) ? '<div class="csf-sorter-title">'. esc_attr( $args['disabled_title'] ) .'</div>' : '';
  40. echo '<ul class="csf-disabled">';
  41. if ( ! empty( $disabled_options ) ) {
  42. foreach ( $disabled_options as $key => $value ) {
  43. echo '<li><input type="hidden" name="'. esc_attr( $this->field_name( '[disabled]['. $key .']' ) ) .'" value="'. esc_attr( $value ) .'"/><label>'. esc_attr( $value ) .'</label></li>';
  44. }
  45. }
  46. echo '</ul>';
  47. echo '</div>';
  48. }
  49. echo $this->field_after();
  50. }
  51. public function enqueue() {
  52. if ( ! wp_script_is( 'jquery-ui-sortable' ) ) {
  53. wp_enqueue_script( 'jquery-ui-sortable' );
  54. }
  55. }
  56. }
  57. }