<?php
    /**
     * Add social media fields as meta in author
     * 
     * @package Blog Bee
     * @since 1.0.0
     */
    namespace BBE\Admin;

    if( ! class_exists( __NAMESPACE__ . '\\Author_Meta' ) ) {
        /**
         * Author_Meta class
         */
        class Author_Meta {

            private static $meta_keys = [];

            /**
             * 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
             *
             * @since 1.0.0
             */
            private function __construct() {
                add_action( 'init', [ $this, 'init' ] );
                // render meta fields
                add_action( 'show_user_profile', [ $this, 'render_input_fields' ] );
                add_action( 'edit_user_profile', [ $this, 'render_input_fields' ] );

                // update meta fields value
                add_action( 'personal_options_update', [ $this, 'update_meta_value' ] );
                add_action( 'edit_user_profile_update', [ $this, 'update_meta_value' ] );
            }

            /**
             * Initialization
             * 
             * @since 1.0.0
             */
            public function init() {
                self::$meta_keys = [
                    'facebook_url'  =>  [
                        'label' =>  __( 'Facebook Url', 'blog-bee' ),
                        'icon'  =>  'fab fa-facebook-f'
                    ],
                    'twitter_url'   =>  [
                        'label' =>  __( 'Twitter Url', 'blog-bee' ),
                        'icon'  =>  'fa-brands fa-x-twitter'
                    ],
                    'linkedin_url'  =>  [
                        'label' =>  __( 'Linkedin Url', 'blog-bee' ),
                        'icon'  =>  'fab fa-linkedin-in'
                    ],
                    'instagram_url' =>  [
                        'label' =>  __( 'Instagram Url', 'blog-bee' ),
                        'icon'  =>  'fab fa-instagram'
                    ],
                    'youtube_url'   =>  [
                        'label' =>  __( 'Youtube Url', 'blog-bee' ),
                        'icon'  =>  'fab fa-youtube'
                    ],
                    'telegram_url'  =>  [
                        'label' =>  __( 'Telegram Url', 'blog-bee' ),
                        'icon'  =>  'fab fa-telegram'
                    ],
                    'whatsapp_url'  =>  [
                        'label' =>  __( 'Whatsapp Url', 'blog-bee' ),
                        'icon'  =>  'fab fa-whatsapp'
                    ],
                    'tiktok_url'    =>  [
                        'label' =>  __( 'Tiktok Url', 'blog-bee' ),
                        'icon'  =>  'fab fa-tiktok'
                    ],
                    'wechat_url'    =>  [
                        'label' =>  __( 'Wechat Url', 'blog-bee' ),
                        'icon'  =>  'fab fa-weixin'
                    ],
                ];
            }

            /**
             * Meta keys getter
             * 
             * @since 1.0.0
             */
            public static function get_meta_keys() {
                return self::$meta_keys;
            }

            /**
             * Render input fields
             * 
             * @param WP_User $user The current WP_User object.
             * @see https://developer.wordpress.org/reference/classes/wp_user/
             * 
             * @see https://developer.wordpress.org/reference/hooks/show_user_profile/
             * @see https://developer.wordpress.org/reference/hooks/edit_user_profile/
             * @since 1.0.0
             */
            public function render_input_fields( $user ) {
                ?>
                    <h2><?php esc_html_e( 'Social Networks', 'blog-bee' ); ?></h2>
                    <table class="form-table">
                        <?php
                            if( ! empty( self::$meta_keys ) && is_array( self::$meta_keys ) ) :
                                foreach( self::$meta_keys as $meta_key => $meta_args ) :
                                    $meta_label = $meta_args[ 'label' ];
                                    $meta_value = get_the_author_meta( $meta_key, $user->ID ) ? esc_url( get_the_author_meta( $meta_key, $user->ID ) ) : '';
                                    ?>
                                        <tr>
                                            <th>
                                                <label for="<?php echo esc_attr( $meta_key ); ?>"><?php echo esc_html( $meta_label ); ?></label>
                                            </th>
                                            <td>
                                                <input
                                                    type = "url"
                                                    name = "<?php echo esc_attr( $meta_key ); ?>"
                                                    id = "<?php echo esc_attr( $meta_key ); ?>"
                                                    value = "<?php echo esc_url( $meta_value ); ?>"
                                                    class = "regular-text code"
                                                >
                                            </td>
                                        </tr>
                                    <?php
                                endforeach;
                            endif;
                        ?>
                    </table>
                <?php
            }

            /**
             * Update author meta values
             * 
             * @param int $user_id The user ID
             * @see https://developer.wordpress.org/reference/hooks/personal_options_update/
             * @see https://developer.wordpress.org/reference/hooks/edit_user_profile_update/
             * @since 1.0.0
             */
            public function update_meta_value( $user_id ) {

                if ( ! current_user_can( 'edit_user', $user_id ) ) return false;    // guard

                if( ! empty( self::$meta_keys ) && is_array( self::$meta_keys ) ) :
                    foreach( self::$meta_keys as $meta_key => $meta_args ) :
                        $current_value = isset( $_POST[ $meta_key ] ) ? esc_url( $_POST[ $meta_key ] ) : '';
                        update_user_meta( $user_id, $meta_key, esc_url( $current_value ) );
                    endforeach;
                endif;
            }
        }
        Author_Meta::get_instance();
    }