<?php

// -- AJAX: Layout form save --
add_action( 'wp_ajax_wcmamtx_save_layout', function() {
    check_ajax_referer( 'wcmamtx_layout_ajax_nonce', '_ajax_nonce' );

    if ( ! current_user_can( 'manage_options' ) ) {
        wp_send_json_error( 'Permission denied.' );
    }

    $option_name = 'wcmamtx_layout';

    if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), $option_name . '-options' ) ) {
        wp_send_json_error( 'Invalid nonce.' );
    }

    $new_value = isset( $_POST[ $option_name ] ) ? $_POST[ $option_name ] : array();
    $new_value = map_deep( $new_value, 'sanitize_text_field' );

    update_option( $option_name, $new_value );

    wp_send_json_success( array( 'message' => 'Settings saved!' ) );
} );

// -- Enqueue AJAX layout JS only on layout tab --
add_action( 'admin_footer', function() {
    $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
    $tab  = isset( $_GET['tab'] )  ? sanitize_text_field( wp_unslash( $_GET['tab'] ) )  : '';

    if ( $tab !== 'wcmamtx_layout' ) {
        return;
    }

    if ( ! current_user_can( 'manage_options' ) ) return;

    $nonce    = wp_create_nonce( 'wcmamtx_layout_ajax_nonce' );
    $ajax_url = admin_url( 'admin-ajax.php' );
    ?>
    <style>
        @keyframes wcmamtx_spin { to { transform: rotate(360deg); } }
        #wcmamtx_layout_loader {
            display: none;
            margin-left: 10px;
            vertical-align: middle;
        }
        #wcmamtx_layout_loader .wcmamtx-spinner {
            display: inline-block;
            width: 16px;
            height: 16px;
            border: 2px solid #ccc;
            border-top-color: #2271b1;
            border-radius: 50%;
            animation: wcmamtx_spin 0.7s linear infinite;
            vertical-align: middle;
        }
        #wcmamtx_layout_loader .wcmamtx-saving-text {
            margin-left: 6px;
            color: #2271b1;
            font-size: 12px;
            vertical-align: middle;
        }
    </style>
    <script>
    (function($){
        $(document).ready(function(){

            var $form = $('form[action="options.php"]');
            var $btn  = $('input.wcmamtx_submit_button.wcmamtx_layout');

            if ( ! $btn.length || ! $form.length ) return;

            // Inject spinner after button
            $btn.after('<span id="wcmamtx_layout_loader"><span class="wcmamtx-spinner"></span><span class="wcmamtx-saving-text">Saving...</span></span>');

            $btn.on('click', function(e) {
                e.preventDefault();

                var $loader = $('#wcmamtx_layout_loader');
                $btn.prop('disabled', true).css('opacity', '0.6');
                $loader.show();

                var formData = $form.serialize();
                formData += '&action=wcmamtx_save_layout';
                formData += '&_ajax_nonce=<?php echo $nonce; ?>';

                $.ajax({
                    url: '<?php echo esc_url( $ajax_url ); ?>',
                    type: 'POST',
                    data: formData,
                    success: function(res) {
                        $loader.hide();
                        $btn.prop('disabled', false).css('opacity', '1');
                        if ( res.success ) {
                            var origVal = $btn.val();
                            var origBg  = $btn.css('background-color');
                            $btn.val('Saved ✓').css({'background-color':'#00a32a','border-color':'#008a20','color':'#fff'});
                            setTimeout(function(){
                                $btn.val(origVal).css({'background-color':'','border-color':'','color':''});
                            }, 2000);
                        } else {
                            alert( 'Error: ' + ( res.data || 'Unknown error' ) );
                        }
                    },
                    error: function() {
                        $loader.hide();
                        $btn.prop('disabled', false).css('opacity', '1');
                        alert( 'AJAX request failed. Please try again.' );
                    }
                });
            });

        });
    })(jQuery);
    </script>
    <?php
} );


