Inventory_Presser_Customize_Dashboard::create_add_media_button_annotation()

create_add_media_button_annotation


Description Description

Computes the number of attachments on vehicles so the number can be shown in the classic editor near the Add Media button.


Top ↑

Return Return

(string)


Top ↑

Source Source

File: includes/class-customize-admin-dashboard.php

	function create_add_media_button_annotation()
	{
		global $post;
		if( !is_object( $post ) && isset( $_POST['post_ID'] ) )
		{
			/**
			 * This function is being called via AJAX and the
			 * post_id is incoming, so get the post
			 */
			$post = get_post( intval( $_POST['post_ID'] ) );
		}

		if( INVP::POST_TYPE != $post->post_type )
		{
			return '';
		}

		$attachments = get_children( array(
			'post_parent'    => $post->ID,
			'post_type'      => 'attachment',
			'posts_per_page' => -1,
		) );
		$counts = array(
			'image' => 0,
			'video' => 0,
			'text'  => 0,
			'PDF'   => 0,
			'other' => 0,
		);
		foreach( $attachments as $attachment )
		{
			switch( $attachment->post_mime_type )
			{
				case 'image/jpeg':
				case 'image/png':
				case 'image/gif':
					$counts['image']++;
					break;
				case 'video/mpeg':
				case 'video/mp4':
				case 'video/quicktime':
					$counts['video']++;
					break;
				case 'text/csv':
				case 'text/plain':
				case 'text/xml':
					$counts['text']++;
					break;
				case 'application/pdf':
					$counts['PDF']++;
					break;
				default:
					$counts['other']++;
					break;
			}
		}
		if( 0 < ( $counts['image'] + $counts['video'] + $counts['text'] + $counts['PDF'] + $counts['other'] ) )
		{
			$note = '';
			foreach( $counts as $key => $count )
			{
				if( 0 < $count )
				{
					if( '' != $note ) { $note .= ', '; }
					$note .= $count . ' ' . $key . ( 1 != $count ? 's' : '' );
				}
			}
			return $note;
		}
		return '0 photos';
	}