<?php

namespace ParcelPanel\CmsPlus;

class ParcelPanelCmsPlus
{
    private static ParcelPanelCmsPlus $instance;

    public static function instance(): ParcelPanelCmsPlus
    {
        if (!isset(self::$instance) || !(self::$instance instanceof self)) {
            self::$instance = new self();
            self::$instance->actions();
            self::$instance->filters();
            self::$instance->register_post_meta();
        }

        /**
         * Return the ParcelPanelCmsPlus Instance
         */
        return self::$instance;
    }

    /**
     * 注册 Meta field
     *
     * @return void
     */
    public function register_post_meta(): void
    {
        // 需要在 RESTful API 显示的字段
        $fields = [
            'first_field_key', 'first_field_value',
            'second_field_key', 'second_field_value',
            'third_field_key', 'third_field_value',
            'tag',
            'customer_bg_img_url', 'customer_bg_img_alt'
        ];

        foreach ($fields as $field) {
            register_post_meta(
                'page',
                $field,
                array(
                    'type' => 'string',
                    'single' => true,
                    'show_in_rest' => true,
                )
            );
        }
    }

    public function actions(): void
    {
        ActionGetPostForUrl::getInstance()->init();
        ActionGetPageTagList::getInstance()->init();
    }

    public function filters(): void
    {
    }
}
