Import legacy nurinda.co.id website

This commit is contained in:
dirga committed 2026-10-01 14:27:19 +07:00
1 parent 3f59bb9038
commit 6e21646ac4
13389 files changed
+3246690

No files matched your search

@@ -0,0 +1,120 @@
<?php
/**
* Autocomplete field class.
*/
class MASHSB_RWMB_Autocomplete_Field extends MASHSB_RWMB_Multiple_Values_Field
{
/**
* Enqueue scripts and styles.
*/
static function admin_enqueue_scripts()
{
wp_enqueue_style( 'mashsb-rwmb-autocomplete', MASHSB_RWMB_CSS_URL . 'autocomplete.css', array( 'wp-admin' ), MASHSB_RWMB_VER );
wp_enqueue_script( 'mashsb-rwmb-autocomplete', MASHSB_RWMB_JS_URL . 'autocomplete.js', array( 'jquery-ui-autocomplete' ), MASHSB_RWMB_VER, true );
wp_localize_script( 'mashsb-rwmb-autocomplete', 'MASHSB_RWMB_Autocomplete', array( 'delete' => __( 'Delete', 'meta-box' ) ) );
}
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
* @return string
*/
static function html( $meta, $field )
{
if ( ! is_array( $meta ) )
$meta = array( $meta );
$field = apply_filters( 'mashsb_rwmb_autocomplete_field', $field, $meta );
$options = $field['options'];
if ( ! is_string( $field['options'] ) )
{
$options = array();
foreach ( (array) $field['options'] as $value => $label )
{
$options[] = array(
'value' => $value,
'label' => $label,
);
}
$options = wp_json_encode( $options );
}
// Input field that triggers autocomplete.
// This field doesn't store field values, so it doesn't have "name" attribute.
// The value(s) of the field is store in hidden input(s). See below.
$html = sprintf(
'<input type="text" class="mashsb-rwmb-autocomplete-search" size="%s">
<input type="hidden" name="%s" class="mashsb-rwmb-autocomplete" data-options="%s" disabled>',
$field['size'],
$field['field_name'],
esc_attr( $options )
);
$html .= '<div class="mashsb-rwmb-autocomplete-results">';
// Each value is displayed with label and 'Delete' option
// The hidden input has to have ".mashsb-rwmb-*" class to make clone work
$tpl = '
<div class="mashsb-rwmb-autocomplete-result">
<div class="label">%s</div>
<div class="actions">%s</div>
<input type="hidden" class="mashsb-rwmb-autocomplete-value" name="%s" value="%s">
</div>
';
if ( is_array( $field['options'] ) )
{
foreach ( $field['options'] as $value => $label )
{
if ( in_array( $value, $meta ) )
{
$html .= sprintf(
$tpl,
$label,
__( 'Delete', 'meta-box' ),
$field['field_name'],
$value
);
}
}
}
else
{
foreach ( $meta as $value )
{
if ( empty( $value ) )
continue;
$label = apply_filters( 'mashsb_rwmb_autocomplete_result_label', $value, $field );
$html .= sprintf(
$tpl,
$label,
__( 'Delete', 'meta-box' ),
$field['field_name'],
$value
);
}
}
$html .= '</div>'; // .mashsb-rwmb-autocomplete-results
return $html;
}
/**
* Normalize parameters for field
*
* @param array $field
* @return array
*/
static function normalize( $field )
{
$field = parent::normalize( $field );
$field = wp_parse_args( $field, array(
'size' => 30,
) );
return $field;
}
}
@@ -0,0 +1,47 @@
<?php
/**
* Button field class.
*/
class MASHSB_RWMB_Button_Field extends MASHSB_RWMB_Field
{
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
* @return string
*/
static function html( $meta, $field )
{
$attributes = self::get_attributes( $field );
return sprintf( '<a href="#" %s>%s</a>', self::render_attributes( $attributes ), $field['std'] );
}
/**
* Normalize parameters for field
*
* @param array $field
* @return array
*/
static function normalize( $field )
{
$field = parent::normalize( $field );
$field['std'] = $field['std'] ? $field['std'] : __( 'Click me', 'meta-box' );
return $field;
}
/**
* Get the attributes for a field
*
* @param array $field
* @param mixed $value
* @return array
*/
static function get_attributes( $field, $value = null )
{
$attributes = parent::get_attributes( $field, $value );
$attributes['class'] .= ' button hide-if-no-js';
return $attributes;
}
}
@@ -0,0 +1,19 @@
<?php
/**
* Checkbox list field class.
*/
class MASHSB_RWMB_Checkbox_List_Field extends MASHSB_RWMB_Input_List_Field
{
/**
* Normalize parameters for field
* @param array $field
* @return array
*/
static function normalize( $field )
{
$field['multiple'] = true;
$field = parent::normalize( $field );
return $field;
}
}
@@ -0,0 +1,87 @@
<?php
/**
* Checkbox field class.
*/
class MASHSB_RWMB_Checkbox_Field extends MASHSB_RWMB_Input_Field
{
/**
* Enqueue scripts and styles.
*/
static function admin_enqueue_scripts()
{
wp_enqueue_style( 'mashsb-rwmb-checkbox', MASHSB_RWMB_CSS_URL . 'checkbox.css', array(), MASHSB_RWMB_VER );
}
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
* @return string
*/
static function html( $meta, $field )
{
$attributes = self::get_attributes( $field, 1 );
return sprintf(
'<input %s %s>',
self::render_attributes( $attributes ),
checked( ! empty( $meta ), 1, false )
);
}
/**
* Get the attributes for a field.
*
* @param array $field
* @param mixed $value
* @return array
*/
static function get_attributes( $field, $value = null )
{
$attributes = parent::get_attributes( $field, $value );
$attributes['type'] = 'checkbox';
$attributes['list'] = false;
return $attributes;
}
/**
* Set the value of checkbox to 1 or 0 instead of 'checked' and empty string
* This prevents using default value once the checkbox has been unchecked
*
* @link https://github.com/rilwis/meta-box/issues/6
*
* @param mixed $new
* @param mixed $old
* @param int $post_id
* @param array $field
*
* @return int
*/
static function value( $new, $old, $post_id, $field )
{
return empty( $new ) ? 0 : 1;
}
/**
* Output the field value
* Display 'Yes' or 'No' instead of '1' and '0'
*
* 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
*/
static function the_value( $field, $args = array(), $post_id = null )
{
$value = self::get_value( $field, $args, $post_id );
return $value ? __( 'Yes', 'meta-box' ) : __( 'No', 'meta-box' );
}
}
@@ -0,0 +1,160 @@
<?php
/**
* Abstract class for any kind of choice field.
*/
abstract class MASHSB_RWMB_Choice_Field extends MASHSB_RWMB_Field
{
/**
* 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 )
{
return '';
}
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
* @return string
*/
public static function html( $meta, $field )
{
$field_class = MASHSB_RW_Meta_Box::get_class_name( $field );
$meta = (array) $meta;
$options = call_user_func( array( $field_class, 'get_options' ), $field );
$db_fields = call_user_func( array( $field_class, 'get_db_fields' ), $field );
return call_user_func( array( $field_class, 'walk' ), $options, $db_fields, $meta, $field );
}
/**
* Normalize parameters for field
*
* @param array $field
* @return array
*/
public static function normalize( $field )
{
$field = parent::normalize( $field );
$field = wp_parse_args( $field, array(
'flatten' => true,
'options' => array(),
) );
return $field;
}
/**
* Get field names of object to be used by walker
*
* @return array
*/
public static function get_db_fields()
{
return array(
'parent' => 'parent',
'id' => 'value',
'label' => 'label',
);
}
/**
* Get options for walker
*
* @param array $field
*
* @return array
*/
public static function get_options( $field )
{
$options = array();
foreach ( (array) $field['options'] as $value => $label )
{
$option = is_array( $label ) ? $label : array( 'label' => (string) $label, 'value' => (string) $value );
if ( isset( $option['label'] ) && isset( $option['value'] ) )
$options[$option['value']] = (object) $option;
}
return $options;
}
/**
* 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
*/
public static function the_value( $field, $args = array(), $post_id = null )
{
$field_class = MASHSB_RW_Meta_Box::get_class_name( $field );
$value = call_user_func( array( $field_class, 'get_value' ), $field, $args, $post_id );
if ( ! $value )
return '';
if ( $field['clone'] && $field['multiple'] )
{
$output = '<ul>';
foreach ( $value as $subvalue )
{
$output .= '<li>';
$output .= call_user_func( array( $field_class, 'list_option_labels' ), $subvalue, $field );
$output .= '</li>';
}
$output .= '</ul>';
}
elseif ( $field['clone'] || $field['multiple'] )
{
$output = call_user_func( array( $field_class, 'list_option_labels' ), $value, $field );
}
else
{
$output = $field['options'][$value]['label'];
}
return $output;
}
/**
* List option labels
*
* @param array $meta
* @param array $field Field parameter
* @return string
*/
public static function list_option_labels( $meta, $field )
{
$field_class = MASHSB_RW_Meta_Box::get_class_name( $field );
$output = '<ul>';
foreach ( $meta as $m )
{
$output .= sprintf( '<li>%s</li>', call_user_func( array( $field_class, 'get_option_label' ), $m, $field ) );
}
return $output . '</ul>';
}
/**
* 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( MASHSB_RW_Meta_Box::get_class_name( $field ), 'get_options' ), $field );
return $options[$value]->label;
}
}
@@ -0,0 +1,85 @@
<?php
/**
* Color field class.
*/
class MASHSB_RWMB_Color_Field extends MASHSB_RWMB_Text_Field
{
/**
* Enqueue scripts and styles
*/
static function admin_enqueue_scripts()
{
wp_enqueue_style( 'mashsb-rwmb-color', MASHSB_RWMB_CSS_URL . 'color.css', array( 'wp-color-picker' ), MASHSB_RWMB_VER );
wp_enqueue_script( 'mashsb-rwmb-color', MASHSB_RWMB_JS_URL . 'color.js', array( 'wp-color-picker' ), MASHSB_RWMB_VER, true );
}
/**
* Normalize parameters for field.
*
* @param array $field
* @return array
*/
static function normalize( $field )
{
$field = wp_parse_args( $field, array(
'size' => 7,
'maxlength' => 7,
'pattern' => '^#+([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$',
'js_options' => array(),
) );
$field['js_options'] = wp_parse_args( $field['js_options'], array(
'defaultColor' => false,
'hide' => true,
'palettes' => true,
) );
$field = parent::normalize( $field );
return $field;
}
/**
* Get the attributes for a field
*
* @param array $field
* @param mixed $value
* @return array
*/
static function get_attributes( $field, $value = null )
{
$attributes = parent::get_attributes( $field, $value );
$attributes = wp_parse_args( $attributes, array(
'data-options' => wp_json_encode( $field['js_options'] ),
) );
return $attributes;
}
/**
* Output color field as a dot.
* @param array $field
* @param array $args
* @param null $post_id
* @return string
*/
static function the_value( $field, $args = array(), $post_id = null )
{
$value = self::get_value( $field, $args, $post_id );
$template = "<span style='display:inline-block;width:20px;height:20px;border-radius:50%%;background:%s;'></span>";
if ( is_array( $value ) )
{
$output = '<ul>';
foreach ( $value as $subvalue )
{
$output .= '<li>' . sprintf( $template, $subvalue ) . '</li>';
}
$output .= '</ul>';
}
else
{
$output = sprintf( $template, $value );
}
return $output;
}
}
@@ -0,0 +1,43 @@
<?php
/**
* Custom HTML field class.
*/
class MASHSB_RWMB_Custom_Html_Field extends MASHSB_RWMB_Field {
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function html( $meta, $field ) {
global $post;
$html = !empty( $field['std'] ) ? $field['std'] : '';
if( !empty( $field['callback'] ) && is_callable( $field['callback'] ) ) {
$html = call_user_func_array( $field['callback'], array($meta, $field) );
}
//return $html;
//return get_permalink( $post->ID );
return self::get_fb_debugger_url($post);
}
/**
* Link to the open graph debugger to check if open graph tags are valid
*
* @global array $post
* @return string
*/
static function get_fb_debugger_url($post) {
//var_dump( $post );
if( isset( $post ) && $post->post_status == "publish" ) {
$url = get_permalink( $post->ID );
return '<a href="https://developers.facebook.com/tools/debug/?q=' . $url . '" target="_blank" rel="noopener"> Validate Open Graph data </a>';
} else {
return '';
}
}
}
@@ -0,0 +1,31 @@
<?php
/**
* Date field class.
*/
class MASHSB_RWMB_Date_Field extends MASHSB_RWMB_Datetime_Field
{
/**
* Enqueue scripts and styles
*
* @return void
*/
public static function admin_enqueue_scripts()
{
parent::admin_register_scripts();
wp_enqueue_style( 'jquery-ui-datepicker' );
wp_enqueue_script( 'mashsb-rwmb-date' );
}
/**
* Returns a date() compatible format string from the JavaScript format
*
* @link http://www.php.net/manual/en/function.date.php
* @param array $field
*
* @return string
*/
public static function translate_format( $field )
{
return strtr( $field['js_options']['dateFormat'], self::$date_formats );
}
}
@@ -0,0 +1,264 @@
<?php
/**
* Datetime field class.
*/
class MASHSB_RWMB_Datetime_Field extends MASHSB_RWMB_Text_Field
{
/**
* Translate date format from jQuery UI date picker to PHP date()
* It's used to store timestamp value of the field
* Missing: '!' => '', 'oo' => '', '@' => '', "''" => "'"
* @var array
*/
protected static $date_formats = array(
'd' => 'j', 'dd' => 'd', 'oo' => 'z', 'D' => 'D', 'DD' => 'l',
'm' => 'n', 'mm' => 'm', 'M' => 'M', 'MM' => 'F', 'y' => 'y', 'yy' => 'Y', 'o' => 'z',
);
/**
* Translate time format from jQuery UI time picker to PHP date()
* It's used to store timestamp value of the field
* Missing: 't' => '', T' => '', 'm' => '', 's' => ''
* @var array
*/
protected static $time_formats = array(
'H' => 'G', 'HH' => 'H', 'h' => 'g', 'hh' => 'h',
'mm' => 'i', 'ss' => 's', 'l' => 'u', 'tt' => 'a', 'TT' => 'A',
);
/**
* Register scripts and styles
*/
public static function admin_register_scripts()
{
$url = MASHSB_RWMB_CSS_URL . 'jqueryui';
wp_register_style( 'jquery-ui-core', "{$url}/jquery.ui.core.css", array(), '1.8.17' );
wp_register_style( 'jquery-ui-theme', "{$url}/jquery.ui.theme.css", array(), '1.8.17' );
wp_register_style( 'wp-datepicker', MASHSB_RWMB_CSS_URL . 'datepicker.css', array( 'jquery-ui-core', 'jquery-ui-theme' ), '1.8.17' );
wp_register_style( 'jquery-ui-datepicker', "{$url}/jquery.ui.datepicker.css", array( 'wp-datepicker' ), '1.8.17' );
wp_register_style( 'jquery-ui-slider', "{$url}/jquery.ui.slider.css", array( 'jquery-ui-core', 'jquery-ui-theme' ), '1.8.17' );
wp_register_style( 'jquery-ui-timepicker', "{$url}/jquery-ui-timepicker-addon.min.css", array( 'jquery-ui-datepicker', 'jquery-ui-slider', 'wp-datepicker' ), '1.5.0' );
$url = MASHSB_RWMB_JS_URL . 'jqueryui';
wp_register_script( 'jquery-ui-timepicker', "{$url}/jquery-ui-timepicker-addon.min.js", array( 'jquery-ui-datepicker', 'jquery-ui-slider' ), '1.5.0', true );
wp_register_script( 'jquery-ui-timepicker-i18n', "{$url}/jquery-ui-timepicker-addon-i18n.min.js", array( 'jquery-ui-timepicker' ), '1.5.0', true );
/**
* Localization
* Use 1 minified JS file for timepicker which contains all languages for simplicity (in version < 4.4.2 we use separated JS files).
* The language is set in Javascript
*
* Note: we use full locale (de-DE) and fallback to short locale (de)
*/
$locale = str_replace( '_', '-', get_locale() );
$locale_short = substr( $locale, 0, 2 );
$date_paths = array( 'jqueryui/datepicker-i18n/jquery.ui.datepicker-' . $locale . '.js' );
if ( strlen( $locale ) > 2 )
{
// Also check alternate i18n filenames
// (e.g. jquery.ui.datepicker-de.js instead of jquery.ui.datepicker-de-DE.js)
$date_paths[] = 'jqueryui/datepicker-i18n/jquery.ui.datepicker-' . substr( $locale, 0, 2 ) . '.js';
}
$deps = array( 'jquery-ui-timepicker-i18n' );
foreach ( $date_paths as $date_path )
{
if ( file_exists( MASHSB_RWMB_DIR . 'js/' . $date_path ) )
{
wp_register_script( 'jquery-ui-datepicker-i18n', MASHSB_RWMB_JS_URL . $date_path, array( 'jquery-ui-datepicker' ), '1.8.17', true );
$deps[] = 'jquery-ui-datepicker-i18n';
break;
}
}
wp_register_script( 'mashsb-rwmb-datetime', MASHSB_RWMB_JS_URL . 'datetime.js', $deps, MASHSB_RWMB_VER, true );
wp_register_script( 'mashsb-rwmb-date', MASHSB_RWMB_JS_URL . 'date.js', $deps, MASHSB_RWMB_VER, true );
wp_register_script( 'mashsb-rwmb-time', MASHSB_RWMB_JS_URL . 'time.js', array( 'jquery-ui-timepicker-i18n' ), MASHSB_RWMB_VER, true );
wp_localize_script( 'mashsb-rwmb-datetime', 'MASHSB_RWMB_Datetimepicker', array(
'locale' => $locale,
'localeShort' => $locale_short,
) );
wp_localize_script( 'mashsb-rwmb-time', 'MASHSB_RWMB_Timepicker', array(
'locale' => $locale,
'localeShort' => $locale_short,
) );
}
/**
* Enqueue scripts and styles
*/
public static function admin_enqueue_scripts()
{
self::admin_register_scripts();
wp_enqueue_style( 'jquery-ui-timepicker' );
wp_enqueue_script( 'mashsb-rwmb-datetime' );
}
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
public static function html( $meta, $field )
{
$output = '';
if ( $field['timestamp'] )
{
$name = $field['field_name'];
$field = wp_parse_args( array( 'field_name' => $name . '[formatted]' ), $field );
$output .= sprintf(
'<input type="hidden" name="%s" class="mashsb-rwmb-datetime-timestamp" value="%s">',
esc_attr( $name . '[timestamp]' ),
isset( $meta['timestamp'] ) ? intval( $meta['timestamp'] ) : ''
);
$meta = isset( $meta['formatted'] ) ? $meta['formatted'] : '';
}
$output .= parent::html( $meta, $field );
if ( $field['inline'] )
{
$output .= '<div class="mashsb-rwmb-datetime-inline"></div>';
}
return $output;
}
/**
* Calculates the timestamp from the datetime string and returns it
* if $field['timestamp'] is set or the datetime string if not
*
* @param mixed $new
* @param mixed $old
* @param int $post_id
* @param array $field
*
* @return string|int
*/
public static function value( $new, $old, $post_id, $field )
{
if ( ! $field['timestamp'] )
return $new;
if ( $field['clone'] )
{
foreach ( $new as $key => $value )
{
$new[$key] = isset( $value['timestamp'] ) ? $value['timestamp'] : 0;
}
return $new;
}
return isset( $new['timestamp'] ) ? $new['timestamp'] : 0;
}
/**
* 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( MASHSB_RW_Meta_Box::get_class_name( $field ), 'translate_format' );
if ( is_array( $meta ) )
{
foreach ( $meta as $key => $value )
{
$meta[$key] = array(
'timestamp' => $value,
'formatted' => date( call_user_func( $method, $field ), intval( $value ) ),
);
}
}
else
{
$meta = array(
'timestamp' => $meta,
'formatted' => date( call_user_func( $method, $field ), intval( $meta ) ),
);
}
return $meta;
}
/**
* Normalize parameters for field
*
* @param array $field
* @return array
*/
public static function normalize( $field )
{
$field = wp_parse_args( $field, array(
'timestamp' => false,
'inline' => false,
'js_options' => array(),
) );
// Deprecate 'format', but keep it for backward compatible
// Use 'js_options' instead
$field['js_options'] = wp_parse_args( $field['js_options'], array(
'timeFormat' => 'HH:mm',
'separator' => ' ',
'dateFormat' => empty( $field['format'] ) ? 'yy-mm-dd' : $field['format'],
'showButtonPanel' => true,
) );
if ( $field['inline'] )
{
$field['js_options'] = wp_parse_args( $field['js_options'], array(
'altFieldTimeOnly' => false,
) );
}
$field = MASHSB_RWMB_Text_Field::normalize( $field );
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 = parent::get_attributes( $field, $value );
$attributes = wp_parse_args( $attributes, array(
'data-options' => wp_json_encode( $field['js_options'] ),
) );
return $attributes;
}
/**
* Returns a date() compatible format string from the JavaScript format
*
* @link http://www.php.net/manual/en/function.date.php
* @param array $field
*
* @return string
*/
public static function translate_format( $field )
{
return strtr( $field['js_options']['dateFormat'], self::$date_formats )
. $field['js_options']['separator']
. strtr( $field['js_options']['timeFormat'], self::$time_formats );
}
}
@@ -0,0 +1,43 @@
<?php
/**
* Divider field class.
*/
class MASHSB_RWMB_Divider_Field extends MASHSB_RWMB_Field
{
/**
* Enqueue scripts and styles
*
* @return void
*/
static function admin_enqueue_scripts()
{
wp_enqueue_style( 'mashsb-rwmb-divider', MASHSB_RWMB_CSS_URL . 'divider.css', array(), MASHSB_RWMB_VER );
}
/**
* Show begin HTML markup for fields
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function begin_html( $meta, $field )
{
$attributes = empty( $field['id'] ) ? '' : " id='{$field['id']}'";
return "<hr$attributes>";
}
/**
* Show end HTML markup for fields
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function end_html( $meta, $field )
{
return '';
}
}
@@ -0,0 +1,50 @@
<?php
/**
* Email field class.
*/
class MASHSB_RWMB_Email_Field extends MASHSB_RWMB_Text_Field
{
/**
* Normalize parameters for field
*
* @param array $field
*
* @return array
*/
static function normalize( $field )
{
$field = parent::normalize( $field );
return $field;
}
/**
* Get the attributes for a field
*
* @param array $field
* @param mixed $value
*
* @return array
*/
static function get_attributes( $field, $value = null )
{
$attributes = parent::get_attributes( $field, $value );
$attributes['type'] = 'email';
return $attributes;
}
/**
* Sanitize email
*
* @param mixed $new
* @param mixed $old
* @param int $post_id
* @param array $field
*
* @return string
*/
static function value( $new, $old, $post_id, $field )
{
return $field['clone'] ? array_map( 'sanitize_email', (array) $new ) : sanitize_email( $new );
}
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,84 @@
<?php
/**
* Fieldset text class.
*/
class MASHSB_RWMB_Fieldset_Text_Field extends MASHSB_RWMB_Text_Field
{
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function html( $meta, $field )
{
$html = array();
$tpl = '<label>%s %s</label>';
foreach ( $field['options'] as $key => $label )
{
$value = isset( $meta[$key] ) ? $meta[$key] : '';
$field['attributes']['name'] = $field['field_name'] . "[{$key}]";
$html[] = sprintf( $tpl, $label, parent::html( $value, $field ) );
}
$out = '<fieldset><legend>' . $field['desc'] . '</legend>' . implode( ' ', $html ) . '</fieldset>';
return $out;
}
/**
* Normalize parameters for field
*
* @param array $field
*
* @return array
*/
static function normalize( $field )
{
$field = parent::normalize( $field );
$field['multiple'] = false;
$field['attributes']['id'] = false;
return $field;
}
/**
* Output the field value
* Display options in format Label: value in unordered list
*
* @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 '';
$output = '<table>';
$output .= '<thead><tr>';
foreach ( $field['options'] as $label )
{
$output .= "<th>$label</th>";
}
$output .= '</tr></thead><tbody>';
foreach ( $value as $subvalue )
{
$output .= '<tr>';
foreach ( $subvalue as $value )
{
$output .= "<td>$value</td>";
}
$output .= '</tr>';
}
$output .= '</tbody></table>';
return $output;
}
}
@@ -0,0 +1,43 @@
<?php
/**
* File advanced field class which users WordPress media popup to upload and select files.
*/
class MASHSB_RWMB_File_Advanced_Field extends MASHSB_RWMB_Media_Field
{
/**
* Get the field value.
* @param array $field
* @param array $args
* @param null $post_id
* @return mixed
*/
static function get_value( $field, $args = array(), $post_id = null )
{
return MASHSB_RWMB_File_Field::get_value( $field, $args, $post_id );
}
/**
* Output the field value.
* @param array $field
* @param array $args
* @param null $post_id
* @return mixed
*/
static function the_value( $field, $args = array(), $post_id = null )
{
return MASHSB_RWMB_File_Field::the_value( $field, $args, $post_id );
}
/**
* Get uploaded file information.
*
* @param int $file_id Attachment image ID (post ID). Required.
* @param array $args Array of arguments (for size).
* @return array|bool False if file not found. Array of image info on success
*/
static function file_info( $file_id, $args = array() )
{
return MASHSB_RWMB_File_Field::file_info( $file_id, $args );
}
}
@@ -0,0 +1,64 @@
<?php
/**
* File input field class which uses an input for file URL.
*/
class MASHSB_RWMB_File_Input_Field extends MASHSB_RWMB_Field
{
/**
* Enqueue scripts and styles
*
* @return void
*/
static function admin_enqueue_scripts()
{
// Make sure scripts for new media uploader in WordPress 3.5 is enqueued
wp_enqueue_media();
wp_enqueue_script( 'mashsb-rwmb-file-input', MASHSB_RWMB_JS_URL . 'file-input.js', array( 'jquery' ), MASHSB_RWMB_VER, true );
wp_localize_script( 'mashsb-rwmb-file-input', 'rwmbFileInput', array(
'frameTitle' => __( 'Select File', 'meta-box' ),
) );
}
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function html( $meta, $field )
{
return sprintf(
'<input type="text" class="mashsb-rwmb-file-input" name="%s" id="%s" value="%s" placeholder="%s" size="%s">
<a href="#" class="mashsb-rwmb-file-input-select button-primary">%s</a>
<a href="#" class="mashsb-rwmb-file-input-remove button %s">%s</a>',
$field['field_name'],
$field['id'],
$meta,
$field['placeholder'],
$field['size'],
__( 'Select', 'meta-box' ),
$meta ? '' : 'hidden',
__( 'Remove', 'meta-box' )
);
}
/**
* Normalize parameters for field
*
* @param array $field
*
* @return array
*/
static function normalize( $field )
{
$field = parent::normalize( $field );
$field = wp_parse_args( $field, array(
'size' => 30,
'placeholder' => '',
) );
return $field;
}
}
@@ -0,0 +1,39 @@
<?php
/**
* File advanced field class which users WordPress media popup to upload and select files.
*/
class MASHSB_RWMB_File_Upload_Field extends MASHSB_RWMB_File_Advanced_Field
{
/**
* Add actions
*
* @return void
*/
static function add_actions()
{
parent::add_actions();
// Print attachment templates
add_action( 'print_media_templates', array( __CLASS__, 'print_templates' ) );
}
/**
* Enqueue scripts and styles
*
* @return void
*/
static function admin_enqueue_scripts()
{
parent::admin_enqueue_scripts();
wp_enqueue_style( 'mashsb-rwmb-upload', MASHSB_RWMB_CSS_URL . 'upload.css', array( 'mashsb-rwmb-media' ), MASHSB_RWMB_VER );
wp_enqueue_script( 'mashsb-rwmb-file-upload', MASHSB_RWMB_JS_URL . 'file-upload.js', array( 'mashsb-rwmb-media' ), MASHSB_RWMB_VER, true );
}
/**
* Template for media item
* @return void
*/
static function print_templates()
{
require_once( MASHSB_RWMB_INC_DIR . 'templates/upload.php' );
}
}
@@ -0,0 +1,367 @@
<?php
/**
* File field class which uses HTML <input type="file"> to upload file.
*/
class MASHSB_RWMB_File_Field extends MASHSB_RWMB_Field
{
/**
* Enqueue scripts and styles
*/
static function admin_enqueue_scripts()
{
wp_enqueue_style( 'mashsb-rwmb-file', MASHSB_RWMB_CSS_URL . 'file.css', array(), MASHSB_RWMB_VER );
wp_enqueue_script( 'mashsb-rwmb-file', MASHSB_RWMB_JS_URL . 'file.js', array( 'jquery' ), MASHSB_RWMB_VER, true );
wp_localize_script( 'mashsb-rwmb-file', 'rwmbFile', array(
'maxFileUploadsSingle' => __( 'You may only upload maximum %d file', 'meta-box' ),
'maxFileUploadsPlural' => __( 'You may only upload maximum %d files', 'meta-box' ),
) );
}
/**
* Add custom actions
*/
static function add_actions()
{
// Add data encoding type for file uploading
add_action( 'post_edit_form_tag', array( __CLASS__, 'post_edit_form_tag' ) );
// Delete file via Ajax
add_action( 'wp_ajax_rwmb_delete_file', array( __CLASS__, 'wp_ajax_delete_file' ) );
// Allow reordering files
add_action( 'wp_ajax_rwmb_reorder_files', array( __CLASS__, 'wp_ajax_reorder_files' ) );
}
/**
* Ajax callback for reordering images
*/
static function wp_ajax_reorder_files()
{
$post_id = (int) filter_input( INPUT_POST, 'post_id', FILTER_SANITIZE_NUMBER_INT );
$field_id = (string) filter_input( INPUT_POST, 'field_id' );
$order = (string) filter_input( INPUT_POST, 'order' );
check_ajax_referer( "mashsb-rwmb-reorder-files_{$field_id}" );
parse_str( $order, $items );
delete_post_meta( $post_id, $field_id );
foreach ( $items['item'] as $item )
{
add_post_meta( $post_id, $field_id, $item, false );
}
wp_send_json_success();
}
/**
* Add data encoding type for file uploading
*
* @return void
*/
static function post_edit_form_tag()
{
echo ' enctype="multipart/form-data"';
}
/**
* Ajax callback for deleting files.
* Modified from a function used by "Verve Meta Boxes" plugin
* @link http://goo.gl/LzYSq
*/
static function wp_ajax_delete_file()
{
$post_id = (int) filter_input( INPUT_POST, 'post_id', FILTER_SANITIZE_NUMBER_INT );
$field_id = (string) filter_input( INPUT_POST, 'field_id' );
$attachment_id = (int) filter_input( INPUT_POST, 'attachment_id', FILTER_SANITIZE_NUMBER_INT );
$force_delete = (int) filter_input( INPUT_POST, 'force_delete', FILTER_SANITIZE_NUMBER_INT );
check_ajax_referer( "mashsb-rwmb-delete-file_{$field_id}" );
delete_post_meta( $post_id, $field_id, $attachment_id );
$success = $force_delete ? wp_delete_attachment( $attachment_id ) : true;
if ( $success )
wp_send_json_success();
else
wp_send_json_error( __( 'Error: Cannot delete file', 'meta-box' ) );
}
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function html( $meta, $field )
{
$i18n_title = apply_filters( 'rwmb_file_upload_string', _x( 'Upload Files', 'file upload', 'meta-box' ), $field );
$i18n_more = apply_filters( 'rwmb_file_add_string', _x( '+ Add new file', 'file upload', 'meta-box' ), $field );
// Uploaded files
$html = self::get_uploaded_files( $meta, $field );
$new_file_classes = array( 'new-files' );
if ( ! empty( $field['max_file_uploads'] ) && count( $meta ) >= (int) $field['max_file_uploads'] )
$new_file_classes[] = 'hidden';
// Show form upload
$html .= sprintf(
'<div class="%s">
<h4>%s</h4>
<div class="file-input"><input type="file" name="%s[]" /></div>
<a class="mashsb-rwmb-add-file" href="#"><strong>%s</strong></a>
</div>',
implode( ' ', $new_file_classes ),
$i18n_title,
$field['id'],
$i18n_more
);
return $html;
}
static function get_uploaded_files( $files, $field )
{
$reorder_nonce = wp_create_nonce( "mashsb-rwmb-reorder-files_{$field['id']}" );
$delete_nonce = wp_create_nonce( "mashsb-rwmb-delete-file_{$field['id']}" );
$classes = array( 'mashsb-rwmb-file', 'mashsb-rwmb-uploaded' );
if ( count( $files ) <= 0 )
$classes[] = 'hidden';
$list = '<ul class="%s" data-field_id="%s" data-delete_nonce="%s" data-reorder_nonce="%s" data-force_delete="%s" data-max_file_uploads="%s" data-mime_type="%s">';
$html = sprintf(
$list,
implode( ' ', $classes ),
$field['id'],
$delete_nonce,
$reorder_nonce,
$field['force_delete'] ? 1 : 0,
$field['max_file_uploads'],
$field['mime_type']
);
foreach ( (array) $files as $attachment_id )
{
$html .= self::file_html( $attachment_id );
}
$html .= '</ul>';
return $html;
}
static function file_html( $attachment_id )
{
$i18n_delete = apply_filters( 'rwmb_file_delete_string', _x( 'Delete', 'file upload', 'meta-box' ) );
$i18n_edit = apply_filters( 'rwmb_file_edit_string', _x( 'Edit', 'file upload', 'meta-box' ) );
$item = '
<li id="item_%s">
<div class="mashsb-rwmb-icon">%s</div>
<div class="mashsb-rwmb-info">
<a href="%s" target="_blank" rel="noopener">%s</a>
<p>%s</p>
<a title="%s" href="%s" target="_blank">%s</a> |
<a title="%s" class="mashsb-rwmb-delete-file" href="#" data-attachment_id="%s">%s</a>
</div>
</li>';
$mime_type = get_post_mime_type( $attachment_id );
return sprintf(
$item,
$attachment_id,
wp_get_attachment_image( $attachment_id, array( 60, 60 ), true ),
wp_get_attachment_url( $attachment_id ),
get_the_title( $attachment_id ),
$mime_type,
$i18n_edit,
get_edit_post_link( $attachment_id ),
$i18n_edit,
$i18n_delete,
$attachment_id,
$i18n_delete
);
}
/**
* Get meta values to save
*
* @param mixed $new
* @param mixed $old
* @param int $post_id
* @param array $field
*
* @return array|mixed
*/
static function value( $new, $old, $post_id, $field )
{
$name = $field['id'];
if ( empty( $_FILES[$name] ) )
return $new;
$new = array();
$files = self::fix_file_array( $_FILES[$name] );
foreach ( $files as $file_item )
{
$file = wp_handle_upload( $file_item, array( 'test_form' => false ) );
if ( ! isset( $file['file'] ) )
continue;
$file_name = $file['file'];
$attachment = array(
'post_mime_type' => $file['type'],
'guid' => $file['url'],
'post_parent' => $post_id,
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_name ) ),
'post_content' => '',
);
$id = wp_insert_attachment( $attachment, $file_name, $post_id );
if ( ! is_wp_error( $id ) )
{
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file_name ) );
// Save file ID in meta field
$new[] = $id;
}
}
return array_unique( array_merge( $old, $new ) );
}
/**
* Fixes the odd indexing of multiple file uploads from the format:
* $_FILES['field']['key']['index']
* To the more standard and appropriate:
* $_FILES['field']['index']['key']
*
* @param array $files
*
* @return array
*/
static function fix_file_array( $files )
{
$output = array();
foreach ( $files as $key => $list )
{
foreach ( $list as $index => $value )
{
$output[$index][$key] = $value;
}
}
return $output;
}
/**
* Normalize parameters for field
*
* @param array $field
*
* @return array
*/
static function normalize( $field )
{
$field = parent::normalize( $field );
$field = wp_parse_args( $field, array(
'std' => array(),
'force_delete' => false,
'max_file_uploads' => 0,
'mime_type' => '',
) );
$field['multiple'] = true;
return $field;
}
/**
* 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 ( (array) $file_ids as $file_id )
{
if ( $file_info = call_user_func( array( MASHSB_RW_Meta_Box::get_class_name( $field ), 'file_info' ), $file_id, $args ) )
{
$value[$file_id] = $file_info;
}
}
return $value;
}
/**
* Output the field value
* Display unordered list of files
*
* @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 '';
$output = '<ul>';
foreach ( $value as $file_id => $file_info )
{
$output .= sprintf(
'<li><a href="%s" target="_blank">%s</a></li>',
wp_get_attachment_url( $file_id ),
get_the_title( $file_id )
);
}
$output .= '</ul>';
return $output;
}
/**
* Get uploaded file information
*
* @param int $file_id Attachment file ID (post ID). Required.
* @param array $args Array of arguments (for size).
*
* @return array|bool False if file not found. Array of (id, name, path, url) on success
*/
static function file_info( $file_id, $args = array() )
{
$path = get_attached_file( $file_id );
if ( ! $path )
{
return false;
}
return array(
'ID' => $file_id,
'name' => basename( $path ),
'path' => $path,
'url' => wp_get_attachment_url( $file_id ),
'title' => get_the_title( $file_id ),
);
}
}
@@ -0,0 +1,41 @@
<?php
/**
* Custom HTML field class.
*/
class MASHSB_RWMB_Force_Creation_Field extends MASHSB_RWMB_Field {
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function html( $meta, $field ) {
global $post;
$html = !empty( $field['std'] ) ? $field['std'] : '';
if( !empty( $field['callback'] ) && is_callable( $field['callback'] ) ) {
$html = call_user_func_array( $field['callback'], array($meta, $field) );
}
return self::get_force_refresh_url($post, $field);
}
/**
* Link to the open graph debugger to check if open graph tags are valid
*
* @global array $post
* @return string
*/
static function get_force_refresh_url($post, $field) {
//var_dump( $post );
if( isset( $post ) && $post->post_status == "publish" ) {
$url = get_permalink( $post->ID );
return '<a href="' . $url . '?mashsb-refresh" target="_blank" rel="noopener" class="button-small"> Get Shares & Shortlinks </a>' . $field['helper'];
} else {
return '';
}
}
}
@@ -0,0 +1,45 @@
<?php
/**
* Heading field class.
*/
class MASHSB_RWMB_Heading_Field extends MASHSB_RWMB_Field
{
/**
* Enqueue scripts and styles
*
* @return void
*/
static function admin_enqueue_scripts()
{
wp_enqueue_style( 'mashsb-rwmb-heading', MASHSB_RWMB_CSS_URL . 'heading.css', array(), MASHSB_RWMB_VER );
}
/**
* Show begin HTML markup for fields
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function begin_html( $meta, $field )
{
$attributes = empty( $field['id'] ) ? '' : " id='{$field['id']}'";
return sprintf( '<h4%s>%s</h4>', $attributes, $field['name'] );
}
/**
* Show end HTML markup for fields
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function end_html( $meta, $field )
{
$id = $field['id'] ? " id='{$field['id']}-description'" : '';
return $field['desc'] ? "<p{$id} class='description'>{$field['desc']}</p>" : '';
}
}
@@ -0,0 +1,28 @@
<?php
/**
* Custom HTML field class.
*/
class MASHSB_RWMB_Hidden_Data_Field extends MASHSB_RWMB_Field {
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function html( $meta, $field )
{
return sprintf(
'<input type="hidden" class="rwmb-hidden" name="%s" id="%s" value="%s">',
$field['field_name'],
$field['id'],
$field['std']
);
}
}
@@ -0,0 +1,22 @@
<?php
/**
* Hidden field class.
*/
class MASHSB_RWMB_Hidden_Field extends MASHSB_RWMB_Input_Field
{
/**
* Get the attributes for a field
*
* @param array $field
* @param mixed $value
*
* @return array
*/
static function get_attributes( $field, $value = null )
{
$attributes = parent::get_attributes( $field, $value );
$attributes['type'] = 'hidden';
return $attributes;
}
}
@@ -0,0 +1,90 @@
<?php
/**
* Image advanced field class which users WordPress media popup to upload and select images.
*/
class MASHSB_RWMB_Image_Advanced_Field extends MASHSB_RWMB_Media_Field
{
/**
* Enqueue scripts and styles
*
* @return void
*/
static function admin_enqueue_scripts()
{
parent::admin_enqueue_scripts();
wp_enqueue_style( 'mashsb-rwmb-image-advanced', MASHSB_RWMB_CSS_URL . 'image-advanced.css', array( 'mashsb-rwmb-media' ), MASHSB_RWMB_VER );
wp_enqueue_script( 'mashsb-rwmb-image-advanced', MASHSB_RWMB_JS_URL . 'image-advanced.js', array( 'mashsb-rwmb-media' ), MASHSB_RWMB_VER, true );
}
/**
* Add actions
*
* @return void
*/
static function add_actions()
{
parent::add_actions();
// Print attachment templates
add_action( 'print_media_templates', array( __CLASS__, 'print_templates' ) );
}
/**
* Normalize parameters for field
*
* @param array $field
*
* @return array
*/
static function normalize( $field )
{
$field = parent::normalize( $field );
$field['mime_type'] = 'image';
return $field;
}
/**
* Get the field value.
* @param array $field
* @param array $args
* @param null $post_id
* @return mixed
*/
static function get_value( $field, $args = array(), $post_id = null )
{
return MASHSB_RWMB_Image_Field::get_value( $field, $args, $post_id );
}
/**
* Output the field value.
* @param array $field
* @param array $args
* @param null $post_id
* @return mixed
*/
static function the_value( $field, $args = array(), $post_id = null )
{
return MASHSB_RWMB_Image_Field::the_value( $field, $args, $post_id );
}
/**
* Get uploaded file information.
*
* @param int $file_id Attachment image ID (post ID). Required.
* @param array $args Array of arguments (for size).
* @return array|bool False if file not found. Array of image info on success
*/
static function file_info( $file_id, $args = array() )
{
return MASHSB_RWMB_Image_Field::file_info( $file_id, $args );
}
/**
* Template for media item
* @return void
*/
static function print_templates()
{
require_once( MASHSB_RWMB_INC_DIR . 'templates/image-advanced.php' );
}
}
@@ -0,0 +1,116 @@
<?php
/**
* Image select field class which uses images as radio options.
*/
class MASHSB_RWMB_Image_Select_Field extends MASHSB_RWMB_Field
{
/**
* Enqueue scripts and styles
*/
static function admin_enqueue_scripts()
{
wp_enqueue_style( 'mashsb-rwmb-image-select', MASHSB_RWMB_CSS_URL . 'image-select.css', array(), MASHSB_RWMB_VER );
wp_enqueue_script( 'mashsb-rwmb-image-select', MASHSB_RWMB_JS_URL . 'image-select.js', array( 'jquery' ), MASHSB_RWMB_VER, true );
}
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
* @return string
*/
static function html( $meta, $field )
{
$html = array();
$tpl = '<label class="mashsb-rwmb-image-select"><img src="%s"><input type="%s" class="hidden" name="%s" value="%s"%s></label>';
$meta = (array) $meta;
foreach ( $field['options'] as $value => $image )
{
$html[] = sprintf(
$tpl,
$image,
$field['multiple'] ? 'checkbox' : 'radio',
$field['field_name'],
$value,
checked( in_array( $value, $meta ), true, false )
);
}
return implode( ' ', $html );
}
/**
* Normalize parameters for field
*
* @param array $field
* @return array
*/
static function normalize( $field )
{
$field = parent::normalize( $field );
$field['field_name'] .= $field['multiple'] ? '[]' : '';
return $field;
}
/**
* 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 '';
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'][$option] ) );
}
$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;
}
}
@@ -0,0 +1,30 @@
<?php
/**
* File advanced field class which users WordPress media popup to upload and select files.
*/
class MASHSB_RWMB_Image_Upload_Field extends MASHSB_RWMB_Image_Advanced_Field
{
/**
* Add actions
*
* @return void
*/
static function add_actions()
{
parent::add_actions();
// Print attachment templates
add_action( 'print_media_templates', array( 'MASHSB_RWMB_File_Upload_Field', 'print_templates' ) );
}
/**
* Enqueue scripts and styles
*
* @return void
*/
static function admin_enqueue_scripts()
{
parent::admin_enqueue_scripts();
MASHSB_RWMB_File_Upload_Field::admin_enqueue_scripts();
wp_enqueue_script( 'mashsb-rwmb-image-upload', MASHSB_RWMB_JS_URL . 'image-upload.js', array( 'mashsb-rwmb-file-upload', 'mashsb-rwmb-image-advanced' ), MASHSB_RWMB_VER, true );
}
}
@@ -0,0 +1,234 @@
<?php
/**
* Image field class which uses <input type="file"> to upload.
*/
class MASHSB_RWMB_Image_Field extends MASHSB_RWMB_File_Field
{
/**
* Enqueue scripts and styles.
*/
static function admin_enqueue_scripts()
{
// Enqueue same scripts and styles as for file field
parent::admin_enqueue_scripts();
wp_enqueue_style( 'mashsb-rwmb-image', MASHSB_RWMB_CSS_URL . 'image.css', array(), MASHSB_RWMB_VER );
wp_enqueue_script( 'mashsb-rwmb-image', MASHSB_RWMB_JS_URL . 'image.js', array( 'jquery-ui-sortable' ), MASHSB_RWMB_VER, true );
}
/**
* Add custom actions.
*/
static function add_actions()
{
// Do same actions as file field
parent::add_actions();
// Reorder images via Ajax
add_action( 'wp_ajax_rwmb_reorder_images', array( __CLASS__, 'wp_ajax_reorder_images' ) );
}
/**
* Ajax callback for reordering images.
*/
static function wp_ajax_reorder_images()
{
$post_id = (int) filter_input( INPUT_POST, 'post_id', FILTER_SANITIZE_NUMBER_INT );
$field_id = (string) filter_input( INPUT_POST, 'field_id' );
$order = (string) filter_input( INPUT_POST, 'order' );
check_ajax_referer( "mashsb-rwmb-reorder-images_{$field_id}" );
parse_str( $order, $items );
delete_post_meta( $post_id, $field_id );
foreach ( $items['item'] as $item )
{
add_post_meta( $post_id, $field_id, $item, false );
}
wp_send_json_success();
}
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function html( $meta, $field )
{
$i18n_title = apply_filters( 'rwmb_image_upload_string', _x( 'Upload Images', 'image upload', 'meta-box' ), $field );
$i18n_more = apply_filters( 'rwmb_image_add_string', _x( '+ Add new image', 'image upload', 'meta-box' ), $field );
// Uploaded images
$html = self::get_uploaded_images( $meta, $field );
// Show form upload
$html .= sprintf(
'<h4>%s</h4>
<div class="new-files">
<div class="file-input"><input type="file" name="%s[]" /></div>
<a class="mashsb-rwmb-add-file" href="#"><strong>%s</strong></a>
</div>',
$i18n_title,
$field['id'],
$i18n_more
);
return $html;
}
/**
* Get HTML markup for uploaded images
*
* @param array $images
* @param array $field
*
* @return string
*/
static function get_uploaded_images( $images, $field )
{
$reorder_nonce = wp_create_nonce( "mashsb-rwmb-reorder-images_{$field['id']}" );
$delete_nonce = wp_create_nonce( "mashsb-rwmb-delete-file_{$field['id']}" );
$classes = array( 'mashsb-rwmb-images', 'mashsb-rwmb-uploaded' );
if ( count( $images ) <= 0 )
$classes[] = 'hidden';
$list = '<ul class="%s" data-field_id="%s" data-delete_nonce="%s" data-reorder_nonce="%s" data-force_delete="%s" data-max_file_uploads="%s">';
$html = sprintf(
$list,
implode( ' ', $classes ),
$field['id'],
$delete_nonce,
$reorder_nonce,
$field['force_delete'] ? 1 : 0,
$field['max_file_uploads']
);
foreach ( $images as $image )
{
$html .= self::img_html( $image );
}
$html .= '</ul>';
return $html;
}
/**
* Get HTML markup for ONE uploaded image
*
* @param int $image Image ID
* @return string
*/
static function img_html( $image )
{
$i18n_delete = apply_filters( 'rwmb_image_delete_string', _x( 'Delete', 'image upload', 'meta-box' ) );
$i18n_edit = apply_filters( 'rwmb_image_edit_string', _x( 'Edit', 'image upload', 'meta-box' ) );
$item = '
<li id="item_%s">
<img src="%s" />
<div class="mashsb-rwmb-image-bar">
<a title="%s" class="mashsb-rwmb-edit-file" href="%s" target="_blank" rel="noopener">%s</a> |
<a title="%s" class="mashsb-rwmb-delete-file" href="#" data-attachment_id="%s">&times;</a>
</div>
</li>
';
$src = wp_get_attachment_image_src( $image, 'thumbnail' );
$src = $src[0];
$link = get_edit_post_link( $image );
return sprintf(
$item,
$image,
$src,
$i18n_edit, $link, $i18n_edit,
$i18n_delete, $image
);
}
/**
* 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 '';
$output = '<ul>';
foreach ( $value as $file_info )
{
$img = sprintf(
'<img src="%s" alt="%s" title="%s">',
esc_url( $file_info['url'] ),
esc_attr( $file_info['alt'] ),
esc_attr( $file_info['title'] )
);
// Link thumbnail to full size image?
if ( isset( $args['link'] ) && $args['link'] )
{
$img = sprintf(
'<a href="%s" title="%s">%s</a>',
esc_url( $file_info['full_url'] ),
esc_attr( $file_info['title'] ),
$img
);
}
$output .= "<li>$img</li>";
}
$output .= '</ul>';
return $output;
}
/**
* Get uploaded file information
*
* @param int $file_id Attachment image ID (post ID). Required.
* @param array $args Array of arguments (for size).
*
* @return array|bool False if file not found. Array of image info on success
*/
static function file_info( $file_id, $args = array() )
{
$args = wp_parse_args( $args, array(
'size' => 'thumbnail',
) );
$img_src = wp_get_attachment_image_src( $file_id, $args['size'] );
if ( ! $img_src )
{
return false;
}
$attachment = get_post( $file_id );
$path = get_attached_file( $file_id );
$info = array(
'ID' => $file_id,
'name' => basename( $path ),
'path' => $path,
'url' => $img_src[0],
'width' => $img_src[1],
'height' => $img_src[2],
'full_url' => wp_get_attachment_url( $file_id ),
'title' => $attachment->post_title,
'caption' => $attachment->post_excerpt,
'description' => $attachment->post_content,
'alt' => get_post_meta( $file_id, '_wp_attachment_image_alt', true ),
);
if ( function_exists( 'wp_get_attachment_image_srcset' ) )
{
$info['srcset'] = wp_get_attachment_image_srcset( $file_id );
}
return $info;
}
}
@@ -0,0 +1,91 @@
<?php
/**
* Input list field.
*/
class MASHSB_RWMB_Input_List_Field extends MASHSB_RWMB_Choice_Field
{
/**
* Enqueue scripts and styles
*/
public static function admin_enqueue_scripts()
{
wp_enqueue_style( 'mashsb-rwmb-input-list', MASHSB_RWMB_CSS_URL . 'input-list.css', array(), MASHSB_RWMB_VER );
wp_enqueue_script( 'mashsb-rwmb-input-list', MASHSB_RWMB_JS_URL . 'input-list.js', array(), MASHSB_RWMB_VER, true );
}
/**
* 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 )
{
$walker = new MASHSB_RWMB_Input_List_Walker( $db_fields, $field, $meta );
$output = sprintf( '<ul class="mashsb-rwmb-input-list %s">', $field['collapse'] ? 'collapse' : '' );
$output .= $walker->walk( $options, $field['flatten'] ? - 1 : 0 );
$output .= '</ul>';
return $output;
}
/**
* Normalize parameters for field
*
* @param array $field
* @return array
*/
public static function normalize( $field )
{
$field = $field['multiple'] ? MASHSB_RWMB_Multiple_Values_Field::normalize( $field ) : $field;
$field = MASHSB_RWMB_Input_Field::normalize( $field );
$field = parent::normalize( $field );
$field = wp_parse_args( $field, array(
'collapse' => true,
) );
$field['flatten'] = $field['multiple'] ? $field['flatten'] : true;
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 = MASHSB_RWMB_Input_Field::get_attributes( $field, $value );
$attributes['id'] = false;
$attributes['type'] = $field['multiple'] ? 'checkbox' : 'radio';
$attributes['value'] = $value;
return $attributes;
}
/**
* Output the field value
* Display option name instead of option value
*
* @use self::meta()
*
* @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 the_value( $field, $args = array(), $post_id = null )
{
$value = parent::get_value( $field, $args, $post_id );
return empty( $value ) ? '' : $field['options'][$value];
}
}
@@ -0,0 +1,83 @@
<?php
/**
* Abstract input field class which is used for all <input> fields.
*/
abstract class MASHSB_RWMB_Input_Field extends MASHSB_RWMB_Field
{
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
* @return string
*/
static function html( $meta, $field )
{
$attributes = call_user_func( array( MASHSB_RW_Meta_Box::get_class_name( $field ), 'get_attributes' ), $field, $meta );
return sprintf( '<input %s>%s', self::render_attributes( $attributes ), self::datalist_html( $field ) );
}
/**
* Normalize parameters for field
*
* @param array $field
* @return array
*/
static function normalize( $field )
{
$field = parent::normalize( $field );
$field = wp_parse_args( $field, array(
'datalist' => false,
'readonly' => false,
) );
if ( $field['datalist'] )
{
$field['datalist'] = wp_parse_args( $field['datalist'], array(
'id' => $field['id'] . '_list',
'options' => array(),
) );
}
return $field;
}
/**
* Get the attributes for a field
*
* @param array $field
* @param mixed $value
* @return array
*/
static function get_attributes( $field, $value = null )
{
$attributes = parent::get_attributes( $field, $value );
$attributes = wp_parse_args( $attributes, array(
'list' => $field['datalist'] ? $field['datalist']['id'] : false,
'readonly' => $field['readonly'],
'value' => $value,
'placeholder' => $field['placeholder'],
) );
return $attributes;
}
/**
* Create datalist, if any
*
* @param array $field
* @return array
*/
static function datalist_html( $field )
{
if ( empty( $field['datalist'] ) )
return '';
$datalist = $field['datalist'];
$html = sprintf( '<datalist id="%s">', $datalist['id'] );
foreach ( $datalist['options'] as $option )
{
$html .= sprintf( '<option value="%s"></option>', $option );
}
$html .= '</datalist>';
return $html;
}
}
@@ -0,0 +1,149 @@
<?php
/**
* Key-value field class.
*/
abstract class MASHSB_RWMB_Key_Value_Field extends MASHSB_RWMB_Text_Field
{
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
* @return string
*/
static function html( $meta, $field )
{
// Key
$key = isset( $meta[0] ) ? $meta[0] : '';
$attributes = self::get_attributes( $field, $key );
$attributes['placeholder'] = esc_attr__( 'Key', 'meta-box' );
$html = sprintf( '<input %s>', self::render_attributes( $attributes ) );
// Value
$val = isset( $meta[1] ) ? $meta[1] : '';
$attributes = self::get_attributes( $field, $val );
$attributes['placeholder'] = esc_attr__( 'Value', 'meta-box' );
$html .= sprintf( '<input %s>', self::render_attributes( $attributes ) );
return $html;
}
/**
* Show begin HTML markup for fields
*
* @param mixed $meta
* @param array $field
* @return string
*/
static function begin_html( $meta, $field )
{
$desc = $field['desc'] ? "<p id='{$field['id']}_description' class='description'>{$field['desc']}</p>" : '';
if ( empty( $field['name'] ) )
return '<div class="mashsb-rwmb-input">' . $desc;
return sprintf(
'<div class="mashsb-rwmb-label">
<label for="%s">%s</label>
</div>
<div class="mashsb-rwmb-input">
%s',
$field['id'],
$field['name'],
$desc
);
}
/**
* 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'] ? self::add_clone_button( $field ) : '';
$html = "$button</div>";
return $html;
}
/**
* Escape meta for field output
*
* @param mixed $meta
* @return mixed
*/
static function esc_meta( $meta )
{
foreach ( (array) $meta as $k => $pairs )
{
$meta[$k] = array_map( 'esc_attr', (array) $pairs );
}
return $meta;
}
/**
* Sanitize field value.
*
* @param mixed $new
* @param mixed $old
* @param int $post_id
* @param array $field
*
* @return string
*/
static function value( $new, $old, $post_id, $field )
{
foreach ( $new as &$arr )
{
if ( empty( $arr[0] ) && empty( $arr[1] ) )
$arr = false;
}
$new = array_filter( $new );
return $new;
}
/**
* Normalize parameters for field
*
* @param array $field
* @return array
*/
static function normalize( $field )
{
$field = parent::normalize( $field );
$field['clone'] = true;
$field['multiple'] = true;
return $field;
}
/**
* Output the field value
* Display unordered list of key - value pairs
*
* @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
*/
static function the_value( $field, $args = array(), $post_id = null )
{
$value = self::get_value( $field, $args, $post_id );
if ( ! is_array( $value ) )
return '';
$output = '<ul>';
foreach ( $value as $subvalue )
{
$output .= sprintf( '<li><label>%s</label>: %s</li>', $subvalue[0], $subvalue[1] );
}
$output .= '</ul>';
return $output;
}
}
@@ -0,0 +1,162 @@
<?php
/**
* Map field class.
*/
class MASHSB_RWMB_Map_Field extends MASHSB_RWMB_Field
{
/**
* Enqueue scripts and styles
*
* @return void
*/
static function admin_enqueue_scripts()
{
/**
* Allows developers load more libraries via a filter.
* @link https://developers.google.com/maps/documentation/javascript/libraries
*/
$google_maps_url = apply_filters( 'rwmb_google_maps_url', 'https://maps.google.com/maps/api/js?sensor=false' );
wp_register_script( 'google-maps', esc_url_raw( $google_maps_url ), array(), '', true );
wp_enqueue_style( 'mashsb-rwmb-map', MASHSB_RWMB_CSS_URL . 'map.css' );
wp_enqueue_script( 'mashsb-rwmb-map', MASHSB_RWMB_JS_URL . 'map.js', array( 'jquery-ui-autocomplete', 'google-maps' ), MASHSB_RWMB_VER, true );
}
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function html( $meta, $field )
{
$html = '<div class="mashsb-rwmb-map-field">';
$html .= sprintf(
'<div class="mashsb-rwmb-map-canvas" data-default-loc="%s"></div>
<input type="hidden" name="%s" class="mashsb-rwmb-map-coordinate" value="%s">',
esc_attr( $field['std'] ),
esc_attr( $field['field_name'] ),
esc_attr( $meta )
);
if ( $address = $field['address_field'] )
{
$html .= sprintf(
'<button class="button mashsb-rwmb-map-goto-address-button" value="%s">%s</button>',
is_array( $address ) ? implode( ',', $address ) : $address,
__( 'Find Address', 'meta-box' )
);
}
$html .= '</div>';
return $html;
}
/**
* Normalize parameters for field
*
* @param array $field
*
* @return array
*/
static function normalize( $field )
{
$field = parent::normalize( $field );
$field = wp_parse_args( $field, array(
'std' => '',
'address_field' => '',
) );
return $field;
}
/**
* 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 Array(latitude, longitude, zoom)
*/
static function get_value( $field, $args = array(), $post_id = null )
{
$value = parent::get_value( $field, $args, $post_id );
list( $latitude, $longitude, $zoom ) = explode( ',', $value . ',,' );
return compact( 'latitude', 'longitude', 'zoom' );
}
/**
* Output the field value
* Display Google maps
*
* @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['latitude'] || ! $value['longitude'] )
{
return '';
}
if ( ! $value['zoom'] )
{
$value['zoom'] = 14;
}
/**
* Enqueue scripts
* Note: We still can enqueue script which outputs in the footer
*/
/**
* Allows developers load more libraries via a filter.
* @link https://developers.google.com/maps/documentation/javascript/libraries
*/
$google_maps_url = apply_filters( 'rwmb_google_maps_url', 'https://maps.google.com/maps/api/js?sensor=false' );
wp_register_script( 'google-maps', esc_url_raw( $google_maps_url ), array(), '', true );
wp_enqueue_script( 'mashsb-rwmb-map-frontend', MASHSB_RWMB_JS_URL . 'map-frontend.js', array( 'google-maps' ), '', true );
// Map parameters
$args = wp_parse_args( $args, array(
'latitude' => $value['latitude'],
'longitude' => $value['longitude'],
'width' => '100%',
'height' => '480px',
'marker' => true, // Display marker?
'marker_title' => '', // Marker title, when hover
'info_window' => '', // Content of info window (when click on marker). HTML allowed
'js_options' => array(),
) );
/**
* Google Maps options
* Option name is the same as specified in Google Maps documentation
* This array will be convert to Javascript Object and pass as map options
* @link https://developers.google.com/maps/documentation/javascript/reference
*/
$args['js_options'] = wp_parse_args( $args['js_options'], array(
// Default to 'zoom' level set in admin, but can be overwritten
'zoom' => $value['zoom'],
// Map type, see https://developers.google.com/maps/documentation/javascript/reference#MapTypeId
'mapTypeId' => 'ROADMAP',
) );
$output = sprintf(
'<div class="mashsb-rwmb-map-canvas" data-map_options="%s" style="width:%s;height:%s"></div>',
esc_attr( wp_json_encode( $args ) ),
esc_attr( $args['width'] ),
esc_attr( $args['height'] )
);
return $output;
}
}
@@ -0,0 +1,152 @@
<?php
/**
* Media field class which users WordPress media popup to upload and select files.
*/
class MASHSB_RWMB_Media_Field extends MASHSB_RWMB_Field
{
/**
* Enqueue scripts and styles
*
* @return void
*/
static function admin_enqueue_scripts()
{
wp_enqueue_media();
wp_enqueue_style( 'mashsb-rwmb-media', MASHSB_RWMB_CSS_URL . 'media.css', array(), MASHSB_RWMB_VER );
wp_enqueue_script( 'mashsb-rwmb-media', MASHSB_RWMB_JS_URL . 'media.js', array( 'jquery-ui-sortable', 'underscore', 'backbone' ), MASHSB_RWMB_VER, true );
wp_localize_script( 'mashsb-rwmb-media', 'i18nRwmbMedia', array(
'add' => apply_filters( 'rwmb_media_add_string', _x( 'Add Image', 'media', 'meta-box' ) ),
'single' => apply_filters( 'rwmb_media_single_files_string', _x( ' file', 'media', 'meta-box' ) ),
'multiple' => apply_filters( 'rwmb_media_multiple_files_string', _x( ' files', 'media', 'meta-box' ) ),
'remove' => apply_filters( 'rwmb_media_remove_string', _x( 'Remove', 'media', 'meta-box' ) ),
'edit' => apply_filters( 'rwmb_media_edit_string', _x( 'Edit', 'media', 'meta-box' ) ),
'view' => apply_filters( 'rwmb_media_view_string', _x( 'View', 'media', 'meta-box' ) ),
'noTitle' => _x( 'No Title', 'media', 'meta-box' ),
'loadingUrl' => MASHSB_RWMB_URL . 'img/loader.gif',
'extensions' => self::get_mime_extensions(),
'select' => _x( 'Select Files', 'media', 'meta-box' ),
'uploadInstructions' => _x( 'Drop files here to upload', 'media', 'meta-box' )
) );
}
/**
* Add actions
*
* @return void
*/
static function add_actions()
{
// Print attachment templates
add_action( 'print_media_templates', array( __CLASS__, 'print_templates' ) );
}
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function html( $meta, $field )
{
$meta = (array) $meta;
$meta = implode( ',', $meta );
$attributes = self::get_attributes( $field, $meta );
$html = sprintf(
'<input %s>
<div class="mashsb-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;
}
/**
* Normalize parameters for field
*
* @param array $field
*
* @return array
*/
static function normalize( $field )
{
$field = parent::normalize( $field );
$field = wp_parse_args( $field, array(
'std' => array(),
'mime_type' => '',
'max_file_uploads' => 0,
'force_delete' => false,
) );
$field['multiple'] = true;
return $field;
}
/**
* Get the attributes for a field
*
* @param array $field
* @param mixed $value
*
* @return array
*/
static function get_attributes( $field, $value = null )
{
$attributes = parent::get_attributes( $field, $value );
$attributes['type'] = 'hidden';
$attributes['name'] .= ! $field['clone'] && $field['multiple'] ? '[]' : '';
$attributes['disabled'] = true;
$attributes['id'] = false;
$attributes['value'] = $value;
return $attributes;
}
static function get_mime_extensions()
{
$mime_types = wp_get_mime_types();
$extensions = array();
foreach( $mime_types as $ext => $mime )
{
$ext = explode( '|', $ext );
$extensions[ $mime ] = $ext;
$mime_parts = explode( '/', $mime );
if( empty( $extensions[ $mime_parts[0] ] ) )
$extensions[ $mime_parts[0] ] = array();
$extensions[ $mime_parts[0] ] = $extensions[ $mime_parts[0] . '/*' ] = array_merge( $extensions[ $mime_parts[0] ], $ext );
}
return $extensions;
}
/**
* Save meta value
*
* @param $new
* @param $old
* @param $post_id
* @param $field
*/
static function save( $new, $old, $post_id, $field )
{
delete_post_meta( $post_id, $field['id'] );
parent::save( $new, array(), $post_id, $field );
}
/**
* Template for media item
* @return void
*/
static function print_templates()
{
require_once( MASHSB_RWMB_INC_DIR . 'templates/media.php' );
}
}
@@ -0,0 +1,73 @@
<?php
/**
* This class implements common methods used in fields which have multiple values
* like checkbox list, autocomplete, etc.
*
* The difference when handling actions for these fields are the way they get/set
* meta value. Briefly:
* - If field is cloneable, value is saved as a single entry in the database
* - Otherwise value is saved as multiple entries
*/
abstract class MASHSB_RWMB_Multiple_Values_Field extends MASHSB_RWMB_Field
{
/**
* Normalize parameters for field
*
* @param array $field
*
* @return array
*/
static function normalize( $field )
{
$field = parent::normalize( $field );
$field['multiple'] = true;
$field['field_name'] = $field['id'];
if ( ! $field['clone'] )
$field['field_name'] .= '[]';
return $field;
}
/**
* Output the field value
* Display option name instead of option value
*
* @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 '';
$output = '<ul>';
if ( $field['clone'] )
{
foreach ( $value as $subvalue )
{
$output .= '<li>';
$output .= '<ul>';
foreach ( $subvalue as $option )
{
$output .= '<li>' . $field['options'][$option] . '</li>';
}
$output .= '</ul>';
$output .= '</li>';
}
}
else
{
foreach ( $value as $option )
{
$output .= '<li>' . $field['options'][$option] . '</li>';
}
}
$output .= '</ul>';
return $output;
}
}
@@ -0,0 +1,47 @@
<?php
/**
* Number field class.
*/
class MASHSB_RWMB_Number_Field extends MASHSB_RWMB_Input_Field
{
/**
* Normalize parameters for field
*
* @param array $field
*
* @return array
*/
static function normalize( $field )
{
$field = parent::normalize( $field );
$field = wp_parse_args( $field, array(
'step' => 1,
'min' => 0,
'max' => false,
) );
return $field;
}
/**
* Get the attributes for a field
*
* @param array $field
* @param mixed $value
*
* @return array
*/
static function get_attributes( $field, $value = null )
{
$attributes = parent::get_attributes( $field, $value );
$attributes = wp_parse_args( $attributes, array(
'step' => $field['step'],
'max' => $field['max'],
'min' => $field['min'],
) );
$attributes['type'] = 'number';
return $attributes;
}
}
@@ -0,0 +1,145 @@
<?php
/**
* Abstract field to select an object: post, user, taxonomy, etc.
*/
abstract class MASHSB_RWMB_Object_Choice_Field extends MASHSB_RWMB_Choice_Field
{
/**
* Get field HTML
*
* @param mixed $options
* @param mixed $db_fields
* @param mixed $meta
* @param array $field
* @return string
*/
public static function walk( $options, $db_fields, $meta, $field )
{
switch ( $field['field_type'] )
{
case 'checkbox_list':
case 'radio_list':
return MASHSB_RWMB_Input_List_Field::walk( $options, $db_fields, $meta, $field );
break;
case 'select_tree':
return MASHSB_RWMB_Select_Tree_Field::walk( $options, $db_fields, $meta, $field );
break;
case 'select_advanced':
return MASHSB_RWMB_Select_Advanced_Field::walk( $options, $db_fields, $meta, $field );
break;
case 'select':
default:
return MASHSB_RWMB_Select_Field::walk( $options, $db_fields, $meta, $field );
break;
}
}
/**
* Normalize parameters for field
*
* @param array $field
*
* @return array
*/
public static function normalize( $field )
{
$field = parent::normalize( $field );
$field = wp_parse_args( $field, array(
'flatten' => true,
'query_args' => array(),
'field_type' => 'select_advanced',
) );
if ( 'checkbox_tree' === $field['field_type'] )
{
$field['field_type'] = 'checkbox_list';
$field['flatten'] = false;
}
switch ( $field['field_type'] )
{
case 'checkbox_list':
case 'radio_list':
$field['multiple'] = 'radio_list' === $field['field_type'] ? false : true;
return MASHSB_RWMB_Input_List_Field::normalize( $field );
break;
case 'select_advanced':
return MASHSB_RWMB_Select_Advanced_Field::normalize( $field );
break;
case 'select_tree':
return MASHSB_RWMB_Select_Tree_Field::normalize( $field );
break;
case 'select':
default:
return MASHSB_RWMB_Select_Field::normalize( $field );
break;
}
}
/**
* Get the attributes for a field
*
* @param array $field
* @param mixed $value
*
* @return array
*/
public static function get_attributes( $field, $value = null )
{
switch ( $field['field_type'] )
{
case 'checkbox_list':
case 'radio_list':
return MASHSB_RWMB_Input_List_Field::get_attributes( $field, $value );
break;
case 'select_advanced':
$attributes = MASHSB_RWMB_Select_Advanced_Field::get_attributes( $field, $value );
$attributes['class'] .= ' mashsb-rwmb-select_advanced';
return $attributes;
break;
case 'select_tree':
return MASHSB_RWMB_Select_Tree_Field::get_attributes( $field, $value );
break;
case 'select':
default:
return MASHSB_RWMB_Select_Field::get_attributes( $field, $value );
break;
}
}
/**
* Get field names of object to be used by walker
* @return array
*/
public static function get_db_fields()
{
return array(
'parent' => '',
'id' => '',
'label' => '',
);
}
/**
* Enqueue scripts and styles
*/
public static function admin_enqueue_scripts()
{
MASHSB_RWMB_Input_List_Field::admin_enqueue_scripts();
MASHSB_RWMB_Select_Field::admin_enqueue_scripts();
MASHSB_RWMB_Select_Tree_Field::admin_enqueue_scripts();
MASHSB_RWMB_Select_Advanced_Field::admin_enqueue_scripts();
}
/**
* Get options for walker
*
* @param array $field
*
* @return array
*/
public static function get_options( $field )
{
return array();
}
}
@@ -0,0 +1,122 @@
<?php
/**
* oEmbed field class.
*/
class MASHSB_RWMB_OEmbed_Field extends MASHSB_RWMB_URL_Field
{
/**
* Enqueue scripts and styles
*/
public static function admin_enqueue_scripts()
{
wp_enqueue_style( 'mashsb-rwmb-oembed', MASHSB_RWMB_CSS_URL . 'oembed.css' );
wp_enqueue_script( 'mashsb-rwmb-oembed', MASHSB_RWMB_JS_URL . 'oembed.js', array(), MASHSB_RWMB_VER, true );
}
/**
* Add actions
*/
public static function add_actions()
{
add_action( 'wp_ajax_rwmb_get_embed', array( __CLASS__, 'wp_ajax_get_embed' ) );
}
/**
* Ajax callback for returning oEmbed HTML
*/
public static function wp_ajax_get_embed()
{
$url = (string) filter_input( INPUT_POST, 'url', FILTER_SANITIZE_URL );
wp_send_json_success( self::get_embed( $url ) );
}
/**
* Get embed html from url
*
* @param string $url
* @return string
*/
public static function get_embed( $url )
{
/**
* Set arguments for getting embeded HTML.
* Without arguments, default width will be taken from global $content_width, which can break UI in the admin
* @link https://github.com/rilwis/meta-box/issues/801
* @see WP_oEmbed::fetch()
* @see WP_Embed::shortcode()
* @see wp_embed_defaults()
*/
$args = array();
if ( is_admin() )
{
$args['width'] = 360;
}
// Try oembed first
$embed = wp_oembed_get( $url, $args );
// If no oembed provides found, try WordPress auto embed
if ( ! $embed )
{
$embed = $GLOBALS['wp_embed']->shortcode( $args, $url );
}
return $embed ? $embed : __( 'Embed HTML not available.', 'meta-box' );
}
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
* @return string
*/
public static function html( $meta, $field )
{
$attributes = self::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 ),
__( 'Preview', 'meta-box' ),
$meta ? self::get_embed( $meta ) : ''
);
}
/**
* Output the field value
* Display embed media
*
* @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
*/
public static function the_value( $field, $args = array(), $post_id = null )
{
$value = self::get_value( $field, $args, $post_id );
$output = '';
if ( empty( $value ) )
{
return $output;
}
if ( $field['clone'] )
{
$output = '<ul>';
foreach ( $value as $subvalue )
{
$output .= '<li>' . self::get_embed( $subvalue ) . '</li>';
}
$output .= '</ul>';
}
else
{
$output = self::get_embed( $value );
}
return $output;
}
}
@@ -0,0 +1,34 @@
<?php
/**
* Password field class.
*/
class MASHSB_RWMB_Password_Field extends MASHSB_RWMB_Text_Field
{
/**
* Get the attributes for a field
*
* @param array $field
* @param mixed $value
* @return array
*/
static function get_attributes( $field, $value = null )
{
$attributes = parent::get_attributes( $field, $value );
$attributes['type'] = 'password';
return $attributes;
}
/**
* Store secured password in the database.
* @param mixed $new
* @param mixed $old
* @param int $post_id
* @param array $field
* @return string
*/
static function value( $new, $old, $post_id, $field )
{
$new = $new != $old ? wp_hash_password( $new ) : $new;
return $new;
}
}
@@ -0,0 +1,5 @@
<?php
/**
* Image upload field which uses plupload library to drag and drop files to upload.
*/
class MASHSB_RWMB_Plupload_Image_Field extends MASHSB_RWMB_Image_Upload_Field {}
@@ -0,0 +1,132 @@
<?php
/**
* Post field class.
*/
class MASHSB_RWMB_Post_Field extends MASHSB_RWMB_Object_Choice_Field
{
/**
* Normalize parameters for field
*
* @param array $field
* @return array
*/
public static function normalize( $field )
{
/**
* Set default field args
*/
$field = parent::normalize( $field );
$field = wp_parse_args( $field, array(
'post_type' => 'post',
'parent' => false,
) );
if ( ! isset( $field['query_args']['post_type'] ) )
$field['query_args']['post_type'] = $field['post_type'];
/**
* Set default placeholder
* - If multiple post types: show 'Select a post'
* - If single post type: show 'Select a %post_type_name%'
*/
if ( empty( $field['placeholder'] ) )
{
$field['placeholder'] = __( 'Select a post', 'meta-box' );
if ( is_string( $field['query_args']['post_type'] ) && post_type_exists( $field['query_args']['post_type'] ) )
{
$post_type_object = get_post_type_object( $field['query_args']['post_type'] );
$field['placeholder'] = sprintf( __( 'Select a %s', 'meta-box' ), $post_type_object->labels->singular_name );
}
}
/**
* Set parent option, which will change field name to `parent_id` to save as post parent
*/
if ( $field['parent'] )
{
$field['multiple'] = false;
$field['field_name'] = 'parent_id';
}
/**
* Set default query args
*/
$field['query_args'] = wp_parse_args( $field['query_args'], array(
'post_status' => 'publish',
'posts_per_page' => - 1,
) );
return $field;
}
/**
* Get field names of object to be used by walker
*
* @return array
*/
public static function get_db_fields()
{
return array(
'parent' => 'post_parent',
'id' => 'ID',
'label' => 'post_title',
);
}
/**
* Get 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)
*
* @see "save" method for better understanding
*
* @param $post_id
* @param $saved
* @param $field
*
* @return array
*/
public static function meta( $post_id, $saved, $field )
{
if ( isset( $field['parent'] ) && $field['parent'] )
{
$post = get_post( $post_id );
return $post->post_parent;
}
return parent::meta( $post_id, $saved, $field );
}
/**
* Get options for walker
*
* @param array $field
* @return array
*/
public static function get_options( $field )
{
$query = new WP_Query( $field['query_args'] );
return $query->have_posts() ? $query->posts : array();
}
/**
* Get option label
*
* @param string $value Option value
* @param array $field Field parameter
*
* @return string
*/
public static function get_option_label( $value, $field )
{
return sprintf(
'<a href="%s" title="%s">%s</a>',
esc_url( get_permalink( $value ) ),
the_title_attribute( array(
'post' => $value,
'echo' => false,
) ),
get_the_title( $value )
);
}
}
@@ -0,0 +1,19 @@
<?php
/**
* Radio field class.
*/
class MASHSB_RWMB_Radio_Field extends MASHSB_RWMB_Input_List_Field
{
/**
* Normalize parameters for field
* @param array $field
* @return array
*/
static function normalize( $field )
{
$field['multiple'] = false;
$field = parent::normalize( $field );
return $field;
}
}
@@ -0,0 +1,93 @@
<?php
/**
* HTML5 range field class.
*/
class MASHSB_RWMB_Range_Field extends MASHSB_RWMB_Number_Field
{
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
* @return string
*/
static function html( $meta, $field )
{
$output = parent::html( $meta, $field );
$output .= sprintf( '<span class="mashsb-rwmb-output">%s</span>', $meta );
return $output;
}
/**
* Enqueue styles
*/
static function admin_enqueue_scripts()
{
wp_enqueue_style( 'mashsb-rwmb-range', MASHSB_RWMB_CSS_URL . 'range.css', array(), MASHSB_RWMB_VER );
wp_enqueue_script( 'mashsb-rwmb-range', MASHSB_RWMB_JS_URL . 'range.js', array(), MASHSB_RWMB_VER, true );
}
/**
* Normalize parameters for field.
*
* @param array $field
*
* @return array
*/
static function normalize( $field )
{
$field = wp_parse_args( $field, array(
'min' => 0,
'max' => 10,
'step' => 1,
) );
$field = parent::normalize( $field );
return $field;
}
/**
* Get the attributes for a field
*
* @param array $field
* @param mixed $value
*
* @return array
*/
static function get_attributes( $field, $value = null )
{
$attributes = parent::get_attributes( $field, $value );
$attributes['type'] = 'range';
return $attributes;
}
/**
* Ensure number in range.
*
* @param mixed $new
* @param mixed $old
* @param int $post_id
* @param array $field
*
* @return int
*/
static function value( $new, $old, $post_id, $field )
{
$new = intval( $new );
$min = intval( $field['min'] );
$max = intval( $field['max'] );
if ( $new < $min )
{
return $min;
}
elseif ( $new > $max )
{
return $max;
}
return $new;
}
}
@@ -0,0 +1,102 @@
<?php
/**
* Select advanced field which uses select2 library.
*/
class MASHSB_RWMB_Select_Advanced_Field extends MASHSB_RWMB_Select_Field
{
/**
* Enqueue scripts and styles
*/
public static function admin_enqueue_scripts()
{
parent::admin_enqueue_scripts();
wp_enqueue_style( 'mashsb-rwmb-select2', MASHSB_RWMB_CSS_URL . 'select2/select2.css', array(), '4.0.1' );
wp_enqueue_style( 'mashsb-rwmb-select-advanced', MASHSB_RWMB_CSS_URL . 'select-advanced.css', array(), MASHSB_RWMB_VER );
wp_register_script( 'mashsb-rwmb-select2', MASHSB_RWMB_JS_URL . 'select2/select2.min.js', array(), '4.0.2', true );
//Localize
$deps = array( 'mashsb-rwmb-select2', 'mashsb-rwmb-select' );
$dir = MASHSB_RWMB_JS_URL . 'select2/i18n/';
$file = str_replace( '_', '-', get_locale() );
$parts = explode( '-', $file );
$file = file_exists( MASHSB_RWMB_DIR . 'js/select2/i18n/' . $file . '.js' ) ? $file : $parts[0];
if ( file_exists( MASHSB_RWMB_DIR . 'js/select2/i18n/' . $file . '.js' ) )
{
wp_register_script( 'mashsb-rwmb-select2-i18n', $dir . $file . '.js', array( 'mashsb-rwmb-select2' ), '4.0.2', true );
$deps[] = 'mashsb-rwmb-select2-i18n';
}
wp_enqueue_script( 'mashsb-rwmb-select', MASHSB_RWMB_JS_URL . 'select.js', array(), MASHSB_RWMB_VER, true );
wp_enqueue_script( 'mashsb-rwmb-select-advanced', MASHSB_RWMB_JS_URL . 'select-advanced.js', $deps, MASHSB_RWMB_VER, true );
}
/**
* 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( MASHSB_RW_Meta_Box::get_class_name( $field ), 'get_attributes' ), $field, $meta );
$walker = new MASHSB_RWMB_Select_Walker( $db_fields, $field, $meta );
$output = sprintf(
'<select %s>',
self::render_attributes( $attributes )
);
$output .= '<option></option>';
$output .= $walker->walk( $options, $field['flatten'] ? - 1 : 0 );
$output .= '</select>';
$output .= self::get_select_all_html( $field );
return $output;
}
/**
* Normalize parameters for field
*
* @param array $field
* @return array
*/
public static function normalize( $field )
{
$field = wp_parse_args( $field, array(
'js_options' => array(),
'placeholder' => 'Select an item',
) );
$field = parent::normalize( $field );
$field['js_options'] = wp_parse_args( $field['js_options'], array(
'allowClear' => true,
'width' => 'none',
'placeholder' => $field['placeholder'],
) );
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 = parent::get_attributes( $field, $value );
$attributes = wp_parse_args( $attributes, array(
'data-options' => wp_json_encode( $field['js_options'] ),
) );
return $attributes;
}
}
@@ -0,0 +1,64 @@
<?php
/**
* Select tree field class.
*/
class MASHSB_RWMB_Select_Tree_Field extends MASHSB_RWMB_Select_Field
{
/**
* 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 )
{
$walker = new MASHSB_RWMB_Select_Tree_Walker( $db_fields, $field, $meta );
return $walker->walk( $options );
}
/**
* Enqueue scripts and styles
*/
public static function admin_enqueue_scripts()
{
parent::admin_enqueue_scripts();
wp_enqueue_style( 'mashsb-rwmb-select-tree', MASHSB_RWMB_CSS_URL . 'select-tree.css', array( 'mashsb-rwmb-select' ), MASHSB_RWMB_VER );
wp_enqueue_script( 'mashsb-rwmb-select-tree', MASHSB_RWMB_JS_URL . 'select-tree.js', array( 'mashsb-rwmb-select' ), MASHSB_RWMB_VER, true );
}
/**
* Normalize parameters for field
*
* @param array $field
* @return array
*/
public static function normalize( $field )
{
$field['multiple'] = true;
$field['size'] = 0;
$field = parent::normalize( $field );
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 = parent::get_attributes( $field, $value );
$attributes['multiple'] = false;
$attributes['id'] = false;
return $attributes;
}
}
@@ -0,0 +1,95 @@
<?php
/**
* Select field class.
*/
class MASHSB_RWMB_Select_Field extends MASHSB_RWMB_Choice_Field
{
/**
* Enqueue scripts and styles
*/
public static function admin_enqueue_scripts()
{
wp_enqueue_style( 'mashsb-rwmb-select', MASHSB_RWMB_CSS_URL . 'select.css', array(), MASHSB_RWMB_VER );
wp_enqueue_script( 'mashsb-rwmb-select', MASHSB_RWMB_JS_URL . 'select.js', array(), MASHSB_RWMB_VER, true );
}
/**
* 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( MASHSB_RW_Meta_Box::get_class_name( $field ), 'get_attributes' ), $field, $meta );
$walker = new MASHSB_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;
}
/**
* Normalize parameters for field
*
* @param array $field
* @return array
*/
public static function normalize( $field )
{
$field = parent::normalize( $field );
$field = $field['multiple'] ? MASHSB_RWMB_Multiple_Values_Field::normalize( $field ) : $field;
$field = wp_parse_args( $field, array(
'size' => $field['multiple'] ? 5 : 0,
'select_all_none' => false,
) );
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 = parent::get_attributes( $field, $value );
$attributes = wp_parse_args( $attributes, array(
'multiple' => $field['multiple'],
'size' => $field['size'],
) );
return $attributes;
}
/**
* Get html for select all|none for multiple select
*
* @param array $field
* @return string
*/
public static function get_select_all_html( $field )
{
if ( $field['multiple'] && $field['select_all_none'] )
{
return '<div class="mashsb-rwmb-select-all-none">' . __( 'Select', 'meta-box' ) . ': <a data-type="all" href="#">' . __( 'All', 'meta-box' ) . '</a> | <a data-type="none" href="#">' . __( 'None', 'meta-box' ) . '</a></div>';
}
return '';
}
}
@@ -0,0 +1,66 @@
<?php
/**
* jQueryUI slider field class.
*/
class MASHSB_RWMB_Slider_Field extends MASHSB_RWMB_Field
{
/**
* Enqueue scripts and styles
*
* @return void
*/
static function admin_enqueue_scripts()
{
$url = MASHSB_RWMB_CSS_URL . 'jqueryui';
wp_enqueue_style( 'jquery-ui-core', "{$url}/jquery.ui.core.css", array(), '1.8.17' );
wp_enqueue_style( 'jquery-ui-theme', "{$url}/jquery.ui.theme.css", array(), '1.8.17' );
wp_enqueue_style( 'jquery-ui-slider', "{$url}/jquery.ui.slider.css", array(), '1.8.17' );
wp_enqueue_style( 'mashsb-rwmb-slider', MASHSB_RWMB_CSS_URL . 'slider.css' );
wp_enqueue_script( 'mashsb-rwmb-slider', MASHSB_RWMB_JS_URL . 'slider.js', array( 'jquery-ui-slider', 'jquery-ui-widget', 'jquery-ui-mouse', 'jquery-ui-core' ), MASHSB_RWMB_VER, true );
}
/**
* Get div HTML
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function html( $meta, $field )
{
return sprintf(
'<div class="clearfix">
<div class="mashsb-rwmb-slider" id="%s" data-options="%s"></div>
<span class="mashsb-rwmb-slider-value-label">%s<span>%s</span>%s</span>
<input type="hidden" name="%s" value="%s" class="mashsb-rwmb-slider-value">
</div>',
$field['id'], esc_attr( wp_json_encode( $field['js_options'] ) ),
$field['prefix'], $meta, $field['suffix'],
$field['field_name'], $meta
);
}
/**
* Normalize parameters for field
*
* @param array $field
*
* @return array
*/
static function normalize( $field )
{
$field = parent::normalize( $field );
$field = wp_parse_args( $field, array(
'prefix' => '',
'suffix' => '',
'js_options' => array(),
) );
$field['js_options'] = wp_parse_args( $field['js_options'], array(
'range' => 'min', // range = 'min' will add a dark background to sliding part, better UI
) );
return $field;
}
}
@@ -0,0 +1,89 @@
<?php
/**
* Taxonomy advanced field class which saves terms' IDs in the post meta.
*/
class MASHSB_RWMB_Taxonomy_Advanced_Field extends MASHSB_RWMB_Taxonomy_Field
{
/**
* Get meta values to save
* Save terms in custom field, no more by setting post terms
* Save in form of comma-separated IDs
*
* @param mixed $new
* @param mixed $old
* @param int $post_id
* @param array $field
*
* @return string
*/
static function value( $new, $old, $post_id, $field )
{
return is_array( $new ) ? implode( ',', array_unique( $new ) ) : null;
}
/**
* Save meta value
*
* @param mixed $new
* @param mixed $old
* @param int $post_id
* @param array $field
*
* @return string
*/
static function save( $new, $old, $post_id, $field )
{
if ( $new )
update_post_meta( $post_id, $field['id'], $new );
else
delete_post_meta( $post_id, $field['id'] );
}
/**
* Standard meta retrieval
*
* @param int $post_id
* @param bool $saved
* @param array $field
*
* @return array
*/
static function meta( $post_id, $saved, $field )
{
$meta = get_post_meta( $post_id, $field['id'], true );
$meta = wp_parse_id_list( $meta );
return $meta;
}
/**
* Get the field value
* Return list of post term objects
*
* @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 array List of post term objects
*/
static function get_value( $field, $args = array(), $post_id = null )
{
if ( ! $post_id )
$post_id = get_the_ID();
$value = self::meta( $post_id, '', $field );
// Allow to pass more arguments to "get_terms"
$args = wp_parse_args( array(
'include' => $value,
'hide_empty' => false,
), $args );
$value = get_terms( $field['taxonomy'], $args );
// Get single value if necessary
if ( ! $field['clone'] && ! $field['multiple'] )
{
$value = reset( $value );
}
return $value;
}
}
@@ -0,0 +1,164 @@
<?php
/**
* Taxonomy field class which set post terms when saving.
*/
class MASHSB_RWMB_Taxonomy_Field extends MASHSB_RWMB_Object_Choice_Field
{
/**
* Add default value for 'taxonomy' field
*
* @param $field
* @return array
*/
static function normalize( $field )
{
/**
* Backwards compatibility with field args
*/
if ( isset( $field['options']['args'] ) )
$field['query_args'] = $field['options']['args'];
if ( isset( $field['options']['taxonomy'] ) )
$field['taxonomy'] = $field['options']['taxonomy'];
if ( isset( $field['options']['type'] ) )
$field['field_type'] = $field['options']['type'];
/**
* Set default field args
*/
$field = parent::normalize( $field );
$field = wp_parse_args( $field, array(
'taxonomy' => 'category',
) );
/**
* Set default query args
*/
$field['query_args'] = wp_parse_args( $field['query_args'], array(
'hide_empty' => false,
) );
/**
* Set default placeholder
* - If multiple taxonomies: show 'Select a term'
* - If single taxonomy: show 'Select a %taxonomy_name%'
*/
if ( empty( $field['placeholder'] ) )
{
$field['placeholder'] = __( 'Select a term', 'meta-box' );
if ( is_string( $field['taxonomy'] ) && taxonomy_exists( $field['taxonomy'] ) )
{
$taxonomy_object = get_taxonomy( $field['taxonomy'] );
$field['placeholder'] = sprintf( __( 'Select a %s', 'meta-box' ), $taxonomy_object->labels->singular_name );
}
}
/**
* Prevent cloning for taxonomy field
*/
$field['clone'] = false;
return $field;
}
/**
* Get field names of object to be used by walker
*
* @return array
*/
static function get_db_fields()
{
return array(
'parent' => 'parent',
'id' => 'term_id',
'label' => 'name',
);
}
/**
* Get options for selects, checkbox list, etc via the terms
*
* @param array $field Field parameters
*
* @return array
*/
static function get_options( $field )
{
$options = get_terms( $field['taxonomy'], $field['query_args'] );
return $options;
}
/**
* Save meta value
*
* @param mixed $new
* @param mixed $old
* @param int $post_id
* @param array $field
*
* @return string
*/
static function save( $new, $old, $post_id, $field )
{
$new = array_unique( array_map( 'intval', (array) $new ) );
$new = empty( $new ) ? null : $new;
wp_set_object_terms( $post_id, $new, $field['taxonomy'] );
}
/**
* Standard meta retrieval
*
* @param int $post_id
* @param bool $saved
* @param array $field
*
* @return array
*/
static function meta( $post_id, $saved, $field )
{
$meta = get_the_terms( $post_id, $field['taxonomy'] );
$meta = (array) $meta;
$meta = wp_list_pluck( $meta, 'term_id' );
return $meta;
}
/**
* Get the field value
* Return list of post term objects
*
* @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 array List of post term objects
*/
static function get_value( $field, $args = array(), $post_id = null )
{
$value = get_the_terms( $post_id, $field['taxonomy'] );
// Get single value if necessary
if ( ! $field['clone'] && ! $field['multiple'] && is_array( $value ) )
{
$value = reset( $value );
}
return $value;
}
/**
* Get option label
*
* @param string $value Option value
* @param array $field Field parameter
*
* @return string
*/
static function get_option_label( $value, $field )
{
return sprintf(
'<a href="%s" title="%s">%s</a>',
esc_url( get_term_link( $value ) ),
esc_attr( $value->name ),
$value->name
);
}
}
@@ -0,0 +1,91 @@
<?php
/**
* Text list field class.
*/
class MASHSB_RWMB_Text_List_Field extends MASHSB_RWMB_Multiple_Values_Field
{
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function html( $meta, $field )
{
$html = array();
$input = '<label><input type="text" class="mashsb-rwmb-text-list" name="%s" value="%s" placeholder="%s"> %s</label>';
$count = 0;
foreach ( $field['options'] as $placeholder => $label )
{
$html[] = sprintf(
$input,
$field['field_name'],
isset( $meta[$count] ) ? esc_attr( $meta[$count] ) : '',
$placeholder,
$label
);
$count ++;
}
return implode( ' ', $html );
}
/**
* Output the field value
* Display option name instead of option value
*
* @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 '';
$output = '<ul>';
if ( $field['clone'] )
{
foreach ( $value as $subvalue )
{
$output .= '<li>';
$output .= '<ul>';
$i = 0;
foreach ( $field['options'] as $placeholder => $label )
{
$output .= sprintf(
'<li><label>%s</label>: %s</li>',
$label,
isset( $subvalue[$i] ) ? $subvalue[$i] : ''
);
$i ++;
}
$output .= '</ul>';
$output .= '</li>';
}
}
else
{
$i = 0;
foreach ( $field['options'] as $placeholder => $label )
{
$output .= sprintf(
'<li><label>%s</label>: %s</li>',
$label,
isset( $value[$i] ) ? $value[$i] : ''
);
$i ++;
}
}
$output .= '</ul>';
return $output;
}
}
@@ -0,0 +1,48 @@
<?php
/**
* Text field class.
*/
class MASHSB_RWMB_Text_Field extends MASHSB_RWMB_Input_Field
{
/**
* Normalize parameters for field
*
* @param array $field
* @return array
*/
static function normalize( $field )
{
$field = parent::normalize( $field );
$field = wp_parse_args( $field, array(
'size' => 30,
'maxlength' => false,
'pattern' => false,
) );
return $field;
}
/**
* Get the attributes for a field
*
* @param array $field
* @param mixed $value
*
* @return array
*/
static function get_attributes( $field, $value = null )
{
$attributes = parent::get_attributes( $field, $value );
$attributes = wp_parse_args( $attributes, array(
'size' => $field['size'],
'maxlength' => $field['maxlength'],
'pattern' => $field['pattern'],
'placeholder' => $field['placeholder'],
) );
$attributes['type'] = 'text';
return $attributes;
}
}
@@ -0,0 +1,79 @@
<?php
/**
* Textarea field class.
*/
class MASHSB_RWMB_Textarea_Field extends MASHSB_RWMB_Field
{
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function html( $meta, $field )
{
$attributes = self::get_attributes( $field, $meta );
return sprintf(
'<textarea %s>%s</textarea>',
self::render_attributes( $attributes ),
$meta
);
}
/**
* Escape meta for field output
*
* @param mixed $meta
* @return mixed
*/
static function esc_meta( $meta )
{
return is_array( $meta ) ? array_map( 'esc_textarea', $meta ) : esc_textarea( $meta );
}
/**
* Normalize parameters for field
*
* @param array $field
* @return array
*/
static function normalize( $field )
{
$field = parent::normalize( $field );
$field = wp_parse_args( $field, array(
'cols' => 60,
'rows' => 3,
'maxlength' => false,
'wrap' => false,
'readonly' => false,
) );
return $field;
}
/**
* Get the attributes for a field
*
* @param array $field
* @param mixed $value
*
* @return array
*/
static function get_attributes( $field, $value = null )
{
$attributes = parent::get_attributes( $field, $value );
$attributes = wp_parse_args( $attributes, array(
'cols' => $field['cols'],
'rows' => $field['rows'],
'maxlength' => $field['maxlength'],
'wrap' => $field['wrap'],
'readonly' => $field['readonly'],
'placeholder' => $field['placeholder'],
) );
$attributes['class'] .= ' large-text';
return $attributes;
}
}
@@ -0,0 +1,79 @@
<?php
/**
* Image upload field which uses thickbox library to upload.
* @deprecated Use image_advanced instead
*/
class MASHSB_RWMB_Thickbox_Image_Field extends MASHSB_RWMB_Image_Field
{
/**
* Add custom actions for the field.
*/
public static function add_actions()
{
parent::add_actions();
add_filter( 'get_media_item_args', array( __CLASS__, 'allow_img_insertion' ) );
}
/**
* Always enable insert to post button in the popup
* @link https://github.com/rilwis/meta-box/issues/809
* @link http://wordpress.stackexchange.com/q/22175/2051
* @param array $vars
* @return array
*/
public static function allow_img_insertion( $vars )
{
$vars['send'] = true; // 'send' as in "Send to Editor"
return $vars;
}
/**
* Enqueue scripts and styles
*/
public static function admin_enqueue_scripts()
{
parent::admin_enqueue_scripts();
add_thickbox();
wp_enqueue_script( 'media-upload' );
wp_enqueue_script( 'mashsb-rwmb-thickbox-image', MASHSB_RWMB_JS_URL . 'thickbox-image.js', array( 'jquery' ), MASHSB_RWMB_VER, true );
}
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
public static function html( $meta, $field )
{
$i18n_title = apply_filters( 'rwmb_thickbox_image_upload_string', _x( 'Upload Images', 'image upload', 'meta-box' ), $field );
// Uploaded images
$html = parent::get_uploaded_images( $meta, $field );
// Show form upload
$html .= "<a href='#' class='button mashsb-rwmb-thickbox-upload' data-field_id='{$field['id']}'>{$i18n_title}</a>";
return $html;
}
/**
* Get field value
* It's the combination of new (uploaded) images and saved images
*
* @param array $new
* @param array $old
* @param int $post_id
* @param array $field
*
* @return array
*/
public static function value( $new, $old, $post_id, $field )
{
return array_unique( array_merge( $old, $new ) );
}
}
@@ -0,0 +1,31 @@
<?php
/**
* Time field class.
*/
class MASHSB_RWMB_Time_Field extends MASHSB_RWMB_Datetime_Field
{
/**
* Enqueue scripts and styles
*
* @return void
*/
public static function admin_enqueue_scripts()
{
parent::admin_register_scripts();
wp_enqueue_style( 'jquery-ui-timepicker' );
wp_enqueue_script( 'mashsb-rwmb-time' );
}
/**
* Normalize parameters for field
*
* @param array $field
* @return array
*/
public static function normalize( $field )
{
$field = parent::normalize( $field );
$field['js_options']['timeFormat'] = empty( $field['format'] ) ? $field['js_options']['timeFormat'] : $field['format'];
return $field;
}
}
@@ -0,0 +1,37 @@
<?php
/**
* HTML5 URL field class.
*/
class MASHSB_RWMB_URL_Field extends MASHSB_RWMB_Text_Field
{
/**
* Get the attributes for a field
*
* @param array $field
* @param mixed $value
*
* @return array
*/
static function get_attributes( $field, $value = null )
{
$attributes = parent::get_attributes( $field, $value );
$attributes['type'] = 'url';
return $attributes;
}
/**
* Sanitize url
*
* @param mixed $new
* @param mixed $old
* @param int $post_id
* @param array $field
*
* @return string
*/
static function value( $new, $old, $post_id, $field )
{
return is_array( $new ) ? array_map( 'esc_url_raw', $new ) : esc_url_raw( $new );
}
}
@@ -0,0 +1,89 @@
<?php
/**
* User field class.
*/
class MASHSB_RWMB_User_Field extends MASHSB_RWMB_Object_Choice_Field
{
/**
* Normalize parameters for field
*
* @param array $field
*
* @return array
*/
static function normalize( $field )
{
/**
* Set default field args
*/
$field = parent::normalize( $field );
/**
* Prevent select tree for user since it's not hierarchical
*/
$field['field_type'] = 'select_tree' === $field['field_type'] ? 'select' : $field['field_type'];
/**
* Set to always flat
*/
$field['flatten'] = true;
/**
* Set default placeholder
*/
$field['placeholder'] = empty( $field['placeholder'] ) ? __( 'Select an user', 'meta-box' ) : $field['placeholder'];
/**
* Set default query args
*/
$field['query_args'] = wp_parse_args( $field['query_args'], array(
'orderby' => 'display_name',
'order' => 'asc',
'role' => '',
'fields' => 'all',
) );
return $field;
}
/**
* Get users
*
* @param array $field
*
* @return array
*/
static function get_options( $field )
{
$options = get_users( $field['query_args'] );
return $options;
}
/**
* Get field names of object to be used by walker
*
* @return array
*/
static function get_db_fields()
{
return array(
'parent' => 'parent',
'id' => 'ID',
'label' => 'display_name',
);
}
/**
* Get option label
*
* @param string $value Option value
* @param array $field Field parameter
*
* @return string
*/
static function get_option_label( $value, $field )
{
$user = get_userdata( $value );
return '<a href="' . get_author_posts_url( $value ) . '">' . $user->display_name . '</a>';
}
}
@@ -0,0 +1,43 @@
<?php
/**
* Custom HTML field class.
*/
class MASHSB_RWMB_Validate_Og_Field extends MASHSB_RWMB_Field {
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function html( $meta, $field ) {
global $post;
$html = !empty( $field['std'] ) ? $field['std'] : '';
if( !empty( $field['callback'] ) && is_callable( $field['callback'] ) ) {
$html = call_user_func_array( $field['callback'], array($meta, $field) );
}
//return $html;
//return get_permalink( $post->ID );
return self::get_fb_debugger_url($post, $field);
}
/**
* Link to the open graph debugger to check if open graph tags are valid
*
* @global array $post
* @return string
*/
static function get_fb_debugger_url($post, $field) {
//var_dump( $post );
if( isset( $post ) && $post->post_status == "publish" ) {
$url = get_permalink( $post->ID );
return '<a href="https://developers.facebook.com/tools/debug/?q=' . $url . '" target="_blank" rel="noopener" class="button-small"> Validate Open Graph data </a>' . $field['helper'];
} else {
return '';
}
}
}
@@ -0,0 +1,106 @@
<?php
/**
* WYSIWYG (editor) field class.
*/
class MASHSB_RWMB_Wysiwyg_Field extends MASHSB_RWMB_Field
{
/**
* Array of cloneable editors.
* @var array
*/
static $cloneable_editors = array();
/**
* Enqueue scripts and styles.
*/
static function admin_enqueue_scripts()
{
wp_enqueue_style( 'mashsb-rwmb-wysiwyg', MASHSB_RWMB_CSS_URL . 'wysiwyg.css', array(), MASHSB_RWMB_VER );
wp_enqueue_script( 'mashsb-rwmb-wysiwyg', MASHSB_RWMB_JS_URL . 'wysiwyg.js', array( 'jquery' ), MASHSB_RWMB_VER, true );
}
/**
* Change field value on save
*
* @param mixed $new
* @param mixed $old
* @param int $post_id
* @param array $field
* @return string
*/
static function value( $new, $old, $post_id, $field )
{
if ( $field['raw'] )
{
$meta = $new;
}
elseif ( $field['clone'] )
{
$meta = array_map( 'wpautop', $new );
}
else
{
$meta = wpautop( $new );
}
return $meta;
}
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
* @return string
*/
static function html( $meta, $field )
{
// Using output buffering because wp_editor() echos directly
ob_start();
$field['options']['textarea_name'] = $field['field_name'];
$attributes = self::get_attributes( $field );
// Use new wp_editor() since WP 3.3
wp_editor( $meta, $attributes['id'], $field['options'] );
return ob_get_clean();
}
/**
* Escape meta for field output
*
* @param mixed $meta
* @return mixed
*/
static function esc_meta( $meta )
{
return $meta;
}
/**
* Normalize parameters for field
*
* @param array $field
* @return array
*/
static function normalize( $field )
{
$field = parent::normalize( $field );
$field = wp_parse_args( $field, array(
'raw' => false,
'options' => array(),
) );
$field['options'] = wp_parse_args( $field['options'], array(
'editor_class' => 'mashsb-rwmb-wysiwyg',
'dfw' => true, // Use default WordPress full screen UI
) );
// Keep the filter to be compatible with previous versions
$field['options'] = apply_filters( 'rwmb_wysiwyg_settings', $field['options'] );
return $field;
}
}