/wp-content/themes/osmosis/includes/framework/inc/fields/sortable/field_sortable.php

https://github.com/Canuckaholic/Pop-Digital · PHP · 151 lines · 100 code · 27 blank · 24 comment · 31 complexity · 54a96db5995c5c7d37779b5d385670f2 MD5 · raw file

  1. <?php
  2. // Exit if accessed directly
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit;
  5. }
  6. if ( ! class_exists( 'ReduxFramework_sortable' ) ) {
  7. class ReduxFramework_sortable {
  8. /**
  9. * Field Constructor.
  10. * Required - must call the parent constructor, then assign field and value to vars, and obviously call the render field function
  11. *
  12. * @since Redux_Options 2.0.1
  13. */
  14. function __construct( $field = array(), $value = '', $parent ) {
  15. $this->parent = $parent;
  16. $this->field = $field;
  17. $this->value = $value;
  18. }
  19. /**
  20. * Field Render Function.
  21. * Takes the vars and outputs the HTML for the field in the settings
  22. *
  23. * @since Redux_Options 2.0.1
  24. */
  25. function render() {
  26. if ( empty( $this->field['mode'] ) ) {
  27. $this->field['mode'] = "text";
  28. }
  29. if ( $this->field['mode'] != "checkbox" && $this->field['mode'] != "text" ) {
  30. $this->field['mode'] = "text";
  31. }
  32. $class = ( isset( $this->field['class'] ) ) ? $this->field['class'] : '';
  33. $options = $this->field['options'];
  34. // This is to weed out missing options that might be in the default
  35. // Why? Who knows. Call it a dummy check.
  36. if ( ! empty( $this->value ) ) {
  37. foreach ( $this->value as $k => $v ) {
  38. if ( ! isset( $options[ $k ] ) ) {
  39. unset( $this->value[ $k ] );
  40. }
  41. }
  42. }
  43. $noSort = false;
  44. foreach ( $options as $k => $v ) {
  45. if ( ! isset( $this->value[ $k ] ) ) {
  46. // A save has previously been done.
  47. if ( is_array( $this->value ) && array_key_exists( $k, $this->value ) ) {
  48. $this->value[ $k ] = $v;
  49. // Missing database entry, meaning no save has yet been done.
  50. } else {
  51. $noSort = true;
  52. $this->value[ $k ] = '';
  53. }
  54. }
  55. }
  56. // If missing database entries are found, it means no save has been done
  57. // and therefore no sort should be done. Set the default array in the same
  58. // order as the options array. Why? The sort order is based on the
  59. // saved default array. If entries are missing, the sort is messed up.
  60. // - kp
  61. if ( true == $noSort ) {
  62. $dummyArr = array();
  63. foreach ( $options as $k => $v ) {
  64. $dummyArr[ $k ] = $this->value[ $k ];
  65. }
  66. unset( $this->value );
  67. $this->value = $dummyArr;
  68. unset( $dummytArr );
  69. }
  70. echo '<ul id="' . $this->field['id'] . '-list" class="redux-sortable ' . $class . '">';
  71. foreach ( $this->value as $k => $nicename ) {
  72. echo '<li>';
  73. $checked = "";
  74. $name = 'name="' . $this->field['name'] . $this->field['name_suffix'] . '[' . $k . ']' . '" ';
  75. if ( $this->field['mode'] == "checkbox" ) {
  76. $value_display = $this->value[ $k ];
  77. if ( ! empty( $this->value[ $k ] ) ) {
  78. $checked = 'checked="checked" ';
  79. }
  80. $class .= " checkbox_sortable";
  81. $name = "";
  82. echo '<input type="hidden" name="' . $this->field['name'] . $this->field['name_suffix'] . '[' . $k . ']' . '" id="' . $this->field['id'] . '-' . $k . '-hidden" value="' . $value_display . '" />';
  83. echo '<div class="checkbox-container">';
  84. } else {
  85. $value_display = isset( $this->value[ $k ] ) ? $this->value[ $k ] : '';
  86. }
  87. if ($this->field['mode'] != "checkbox") {
  88. echo '<label class="bugger" for="' . $this->field['id'] . '[' . $k . ']"><strong>' . $k . '</strong></label>';
  89. echo "<br />";
  90. }
  91. echo '<input rel="' . $this->field['id'] . '-' . $k . '-hidden" class="' . $class . '" ' . $checked . 'type="' . $this->field['mode'] . '" ' . $name . 'id="' . $this->field['id'] . '[' . $k . ']" value="' . esc_attr( $value_display ) . '" placeholder="' . $nicename . '" />';
  92. echo '<span class="compact drag"><i class="el-icon-move icon-large"></i></span>';
  93. if ( $this->field['mode'] == "checkbox" || ( isset( $this->field['label'] ) && $this->field['label'] == true ) ) {
  94. if ( $this->field['mode'] != "checkbox" ) {
  95. //echo "<br />";
  96. //echo '<label for="' . $this->field['id'] . '[' . $k . ']"><strong>' . $k . '</strong></label>';
  97. } else {
  98. echo '<label for="' . $this->field['id'] . '[' . $k . ']"><strong>' . $options[ $k ] . '</strong></label>';
  99. }
  100. }
  101. if ( $this->field['mode'] == "checkbox" ) {
  102. echo '</div>';
  103. }
  104. echo '</li>';
  105. }
  106. echo '</ul>';
  107. }
  108. function enqueue() {
  109. wp_enqueue_style(
  110. 'redux-field-sortable-css',
  111. ReduxFramework::$_url . 'inc/fields/sortable/field_sortable.css',
  112. time(),
  113. true
  114. );
  115. wp_enqueue_script(
  116. 'redux-field-sortable-js',
  117. ReduxFramework::$_url . 'inc/fields/sortable/field_sortable' . Redux_Functions::isMin() . '.js',
  118. array( 'jquery', 'redux-js' ),
  119. time(),
  120. true
  121. );
  122. }
  123. }
  124. }