<?php

if ( !defined( 'ABSPATH' ) ) {
    exit;
}

if ( !class_exists( 'Reon_Option_Page' ) ) {

    class Reon_Option_Page {

        private static $instance;
        private static $option_page;
        private static $user_capabilities;
        private static $nonce;
        private static $nonce_id;

        public static function get_instance(): self {

            if ( !self::$instance ) {

                self::$instance = new self();
            }

            return self::$instance;
        }

        public function __construct() {

            self::$nonce_id = 'reon-admin';
        }

        public static function init() {

            add_action( "admin_menu", array( new self(), 'init_menu' ), 100 );

            Reon_Ajax::add_action( 'save_option', array( new self(), 'save_option' ) ); //export_option
            Reon_Ajax::add_action( 'reset_option', array( new self(), 'reset_option' ) );
            Reon_Ajax::add_action( 'import_option', array( new self(), 'import_option' ) );
            Reon_Ajax::add_action( 'export_option', array( new self(), 'export_option' ) );
        }

        public function init_menu() {

            foreach ( self::get_pages( false ) as $pg ) {

                $option_page = self::get_page( $pg[ 'option_name' ], true );

                if ( !$option_page[ 'menu' ][ 'enable' ] ) {

                    continue;
                }

                $menu_args = $option_page[ 'menu' ];

                $reon_page_title = (isset( $menu_args[ 'page_title' ] ) && !empty( $menu_args[ 'page_title' ] )) ? $menu_args[ 'page_title' ] : 'Reon Settings Page';
                $reon_menu_title = (isset( $menu_args[ 'title' ] ) && !empty( $menu_args[ 'title' ] )) ? $menu_args[ 'title' ] : 'Reon Settings';
                $reon_capability = (isset( $option_page[ 'user_capability' ] ) && !empty( $option_page[ 'user_capability' ] )) ? $option_page[ 'user_capability' ] : 'manage_options';

                $reon_menu_slug = 'reon';

                if ( isset( $option_page[ 'slug' ] ) && !empty( $option_page[ 'slug' ] ) ) {

                    $reon_menu_slug = $option_page[ 'slug' ];
                }

                $reon_position = (isset( $menu_args[ 'priority' ] ) && !empty( $menu_args[ 'priority' ] )) ? $menu_args[ 'priority' ] : null;

                if ( !isset( $menu_args[ 'parent' ] ) || empty( $menu_args[ 'parent' ] ) ) {

                    $reon_icon_url = (isset( $menu_args[ 'icon' ] ) && !empty( $menu_args[ 'icon' ] )) ? $menu_args[ 'icon' ] : 'dashicons dashicons-admin-generic';

                    /* =================================================================================
                     * The menu slug is already prefixed from plugins/themes that uses this framework.
                     * This is a shared framework library.
                     * This framework allows plugins/themes to choose the menu slug prefix.
                     * =================================================================================
                     */
                    add_menu_page( $reon_page_title, $reon_menu_title, $reon_capability, $reon_menu_slug, array( new self(), 'render' ), $reon_icon_url, $reon_position );
                } else {

                    $reon_parent_slug = $menu_args[ 'parent' ];

                    /* =================================================================================
                     * The menu slug is already prefixed from plugins/themes that uses this framework.
                     * This is a shared framework library.
                     * This framework allows plugins/themes to choose the menu slug prefix.
                     * =================================================================================
                     */
                    add_submenu_page( $reon_parent_slug, $reon_page_title, $reon_menu_title, $reon_capability, $reon_menu_slug, array( new self(), 'render' ), $reon_position );
                }
            }
        }

        public static function render() {

            $page_slug = '';

            /*==============================================================================================================
             * We are rendering the admin page here not processing or saving settings, nonce verification is not applicable.
             * =============================================================================================================
             */
            if ( isset( $_GET[ 'page' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
                $page_slug = sanitize_key( wp_unslash( $_GET[ 'page' ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
            }

            $page = self::get_page_by_slug( $page_slug, true, true );

            if ( !isset( $page[ 'user_capability' ] ) ) {

                wp_die();
            }

            if ( !current_user_can( $page[ 'user_capability' ] ) ) {

                wp_die();
            }

            $tabs_key = $page[ 'tabs_key' ];

            /*==============================================================================================================
             * We are rendering the admin page here not processing or saving settings, nonce verification is not applicable.
             * =============================================================================================================
             */
            if ( isset( $_GET[ $tabs_key ] ) && $page_slug != 'wc-settings' ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
                $page[ 'tab' ] = sanitize_key( wp_unslash( $_GET[ $tabs_key ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
            }


            $active_section_data = self::get_active_section_data_by_tab( $page[ 'sections' ], $page[ 'tab' ] );

            if ( isset( $active_section_data[ 'group' ] ) ) {

                $page[ 'group' ] = $active_section_data[ 'group' ];
            }

            if ( isset( $active_section_data[ 'header_title' ] ) ) {

                $page[ 'header_title' ] = $active_section_data[ 'header_title' ];
            }

            self::render_page( $page );
        }

        public static function render_page( $page ) {

            if ( !isset( $page[ 'header_title' ] ) ) {

                $active_section_data = self::get_active_section_data_by_tab( $page[ 'sections' ], $page[ 'tab' ] );

                if ( isset( $active_section_data[ 'header_title' ] ) ) {

                    $page[ 'header_title' ] = $active_section_data[ 'header_title' ];
                }
            }

            $instance_id_param = '';

            if ( isset( $page[ 'instance_id' ] ) && $page[ 'instance_id' ] != '' ) {

                $instance_id_param = '&instance_id=' . $page[ 'instance_id' ];
            }

            $page[ 'export_url' ] = admin_url( 'admin-ajax.php?reon-admin-ajax=1&action=reon_export_option&optn=' . $page[ 'option_name' ] . $instance_id_param . '&reon_admin_nonce=' . self::get_nonce() );

            do_action( 'reon_render-option-page', $page );
        }

        public static function save_option() {

            if ( !isset( $_POST[ 'reon_admin_nonce' ] ) || !wp_verify_nonce( sanitize_key( wp_unslash( $_POST[ 'reon_admin_nonce' ] ) ), self::$nonce_id ) ) {

                wp_die( '' ); // Silence is good
            }

            $output = array();
            $instance_id = '';
            $opt_name = '';
            $sec_id = '';

            if ( isset( $_POST[ 'reon_instance_id' ] ) ) {

                $instance_id = sanitize_key( wp_unslash( $_POST[ 'reon_instance_id' ] ) );
            }

            if ( isset( $_POST[ 'option_name' ] ) ) {

                $opt_name = sanitize_key( wp_unslash( $_POST[ 'option_name' ] ) );
            }

            if ( isset( $_POST[ 'section_id' ] ) ) {

                $sec_id = sanitize_key( wp_unslash( $_POST[ 'section_id' ] ) );
            }


            do_action( 'reon_before-save-' . $opt_name . '-options', $instance_id );

            $sanitized_options = array();

            if ( isset( $_POST[ 'options' ][ $opt_name ] ) ) {

                //This is further sanitized below using the "sanitize_options()" method after "wp_kses_post()" call
                $posted_options = map_deep( wp_unslash( $_POST[ 'options' ][ $opt_name ] ), 'wp_kses_post' );

                //Sanitization done here
                $sanitized_options = self::sanitize_options( $opt_name, $sec_id, $posted_options );
            }

            $option_args = self::get_page_option_args( $opt_name, $instance_id );

            $field_ids = self::get_page_field_ids( $opt_name, $sec_id );

            $user_capability = self::get_user_capability( $opt_name );

            if ( current_user_can( $user_capability ) ) {

                Reon_Core::save_options( $option_args, $sanitized_options, false, $field_ids );

                $output[ 'status' ] = 200;
                $output[ 'status_message' ] = wp_kses( $option_args[ 'ajax' ][ 'save_msg' ], Reon_Utils::get_allow_html() );
            } else {

                $output[ 'status' ] = 400;
                $output[ 'status_message' ] = wp_kses( $option_args[ 'ajax' ][ 'save_error_msg' ], Reon_Utils::get_allow_html() );
            }

            Reon_Ajax::render_json( $output );
        }

        public static function reset_option() {

            if ( !isset( $_POST[ 'reon_admin_nonce' ] ) || !wp_verify_nonce( sanitize_key( wp_unslash( $_POST[ 'reon_admin_nonce' ] ) ), self::$nonce_id ) ) {

                wp_die( '' ); // Silence is good
            }

            $output = array();
            $instance_id = '';
            $option_name = '';
            $section_id = '';

            if ( isset( $_POST[ 'reon_instance_id' ] ) ) {

                $instance_id = sanitize_key( wp_unslash( $_POST[ 'reon_instance_id' ] ) );
            }

            if ( isset( $_POST[ 'option_name' ] ) ) {

                $option_name = sanitize_key( wp_unslash( $_POST[ 'option_name' ] ) );
            }

            if ( isset( $_POST[ 'section_id' ] ) ) {

                $section_id = sanitize_key( wp_unslash( $_POST[ 'section_id' ] ) );
            }


            $field_ids = self::get_page_field_ids( $option_name, $section_id );

            $option_args = self::get_page_option_args( $option_name, $instance_id );

            $user_capability = self::get_user_capability( $option_name );

            if ( current_user_can( $user_capability ) ) {

                Reon_Core::reset_options( $option_args, $field_ids );

                $output[ 'status' ] = 200;
                $output[ 'status_message' ] = wp_kses( $option_args[ 'ajax' ][ 'reset_msg' ], Reon_Utils::get_allow_html() );
            } else {

                $output[ 'status' ] = 400;
                $output[ 'status_message' ] = wp_kses( $option_args[ 'ajax' ][ 'reset_error_msg' ], Reon_Utils::get_allow_html() );
            }

            Reon_Ajax::render_json( $output );
        }

        public static function import_option() {

            if ( !isset( $_POST[ 'reon_admin_nonce' ] ) || !wp_verify_nonce( sanitize_key( wp_unslash( $_POST[ 'reon_admin_nonce' ] ) ), self::$nonce_id ) ) {

                wp_die( '' ); // Silence is good
            }

            $output = array( 'status' => 200 );
            $instance_id = '';
            $opt_name = '';

            if ( isset( $_POST[ 'reon_instance_id' ] ) ) {

                $instance_id = sanitize_key( wp_unslash( $_POST[ 'reon_instance_id' ] ) );
            }

            if ( isset( $_POST[ 'option_name' ] ) ) {

                $opt_name = sanitize_key( wp_unslash( $_POST[ 'option_name' ] ) );
            }

            do_action( 'reon_before-import-' . $opt_name . '-options', $instance_id );

            $user_capability = self::get_user_capability( $opt_name );

            if ( isset( $_POST[ 'import_data' ] ) && current_user_can( $user_capability ) ) {

                //This is further sanitized below using the "sanitize_options()" method after "wp_kses_post()" call
                $posted_options = map_deep( wp_unslash( $_POST[ 'import_data' ] ), 'wp_kses_post' );

                //Sanitization is done here
                $sanitized_options = self::sanitize_options( $opt_name, '', $posted_options );

                $option_args = self::get_page_option_args( $opt_name, $instance_id );
                $opt = Reon_Core::load_options( $option_args, array() );

                foreach ( $sanitized_options as $key => $value ) {

                    $opt[ $key ] = $value;
                }

                Reon_Core::save_options( $option_args, $opt, false );

                $output[ 'status' ] = 200;
                $output[ 'status_message' ] = wp_kses( $option_args[ 'ajax' ][ 'reset_msg' ], Reon_Utils::get_allow_html() );
            } else {

                $output[ 'status' ] = 400;
                $output[ 'status_message' ] = wp_kses( $option_args[ 'ajax' ][ 'reset_error_msg' ], Reon_Utils::get_allow_html() );
            }

            Reon_Ajax::render_json( $output );
        }

        public static function export_option() {

            if ( !isset( $_GET[ 'reon_admin_nonce' ] ) || !wp_verify_nonce( sanitize_key( wp_unslash( $_GET[ 'reon_admin_nonce' ] ) ), self::$nonce_id ) ) {

                wp_die( '' ); // Silence is good
            }

            $instance_id = '';
            $option_name = '';

            if ( isset( $_GET[ 'instance_id' ] ) ) {

                $instance_id = sanitize_key( wp_unslash( $_GET[ 'instance_id' ] ) );
            }

            if ( isset( $_GET[ 'optn' ] ) ) {

                $option_name = sanitize_key( wp_unslash( $_GET[ 'optn' ] ) );
            }

            $option_args = self::get_page_option_args( $option_name, $instance_id );
            $output = Reon_Core::load_options( $option_args );

            Reon_Ajax::render_json( $output );
        }

        public static function get_page( $option_name = '', $process_sections = false, $process_fields = false ) {

            if ( $option_name == '' ) {

                return array();
            }

            foreach ( self::get_pages() as $pg ) {

                if ( $pg[ 'option_name' ] == $option_name ) {

                    $page = apply_filters( 'reon_process-option-page-' . $pg[ 'option_name' ], $pg );

                    if ( $process_sections == true ) {

                        $page[ 'sections' ] = self::get_page_sections( $page[ 'option_name' ], $page[ 'sections' ], $process_fields );
                    }

                    return $page;
                }
            }

            return array();
        }

        public static function get_page_by_slug( $slug = '', $process_sections = false, $process_fields = false ) {

            if ( $slug == '' ) {

                return array();
            }

            foreach ( self::get_pages() as $pg ) {

                if ( isset( $pg[ 'slug' ] ) && $pg[ 'slug' ] == $slug ) {

                    $page = apply_filters( 'reon_process-option-page-' . $pg[ 'option_name' ], $pg );

                    if ( $process_sections == true ) {

                        $page[ 'sections' ] = self::get_page_sections( $page[ 'option_name' ], $page[ 'sections' ], $process_fields );
                    }

                    return $page;
                }
            }

            return array();
        }

        public static function get_page_option_args( $option_name = '', $instance_id = '' ) {

            $page = self::get_page( $option_name );

            if ( count( $page ) > 0 ) {

                return array(
                    'option_name' => $page[ 'option_name' ],
                    'database' => $page[ 'database' ],
                    'ajax' => $page[ 'ajax' ],
                    'instance_id' => $instance_id,
                );
            }

            return array();
        }

        public static function get_pages( $process_sections = false, $process_fields = false ) {

            $pages = array();

            if ( !is_array( self::$option_page ) ) {

                return $pages;
            }

            foreach ( self::$option_page as $page ) {

                $page = apply_filters( 'reon_process-option-page-' . $page[ 'option_name' ] . '-args', $page );

                if ( $process_sections == true ) {

                    $page[ 'sections' ] = self::get_page_sections( $page[ 'option_name' ], $page[ 'sections' ], $process_fields );
                }

                $pages[] = $page;
            }

            return $pages;
        }

        public static function set_page( $args = array() ) {

            $option = self::default_optionpage();

            foreach ( $args as $key => $value ) {

                $option[ $key ] = $value;
            }

            self::$option_page[] = $option;
        }

        public static function set_section( $option_name = '', $args = array() ) {

            $section = self::default_section();

            foreach ( $args as $key => $value ) {

                $section[ $key ] = $value;
            }

            for ( $i = 0; $i < count( self::$option_page ); $i++ ) {

                if ( self::$option_page[ $i ][ 'option_name' ] == $option_name ) {

                    self::$option_page[ $i ][ 'sections' ][] = $section;
                }
            }
        }

        public static function get_page_field_ids( $option_name = '', $section_id = '' ) {

            $page = self::get_page( $option_name, true, true );

            $field_ids = array();

            if ( !count( $page ) ) {

                return apply_filters( 'reon_get-page-field-ids', $field_ids );
            }

            foreach ( $page[ 'sections' ] as $section ) {

                if ( $section[ 'id' ] == $section_id || $section_id == '' ) {

                    foreach ( $section[ 'fields' ] as $field ) {

                        if ( isset( $field[ 'merge_fields' ] ) && $field[ 'merge_fields' ] == false ) {

                            foreach ( Reon_Utils::get_controls_field_ids( $field[ 'fields' ] ) as $fld_id ) {

                                $field_ids[] = $fld_id;
                            }
                        } else {

                            $field_ids[] = $field[ 'id' ];
                        }
                    }
                }
            }

            return apply_filters( 'reon_get-page-field-ids', $field_ids );
        }

        public static function get_user_capability( $option_name ) {

            $capabilities = self::get_user_capabilities();

            if ( !$capabilities ) {

                return 'manage_options';
            }

            if ( !isset( $capabilities[ $option_name ] ) ) {

                return 'manage_options';
            }

            return $capabilities[ $option_name ];
        }

        public static function get_user_capabilities() {

            if ( is_null( self::$user_capabilities ) ) {

                self::$user_capabilities = self::load_user_capabilities();
            }

            return self::$user_capabilities;
        }

        private static function sanitize_options( $option_name = '', $section_id = '', $options = array() ) {

            $sanitize_mode = self::get_sanitize_mode( $option_name );

            if ( 'recursive' == $sanitize_mode ) {

                return self::get_recursive_sanitised_options( $option_name, $options );
            }

            $sanitise_filter = $option_name . '-options';

            if ( has_filter( 'reon_sanitize-' . $sanitise_filter ) && 'filter' == $sanitize_mode ) {

                return apply_filters( 'reon_sanitize-' . $sanitise_filter, $options );
            }

            $field_types = self::get_field_types( $option_name, $section_id );

            $opt = array();

            foreach ( $field_types as $field_type ) {

                if ( isset( $options[ $field_type[ 'id' ] ] ) ) {

                    $opt[ $field_type[ 'id' ] ] = apply_filters( 'reon_sanitize-' . $field_type[ 'type' ], $options[ $field_type[ 'id' ] ], $options[ $field_type[ 'id' ] ], $field_type[ 'id' ], $field_type[ 'children' ] );
                }
            }

            return $opt;
        }

        private static function get_field_types( $option_name = '', $section_id = '' ) {

            $page = self::get_page( $option_name, true, true );

            $field_args = array(
                'screen' => 'option-page',
                'option_name' => $option_name
            );

            $field_types = array();

            if ( !count( $page ) ) {

                return $field_types;
            }


            foreach ( $page[ 'sections' ] as $section ) {

                if ( !( $section[ 'id' ] == $section_id || $section_id == '' ) ) {

                    continue;
                }

                foreach ( $section[ 'fields' ] as $field ) {

                    if ( !( isset( $field[ 'merge_fields' ] ) && $field[ 'merge_fields' ] == false && isset( $field[ 'fields' ] ) ) ) {


                        $field_types[] = array(
                            'id' => $field[ 'id' ],
                            'type' => $field[ 'type' ],
                            'screen' => 'option-page',
                            'option_name' => $option_name,
                            'children' => apply_filters( 'reon_get-' . $field[ 'type' ] . '-children-types', array(), $field, $field_args )
                        );

                        continue;
                    }


                    foreach ( Reon_Utils::get_controls_field_types( $field[ 'fields' ], $field_args ) as $field_type ) {

                        $field_types[] = array(
                            'id' => $field_type[ 'id' ],
                            'type' => $field_type[ 'type' ],
                            'screen' => $field_type[ 'screen' ],
                            'option_name' => $field_type[ 'option_name' ],
                            'children' => $field_type[ 'children' ]
                        );
                    }
                }
            }

            return $field_types;
        }

        private static function get_recursive_sanitised_options( $in_key, $options ) {

            $opt = array();

            foreach ( $options as $option_key => $option ) {

                $filter_key = $in_key . '-' . $option_key;

                if ( is_numeric( $option_key ) ) {

                    $filter_key = $in_key;
                }

                if ( has_filter( 'reon_sanitize-' . $filter_key ) ) {

                    $opt[ sanitize_key( $option_key ) ] = apply_filters( 'reon_sanitize-' . $filter_key, $option );

                    continue;
                }

                if ( is_array( $option ) ) {

                    $opt[ sanitize_key( $option_key ) ] = self::get_recursive_sanitised_options( $filter_key, $option );

                    continue;
                }

                if ( $option == '<=' || $option == '<' || $option == '>=' || $option == '>=' || $option == '==' || $option == '!=' ) {

                    $opt[ sanitize_key( $option_key ) ] = $option;

                    continue;
                }

                $opt[ sanitize_key( $option_key ) ] = sanitize_text_field( $option );
            }

            return $opt;
        }

        private static function get_sanitize_mode( $option_name ) {

            $page = self::get_page( $option_name, false, false );

            if ( isset( $page[ 'sanitize_mode' ] ) ) {

                return $page[ 'sanitize_mode' ];
            }

            return 'default';
        }

        private static function get_page_sections( $option_name, $in_sections = array(), $process_fields = false ) {

            $sections = apply_filters( 'reon_get-option-page-' . $option_name . '-sections', $in_sections );

            $d_section = self::default_section();
        
            foreach ( $sections as $key => $section ) {
            
                foreach ( $d_section as $d_key => $d_sec ) {
                
                    if ( !isset( $section[ $d_key ] ) ) {
                    
                        $section[ $d_key ] = $d_sec;
                    }
                }
                
                $sect = apply_filters( 'reon_process-' . $option_name . '-section', $section );
                
                if ( $process_fields == true ) {
                
                    $sect[ 'fields' ] = self::process_section_fields( $option_name, $sect[ 'id' ], $sect[ 'fields' ] );
                }
                
                $sections[ $key ] = $sect;
            }

            return $sections;
        }

        private static function process_section_fields( $option_name, $section_id, $in_fields ) {

            $fields = apply_filters( 'reon_get-option-page-' . $option_name . 'section-' . $section_id . '-fields', $in_fields, $section_id );

            return Reon_Core::process_default_option_page_fields( $fields );
        }

        public static function load_user_capabilities() {

            $capabilities = array();

            if ( !is_array( self::$option_page ) ) {

                return $capabilities;
            }

            foreach ( self::$option_page as $in_page ) {

                $page = apply_filters( 'reon_process-option-page-' . $in_page[ 'option_name' ] . '-args', $in_page );

                if ( !isset( $page[ 'option_name' ] ) ) {

                    continue;
                }

                $option_name = $page[ 'option_name' ];
                $capability = 'manage_options';

                if ( empty( $option_name ) ) {

                    continue;
                }

                if ( !isset( $page[ 'user_capability' ] ) || empty( $page[ 'user_capability' ] ) ) {

                    $capabilities[ $option_name ] = $capability;

                    continue;
                }

                $capabilities[ $option_name ] = $page[ 'user_capability' ];
            }

            return $capabilities;
        }

        public static function get_nonce() {

            if ( !is_null( self::$nonce ) ) {

                return self::$nonce;
            }

            self::$nonce = wp_create_nonce( self::$nonce_id );

            return self::$nonce;
        }

        private static function default_optionpage() {
            
            $defuault_args = array(
                'option_name' => 'reon',
                'database' => 'theme_mod',
                'sanitize_mode' => 'default', // default, recursive, filter
                'slug' => 'reon',
                'url_base' => 'themes.php',
                'use_slug_url' => true,
                'auto_load' => true,
                'tabs_key' => 'tab',
                'tab' => 1,
                'user_capability' => 'manage_options',
                'display' => array(
                    'enabled' => true,
                    'image' => '',
                    'name' => 'Reon Framework',
                    'version' => '',
                    'sub_version' => '',
                    'styles' => array(
                        'bg_image' => '',
                        'bg_color' => '',
                        'color' => '',
                    ),
                ),
                'ajax' => array(
                    'save_msg' => 'Done!!',
                    'save_error_msg' => 'Unable to save your settings',
                    'reset_msg' => 'Done!!',
                    'reset_error_msg' => 'Unable to reset your settings',
                    'reset_prompt_msg' => "All settings will be removed, Press 'OK' to continue",
                    'reset_prompt_section_msg' => "Settings in this section will be removed, Press 'OK' to continue",
                    'nonce_error_msg' => 'invalid nonce'
                ),
                'menu' => array(
                    'enable' => true,
                    'title' => 'Reon Framework',
                    'icon' => 'dashicons-admin-generic',
                    'priority' => null,
                    'parent' => 'themes.php',
                    'page_title' => '',
                ),
                'admin_bar' => array(
                    'enable' => false,
                    'title' => 'Reon Framework',
                    'icon' => '',
                    'priority' => 0,
                ),
                'import_export' => array(
                    'enable' => true,
                    'min_height' => '565px',
                    'title' => 'Import / Export',
                    'import' => array(
                        'title' => 'Import Settings',
                        'desc' => 'Here you can import new settings. Simply paste the settings url or data on the field below.',
                        'url_button_text' => 'Import from url',
                        'url_textbox_desc' => "Paste the url to another site's settings below and click the 'Import Now' button.",
                        'url_textbox_hint' => "Paste the url to another site's settings here...",
                        'data_button_text' => 'Import Data',
                        'data_textbox_desc' => "Paste your backup settings below and click the 'Import Now' button.",
                        'data_textbox_hint' => 'Paste your backup settings here...',
                        'import_button_text' => 'Import Now',
                        'warn_text' => 'Warning! This will override all existing settings. proceed with caution!',
                    ),
                    'export' => array(
                        'title' => 'Export Settings',
                        'desc' => 'Here you can backup your current settings. You can later use it to restore your settings.',
                        'download_button_text' => 'Download Data',
                        'url_button_text' => 'Export url',
                        'url_textbox_desc' => 'Copy the url below, use it to transfer the settings from this site.',
                        'data_button_text' => 'Export Data',
                        'data_textbox_desc' => 'Copy the data below, use it as your backup.',
                    ),
                ),
                'header_buttons' => array(),
                'footer_buttons' => array(),
                'page_links' => array(),
                'social_links' => array(),
                'sections' => array(),
            );
            
            return $defuault_args;
        }

        private static function default_section() {
         
            $args = array(
                'id' => '',
                'title' => '',
                'group' => 0,
                'subsection' => false,
                'fields' => array()
            );
            
            return $args;
        }

        private static function get_active_section_data_by_tab( $sections, $tab ) {

            $cnt = 1;

            foreach ( $sections as $section ) {

                if ( $cnt == $tab ) {

                    $active_group_data = array(
                        'group' => $section[ 'group' ],
                    );

                    if ( isset( $section[ 'header_title' ] ) && !empty( $section[ 'header_title' ] ) ) {

                        $active_group_data[ 'header_title' ] = $section[ 'header_title' ];
                    }

                    return $active_group_data;
                }

                $cnt++;
            }
        }

    }

}

