ReduxFramework_sorter

The evolve ReduxFramework sorter class.

Defined (1)

The class is defined in the following location(s).

/library/admin/redux-framework/inc/fields/sorter/field_sorter.php  
  1. class ReduxFramework_sorter { 
  2.  
  3. /** 
  4. * Field Constructor. 
  5. * Required - must call the parent constructor, then assign field and value to vars, and obviously call the render field function 
  6. * @since Redux_Options 1.0.0 
  7. */ 
  8. function __construct( $field = array(), $value = '', $parent ) { 
  9. $this->parent = $parent; 
  10. $this->field = $field; 
  11. $this->value = $value; 
  12.  
  13. private function replace_id_with_slug( $arr ) { 
  14. $new_arr = array(); 
  15. if ( ! empty( $arr ) ) { 
  16. foreach ( $arr as $id => $name ) { 
  17.  
  18. if ( is_numeric( $id ) ) { 
  19. $slug = strtolower( $name ); 
  20. $slug = str_replace( ' ', '-', $slug ); 
  21.  
  22. $new_arr[ $slug ] = $name; 
  23. } else { 
  24. $new_arr[ $id ] = $name; 
  25.  
  26. return $new_arr; 
  27.  
  28. private function is_value_empty( $val ) { 
  29. if ( ! empty( $val ) ) { 
  30. foreach ( $val as $section => $arr ) { 
  31. if ( ! empty( $arr ) ) { 
  32. return false; 
  33.  
  34.  
  35. return true; 
  36.  
  37. /** 
  38. * Field Render Function. 
  39. * Takes the vars and outputs the HTML for the field in the settings 
  40. * @since 1.0.0 
  41. */ 
  42. function render() { 
  43.  
  44. if ( ! is_array( $this->value ) && isset( $this->field['options'] ) ) { 
  45. $this->value = $this->field['options']; 
  46.  
  47. if ( ! isset( $this->field['args'] ) ) { 
  48. $this->field['args'] = array(); 
  49.  
  50. if ( isset( $this->field['data'] ) ) { 
  51. $this->field['options'] = $this->parent->options_defaults[ $this->field['id'] ]; 
  52.  
  53. // Make sure to get list of all the default blocks first 
  54. $all_blocks = ! empty( $this->field['options'] ) ? $this->field['options'] : array(); 
  55. $temp = array(); // holds default blocks 
  56. $temp2 = array(); // holds saved blocks 
  57.  
  58. foreach ( $all_blocks as $blocks ) { 
  59. $temp = array_merge( $temp, $blocks ); 
  60.  
  61. $temp = $this->replace_id_with_slug( $temp ); 
  62.  
  63. if ( $this->is_value_empty( $this->value ) ) { 
  64. if ( ! empty( $this->field['options'] ) ) { 
  65. $this->value = $this->field['options']; 
  66.  
  67. $sortlists = $this->value; 
  68. if ( ! empty( $sortlists ) ) { 
  69. foreach ( $sortlists as $section => $arr ) { 
  70. $sortlists[ $section ] = $this->replace_id_with_slug( $arr ); 
  71.  
  72. if ( is_array( $sortlists ) ) { 
  73. foreach ( $sortlists as $sortlist ) { 
  74. $temp2 = array_merge( $temp2, $sortlist ); 
  75.  
  76. // now let's compare if we have anything missing 
  77. foreach ( $temp as $k => $v ) { 
  78. // k = id/slug 
  79. // v = name 
  80.  
  81. if ( ! empty( $temp2 ) ) { 
  82. if ( ! array_key_exists( $k, $temp2 ) ) { 
  83. if (isset($sortlists['Disabled'])) { 
  84. $sortlists['Disabled'][ $k ] = $v; 
  85. } else { 
  86. $sortlists['disabled'][ $k ] = $v; 
  87.  
  88. // now check if saved blocks has blocks not registered under default blocks 
  89. foreach ( $sortlists as $key => $sortlist ) { 
  90. // key = enabled, disabled, backup 
  91. // sortlist = id => name 
  92.  
  93. foreach ( $sortlist as $k => $v ) { 
  94. // k = id 
  95. // v = name 
  96. if ( ! array_key_exists( $k, $temp ) ) { 
  97. unset( $sortlist[ $k ] ); 
  98. $sortlists[ $key ] = $sortlist; 
  99.  
  100. // assuming all sync'ed, now get the correct naming for each block 
  101. foreach ( $sortlists as $key => $sortlist ) { 
  102. foreach ( $sortlist as $k => $v ) { 
  103. $sortlist[ $k ] = $temp[ $k ]; 
  104. $sortlists[ $key ] = $sortlist; 
  105.  
  106. if ( $sortlists ) { 
  107. echo '<fieldset id="' . esc_attr($this->field['id']) . '" class="redux-sorter-container redux-sorter">'; 
  108.  
  109. foreach ( $sortlists as $group => $sortlist ) { 
  110. $filled = ""; 
  111.  
  112. if ( isset( $this->field['limits'][ $group ] ) && count( $sortlist ) >= $this->field['limits'][ $group ] ) { 
  113. $filled = " filled"; 
  114.  
  115. echo '<ul id="' . esc_attr($this->field['id'] . '_' . $group) . '" class="sortlist_' . esc_attr($this->field['id'] . $filled) . '" data-id="' . esc_attr($this->field['id']) . '" data-group-id="' . esc_attr($group) . '">'; 
  116. echo '<h3>' . esc_html($group) . '</h3>'; 
  117.  
  118. if ( ! isset( $sortlist['placebo'] ) ) { 
  119. array_unshift( $sortlist, array( "placebo" => "placebo" ) ); 
  120.  
  121. foreach ( $sortlist as $key => $list ) { 
  122.  
  123. echo '<input class="sorter-placebo" type="hidden" name="' . esc_attr($this->field['name']) . '[' . $group . '][placebo]' . esc_attr($this->field['name_suffix']) . '" value="placebo">'; 
  124.  
  125. if ( $key != "placebo" ) { 
  126.  
  127. //echo '<li id="' . $key . '" class="sortee">'; 
  128. echo '<li id="sortee-' . esc_attr($key) . '" class="sortee" data-id="' . esc_attr($key) . '">'; 
  129. echo '<input class="position ' . esc_attr($this->field['class']) . '" type="hidden" name="' . esc_attr($this->field['name'] . '[' . $group . '][' . $key . ']' . $this->field['name_suffix']) . '" value="' . esc_attr($list) . '">'; 
  130. echo esc_html($list); 
  131. echo '</li>'; 
  132.  
  133. echo '</ul>'; 
  134. echo '</fieldset>'; 
  135.  
  136. function enqueue() { 
  137. if ( $this->parent->args['dev_mode'] ) { 
  138. wp_enqueue_style( 
  139. 'redux-field-sorder-css',  
  140. ReduxFramework::$_url . 'inc/fields/sorter/field_sorter.css',  
  141. array(),  
  142. time(),  
  143. 'all' 
  144. ); 
  145.  
  146. wp_enqueue_script( 
  147. 'redux-field-sorter-js',  
  148. ReduxFramework::$_url . 'inc/fields/sorter/field_sorter' . Redux_Functions::isMin() . '.js',  
  149. array( 'jquery', 'redux-js', 'jquery-ui-sortable' ),  
  150. time(),  
  151. true 
  152. ); 
  153.  
  154. /** 
  155. * Functions to pass data from the PHP to the JS at render time. 
  156. * @return array Params to be saved as a javascript object accessable to the UI. 
  157. * @since Redux_Framework 3.1.5 
  158. */ 
  159. function localize( $field, $value = "" ) { 
  160.  
  161. $params = array(); 
  162.  
  163. if ( isset( $field['limits'] ) && ! empty( $field['limits'] ) ) { 
  164. $params['limits'] = $field['limits']; 
  165.  
  166. if ( empty( $value ) ) { 
  167. $value = $this->value; 
  168. $params['val'] = $value; 
  169.  
  170. return $params;