<?php
/**
 * Real Testimonials admin class file.
 *
 * @link http://shapedplugin.com
 * @since 2.0.0
 *
 * @package Testimonial_free.
 * @subpackage Testimonial_free/Admin.
 */

namespace ShapedPlugin\TestimonialFree\Admin;

use ShapedPlugin\TestimonialFree\Admin\DBUpdates;
use ShapedPlugin\TestimonialFree\Admin\HelpPage\Help;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * The Admin class.
 */
class Admin {
	/**
	 * The class constructor.
	 *
	 * @since 1.0
	 */
	public function __construct() {
		add_filter( 'init', array( $this, 'register_testimonial_post_type' ) );
		add_filter( 'init', array( $this, 'register_shortcode_post_type' ) );
		add_action( 'admin_head', array( $this, 'spt_testimonial_form_admin_head' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
		add_action( 'widgets_init', array( $this, 'sp_testimonial_free_widget' ) );
		new DBUpdates();
		// Help Page.
		Help::instance();
	}

	/**
	 * Show the form add new button if post is less then one. Otherwise, Hide it.
	 *
	 * @since 2.6.8
	 */
	public function spt_testimonial_form_admin_head() {
		$screen               = get_current_screen();
		$count_published_form = wp_count_posts( 'spt_testimonial_form' )->publish;
		$count_draft_form     = wp_count_posts( 'spt_testimonial_form' )->draft;
		$count_pending_form   = wp_count_posts( 'spt_testimonial_form' )->pending;

		if ( 'spt_testimonial_form' === $screen->post_type && $count_published_form < '1' && $count_draft_form < '1' && $count_pending_form < '1' ) {
			?><script> jQuery(document).ready(function(){
					jQuery(".post-type-spt_testimonial_form .page-title-action").css({'visibility': 'visible', 'opacity': '1'});
				});</script>
			<?php
		}
	}

	/**
	 * Enqueue admin scripts
	 *
	 * @return void
	 */
	public function admin_scripts() {
		wp_enqueue_style( 'testimonial-free-admin', SP_TFREE_URL . 'Admin/assets/css/admin.min.css', array(), SP_TFREE_VERSION );
	}

	/**
	 * Register Shortcode Post Type
	 *
	 * @return void
	 */
	public function register_shortcode_post_type() {
		$capability = apply_filters( 'sp_tfree_ui_permission', 'manage_options' );

		$show_ui = current_user_can( $capability ) ? true : false;
		register_post_type(
			'spt_shortcodes',
			array(
				'label'              => __( 'Manage Views', 'testimonial-free' ),
				'description'        => __( 'Manage Views', 'testimonial-free' ),
				'public'             => false,
				'has_archive'        => false,
				'publicly_queryable' => false,
				'show_ui'            => $show_ui,
				'show_in_menu'       => 'edit.php?post_type=spt_testimonial',
				'hierarchical'       => false,
				'query_var'          => false,
				'supports'           => array( 'title' ),
				'capability_type'    => 'post',
				'labels'             => array(
					'name'               => __( 'Manage Views', 'testimonial-free' ),
					'singular_name'      => __( 'Manage View', 'testimonial-free' ),
					'menu_name'          => __( 'Manage Views', 'testimonial-free' ),
					'add_new'            => __( 'Add New', 'testimonial-free' ),
					'add_new_item'       => __( 'Add New View', 'testimonial-free' ),
					'edit'               => __( 'Edit', 'testimonial-free' ),
					'edit_item'          => __( 'Edit View', 'testimonial-free' ),
					'new_item'           => __( 'New View', 'testimonial-free' ),
					'search_items'       => __( 'Search View', 'testimonial-free' ),
					'not_found'          => __( 'No View Found', 'testimonial-free' ),
					'not_found_in_trash' => __( 'No View Found in Trash', 'testimonial-free' ),
					'parent'             => __( 'Parent View', 'testimonial-free' ),
				),
			)
		);

		register_post_type(
			'spt_testimonial_form',
			array(
				'label'               => __( 'Form', 'testimonial-free' ),
				'description'         => __( 'Generate forms for Frontend.', 'testimonial-free' ),
				'public'              => false,
				'has_archive'         => false,
				'publicaly_queryable' => false,
				'show_ui'             => $show_ui,
				'show_in_menu'        => 'edit.php?post_type=spt_testimonial',
				'hierarchical'        => false,
				'query_var'           => false,
				'supports'            => array( 'title' ),
				'capability_type'     => 'post',
				'labels'              => array(
					'name'               => __( 'Testimonial Form', 'testimonial-free' ),
					'singular_name'      => __( 'Testimonial Form', 'testimonial-free' ),
					'menu_name'          => __( 'Testimonial Form', 'testimonial-free' ),
					'add_new'            => __( 'Add New', 'testimonial-free' ),
					'add_new_item'       => __( 'Add New Form', 'testimonial-free' ),
					'edit'               => __( 'Edit', 'testimonial-free' ),
					'edit_item'          => __( 'Edit Form', 'testimonial-free' ),
					'new_item'           => __( 'New Form', 'testimonial-free' ),
					'search_items'       => __( 'Search Forms', 'testimonial-free' ),
					'not_found'          => __( 'No Form Found', 'testimonial-free' ),
					'not_found_in_trash' => __( 'No Form Found in Trash', 'testimonial-free' ),
					'parent'             => __( 'Parent Form', 'testimonial-free' ),
				),
			)
		);

		add_action( 'pre_post_update', array( $this, 'testimonial_form_limit' ), 10, 2 );
	}

	/**
	 *
	 * Register testimonial post type for this plugin.
	 *
	 * @param int    $post_id Post ID.
	 * @param object $post Form post.
	 * @return void
	 */
	public function testimonial_form_limit( $post_id, $post ) {
		// Check if the post type is 'spt_testimonial_form'.
		if ( 'spt_testimonial_form' === get_post_type( $post_id ) ) {

			// Check if the user has already published a post of this type.
			$existing_forms = get_posts(
				array(
					'post_type'   => 'spt_testimonial_form',
					'fields'      => 'ids',
					'post_status' => 'any',
				)
			);

			// If the user has already published a post, display an error message and prevent the update.
			if ( count( $existing_forms ) > 0 && $post_id !== $existing_forms[0] ) {
				wp_die( esc_html__( 'You are only allowed to have one form in lite version.', 'testimonial-free' ) );
			}
		}
	}

	/**
	 * Register testimonial post type for this plugin.
	 *
	 * @return void
	 */
	public function register_testimonial_post_type() {

		if ( post_type_exists( 'spt_testimonial' ) ) {
			return;
		}
		$settings      = get_option( 'sp_testimonial_pro_options' );
		$singular_name = isset( $settings['tpro_singular_name'] ) ? $settings['tpro_singular_name'] : 'Testimonial';
		$plural_name   = isset( $settings['tpro_plural_name'] ) ? $settings['tpro_plural_name'] : 'Testimonials';
		$capability    = apply_filters( 'sp_tfree_ui_permission', 'manage_options' );
		$show_ui       = current_user_can( $capability ) ? true : false;
		$labels        = apply_filters(
			'sp_testimonial_post_type_labels',
			array(
				/* translators: %s is replaced with 'member plural name' */
				'name'                  => wp_sprintf( __( 'All %s', 'testimonial-free' ), esc_html( $plural_name ) ),
				'singular_name'         => $singular_name,
				'menu_name'             => __( 'Real Testimonials', 'testimonial-free' ),
				/* translators: %s is replaced with 'member plural name' */
				'all_items'             => wp_sprintf( __( 'All %s', 'testimonial-free' ), esc_html( $plural_name ) ),
				'add_new'               => __( 'Add New', 'testimonial-free' ),
				'add_new_item'          => __( 'Add New', 'testimonial-free' ),
				'edit'                  => __( 'Edit', 'testimonial-free' ),
				/* translators: %s is replaced with 'member plural name' */
				'edit_item'             => wp_sprintf( __( 'Edit %s', 'testimonial-free' ), esc_html( $singular_name ) ),
				/* translators: %s is replaced with 'member plural name' */
				'new_item'              => wp_sprintf( __( 'New %s', 'testimonial-free' ), esc_html( $singular_name ) ),
				/* translators: %s is replaced with 'member plural name' */
				'search_items'          => wp_sprintf( __( 'Search %s', 'testimonial-free' ), esc_html( $plural_name ) ),
				/* translators: %s is replaced with 'member plural name' */
				'not_found'             => wp_sprintf( __( 'No %s found', 'testimonial-free' ), esc_html( $plural_name ) ),
				/* translators: %s is replaced with 'member plural name' */
				'not_found_in_trash'    => wp_sprintf( __( 'No %s found in Trash', 'testimonial-free' ), esc_html( $plural_name ) ),
				/* translators: %s is replaced with 'member plural name' */
				'parent'                => wp_sprintf( __( 'Parent %s', 'testimonial-free' ), esc_html( $plural_name ) ),
				/* translators: %s is replaced with 'member plural name' */
				'featured_image'        => wp_sprintf( __( '%s Image', 'testimonial-free' ), esc_html( $singular_name ) ),
				/* translators: %s is replaced with 'member plural name' */
				'set_featured_image'    => wp_sprintf( __( 'Set %s image', 'testimonial-free' ), esc_html( $singular_name ) ),
				'remove_featured_image' => __( 'Remove image', 'testimonial-free' ),
				'use_featured_image'    => __( 'Use as image', 'testimonial-free' ),
			)
		);

		$args = apply_filters(
			'sp_testimonial_post_type_args',
			array(
				'label'              => $singular_name,
				/* translators: %s is replaced with 'member plural name' */
				'description'        => wp_sprintf( __( '%s custom post type.', 'testimonial-free' ), esc_html( $singular_name ) ),
				'taxonomies'         => array(),
				'public'             => false,
				'has_archive'        => false,
				'publicly_queryable' => false,
				'query_var'          => false,
				'show_ui'            => $show_ui,
				'show_in_menu'       => true,
				'menu_icon'          => 'data:image/svg+xml;base64, ',
				'show_in_nav_menus'  => true,
				'show_in_admin_bar'  => true,
				'hierarchical'       => false,
				'menu_position'      => 20,
				'supports'           => array(
					'title',
					'editor',
					'thumbnail',
				),
				'capability_type'    => 'post',
				'labels'             => $labels,
			)
		);

		register_post_type( 'spt_testimonial', $args );
	}

	/**
	 * Register the widget for the public-facing side of the site.
	 *
	 * The register_widget should have full path of namespace of the Widget class file.
	 *
	 * @param object $widget Widget instance.
	 *
	 * @since    2.0
	 */
	public function sp_testimonial_free_widget( $widget ) {
		register_widget( 'ShapedPlugin\TestimonialFree\Admin\Views\TFREE_Widget' );
		return $widget;
	}
}