if (!class_exists('wcmamtx_add_settings_page_class')) {

class wcmamtx_add_settings_page_class {
	
	

	private $wcmamtx_plugin_options_key    = 'wcmamtx_plugin_options';
	private $wcmamtx_notices_settings_page = 'wcmamtx_advanced_settings';
	private $wcmamtx_order_settings_page   = 'wcmamtx_module_settings';
    private $wcmamtx_avatar_settings_page  = 'wcmamtx_avatar_settings';
    private $wcmamtx_pro_settings          = 'wcmamtx_pro_settings';
    private $wcmamtx_download_columns_page = 'wcmamtx_download_columns';
    private $wcmamtx_account_fields_page   = 'wcmamtx_account_fields';
    private $wcmamtx_layout_page           = 'wcmamtx_layout';
	private $wcmamtx_plugin_settings_tab   = array();
	

	
	public function __construct() {
		
		add_action( 'admin_init', array( $this, 'wcmamtx_register_settings_settings' ) );
		add_action( 'admin_menu', array( $this, 'add_admin_menus' ) ,100);
		add_action( 'admin_enqueue_scripts', array($this, 'wcmamtx_register_admin_scripts'));
		add_action( 'admin_enqueue_scripts', array($this, 'wcmamtx_load_admin_menu_style'));
        add_action( 'wp_ajax_restore_my_account_tabs', array( $this, 'restore_my_account_tabs' ) );
        add_action( 'admin_post_nds_form_response_endpoint', array( $this, 'add_endpoint_form_response' ));    
        add_action( 'wp_ajax_wcmamtx_export_endpoints', array( $this, 'wcmamtx_export_endpoints_function' ) );
        add_action( 'wp_ajax_wcmamtx_import_menu', array( $this, 'wcmamtx_import_menu_function' ) );
        
	}

    public function wcmamtx_sanitize_items($items) {

       //$items = json_decode($items, TRUE);

       $new_row_values    = wcmamtx_get_new_row_values($items[0]);

       
       return $new_row_values;
    }


    public function wcmamtx_import_menu_function() {
        check_ajax_referer( 'wcmamtx_nonce', 'nonce' );
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( 'Unauthorized' );
        }

       
        $json = isset( $_POST['json'] ) ? json_decode( wp_unslash( $_POST['json'] ), true ) : null;
        
        if ( ! $json || ! isset( $json['items'] ) || ! is_array( $json['items'] ) ) {
            wp_send_json_error( 'Invalid JSON' );
        }


        $new_row_values    =  $this->wcmamtx_sanitize_items($json['items']);

        $advancedsettings  = (array) get_option('wcmamtx_advanced_settings');
       

        
        if (($new_row_values != $advancedsettings) && !empty($new_row_values)) {

            delete_option('wcmamtx_advanced_settings');
            update_option($this->wcmamtx_notices_settings_page,$new_row_values);
            update_option('wcmamtx_flush_rewrite_cache_required',"yes");
            
        }

        wp_send_json_success( [ 'sucess' => "yes",'reload' => true,'flush_rewrite'=>'yes' ] );
    }


    public function wcmamtx_export_endpoints_function() {
         check_ajax_referer( 'wcmamtx_nonce', 'nonce' );
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( 'Unauthorized' );
        }

        wp_send_json_success( [
            'plugin'   => 'customize-my-account-for-woocommerce',
            'exported' => current_time( 'mysql' ),
            'items'    => wcmamtx_get_default_endpoint_data(),
            
        ] );
    }







	public function add_endpoint_form_response() {
		
           include("endpoint_form_response.php");
	}


	public function wcmamtx_get_posts_ajax_callback(){
 
	
	  $return          = array();
	  
      $post_type_array = array('elementor_library');
	  // you can use WP_Query, query_posts() or get_posts() here - it doesn't matter
	  $search_results  = new WP_Query( array(
		's'                   => isset( $_GET['q'] ) ? sanitize_text_field( wp_unslash( $_GET['q'] ) ) : '',
		'post_status'         => 'publish', // if you don't want drafts to be returned
		'ignore_sticky_posts' => 1,
		'post_type'           => $post_type_array
	  ) );
	  



	  if( $search_results->have_posts() ) {
		while( $search_results->have_posts() ) : $search_results->the_post();

		    $product_type = WC_Product_Factory::get_product_type($search_results->post->ID);	
			// shorten the title a little
			

			
				 $finaltitle=''.$search_results->post->post_title.'';
				 $return[] = array( $search_results->post->ID, $finaltitle );
			
			
			  

			 // array( Post ID, Post Title )
		endwhile;
	  } 
	   echo json_encode( $return );
	  die;
    }





	public function wcmamtx_load_admin_menu_style() {

	    wp_enqueue_style( 'woomatrix_admin_menu_css', ''.wcmamtx_PLUGIN_URL.'assets/css/admin_menu.css' );
	    wp_enqueue_script( 'woomatrix_admin_menu_js', ''.wcmamtx_PLUGIN_URL.'assets/js/admin_menu.js' );

	}


	public function restore_my_account_tabs() {
        check_ajax_referer( 'wcmamtx_nonce', 'nonce' );
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( 'Unauthorized' );
        }
        delete_option( $this->wcmamtx_notices_settings_page );
        update_option( 'wcmamtx_flush_rewrite_cache_required', 'yes' );
        delete_option( 'wcmamtx_endpoint_allowed_to_add' );
        delete_option( 'wcmamtx_groups_allowed_to_add' );
        wp_send_json_success();
	}





	
	/*
	 * registers admin scripts via admin enqueue scripts
	 */
	public function wcmamtx_register_admin_scripts($hook) {
	    global $general_wcmamtxsettings_page;



			
		if ( $hook == $general_wcmamtxsettings_page )  {

		    $current_tab = isset($_GET['tab']) ? sanitize_text_field($_GET['tab']) : "wcmamtx_advanced_settings";
 
            wp_enqueue_style( 'wcmamtx_fontawesome', ''.wcmamtx_PLUGIN_URL.'assets/css/font-awesome.min.css');

            
            wp_enqueue_script( 'wcmamtx_bootstrap', ''.wcmamtx_PLUGIN_URL.'assets/js/bootstrap.min.js');
            wp_enqueue_script( 'wcmamtx_bootstrap_toggle', ''.wcmamtx_PLUGIN_URL.'assets/js/bootstrap4-toggle.min.js');
            wp_enqueue_style( 'wcmamtx_bootstrap', ''.wcmamtx_PLUGIN_URL.'assets/css/bootstrap.min.css');
            wp_enqueue_style( 'wcmamtx_bootstrap_toggle', ''.wcmamtx_PLUGIN_URL.'assets/css/bootstrap4-toggle.min.css');

		    wp_enqueue_script( 'select2', ''.wcmamtx_PLUGIN_URL.'assets/js/select2.js' );

		    if (isset($current_tab) && (($current_tab == "wcmamtx_advanced_settings") || ($current_tab == "wcmamtx_pro_settings") )) {

                wp_enqueue_style( 'wp-color-picker' );

		        wp_enqueue_script( 'wcmamtxadmin', ''.wcmamtx_PLUGIN_URL.'assets/js/admin.js',array('jquery-ui-accordion','wp-color-picker'), '1.0.0', true );

		    } else if (isset($current_tab) && ($current_tab == "wcmamtx_order_settings")) {
		    	wp_enqueue_script( 'wcmamtxsortable', ''.wcmamtx_PLUGIN_URL.'assets/js/sortable.js' );
		    	wp_enqueue_script( 'wcmamtxsortable2', ''.wcmamtx_PLUGIN_URL.'assets/js/sortable2.js');

		    	wp_enqueue_script( 'wcmamtxadmin', ''.wcmamtx_PLUGIN_URL.'assets/js/admin2.js',array('jquery-ui-accordion'), '1.0.0', true );

		    } else if (isset($current_tab) && ($current_tab == "wcmamtx_order_actions")) {

		    	wp_enqueue_script( 'wcmamtxsortable', ''.wcmamtx_PLUGIN_URL.'assets/js/sortable.js' );
		    	wp_enqueue_script( 'wcmamtxsortable2', ''.wcmamtx_PLUGIN_URL.'assets/js/sortable2.js');

		    	wp_enqueue_script( 'wcmamtxadmin', ''.wcmamtx_PLUGIN_URL.'assets/js/admin3.js',array('jquery-ui-accordion'), '1.0.0', true );

		    } else if (isset($current_tab) && ($current_tab == "wcmamtx_plugin_options")) {

		    	wp_enqueue_script( 'wcmamtxadmin', ''.wcmamtx_PLUGIN_URL.'assets/js/admin.js',array('jquery-ui-accordion'), '1.0.0', true );

		    }  

            if (isset($current_tab) && ($current_tab == "wcmamtx_layout")) {

                wp_enqueue_style( 'wcmamtx_layout', ''.wcmamtx_PLUGIN_URL.'assets/css/layout.css');

                

                wp_enqueue_script( 'wcmamtx_layout', ''.wcmamtx_PLUGIN_URL.'assets/js/layout.js',array('jquery'), '1.0.0', true );

                $wcmamtx_js_array_layout = array(
                    'proonly'           => esc_html__( 'Pro feature' ,'customize-my-account-for-woocommerce'), 
                    'previewtxt'           => esc_html__( 'Preview' ,'customize-my-account-for-woocommerce'),            
                    'image01'              => ''.wcmamtx_PLUGIN_URL.'assets/images/layout1.png',
                    'image02'              => ''.wcmamtx_PLUGIN_URL.'assets/images/layout2.png',
                    'image03'              => ''.wcmamtx_PLUGIN_URL.'assets/images/layout3.png',
                    'image04'              => ''.wcmamtx_PLUGIN_URL.'assets/images/layout4.png',
                    'sidebar1'             => ''.wcmamtx_PLUGIN_URL.'assets/images/sidebar1.png',
                    'sidebar2'             => ''.wcmamtx_PLUGIN_URL.'assets/images/sidebar2.png',
                    'sidebar3'             => ''.wcmamtx_PLUGIN_URL.'assets/images/sidebar3.png',
                    'navigation1'          => ''.wcmamtx_PLUGIN_URL.'assets/images/navigation1.png',
                    'navigation2'          => ''.wcmamtx_PLUGIN_URL.'assets/images/navigation2.png',
                    'navigation3'          => ''.wcmamtx_PLUGIN_URL.'assets/images/navigation3.png',
                    'navigation4'          => ''.wcmamtx_PLUGIN_URL.'assets/images/navigation4.png',
                    'orders1'          => ''.wcmamtx_PLUGIN_URL.'assets/images/orders1.png',
                    'orders2'          => ''.wcmamtx_PLUGIN_URL.'assets/images/orders2.png',
                    'download1'          => ''.wcmamtx_PLUGIN_URL.'assets/images/download1.png',
                    'download2'          => ''.wcmamtx_PLUGIN_URL.'assets/images/download2.png',
                    'view_order1'          => ''.wcmamtx_PLUGIN_URL.'assets/images/vieworder1.png',
                    'view_order2'          => ''.wcmamtx_PLUGIN_URL.'assets/images/vieworder2.png',
                    'thankyou1'          => ''.wcmamtx_PLUGIN_URL.'assets/images/thankyou1.png',
                    'thankyou2'          => ''.wcmamtx_PLUGIN_URL.'assets/images/thankyou2.png',
                    'linkbox1'          => ''.wcmamtx_PLUGIN_URL.'assets/images/linkbox1.png',
                    'profilebox1'          => ''.wcmamtx_PLUGIN_URL.'assets/images/profilebox1.png',
                    'orderpay1'          => ''.wcmamtx_PLUGIN_URL.'assets/images/orderpay1.png',
                    'orderpay2'          => ''.wcmamtx_PLUGIN_URL.'assets/images/orderpay2.png',
                );

               wp_localize_script( 'wcmamtx_layout', 'wcmamtx_layout', $wcmamtx_js_array_layout );

            }


            if (isset($current_tab) && ($current_tab == "wcmamtx_avatar_settings"))  {

                wp_enqueue_media();


                wp_enqueue_script( 'wcmamtxadmin_avatar', ''.wcmamtx_PLUGIN_URL.'assets/js/admin_avatar.js', '1.0.0', true );

                $wcmamtx_js_array_avatar = array(
                
                'uploadimage'           => esc_html__( 'Choose an image' ,'customize-my-account-for-woocommerce'),
                'useimage'              => esc_html__( 'Use Image' ,'customize-my-account-for-woocommerce'),
                'placeholder'           => wcmamtx_placeholder_img_src(),
            
                
                
                );

               wp_localize_script( 'wcmamtxadmin_avatar', 'wcmamtxadmin_avatar', $wcmamtx_js_array_avatar );

            }

		    wp_enqueue_script( 'wcmamtx-dashicons', ''.wcmamtx_PLUGIN_URL.'assets/js/dashicons-picker.js');

		    wp_enqueue_style( 'wcmamtx-dashicons', ''.wcmamtx_PLUGIN_URL.'assets/css/dashicons-picker.css');
		
            wp_enqueue_script( 'wcmamtx-tageditor', ''.wcmamtx_PLUGIN_URL.'assets/js/tageditor.js');
		    wp_enqueue_style( 'wcmamtx-tageditor', ''.wcmamtx_PLUGIN_URL.'assets/css/tageditor.css');

	        wp_enqueue_style( 'jquery-ui-core', ''.wcmamtx_PLUGIN_URL.'assets/css/jquery-ui.css' );
            wp_enqueue_style( 'select2',''.wcmamtx_PLUGIN_URL.'assets/css/select2.css');
		 
		    wp_enqueue_style( 'wcmamtxadmin', ''.wcmamtx_PLUGIN_URL.'assets/css/admin.css' );


		 
		    $wcmamtx_js_array = array(
                'new_row_alert_text'   => esc_html__( 'Enter name for new endpoint' ,'customize-my-account-for-woocommerce'),
                'new_group_alert_text' => esc_html__( 'Enter name for new group' ,'customize-my-account-for-woocommerce'),
                'new_link_alert_text'  => esc_html__( 'Enter name for new link' ,'customize-my-account-for-woocommerce'),
                'group_mixing_text'    => esc_html__( 'Group can not be dropped into group' ,'customize-my-account-for-woocommerce'),
                'restorealert'         => esc_html__( 'Are you sure you want to restore to default my account tabs ? you can not undo this.' ,'customize-my-account-for-woocommerce'),
                'endpoint_remove_alert'   => esc_html__( "Are you sure you want to delete this ?" ,'customize-my-account-for-woocommerce'),
                'endpoint_remove_alert_thirdparty'   => esc_html__( "This is third party endpoint, if parent plugin is active, it will keep coming back. Only delete after parent plugin is deactivated otherwise just hide it." ,'customize-my-account-for-woocommerce'),
                'core_remove_alert'     => esc_html__( "this group has core endpoints. please move them before removing this group" ,'customize-my-account-for-woocommerce'),
                'dt_type'               => wcmamtx_get_version_type(),
                'pro_notice'            => esc_html__( 'This feature is available in pro version only.' ,'customize-my-account-for-woocommerce'),
                'empty_label_notice'    => esc_html__( 'Label can not be empty.' ,'customize-my-account-for-woocommerce'),
                'nonce'                 => wp_create_nonce( 'wcmamtx_nonce' ),
                'ajax_url'              => admin_url( 'admin-ajax.php' ),
                'wait_text'             => esc_html__( 'Adding....' ,'customize-my-account-for-woocommerce'),
                'order_action_text'     => esc_html__( 'Plugin do not support movement of order actions yet' ,'customize-my-account-for-woocommerce'),
                'chose_template'        => esc_html__( 'Choose Template' ,'customize-my-account-for-woocommerce'),
                'uploadimage'           => esc_html__( 'Choose an image' ,'customize-my-account-for-woocommerce'),
                'useimage'              => esc_html__( 'Use Image' ,'customize-my-account-for-woocommerce'),
                'placeholder'           => wcmamtx_placeholder_img_src(),
                'chosebulkaction'       => esc_html__( 'No Action Selected' ,'customize-my-account-for-woocommerce'),
                'firstsucess'           => esc_html__( 'Bulk Action Applied to all sucessfully.Make sure to save changes.' ,'customize-my-account-for-woocommerce'),
                'importSuccess'        => esc_html__( 'Import sucessfully completed, Click ok to refresh the page.' ,'customize-my-account-for-woocommerce'),
                'importError'           => esc_html__( 'Import failed' ,'customize-my-account-for-woocommerce'),
                
                
            );

            wp_localize_script( 'wcmamtxadmin', 'wcmamtxadmin', $wcmamtx_js_array );

        }
	}
	
	

	
	
	public function wcmamtx_register_settings_settings() {


        //add endpoints tab

		$this->wcmamtx_plugin_settings_tab[$this->wcmamtx_notices_settings_page] = esc_html__( 'Endpoints' ,'customize-my-account-for-woocommerce');

        register_setting( $this->wcmamtx_notices_settings_page, $this->wcmamtx_notices_settings_page );

        add_settings_section( 'wcmamtx_advance_section', '', '', $this->wcmamtx_notices_settings_page );

        add_settings_field( 'advanced_option', '', array( $this, 'linked_product_swatches_settings' ), $this->wcmamtx_notices_settings_page, 'wcmamtx_advance_section' );


        

        $this->wcmamtx_plugin_settings_tab[$this->wcmamtx_layout_page] = esc_html__( 'Design' ,'customize-my-account-for-woocommerce');

        register_setting( $this->wcmamtx_layout_page, $this->wcmamtx_layout_page );

        add_settings_section( 'wcmamtx_layout_section', '', '', $this->wcmamtx_layout_page );

        add_settings_field( 'layout_option', '', array( $this, 'layout_settings_form_function' ), $this->wcmamtx_layout_page, 'wcmamtx_layout_section' );

        
        /*

        $this->wcmamtx_plugin_settings_tab[$this->wcmamtx_account_fields_page] = esc_html__( 'My Account Fields' ,'customize-my-account-for-woocommerce');


            register_setting( $this->wcmamtx_account_fields_page, $this->wcmamtx_account_fields_page );

            add_settings_section( 'wcmamtx_account_fields_section', '', '', $this->wcmamtx_account_fields_page );

            add_settings_field( 'account_fields_option', '', array( $this, 'wcmamtx_account_fields_form' ), $this->wcmamtx_account_fields_page, 'wcmamtx_account_fields_section' );



        
        
        

        //order actions & columns tab


        $this->wcmamtx_plugin_settings_tab[$this->wcmamtx_order_settings_page]   = esc_html__( 'Order Columns & Actions' ,'customize-my-account-for-woocommerce');

        register_setting( $this->wcmamtx_order_settings_page, $this->wcmamtx_order_settings_page );

        add_settings_section( 'wcmamtx_order_section', '', '', $this->wcmamtx_order_settings_page );

        add_settings_field( 'order_option', '', array( $this, 'wcmamtx_order_coumns_form' ), $this->wcmamtx_order_settings_page, 'wcmamtx_order_section' );

        
        */
        
        //User avatar tab

        /*
        
        $this->wcmamtx_plugin_settings_tab[$this->wcmamtx_plugin_options_key]    = esc_html__( 'Elementor Templates' ,'customize-my-account-for-woocommerce');


        register_setting( $this->wcmamtx_plugin_options_key, $this->wcmamtx_plugin_options_key );

        add_settings_section( 'wcmamtx_general_section', '', '', $this->wcmamtx_plugin_options_key );

        add_settings_field( 'general_option', '', array( $this, 'wcmamtx_options_page' ), $this->wcmamtx_plugin_options_key, 'wcmamtx_general_section' );



        //download columns tab

        
        $this->wcmamtx_plugin_settings_tab[$this->wcmamtx_download_columns_page] = esc_html__( 'Download Columns' ,'customize-my-account-for-woocommerce');


        register_setting( $this->wcmamtx_download_columns_page, $this->wcmamtx_download_columns_page );

        add_settings_section( 'wcmamtx_download_section', '', '', $this->wcmamtx_download_columns_page );

        add_settings_field( 'download_option', '', array( $this, 'wcmamtx_download_coumns_form' ), $this->wcmamtx_download_columns_page, 'wcmamtx_download_section' );


        */


        //settings tab
        
		

	}

    public function layout_settings_form_function() {
        include ('forms/layout_form.php');
    }


    public function wcmamtx_account_fields_form() { 
         
         include ('forms/orders_form.php');
        
    }

    public function wcmamtx_order_coumns_form() { 
         
         include ('forms/orders_form.php');
        
    }

    public function wcmamtx_download_coumns_form() { 
         
         include ('forms/orders_form.php');
        
    }









	/**
      * Recursive sanitation for an array
      * 
      * @param $array
      *
      * @return mixed
      */
	public function recursive_sanitize_text_field($array) {
		foreach ( $array as $key => $value ) {

			$value = sanitize_text_field( $value );

		}

		return $array;
	}
	

	

	

	/*
     * Linked product swatached settings
     * includes form field from forms folder
     */
	
	public function linked_product_swatches_settings() { 

        $permalink_structure = get_option( 'permalink_structure' );

        if ($permalink_structure == "") {
            ?>

            <div class="wcmamtx_notice_div review">

                <div class="wcmamtx_notice_div_uppertext">
                    <?php echo esc_html__( 'Looks like your site is using default permalink structure (?p=123), which is not recommended and could cause broken links on frontend, consider changing it to something else.','customize-my-account-for-woocommerce'); ?>

                    <a type="button" target="_blank" href="options-permalink.php"  class="" >

                        <?php echo esc_html__( 'Change Permalink Structure' ,'customize-my-account-for-woocommerce'); ?>
                    </a>

                    
                </div>
            </div>

            <?php
        }


        include ('forms/settings_form.php');

    }






    /*
     * Plugin options page
     * 
     */
    
    public function wcmamtx_options_page() { 

      
           
    }

	
	
	/*
     * Adds Admin Menu "cart notices"
     * global $general_wcmamtxsettings_page is used to include page specific scripts
     */

	public function add_admin_menus() {
	    global $general_wcmamtxsettings_page;
        
        add_menu_page(
          __( 'SysBasics', 'customize-my-account-for-woocommerce' ),
         'SysBasics',
         'manage_woocommerce',
         'sysbasics',
         array($this,'plugin_options_page'),
         ''.wcmamtx_PLUGIN_URL.'assets/images/icon.png',
         70
        );
	    

        $general_wcmamtxsettings_page = add_submenu_page( 'sysbasics', 'Customize My Account' , 'Customize My Account' , 'manage_woocommerce', $this->wcmamtx_notices_settings_page, array($this, 'plugin_options_page'));


	         
	}




	public function plugin_options_page() {
		$tab = isset( $_GET['tab'] ) ? sanitize_text_field($_GET['tab']) : sanitize_text_field($this->wcmamtx_notices_settings_page);
        $current_tab = isset($_GET['tab']) ? sanitize_text_field($_GET['tab']) : $tab;
        $dt_type = wcmamtx_get_version_type();
       
        ?>
        <div class="wrap">
           <?php $this->wcmamtx_options_tab_wrap(); ?>
            <form method="post" action="options.php">
                <?php wp_nonce_field( 'update-options' ); ?>
                <?php settings_fields( $tab ); ?>
                <?php 
                do_settings_sections( $tab ); 

               

                $allowed_endpoints = get_option("wcmamtx_allowed_endpoint_trial");



                     $endpoint_button_id = "#wcmamtx_example_modal2";
                    $endpoint_button_class = "wcmamtx_disabled2";
                


                $allowed_links = get_option("wcmamtx_allowed_link_trial");




                if (isset($allowed_links) && ($allowed_links > 0))  {

                

                    $link_button_id = "#wcmamtx_example_modal3";
                    $link_button_class = "";

                
                } else {
                     $link_button_id = "#wcmamtx_example_modal2";
                    $link_button_class = "wcmamtx_disabled2";
                }

            
                ?>

                <div class="wcmamtx_buttons_section">

                    <?php if (isset($current_tab) && ($current_tab == "wcmamtx_advanced_settings") && ($current_tab != "wcmamtx_wizard_settings") ) { ?>
                    <div class="wcmamtx-btn-group wcmamtx-btn-group--add">
                        <button type="button" data-toggle="modal" data-target="<?php echo $endpoint_button_id; ?>" data-etype="endpoint" id="wcmamtx_add_endpoint" class="wcmamtx-action-btn wcmamtx_add_group <?php echo $endpoint_button_class; ?>">
                            <span class="dashicons dashicons-insert"></span>
                            <span class="wcmamtx-btn-label"><?php echo esc_html__( 'Endpoint', 'customize-my-account-for-woocommerce' ); ?></span>
                        </button>
                        <button type="button" data-toggle="modal" data-target="<?php echo $link_button_id; ?>" data-etype="link" id="wcmamtx_add_link" class="wcmamtx-action-btn wcmamtx_add_group <?php echo $link_button_class; ?>">
                            <span class="dashicons dashicons-insert"></span>
                            <span class="wcmamtx-btn-label"><?php echo esc_html__( 'Link', 'customize-my-account-for-woocommerce' ); ?></span>
                        </button>
                        <button type="button" data-toggle="modal" data-target="#wcmamtx_example_modal2" data-etype="group" id="wcmamtx_add_group" class="wcmamtx-action-btn wcmamtx_add_group wcmamtx_disabled2">
                            <span class="dashicons dashicons-insert"></span>
                            <span class="wcmamtx-btn-label"><?php echo esc_html__( 'Group', 'customize-my-account-for-woocommerce' ); ?></span>
                        </button>
                        <button type="submit" form="wcmamtx_separater_form" id="wcmamtx_add_space" class="wcmamtx-action-btn wcmamtx_add_group">
                            <span class="dashicons dashicons-insert"></span>
                            <span class="wcmamtx-btn-label"><?php echo esc_html__( 'Separater', 'customize-my-account-for-woocommerce' ); ?></span>
                        </button>
                        <button type="button" data-toggle="modal" data-target="#wcmamtx_example_modal3" data-etype="heading" class="wcmamtx-action-btn wcmamtx_add_group">
                            <span class="dashicons dashicons-insert"></span>
                            <span class="wcmamtx-btn-label"><?php echo esc_html__( 'Heading', 'customize-my-account-for-woocommerce' ); ?></span>
                        </button>
                    </div>
                    <?php } ?>

                    <div class="wcmamtx-btn-group wcmamtx-btn-group--actions wcmamtx_submit_section_div <?php echo esc_attr($current_tab); ?>">

                        <?php if (isset($current_tab) && ($current_tab != "wcmamtx_wizard_settings")) {
                            $load_wcmamtx_optional_class = load_wcmamtx_optional_class($current_tab);
                        ?>
                        <input type="submit" name="submit" id="submit" class="wcmamtx-action-btn wcmamtx-action-btn--save wcmamtx_submit_button <?php echo esc_attr($current_tab); ?> <?php echo $load_wcmamtx_optional_class; ?>" value="<?php echo esc_html__( 'Save Changes', 'customize-my-account-for-woocommerce' ); ?>">
                        <?php } ?>

                        <?php if (isset($current_tab) && ($current_tab == "wcmamtx_advanced_settings") && ($current_tab != "wcmamtx_wizard_settings")) { ?>
                            <input type="button" id="wcmamtx_reset_tabs_button" class="wcmamtx-action-btn wcmamtx-action-btn--danger wcmamtx_reset_tabs_button" value="<?php echo esc_html__( 'Restore Default', 'customize-my-account-for-woocommerce' ); ?>">
                        <?php } elseif (isset($current_tab) && ($current_tab == "wcmamtx_order_settings") && ($current_tab != "wcmamtx_wizard_settings")) { ?>
                            <input type="button" id="wcmamtx_reset_order_button" class="wcmamtx-action-btn wcmamtx-action-btn--danger wcmamtx_reset_order_button" value="<?php echo esc_html__( 'Restore Default', 'customize-my-account-for-woocommerce' ); ?>">
                        <?php } ?>

                        <?php
                        $frontend_url = get_permalink( get_option( 'woocommerce_myaccount_page_id' ) );
                        if ( ( $tab == "wcmamtx_order_settings" ) || ( $tab == "wcmamtx_order_actions" ) ) {
                            $frontend_url = wc_get_account_endpoint_url( 'orders' );
                        }
                        ?>

                        <?php if (isset($current_tab) && ($current_tab != "wcmamtx_wizard_settings")) { ?>
                            <button type="button" class="wcmamtx-action-btn wcmamtx-action-btn--info expimpbuttons wcmamtx-btn-export">
                                <span class="dashicons dashicons-upload"></span>
                                <span class="wcmamtx-btn-label"><?php esc_html_e( 'Export', 'customize-my-account-for-woocommerce' ); ?></span>
                            </button>
                            <button type="button" class="wcmamtx-action-btn wcmamtx-action-btn--info expimpbuttons wcmamtx-btn-import">
                                <span class="dashicons dashicons-download"></span>
                                <span class="wcmamtx-btn-label"><?php esc_html_e( 'Import', 'customize-my-account-for-woocommerce' ); ?></span>
                            </button>
                            <input type="file" id="wcmamtx-import-file" accept=".json" style="display:none;">
                            <a type="button" target="_blank" href="<?php echo esc_url( $frontend_url ); ?>" class="wcmamtx-action-btn wcmamtx-action-btn--primary wcmamtx_frontend_link">
                                <span class="dashicons dashicons-welcome-view-site"></span>
                                <span class="wcmamtx-btn-label"><?php echo esc_html__( 'Frontend', 'customize-my-account-for-woocommerce' ); ?></span>
                            </a>
                        <?php } ?>

                        <?php do_action( 'wcmamtx_add_author_links' ); ?>
                    </div>

                </div>

                </form>

                <form id="wcmamtx_separater_form" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" class="nds_add_user_meta_form" style="display:none;">
                    <input type="hidden" name="action" value="nds_form_response_endpoint">
                    <input type="hidden" name="wcmamtx_add_endpoint_nonce" value="<?php echo wp_create_nonce( 'wcmamtx_nonce_hidden' ); ?>" />
                    <input type="hidden" class="form-control" nonce="<?php echo wp_create_nonce( 'wcmamtx_nonce_hidden' ); ?>" name="<?php echo 'nds'; ?>[row_type]" id="wcmamtx_hidden_endpoint_type" value="separater">
                </form>

                <form id="wcmamtx_separater_form" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" class="nds_add_user_meta_form" style="display:none;">
                    <input type="hidden" name="action" value="nds_form_response_endpoint">
                    <input type="hidden" name="wcmamtx_add_endpoint_nonce" value="<?php echo wp_create_nonce( 'wcmamtx_nonce_hidden' ); ?>" />
                    <input type="hidden" class="form-control" nonce="<?php echo wp_create_nonce( 'wcmamtx_nonce_hidden' ); ?>" name="<?php echo 'nds'; ?>[row_type]" id="wcmamtx_hidden_endpoint_type" value="separater">
                </form>




            <div class="modal fade" id="wcmamtx_bulk_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">

                        <div class="modal-body">
                            
                            <select class="wcmamtx_bulk_action_select">
                                <option disabled selected value><?php echo esc_html__( 'Choose Action' ,'customize-my-account-for-woocommerce'); ?></option>
                                <option value="01"><?php echo esc_html__( 'Restore Default Background Color to all Dashboard Links' ,'customize-my-account-for-woocommerce'); ?></option>
                                <option value="02"><?php echo esc_html__( 'Restore Default Font Color to all Dashboard Links' ,'customize-my-account-for-woocommerce'); ?></option>
                            </select>

                            
                        </div>
                        <div class="modal-footer">

                            <button type="button" class="btn btn-secondary bulk apply wcmamtx_bulk_action_select_apply"><?php echo esc_html__( 'Apply' ,'customize-my-account-for-woocommerce'); ?></button>

                            <button type="button" class="btn btn-secondary bulk" data-dismiss="modal"><?php echo esc_html__( 'Close' ,'customize-my-account-for-woocommerce'); ?></button>

                        </div>
                    </div>
                </div>
            </div>

            <div class="modal fade" id="wcmamtx_upgrade_modal" tabindex="-1" role="dialog" aria-hidden="true">
                <div class="modal-dialog modal-dialog-centered" role="document">
                    <div class="modal-content" style="border-radius:10px;overflow:hidden;">
                        <div class="modal-body" style="text-align:center;padding:36px 28px 28px;">
                            <div style="font-size:40px;margin-bottom:12px;">&#128274;</div>
                            <h4 style="margin:0 0 10px;font-size:18px;"><?php esc_html_e('Pro Feature','customize-my-account-for-woocommerce'); ?></h4>
                            <p style="color:#666;font-size:14px;margin:0 0 24px;line-height:1.6;"><?php esc_html_e('This style is available in the Pro version of Customize My Account for WooCommerce.','customize-my-account-for-woocommerce'); ?></p>
                            <a href="https://sysbasics.com/go/customize/" target="_blank" class="button button-primary" style="margin-right:8px;text-decoration:none;">&#9889; <?php esc_html_e('Upgrade to Pro','customize-my-account-for-woocommerce'); ?></a>
                            <button type="button" class="button" data-dismiss="modal"><?php esc_html_e('Close','customize-my-account-for-woocommerce'); ?></button>
                        </div>
                    </div>
                </div>
            </div>

            <div class="modal fade" id="wcmamtx_example_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">

                        <div class="modal-body">
                          <form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" id="nds_add_user_meta_form" >            


                                <input type="hidden" name="action" value="nds_form_response_endpoint">
                                <input type="hidden" name="wcmamtx_add_endpoint_nonce" value="<?php echo wp_create_nonce( 'wcmamtx_nonce_hidden' ); ?>" />          
                                <div class="form-group">
                                    
                                    
                                    <input class="form-control" required id="sdfsd-user_meta_key" type="text" name="<?php echo "nds"; ?>[label]" value="" placeholder="<?php echo esc_html__('Enter Label','customize-my-account-for-woocommerce'); ?>" /><br>
                                    <input type="hidden" class="form-control" nonce="<?php echo wp_create_nonce( 'wcmamtx_nonce_hidden' ); ?>" name="<?php echo "nds"; ?>[row_type]" id="wcmamtx_hidden_endpoint_type" value="">
                                </div>

                                <button type="button" class="btn btn-secondary" data-dismiss="modal"><?php echo esc_html__( 'Close' ,'customize-my-account-for-woocommerce'); ?></button>
                                <button type="submit" name="submit"  class="btn btn-primary wcmamtx_new_end_point"><?php echo esc_html__( 'Add' ,'customize-my-account-for-woocommerce'); ?>

                                </button>
                            </form>
                        </div>
                        <div class="modal-footer">

                        </div>
                    </div>
                </div>
            </div>

            <div class="modal fade" id="wcmamtx_example_modal2" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">

                        <div class="modal-body">

                             <?php  wcmamtx_load_pro_reminder_div(); ?>

                        </div>
                        <div class="modal-footer">

                        </div>
                    </div>
                </div>
            </div>

            <div class="modal fade" id="wcmamtx_example_modal3" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">

                        <div class="modal-body">

                            

                            <form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" id="nds_add_user_meta_form" > 

                                


                                <input type="hidden" name="action" value="nds_form_response_endpoint">
                                <input type="hidden" name="wcmamtx_add_endpoint_nonce" value="<?php echo wp_create_nonce( 'wcmamtx_nonce_hidden' ); ?>" />          
                                <div class="form-group">
                                    
                                    
                                    <input class="form-control" required id="sdfsd-user_meta_key" type="text" name="<?php echo "nds"; ?>[label]" value="" placeholder="<?php echo esc_html__('Enter Label','customize-my-account-for-woocommerce'); ?>" /><br>
                                    <input type="hidden" class="form-control" nonce="<?php echo wp_create_nonce( 'wcmamtx_nonce_hidden' ); ?>" name="<?php echo "nds"; ?>[row_type]" id="wcmamtx_hidden_endpoint_type" value="">
                                </div>

                                <button type="button" class="btn btn-secondary" data-dismiss="modal"><?php echo esc_html__( 'Close' ,'customize-my-account-for-woocommerce'); ?></button>
                                <button type="submit" name="submit"  class="btn btn-primary wcmamtx_new_end_point"><?php echo esc_html__( 'Add' ,'customize-my-account-for-woocommerce'); ?>

                                </button>
                            </form>
                            

                        </div>
                        <div class="modal-footer">

                        </div>
                    </div>
                </div>
            </div>

            <div class="modal fade" id="wcmamtx_order_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">

                        <div class="modal-body">

                            <form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" id="nds_add_column_form" >          


                                <input type="hidden" name="action" value="nds_form_response_column">
                                <input type="hidden" name="wcmamtx_add_column_nonce" value="<?php echo wp_create_nonce( 'wcmamtx_nonce_hidden_column' ); ?>" />          
                                <div class="form-group">
                                    
                                    
                                    <input class="form-control" required id="sdfsd-user_meta_key" type="text" name="<?php echo "ndscolumn"; ?>[label]" value="" placeholder="<?php echo esc_html__('Enter Label','customize-my-account-for-woocommerce'); ?>" /><br>
                                    
                                </div>

                                <button type="button" class="btn btn-secondary" data-dismiss="modal"><?php echo esc_html__( 'Close' ,'customize-my-account-for-woocommerce'); ?></button>
                                <button type="submit" name="submit"  class="btn btn-primary wcmamtx_new_order"><?php echo esc_html__( 'Add New Column' ,'customize-my-account-for-woocommerce'); ?>

                                </button>
                            </form>

                        </div>
                        <div class="modal-footer">

                        </div>
                    </div>
                </div>
            </div>
            <div class="modal fade" id="wcmamtx_actions_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">

                        <div class="modal-body">

                            <form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" id="nds_add_action_form" >          


                                <input type="hidden" name="action" value="nds_form_response_action">
                                <input type="hidden" name="wcmamtx_add_action_nonce" value="<?php echo wp_create_nonce( 'wcmamtx_nonce_hidden_action' ); ?>" />          
                                <div class="form-group">
                                    
                                    
                                    <input class="form-control" required id="sdfsd-user_meta_key" type="text" name="<?php echo "ndsaction"; ?>[label]" value="" placeholder="<?php echo esc_html__('Enter Label','customize-my-account-for-woocommerce'); ?>" /><br>
                                    
                                </div>

                                <button type="button" class="btn btn-secondary" data-dismiss="modal"><?php echo esc_html__( 'Close' ,'customize-my-account-for-woocommerce'); ?></button>
                                <button type="submit" name="submit"  class="btn btn-primary wcmamtx_new_actions"><?php echo esc_html__( 'Add New Action' ,'customize-my-account-for-woocommerce'); ?>

                                </button>
                            </form>

                        </div>
                        <div class="modal-footer">

                        </div>
                    </div>
                </div>
            </div>


        </div>
        <?php
	}


	
	public function wcmamtx_options_tab_wrap() {

        $current_tab = isset( $_GET['tab'] ) ? esc_attr($_GET['tab']) : sanitize_text_field($this->wcmamtx_notices_settings_page);

        if (isset($current_tab)  && ($current_tab != "wcmamtx_wizard_settings")) {
           
            if (($current_tab == "wcmamtx_advanced_settings") || ($current_tab == "wcmamtx_avatar_settings")) {
                echo '<a href="admin.php?page=wcmamtx_frontend_customizer" class="btn wcmamtx_flot_right_endpoint btn-warning wcmamtx_live_customizer_endpoint ">
                            <span class="dashicons dashicons-art"></span>
                            <span class="wcmamtx-btn-label">Live Customizer</span>
                    </a>';
            }
           
            echo '<a target="_blank" class="btn wcmamtx_support_buton btn-success wcmamtx_flot_right_endpoint" href="https://www.sysbasics.com/go/customize/"><span class="wcmamtx_docs_icon dashicons dashicons-admin-generic"></span>'.esc_html__( 'Support' ,'customize-my-account-for-woocommerce').'</a>';
        } else {

            echo '<a class="btn wcmamtx_exit_setup btn-danger" href="?page=' .$this->wcmamtx_notices_settings_page. '&wcmamtx_disable_wizard=yes"><span class="wcmamtx_docs_icon dashicons dashicons-controls-forward"></span>'.esc_html__( 'Skip Quick Setup Wizard' ,'customize-my-account-for-woocommerce').'</a>';

        }


        ?>




        <?php


        $current_tab = isset( $_GET['tab'] ) ? esc_attr($_GET['tab']) : sanitize_text_field($this->wcmamtx_notices_settings_page);

        
        
        

        echo '<h2 class="nav-tab-wrapper">';

        foreach ( $this->wcmamtx_plugin_settings_tab as $tab_key => $tab_caption ) {

            $href = '?page=' . $this->wcmamtx_notices_settings_page . '&tab=' . $tab_key . '';
            $extra_att = '';

            switch($tab_key) {

                case "wcmamtx_module_settings":

                   $href = '#';
                   $extra_att = 'data-toggle="modal" data-target="#wcmamtx_example_modal2"';

                break;

                case "wcmamtx_download_columns":

                   $href = '#';
                   $extra_att = 'data-toggle="modal" data-target="#wcmamtx_example_modal2"';

                break;

                 case "wcmamtx_plugin_options":

                   $href = '#';
                   $extra_att = 'data-toggle="modal" data-target="#wcmamtx_example_modal2"';

                break;

                case "wcmamtx_account_fields":

                   $href = 'https://www.sysbasics.com/product/woocommerce-my-account-fields/';
                   $extra_att = '';

                break;




            }

            $active = $current_tab == $tab_key ? 'nav-tab-active' : '';


            $extra_new_html = '';

            if (($tab_key == "wcmamtx_layout") || ($tab_key == "wcmamtx_avatar_settings")) {
                $extra_new_html = '<span class="wcmamtx_new_tab_note">'.esc_html__( 'New' ,'customize-my-account-for-woocommerce').'</span>';
            }

            echo '<a '. $extra_att.' class="nav-tab ' . $active . ' '.$tab_key.' " href="'.$href.'">' . esc_html($tab_caption) . ' ' . $extra_new_html . '</a>'; 

        }

        echo '</h2>';




	}

    /**
     * render accordion content from $key and $value
     */

	public function get_accordion_content($key,$name,$core_fields,$value = null,$old_value = null,$third_party = null) {
	     
		$third_party = isset($value['third_party']) ? $value['third_party'] : $third_party; 

        $third_party_go_ahead = 'yes';

		if (isset($third_party)) {
			$key = strtolower($key);
			$key = str_replace(' ', '_', $key);
            $third_party_go_ahead = wcmamtx_third_party_goahead_check($key);
		}

        $wcmamtx_type = isset($value['wcmamtx_type']) ? $value['wcmamtx_type'] : "endpoint";

        if ($wcmamtx_type == "heading") {
            $wcmamtx_type = "endpoint";
        }



        if ($third_party_go_ahead == "yes") {

   
        ?>

            <li keyvalue="<?php echo $key; ?>" litype="<?php if (isset($wcmamtx_type)) { echo  $wcmamtx_type; } ?>" class="<?php if (isset($value['show']) && ($value['show'] == "no"))  { echo "wcmamtx_disabled"; } ?> wcmamtx_endpoint <?php echo $key; ?> <?php if (isset($value['wcmamtx_type']) && ($value['wcmamtx_type'] == "group")) { echo 'group'; } ?> <?php if (preg_match('/\b'.$key.'\b/', $core_fields )) { echo "core"; } ?>">

                <?php $this->get_main_li_content($key,$name,$core_fields,$value,$old_value,$third_party); ?>


            </li> 

        <?php

        }
    }


    /**
     * render accordion content from $key and $value
     */

	public function get_order_content($key,$name,$core_fields,$value = null,$old_value = null,$third_party = null) {
	     
		$third_party = isset($value['third_party']) ? $value['third_party'] : $third_party; 

		if (isset($third_party)) {
			$key = strtolower($key);
			$key = str_replace(' ', '_', $key);
		}
        
        ?>
        <li keyvalue="<?php echo $key; ?>" litype="<?php if (isset($value['wcmamtx_type'])) { echo  $value['wcmamtx_type']; } ?>" class="<?php if (isset($value['show']) && ($value['show'] == "no"))  { echo "wcmamtx_disabled"; } ?> wcmamtx_endpoint <?php echo $key; ?> <?php if (isset($value['wcmamtx_type']) && ($value['wcmamtx_type'] == "group")) { echo 'group'; } ?> <?php if (preg_match('/\b'.$key.'\b/', $core_fields )) { echo "core"; } ?>">

            <?php $this->get_main_order_content($key,$name,$core_fields,$value,$old_value,$third_party); ?>


        </li> <?php
        
    }


    /**
     * render accordion content from $key and $value
     */

	public function get_order_actions($key,$name,$core_fields,$value = null,$old_value = null,$third_party = null) {
	     
		$third_party = isset($value['third_party']) ? $value['third_party'] : $third_party; 

		if (isset($third_party)) {
			$key = strtolower($key);
			$key = str_replace(' ', '_', $key);
		}
        
        ?>
        <li keyvalue="<?php echo $key; ?>" litype="<?php if (isset($value['wcmamtx_type'])) { echo  $value['wcmamtx_type']; } ?>" class="<?php if (isset($value['show']) && ($value['show'] == "no"))  { echo "wcmamtx_disabled"; } ?> wcmamtx_endpoint <?php echo $key; ?> <?php if (isset($value['wcmamtx_type']) && ($value['wcmamtx_type'] == "group")) { echo 'group'; } ?> <?php if (preg_match('/\b'.$key.'\b/', $core_fields )) { echo "core"; } ?>">

            <?php $this->get_main_order_actions($key,$name,$core_fields,$value,$old_value,$third_party); ?>


        </li> <?php
        
    }


    public function get_main_order_actions($key,$name,$core_fields,$value = null,$old_value = null,$third_party = null) { 
         
        include ('wrap/orders-wrap.php');
    
    }


    public function get_main_order_content($key,$name,$core_fields,$value = null,$old_value = null,$third_party = null) { 
         
        include ('wrap/orders-content.php');
    
    }

    public function get_main_li_content($key,$name,$core_fields,$value = null,$old_value = null,$third_party = null) { 
         
        include ('wrap/endpoints-content.php');
    
    }


    public function get_group_content($name,$key,$value) {

        	    $all_keys  = (array) get_option('wcmamtx_advanced_settings');  
                
                $matches   = $this->wcmamtx_search($all_keys, $key);

         
    	    ?>

            	<ol class="wcmamtx_group_items">

                    <?php 
                        foreach($matches as $mkey=>$mvalue) {
                        	$mname             = $mvalue['endpoint_name'];
                        	$core_fields       = 'dashboard,orders,downloads,edit-address,edit-account,customer-logout';


                            $this->get_accordion_content($mkey,$mname,$core_fields,$mvalue,null);
                        }
                    ?>
                
                </ol>
            <?php
                
    }






    public function wcmamtx_search($array, $key) {
          
            $results = array();

            
        
                foreach ($array as $subkey=>$subvalue) {

                	if (isset($subvalue['parent'])) {

                		if ($subvalue['parent'] == $key) {
                    	    $results[$subkey] = $subvalue;
                        }
                	}
                    
                }
            
            return $results;
        }
    


    }
}


new wcmamtx_add_settings_page_class();
?>