<?php

namespace ProudNerds\PostTypes;

use ProudNerds\Helpers\ArchiveHooks;
use ProudNerds\WordPress\Register\PostType;
use ProudNerds\WordPress\Register\Taxonomy;

/**
 * This file provides an example for creating post types, feel free to rename or delete this file. All functions
 * displayed are optional and filled with either the default value or an example value to pass to the function. Consult
 * the PostType class Example for more information or the WordPress developer page.
 *
 * @see https://developer.wordpress.org/reference/functions/register_post_type/
 */
final class Example
{
    /**
     * The post type slug. Give this a logical name. And make it singular! E.g.: 'event', 'project', 'team_member'.
     *
     * Note: Translate this slug by setting the rewrite slug in the registerPostType function.
     */
    public const POST_TYPE = 'my_example_post_type';

    /**
     * Initializes the class. Calls the functions required to register the post type and optional taxonomies.
     */
    public static function init()
    {
        // Create the "fake" archive page if it does not exist.
        ArchiveHooks::createArchivePageIfNotExists(
            title: __('Voorbeeld archief', 'proudnerds'),
            slug: __('voorbeeld', 'proudnerds'),
            postType: static::POST_TYPE
        );

        // Create an instance of the class to attach to hooks.
        $instance = new static();

        // Register the post type and taxonomies on the init hook.
        add_action('init', [$instance, 'registerPostType']);
        add_action('init', [$instance, 'registerTaxonomies']);
    }

    /**
     * Registers the custom post type.
     */
    public function registerPostType(): void
    {
        // Set up the post type.
        $postType = PostType::factory(static::POST_TYPE)
            ->setLabels(
                labels: __('Voorbeelden', 'proudnerds'),
                singularName: __('Voorbeeld', 'proudnerds')
            )
            ->setDescription(__('Uitleg over het voorbeeld post type', 'proudnerds'))
            ->setPublic(true)
            ->setExcludeFromSearch(false)
            ->setPubliclyQueryable(true)
            ->setShowUI(true)
            ->setShowInNavMenus(false)
            ->setShowInMenu(true)
            ->setShowInAdminBar(true)
            ->setMenuPosition(25)
            ->setMenuIcon('dashicons-warning')
            ->setHasArchive(true)
            ->setSupports(['title', 'editor', 'thumbnail', 'page-attributes', 'revision', 'excerpt'])
            ->setCapabilityType('post')
            ->setMapMetaCap(true)
            ->setHierarchical(true)
            ->setRewrite(['slug' => __('voorbeeld', 'proudnerds')])
            ->setQueryVar(true)
            ->setCanExport(true)
            ->setShowInRest(true)
            ->setRestBase('example')
            ->setRestControllerClass('WP_REST_Posts_Controller')
            ->setTaxonomies(['category', 'post_tag']);

        // Add custom fields to the post type.
        $postType->customFields()
            ->addText('test_field', [
                'label' => __('Testveld', 'proudnerds'),
            ]);

        // Register the post type.
        $postType->register();
    }

    /**
     * Registers the custom taxonomies related to the post type.
     */
    public function registerTaxonomies()
    {
        // Set up the taxonomy.
        $taxonomy = Taxonomy::factory('example_taxonomy', static::POST_TYPE)
            ->setLabels(__('Voorbeeld taxonomieën', 'proudnerds'), __('Voorbeeld taxonomie', 'proudnerds'))
            ->setDescription(__('Uitleg over de voorbeeld taxonomie', 'proudnerds'))
            ->setPublic(true)
            ->setShowUI(true)
            ->setShowInNavMenus(true)
            ->setShowInMenu(true)
            ->setShowTagcloud(true)
            ->setShowInRest(true)
            ->setShowInQuickEdit(true)
            ->setShowAdminColumn(true)
            ->setHierarchical(false)
            ->setUpdateCountCB('')
            ->setQueryVar('example-taxonomy')
            ->setRewrite(['slug' => __('voorbeeld-taxonomie', 'proudnerds')])
            ->setCapabilities([
                'manage_terms' => 'manage_categories',
                'edit_terms' => 'manage_categories',
                'delete_terms' => 'manage_categories',
                'assign_terms' => 'edit_posts',
            ])
            ->setSort(false);

        // Add custom fields to the taxonomy.
        $taxonomy->customFields()
            ->addText('test_field', [
                'label' => __('Testveld', 'proudnerds'),
            ]);

        // Register the taxonomy.
        $taxonomy->register();
    }
}
