<?php
/**
 * Lesson post type collection.
 *
 * @package ST\Lms
 */

namespace ST\Lms\Collections\PostType;

use const ST\Lms\STLMS_LESSON_CPT;
use const ST\Lms\PARENT_MENU_SLUG;

/**
 * Registers the `stlms_lesson` post type.
 */
function stlms_lesson_init() {
	$capability = array(
		'edit_post'              => 'edit_lesson',
		'read_post'              => 'read_lesson',
		'delete_post'            => 'delete_lesson',
		'edit_posts'             => 'edit_lessons',
		'edit_others_posts'      => 'edit_others_lessons',
		'publish_posts'          => 'publish_lessons',
		'delete_posts'           => 'delete_lessons',
		'delete_published_posts' => 'delete_published_lessons',
		'delete_others_posts'    => 'delete_others_lessons',
		'edit_published_posts'   => 'edit_published_lessons',
		'create_posts'           => 'create_lessons',
	);
	$capability = apply_filters( 'stlms/lesson/capability', $capability );
	register_post_type(
		STLMS_LESSON_CPT,
		array(
			'labels'                => array(
				'name'                  => __( 'Lessons', 'skilltriks' ),
				'singular_name'         => __( 'Lesson', 'skilltriks' ),
				'all_items'             => __( 'Lessons', 'skilltriks' ),
				'archives'              => __( 'Lesson Archives', 'skilltriks' ),
				'attributes'            => __( 'Lesson Attributes', 'skilltriks' ),
				'insert_into_item'      => __( 'Insert into lesson', 'skilltriks' ),
				'uploaded_to_this_item' => __( 'Uploaded to this lesson', 'skilltriks' ),
				'featured_image'        => _x( 'Featured Image', 'stlms_lesson', 'skilltriks' ),
				'set_featured_image'    => _x( 'Set featured image', 'stlms_lesson', 'skilltriks' ),
				'remove_featured_image' => _x( 'Remove featured image', 'stlms_lesson', 'skilltriks' ),
				'use_featured_image'    => _x( 'Use as featured image', 'stlms_lesson', 'skilltriks' ),
				'filter_items_list'     => __( 'Filter Lessons list', 'skilltriks' ),
				'items_list_navigation' => __( 'Lessons list navigation', 'skilltriks' ),
				'items_list'            => __( 'Lessons list', 'skilltriks' ),
				'new_item'              => __( 'New lesson', 'skilltriks' ),
				'add_new'               => __( 'Add New', 'skilltriks' ),
				'add_new_item'          => __( 'Add New Lesson', 'skilltriks' ),
				'edit_item'             => __( 'Edit lesson', 'skilltriks' ),
				'view_item'             => '',
				'view_items'            => '',
				'search_items'          => __( 'Search Lessons', 'skilltriks' ),
				'not_found'             => __( 'No lessons found', 'skilltriks' ),
				'not_found_in_trash'    => __( 'No Lessons found in trash', 'skilltriks' ),
				'parent_item_colon'     => __( 'Parent lesson:', 'skilltriks' ),
				'menu_name'             => __( 'Lessons', 'skilltriks' ),
			),
			'capability_type'       => ! current_user_can( 'manage_options' ) ? 'lesson' : 'post',
			'map_meta_cap'          => true,
			'capabilities'          => ! current_user_can( 'manage_options' ) ? $capability : array(),
			'publicly_queryable'    => false,
			'public'                => true,
			'hierarchical'          => false,
			'show_in_menu'          => current_user_can( apply_filters( 'stlms/lesson_menu/capability', 'edit_lessons' ) ) || current_user_can( 'manage_options' ) ? PARENT_MENU_SLUG : false,
			'show_ui'               => true,
			'show_in_nav_menus'     => true,
			'supports'              => array( 'title', 'editor', 'revisions', 'author', 'custom-fields' ),
			'register_meta_box_cb'  => array( new \ST\Lms\Admin\MetaBoxes\Lesson(), 'register_boxes' ),
			'has_archive'           => false,
			'rewrite'               => false,
			'query_var'             => true,
			'show_in_rest'          => true,
			'rest_base'             => STLMS_LESSON_CPT,
			'rest_controller_class' => 'WP_REST_Posts_Controller',
		)
	);
}

add_action( 'init', __NAMESPACE__ . '\\stlms_lesson_init' );

