<?php

/**
 * Posttypes class.
 *
 * @package woowgallery
 * @author  Sergey Pasyuk
 */
namespace WoowGallery;

use  WoowGallery\Admin\Settings ;
defined( 'ABSPATH' ) || die( 'No script kiddies please!' );
/**
 * Class Posttypes
 */
class Posttypes
{
    const  GALLERY_POSTTYPE = 'woowgallery' ;
    const  ALBUM_POSTTYPE = 'woowgallery-album' ;
    const  DYNAMIC_POSTTYPE = 'woowgallery-dynamic' ;
    /**
     * Primary class constructor.
     */
    public function __construct()
    {
        $this->register_woowgallery_posttype();
        $this->register_woowgallery_dynamic_posttype();
        $this->register_woowgallery_album_posttype();
        do_action( 'register_woowgallery_posttypes' );
        
        if ( is_admin() ) {
            // Update post type messages.
            add_filter( 'post_updated_messages', [ $this, 'messages' ] );
            add_filter( 'custom_menu_order', [ $this, 'custom_menu_order' ] );
        }
    
    }
    
    /**
     * Register WoowGallery CPT.
     */
    public function register_woowgallery_posttype()
    {
        // Build the labels for the post type.
        $labels = [
            'name'                  => __( 'WoowGallery', 'woowgallery' ),
            'singular_name'         => __( 'WoowGallery', 'woowgallery' ),
            'menu_name'             => __( 'WoowGallery', 'woowgallery' ),
            'name_admin_bar'        => __( 'WoowGallery', 'woowgallery' ),
            'all_items'             => __( 'Galleries', 'woowgallery' ),
            'add_new_item'          => __( 'Add New Gallery', 'woowgallery' ),
            'add_new'               => __( 'Add New Gallery', 'woowgallery' ),
            'new_item'              => __( 'New Gallery', 'woowgallery' ),
            'edit_item'             => __( 'Edit WoowGallery', 'woowgallery' ),
            'update_item'           => __( 'Update Gallery', 'woowgallery' ),
            'view_item'             => __( 'View Gallery', 'woowgallery' ),
            'view_items'            => __( 'View Galleries', 'woowgallery' ),
            'search_items'          => __( 'Search Galleries', 'woowgallery' ),
            'not_found'             => __( 'No Galleries found.', 'woowgallery' ),
            'not_found_in_trash'    => __( 'No Galleries found in trash.', 'woowgallery' ),
            'featured_image'        => __( 'Cover Image', 'woowgallery' ),
            'set_featured_image'    => __( 'Set Cover Image', 'woowgallery' ),
            'remove_featured_image' => __( 'Remove Cover Image', 'woowgallery' ),
            'use_featured_image'    => __( 'Use as Cover Image', 'woowgallery' ),
            'insert_into_item'      => __( 'Insert into Gallery', 'woowgallery' ),
            'uploaded_to_this_item' => __( 'Uploaded to this Gallery', 'woowgallery' ),
            'items_list'            => __( 'Galleries list', 'woowgallery' ),
            'items_list_navigation' => __( 'Galleries list navigation', 'woowgallery' ),
            'filter_items_list'     => __( 'Filter Galleries list', 'woowgallery' ),
        ];
        // Build out the post type arguments.
        $args = [
            'label'               => __( 'WoowGallery', 'woowgallery' ),
            'labels'              => $labels,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'menu_icon'           => 'data:image/svg+xml;base64, ',
            'menu_position'       => 10,
            'show_in_admin_bar'   => true,
            'show_in_nav_menus'   => false,
            'can_export'          => true,
            'supports'            => [
            'title',
            'excerpt',
            'thumbnail',
            'author',
            'custom-fields'
        ],
            'hierarchical'        => false,
            'public'              => false,
            'has_archive'         => false,
            'exclude_from_search' => true,
            'publicly_queryable'  => false,
            'query_var'           => false,
            'rewrite'             => false,
            'show_in_rest'        => true,
        ];
        // Define custom capaibilities.
        $args['capabilities'] = $this->capabilities();
        // Filter arguments.
        $args = apply_filters( 'woowgallery_posttype_args', $args, self::GALLERY_POSTTYPE );
        // Register the post type with WordPress.
        register_post_type( self::GALLERY_POSTTYPE, $args );
    }
    
