<?php
    /**
     * Add post meta in posts
     * 
     * @package Blog Bee
     * @since 1.0.0
     */
    namespace BBE\Admin;
    require_once get_template_directory() . '/inc/meta/base-meta.php';

    use BBE\Admin\Base_Meta as Base_Meta;

    use function __;
    use function add_action;
    use function add_meta_box;
    use function checked;
    use function current_user_can;
    use function esc_attr;
    use function esc_html;
    use function esc_html__;
    use function esc_html_e;
    use function esc_url;
    use function get_post_meta;
    use function get_template_directory_uri;
    use function sanitize_text_field;
    use function update_post_meta;
    use function wp_nonce_field;
    use function wp_verify_nonce;

    if( ! class_exists( __NAMESPACE__ . '\\Post_Meta' ) ) {
        /**
         * Post_Meta class
         */
        class Post_Meta extends Base_Meta {
            /**
             * Nonce key
             * 
             * @since 1.0.0
             */
            private $nonce_key = 'bbe_post_metabox_nonce';

            /**
             * Nonce value
             * 
             * @since 1.0.0
             */
            private $nonce_value = 'bbe_post_layouts_metabox_nonce';

            /**
             * Sidebar layout key
             * 
             * @since 1.0.0
             */
            private $post_sidebar_layout_key = 'bbe_post_sidebar_layout';

            /**
             * Instance
             * 
             * @since 1.0.0
             */
            private static $instance = null;

            /**
             * Instance getter
             * 
             * @since 1.0.0
             */
            public static function get_instance() {
                if( self::$instance === null ) {
                    self::$instance = new self();
                }
                return self::$instance;
            }

            /**
             * Constructor
             * 
             * @see https://developer.wordpress.org/reference/hooks/add_meta_boxes/
             * @see https://developer.wordpress.org/reference/hooks/save_post/
             * @since 1.0.0
             */
            private function __construct() {
                add_action( 'add_meta_boxes', [ $this, 'register_meta_boxes' ] );
                add_action( 'save_post', [ $this, 'update_meta_value' ] );
            }

            /**
             * Register meta boxes
             * 
             * @see https://developer.wordpress.org/reference/functions/add_meta_box/
             * @since 1.0.0
             */
            public function register_meta_boxes() {
                $meta_box_params = [
                    'post_layouts_metabox',
                    __( 'Layouts', 'blog-bee' ),
                    [ $this, 'layouts_metabox_callback' ],
                    [ 'post', 'page' ],
                    'normal',
                    'high',
                    null
                ];
                add_meta_box( ...$meta_box_params );
            }

            /**
             * Layouts metabox callback
             * 
             * @param WP_Post $post The current post
             * @since 1.0.0
             */
            public function layouts_metabox_callback( $post ) {
                wp_nonce_field( $this->nonce_value, $this->nonce_key );
                $this->get_sidebar_layouts_html( $post, $this->post_sidebar_layout_key );
            }

            /**
             * Update meta value
             * 
             * @param int $post_id  The post being saved
             * @since 1.0.0
             */
            public function update_meta_value( $post_id ) {
                /**
                 * Nonce verification to prevent CSRF attacks
                 */
                if( ! isset( $_POST[ $this->nonce_key ] ) || ! wp_verify_nonce( $_POST[ $this->nonce_key ], $this->nonce_value ) ) {
                    return;
                }

                /**
                 * WordPress saves the post values every 60 seconds while editing post
                 * to prevent 'save_post' hook from running everytime and allow only real, intentional
                 * saves(clicking "publish"/"update") proceed
                 */
                if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

                /**
                 * Permission check
                 */
                if( ! current_user_can( 'edit_post', $post_id ) ) return;

                /**
                 * Save values
                 */
                if( isset( $_POST[ $this->post_sidebar_layout_key ] ) ) {
                    update_post_meta( $post_id, $this->post_sidebar_layout_key, sanitize_text_field( $_POST[ $this->post_sidebar_layout_key ] ) );
                }
            }
        }
        Post_Meta::get_instance();
    }