select.php 4.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
<?php
// Prevent loading this file directly
defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'WCQD_METABOX_Select_Field' ) )
{
	class WCQD_METABOX_Select_Field extends WCQD_METABOX_Field
	{
		/**
		 * Enqueue scripts and styles
		 *
		 * @return void
		 */
		static function admin_enqueue_scripts()
		{
			wp_enqueue_style( 'rwmb-select', WCQD_METABOX_CSS_URL . 'select.css', array(), WCQD_METABOX_VER );
			wp_enqueue_script( 'rwmb-select', WCQD_METABOX_JS_URL . 'select.js', array(), WCQD_METABOX_VER, true );
		}

		/**
		 * Get field HTML
		 *
		 * @param mixed $meta
		 * @param array $field
		 *
		 * @return string
		 */
		static function html( $meta, $field )
		{
			$html = sprintf(
				'<select class="rwmb-select" name="%s" id="%s" size="%s"%s>',
				$field['field_name'],
				$field['id'],
				$field['size'],
				$field['multiple'] ? ' multiple' : ''
			);

			$html .= self::options_html( $field, $meta );

			$html .= '</select>';

			$html .= self::get_select_all_html( $field['multiple'] );

			return $html;
		}

		/**
		 * Save meta value
		 * If field is cloneable, value is saved as a single entry in DB
		 * Otherwise value is saved as multiple entries (for backward compatibility)
		 *
		 * @param $new
		 * @param $old
		 * @param $post_id
		 * @param $field
		 *
		 * @return void
		 */
		static function save( $new, $old, $post_id, $field )
		{
			if ( ! $field['clone'] )
			{
				parent::save( $new, $old, $post_id, $field );

				return;
			}

			if ( empty( $new ) )
				delete_post_meta( $post_id, $field['id'] );
			else
				update_post_meta( $post_id, $field['id'], $new );
		}

		/**
		 * Normalize parameters for field
		 *
		 * @param array $field
		 *
		 * @return array
		 */
		static function normalize_field( $field )
		{
			$field = wp_parse_args( $field, array(
				'size' => $field['multiple'] ? 5 : 0,
			) );
			if ( ! $field['clone'] && $field['multiple'] )
				$field['field_name'] .= '[]';

			return $field;
		}

		/**
		 * Creates html for options
		 *
		 * @param array $field
		 * @param mixed $meta
		 *
		 * @return array
		 */
		static function options_html( $field, $meta )
		{
			$html = '';
			if ( $field['placeholder'] )
			{
				$show_placeholder = ( 'select' === $field['type'] ) // Normal select field
					|| ( isset( $field['field_type'] ) && 'select' === $field['field_type'] ) // For 'post' field
					|| ( isset( $field['display_type'] ) && 'select' === $field['display_type'] ); // For 'taxonomy' field
				$html             = $show_placeholder ? "<option value=''>{$field['placeholder']}</option>" : '<option></option>';
			}

			$option = '<option value="%s"%s>%s</option>';

			foreach ( $field['options'] as $value => $label )
			{
				$html .= sprintf(
					$option,
					$value,
					selected( in_array( $value, (array) $meta ), true, false ),
					$label
				);
			}

			return $html;
		}

		/**
		 * Output the field value
		 * Display unordered list of option labels, not option values
		 *
		 * @param  array    $field   Field parameters
		 * @param  array    $args    Additional arguments. Not used for these fields.
		 * @param  int|null $post_id Post ID. null for current post. Optional.
		 *
		 * @return string Link(s) to post
		 */
		static function the_value( $field, $args = array(), $post_id = null )
		{
			$value = self::get_value( $field, $args, $post_id );
			if ( ! $value )
				return '';

			$function = array( RW_Meta_Box::get_class_name( $field ), 'get_option_label' );

			if ( $field['clone'] )
			{
				$output = '<ul>';
				if ( $field['multiple'] )
				{
					foreach ( $value as $subvalue )
					{
						$output .= '<li>';
						array_walk_recursive( $subvalue, $function, $field );
						$output .= '<ul><li>' . implode( '</li><li>', $subvalue ) . '</li></ul>';
						$output .= '</li>';
					}
				}
				else
				{
					array_walk_recursive( $value, $function, $field );
					$output = '<li>' . implode( '</li><li>', $value ) . '</li>';
				}
				$output .= '</ul>';
			}
			else
			{
				if ( $field['multiple'] )
				{
					array_walk_recursive( $value, $function, $field );
					$output = '<ul><li>' . implode( '</li><li>', $value ) . '</li></ul>';
				}
				else
				{
					call_user_func_array( $function, array( &$value, 0, $field ) );
					$output = $value;
				}
			}

			return $output;
		}

		/**
		 * Get option label to display in the frontend
		 *
		 * @param int   $value Option value
		 * @param int   $index Array index
		 * @param array $field Field parameter
		 *
		 * @return string
		 */
		static function get_option_label( &$value, $index, $field )
		{
			$value = $field['options'][$value];
		}

		/**
		 * Get html for select all|none for multiple select
		 *
		 * @param $multiple
		 *
		 * @return string
		 */
		static function get_select_all_html( $multiple )
		{
			if ( $multiple === true )
			{
				return '<div class="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 '';
		}
	}
}