/** * 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)); }
/** * Render a meta box * @param object $taxonomy_key * @param array $term_config * @param bool $return Return the output? If false, echoes the output */ public function renderMetaBox($taxonomy_key, $term_config, $return = false) { $config =& $term_config['args']; if (!Arr::iterable($config['fields'])) { return false; } $extra = get_option($this->getOptionID()); $out = array(); $out[] = '<table class="form-table">'; foreach ($config['fields'] as $name => $field) { // Hack to determine if we're on the category admin page $is_quick_add_page = !preg_match('/tag_ID/', $_SERVER['REQUEST_URI']); if ($is_quick_add_page && is_array($field) && array_key_exists('value', $field)) { $field['value'] = $field['value']; } elseif (is_array($extra) && array_key_exists($name, $extra)) { $field['value'] = $extra[$name]; } else { $field['value'] = null; } // Remove the escaped double quotes that WordPress fails to remove // when it unserializes data stored in the options table. // This bug was identified when links were added to a WYSIWYG field // and the anchor tags were rendered like <a href=\"foo.com\">... $field['value'] = str_replace('\\"', '"', $field['value']); $default = null; if (array_key_exists('default', $field)) { if ($field['type'] === 'select') { $default = $field['options'][$field['default']]; } elseif ($field['type'] === 'image') { $default = '<br>' . Html::image($field['default'], null, array('style' => 'max-width:100px;')); } else { $default = nl2br($field['default']); } } $tr_class = $name . ' form-field'; if ($field['type'] === 'hidden') { $tr_class .= ' hidden'; } $out[] = sprintf('<tr%s><td><label for="%s">%s%s</label></td><td>%s%s</td><td>%s</td></tr>', Html::attribs(array('class' => $tr_class)), array_key_exists('id', $field) ? $field['id'] : $name, array_key_exists('label', $field) ? $field['label'] : Str::human($name), array_key_exists('required', $field) && $field['required'] ? ' <span class="required">*</span>' : '', $this->getRenderMetaBoxField($name, $field), array_key_exists('description', $field) ? Html::p($field['description'], array('class' => 'description')) : null, !is_null($default) ? sprintf('<p class="description">Default: %s</p>', $default) : null); } $out[] = '</table>'; $html = join("\n", $out); if ($return) { return $html; } echo $html; }
/** * Render a meta box * @param object $post * @param array $post_config * @param bool $return Return the output? If false, echoes the output */ public function renderMetaBox($post, $post_config, $return = false) { $config = $this->getMetaBoxConfig($post_config['args']); if (!Arr::iterable($config['fields'])) { return false; } $this->load($post); $out = array(); $out[] = '<table>'; foreach ($config['fields'] as $name => $field) { // Hack to know if we're editing an existing post $is_existing_post = is_array($_SERVER) && array_key_exists('REQUEST_URI', $_SERVER) && preg_match('/post=([\\d]{1,})/', $_SERVER['REQUEST_URI']); $field['type'] = array_key_exists('type', $field) ? $field['type'] : 'text'; if (array_key_exists($name, $this->_info)) { $field['value'] = $this->_info[$name]; } elseif ($is_existing_post && $field['type'] !== 'html') { $field['value'] = null; } $default = null; if (array_key_exists('default', $field)) { if ($field['type'] === 'select') { $default = $field['options'][$field['default']]; } elseif ($field['type'] === 'image') { $default = '<br>' . Html::image($field['default'], null, array('style' => 'max-width:100px;')); } else { $default = nl2br($field['default']); } } $tr_class = $name; if ($field['type'] === 'hidden') { $tr_class .= ' hidden'; } $out[] = sprintf('<tr%s><td>%s</td><td>%s%s%s</td></tr>', Html::attribs(array('class' => $tr_class)), $this->getRenderLabel($name), $this->getRenderMetaBoxField($name, $field), array_key_exists('description', $field) ? Html::p($field['description'], array('class' => 'description')) : null, !is_null($default) ? sprintf('<p class="description">Default: %s</p>', $default) : null); } $out[] = '</table>'; $html = join("\n", $out); if ($return) { return $html; } echo $html; }