get_data( $handle, 'data' ) ) { wp_localize_script( $handle, $name, $data ); } } /** * Show field HTML * Filters are put inside this method, not inside methods such as "meta", "html", "begin_html", etc. * That ensures the returned value are always been applied filters * This method is not meant to be overwritten in specific fields * * @param array $field * @param bool $saved */ public static function show( $field, $saved ) { $post = get_post(); $post_id = isset( $post->ID ) ? $post->ID : 0; $meta = self::call( $field, 'meta', $post_id, $saved ); $meta = self::filter( 'field_meta', $meta, $field, $saved ); $begin = self::call( $field, 'begin_html', $meta ); $begin = self::filter( 'begin_html', $begin, $field, $meta ); // Separate code for cloneable and non-cloneable fields to make easy to maintain // Cloneable fields if ( $field['clone'] ) { $field_html = RWMB_Clone::html( $meta, $field ); } // End if(). else { // Call separated methods for displaying each type of field $field_html = self::call( $field, 'html', $meta ); $field_html = self::filter( 'html', $field_html, $field, $meta ); } $end = self::call( $field, 'end_html', $meta ); $end = self::filter( 'end_html', $end, $field, $meta ); $html = self::filter( 'wrapper_html', "$begin$field_html$end", $field, $meta ); // Display label and input in DIV and allow user-defined classes to be appended $classes = "rwmb-field rwmb-{$field['type']}-wrapper " . $field['class']; if ( 'hidden' === $field['type'] ) { $classes .= ' hidden'; } if ( ! empty( $field['required'] ) ) { $classes .= ' required'; } $outer_html = sprintf( $field['before'] . '
{$field['desc']}
" : ''; } /** * Get raw meta value * * @param int $post_id * @param array $field * * @return mixed */ public static function raw_meta( $post_id, $field ) { if ( empty( $field['id'] ) ) { return ''; } $single = $field['clone'] || ! $field['multiple']; return get_post_meta( $post_id, $field['id'], $single ); } /** * Get meta value * * @param int $post_id * @param bool $saved * @param array $field * * @return mixed */ public 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 displaying fields */ if ( empty( $field['id'] ) ) { return ''; } // Get raw meta $meta = self::call( $field, 'raw_meta', $post_id ); // Use $field['std'] only when the meta box hasn't been saved (i.e. the first time we run) $meta = ! $saved ? $field['std'] : $meta; // Escape attributes $meta = self::call( $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; } /** * Escape meta for field output * * @param mixed $meta * * @return mixed */ public static function esc_meta( $meta ) { return is_array( $meta ) ? array_map( __METHOD__, $meta ) : esc_attr( $meta ); } /** * Set value of meta before saving into database * * @param mixed $new * @param mixed $old * @param int $post_id * @param array $field * * @return int */ public static function value( $new, $old, $post_id, $field ) { return $new; } /** * Save meta value * * @param $new * @param $old * @param $post_id * @param $field */ public static function save( $new, $old, $post_id, $field ) { $name = $field['id']; // Remove post meta if it's empty if ( '' === $new || array() === $new ) { delete_post_meta( $post_id, $name ); return; } // If field is cloneable, value is saved as a single entry in the database if ( $field['clone'] ) { // Remove empty values $new = (array) $new; foreach ( $new as $k => $v ) { if ( '' === $v || array() === $v ) { unset( $new[ $k ] ); } } // Reset indexes $new = array_values( $new ); update_post_meta( $post_id, $name, $new ); return; } // If field is multiple, value is saved as multiple entries in the database (WordPress behaviour) if ( $field['multiple'] ) { $new_values = array_diff( $new, $old ); foreach ( $new_values as $new_value ) { add_post_meta( $post_id, $name, $new_value, false ); } $old_values = array_diff( $old, $new ); foreach ( $old_values as $old_value ) { delete_post_meta( $post_id, $name, $old_value ); } return; } // Default: just update post meta update_post_meta( $post_id, $name, $new ); } /** * Normalize parameters for field * * @param array $field * * @return array */ public static function normalize( $field ) { $field = wp_parse_args( $field, array( 'id' => '', 'name' => '', 'multiple' => false, 'std' => '', 'desc' => '', 'format' => '', 'before' => '', 'after' => '', 'field_name' => isset( $field['id'] ) ? $field['id'] : '', 'placeholder' => '', 'clone' => false, 'max_clone' => 0, 'sort_clone' => false, 'class' => '', 'disabled' => false, 'required' => false, 'attributes' => array(), ) ); return $field; } /** * Get the attributes for a field * * @param array $field * @param mixed $value * * @return array */ public static function get_attributes( $field, $value = null ) { $attributes = wp_parse_args( $field['attributes'], array( 'disabled' => $field['disabled'], 'required' => $field['required'], 'id' => $field['id'], 'class' => '', 'name' => $field['field_name'], ) ); $attributes['class'] = implode( ' ', array_merge( array( "rwmb-{$field['type']}" ), (array) $attributes['class'] ) ); return $attributes; } /** * Renders an attribute array into an html attributes string * * @param array $attributes * * @return string */ public static function render_attributes( $attributes ) { $output = ''; foreach ( $attributes as $key => $value ) { if ( false === $value || '' === $value ) { continue; } if ( is_array( $value ) ) { $value = json_encode( $value ); } $output .= sprintf( true === $value ? ' %s' : ' %s="%s"', $key, esc_attr( $value ) ); } return $output; } /** * 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, for ex.: * for file/image: return array of file/image information instead of file/image IDs * * Each field can extend this function and add more data to the returned value. * See specific field classes for details. * * @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 mixed Field value */ public static function get_value( $field, $args = array(), $post_id = null ) { // Some fields does not have ID like heading, custom HTML, etc. if ( empty( $field['id'] ) ) { return ''; } if ( ! $post_id ) { $post_id = get_the_ID(); } // Get raw meta value in the database, no escape $value = RWMB_Field::call( $field, 'raw_meta', $post_id ); // Make sure meta value is an array for cloneable and multiple fields if ( $field['clone'] || $field['multiple'] ) { $value = is_array( $value ) && $value ? $value : array(); } return $value; } /** * 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_value() * * @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 */ public static function the_value( $field, $args = array(), $post_id = null ) { $value = self::call( 'get_value', $field, $args, $post_id ); return self::call( 'format_value', $field, $value ); } /** * Format value for the helper functions. * * @param array $field Field parameter * @param string|array $value The field meta value * @return string */ public static function format_value( $field, $value ) { if ( ! is_array( $value ) ) { return self::call( 'format_single_value', $field, $value ); } $output = '