    /**
     * Custom capabilities for WoowGallery CPT.
     *
     * @return array.
     */
    private function capabilities()
    {
        return [
            'edit_post'              => 'edit_woowgallery_post',
            'read_post'              => 'read_woowgallery_post',
            'delete_post'            => 'delete_woowgallery_post',
            'edit_posts'             => 'edit_woowgallery_posts',
            'edit_others_posts'      => 'edit_others_woowgallery_posts',
            'publish_posts'          => 'publish_woowgallery_posts',
            'read_private_posts'     => 'read_private_woowgallery_posts',
            'read'                   => 'read_woowgallery',
            'delete_posts'           => 'delete_woowgallery_posts',
            'delete_private_posts'   => 'delete_private_woowgallery_posts',
            'delete_published_posts' => 'delete_published_woowgallery_posts',
            'delete_others_posts'    => 'delete_others_woowgallery_posts',
            'edit_private_posts'     => 'edit_private_woowgallery_posts',
            'edit_published_posts'   => 'edit_published_woowgallery_posts',
            'create_posts'           => 'create_woowgallery_posts',
        ];
    }
    
    /**
     * Register WoowGallery CPT.
     */
    public function register_woowgallery_dynamic_posttype()
    {
        // Build the labels for the post type.
        $labels = [
            'name'                  => __( 'WoowGallery Dynamic', 'woowgallery' ),
            'singular_name'         => __( 'WoowGallery Dynamic', 'woowgallery' ),
            'menu_name'             => __( 'WoowGallery Dynamic', 'woowgallery' ),
            'name_admin_bar'        => __( 'WoowGallery Dynamic', 'woowgallery' ),
            'all_items'             => __( 'Dynamic Galleries', 'woowgallery' ),
            'add_new_item'          => __( 'Add New Dynamic Gallery', 'woowgallery' ),
            'add_new'               => __( 'Add New Dynamic Gallery', 'woowgallery' ),
            'new_item'              => __( 'New Dynamic Gallery', 'woowgallery' ),
            'edit_item'             => __( 'Edit WoowGallery', 'woowgallery' ),
            'update_item'           => __( 'Update Dynamic Gallery', 'woowgallery' ),
            'view_item'             => __( 'View Dynamic Gallery', 'woowgallery' ),
            'view_items'            => __( 'View Dynamic Galleries', 'woowgallery' ),
            'search_items'          => __( 'Search Dynamic Galleries', 'woowgallery' ),
            'not_found'             => __( 'No Dynamic Galleries found.', 'woowgallery' ),
            'not_found_in_trash'    => __( 'No Dynamic Galleries found in trash.', 'woowgallery' ),
            'featured_image'        => __( 'Cover Image', 'woowgallery' ),
            'set_featured_image'    => __( 'Set Cover Image', 'woowgallery' ),
            'remove_featured_image' => __( 'Remove Cover Image', 'woowgallery' ),
            'use_featured_image'    => __( 'Use as Cover Image', 'woowgallery' ),
            'items_list'            => __( 'Dynamic Galleries list', 'woowgallery' ),
            'items_list_navigation' => __( 'Dynamic Galleries list navigation', 'woowgallery' ),
            'filter_items_list'     => __( 'Filter Dynamic Galleries list', 'woowgallery' ),
        ];
        // Build out the post type arguments.
        $args = [
            'label'               => __( 'WoowGallery Dynamic', 'woowgallery' ),
            'labels'              => $labels,
            'show_ui'             => true,
            'show_in_menu'        => 'edit.php?post_type=' . self::GALLERY_POSTTYPE,
            'menu_icon'           => 'none',
            'menu_position'       => 10,
            'show_in_admin_bar'   => true,
            'show_in_nav_menus'   => false,
            'can_export'          => true,
            'supports'            => [
            'title',
            'excerpt',
            'thumbnail',
            'author',
            'custom-fields'
        ],
            'hierarchical'        => false,
            'public'              => false,
            'has_archive'         => false,
            'exclude_from_search' => true,
            'publicly_queryable'  => false,
            'query_var'           => false,
            'rewrite'             => false,
            'show_in_rest'        => true,
        ];
        // Define custom capaibilities.
        $args['capabilities'] = $this->capabilities();
        // Filter arguments.
        $args = apply_filters( 'woowgallery_posttype_args', $args, self::DYNAMIC_POSTTYPE );
        // Register the post type with WordPress.
        register_post_type( self::DYNAMIC_POSTTYPE, $args );
        // "Add New" submenu.
        add_action( 'admin_menu', [ $this, 'dynamic_new_submenu' ], 11 );
    }
    
