/**
  * Sanitize callback
  *
  * @param mixed $newValue
  * @param mixed $oldValue
  * @return mixed
  */
 public function sanitize($newValue, $oldValue)
 {
     if (isset($this->options['sanitize']) && is_callable($this->options['sanitize'])) {
         $widgetOptions = $this->getWidget()->getOptions();
         $sanitizeArguments = array($newValue, $oldValue, $this->getSlug()) + $widgetOptions;
         return callUserFunctionWithSafeArguments($this->options['sanitize'], $sanitizeArguments);
     } else {
         return $newValue;
     }
 }
Пример #2
0
    /**
     * Display Dropdown callback: displays a harvesthq.github.io/chosen/ dropdown.
     * special arguments:
     *
     * - bool required
     * - bool multiple
     * - array | string value
     * - array items | callable itemsCallback
     * @param $args
     * @return mixed|string
     */
    public function displayDropdown($args)
    {
        $key = $args['post']->ID . '_' . $args['key'];
        $html = $this->getTemplate($args, $key);
        if (isset($args['name'])) {
            $name = $args['name'];
        } else {
            // Get the current value
            $name = $key;
        }
        $attr = '';
        if (isset($args['required'])) {
            $attr .= __checked_selected_helper($args['required'], true, false, 'required');
        }
        $singleValue = true;
        if (isset($args['multiple']) && $args['multiple']) {
            $attr .= ' multiple';
            $arrayNotation = '[]';
            if (substr($name, -strlen($arrayNotation)) !== $arrayNotation) {
                $name .= $arrayNotation;
            }
            $singleValue = false;
        }
        if (isset($args['value']) && (!$singleValue || is_string($args['value']))) {
            $value = $args['value'];
        } elseif (isset($args['value'][0]) && $singleValue) {
            $value = $args['value'][0];
        } else {
            // Get the current value
            $value = get_post_meta($args['post']->ID, $args['key'], $singleValue);
            if (!$singleValue) {
                $value = array_filter($value);
            }
        }
        $items = $args['items'];
        if (isset($args['itemsCallback']) && is_callable($args['itemsCallback'])) {
            $items = callUserFunctionWithSafeArguments($args['itemsCallback'], array($args));
        }
        if (isset($args['allow_single_deselect']) && $args['allow_single_deselect'] == true) {
            $items = array(0 => array('title' => '')) + $items;
        }
        $options = self::convertDropdownItemsToOptions($items, $value, $singleValue);
        $select = '<select name="' . $name . '" id="' . $key . '" ' . $attr . '>' . implode('', $options) . '</select>';
        $chosenArguments = array_merge(array('width' => '100%'), $args);
        $chosenKey = str_replace('-', '_', $key) . '_chosen';
        // only call the sortable method if it was requested
        $sortableCall = '';
        if ($chosenArguments['sortable'] != false) {
            $sortableCall = '.chosenSortable()';
        }
        // convert array to object recursively
        $chosenArgumentsObject = json_decode(json_encode($chosenArguments), FALSE);
        $chosenArgumentsJson = json_encode($chosenArgumentsObject);
        $select .= '
      <script>
        (function ($) {
          $(document).ready(function(){

            $("#' . $key . '").on("chosen:ready change", function(evt, params) {
              $("#' . $chosenKey . ' .search-choice").each(function(){

                // compare label to option text
                var label = $(this).text().trim();
                var options = $("#' . $key . '").find("option").filter(function(){
                  return $(this).text().trim() == label;
                });
                if(options.length > 0){
                  var option = options[0];
                  if($(option).data("url")){
                    if(!$(this).hasClass("has-link-action")){
                      $(".search-choice-close", this).before("<a class=\\"search-choice-link\\" href=\\"" + $(option).data("url") + "\\" ></a>");
                      $(this).addClass("has-link-action");
                    }
                  }
                  if($(option).data("image")){
                    if(!$(this).hasClass("has-image")){
                      $("span", this).before("<img class=\\"search-choice-image\\" src=\\"" + $(option).data("image") + "\\" />");
                      $(this).addClass("has-image");
                    }
                  }
                }
              });

              $("#' . $chosenKey . ' .search-choice-link").click(function(e){
                // chosen is registered on parent, stop the propagation, but don\'t prevent the default action (i.e. browswer link)
                e.stopPropagation();
              });
            });

            $("#' . $key . '").chosen(' . $chosenArgumentsJson . ')' . $sortableCall . ';
          });
        }(jQuery));
      </script>
    ';
        $html = str_replace('{input}', $select, $html);
        return $html;
    }