<?php

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
defined( 'EAPSGT' ) or define( 'EAPSGT', 'eapsgt' );

add_action( 'init', 'eapsgt_post_types' );
function eapsgt_post_types() {
	// Setup labels for post
	$labels = array(
		'name'               => __( 'Easy Animated Popup', 'easy-animated-popup' ),
		'singular_name'      => __( 'Easy Animated Popup', 'easy-animated-popup' ),
		'add_new'            => __( 'Add New PopUp', 'easy-animated-popup' ),
		'add_new_item'       => __( 'Add New PopUp', 'easy-animated-popup' ),
		'edit_item'          => __( 'Edit PopUp', 'easy-animated-popup' ),
		'new_item'           => __( 'New PopUp', 'easy-animated-popup' ),
		'all_items'          => __( 'All PopUps', 'easy-animated-popup' ),
		'view_item'          => __( 'View PopUp', 'easy-animated-popup' ),
		'search_items'       => __( 'Search PopUps', 'easy-animated-popup' ),
		'not_found'          => __( 'No PopUp found', 'easy-animated-popup' ),
		'not_found_in_trash' => __( 'No PopUp found in Trash', 'easy-animated-popup' ),
		'menu_name'          => __( 'Easy Animated Popup', 'easy-animated-popup' ),
	);

	// Add needed feature(s) to post type
	$supports = array( 'title' );

	$args = array(
		'labels'          => $labels,
		'show_ui'         => true,
		'query_var'       => false,
		'rewrite'         => array( 'slug' => 'eapsgt' ),
		'capability_type' => 'post',
		'menu_position'   => 30,
		'menu_icon'       => 'dashicons-images-alt2',
		'supports'        => $supports,
	);
	register_post_type( EAPSGT, $args );

	//Custom post title
	function eapsgt_custom_post_title( $title ) {
		$screen = get_current_screen();
		if ( EAPSGT === $screen->post_type ) {
			$title = 'Popup Name';
		}

		return $title;
	}

	add_filter( 'enter_title_here', 'eapsgt_custom_post_title' );

	function eapsgt_custom_publish_box() {
		remove_meta_box( 'submitdiv', EAPSGT, 'side' );
	}

	add_action( 'add_meta_boxes_eapsgt', 'eapsgt_custom_publish_box' );

	function eapsgt_custom_layout_column( $result, $option, $user ) {
		$result = 1;

		return $result;
	}

	add_filter( "get_user_option_screen_layout_eapsgt", "eapsgt_custom_layout_column", 10, 3 );

	function eapsgt_NoVisibility() {
		echo '<style>div#visibility.misc-pub-section.misc-pub-visibility{display:none}</style>';
	}

	function eapsgt_CurrentScreenAction( $current_screen ) {
		if ( EAPSGT == $current_screen->post_type && 'post' == $current_screen->base ) {
			add_action( 'admin_head', 'eapsgt_NoVisibility' );
		}
	}

	add_action( 'current_screen', 'eapsgt_CurrentScreenAction' );
}

//manage the columns of the `page` post type
function eapsgt_manage_columns( $columns ) {
	//Modify column of sgt post
	$columns = array(
		'cb'     => $columns['cb'],
		'title'  => __( 'Popup name' ),
		'date'   => __( 'Date' ),
		'status' => __( 'Popup status' ),
	);

	return $columns;
}

add_action( 'manage_eapsgt_posts_columns', 'eapsgt_manage_columns' );

//Populate custom columns for `page` post type
function eapsgt_custom_page_columns( $column, $post_id ) {
	$the_id = get_the_ID();

	//page content column
	if ( $column === 'status' ) {
		echo '<pre><code>' . get_post_status( $the_id ) . '</code></pre>';
	}
}

add_action( 'manage_eapsgt_posts_custom_column', 'eapsgt_custom_page_columns', 10, 2 );

function eapsgt_remove_row_actions( $actions ) {
	if ( get_post_type() === EAPSGT ) {
		unset( $actions['inline hide-if-no-js'] );
	}

	return $actions;
}

add_filter( 'post_row_actions', 'eapsgt_remove_row_actions', 10, 1 );
