custom-field.php 10.8 KB
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
<?php
/**
 * Custom field column, displaying the contents of meta fields.
 * Suited for all storage models supporting WordPress' default way of handling meta data.
 *
 * Supports different types of meta fields, including dates, serialized data, linked content,
 * and boolean values.
 *
 * @since 1.0
 */
class CPAC_Column_Custom_Field extends CPAC_Column {

	/**
	 * @see CPAC_Column::init()
	 * @since 2.2.1
	 */
	public function init() {

		parent::init();

		// Properties
		$this->properties['type']	 		= 'column-meta';
		$this->properties['label']	 		= __( 'Custom Field', 'codepress-admin-columns' );
		$this->properties['classes']		= 'cpac-box-metafield';
		$this->properties['is_cloneable']	= true;
		$this->properties['group']			= 'custom-field';

		// Options
		$this->options['field']				= '';
		$this->options['field_type']		= '';
		$this->options['before']			= '';
		$this->options['after']				= '';

		$this->options['image_size']		= '';
		$this->options['image_size_w']		= 80;
		$this->options['image_size_h']		= 80;

		$this->options['excerpt_length']	= 15;

		$this->options['date_format']		= '';
		$this->options['date_save_format']	= '';
	}

	/**
	 * @since 3.2.1
	 */
	public function is_field_type( $type ) {
		return $type === $this->get_field_type();
	}

	/**
	 * @since 3.2.1
	 */
	public function is_field( $field ) {
		return $field === $this->get_field();
	}

	/**
	 * @since 3.2.1
	 */
	public function get_field_type() {
		return $this->options->field_type;
	}

	/**
	 * @since 3.2.1
	 */
	public function get_field() {
		return $this->options->field;
	}

	/**
	 * @see CPAC_Column::sanitize_options()
	 * @since 1.0
	 */
	public function sanitize_options( $options ) {

		if ( empty( $options['date_format'] ) ) {
			$options['date_format'] = get_option( 'date_format' );
		}

		return $options;
	}

	/**
	 * Get Custom FieldType Options - Value method
	 *
	 * @since 1.0
	 *
	 * @return array Customfield types.
	 */
	public function get_custom_field_types() {

		$custom_field_types = array(
			''				=> __( 'Default', 'codepress-admin-columns' ),
			'checkmark'		=> __( 'Checkmark (true/false)', 'codepress-admin-columns' ),
			'color'			=> __( 'Color', 'codepress-admin-columns' ),
			'count'			=> __( 'Counter', 'codepress-admin-columns' ),
			'date'			=> __( 'Date', 'codepress-admin-columns' ),
			'excerpt'		=> __( 'Excerpt'),
			'image'			=> __( 'Image', 'codepress-admin-columns' ),
			'library_id'	=> __( 'Media Library', 'codepress-admin-columns' ),
			'array'			=> __( 'Multiple Values', 'codepress-admin-columns' ),
			'numeric'		=> __( 'Numeric', 'codepress-admin-columns' ),
			'title_by_id'	=> __( 'Post Title (Post ID\'s)', 'codepress-admin-columns' ),
			'user_by_id'	=> __( 'Username (User ID\'s)', 'codepress-admin-columns' ),
			'term_by_id'	=> __( 'Term Name (Term ID\'s)', 'codepress-admin-columns' ),
		);

		// deprecated. do not use, will be removed.
		$custom_field_types = apply_filters( 'cpac_custom_field_types', $custom_field_types );

		/**
		 * Filter the available custom field types for the meta (custom field) field
		 *
		 * @since 2.0
		 *
		 * @param array $custom_field_types Available custom field types ([type] => [label])
		 */
		$custom_field_types = apply_filters( 'cac/column/meta/types', $custom_field_types );

		return $custom_field_types;
	}

	/**
	 * Get First ID from array
	 *
	 * @since 1.0
	 *
	 * @param string $meta
	 * @return string Titles
	 */
	public function get_ids_from_meta( $meta ) {

		//remove white spaces and strip tags
		$meta = $this->strip_trim( str_replace( ' ','', $meta ) );

		// var
		$ids = array();

		// check for multiple id's
		if ( strpos( $meta, ',' ) !== false )
			$ids = explode( ',', $meta );
		elseif ( is_numeric( $meta ) )
			$ids[] = $meta;

		return $ids;
	}

