* @link http://wp-dreams.com, http://codecanyon.net/user/anago/portfolio * @copyright Copyright (c) 2016, Ernest Marcinko */ class wd_DraggableFields extends wpdreamsType { private $e_data; private $args = array( "show_checkboxes" => 0, // Checkboxes for default states "show_display_mode" => 0, // Display mode option "show_labels" => 0, // Display inputs to edit the labels "show_required" => 0, /** * It's more logical to add the fields via arguments, because it can be extended - as opposing to * options, which are stored permanently after saving. */ 'fields' => array( /*'field1' => 'Field label 1', 'field2' => 'Field label 2'*/ ), 'checked' => array() // true|array() Which ones are checked by default on drag? ); private $default_options = array( 'display_mode' => 'checkboxes', // checkboxes, dropdown, radio 'selected' => array('exact', 'title', 'content', 'excerpt', 'comments'), 'required' => false, 'invalid_input_text' => 'Please select an option!', 'unselected' => array(), /** * This only contains the items from the selected array, for the front-end, * because of the labels - as the $args is not available on the front-end, * and somehow we need to display the labels, do we? * (this is calculated every time, but a default value is needed) */ 'labels' => array( 'field1' => 'Field label 1', 'field2' => 'Field label 2' ), 'checked' => array('exact', 'title', 'content', 'excerpt', 'comments') ); public function getType() { parent::getType(); $this->processData(); ?>
label; ?> args['show_display_mode']): ?>
args['show_required']): ?>

 

    printAvailableFields(); ?>

    printSelectedFields(); ?>
args['fields'] as $k=>$field) { $checkbox = ''; if ($this->args['show_checkboxes'] == 1) { if ( $this->args['checked'] === true || in_array($k, $this->args['checked']) ) $_c = ' checked="checked"'; else $_c = ''; $checkbox = ''; } if ($this->args['show_labels'] == 1) $input = ''; else $input = $this->args['fields'][$k]; if ( in_array($k, $this->e_data['selected']) ) $disabled = ' ui-state-disabled'; else $disabled = ''; echo '
  • '.$this->args['fields'][$k].' '.$input.$checkbox.'
  • '; } } public function printSelectedFields() { foreach ($this->e_data['selected'] as $field) { $checkbox = ''; if ($this->args['show_labels'] == 1) $input = ''; else $input = $this->args['fields'][$field]; if ($this->args['show_checkboxes'] == 1) $checkbox = ''; echo '
  • '.$input.$checkbox.'
  • '; } } public function processData() { // Get the args first if exists if ( is_array($this->data) && isset($this->data['args']) ) $this->args = array_merge($this->args, $this->data['args']); if ( is_array($this->data) && isset($this->data['value']) ) { // If called from back-end non-post context $this->e_data = $this->decode_param($this->data['value']); $this->data = $this->encode_param($this->data['value']); } else { // POST method or something else $this->e_data = $this->decode_param($this->data); $this->data = $this->encode_param($this->data); } // All keys are strings in array, merge it with defaults to override $this->e_data = array_merge($this->default_options, $this->e_data); /** * At this point the this->data variable surely contains the encoded data, no matter what. */ } public final function getData() { return $this->data; } public final function getSelected() { return $this->e_data; } } }