<?php

/**
 * Registers the Offer custom post type and its admin list table columns.
 *
 * @link       https://nudgesells.com
 * @since      1.0.0
 *
 * @package    NudgeSells
 * @subpackage NudgeSells/includes
 */

if (! defined('ABSPATH')) {
	exit;
}

/**
 * NudgeSells_CPT
 *
 * Registers the nudgesells_offer custom post type used to store every upsell offer's configuration.
 *
 * @since      1.0.0
 * @package    NudgeSells
 * @subpackage NudgeSells/includes
 * @author     NudgeSells <support@nudgesells.com>
 */
class NudgeSells_CPT
{

	const POST_TYPE = 'nudgesells_offer';

	/**
	 * Register.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public function register()
	{
		register_post_type(self::POST_TYPE, array(
			'label'               => __('Upsell Offers', 'nudgesells'),
			'labels'              => array(
				'name'               => __('Upsell Offers', 'nudgesells'),
				'singular_name'      => __('Upsell Offer', 'nudgesells'),
				'add_new_item'       => __('Add New Offer', 'nudgesells'),
				'edit_item'          => __('Edit Offer', 'nudgesells'),
				'all_items'          => __('Offers', 'nudgesells'),
				'not_found'          => __('No offers found. Create one to start showing inline upsells.', 'nudgesells'),
			),
			'public'              => false,
			'show_ui'             => true,
			'show_in_menu'        => false,
			'capability_type'     => 'post',
			'map_meta_cap'        => true,
			'supports'            => array('title'),
			'menu_icon'           => 'dashicons-megaphone',
		));
	}

	/**
	 * Columns.
	 *
	 * @param mixed $columns
	 *
	 * @return mixed
	 * @since 1.0.0
	 */
	public function columns($columns)
	{
		$new = array();
		foreach ($columns as $key => $label) {
			$new[$key] = $label;
			if ('title' === $key) {
				$new['nudgesells_scope']     = __('Scope', 'nudgesells');
				$new['nudgesells_offers']    = __('Offers', 'nudgesells');
				$new['nudgesells_priority']  = __('Priority', 'nudgesells');
			}
		}
		return $new;
	}

	/**
	 * Render column.
	 *
	 * @param mixed $column
	 * @param mixed $post_id
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public function render_column($column, $post_id)
	{
		if ('nudgesells_scope' === $column) {
			$scope = NudgeSells_Offer_Repository::get_scope($post_id);
			echo esc_html(NudgeSells_Offer_Repository::describe_scope($scope));
		}
		if ('nudgesells_offers' === $column) {
			$addons   = get_post_meta($post_id, '_nudgesells_addons', true);
			$upgrades = get_post_meta($post_id, '_nudgesells_upgrades', true);
			$parts = array();
			if (is_array($addons) && count($addons)) {
				/* translators: %d: number of add-ons configured on this offer */
				$parts[] = sprintf(_n('%d add-on', '%d add-ons', count($addons), 'nudgesells'), count($addons));
			}
			if (is_array($upgrades) && count($upgrades)) {
				/* translators: %d: number of upgrades configured on this offer */
				$parts[] = sprintf(_n('%d upgrade', '%d upgrades', count($upgrades), 'nudgesells'), count($upgrades));
			}
			echo esc_html($parts ? implode(', ', $parts) : '-');
		}
		if ('nudgesells_priority' === $column) {
			echo esc_html(get_post_meta($post_id, '_nudgesells_priority', true) ?: '10');
		}
	}

	/**
	 * Row actions.
	 *
	 * @param mixed $actions
	 * @param mixed $post
	 *
	 * @return mixed
	 * @since 1.0.0
	 */
	public function row_actions($actions, $post)
	{
		if (self::POST_TYPE === $post->post_type) {
			unset($actions['inline hide-if-no-js']);

			if (current_user_can('edit_post', $post->ID)) {
				$duplicate_url = wp_nonce_url(
					admin_url('admin-post.php?action=nudgesells_duplicate_offer&offer_id=' . $post->ID),
					'nudgesells_duplicate_offer_' . $post->ID
				);
				$actions['nudgesells_duplicate'] = sprintf(
					'<a href="%s">%s</a>',
					esc_url($duplicate_url),
					esc_html__('Duplicate', 'nudgesells')
				);
			}
		}
		return $actions;
	}
}