	/**
	 * Get Title by ID - Value method
	 *
	 * @since 1.0
	 *
	 * @param string $meta
	 * @return string Titles
	 */
	private function get_titles_by_id( $meta ) {

		$titles = array();

		// display title with link
		if ( $ids = $this->get_ids_from_meta( $meta ) ) {
			foreach ( (array) $ids as $id ) {

				if ( ! is_numeric( $id ) ) {
					continue;
				}

				$link = get_edit_post_link( $id );
				if ( $title = get_the_title( $id ) )
					$titles[] = $link ? "<a href='{$link}'>{$title}</a>" : $title;
			}
		}

		return implode('<span class="cpac-divider"></span>', $titles);
	}

	/**
	 * Get Users by ID - Value method
	 *
	 * @since 1.0
	 *
	 * @param string $meta
	 * @return string Users
	 */
	private function get_users_by_id( $meta )	{

		$names = array();

		// display username
		if ( $ids = $this->get_ids_from_meta( $meta ) ) {
			foreach ( (array) $ids as $id ) {
				if ( ! is_numeric( $id ) ) continue;

				$userdata = get_userdata( $id );
				if ( is_object( $userdata ) && ! empty( $userdata->display_name ) ) {

					// link
					$link = get_edit_user_link( $id );

					$names[] = $link ? "<a href='{$link}'>{$userdata->display_name}</a>" : $userdata->display_name;
				}
			}
		}

		return implode( '<span class="cpac-divider"></span>', $names );
	}

	/**
	 * Get Terms by ID - Value method
	 *
	 * @since 2.3.2
	 *
	 * @param array $meta_value Term ID's
	 * @return string Terms
	 */
	public function get_terms_by_id( $meta_value )	{
		// as used by Pods, @todo
		if ( ! is_array( $meta_value) || ! isset( $meta_value['term_id'] ) || ! isset( $meta_value['taxonomy'] ) ) {
			return false;
		}
		return $this->get_terms_for_display( $meta_value['term_id'], $meta_value['taxonomy'] );
	}

	/**
	 * Get meta value
	 *
	 * @since 2.0
	 *
	 * @param string $meta Contains Meta Value
	 * @param int $id Optional Object ID
	 * @return string Users
	 */
	public function get_value_by_meta( $meta, $id = null ) {

		switch ( $this->options->field_type ) :

			case "image" :
			case "library_id" :
				$meta = implode( $this->get_thumbnails( $meta, array(
					'image_size'	=> $this->options->image_size,
					'image_size_w'	=> $this->options->image_size_w,
					'image_size_h'	=> $this->options->image_size_h,
				)));
				break;

			case "excerpt" :
				$meta = $this->get_shortened_string( $meta, $this->options->excerpt_length );
				break;

			case "date" :
				$meta = $this->get_date( $meta, $this->options->date_format );
				break;

			case "title_by_id" :
				$meta = $this->get_titles_by_id( $meta );
				break;

			case "user_by_id" :
				$meta = $this->get_users_by_id( $meta );
				break;

			case "term_by_id" :
				$meta = $this->get_terms_by_id( $this->get_raw_value( $id ) );
				break;

			case "checkmark" :
				$checkmark = $this->get_asset_image( 'checkmark.png' );

				if ( empty( $meta ) || 'false' === $meta || '0' === $meta ) {
					$checkmark = '';
				}

				$meta = $checkmark;
				break;

			case "color" :
				if ( ! empty( $meta ) ) {
					$meta = $this->get_color_for_display( $meta );
				}
				break;

			case "count" :
				if ( $count = $this->get_raw_value( $id, false ) ) {
					$meta = count( $count );
				}
				break;

		endswitch;

		return $meta;
	}

	/**
	 * Get Field key
	 *
	 * @since 2.0.3
	 *
	 * @param string Custom Field Key
	 */
	public function get_field_key() {

		return substr( $this->options->field, 0, 10 ) == "cpachidden" ? str_replace( 'cpachidden', '', $this->options->field ) : $this->options->field;
	}

