/** * Get field meta value * @param mixed $meta Meta value * @param array $field Field parameters * @return mixed */ public function field_meta($meta, $field) { if (empty($_GET['tag_ID'])) { return $meta; } $term_id = intval($_GET['tag_ID']); $single = $field['clone'] || !$field['multiple']; $meta = get_term_meta($term_id, $field['id'], $single); // Use $field['std'] only when the meta box hasn't been saved (i.e. the first time we run) $meta = !$this->is_saved() && '' === $meta || array() === $meta ? $field['std'] : $meta; // Escape attributes $meta = call_user_func(array(RW_Meta_Box::get_class_name($field), 'esc_meta'), $meta); // Make sure meta value is an array for clonable and multiple fields if ($field['clone'] || $field['multiple']) { if (empty($meta) || !is_array($meta)) { /** * Note: if field is clonable, $meta must be an array with values * so that the foreach loop in self::show() runs properly * @see self::show() */ $meta = $field['clone'] ? array('') : array(); } } return $meta; }
/** * Get meta value * * @param int $post_id * @param bool $saved * @param array $field * * @return mixed */ static function meta($post_id, $saved, $field) { /** * For special fields like 'divider', 'heading' which don't have ID, just return empty string * to prevent notice error when displayin fields */ if (empty($field['id'])) { return ''; } /** * Maybe set value from parent */ $post = get_post($post_id); if ($post && $post->post_parent > 0) { $property_inheritance = ud_get_wp_property('property_inheritance', array()); $type = get_post_meta($post_id, 'property_type', true); if (isset($property_inheritance[$type]) && in_array($field['id'], $property_inheritance[$type])) { $meta = get_post_meta($post->post_parent, $field['id'], !$field['multiple']); } } if (!$meta) { $meta = get_post_meta($post_id, $field['id'], !$field['multiple']); } // Use $field['std'] only when the meta box hasn't been saved (i.e. the first time we run) $meta = !$saved && '' === $meta || array() === $meta ? $field['std'] : $meta; // Escape attributes $meta = call_user_func(array(RW_Meta_Box::get_class_name($field), 'esc_meta'), $meta); return $meta; }
/** * Show end HTML markup for fields * Do not show field description. Field description is shown before list of fields * * @param mixed $meta * @param array $field * * @return string */ static function end_html($meta, $field) { $button = $field['clone'] ? call_user_func(array(RW_Meta_Box::get_class_name($field), 'add_clone_button'), $field) : ''; // Closes the container $html = "{$button}</div>"; return $html; }
/** * Get field meta value * * @param mixed $meta Meta value * @param array $field Field parameters * * @return mixed */ public function field_meta($meta, $field) { if (!$this->is_edit_screen()) { return $meta; } $option_name = sanitize_text_field($this->page_args['option_name']); $data = get_option($option_name); if (empty($data)) { $data = array(); } if (!is_array($data)) { $data = (array) $data; } $meta = isset($data[$field['id']]) ? $data[$field['id']] : $field['std']; // Escape attributes $meta = call_user_func(array(RW_Meta_Box::get_class_name($field), 'esc_meta'), $meta); // Make sure meta value is an array for clonable and multiple fields if ($field['clone'] || $field['multiple']) { if (empty($meta) || !is_array($meta)) { /** * Note: if field is clonable, $meta must be an array with values * so that the foreach loop in self::show() runs properly * @see self::show() */ $meta = $field['clone'] ? array('') : array(); } } return $meta; }
/** * Get field HTML * * @param mixed $meta * @param array $field * * @return string */ static function html($meta, $field) { $attributes = call_user_func(array(RW_Meta_Box::get_class_name($field), 'get_attributes'), $field, $meta); return sprintf('<input %s> <a href="#" class="show-embed button">%s</a> <span class="spinner"></span> <div class="embed-code">%s</div>', self::render_attributes($attributes), $field['size'], __('Preview', 'meta-box'), $meta ? self::get_embed($meta) : ''); }
/** * Get field HTML * * @param mixed $meta * @param array $field * * @return string */ static function html($meta, $field) { $meta = (array) $meta; $meta = implode(',', $meta); $attributes = call_user_func(array(RW_Meta_Box::get_class_name($field), 'get_attributes'), $field, $meta); $html = sprintf('<input %s> <div class="rwmb-media-view" data-mime-type="%s" data-max-files="%s" data-force-delete="%s"></div>', self::render_attributes($attributes), $field['mime_type'], $field['max_file_uploads'], $field['force_delete'] ? 'true' : 'false'); return $html; }
/** * Walk options * * @param mixed $meta * @param array $field * @param mixed $options * @param mixed $db_fields * * @return string */ public static function walk($options, $db_fields, $meta, $field) { $attributes = call_user_func(array(RW_Meta_Box::get_class_name($field), 'get_attributes'), $field, $meta); $walker = new RWMB_Select_Walker($db_fields, $field, $meta); $output = sprintf('<select %s>', self::render_attributes($attributes)); $output .= $walker->walk($options, $field['flatten'] ? -1 : 0); $output .= '</select>'; $output .= self::get_select_all_html($field); return $output; }
/** * Get field HTML * * @param mixed $meta * @param array $field * * @return string */ static function html($meta, $field) { $field_class = RW_Meta_Box::get_class_name($field); $attributes = call_user_func(array($field_class, 'get_attributes'), $field, $meta); $html = sprintf('<select %s>', self::render_attributes($attributes)); $html .= self::options_html($field, $meta); $html .= '</select>'; $html .= self::get_select_all_html($field['multiple']); return $html; }
/** * @see Walker::start_el() * * @param string $output Passed by reference. Used to append additional content. * @param object $object Item data object. * @param int $depth Depth of item. * @param int $current_object_id Item ID. * @param array $args */ public function start_el(&$output, $object, $depth = 0, $args = array(), $current_object_id = 0) { $label = $this->db_fields['label']; $id = $this->db_fields['id']; $meta = $this->meta; $field = $this->field; $field_class = RW_Meta_Box::get_class_name($field); $attributes = call_user_func(array($field_class, 'get_attributes'), $field, $object->{$id}); $output .= sprintf('<li><label><input %s %s>%s</label>', RWMB_Field::render_attributes($attributes), checked(in_array($object->{$id}, $meta), 1, false), $object->{$label}); }
/** * Get field HTML * * @param mixed $meta * @param array $field * * @return string */ static function html($meta, $field) { $html = array(); $tpl = '<label><input %s %s> %s</label>'; $field_class = RW_Meta_Box::get_class_name($field); foreach ($field['options'] as $value => $label) { $attributes = call_user_func(array($field_class, 'get_attributes'), $field, $value); $html[] = sprintf($tpl, self::render_attributes($attributes), checked($value, $meta, false), $label); } return implode(' ', $html); }
/** * Walk options * * @param mixed $meta * @param array $field * @param mixed $options * @param mixed $db_fields * * @return string */ public static function walk($options, $db_fields, $meta, $field) { $attributes = call_user_func(array(RW_Meta_Box::get_class_name($field), 'get_attributes'), $field, $meta); $walker = new RWMB_Select_Walker($db_fields, $field, $meta); $output = sprintf('<select %s>', self::render_attributes($attributes)); if (false === $field['multiple']) { $output .= isset($field['placeholder']) ? "<option value=''>{$field['placeholder']}</option>" : '<option></option>'; } $output .= $walker->walk($options, $field['flatten'] ? -1 : 0); $output .= '</select>'; $output .= self::get_select_all_html($field); return $output; }
/** * Get field HTML * * @param mixed $meta * @param array $field * * @return string */ static function html($meta, $field) { // Get parent field and filter to child field meta value self::$meta = $meta; add_filter('rwmb_field_meta', array(__CLASS__, 'child_field_meta'), 10, 3); ob_start(); foreach ($field['fields'] as $child_field) { $child_field['field_name'] = self::child_field_name($field['field_name'], $child_field['field_name']); call_user_func(array(RW_Meta_Box::get_class_name($child_field), 'show'), $child_field, RWMB_Group::$saved); } // Remove filter to child field meta value and reset class's parent field's meta remove_filter('rwmb_field_meta', array(__CLASS__, 'child_field_meta')); self::$meta = null; return ob_get_clean(); }
function display_level($options, $parent_id = 0, $active = false) { $id = $this->db_fields['id']; $field = $this->field; $meta = $this->meta; $walker = new RWMB_Select_Walker($this->db_fields, $this->field, $this->meta); $field_class = RW_Meta_Box::get_class_name($field); $attributes = call_user_func(array($field_class, 'get_attributes'), $field, $meta); $children = $options[$parent_id]; $output = sprintf('<div class="rwmb-select-tree %s" data-parent-id="%s"><select %s>', $active ? '' : 'hidden', $parent_id, RWMB_Field::render_attributes($attributes)); $output .= isset($field['placeholder']) ? "<option value=''>{$field['placeholder']}</option>" : '<option></option>'; $output .= $walker->walk($children, -1); $output .= '</select>'; foreach ($children as $c) { if (isset($options[$c->{$id}])) { $output .= $this->display_level($options, $c->{$id}, in_array($c->{$id}, $meta) && $active); } } $output .= '</div>'; return $output; }
/** * Display the value of a field * * @param string $field_id Field ID. Required. * @param array $args Additional arguments. Rarely used. See specific fields for details * @param int|null $post_id Post ID. null for current post. Optional. * @param bool $echo Display field meta value? Default `true` which works in almost all cases. We use `false` for the [rwmb_meta] shortcode * * @return string */ function rwmb_the_field($field_id, $args = array(), $post_id = null, $echo = true) { // Find field $field = RWMB_Helper::find_field($field_id); if (!$field) { return ''; } $output = call_user_func(array(RW_Meta_Box::get_class_name($field), 'the_value'), $field, $args, $post_id); /** * Allow developers to change the returned value of field * * @param mixed $value Field HTML output * @param array $field Field parameter * @param array $args Additional arguments. Rarely used. See specific fields for details * @param int|null $post_id Post ID. null for current post. Optional. */ $output = apply_filters('rwmb_the_field', $output, $field, $args, $post_id); if ($echo) { echo $output; } return $output; }
/** * Get post meta * * @param string $key Meta key. Required. * @param int|null $post_id Post ID. null for current post. Optional * @param array $args Array of arguments. Optional. * * @return mixed */ public static function meta($key, $args = array(), $post_id = null) { $post_id = empty($post_id) ? get_the_ID() : $post_id; $args = wp_parse_args($args, array('type' => 'text', 'multiple' => false, 'clone' => false)); // Always set 'multiple' true for following field types if (in_array($args['type'], array('checkbox_list', 'autocomplete', 'file', 'file_advanced', 'image', 'image_advanced', 'plupload_image', 'thickbox_image'))) { $args['multiple'] = true; } $field = array('id' => $key, 'type' => $args['type'], 'clone' => $args['clone'], 'multiple' => $args['multiple']); $class = RW_Meta_Box::get_class_name($field); switch ($args['type']) { case 'taxonomy_advanced': if (empty($args['taxonomy'])) { break; } $meta = get_post_meta($post_id, $key, !$args['multiple']); $term_ids = wp_parse_id_list($meta); // Allow to pass more arguments to "get_terms" $func_args = wp_parse_args(array('include' => $term_ids, 'hide_empty' => false), $args); unset($func_args['type'], $func_args['taxonomy'], $func_args['multiple']); $meta = get_terms($args['taxonomy'], $func_args); break; case 'taxonomy': $meta = empty($args['taxonomy']) ? array() : get_the_terms($post_id, $args['taxonomy']); break; case 'map': $field = array('id' => $key, 'multiple' => false, 'clone' => false); $meta = RWMB_Map_Field::the_value($field, $args, $post_id); break; case 'oembed': $meta = RWMB_OEmbed_Field::the_value($field, $args, $post_id); break; default: $meta = call_user_func(array($class, 'get_value'), $field, $args, $post_id); break; } return apply_filters('rwmb_meta', $meta, $key, $args, $post_id); }
/** * Output the field value * Display unordered list of images with option for size and link to full size * * @param array $field Field parameters * @param array $args Additional arguments. Not used for these fields. * @param int|null $post_id Post ID. null for current post. Optional. * * @return mixed Field value */ static function the_value($field, $args = array(), $post_id = null) { $value = self::get_value($field, $args, $post_id); if (!$value) { return ''; } $function = array(RW_Meta_Box::get_class_name($field), 'get_option_label'); if ($field['clone']) { $output = '<ul>'; if ($field['multiple']) { foreach ($value as $subvalue) { $output .= '<li><ul>'; foreach ($subvalue as &$option) { $output .= sprintf('<li><img src="%s"></li>', esc_url($field['options'][$value])); } $output .= '</ul></li>'; } } else { foreach ($value as &$subvalue) { $output .= sprintf('<li><img src="%s"></li>', esc_url($field['options'][$subvalue])); } } $output .= '</ul>'; } else { if ($field['multiple']) { $output = '<ul>'; foreach ($value as &$subvalue) { $output .= sprintf('<li><img src="%s"></li>', esc_url($field['options'][$subvalue])); } $output .= '</ul>'; } else { $output = sprintf('<img src="%s">', esc_url($field['options'][$value])); } } return $output; }
/** * Output the field value * Display unordered list of option labels, not option values * * @param array $field Field parameters * @param array $args Additional arguments. Not used for these fields. * @param int|null $post_id Post ID. null for current post. Optional. * * @return string Link(s) to post */ static function the_value($field, $args = array(), $post_id = null) { $class = RW_Meta_Box::get_class_name($field); $value = call_user_func(array($class, 'get_value'), $field, $args, $post_id); if (!$value || is_wp_error($value)) { return ''; } $function = array($class, 'get_option_label'); if ($field['clone']) { $output = '<ul>'; if ($field['multiple']) { foreach ($value as $subvalue) { $output .= '<li>'; array_walk_recursive($subvalue, $function, $field); $output .= '<ul><li>' . implode('</li><li>', $subvalue) . '</li></ul>'; $output .= '</li>'; } } else { array_walk_recursive($value, $function, $field); $output = '<li>' . implode('</li><li>', $value) . '</li>'; } $output .= '</ul>'; } else { if ($field['multiple']) { array_walk_recursive($value, $function, $field); $output = '<ul><li>' . implode('</li><li>', $value) . '</li></ul>'; } else { call_user_func_array($function, array(&$value, 0, $field)); $output = $value; } } return $output; }
/** * Get option label * * @param string $value Option value * @param array $field Field parameter * * @return string */ public static function get_option_label($value, $field) { $options = call_user_func(array(RW_Meta_Box::get_class_name($field), 'get_options'), $field); return $options[$value]->label; }
/** * Get value of custom field. * This is used to replace old version of rwmb_meta key. rwmb_meta will be used internally only. * * @uses rwmb_meta() * @param string $key Meta key. Required. * @param int|null $post_id Post ID. null for current post. Optional. * @return mixed false if field doesn't exist. Field value otherwise. */ function rwmb_get_field($key, $post_id = null) { /** * Search all the registered meta box to find needed field * The field will have all needed parameters which we can pass to rwmb_meta function without * having users to manually set them (field type, multiple, ect.). So users only need to remember * field ID only. */ $found = false; $meta_boxes = apply_filters('rwmb_meta_boxes', array()); foreach ($meta_boxes as $meta_box) { foreach ($meta_box['fields'] as $field) { if ($key == $field['id']) { $found = true; break; } } } // If field doesn't exist, return false if (!$found) { return false; } // Normalize field to make sure all params are set properly $field = wp_parse_args($field, array('id' => '', 'multiple' => false, 'clone' => false, 'std' => '', 'desc' => '', 'format' => '', 'before' => '', 'after' => '', 'field_name' => isset($field['id']) ? $field['id'] : '', 'required' => false, 'placeholder' => '')); $field = call_user_func(array(RW_Meta_Box::get_class_name($field), 'normalize_field'), $field); // Get field value return RWMB_Helper::meta($key, $field, $post_id); }
/** * Output the field value * Depends on field value and field types, each field can extend this method to output its value in its own way * See specific field classes for details. * * Note: we don't echo the field value directly. We return the output HTML of field, which will be used in * rwmb_the_field function later. * * @use self::get_value() * @see rwmb_the_field() * * @param array $field Field parameters * @param array $args Additional arguments. Rarely used. See specific fields for details * @param int|null $post_id Post ID. null for current post. Optional. * * @return string HTML output of the field */ static function the_value($field, $args = array(), $post_id = null) { $value = call_user_func(array(RW_Meta_Box::get_class_name($field), 'get_value'), $field, $args, $post_id); $output = $value; if (is_array($value)) { $output = '<ul>'; foreach ($value as $subvalue) { $output .= '<li>' . $subvalue . '</li>'; } $output .= '</ul>'; } return $output; }
/** * Get field HTML * * @param mixed $meta * @param array $field * * @return string */ static function html($meta, $field) { $attributes = call_user_func(array(RW_Meta_Box::get_class_name($field), 'get_attributes'), $field, 1); return sprintf('<input %s %s>', self::render_attributes($attributes), checked(!empty($meta), 1, false)); }
/** * Show field HTML * * @param array $field * @param bool $saved * * @return string */ static function show($field, $saved) { global $post; $field_class = RW_Meta_Box::get_class_name($field); $meta = call_user_func(array($field_class, 'meta'), $post->ID, $saved, $field); $group = ''; // Empty the clone-group field $type = $field['type']; $id = $field['id']; $begin = call_user_func(array($field_class, 'begin_html'), $meta, $field); // Apply filter to field begin HTML // 1st filter applies to all fields // 2nd filter applies to all fields with the same type // 3rd filter applies to current field only $begin = apply_filters('rwmb_begin_html', $begin, $field, $meta); $begin = apply_filters("rwmb_{$type}_begin_html", $begin, $field, $meta); $begin = apply_filters("rwmb_{$id}_begin_html", $begin, $field, $meta); // Separate code for cloneable and non-cloneable fields to make easy to maintain // Cloneable fields if ($field['clone']) { if (isset($field['clone-group'])) { $group = " clone-group='{$field['clone-group']}'"; } $meta = (array) $meta; $field_html = ''; foreach ($meta as $index => $sub_meta) { $sub_field = $field; $sub_field['field_name'] = $field['field_name'] . "[{$index}]"; if ($field['multiple']) { $sub_field['field_name'] .= '[]'; } // Wrap field HTML in a div with class="rwmb-clone" if needed $input_html = '<div class="rwmb-clone">'; // Call separated methods for displaying each type of field $input_html .= call_user_func(array($field_class, 'html'), $sub_meta, $sub_field); // Apply filter to field HTML // 1st filter applies to all fields with the same type // 2nd filter applies to current field only $input_html = apply_filters("rwmb_{$type}_html", $input_html, $field, $sub_meta); $input_html = apply_filters("rwmb_{$id}_html", $input_html, $field, $sub_meta); // Add clone button $input_html .= self::clone_button(); $input_html .= '</div>'; $field_html .= $input_html; } } else { // Call separated methods for displaying each type of field $field_html = call_user_func(array($field_class, 'html'), $meta, $field); // Apply filter to field HTML // 1st filter applies to all fields with the same type // 2nd filter applies to current field only $field_html = apply_filters("rwmb_{$type}_html", $field_html, $field, $meta); $field_html = apply_filters("rwmb_{$id}_html", $field_html, $field, $meta); } $end = call_user_func(array($field_class, 'end_html'), $meta, $field); // Apply filter to field end HTML // 1st filter applies to all fields // 2nd filter applies to all fields with the same type // 3rd filter applies to current field only $end = apply_filters('rwmb_end_html', $end, $field, $meta); $end = apply_filters("rwmb_{$type}_end_html", $end, $field, $meta); $end = apply_filters("rwmb_{$id}_end_html", $end, $field, $meta); // Apply filter to field wrapper // This allow users to change whole HTML markup of the field wrapper (i.e. table row) // 1st filter applies to all fields with the same type // 2nd filter applies to current field only $html = apply_filters("rwmb_{$type}_wrapper_html", "{$begin}{$field_html}{$end}", $field, $meta); $html = apply_filters("rwmb_{$id}_wrapper_html", $html, $field, $meta); // Display label and input in DIV and allow user-defined classes to be appended $classes = array('rwmb-field', "rwmb-{$type}-wrapper"); if ('hidden' === $field['type']) { $classes[] = 'hidden'; } if (!empty($field['required'])) { $classes[] = 'required'; } if (!empty($field['class'])) { $classes[] = $field['class']; } printf($field['before'] . '<div class="%s"%s>%s</div>' . $field['after'], implode(' ', $classes), $group, $html); }
/** * Show end HTML markup for fields * * @param mixed $meta * @param array $field * * @return string */ static function end_html($meta, $field) { $button = $field['clone'] ? call_user_func(array(RW_Meta_Box::get_class_name($field), 'add_clone_button')) : ''; $desc = $field['desc'] ? "<p id='{$field['id']}_description' class='description'>{$field['desc']}</p>" : ''; // Closes the container $html = "{$button}{$desc}</div>"; return $html; }
/** * Get field HTML * * @param mixed $meta * @param array $field * * @return string */ static function html($meta, $field) { $attributes = call_user_func(array(RW_Meta_Box::get_class_name($field), 'get_attributes'), $field, $meta); return sprintf('<input %s>%s', self::render_attributes($attributes), self::datalist_html($field)); }
/** * Get meta value * * @param int $post_id * @param bool $saved * @param array $field * * @return mixed */ public static function meta($post_id, $saved, $field) { $meta = parent::meta($post_id, $saved, $field); if (!$field['timestamp']) { return $meta; } $method = array(RW_Meta_Box::get_class_name($field), 'translate_format'); if (is_array($meta)) { foreach ($meta as $key => $value) { $meta[$key] = array('timestamp' => $value != "" ? $value : null, 'formatted' => $value != "" ? date(call_user_func($method, $field), intval($value)) : ""); } } else { $meta = array('timestamp' => $meta != "" ? $meta : null, 'formatted' => $meta != "" ? date(call_user_func($method, $field), intval($meta)) : ""); } return $meta; }
/** * Get the field value * The difference between this function and 'meta' function is 'meta' function always returns the escaped value * of the field saved in the database, while this function returns more meaningful value of the field * * @param array $field Field parameters * @param array $args Not used for this field * @param int|null $post_id Post ID. null for current post. Optional. * * @return mixed Full info of uploaded files */ static function get_value($field, $args = array(), $post_id = null) { if (!$post_id) { $post_id = get_the_ID(); } /** * Get raw meta value in the database, no escape * Very similar to self::meta() function */ $file_ids = get_post_meta($post_id, $field['id'], false); // For each file, get full file info $value = array(); foreach ($file_ids as $file_id) { if ($file_info = call_user_func(array(RW_Meta_Box::get_class_name($field), 'file_info'), $file_id, $args)) { $value[$file_id] = $file_info; } } return $value; }
/** * Get field HTML * * @param mixed $meta * @param array $field * * @return string */ static function html($meta, $field) { $attributes = call_user_func(array(RW_Meta_Box::get_class_name($field), 'get_attributes'), $field, null); return sprintf('<a href="#" %s>%s</a>', self::render_attributes($attributes), $field['std']); }
/** * Find field by field ID * This function finds field in meta boxes registered by 'rwmb_meta_boxes' filter * Note: if users use old code to add meta boxes, this function might not work properly * @param string $id Field ID * @return array|false Field params (array) if success. False otherwise. */ static function find_field($id) { $found = false; // Get all meta boxes registered with 'rwmb_meta_boxes' hook $meta_boxes = apply_filters('rwmb_meta_boxes', array()); // Find field foreach ($meta_boxes as $meta_box) { foreach ($meta_box['fields'] as $field) { if ($key == $field['id']) { $found = true; break; } } } // If field doesn't exist, return false if (!$found) { return false; } // Normalize field to make sure all params are set properly $field = wp_parse_args($field, array('id' => '', 'multiple' => false, 'clone' => false, 'std' => '', 'desc' => '', 'format' => '', 'before' => '', 'after' => '', 'field_name' => isset($field['id']) ? $field['id'] : '', 'required' => false, 'placeholder' => '')); $field = call_user_func(array(RW_Meta_Box::get_class_name($field), 'normalize_field'), $field); return $field; }
/** * Render select_tree * * @param $options * @param $meta * @param $field * * @return array */ static function render_select_tree($options, $meta, $field) { $field_class = RW_Meta_Box::get_class_name($field); $db_fields = call_user_func(array($field_class, 'get_db_fields'), $field); $walker = new RWMB_Select_Tree_Walker($db_fields, $field, $meta); $output = $walker->walk($options); return $output; }
/** * Get field HTML * * @param mixed $meta * @param array $field * * @return string */ static function html($meta, $field) { $field_class = RW_Meta_Box::get_class_name($field); $attributes = call_user_func(array($field_class, 'get_attributes'), $field, $meta); return sprintf('<textarea %s>%s</textarea>', self::render_attributes($attributes), $meta); }