<?php
/**
 * @version    $Id$
 * @package    IT_Air
 * @author     InnoThemes Team <support@innothemes.com>
 * @copyright  Copyright (C) 2012 InnoThemes.com. All Rights Reserved.
 * @license    GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
 *
 * Websites: http://www.innothemes.com
 */

/**
 * Custom post type initialization.
 *
 * @package  IT_Air
 * @since    1.0.0
 */
class IT_Air_Init_Post_Type {
	/**
	 * Hook into WordPress.
	 *
	 * @return  void
	 */
	public static function hook() {
		// Register action to add custom post type
		static $registered;

		if ( ! isset( $registered ) ) {
			// Register 3 customer post types: 'member', 'customer' and 'portfolio'
			foreach ( array( 'member', 'customer', 'portfolio' ) as $type ) {
				$ucfirst = ucfirst( $type );

				IT_Init_Post_Type::add(
					array(
						'slug'    => $type,
						'options' => array(
							'labels' => array(
								'name'               => __( $ucfirst,                    IT_THEME_TEXTDOMAIN ),
								'singular_name'      => __( $ucfirst,                    IT_THEME_TEXTDOMAIN ),
								'add_new'            => __( "Add {$ucfirst}",            IT_THEME_TEXTDOMAIN ),
								'add_new_item'       => __( "Add New {$ucfirst}",        IT_THEME_TEXTDOMAIN ),
								'all_items'          => __( "All {$ucfirst}s",           IT_THEME_TEXTDOMAIN ),
								'edit_item'          => __( "Edit {$ucfirst}",           IT_THEME_TEXTDOMAIN ),
								'new_item'           => __( "Add New {$ucfirst}",        IT_THEME_TEXTDOMAIN ),
								'view_item'          => __( "View {$ucfirst}",           IT_THEME_TEXTDOMAIN ),
								'search_items'       => __( "Search {$ucfirst}",         IT_THEME_TEXTDOMAIN ),
								'not_found'          => __( "No {$type} found",          IT_THEME_TEXTDOMAIN ),
								'not_found_in_trash' => __( "No {$type} found in trash", IT_THEME_TEXTDOMAIN ),
							),
							'public'          => true,
							'show_ui'         => true,
							'capability_type' => 'post',
							'hierarchical'    => false,
							'rewrite'         => true,
							'menu_position'   => 30,
							'has_archive'	  => true,
							'supports'        => array( 'title', 'thumbnail' ),
						),
						'meta_boxes'      => array(
							array(
								'id'            => "{$type}_meta",
								'title'         => __( "$ucfirst Images", IT_THEME_TEXTDOMAIN ),
								'callback'      => array( __CLASS__, "render_meta_box_{$type}" ),
								'context'       => 'normal',
								'priority'      => 'high',
								'save_post'     => array( __CLASS__, "save_meta_box_{$type}" ),
								'do_meta_boxes' => array( __CLASS__, "do_meta_boxes_{$type}" ),
							),
						),
						'enter_title_here' => array( __CLASS__, 'default_title' ),
						'post_updated_messages' => array( __CLASS__, "updated_messages_{$type}" ),
					)
				);
			}
		}
	}

	/**
	 * Change default post title.
	 *
	 * @param   array   $title  Default title.
	 *
	 * @param
	 */
	public static function default_title( $title ) {
		$screen = get_current_screen();

		if ( in_array( $screen->post_type, array( 'member', 'customer', 'portfolio' ) ) ) {
			$title = __( "Enter {$screen->post_type} name here", IT_THEME_TEXTDOMAIN );
		}

		return $title;
	}