	/**
	 * Get meta by ID
	 *
	 * @since 1.0
	 *
	 * @param int $id ID
	 * @return string Meta Value
	 */
	public function get_meta_by_id( $id ) {

		$meta = $this->get_raw_value( $id );

		// try to turn any array into a comma seperated string for further use
		if ( ( 'array' == $this->options->field_type && is_array( $meta ) ) || is_array( $meta ) ) {
			$meta = $this->recursive_implode( ', ', $meta );
		}

		if ( ! is_string( $meta ) && ! is_numeric( $meta ) ) {
			return false;
		}

		return $meta;
	}

	/**
	 * @see CPAC_Column::get_raw_value()
	 * @since 2.0.3
	 */
	public function get_raw_value( $id, $single = true ) {

		$raw_value = '';

		if ( $field_key = $this->get_field_key() ) {
			$raw_value = get_metadata( $this->storage_model->meta_type, $id, $field_key, $single );
		}

		return apply_filters( 'cac/column/meta/raw_value', $raw_value, $id, $field_key, $this );
	}

	/**
	 * @see CPAC_Column::get_value()
	 * @since 1.0
	 */
	public function get_value( $id ) {

		$value = '';

		if ( $meta = $this->get_meta_by_id( $id ) ) {
			$value = $this->get_value_by_meta( $meta, $id );
		}

		/**
		 * Filter the display value for Custom Field columns
		 *
		 * @param mixed $value Custom field value
		 * @param int $id Object ID
		 * @param object $this Column instance
		 */
		$value = apply_filters( 'cac/column/meta/value', $value, $id, $this );

		$before = $this->get_before();
		$after 	= $this->get_after();

		// add before and after string
		if ( $value ) {
			$value = "{$before}{$value}{$after}";
		}

		return $value;
	}

	/**
	 * @since 2.4.7
	 */
	public function get_meta_keys() {
		return $this->storage_model->get_meta_keys();
	}

	/**
	 * @see CPAC_Column::display_settings()
	 * @since 1.0
	 */
	public function display_settings() { ?>
		<tr class="column_field">
			<?php $this->label_view( __( "Custom Field", 'codepress-admin-columns' ), __( "Select your custom field.", 'codepress-admin-columns' ), 'field' ); ?>
			<td class="input">

				<?php if ( $meta_keys = $this->get_meta_keys() ) : ?>
				<select name="<?php $this->attr_name( 'field' ); ?>" id="<?php $this->attr_id( 'field' ); ?>">
				<?php foreach ( $meta_keys as $field ) : ?>
					<option value="<?php echo $field ?>"<?php selected( $field, $this->options->field ) ?>><?php echo substr( $field, 0, 10 ) == "cpachidden" ? str_replace( 'cpachidden', '', $field ) : $field; ?></option>
				<?php endforeach; ?>
				</select>
				<?php else : ?>
					<?php _e( 'No custom fields available.', 'codepress-admin-columns' ); ?> <?php printf( __( 'Please create a %s item first.', 'codepress-admin-columns' ), '<em>' . $this->storage_model->singular_label . '</em>' ); ?>
				<?php endif; ?>

			</td>
		</tr>

		<tr class="column_field_type">
			<?php $this->label_view( __( "Field Type", 'codepress-admin-columns' ), __( 'This will determine how the value will be displayed.', 'codepress-admin-columns' ) . '<em>' . __( 'Type', 'codepress-admin-columns' ) . ': ' . $this->options->field_type . '</em>', 'field_type' ); ?>
			<td class="input">
				<select name="<?php $this->attr_name( 'field_type' ); ?>" id="<?php $this->attr_id( 'field_type' ); ?>">
				<?php foreach ( $this->get_custom_field_types() as $fieldkey => $fieldtype ) : ?>
					<option value="<?php echo $fieldkey ?>"<?php selected( $fieldkey, $this->options->field_type ) ?>><?php echo $fieldtype; ?></option>
				<?php endforeach; ?>
				</select>
			</td>
		</tr>

		<?php
		switch ( $this->options->field_type ) {
			case 'date':
				$this->display_field_date_format();
				break;
			 case 'image':
			 case 'library_id':
			 	$this->display_field_preview_size();
			 	break;
			 case 'excerpt':
			 	$this->display_field_excerpt_length();
			 	break;
		}

		$this->display_field_before_after();
	}

}