    /**
     * Register WoowGallery Album CPT.
     */
    public function register_woowgallery_album_posttype()
    {
        // Build the labels for the post type.
        $labels = [
            'name'                  => __( 'WoowGallery Album', 'woowgallery' ),
            'singular_name'         => __( 'WoowGallery Album', 'woowgallery' ),
            'menu_name'             => __( 'WoowGallery Album', 'woowgallery' ),
            'name_admin_bar'        => __( 'WoowGallery Album', 'woowgallery' ),
            'all_items'             => __( 'Albums', 'woowgallery' ),
            'add_new_item'          => __( 'Add New Album', 'woowgallery' ),
            'add_new'               => __( 'Add New Album', 'woowgallery' ),
            'new_item'              => __( 'New Album', 'woowgallery' ),
            'edit_item'             => __( 'Edit WoowGallery Album', 'woowgallery' ),
            'update_item'           => __( 'Update Album', 'woowgallery' ),
            'view_item'             => __( 'View Album', 'woowgallery' ),
            'view_items'            => __( 'View Albums', 'woowgallery' ),
            'search_items'          => __( 'Search Albums', 'woowgallery' ),
            'not_found'             => __( 'No Albums found.', 'woowgallery' ),
            'not_found_in_trash'    => __( 'No Albums found in trash.', 'woowgallery' ),
            'featured_image'        => __( 'Cover Image', 'woowgallery' ),
            'set_featured_image'    => __( 'Set Cover Image', 'woowgallery' ),
            'remove_featured_image' => __( 'Remove Cover Image', 'woowgallery' ),
            'use_featured_image'    => __( 'Use as Cover Image', 'woowgallery' ),
            'insert_into_item'      => __( 'Insert into Album', 'woowgallery' ),
            'uploaded_to_this_item' => __( 'Uploaded to this Album', 'woowgallery' ),
            'items_list'            => __( 'Albums list', 'woowgallery' ),
            'items_list_navigation' => __( 'Albums list navigation', 'woowgallery' ),
            'filter_items_list'     => __( 'Filter Albums list', 'woowgallery' ),
        ];
        // Build out the post type arguments.
        $args = [
            'label'               => __( 'WoowGallery Album', 'woowgallery' ),
            'labels'              => $labels,
            'show_ui'             => true,
            'show_in_menu'        => 'edit.php?post_type=' . self::GALLERY_POSTTYPE,
            'menu_icon'           => 'none',
            'menu_position'       => 10,
            'show_in_admin_bar'   => true,
            'show_in_nav_menus'   => false,
            'can_export'          => true,
            'supports'            => [
            'title',
            'excerpt',
            'thumbnail',
            'author',
            'custom-fields'
        ],
            'hierarchical'        => false,
            'public'              => false,
            'has_archive'         => false,
            'exclude_from_search' => true,
            'publicly_queryable'  => false,
            'query_var'           => false,
            'rewrite'             => false,
            'show_in_rest'        => true,
        ];
        // Define custom capaibilities.
        $args['capabilities'] = $this->capabilities();
        // Filter arguments.
        $args = apply_filters( 'woowgallery_posttype_args', $args, self::ALBUM_POSTTYPE );
        // Register the post type with WordPress.
        register_post_type( self::ALBUM_POSTTYPE, $args );
        // "Add New" submenu.
        add_action( 'admin_menu', [ $this, 'album_new_submenu' ], 11 );
    }
    
    /**
     * Add "Add New Album" submenu.
     */
    public function dynamic_new_submenu()
    {
        $obj = get_post_type_object( self::DYNAMIC_POSTTYPE );
        add_submenu_page(
            $obj->show_in_menu,
            $obj->labels->add_new_item,
            $obj->labels->add_new,
            $obj->cap->edit_post,
            'post-new.php?post_type=' . self::DYNAMIC_POSTTYPE
        );
    }
    