	/**
	 * Post updated messages.
	 *
	 * @param   string  $type      Either 'member', 'customer' or 'portfolio'.
	 * @param   array   $messages  Default messages.
	 *
	 * @return  array
	 */
	public static function updated_messages( $type, $messages ) {
		global $post, $post_ID;

		// Define messages
		$ucfirst = ucfirst( $type );

		$messages[ $type ] = array(
			0 => '', // Unused. Messages start at index 1.
			1 => sprintf(
				__( "{$ucfirst} updated. <a href='%s'>View {$type}</a>", IT_THEME_TEXTDOMAIN ),
				esc_url( get_permalink( $post_ID ) )
			),
			2 => __( 'Custom field updated.', IT_THEME_TEXTDOMAIN ),
			3 => __( 'Custom field deleted.', IT_THEME_TEXTDOMAIN ),
			4 => __( "{$ucfirst} updated.", IT_THEME_TEXTDOMAIN ),
			5 => isset( $_GET['revision'] )
				? sprintf(
					__( "{$ucfirst} restored to revision from %s", IT_THEME_TEXTDOMAIN ),
					wp_post_revision_title( (int) $_GET['revision'], false )
				)
				: false,
			6 => sprintf(
				__( "{$ucfirst} published. <a href='%s'>View {$type}</a>", IT_THEME_TEXTDOMAIN ),
				esc_url( get_permalink( $post_ID ) )
			),
			7 => __( "{$ucfirst} saved.", IT_THEME_TEXTDOMAIN ),
			8 => sprintf(
				__( "{$ucfirst} submitted. <a target='_blank' href='%s'>Preview {$type}</a>", IT_THEME_TEXTDOMAIN ),
				esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) )
			),
			9 => sprintf(
				__( "{$ucfirst} scheduled for: <strong>%s</strong>. <a target='_blank' href='%s'>Preview {$type}</a>", IT_THEME_TEXTDOMAIN ),
				date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink( $post_ID ) )
			),
			10 => sprintf(
				__( "{$ucfirst} draft updated. <a target='_blank' href='%s'>Preview {$type}</a>", IT_THEME_TEXTDOMAIN ),
				esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) )
			),
		);

		return $messages;
	}

	/**
	 * Render meta box.
	 *
	 * @param   string   $type  Either 'member', 'customer' or 'portfolio'.
	 * @param   WP_Post  $post  The object for the current post.
	 *
	 * @return  void
	 */
	public static function render_meta_box( $type, $post ) {
		// Get current meta data
		$meta = get_post_meta( $post->ID, "{$type}_meta", true );

		// Define form fields
		$ucfirst = ucfirst( $type );

		$form = array(
			'fields' => array(
				'url' => array(
					'type'    => 'text',
					'name'    => "{$type}_meta[url]",
					'label'   => __( "{$ucfirst} Image", IT_THEME_TEXTDOMAIN ),
					'default' => isset( $meta['url'] ) ? $meta['url'] : '',
				),
				'urlimg' => array(
					'type'    => 'text',
					'name'    => "{$type}_meta[urlimg]",
					'label'   => __( "Link to {$ucfirst}", IT_THEME_TEXTDOMAIN ),
					'default' => isset( $meta['urlimg'] ) ? $meta['urlimg'] : '',
				),
			)
		);

		// Init form renderer
		$form = IT_Form::get_instance( "it-air-{$type}-meta-box", $form );

		// Render HTML form
		$form->render( 'horizontal', array( 'tips' ) );
	}

	/**
	 * Save meta box.
	 *
	 * @param   string   $type     Either 'member', 'customer' or 'portfolio'.
	 * @param   integer  $post_id  Post id.
	 *
	 * @return  mixed
	 */
	public static function save_meta_box( $type, $post_id ) {
		return ( @is_array( $_POST["{$type}_meta"] ) && count( $_POST["{$type}_meta"] ) ) ? $_POST["{$type}_meta"] : false;
	}

	/**
	 * Do customer meta boxes.
	 *
	 * @param   string  $type  Either 'member', 'customer' or 'portfolio'.
	 *
	 * @return  mixed
	 */
	public static function do_meta_boxes( $type ) {
		// Remove the Featured Image meta box at the right side
		remove_meta_box( 'postimagediv', $type, 'side' );

		// Re-add the Featured Image meta box to below our meta box
		$ucfirst = ucfirst( $type );

		add_meta_box( 'postimagediv', __( "{$ucfirst} Images", IT_THEME_TEXTDOMAIN ), 'post_thumbnail_meta_box', $type, 'normal', 'high' );
	}

	/**
	 * Handle custom post type manipulation.
	 *
	 * @param   string  $name  Name of function to be called.
	 * @param   array   $args  Arguments to be passed.
	 *
	 * @return  mixed
	 */
	public static function __callStatic( $name, $args ) {
		if ( preg_match( '/^(updated_messages|render_meta_box|save_meta_box|do_meta_boxes)_(.+)$/', $name, $match ) ) {
			// Update arguments
			array_unshift( $args, $match[2] );

			// Execute requested function
			return call_user_func_array( array( __CLASS__, $match[1] ), $args );
		}
	}
}
