/** * 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 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; }
/** * Upload * Ajax callback function * * @return string Error or (XML-)response */ static function handle_upload() { global $wpdb; $post_id = is_numeric($_REQUEST['post_id']) ? $_REQUEST['post_id'] : 0; $field_id = isset($_REQUEST['field_id']) ? $_REQUEST['field_id'] : ''; check_ajax_referer("rwmb-upload-images_{$field_id}"); // You can use WP's wp_handle_upload() function: $file = $_FILES['async-upload']; $file_attr = wp_handle_upload($file, array('test_form' => false)); //Get next menu_order $meta = get_post_meta($post_id, $field_id, false); if (empty($meta)) { $next = 0; } else { $meta = implode(',', (array) $meta); $max = $wpdb->get_var("\n\t\t\t\t\tSELECT MAX(menu_order) FROM {$wpdb->posts}\n\t\t\t\t\tWHERE post_type = 'attachment'\n\t\t\t\t\tAND ID in ({$meta})\n\t\t\t\t"); $next = is_numeric($max) ? (int) $max + 1 : 0; } $attachment = array('guid' => $file_attr['url'], 'post_mime_type' => $file_attr['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($file['name'])), 'post_content' => '', 'post_status' => 'inherit', 'menu_order' => $next); // Adds file as attachment to WordPress $id = wp_insert_attachment($attachment, $file_attr['file'], $post_id); if (!is_wp_error($id)) { wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file_attr['file'])); // Save file ID in meta field add_post_meta($post_id, $field_id, $id, false); RW_Meta_Box::ajax_response(self::img_html($id), 'success'); } exit; }
/** * 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; }
/** * 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; }
/** * Ajax callback for attaching media to field * * @return void */ static function wp_ajax_attach_media() { $post_id = is_numeric($_REQUEST['post_id']) ? $_REQUEST['post_id'] : 0; $field_id = isset($_POST['field_id']) ? $_POST['field_id'] : 0; $attachment_id = isset($_POST['attachment_id']) ? $_POST['attachment_id'] : 0; check_ajax_referer("rwmb-attach-media_{$field_id}"); add_post_meta($post_id, $field_id, $attachment_id, false); RW_Meta_Box::ajax_response(self::img_html($attachment_id), 'success'); }
/** * 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; }
function add_missed_values() { parent::add_missed_values(); // add 'multiple' option to taxonomy field with checkbox_list type foreach ($this->_meta_box['fields'] as $key => $field) { if ('taxonomy' == $field['type'] && 'checkbox_list' == $field['options']['type']) { $this->_meta_box['fields'][$key]['multiple'] = true; } } }
/** * @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) { $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; }
/** * 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); }
/** * Ajax callback for returning oEmbed HTML * * @return void */ static function wp_ajax_get_embed() { global $post; $url = isset($_POST['oembed_url']) ? $_POST['oembed_url'] : 0; $post_id = is_numeric($_REQUEST['post_id']) ? (int) $_REQUEST['post_id'] : 0; if (isset($_REQUEST['post_id'])) { $post = get_post($_REQUEST['post_id']); } $embed = self::get_embed($url); RW_Meta_Box::ajax_response($embed, 'success'); exit; }
/** * Save meta value * If field is cloneable, value is saved as a single entry in DB * Otherwise value is saved as multiple entries (for backward compatibility) * * TODO: A good way to ALWAYS save values in single entry in DB, while maintaining backward compatibility * * @param $new * @param $old * @param $post_id * @param $field */ static function save($new, $old, $post_id, $field) { if (!$field['clone']) { RW_Meta_Box::save($new, $old, $post_id, $field); return; } if (empty($new)) { delete_post_meta($post_id, $field['id']); } else { update_post_meta($post_id, $field['id'], $new); } }
/** * Find field by field ID. * This function finds field in meta boxes registered by 'rwmb_meta_boxes' filter. * * @param string $field_id Field ID * @return array|false Field params (array) if success. False otherwise. */ static function find_field($field_id) { $meta_boxes = RWMB_Core::get_meta_boxes(); foreach ($meta_boxes as $meta_box) { $meta_box = RW_Meta_Box::normalize($meta_box); foreach ($meta_box['fields'] as $field) { if ($field_id == $field['id']) { return $field; } } } return false; }
/** * 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; }
/** * Ajax callback for deleting files. * Modified from a function used by "Verve Meta Boxes" plugin * * @link http://goo.gl/LzYSq * @return void */ static function wp_ajax_delete_file() { $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; $field_id = isset($_POST['field_id']) ? $_POST['field_id'] : 0; $attachment_id = isset($_POST['attachment_id']) ? intval($_POST['attachment_id']) : 0; check_admin_referer("rwmb-delete-file_{$field_id}"); $ok = delete_post_meta($post_id, $field_id, $attachment_id); $ok = $ok && wp_delete_attachment($attachment_id); if ($ok) { RW_Meta_Box::ajax_response('', 'success'); } else { RW_Meta_Box::ajax_response(__('Error: Cannot delete file', 'bootstrap'), 'error'); } }
/** * 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(); }
/** * Find field by field ID * This function finds field in meta boxes registered by 'wcqd_metabox_meta_boxes' filter * Note: if users use old code to add meta boxes, this function might not work properly * * @param string $field_id Field ID * * @return array|false Field params (array) if success. False otherwise. */ static function find_field($field_id) { // Get all meta boxes registered with 'wcqd_metabox_meta_boxes' hook $meta_boxes = apply_filters('wcqd_metabox_meta_boxes', array()); // Find field foreach ($meta_boxes as $meta_box) { $meta_box = RW_Meta_Box::normalize($meta_box); foreach ($meta_box['fields'] as $field) { if ($field_id == $field['id']) { return $field; } } } return false; }
/** * Hash all fields into an indexed array for search * * @param string $post_type Post type */ public static function hash_fields($post_type) { self::$fields[$post_type] = array(); $meta_boxes = RWMB_Core::get_meta_boxes(); foreach ($meta_boxes as $meta_box) { $meta_box = RW_Meta_Box::normalize($meta_box); if (!in_array($post_type, $meta_box['post_types'])) { continue; } foreach ($meta_box['fields'] as $field) { if (!empty($field['id'])) { self::$fields[$post_type][$field['id']] = $field; } } } }
static function wp_ajax_save_map() { $post_id = isset($_POST['post_id']) ? $_POST['post_id'] : 0; $map_address = isset($_POST['map_address']) ? $_POST['map_address'] : ''; $map_location = isset($_POST['map_location']) ? $_POST['map_location'] : ''; if ($post_id) { if (!empty($map_location)) { update_post_meta($post_id, '_map_geo', $map_location); update_post_meta($post_id, '_map_location', $map_location); } if (!empty($map_address)) { update_post_meta($post_id, '_map_address', $map_address); } RW_Meta_Box::ajax_response(__('Map Address saved', 'wpsight'), 'success'); } else { RW_Meta_Box::ajax_response(__('Map Address saving fail', 'wpsight'), 'error'); } }
/** * Ajax callback for reordering images * * @return void */ static function wp_ajax_reorder_images() { $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; $field_id = isset($_POST['field_id']) ? $_POST['field_id'] : 0; $order = isset($_POST['order']) ? $_POST['order'] : 0; check_admin_referer("rwmb-reorder-images_{$field_id}"); parse_str($order, $items); $items = $items['item']; $order = 1; // Delete old meta values delete_post_meta($post_id, $field_id); foreach ($items as $item) { wp_update_post(array('ID' => $item, 'post_parent' => $post_id, 'menu_order' => $order++)); // Save images in that order to meta field // That helps retrieving values easier add_post_meta($post_id, $field_id, $item, false); } RW_Meta_Box::ajax_response(__('Order saved', 'rwmb'), 'success'); }
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; }
/** * Upload * Ajax callback function * * @return string Error or (XML-)response */ static function handle_upload() { $post_id = is_numeric($_REQUEST['post_id']) ? $_REQUEST['post_id'] : 0; $field_id = isset($_REQUEST['field_id']) ? $_REQUEST['field_id'] : ''; check_admin_referer("rwmb-upload-images_{$field_id}"); // You can use WP's wp_handle_upload() function: $file = $_FILES['async-upload']; $file_attr = wp_handle_upload($file, array('test_form' => false)); $attachment = array('guid' => $file_attr['url'], 'post_mime_type' => $file_attr['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($file['name'])), 'post_content' => '', 'post_status' => 'inherit'); // Adds file as attachment to WordPress $id = wp_insert_attachment($attachment, $file_attr['file'], $post_id); if (!is_wp_error($id)) { wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file_attr['file'])); // Save file ID in meta field add_post_meta($post_id, $field_id, $id, false); // Fake field array. We need ID and force_delete only $field = array('id' => $field_id, 'force_delete' => isset($_REQUEST['force_delete']) ? intval($_REQUEST['force_delete']) : 0); RW_Meta_Box::ajax_response(self::img_html($id, $field), 'success'); } exit; }
/** * 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; }