    /**
     * Add "Add New Album" submenu.
     */
    public function album_new_submenu()
    {
        $obj = get_post_type_object( self::ALBUM_POSTTYPE );
        add_submenu_page(
            $obj->show_in_menu,
            $obj->labels->add_new_item,
            $obj->labels->add_new,
            $obj->cap->edit_post,
            'post-new.php?post_type=' . self::ALBUM_POSTTYPE
        );
    }
    
    /**
     * Contextualizes the post updated messages.
     *
     * @param array $messages Array of default post updated messages.
     *
     * @return array $messages Amended array of post updated messages.
     */
    public function messages( $messages )
    {
        global  $post ;
        $revision = woowgallery_GET( 'revision' );
        // Contextualize the messages for WoowGallery Galleries.
        $woowgallery_messages = [
            0  => '',
            1  => __( 'Gallery updated.', 'woowgallery' ),
            2  => __( 'Gallery custom field updated.', 'woowgallery' ),
            3  => __( 'Gallery custom field deleted.', 'woowgallery' ),
            4  => __( 'Gallery updated.', 'woowgallery' ),
            5  => ( $revision ? sprintf( __( 'Gallery restored to revision from %s.', 'woowgallery' ), wp_post_revision_title( (int) $revision, false ) ) : false ),
            6  => __( 'Gallery published.', 'woowgallery' ),
            7  => __( 'Gallery saved.', 'woowgallery' ),
            8  => __( 'Gallery submitted.', 'woowgallery' ),
            9  => sprintf( __( 'WoowGallery scheduled for: <strong>%1$s</strong>.', 'woowgallery' ), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ) ),
            10 => __( 'Gallery draft updated.', 'woowgallery' ),
        ];
        $messages[self::GALLERY_POSTTYPE] = $woowgallery_messages;
        $messages[self::DYNAMIC_POSTTYPE] = $woowgallery_messages;
        $messages[self::ALBUM_POSTTYPE] = $woowgallery_messages;
        return $messages;
    }
    
    /**
     * Reorder WoowGallery submenus.
     *
     * @param bool $custom Whether custom ordering is enabled.
     *
     * @return bool
     */
    public function custom_menu_order( $custom )
    {
        // Get submenu key location based on slug.
        global  $submenu ;
        if ( empty($submenu['edit.php?post_type=' . self::GALLERY_POSTTYPE]) ) {
            return $custom;
        }
        $wg_submenu = $submenu['edit.php?post_type=' . self::GALLERY_POSTTYPE];
        $wg_menu_order = [];
        $wg_menu_last = [];
        foreach ( $wg_submenu as $details ) {
            $url = (array) explode( '?', $details[2], 2 );
            
            if ( 'edit.php' === $url[0] ) {
                $details[0] = '<span style="margin-right: 10px;">' . $details[0] . '</span></a></li>';
                $details[0] .= '<li style="position:absolute; right:0; transform: translateY(-100%);" class="wg-cpt-add-new"><a style="padding-left:5px; padding-right:3px;" href="post-new.php?' . $url[1] . '" title="' . esc_attr__( 'Add New', 'woowgallery' ) . '"><span style="transform: translateY(2px);" class="dashicons dashicons-plus"></span>';
                $wg_menu_order[$url[1]][] = $details;
            } elseif ( 'post-new.php' !== $url[0] ) {
                $wg_menu_last[] = $details;
            }
        
        }
        $wg_menu_order['last'] = $wg_menu_last;
        $wg_menu_order = call_user_func_array( 'array_merge', $wg_menu_order );
        $submenu['edit.php?post_type=' . self::GALLERY_POSTTYPE] = apply_filters( 'woowgallery_submenu_order', $wg_menu_order );
        // Return the new submenu order.
        return $custom;
    }
    
    /**
     * Filter post type arguments.
     *
     * @param array  $args     Post type arguments.
     * @param string $posttype Post type name.
     *
     * @return array
     */
    public function standalone( $args, $posttype )
    {
        return $args;
    }

}