Nael
5/26/2018 - 2:14 PM

Wordpress custom post formats via custom fields

Wordpress custom post formats via custom fields

<?php

/**
 * Retrieves custom post format. Custom post formats created by custom post fields
 */
function get_custom_post_format( $post_id = null, $args = null ){
	if ( ! $post_id ){
		$post = get_post();
		$post_id = $post->ID;
		if( ! $post_id )
			return false;
	}

	$post_format = get_post_meta($post_id, 'post_format', true);
	if( $args['exclude'] && $args['exclude'] == $post_format )
		$post_format = '';
	$post_format = filter_post_format($post_format);
	return $post_format;
}

/**
 * Wrapper for post format slug filter
 */
function filter_post_format( $post_format ){
	global $PostFormats;
	$post_format = $PostFormats->filter_post_format($post_format);
	return $post_format;
}

class Custom_Post_Formats {

	private $post_formats = array();
  
  private $textdomain = 'textdomain';

	public function __construct() {
    // Because we use translation function we fill $post_formats here
		$this->post_formats = array(
			'featured' => _x('Featured', 'Post format', $this->textdomain),
			'normal'   => _x('Normal', 'Post format', $this->textdomain),
			'no-image' => _x('No image', 'Post format', $this->textdomain)
		);

		add_action('add_meta_boxes', [$this, 'add_metabox'], 1);
		add_action('save_post', [$this, 'save_metabox'], 10);
	}

	/**
	 * Validate format value. Works like filter.
	 *
	 * @param string	$check_format_slug
	 *
	 * @return string	Returns only existing post format or 'normal' as default.
	 */
	public function filter_post_format($check_format_slug = NULL){
		if( $check_format_slug && array_key_exists($check_format_slug, $this->post_formats) )
			return $check_format_slug;
		else
			return 'normal';
	}

	/**
	 * Add custom post formats metabox to post
	 */
	public function add_metabox(){
		add_meta_box(
			'custom_post_formats',
			__('Post Format', $this->textdomain),
			[$this, 'metabox_html'],
			'post',
			'side',
			'core'
		);
	}

	/**
	 * Fires when post saves
	 *
	 * @param $post_id
	 */
	public function save_metabox($post_id){
		// Check if user has permissions to save data.
		// Checks save status
		if ( ! current_user_can( 'edit_post', $post_id ) || wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) {
			return;
		}

		// Checks for input and sanitizes/saves if needed
		if( isset( $_POST[ 'custom_post_format' ] ) ) {

			update_post_meta( $post_id, 'post_format', $this->filter_post_format( $_POST[ 'custom_post_format' ] ) );
		}
	}

	/**
	 * Displays custom post formats metabox html
	 *
	 * @param $post
	 */
	public function metabox_html($post){
		$value = get_post_meta($post->ID, 'post_format', true);
		?>
		<div id="post-formats-select">
			<fieldset>
				<legend class="screen-reader-text"><?php _e('Post Formats', $this->textdomain); ?></legend>
				<?php foreach( $this->post_formats as $slug => $title ) : ?>
					<input type="radio" name="custom_post_format" value="<?php esc_attr_e($slug); ?>" class="post-format" id="post-format-<?php esc_attr_e($slug); ?>" <?php checked($value, $slug); ?> />
					<label for="post-format-<?php esc_attr_e($slug); ?>"><?php esc_attr_e($title); ?></label><br/>
				<?php endforeach; ?>
			</fieldset>
		</div>
		<?php
	}
}

$PostFormats = new Custom_Post_Formats();