/**
 * Sets the post updated messages for the `stlms_lesson` post type.
 *
 * @param  array $messages Post updated messages.
 * @return array Messages for the `stlms_lesson` post type.
 */
function stlms_lesson_updated_messages( $messages ) {
	global $post;

	$permalink = get_permalink( $post );

	$messages[ STLMS_LESSON_CPT ] = array(
		0  => '', // Unused. Messages start at index 1.
		/* translators: %s: post permalink */
		1  => sprintf( __( 'Lesson updated. <a target="_blank" href="%s">View lesson</a>', 'skilltriks' ), esc_url( $permalink ) ),
		2  => __( 'Custom field updated.', 'skilltriks' ),
		3  => __( 'Custom field deleted.', 'skilltriks' ),
		4  => __( 'Lesson updated.', 'skilltriks' ),
		/* translators: %s: date and time of the revision */
		5  => isset( $_GET['revision'] ) ? sprintf( __( 'lesson restored to revision from %s', 'skilltriks' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		/* translators: %s: post permalink */
		6  => sprintf( __( 'Lesson published. <a href="%s">View lesson</a>', 'skilltriks' ), esc_url( $permalink ) ),
		7  => __( 'Lesson saved.', 'skilltriks' ),
		/* translators: %s: post permalink */
		8  => sprintf( __( 'Lesson submitted. <a target="_blank" href="%s">Preview lesson</a>', 'skilltriks' ), esc_url( add_query_arg( 'preview', 'true', $permalink ) ) ),
		/* translators: 1: Publish box date format, see https://secure.php.net/date 2: Post permalink */
		9  => sprintf( __( 'Lesson scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview lesson</a>', 'skilltriks' ), date_i18n( __( 'M j, Y @ G:i', 'skilltriks' ), strtotime( $post->post_date ) ), esc_url( $permalink ) ),
		/* translators: %s: post permalink */
		10 => sprintf( __( 'Lesson draft updated. <a target="_blank" href="%s">Preview lesson</a>', 'skilltriks' ), esc_url( add_query_arg( 'preview', 'true', $permalink ) ) ),
	);

	return $messages;
}

add_filter( 'post_updated_messages', __NAMESPACE__ . '\\stlms_lesson_updated_messages' );

/**
 * Sets the bulk post updated messages for the `stlms_lesson` post type.
 *
 * @param  array $bulk_messages Arrays of messages, each keyed by the corresponding post type. Messages are
 *                              keyed with 'updated', 'locked', 'deleted', 'trashed', and 'untrashed'.
 * @param  int[] $bulk_counts   Array of item counts for each message, used to build internationalized strings.
 * @return array Bulk messages for the `stlms_lesson` post type.
 */
function stlms_lesson_bulk_updated_messages( $bulk_messages, $bulk_counts ) {
	global $post;

	$bulk_messages[ STLMS_LESSON_CPT ] = array(
		/* translators: %s: Number of Lessons. */
		'updated'   => _n( '%s Lesson updated.', '%s Lessons updated.', $bulk_counts['updated'], 'skilltriks' ),
		'locked'    => ( 1 === $bulk_counts['locked'] ) ? __( '1 lesson not updated, somebody is editing it.', 'skilltriks' ) :
						/* translators: %s: Number of Lessons. */
						_n( '%s Lesson not updated, somebody is editing it.', '%s Lessons not updated, somebody is editing them.', $bulk_counts['locked'], 'skilltriks' ),
		/* translators: %s: Number of Lessons. */
		'deleted'   => _n( '%s Lesson permanently deleted.', '%s Lessons permanently deleted.', $bulk_counts['deleted'], 'skilltriks' ),
		/* translators: %s: Number of Lessons. */
		'trashed'   => _n( '%s Lesson moved to the Trash.', '%s Lessons moved to the Trash.', $bulk_counts['trashed'], 'skilltriks' ),
		/* translators: %s: Number of Lessons. */
		'untrashed' => _n( '%s Lesson restored from the Trash.', '%s Lessons restored from the Trash.', $bulk_counts['untrashed'], 'skilltriks' ),
	);

	return $bulk_messages;
}

add_filter( 'bulk_post_updated_messages', __NAMESPACE__ . '\\stlms_lesson_bulk_updated_messages', 10, 2 );
