<?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'            =>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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;
	}
}
