<?php
namespace ERROPiX\AIOW;

// don't load directly
if ( ! defined( 'ABSPATH' ) ) die( '-1' );

/**
 * Post Type
*/
class Post {
	const type = 'widget';
	
	static function load() {
		add_action( 'init', __class__.'::init' );
		add_action( 'admin_init', __class__.'::admin_init', 0 );
		add_action( 'current_screen', __class__.'::current_screen' );
		add_action( 'admin_menu', __class__.'::admin_menu' );
		add_action( 'vc_after_set_mode', __class__.'::vc_after_set_mode' );
	}
	
	static function init() {
		$public = true;
		
		register_post_type( self::type, array( 
			'label'               => 'Widgets',
			'capability_type'     => 'post',
			'description'         => 'All In One Widget Content',
			'show_in_menu'        => 'themes.php',
			'public'              => true,
			'show_ui'             => true,
			'can_export'          => true,
			'publicly_queryable'  => true,
			'exclude_from_search' => true,
			'show_in_admin_bar'   => true,
			'show_in_nav_menus'   => false,
			'hierarchical'        => false,
			'has_archive'         => false,
			'rewrite'             => false,
			'supports' => array('title','editor','revisions'),
			'labels' => array (
				'name' => 'Widgets',
				'singular_name' => 'Widget',
				'menu_name' => 'All In One Widgets',
				'add_new' => 'Add New Widget',
				'add_new_item' => 'Add New Widget',
				'edit' => 'Edit',
				'edit_item' => 'Edit Widget',
				'new_item' => 'New Widget',
				'view' => 'View Widget',
				'view_item' => 'View Widget',
				'search_items' => 'Search Widgets',
				'not_found' => 'No Widgets Found',
				'not_found_in_trash' => 'No Widgets Found in Trash',
			)
		));
	}
	
	static function vc_after_set_mode() {
		if( vc_is_page_editable() ) {
			show_admin_bar(false);
			add_filter( 'template_include', __class__.'::vc_front_edit', 99 );	
		}
	}
	
	static function vc_front_edit( $template ) {
		$post_id = vc_get_param('vc_post_id');
		$query = new \WP_Query(array(
			'p' => $post_id,
			'post_type' => self::type,
		));
		
		if( $query->have_posts() ) {
			$content = vc_post_param('content');
			if( $content ) {
				$query->posts[0]->post_content = $content;
				$query->the_post();
				$template = EPX_AIOW_DIR . 'templates/vc_frontend_content.php';
			} else {
				$query->the_post();
				$template = EPX_AIOW_DIR . 'templates/vc_frontend_editor.php';
			}
		}
		
		return $template;
	}
	
	static function current_screen() {
		global $current_screen;
		
		if( $current_screen->post_type == self::type ) {
			if( Main::vc_enabled() ) {
				// vc_disable_frontend();
			}
		}
	}
	
	static function admin_init() {
		global $typenow, $vc_manager;
		
		// Enablde Visual Composer
		if( Main::vc_enabled() ) {
			$post_types = vc_settings()->get( 'content_types' );
			$vc_manager->editor_post_types = $post_types ? $post_types : $vc_manager->editorDefaultPostTypes();
			$vc_manager->editor_post_types[] = self::type;
		}
		
		// Enable Fusion builder
		if( class_exists('Fusion_Core_PageBuilder') ) {
			$FCPB = \Fusion_Core_PageBuilder::get_instance();
			$FCPB->allowed_post_types[] = self::type;
			
			$FCPB_UI = \Fusion_Core_PageBuilder_UI::get_instance();
			$FCPB_UI->settings['allowed_post_types'] = $FCPB->allowed_post_types;
		}
		
		add_action( 'submitpost_box', __class__.'::submitpost_box' );
		if( $typenow == self::type ) {
			add_filter( 'months_dropdown_results', '__return_empty_array' );
			add_filter( 'manage_posts_columns', __class__.'::columns_head' );
			add_action( 'manage_posts_custom_column', __class__.'::columns_content' ,1 ,2 );
		}
	}
	
	static function admin_menu() {
		remove_meta_box( 'submitdiv', self::type, 'side' );
	}
	
	static function submitpost_box( $post ) {
		if( $post->post_type != self::type ) return;
		
		if( $post->post_status == 'publish' ) {
			$l = __( 'Update' );
			$n = 'save';
		} else {
			$l = __( 'Publish' );
			$n = 'publish';
		}
		?>
		<div id="submitpost">
			<input name="original_publish" id="original_publish" type="hidden" value="<?php esc_attr_e($l) ?>"/>
			<input name="<?php echo $n ?>" id="publish" type="submit" class="button button-primary widefat" accesskey="p" value="<?php esc_attr_e($l) ?>"/>
		</div>
		<style type="text/css">
		#titlediv { margin-bottom: 10px; }
		#titlediv .inside { display: none; }
		#submitpost { margin-bottom: 20px; }
		#submitpost #publish { font-size: 16px; height: 38px; line-height: 34px; }
		</style>
		<?php
	}
	
	static function columns_head($columns) {
		unset($columns['date']);
		return $columns;
	}
	
	static function columns_content($column_name, $post_ID) {  
		
	}
	
	static function get_post( $id ) {
		if( function_exists('icl_object_id') ) {
			$id = icl_object_id( $id, self::type, true, ICL_LANGUAGE_CODE );
		}
		return get_post($id);
	}
	static function get_meta( $id, $key ) {
		if( function_exists('icl_object_id') ) {
			$id = icl_object_id( $id, self::type, true, ICL_LANGUAGE_CODE );
		}
		return get_post_meta( $id, $key, true );
	}
}