Exemplo n.º 1
0
 /**
  * Get a rendered metabox field
  * @param string $name
  * @param array $field
  */
 public function getRenderMetaBoxField($name, $field = null)
 {
     if (is_null($field)) {
         $field = $this->getField($name);
     }
     $type = $field['type'];
     // Arbitrary HTML is allowed if you specify type=html and value=<yourhtml>
     if ($type === 'html') {
         return $field['value'];
     }
     if (!array_key_exists('value', $field)) {
         $field['value'] = null;
     }
     if (!array_key_exists('name', $field)) {
         $field['name'] = $name;
     }
     if (!$this->isRequired($field)) {
         unset($field['required']);
     }
     if (array_key_exists('required', $field)) {
         $field['required'] = 'required';
     }
     if (!array_key_exists('id', $field)) {
         $field['id'] = $name;
     }
     if (in_array($type, array('image', 'file'))) {
         $htmls = array();
         $field['class'] = array_key_exists('class', $field) ? $field['class'] . ' upload' : 'upload';
         $field['placeholder'] = array_key_exists('placeholder', $field) ? $field['placeholder'] : 'URL';
         $htmls[] = '<div class="upload_field">';
         $htmls[] = '<div class="upload-field-container">';
         $htmls[] = Html::tag('input', null, array_merge(self::scrubAttributes($field), array('type' => 'text')));
         $htmls[] = Html::tag('input', null, array('type' => 'button', 'value' => 'Select file', 'class' => 'browse'));
         $htmls[] = Html::tag('input', null, array('type' => 'button', 'value' => 'Clear', 'class' => 'clear'));
         $htmls[] = '</div>';
         $htmls[] = '</div>';
         return join("\n", $htmls);
     }
     if ($type === 'textarea') {
         $is_wysiwyg = array_key_exists('class', $field) && in_array('wysiwyg', explode(' ', $field['class']));
         if ($is_wysiwyg) {
             $settings = array_key_exists('settings', $field) ? $field['settings'] : array();
             ob_start();
             wp_editor($field['value'], $name, $settings);
             return ob_get_clean();
         }
         unset($field['type']);
         return sprintf('<textarea%s>%s</textarea>', Html::attribs(self::scrubAttributes($field)), $field['value']);
     }
     if (in_array($type, array('checkbox', 'radio'))) {
         if (in_array($field['value'], array(1, 'on'))) {
             // if value starts at 1, then it's checked
             $field['checked'] = 'checked';
         }
         $field['value'] = 1;
         // value attrib should be 1 so that $_POST[$name]=1 (or doesn't exist)
         return Html::tag('input', null, self::scrubAttributes($field));
     }
     if ($type === 'select') {
         if (!array_key_exists('', $field['options'])) {
             // Straight up array_merge will blow away your numeric keys
             $options = array('' => 'Select');
             if (Arr::iterable($field['options'])) {
                 foreach ($field['options'] as $k => $v) {
                     $options[$k] = $v;
                 }
             }
             $field['options'] = $options;
         }
         return Html::selecty($field['options'], $field['value'], self::scrubAttributes($field));
     }
     // Default to text field
     $field['value'] = htmlentities($field['value']);
     if (!array_key_exists('type', $field)) {
         $field['type'] = 'text';
     }
     // Render remaining fields with types normally assigned by type attrib (text, email, search, password)
     return Html::tag('input', null, self::scrubAttributes($field));
 }