<?php
    /**
     * Includes: Options

     */
    if (! defined('ABSPATH')) {
        die('Sorry, you cannot access this page.');
    }
    /**
     * Display test mode notification
     */
    function chama_display_test_mode_notification_in_admin_bar($wp_admin_bar)
    {
        //check if testmode is enabled
        if (! chama_is_live_mode()) {
            // Add the main site admin menu item.
            $wp_admin_bar->add_menu(
                [
                    'id'     => 'chama-test-notice',
                    'href'   => admin_url('admin.php?page=chama-settings&tab=stripe'),
                    'parent' => 'top-secondary',
                    'title'  => __('Test Mode', 'chama'),
                    'meta'   => [
                        'class' => 'chama-test-mode-active',
                    ],
                ]
            );

        }

        return true;
    }

    add_action('admin_bar_menu', 'chama_display_test_mode_notification_in_admin_bar', 1000, 1);
    /**
     * Add plugin menu
     */
    add_action('admin_menu', 'chama_add_plugin_menu', 0);
    function chama_add_plugin_menu()
    {
        $menu_parent_slug = 'chama';
        $permissions      = 'manage_options';
        add_menu_page('ChamaWP', 'ChamaWP', 'edit_posts', $menu_parent_slug, 'chama_dashboard_page', chama_get_svg_icon_url(), 2);
        add_submenu_page($menu_parent_slug, 'Dashboard', 'Dashboard', $permissions, 'chama');
        add_submenu_page($menu_parent_slug, 'Settings', 'Settings', $permissions, 'chama-settings', 'chama_settings_page', 3);
        add_submenu_page($menu_parent_slug, 'Add Tier', 'Add Tier', $permissions, 'post-new.php?post_type=chama_tier', null, 4);
        add_submenu_page($menu_parent_slug, 'Tiers', 'Tiers', $permissions, 'edit.php?post_type=chama_tier', null, 5);
        add_submenu_page($menu_parent_slug, 'Add Campaign', 'Add Campaign', $permissions, 'post-new.php?post_type=chama_campaign', null, 6);
        add_submenu_page($menu_parent_slug, 'Campaigns', 'Campaigns', $permissions, 'edit.php?post_type=chama_campaign', null, 7);
        add_submenu_page($menu_parent_slug, 'Add Commission', 'Add Commission', $permissions, 'post-new.php?post_type=chama_commission', null, 8);
        add_submenu_page($menu_parent_slug, 'Commissions', 'Commissions', $permissions, 'edit.php?post_type=chama_commission', null, 9);
        add_submenu_page($menu_parent_slug, 'Add Newsletter', 'Add Newsletter', $permissions, 'post-new.php?post_type=chama_newsletter', null, 10);
        add_submenu_page($menu_parent_slug, 'Newsletters', 'Newsletters', $permissions, 'edit.php?post_type=chama_newsletter', null, 11);

        add_submenu_page($menu_parent_slug, 'Donations', 'Donations', $permissions, 'edit.php?post_type=chama_donation', null, 12);
        add_submenu_page($menu_parent_slug, 'Subscriptions', 'Subscriptions', $permissions, 'edit.php?post_type=chama_subscription', null, 13);
        add_submenu_page($menu_parent_slug, 'Funds', 'Funds', $permissions, 'edit.php?post_type=chama_fund', null, 14);
        add_submenu_page($menu_parent_slug, 'Commission Orders', 'Commission Orders', $permissions, 'edit.php?post_type=chama_com_order', null, 15);
        add_submenu_page($menu_parent_slug, 'Donors', 'Donors', $permissions, 'users.php?role=chama_donor', null, 16);
        add_submenu_page($menu_parent_slug, 'Members', 'Members', $permissions, 'users.php?role=chama_member', null, 17);
        add_submenu_page($menu_parent_slug, 'Funders', 'Funders', $permissions, 'users.php?role=chama_funder', null, 18);
        add_submenu_page($menu_parent_slug, 'Commissioners', 'Commissioners', $permissions, 'users.php?role=chama_comissioner', null, 19);

    }
    /**
     * Adds the "Upgrade to Premium" menu item to the very end of the submenu.
     */

    add_action('admin_menu', 'chama_add_upgrade_menu_item');
    function chama_add_upgrade_menu_item()
    {

        global $submenu;
        $submenu['chama'][9999] = [__('Upgrade to Pro', 'chama'), 'manage_options', CHAMA_UPGRADE_URL, '', 'chama-upgrade-side-menu chama-external-url'];

    }

    /**
     * Admin render admin bar
     */
    add_action('wp_after_admin_bar_render', 'chama_admin_display_admin_brand_bar');
    function chama_admin_display_admin_brand_bar()
    {
        $display_branding_header = false;
        if (function_exists('get_current_screen')) {
            $screen = get_current_screen();
            if ($screen->post_type === 'chama_tier' || $screen->post_type === 'chama_campaign' || $screen->post_type === 'chama_commission' || $screen->post_type === 'chama_donation' || $screen->post_type === 'chama_fund' || $screen->post_type === 'chama_com_order' || $screen->post_type === 'chama_subscription') {
                $display_branding_header = true;

            }
        }
        $page = sanitize_text_field(wp_unslash(filter_input(INPUT_GET, 'page', FILTER_SANITIZE_STRING)));

        if (strpos($page, "chama") === 0 || $display_branding_header) {
            include_once CHAMA_PATH . 'views/admin-branding-bar.php'; // @phpstan-ignore-line
        }

    }
    /**
     * Admin dashboard page
     */
    function chama_dashboard_page()
    {
        $breadcrumb = "Dashboard &rsaquo; Welcome";
        include_once CHAMA_PATH . 'views/admin-dashboard.php'; // @phpstan-ignore-line
    }
    /**
     * Admin settings page
     */
    function chama_settings_page()
    {
        $breadcrumb = "Settings &rsaquo; Settings";
        include_once CHAMA_PATH . 'views/admin-settings.php'; // @phpstan-ignore-line
    }
    /**
     * Initialize all fields
     */
    add_action('admin_init', 'chama_init_option_fields');
    function chama_init_option_fields()
    {
        //initialize fields
        if (! get_option('chama_stripe_currency')) {
            add_option('chama_stripe_currency', 'USD');
        }
        if (! get_option('chama_test_mode')) {
            add_option('chama_test_mode', 'enabled');
        }
        if (! get_option('chama_currency_position')) {
            add_option('chama_currency_position', 'left');
        }
        if (! get_option('chama_separator')) {
            add_option('chama_separator', false);
        }
        if (! get_option('chama_require_login')) {
            add_option('chama_require_login', false);
        }
        if (! get_option('chama_hide_admin_bar')) {
            add_option('chama_hide_admin_bar', false);
        }
        if (! get_option('chama_prevent_dashboard_access')) {
            add_option('chama_prevent_dashboard_access', false);
        }
        if (! get_option('chama_email_from_name')) {
            add_option('chama_email_from_name', get_bloginfo('name'));
        }
        if (! get_option('chama_email_from_address')) {
            add_option('chama_email_from_address', get_bloginfo('admin_email'));
        }
        if (! get_option('chama_admin_email')) {
            add_option('chama_admin_email', get_bloginfo('admin_email'));
        }
        if (! get_option('chama_new_user_notification')) {
            add_option('chama_new_user_notification', false);
        }
        if (! get_option('chama_supporter_enable_payment_notification')) {
            add_option('chama_supporter_enable_payment_notification', false);
        }
        if (! get_option('chama_supporter_enable_renewal_notification')) {
            add_option('chama_supporter_enable_renewal_notification', false);
        }
        if (! get_option('chama_supporter_enable_failed_payment_notification')) {
            add_option('chama_supporter_enable_failed_payment_notification', false);
        }
        if (! get_option('chama_admin_enable_payment_notification')) {
            add_option('chama_admin_enable_payment_notification', false);
        }
        if (! get_option('chama_display_hub')) {
            add_option('chama_display_hub', 'yes');
        }
        if (! get_option('chama_display_donate')) {
            add_option('chama_display_donate', 'yes');
        }
        if (! get_option('chama_display_membership')) {
            add_option('chama_display_membership', false);
        }
        if (! get_option('chama_display_crowdfunding')) {
            add_option('chama_display_crowdfunding', false);
        }
        if (! get_option('chama_display_commissions')) {
            add_option('chama_display_commissions', false);
        }
        if (! get_option('chama_donation_min_limit')) {
            add_option('chama_donation_min_limit', 1);
        }

        if (! get_option('chama_total_donors')) {
            add_option('chama_total_donors', 0);
        }

        if (! get_option('chama_total_members')) {
            add_option('chama_total_members', 0);
        }

        if (! get_option('chama_total_funders')) {
            add_option('chama_total_funders', 0);
        }

        if (! get_option('chama_total_commissioners')) {
            add_option('chama_total_commissioners', 0);
        }

        if (! get_option('chama_donation_processing_template')) {
            update_option('chama_donation_processing_template', chama_get_default_email_template('chama_donation_processing_template'));
        }
        if (! get_option('chama_donation_receipt_template')) {
            update_option('chama_donation_receipt_template', chama_get_default_email_template('chama_donation_receipt_template'));
        }
        if (! get_option('chama_new_donation_template')) {
            update_option('chama_new_donation_template', chama_get_default_email_template('chama_new_donation_template'));
        }
        if (! get_option('chama_fund_processing_template')) {
            update_option('chama_fund_processing_template', chama_get_default_email_template('chama_fund_processing_template'));
        }
        if (! get_option('chama_fund_receipt_template')) {
            update_option('chama_fund_receipt_template', chama_get_default_email_template('chama_fund_receipt_template'));
        }
        if (! get_option('chama_new_fund_template')) {
            update_option('chama_new_fund_template', chama_get_default_email_template('chama_new_fund_template'));
        }
        if (! get_option('chama_subscription_processing_template')) {
            update_option('chama_subscription_processing_template', chama_get_default_email_template('chama_subscription_processing_template'));
        }
        if (! get_option('chama_subscription_receipt_template')) {
            update_option('chama_subscription_receipt_template', chama_get_default_email_template('chama_subscription_receipt_template'));
        }
        if (! get_option('chama_new_subscription_template')) {
            update_option('chama_new_subscription_template', chama_get_default_email_template('chama_new_subscription_template'));
        }
        if (! get_option('chama_com_order_processing_template')) {
            update_option('chama_com_order_processing_template', chama_get_default_email_template('chama_com_order_processing_template'));
        }
        if (! get_option('chama_com_order_receipt_template')) {
            update_option('chama_com_order_receipt_template', chama_get_default_email_template('chama_com_order_receipt_template'));
        }
        if (! get_option('chama_new_commission_order_template')) {
            update_option('chama_new_commission_order_template', chama_get_default_email_template('chama_new_commission_order_template'));
        }
        if (! get_option('chama_payment_failure_template')) {
            update_option('chama_payment_failure_template', chama_get_default_email_template('chama_payment_failure_template'));
        }
        if (! get_option('chama_subscription_cancellation_template')) {
            update_option('chama_subscription_cancellation_template', chama_get_default_email_template('chama_subscription_cancellation_template'));
        }

        // Newsletter form defaults
        if (! get_option('chama_newsletter_form_background_color')) {
            add_option('chama_newsletter_form_background_color', '#f9fafb');
        }
        if (! get_option('chama_newsletter_form_border_color')) {
            add_option('chama_newsletter_form_border_color', '#d4d4d4');
        }
        if (! get_option('chama_newsletter_form_border_radius')) {
            add_option('chama_newsletter_form_border_radius', '4');
        }
        if (! get_option('chama_newsletter_form_background_image')) {
            add_option('chama_newsletter_form_background_image', '');
        }
        if (! get_option('chama_newsletter_form_background_transparency')) {
            add_option('chama_newsletter_form_background_transparency', '20');
        }

        // Header defaults
        if (! get_option('chama_newsletter_header_font_color')) {
            add_option('chama_newsletter_header_font_color', '#000000');
        }
        if (! get_option('chama_newsletter_header_font_size')) {
            add_option('chama_newsletter_header_font_size', '24');
        }
        if (! get_option('chama_newsletter_header_font_weight')) {
            add_option('chama_newsletter_header_font_weight', 'normal');
        }
        if (! get_option('chama_newsletter_form_header_text')) {
            update_option('chama_newsletter_form_header_text', 'Join the Newsletter');
        }

        // Sub Header defaults
        if (! get_option('chama_newsletter_sub_header_font_color')) {
            add_option('chama_newsletter_sub_header_font_color', '#686868');
        }
        if (! get_option('chama_newsletter_sub_header_font_size')) {
            add_option('chama_newsletter_sub_header_font_size', '18');
        }
        if (! get_option('chama_newsletter_form_sub_header_text')) {
            update_option('chama_newsletter_form_sub_header_text', 'Subscribe to our newsletter get the latest news');
        }

        // Email field defaults
        if (! get_option('chama_newsletter_email_font_color')) {
            add_option('chama_newsletter_email_font_color', '#000000');
        }
        if (! get_option('chama_newsletter_email_border_color')) {
            add_option('chama_newsletter_email_border_color', '#e3e3e3');
        }
        if (! get_option('chama_newsletter_email_field_border_radius')) {
            add_option('chama_newsletter_email_field_border_radius', '4');
        }
        if (! get_option('chama_newsletter_email_font_weight')) {
            add_option('chama_newsletter_email_font_weight', 'normal');
        }
        if (! get_option('chama_newsletter_email_field_label')) {
            update_option('chama_newsletter_email_field_label', 'Email address');
        }

        // Button defaults
        if (! get_option('chama_newsletter_button_font_color')) {
            add_option('chama_newsletter_button_font_color', '#ffffff');
        }
        if (! get_option('chama_newsletter_button_border_radius')) {
            add_option('chama_newsletter_button_border_radius', '4');
        }
        if (! get_option('chama_newsletter_button_font_weight')) {
            add_option('chama_newsletter_button_font_weight', 'normal');
        }
        if (! get_option('chama_newsletter_button_background_color')) {
            add_option('chama_newsletter_button_background_color', '#ff4757');
        }

    }
    /**
     * Admin 'Settings > Stripe' page
     */
    //Initialize stripe input fields
    add_action('admin_init', 'chama_stripe_fields');
    function chama_stripe_fields()
    {
        $page_slug      = 'chama-stripe-page';
        $settings_group = 'chama_stripe_settings';

        //setup page section
        add_settings_section("chama_stripe_section", '', '', $page_slug);

        //Payment mode field
        $key_value_payment_modes = ['enabled' => 'Test Mode', 'disabled' => 'Live Mode'];
        $chama_test_mode_args    = [
            'option'         => 'chama_test_mode',
            'label'          => 'Payment mode',
            'desc'           => null,
            'select_options' => $key_value_payment_modes,
        ];
        add_settings_field("chama_test_mode", "Payment mode", 'chama_select_dropdown_generator', $page_slug, "chama_stripe_section", $chama_test_mode_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_test_mode", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Currency field

        $currencies    = chama_get_stripe_currencies();
        $currency_list = [];
        foreach ($currencies as $currency_code => $currency_label) {
            $currency_list[$currency_code] = sprintf(
                '%s (%s)',
                $currency_label,
                chama_get_currency_symbol($currency_code)
            );
        }
        $chama_stripe_currency_args = [
            'option'         => 'chama_stripe_currency',
            'label'          => 'Currency',
            'desc'           => null,
            'select_options' => $currency_list,
        ];

        add_settings_field("chama_stripe_currency", "Currency", 'chama_select_dropdown_generator', $page_slug, "chama_stripe_section", $chama_stripe_currency_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_stripe_currency", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Currency position
        $currency         = get_option('chama_stripe_currency');
        $symbol           = chama_get_currency_symbol($currency);
        $formatted_amount = chama_format_currency(
            chama_get_currency_minimum($currency),
            $currency,
            false
        );
        $currency_position_options = [
            'left'        => esc_html(
                sprintf(
                    /* translators: %1$s Currency symbol. %2$s Sample amount. */
                    __('Left (%1$s%2$s)', 'chama'),
                    $symbol,
                    $formatted_amount
                )
            ),
            'right'       => esc_html(
                sprintf(
                    /* translators: %1$s Currency symbol. %2$s Sample amount. */
                    __('Right (%1$s%2$s)', 'chama'),
                    $formatted_amount,
                    $symbol
                )
            ),
            'left_space'  => esc_html(
                sprintf(
                    /* translators: %1$s Currency symbol. %2$s Sample amount. */
                    __('Left with Space (%1$s %2$s)', 'chama'),
                    $symbol,
                    $formatted_amount
                )
            ),
            'right_space' => esc_html(
                sprintf(
                    /* translators: %1$s Currency symbol. %2$s Sample amount. */
                    __('Right with Space (%1$s %2$s)', 'chama'),
                    $formatted_amount,
                    $symbol
                )
            ),
        ];
        $chama_currency_position_args = [
            'option'         => 'chama_currency_position',
            'label'          => 'Currency position',
            'desc'           => null,
            'select_options' => $currency_position_options,
        ];
        add_settings_field("chama_currency_position", "Currency position", 'chama_select_dropdown_generator', $page_slug, "chama_stripe_section", $chama_currency_position_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_currency_position", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Currency separator
        $chama_separator_args = [
            'option'         => 'chama_separator',
            'checkbox_value' => 'yes',
            'label'          => 'Use a comma when formatting decimal amounts and use a period to separate thousands.',
            'desc'           => 'If enabled, amounts will be formatted as "1.234,56" instead of "1,234.56".',
        ];

        add_settings_field("chama_separator", "Separator", 'chama_checkbox_option_generator', $page_slug, "chama_stripe_section", $chama_separator_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_separator", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    }
    /**
     * Admin 'Settings > Users' page
     */
    function chama_users_fields()
    {
        $page_slug      = 'chama-users-page';
        $settings_group = 'chama_users_settings';

        //setup page section
        add_settings_section("chama_users_section", '', '', $page_slug);

        //Require login for payments
        $chama_require_login_args = [
            'option'         => 'chama_require_login',
            'checkbox_value' => 'yes',
            'label'          => 'Require that users login whenever a payment is being made.',
            'desc'           => 'If enabled, users will be redirected to the login page whenever they are ready to make a payment.',
        ];

        add_settings_field("chama_require_login", "Require login", 'chama_checkbox_option_generator', $page_slug, "chama_users_section", $chama_require_login_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_require_login", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Require email verification to access protected content
        $chama_require_email_verification_args = [
            'option'         => 'chama_require_email_verification',
            'checkbox_value' => 'yes',
            'label'          => 'Require email verification for protected content.',
            'desc'           => 'If enabled, members on all subscription tiers will have to verify their emails to access content.',
        ];

        add_settings_field("chama_require_email_verification", "Require email verification", 'chama_checkbox_option_generator', $page_slug, "chama_users_section", $chama_require_email_verification_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_require_email_verification", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Hide admin bar
        $chama_hide_admin_bar_args = [
            'option'         => 'chama_hide_admin_bar',
            'checkbox_value' => 'yes',
            'label'          => 'Hide "Admin Bar" for all users except administrator users.',
            'desc'           => 'Enable this option if you do not want the WordPress admin bar displayed on all pages on your website when a user is logged in.',
        ];

        add_settings_field("chama_hide_admin_bar", "Disable admin bar", 'chama_checkbox_option_generator', $page_slug, "chama_users_section", $chama_hide_admin_bar_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_hide_admin_bar", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Prevent WP dashboard access
        $chama_prevent_dashboard_access_args = [
            'option'         => 'chama_prevent_dashboard_access',
            'checkbox_value' => 'yes',
            'label'          => 'Prevent all nonadmin users from accessing the WordPress dashboard page.',
            'desc'           => 'Enable this option if you do not want logged in users(who are not administrators) from accessing the WordPress Dashboard',
        ];

        add_settings_field("chama_prevent_dashboard_access", "Prevent dashboard access", 'chama_checkbox_option_generator', $page_slug, "chama_users_section", $chama_prevent_dashboard_access_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_prevent_dashboard_access", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    }
    add_action('admin_init', 'chama_users_fields');
    /**
     * Admin 'Settings > reCAPTCHA' page
     */
    //Initialize recaptcha input fields
    add_action('admin_init', 'chama_recaptcha_fields');
    function chama_recaptcha_fields()
    {
        $page_slug      = 'chama-recaptcha-page';
        $settings_group = 'chama_recaptcha_settings';

        //setup page section
        add_settings_section("chama_recaptcha_section", '', '', $page_slug);

        //Google reCAPTCHA site key
        $chama_recaptcha_site_key_args = [
            'option' => 'chama_recaptcha_site_key',
            'desc'   => null,
        ];
        add_settings_field("chama_recaptcha_site_key", "Site Key", 'chama_textbox_option_generator', $page_slug, "chama_recaptcha_section", $chama_recaptcha_site_key_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_recaptcha_site_key", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Google reCAPTCHA secret key
        $chama_recaptcha_secret_key_args = [
            'option' => 'chama_recaptcha_secret_key',
            'desc'   => null,
        ];
        add_settings_field("chama_recaptcha_secret_key", "Secret Key", 'chama_textbox_option_generator', $page_slug, "chama_recaptcha_section", $chama_recaptcha_secret_key_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_recaptcha_secret_key", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    }
    /**
     * Admin 'Settings > Pages' page
     */
    //Initialize pages input fields
    add_action('admin_init', 'chama_pages_fields');
    function chama_pages_fields()
    {
        $page_slug      = 'chama-pages-page';
        $settings_group = 'chama_pages_settings';

        //setup page section
        add_settings_section("chama_pages_section", '', '', $page_slug);

        //Hub page
        $chama_hub_page_args = [
            'option' => 'chama_hub_page',
        ];
        add_settings_field("chama_hub_page", "Hub Page", 'chama_page_select_option_generator', $page_slug, "chama_pages_section", $chama_hub_page_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_hub_page", ['type' => 'string', 'sanitize_callback' => 'absint']);

        //Donate page
        $chama_donate_page_args = [
            'option' => 'chama_donate_page',
        ];
        add_settings_field("chama_donate_page", "Donate Page", 'chama_page_select_option_generator', $page_slug, "chama_pages_section", $chama_donate_page_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_donate_page", ['type' => 'string', 'sanitize_callback' => 'absint']);

        //Membership page
        $chama_membership_page_args = [
            'option' => 'chama_membership_page',
        ];
        add_settings_field("chama_membership_page", "Membership Page", 'chama_page_select_option_generator', $page_slug, "chama_pages_section", $chama_membership_page_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_membership_page", ['type' => 'string', 'sanitize_callback' => 'absint']);

        //Crowdfunding page
        $chama_crowdfunding_page_args = [
            'option' => 'chama_crowdfunding_page',
        ];
        add_settings_field("chama_crowdfunding_page", "Crowdfunding Page", 'chama_page_select_option_generator', $page_slug, "chama_pages_section", $chama_crowdfunding_page_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_crowdfunding_page", ['type' => 'string', 'sanitize_callback' => 'absint']);

        //Commissions page
        $chama_commissions_page_args = [
            'option' => 'chama_commissions_page',
        ];
        add_settings_field("chama_commissions_page", "Commissions Page", 'chama_page_select_option_generator', $page_slug, "chama_pages_section", $chama_commissions_page_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_commissions_page", ['type' => 'string', 'sanitize_callback' => 'absint']);

        //Login page
        $chama_login_page_args = [
            'option' => 'chama_login_page',
        ];
        add_settings_field("chama_login_page", "Login Page", 'chama_page_select_option_generator', $page_slug, "chama_pages_section", $chama_login_page_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_login_page", ['type' => 'string', 'sanitize_callback' => 'absint']);

        //Registration page
        $chama_registration_page_args = [
            'option' => 'chama_registration_page',
        ];
        add_settings_field("chama_registration_page", "Registration Page", 'chama_page_select_option_generator', $page_slug, "chama_pages_section", $chama_registration_page_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_registration_page", ['type' => 'string', 'sanitize_callback' => 'absint']);

        //Edit Acccount page
        $chama_edit_account_page_args = [
            'option' => 'chama_edit_account_page',
        ];
        add_settings_field("chama_edit_account_page", "Edit Account Page", 'chama_page_select_option_generator', $page_slug, "chama_pages_section", $chama_edit_account_page_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_edit_account_page", ['type' => 'string', 'sanitize_callback' => 'absint']);

        //Edit Password page
        $chama_edit_password_page_args = [
            'option' => 'chama_edit_password_page',
        ];
        add_settings_field("chama_edit_password_page", "Edit Password Page", 'chama_page_select_option_generator', $page_slug, "chama_pages_section", $chama_edit_password_page_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_edit_password_page", ['type' => 'string', 'sanitize_callback' => 'absint']);

        //Forgot Password page
        $chama_forgot_password_page_args = [
            'option' => 'chama_forgot_password_page',
        ];
        add_settings_field("chama_forgot_password_page", "Forgot Password Page", 'chama_page_select_option_generator', $page_slug, "chama_pages_section", $chama_forgot_password_page_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_forgot_password_page", ['type' => 'string', 'sanitize_callback' => 'absint']);

        //Reset Password page
        $chama_password_reset_page_args = [
            'option' => 'chama_reset_password_page',
        ];
        add_settings_field("chama_reset_password_page", "Reset Password Page", 'chama_page_select_option_generator', $page_slug, "chama_pages_section", $chama_password_reset_page_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_reset_password_page", ['type' => 'string', 'sanitize_callback' => 'absint']);

        //Verify Email page
        $chama_verify_email_page_args = [
            'option' => 'chama_verify_email_page',
        ];
        add_settings_field("chama_verify_email_page", "Verify Email Page", 'chama_page_select_option_generator', $page_slug, "chama_pages_section", $chama_verify_email_page_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_verify_email_page", ['type' => 'string', 'sanitize_callback' => 'absint']);

        //Account page
        $chama_account_page_args = [
            'option' => 'chama_account_page',
        ];
        add_settings_field("chama_account_page", "Account Page", 'chama_page_select_option_generator', $page_slug, "chama_pages_section", $chama_account_page_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_account_page", ['type' => 'string', 'sanitize_callback' => 'absint']);

        //Payment page
        $chama_payment_page_args = [
            'option' => 'chama_payment_page',
        ];
        add_settings_field("chama_payment_page", "Payment Page", 'chama_page_select_option_generator', $page_slug, "chama_pages_section", $chama_payment_page_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_payment_page", ['type' => 'string', 'sanitize_callback' => 'absint']);

        //Payment Confirmation Page
        $chama_payment_confirmation_page_args = [
            'option' => 'chama_payment_confirmation_page',
        ];
        add_settings_field("chama_payment_confirmation_page", "Payment Confirmation Page", 'chama_page_select_option_generator', $page_slug, "chama_pages_section", $chama_payment_confirmation_page_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_payment_confirmation_page", ['type' => 'string', 'sanitize_callback' => 'absint']);

        //Subscription page
        $chama_subscription_page_args = [
            'option' => 'chama_subscription_page',
        ];
        add_settings_field("chama_subscription_page", "Subscription Page", 'chama_page_select_option_generator', $page_slug, "chama_pages_section", $chama_subscription_page_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_subscription_page", ['type' => 'string', 'sanitize_callback' => 'absint']);

        //Subscription Confirmation Page
        $chama_subscription_confirmation_page_args = [
            'option' => 'chama_subscription_confirmation_page',
        ];
        add_settings_field("chama_subscription_confirmation_page", "Subscription Confirmation Page", 'chama_page_select_option_generator', $page_slug, "chama_pages_section", $chama_subscription_confirmation_page_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_subscription_confirmation_page", ['type' => 'string', 'sanitize_callback' => 'absint']);

        //Newsletter Signup Confirmation Page
        $chama_newsletter_sign_up_confirmation_page_args = [
            'option' => 'chama_newsletter_sign_up_confirmation_page',
        ];
        add_settings_field("chama_newsletter_sign_up_confirmation_page", "Newsletter Signup Confirmation Page", 'chama_page_select_option_generator', $page_slug, "chama_pages_section", $chama_newsletter_sign_up_confirmation_page_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_sign_up_confirmation_page", ['type' => 'string', 'sanitize_callback' => 'absint']);

        //Newsletter Opt-out Confirmation Page
        $chama_newsletter_opt_out_confirmation_page_args = [
            'option' => 'chama_newsletter_opt_out_confirmation_page',
        ];
        add_settings_field("chama_newsletter_opt_out_confirmation_page", "Newsletter Opt-out Confirmation Page", 'chama_page_select_option_generator', $page_slug, "chama_pages_section", $chama_newsletter_opt_out_confirmation_page_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_opt_out_confirmation_page", ['type' => 'string', 'sanitize_callback' => 'absint']);

    }
    function chama_page_select_option_generator($args)
    {
        $option       = $args['option'];
        $current_page = get_option($option);
        global $wpdb;
        $pages = $wpdb->get_results("SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type = 'page' AND post_status = 'publish' ORDER BY ID");

        $page_list = [];

        foreach ($pages as $page) {
            $page_list[$page->ID] = $page->post_title;
        }

    ?>
<select id="<?php echo esc_attr($option); ?>" name="<?php echo esc_attr($option); ?>">
    <option value=""></option>
<?php foreach ($page_list as $value => $label): ?>
    <option value="<?php echo esc_attr($value); ?>"<?php selected($value, $current_page); ?> ><?php echo esc_html($label); ?></option>
<?php endforeach; ?>

</select>
<?php

    }
    function chama_post_select_option_generator($args)
    {
        $option       = $args['option'];
        $cpt          = $args['cpt'];
        $desc         = $args['desc'];
        $current_post = get_option($option);
        global $wpdb;
        $posts = $wpdb->get_results(
            $wpdb->prepare(
                "SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type = %s AND post_status = 'publish' ORDER BY post_title",
                $cpt
            )
        );

        $post_list = [];

        foreach ($posts as $post) {
            $post_list[$post->ID] = $post->post_title;
        }

    ?>
<select id="<?php echo esc_attr($option); ?>" name="<?php echo esc_attr($option); ?>">
    <option value=""></option>
<?php foreach ($post_list as $value => $label): ?>
    <option value="<?php echo esc_attr($value); ?>"<?php selected($value, $current_post); ?> ><?php echo esc_html($label); ?></option>
<?php endforeach; ?>

</select>
<?php if ($desc): ?>
         <p><em><?php echo esc_html($desc); ?></em></p>
         <?php endif; ?>
<?php

    }
    function chama_tier_select_option_generator($args)
    {
        $option       = $args['option'];
        $current_tier = get_option($option);
        $tier_list    = chama_get_tier_levels();

    ?>

<select id="<?php echo esc_attr($option); ?>" name="<?php echo esc_attr($option); ?>">

<?php foreach ($tier_list as $value => $label): ?>
    <option value="<?php echo esc_attr($value); ?>"<?php selected($value, $current_tier); ?> ><?php echo esc_html($label); ?></option>
<?php endforeach; ?>

</select>
<p><em>Choose the tier level you would like this content protected.</em></p>
<?php

    }
    /**
     * Admin 'Settings > Display' page
     */
    //Initialize display input fields
    add_action('admin_init', 'chama_display_fields');
    function chama_display_fields()
    {
        $page_slug      = 'chama-display-page';
        $settings_group = 'chama_display_settings';

        //setup page section
        add_settings_section("chama_display_section", '', '', $page_slug);

        //Display Hub?
        $chama_display_hub_args = [
            'option'         => 'chama_display_hub',
            'checkbox_value' => 'yes',
            'label'          => 'Select this option if you want the "Hub" page displayed.',
            'desc'           => 'If enabled, the "Hub" page will be displayed as a monetization option',
        ];

        add_settings_field("chama_display_hub", "Hub", 'chama_checkbox_option_generator', $page_slug, "chama_display_section", $chama_display_hub_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_display_hub", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Display Donate?
        $chama_display_donate_args = [
            'option'         => 'chama_display_donate',
            'checkbox_value' => 'yes',
            'label'          => 'Select this option if you want the "Donate" option displayed.',
            'desc'           => 'If enabled, the "One time" option as well as the "Donate" page will be displayed as a monetization option',
        ];

        add_settings_field("chama_display_donate", "Donate", 'chama_checkbox_option_generator', $page_slug, "chama_display_section", $chama_display_donate_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_display_donate", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Display Membership?
        $chama_display_membership_args = [
            'option'         => 'chama_display_membership',
            'checkbox_value' => 'yes',
            'label'          => 'Select this option if you want your "Membership" options displayed.',
            'desc'           => 'If enabled, your "Membership" tiers as well as the "Membership" page will be displayed as a monetization option',
        ];

        add_settings_field("chama_display_membership", "Membership", 'chama_checkbox_option_generator', $page_slug, "chama_display_section", $chama_display_membership_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_display_membership", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Display Crowdfunding?
        $chama_display_crowdfunding_args = [
            'option'         => 'chama_display_crowdfunding',
            'checkbox_value' => 'yes',
            'label'          => 'Select this option if you want your "Crowdfunding" options displayed.',
            'desc'           => 'If enabled, your "Crowdfunding" campaigns as well as the "Crowdfunding" page will be displayed as a monetization option',
        ];

        add_settings_field("chama_display_crowdfunding", "Crowdfunding", 'chama_checkbox_option_generator', $page_slug, "chama_display_section", $chama_display_crowdfunding_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_display_crowdfunding", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Display Commissions?
        $chama_display_commissions_args = [
            'option'         => 'chama_display_commissions',
            'checkbox_value' => 'yes',
            'label'          => 'Select this option if you want your "Commissions" options displayed.',
            'desc'           => 'If enabled, your "Commissions" listings as well as the "Commissions" page will be displayed as a monetization option',
        ];

        add_settings_field("chama_display_commissions", "Commissions", 'chama_checkbox_option_generator', $page_slug, "chama_display_section", $chama_display_commissions_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_display_commissions", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Display Number of Donors?
        $chama_display_donor_analytics_args = [
            'option'         => 'chama_display_donor_analytics',
            'checkbox_value' => 'yes',
            'label'          => 'Select this option if you want donor analytics displayed.',
            'desc'           => 'If enabled, the total number of your donors will be displayed on your monetization pages',
        ];

        add_settings_field("chama_display_donor_analytics", "Donor Analytics", 'chama_checkbox_option_generator', $page_slug, "chama_display_section", $chama_display_donor_analytics_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_display_donor_analytics", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Display Number of Members?
        $chama_display_member_analytics_args = [
            'option'         => 'chama_display_member_analytics',
            'checkbox_value' => 'yes',
            'label'          => 'Select this option if you want member analytics displayed.',
            'desc'           => 'If enabled, the total number of your members will be displayed on your monetization pages',
        ];

        add_settings_field("chama_display_member_analytics", "Member Analytics", 'chama_checkbox_option_generator', $page_slug, "chama_display_section", $chama_display_member_analytics_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_display_member_analytics", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Display Number of Funders?
        $chama_display_funder_analytics_args = [
            'option'         => 'chama_display_funder_analytics',
            'checkbox_value' => 'yes',
            'label'          => 'Select this option if you want funder analytics displayed.',
            'desc'           => 'If enabled, the total number of your funders will be displayed on your monetization pages',
        ];

        add_settings_field("chama_display_funder_analytics", "Funder Analytics", 'chama_checkbox_option_generator', $page_slug, "chama_display_section", $chama_display_funder_analytics_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_display_funder_analytics", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Display Number of Commissioners?
        $chama_display_commissioner_analytics_args = [
            'option'         => 'chama_display_commissioner_analytics',
            'checkbox_value' => 'yes',
            'label'          => 'Select this option if you want commissioner analytics displayed.',
            'desc'           => 'If enabled, the total number of your commissioners will be displayed on your monetization pages',
        ];

        add_settings_field("chama_display_commissioner_analytics", "Commissioner Analytics", 'chama_checkbox_option_generator', $page_slug, "chama_display_section", $chama_display_commissioner_analytics_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_display_commissioner_analytics", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Post types to display on hub page?
        $args = [
            'public' => true,

        ];
        $excluded_post_types  = ['page', 'attachment', 'chama_tier', 'chama_commission', 'chama_campaign', 'slide'];
        $post_types           = get_post_types($args);
        $filtered_post_types  = array_diff($post_types, $excluded_post_types);
        $key_value_post_types = [];
        foreach ($filtered_post_types as $post_type) {
            $key_value_post_types[$post_type] = ucfirst($post_type);
        }

        $chama_display_post_types_args = [
            'option'           => 'chama_display_post_types',
            'label'            => 'Select post types to display.',
            'desc'             => 'Determines which post types will be displayed on the hub page',
            'checkbox_options' => $key_value_post_types,
        ];

        add_settings_field("chama_display_post_types", "Post Types", 'chama_multi_select_checkbox_generator', $page_slug, "chama_display_section", $chama_display_post_types_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_display_post_types", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_array']);

    }
    /**
     * Admin 'Settings > Email Settings' page
     */
    //Initialize email input fields
    add_action('admin_init', 'chama_email_settings_fields');
    function chama_email_settings_fields()
    {
        $page_slug      = 'chama-email-settings-page';
        $settings_group = 'chama_email_settings_settings';

        //setup page section
        add_settings_section("chama_email_settings_section", '', '', $page_slug);

        //Email from name
        $chama_email_from_name_args = [
            'option' => 'chama_email_from_name',
            'desc'   => 'The name that emails come from. This is usually your site name.',
        ];
        add_settings_field("chama_email_from_name", "From Name", 'chama_textbox_option_generator', $page_slug, "chama_email_settings_section", $chama_email_from_name_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_email_from_name", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Email from address
        $chama_email_from_address_args = [
            'option' => 'chama_email_from_address',
            'desc'   => 'The email address to send emails from. This will act as the "from" and "reply-to" address.',
        ];
        add_settings_field("chama_email_from_address", "From Address", 'chama_email_option_generator', $page_slug, "chama_email_settings_section", $chama_email_from_address_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_email_from_address", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Admin Email address
        $chama_admin_email_args = [
            'option' => 'chama_admin_email',
            'desc'   => 'The email address where notifications of new payments will be sent',
        ];
        add_settings_field("chama_admin_email", "Admin Email Address", 'chama_email_option_generator', $page_slug, "chama_email_settings_section", $chama_admin_email_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_admin_email", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Enable email notifications for new uers
        $chama_new_user_notification_args = [
            'option'         => 'chama_new_user_notification',
            'checkbox_value' => 'yes',
            'label'          => 'Get email notifications of new users.',
            'desc'           => 'If enabled, an email will be sent to the admin alerting them of a new registration.',
        ];

        add_settings_field("chama_new_user_notification", "User notifications", 'chama_checkbox_option_generator', $page_slug, "chama_email_settings_section", $chama_new_user_notification_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_new_user_notification", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Enable email notifications for supporters for new payments
        $chama_supporter_enable_payment_notification_args = [
            'option'         => 'chama_supporter_enable_payment_notification',
            'checkbox_value' => 'yes',
            'label'          => 'Send payment receipts via email.',
            'desc'           => 'If enabled, your supporters will receive an email notification with a receipt of their payment.',
        ];

        add_settings_field("chama_supporter_enable_payment_notification", "Supporter - payment notifications", 'chama_checkbox_option_generator', $page_slug, "chama_email_settings_section", $chama_supporter_enable_payment_notification_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_supporter_enable_payment_notification", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Enable email notifications for renewals
        $chama_supporter_enable_renewal_notification_args = [
            'option'         => 'chama_supporter_enable_renewal_notification',
            'checkbox_value' => 'yes',
            'label'          => 'Send renewal receipts via email.',
            'desc'           => 'If enabled, your supporters will receive an email notification with a receipt of their renewal.',
        ];

        add_settings_field("chama_supporter_enable_renewal_notification", "Supporter - renewal notifications", 'chama_checkbox_option_generator', $page_slug, "chama_email_settings_section", $chama_supporter_enable_renewal_notification_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_supporter_enable_renewal_notification", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Enable email notifications for supporters for failed payments
        $chama_supporter_enable_failed_payment_notification_args = [
            'option'         => 'chama_supporter_enable_failed_payment_notification',
            'checkbox_value' => 'yes',
            'label'          => 'Send failed payment notifications.',
            'desc'           => 'If enabled, your supporters will receive an email notification whenever a payment fails to go through.',
        ];

        add_settings_field("chama_supporter_enable_failed_payment_notification", "Supporter - failed payment notifications", 'chama_checkbox_option_generator', $page_slug, "chama_email_settings_section", $chama_supporter_enable_failed_payment_notification_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_supporter_enable_failed_payment_notification", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Enable email notifications for admins for new payments
        $chama_admin_enable_payment_notification_args = [
            'option'         => 'chama_admin_enable_payment_notification',
            'checkbox_value' => 'yes',
            'label'          => 'Get email notifications of new payments.',
            'desc'           => 'If enabled, the admin email address will receive notifications of all payments.',
        ];

        add_settings_field("chama_admin_enable_payment_notification", "Admin notifications", 'chama_checkbox_option_generator', $page_slug, "chama_email_settings_section", $chama_admin_enable_payment_notification_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_admin_enable_payment_notification", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Email logo
        $chama_email_logo_args = [
            'option' => 'chama_email_logo',
            'desc'   => 'Upload an image that is no more than 300px wide that will be displayed at the top of your email notifications.',
            'img-id' => 'chama-email-logo',
        ];
        add_settings_field("chama_email_logo", "Header", 'chama_image_option_generator', $page_slug, "chama_email_settings_section", $chama_email_logo_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_email_logo", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    }
    /**
     * Admin 'Settings > Newsletter' page
     */
    //Initialize email input fields
    add_action('admin_init', 'chama_newsletter_fields');
    function chama_newsletter_fields()
    {
        $page_slug      = 'chama-newsletter-page';
        $settings_group = 'chama_newsletter_settings';

    //setup notification section
        add_settings_section(
            "chama_form_notification_section",
            '',
            function () {
                echo '<h1 style="font-weight: bold">Newsletter Signup Form Styles</h1><p>Use the settings below to customize the look and feel of your newsletter signup form. These options control how the form will appear to visitors on your website, so you can match it to your site’s design and branding.</p><p>You can place the newsletter form anywhere on your website using the shortcode: <code>[chama_newsletter_sign_up_form]</code>.</p><hr/>';
            },
            $page_slug
        );

    //setup form styles section
        add_settings_section(
            "chama_newsletter_form_styles_section",
            '',
            function () {
                echo '<h2 style="color:#2271b1; text-transform:uppercase;">Form Styles</h2><hr style="margin:1em 0;">';
            },
            $page_slug
        );

    //Form background color
        $chama_form_background_color_args = [
            'option' => 'chama_newsletter_form_background_color',
            'desc'   => 'Choose the color you’d like to use for your newsletter form’s background.',
        ];
        add_settings_field("chama_newsletter_form_background_color", "Form Background Color", 'chama_color_option_generator', $page_slug, "chama_newsletter_form_styles_section", $chama_form_background_color_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_form_background_color", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);
    //Form border color
        $chama_form_border_color_args = [
            'option' => 'chama_newsletter_form_border_color',
            'desc'   => 'Choose the color you’d like to use for your newsletter form’s border.',
        ];
        add_settings_field("chama_newsletter_form_border_color", "Form Border Color", 'chama_color_option_generator', $page_slug, "chama_newsletter_form_styles_section", $chama_form_border_color_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_form_border_color", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    //Border radius
        $chama_newsletter_form_border_radius_args = [
            'option' => 'chama_newsletter_form_border_radius',
            'desc'   => 'Set the border radius of your form (in pixels).',
        ];
        add_settings_field("chama_newsletter_form_border_radius", "Form Border Radius", 'chama_number_option_generator', $page_slug, "chama_newsletter_form_styles_section", $chama_newsletter_form_border_radius_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_form_border_radius", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_integer']);

    //Background transparency
        $chama_newsletter_form_background_transparency_args = [
            'option' => 'chama_newsletter_form_background_transparency',
            'desc'   => 'Enter a value (0–100%) to adjust the transparency of your form’s background image, if you have added one.',
        ];
        add_settings_field("chama_newsletter_form_background_transparency", "Form Background Transparency", 'chama_number_option_generator', $page_slug, "chama_newsletter_form_styles_section", $chama_newsletter_form_background_transparency_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_form_background_transparency", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_integer']);

    //Form background image
        $chama_newsletter_form_background_image_args = [
            'option' => 'chama_newsletter_form_background_image',
            'desc'   => 'Upload an image to use as the background for your newsletter form.',
            'img-id' => 'chama-newsletter-form-background-image',
        ];
        add_settings_field("chama_newsletter_form_background_image", "Background Image", 'chama_image_option_generator', $page_slug, "chama_newsletter_form_styles_section", $chama_newsletter_form_background_image_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_form_background_image", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    //setup header styles section
        add_settings_section(
            "chama_newsletter_header_styles_section",
            '',
            function () {
                echo '<h2 style="color:#2271b1; text-transform:uppercase;">Header Styles</h2><hr style="margin:1em 0;">';
            },
            $page_slug
        );

    //Header font color
        $chama_header_font_color_args = [
            'option' => 'chama_newsletter_header_font_color',
            'desc'   => 'Choose a color for the text in your newsletter form’s header.',
        ];
        add_settings_field("chama_newsletter_header_font_color", "Header Font Color", 'chama_color_option_generator', $page_slug, "chama_newsletter_header_styles_section", $chama_header_font_color_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_header_font_color", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    //Header text
        $chama_newsletter_form_header_text_args = [
            'option' => 'chama_newsletter_form_header_text',
            'desc'   => null,
        ];
        add_settings_field("chama_newsletter_form_header_text", "Header Text", 'chama_textbox_option_generator', $page_slug, "chama_newsletter_header_styles_section", $chama_newsletter_form_header_text_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_form_header_text", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    //Header font size
        $chama_newsletter_header_font_size_args = [
            'option' => 'chama_newsletter_header_font_size',
            'desc'   => 'Set the font size for the form’s header (in pixels).',
        ];
        add_settings_field("chama_newsletter_header_font_size", "Header Font Size", 'chama_number_option_generator', $page_slug, "chama_newsletter_header_styles_section", $chama_newsletter_header_font_size_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_header_font_size", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_integer']);

    //Header font weight
        $key_value_font_weights                   = ['normal' => 'Normal', 'bold' => 'Bold'];
        $chama_newsletter_header_font_weight_args = [
            'option'         => 'chama_newsletter_header_font_weight',
            'label'          => 'Font weight',
            'desc'           => 'Set the font weight for the form’s header.',
            'select_options' => $key_value_font_weights,
        ];
        add_settings_field("chama_newsletter_header_font_weight", "Header Font Weight", 'chama_select_dropdown_generator', $page_slug, "chama_newsletter_header_styles_section", $chama_newsletter_header_font_weight_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_header_font_weight", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    //setup sub-header styles section
        add_settings_section(
            "chama_newsletter_sub_header_styles_section",
            '',
            function () {
                echo '<h2 style="color:#2271b1; text-transform:uppercase;">Sub Header Styles</h2><hr style="margin:1em 0;">';
            },
            $page_slug
        );

    //Sub Header font color
        $chama_sub_header_font_color_args = [
            'option' => 'chama_newsletter_sub_header_font_color',
            'desc'   => 'Choose a color for the text in your newsletter form’s sub-header.',
        ];
        add_settings_field("chama_newsletter_sub_header_font_color", "Sub Header Font Color", 'chama_color_option_generator', $page_slug, "chama_newsletter_sub_header_styles_section", $chama_sub_header_font_color_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_sub_header_font_color", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    //Sub header text
        $chama_newsletter_form_sub_header_text_args = [
            'option' => 'chama_newsletter_form_sub_header_text',
            'desc'   => null,
        ];
        add_settings_field("chama_newsletter_form_sub_header_text", "Sub Header Text", 'chama_textbox_option_generator', $page_slug, "chama_newsletter_sub_header_styles_section", $chama_newsletter_form_sub_header_text_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_form_sub_header_text", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    //Sub Header font size
        $chama_newsletter_sub_header_font_size_args = [
            'option' => 'chama_newsletter_sub_header_font_size',
            'desc'   => 'Set the font size for the form’s sub header (in pixels).',
        ];
        add_settings_field("chama_newsletter_sub_header_font_size", "Sub Header Font Size", 'chama_number_option_generator', $page_slug, "chama_newsletter_sub_header_styles_section", $chama_newsletter_sub_header_font_size_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_sub_header_font_size", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_integer']);

    //setup email field styles section
        add_settings_section(
            "chama_newsletter_email_styles_section",
            '',
            function () {
                echo '<h2 style="color:#2271b1; text-transform:uppercase;">Email Field Styles</h2><hr style="margin:1em 0;">';
            },
            $page_slug
        );

    //Email font color
        $chama_email_font_color_args = [
            'option' => 'chama_newsletter_email_font_color',
            'desc'   => 'Choose a color for the text that appears in the email input field of your newsletter form.',
        ];
        add_settings_field("chama_newsletter_email_font_color", "Email Field Font Color", 'chama_color_option_generator', $page_slug, "chama_newsletter_email_styles_section", $chama_email_font_color_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_email_font_color", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    //Email border color
        $chama_email_border_color_args = [
            'option' => 'chama_newsletter_email_border_color',
            'desc'   => 'Choose a color for the border around the email input field in your newsletter form.',
        ];
        add_settings_field("chama_newsletter_email_border_color", "Email Field Border Color", 'chama_color_option_generator', $page_slug, "chama_newsletter_email_styles_section", $chama_email_border_color_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_email_border_color", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    //Email field label
        $chama_newsletter_email_field_label_args = [
            'option' => 'chama_newsletter_email_field_label',
            'desc'   => null,
        ];
        add_settings_field("chama_newsletter_email_field_label", "Email Field Label", 'chama_textbox_option_generator', $page_slug, "chama_newsletter_email_styles_section", $chama_newsletter_email_field_label_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_email_field_label", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    //Email Field Border radius
        $chama_newsletter_email_field_border_radius_args = [
            'option' => 'chama_newsletter_email_field_border_radius',
            'desc'   => 'Set the border radius around the email input field (in pixels).',
        ];
        add_settings_field("chama_newsletter_email_field_border_radius", "Email Field Border Radius", 'chama_number_option_generator', $page_slug, "chama_newsletter_email_styles_section", $chama_newsletter_email_field_border_radius_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_email_field_border_radius", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_integer']);

    //Email field font weight
        $key_value_font_weights       = ['normal' => 'Normal', 'bold' => 'Bold'];
        $chama_email_font_weight_args = [
            'option'         => 'chama_newsletter_email_font_weight',
            'label'          => 'Font weight',
            'desc'           => 'Set the font weight for the email input field.',
            'select_options' => $key_value_font_weights,
        ];
        add_settings_field("chama_newsletter_email_font_weight", "Email Field Font Weight", 'chama_select_dropdown_generator', $page_slug, "chama_newsletter_email_styles_section", $chama_email_font_weight_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_email_font_weight", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    //setup button field styles section
        add_settings_section(
            "chama_newsletter_button_styles_section",
            '',
            function () {
                echo '<h2 style="color:#2271b1; text-transform:uppercase;">Button Styles</h2><hr style="margin:1em 0;">';
            },
            $page_slug
        );

    //Button font color
        $chama_button_font_color_args = [
            'option' => 'chama_newsletter_button_font_color',
            'desc'   => 'Choose a color for the text on your newsletter form’s button.',
        ];
        add_settings_field("chama_newsletter_button_font_color", "Button Font Color", 'chama_color_option_generator', $page_slug, "chama_newsletter_button_styles_section", $chama_button_font_color_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_button_font_color", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    //Button background color
        $chama_button_background_color_args = [
            'option' => 'chama_newsletter_button_background_color',
            'desc'   => 'Choose a color for the background on your newsletter form’s button.',
        ];
        add_settings_field("chama_newsletter_button_background_color", "Button Background Color", 'chama_color_option_generator', $page_slug, "chama_newsletter_button_styles_section", $chama_button_background_color_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_button_background_color", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    //Button Border radius
        $chama_newsletter_button_border_radius_args = [
            'option' => 'chama_newsletter_button_border_radius',
            'desc'   => 'Set the border radius around the email input field (in pixels).',
        ];
        add_settings_field("chama_newsletter_button_border_radius", "Button Border Radius", 'chama_number_option_generator', $page_slug, "chama_newsletter_button_styles_section", $chama_newsletter_button_border_radius_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_button_border_radius", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_integer']);

    //Button font weight
        $key_value_font_weights        = ['normal' => 'Normal', 'bold' => 'Bold'];
        $chama_button_font_weight_args = [
            'option'         => 'chama_newsletter_button_font_weight',
            'label'          => 'Font weight',
            'desc'           => 'Set the font weight for the form’s button.',
            'select_options' => $key_value_font_weights,
        ];
        add_settings_field("chama_newsletter_button_font_weight", "Button Font Weight", 'chama_select_dropdown_generator', $page_slug, "chama_newsletter_button_styles_section", $chama_button_font_weight_args);
    // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_newsletter_button_font_weight", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    }


    /**
     * Admin 'Settings > Email Templates' page
     */
    //Initialize email input fields
    add_action('admin_init', 'chama_email_templates_fields');
    function chama_email_templates_fields()
    {
        $page_slug      = 'chama-email-templates-page';
        $settings_group = 'chama_email_templates_settings';

        //setup page section
        add_settings_section("chama_email_templates_section", '', '', $page_slug);

        //Supporter - Donation processing
        $chama_donation_processing_args = [
            'option' => 'chama_donation_processing_template',
            'desc'   => 'Available template tags for this email:<br/><code>{date}</code> - The date of the donation.<br/><code>{amount}</code> - The total donation amount.<br><code>{receipt_id}</code> - The receipt number for the donation.<br/>',
            'rows'   => 15,
        ];
        add_settings_field("chama_donation_processing_template", "Donation Processing", 'chama_rich_text_option_generator', $page_slug, "chama_email_templates_section", $chama_donation_processing_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_donation_processing_template", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_rich_text_editor']);
        //Supporter - Donation receipt
        $chama_donation_processing_args = [
            'option' => 'chama_donation_receipt_template',
            'desc'   => 'Available template tags for this email:<br/><code>{date}</code> - The date of the donation.<br/><code>{amount}</code> - The total donation amount.<br><code>{receipt_id}</code> - The receipt number for the donation.<br/>',
            'rows'   => 15,
        ];
        add_settings_field("chama_donation_receipt_template", "Donation Receipt", 'chama_rich_text_option_generator', $page_slug, "chama_email_templates_section", $chama_donation_processing_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_donation_receipt_template", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_rich_text_editor']);

        //Admin - New Donation
        $chama_donation_processing_args = [
            'option' => 'chama_new_donation_template',
            'desc'   => 'Available template tags for this email:<br/><code>{date}</code> - The date of the donation.<br/><code>{amount}</code> - The total donation amount.<br><code>{receipt_id}</code> - The receipt number for the donation.<br/>',
            'rows'   => 15,
        ];
        add_settings_field("chama_new_donation_template", "New Donation", 'chama_rich_text_option_generator', $page_slug, "chama_email_templates_section", $chama_donation_processing_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_new_donation_template", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_rich_text_editor']);

        //Supporter - Subscription payment processing
        $chama_subscription_processing_args = [
            'option' => 'chama_subscription_processing_template',
            'desc'   => 'Available template tags for this email:<br/><code>{description}</code> - Subscription tier.<br/><code>{date}</code> - The date of the subscription.<br/><code>{renewal-date}</code> - The next payment for renewal.<br/><code>{amount}</code> - The total subscription amount.<br><code>{receipt_id}</code> - The receipt number for the subscription.<br/>',
            'rows'   => 15,
        ];
        add_settings_field("chama_subscription_processing_template", "Subscription Payment Processing", 'chama_rich_text_option_generator', $page_slug, "chama_email_templates_section", $chama_subscription_processing_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_subscription_processing_template", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_rich_text_editor']);

        //Supporter - Subscription payment receipt
        $chama_subscription_processing_args = [
            'option' => 'chama_subscription_receipt_template',
            'desc'   => 'Available template tags for this email:<br/><code>{description}</code> - Subscription tier.<br/><code>{date}</code> - The date of the subscription.<br/><code>{renewal-date}</code> - The next payment for renewal.<br/><code>{amount}</code> - The total subscription amount.<br><code>{receipt_id}</code> - The receipt number for the subscription.<br/>',
            'rows'   => 15,
        ];
        add_settings_field("chama_subscription_receipt_template", "Subscription Payment Receipt", 'chama_rich_text_option_generator', $page_slug, "chama_email_templates_section", $chama_subscription_processing_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_subscription_receipt_template", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_rich_text_editor']);

        //Admin - New Subscription payment
        $chama_subscription_processing_args = [
            'option' => 'chama_new_subscription_template',
            'desc'   => 'Available template tags for this email:<br/><code>{description}</code> - Subscription tier.<br/><code>{date}</code> - The date of the subscription.<br/><code>{renewal-date}</code> - The next payment for renewal.<br/><code>{amount}</code> - The total subscription amount.<br><code>{receipt_id}</code> - The receipt number for the subscription.<br/>',
            'rows'   => 15,
        ];
        add_settings_field("chama_new_subscription_template", "New Subscription Payment", 'chama_rich_text_option_generator', $page_slug, "chama_email_templates_section", $chama_subscription_processing_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_new_subscription_template", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_rich_text_editor']);

        //Supporter - Crowdfunding payment processing
        $chama_fund_processing_args = [
            'option' => 'chama_fund_processing_template',
            'desc'   => 'Available template tags for this email:<br/><code>{description}</code> - Title of the crowdfunding campaign.<br/><code>{date}</code> - The date of the fund.<br/><code>{amount}</code> - The total fund amount.<br><code>{receipt_id}</code> - The receipt number for the fund.<br/>',
            'rows'   => 15,
        ];
        add_settings_field("chama_fund_processing_template", "Crowdfunding Payment Processing", 'chama_rich_text_option_generator', $page_slug, "chama_email_templates_section", $chama_fund_processing_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_fund_processing_template", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_rich_text_editor']);

        //Supporter - Crowdfunding payment receipt
        $chama_fund_processing_args = [
            'option' => 'chama_fund_receipt_template',
            'desc'   => 'Available template tags for this email:<br/><code>{description}</code> - Title of the crowdfunding campaign.<br/><code>{date}</code> - The date of the fund.<br/><code>{amount}</code> - The total fund amount.<br><code>{receipt_id}</code> - The receipt number for the fund.<br/>',
            'rows'   => 15,
        ];
        add_settings_field("chama_fund_receipt_template", "Crowdfunding Payment Receipt", 'chama_rich_text_option_generator', $page_slug, "chama_email_templates_section", $chama_fund_processing_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_fund_receipt_template", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_rich_text_editor']);

        //Admin - New Crowdfunding payment
        $chama_fund_processing_args = [
            'option' => 'chama_new_fund_template',
            'desc'   => 'Available template tags for this email:<br/><code>{description}</code> - Title of the crowdfunding campaign.<br/><code>{date}</code> - The date of the fund.<br/><code>{amount}</code> - The total fund amount.<br><code>{receipt_id}</code> - The receipt number for the fund.<br/>',
            'rows'   => 15,
        ];
        add_settings_field("chama_new_fund_template", "New Crowdfunding Payment", 'chama_rich_text_option_generator', $page_slug, "chama_email_templates_section", $chama_fund_processing_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_new_fund_template", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_rich_text_editor']);

        //Commission payment processing
        $chama_com_order_processing_args = [
            'option' => 'chama_com_order_processing_template',
            'desc'   => 'Available template tags for this email:<br/><code>{price}</code> - Base cost of the commission.<br/><code>{description}</code> - Name of the commission.<br/><code>{date}</code> - The date of the commission order.<br/><code>{amount}</code> - The total amount paid for the commission.<br><code>{receipt_id}</code> - The receipt number for the commission order.<br><code>{request}</code> - The request for the commission order.<br><code>{add_ons}</code> - The add ons for the commission order.<br><code>{shipping-location}</code> - The shipping location for the commission order.<br><code>{shipping-fee}</code> - The shipping fee for the commission order.<br/>',
            'rows'   => 15,
        ];
        add_settings_field("chama_com_order_processing_template", "Commission Order Processing", 'chama_rich_text_option_generator', $page_slug, "chama_email_templates_section", $chama_com_order_processing_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_com_order_processing_template", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_rich_text_editor']);

        //Commission payment receipt
        $chama_com_order_processing_args = [
            'option' => 'chama_com_order_receipt_template',
            'desc'   => 'Available template tags for this email:<br/><code>{price}</code> - Base cost of the commission.<br/><code>{description}</code> - Name of the commission.<br/><code>{date}</code> - The date of the commission order.<br/><code>{amount}</code> - The total amount paid for the commission.<br><code>{receipt_id}</code> - The receipt number for the commission order.<br><code>{request}</code> - The request for the commission order.<br><code>{add_ons}</code> - The add ons for the commission order.<br><code>{shipping-location}</code> - The shipping location for the commission order.<br><code>{shipping-fee}</code> - The shipping fee for the commission order.<br/>',
            'rows'   => 15,
        ];
        add_settings_field("chama_com_order_receipt_template", "Commission Order Receipt", 'chama_rich_text_option_generator', $page_slug, "chama_email_templates_section", $chama_com_order_processing_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_com_order_receipt_template", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_rich_text_editor']);

        //Admin - New Commission payment
        $chama_com_order_processing_args = [
            'option' => 'chama_new_commission_order_template',
            'desc'   => 'Available template tags for this email:<br/><code>{price}</code> - Base cost of the commission.<br/><code>{description}</code> - Name of the commission.<br/><code>{date}</code> - The date of the commission order.<br/><code>{amount}</code> - The total amount paid for the commission.<br><code>{receipt_id}</code> - The receipt number for the commission order.<br><code>{request}</code> - The request for the commission order.<br><code>{add_ons}</code> - The add ons for the commission order.<br><code>{shipping-location}</code> - The shipping location for the commission order.<br><code>{shipping-fee}</code> - The shipping fee for the commission order.<br/>',
            'rows'   => 15,
        ];
        add_settings_field("chama_new_commission_order_template", "New Commission Order", 'chama_rich_text_option_generator', $page_slug, "chama_email_templates_section", $chama_com_order_processing_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_new_commission_order_template", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_rich_text_editor']);

        //Supporter - Payment failure notification
        $chama_payment_failure_args = [
            'option' => 'chama_payment_failure_template',
            'desc'   => 'Available template tags for this email:<br/><code>{description}</code> - Subscription tier.<br/><code>{renewal-date}</code> - The next payment for renewal.<br/><code>{amount}</code> - The total subscription amount due.<br>',
            'rows'   => 15,
        ];
        add_settings_field("chama_payment_failure_template", "Payment Failure Notification", 'chama_rich_text_option_generator', $page_slug, "chama_email_templates_section", $chama_payment_failure_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_payment_failure_template", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_rich_text_editor']);

        //Supporter - Subscription cancellation
        $chama_subscription_cancellation_args = [
            'option' => 'chama_subscription_cancellation_template',
            'desc'   => 'Available template tags for this email:<br/><code>{description}</code> - Either the reason for cancellation(when changing a tier) or when the subscription will end(when simply ending the subscription) .<br/>',
            'rows'   => 15,
        ];
        add_settings_field("chama_subscription_cancellation_template", "Subscription Cancellation Notification", 'chama_rich_text_option_generator', $page_slug, "chama_email_templates_section", $chama_subscription_cancellation_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_subscription_cancellation_template", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_rich_text_editor']);

    }
    /**
     * Admin 'Settings > Payment Limits' page
     */
    //Initialize payment limit input fields
    add_action('admin_init', 'chama_payment_limit_fields');
    function chama_payment_limit_fields()
    {
        $page_slug      = 'chama-limits-page';
        $settings_group = 'chama_limits_settings';

        //setup page section
        add_settings_section("chama_limits_section", '', '', $page_slug);

        //Minimum donation limit
        $chama_donation_min_limit_args = [
            'option' => 'chama_donation_min_limit',
            'desc'   => 'Limits the minimum amount that can be donated.',
        ];
        add_settings_field("chama_donation_min_limit", "Minimum Donation Amount", 'chama_number_option_generator', $page_slug, "chama_limits_section", $chama_donation_min_limit_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_donation_min_limit", ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_money']);

    }
    /**
     * Admin 'Settings > Content' page
     */
    //Initialize page content input fields
    add_action('admin_init', 'chama_content_fields');
    function chama_content_fields()
    {
        $page_slug      = 'chama-content-page';
        $settings_group = 'chama_content_settings';

        //setup page section
        add_settings_section("chama_content_section", '', '', $page_slug);
        //Name
        $chama_bio_args = [
            'option' => 'chama_name',
            'desc'   => 'Enter the name you are known by: either your full name, nickname or business name. This will be used on all your monetization pages.',
        ];
        add_settings_field("chama_name", "Name", 'chama_textbox_option_generator', $page_slug, "chama_content_section", $chama_bio_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_name", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Introduction/Bio
        $chama_bio_args = [
            'option' => 'chama_introduction',
            'desc'   => 'A brief introduction about who you are. This will be displayed on the hub page. Max of 160 characters.',
            'max'    => 160,
        ];
        add_settings_field("chama_introduction", "Bio", 'chama_textarea_option_generator', $page_slug, "chama_content_section", $chama_bio_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_introduction", ['type' => 'string', 'sanitize_callback' => 'sanitize_textarea_field']);

        //Avatar
        $chama_avatar_args = [
            'option' => 'chama_avatar',
            'desc'   => 'Upload a 400 by 400px image that will be used as an avatar on your monetization pages.',
            'img-id' => 'chama-avatar',
        ];
        add_settings_field("chama_avatar", "Avatar", 'chama_image_option_generator', $page_slug, "chama_content_section", $chama_avatar_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_avatar", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Page header
        $chama_header_args = [
            'option' => 'chama_header',
            'desc'   => 'Upload a 1200 by 400px image that will be used as the header for your monetization pages.',
            'img-id' => 'chama-header',
        ];
        add_settings_field("chama_header", "Header", 'chama_image_option_generator', $page_slug, "chama_content_section", $chama_header_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_header", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);
    }
    /**
     * Admin 'Settings > Branding' page
     */
    //Initialize branding input fields
    add_action('admin_init', 'chama_branding_fields');
    function chama_branding_fields()
    {
        $page_slug      = 'chama-branding-page';
        $settings_group = 'chama_branding_settings';

        //setup page section
        add_settings_section("chama_branding_section", '', '', $page_slug);

        //Accent background color
        $chama_accent_background_color_args = [
            'option' => 'chama_accent_background_color',
            'desc'   => 'This is the color that will be used for background of elements like buttons, headers and progress bars.',
        ];
        add_settings_field("chama_accent_background_color", "Accent Background Color", 'chama_color_option_generator', $page_slug, "chama_branding_section", $chama_accent_background_color_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_accent_background_color", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Accent text color
        $chama_accent_text_color_args = [
            'option' => 'chama_accent_text_color',
            'desc'   => 'This is the color of the text that appears on page elements that have the accent background color, e.g. buttons, headers and progress bars.',
        ];
        add_settings_field("chama_accent_text_color", "Accent Text Color", 'chama_color_option_generator', $page_slug, "chama_branding_section", $chama_accent_text_color_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_accent_text_color", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Content background color
        $chama_content_background_color_args = [
            'option' => 'chama_content_background_color',
            'desc'   => 'This is the color that will be used for background of the content area for your account and monetization pages.',
        ];
        add_settings_field("chama_content_background_color", "Content Background Color", 'chama_color_option_generator', $page_slug, "chama_branding_section", $chama_content_background_color_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_content_background_color", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Secondary Content background color
        $chama_content_background_secondary_color_args = [
            'option' => 'chama_content_background_secondary_color',
            'desc'   => 'This is the color that will be used for background of the content area in some sections of the monetization pages. For example, the order summary area.',
        ];
        add_settings_field("chama_content_background_secondary_color", "Content Background Secondary Color", 'chama_color_option_generator', $page_slug, "chama_branding_section", $chama_content_background_secondary_color_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_content_background_secondary_color", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Content text color
        $chama_content_text_color_args = [
            'option' => 'chama_content_text_color',
            'desc'   => 'This is the color of the text that appears in the content area for your account and monetization pages.',
        ];
        add_settings_field("chama_content_text_color", "Content Text Color", 'chama_color_option_generator', $page_slug, "chama_branding_section", $chama_content_text_color_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_content_text_color", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    }
    /**
     * Admin 'Settings > Miscellaneous' page
     */
    function chama_miscellaneous_fields()
    {
        $page_slug      = 'chama-miscellaneous-page';
        $settings_group = 'chama_miscellaneous_settings';

        //setup page section
        add_settings_section("chama_miscellaneous_section", '', '', $page_slug);

        //Delete plugin data
        $chama_delete_plugin_data_args = [
            'option'         => 'chama_delete_plugin_data',
            'checkbox_value' => 'yes',
            'label'          => 'Remove all ChamaWP data.',
            'desc'           => 'If enabled, all ChamaWP data will be removed when ChamaWP plugin is deleted. Note that the data in your Stripe account will not be affected.',
        ];

        add_settings_field("chama_delete_plugin_data", "Delete plugin data", 'chama_checkbox_option_generator', $page_slug, "chama_miscellaneous_section", $chama_delete_plugin_data_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_delete_plugin_data", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Option to add subscriptions and donations to crowdfunding campaign
        $chama_active_crowdfunding_campaign_args = [
            'option' => 'chama_active_crowdfunding_campaign',
            'cpt'    => 'chama_campaign',
            'desc'   => 'Select the crowdfunding campaign that you would like all donations and subscriptions to be added to. If you do not want donations and subscriptions to be added to any campaign, simply leave this field blank',
        ];
        add_settings_field("chama_active_crowdfunding_campaign", "Active Crowdfunding Campaign", 'chama_post_select_option_generator', $page_slug, "chama_miscellaneous_section", $chama_active_crowdfunding_campaign_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_active_crowdfunding_campaign", ['type' => 'string', 'sanitize_callback' => 'absint']);

    }
    add_action('admin_init', 'chama_miscellaneous_fields');
    /**
     * Admin 'Settings > Analytics' page
     */
    //Initialize analytics input fields
    add_action('admin_init', 'chama_analytics_fields');
    function chama_analytics_fields()
    {
        $page_slug      = 'chama-analytics-page';
        $settings_group = 'chama_analytics_settings';

        //setup page section
        add_settings_section("chama_analytics_section", '', '', $page_slug);

        //Donor analytics
        $chama_total_donors_args = [
            'option' => 'chama_total_donors',
        ];
        add_settings_field("chama_total_donors", "Total Donors", 'chama_hidden_option_generator', $page_slug, "chama_analytics_section", $chama_total_donors_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_total_donors", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Member analytics
        $chama_total_members_args = [
            'option' => 'chama_total_members',
        ];
        add_settings_field("chama_total_members", "Total Members", 'chama_hidden_option_generator', $page_slug, "chama_analytics_section", $chama_total_members_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_total_members", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Funder analytics
        $chama_total_funders_args = [
            'option' => 'chama_total_funders',
        ];
        add_settings_field("chama_total_funders", "Total Funders", 'chama_hidden_option_generator', $page_slug, "chama_analytics_section", $chama_total_funders_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_total_funders", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Commissioner analytics
        $chama_total_commissioners_args = [
            'option' => 'chama_total_commissioners',
        ];
        add_settings_field("chama_total_commissioners", "Total Commissioners", 'chama_hidden_option_generator', $page_slug, "chama_analytics_section", $chama_total_commissioners_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_total_commissioners", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

    }
    /**
     * Admin 'Settings > Protection' page
     */
    function chama_protection_fields()
    {
        $page_slug      = 'chama-protection-page';
        $settings_group = 'chama_protection_settings';

        //setup page section
        add_settings_section("chama_protection_section", '', '', $page_slug);

        //Require membership for commenting
        $chama_require_membership_for_comments_args = [
            'option'         => 'chama_require_mebership_for_comments',
            'checkbox_value' => 'yes',
            'label'          => 'Require that only members can comment.',
            'desc'           => 'If enabled, users who are not members who will not be able to comment.',
        ];

        add_settings_field("chama_require_mebership_for_comments", "Commenting for members only", 'chama_checkbox_option_generator', $page_slug, "chama_protection_section", $chama_require_membership_for_comments_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_require_mebership_for_comments", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Hide comment below "more tag"
        $chama_more_tag_protection_args = [
            'option'         => 'chama_more_tag_protection',
            'checkbox_value' => 'yes',
            'label'          => 'Hide content below the more tag if the content is protected.',
            'desc'           => 'Enables or disables "more" tag protection in the content. Anything after the "more" tag is protected. Anything before the "more" tag is teaser content.',
        ];

        add_settings_field("chama_more_tag_protection", "Enable more tag protection", 'chama_checkbox_option_generator', $page_slug, "chama_protection_section", $chama_more_tag_protection_args);
        // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
        register_setting($settings_group, "chama_more_tag_protection", ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);

        //Post protection levels
        $args = [
            'public' => true,

        ];
        $excluded_post_types  = ['chama_tier', 'chama_commission', 'chama_campaign', 'attachment', 'slide', 'page'];
        $post_types           = get_post_types($args);
        $filtered_post_types  = array_diff($post_types, $excluded_post_types);
        $key_value_post_types = [];
        foreach ($filtered_post_types as $post_type) {
            $option          = 'chama_' . $post_type . '_post_type';
            $label           = ucfirst($post_type);
            $chama_post_args = [
                'option' => $option,
            ];
            add_settings_field($option, $label, 'chama_tier_select_option_generator', $page_slug, "chama_protection_section", $chama_post_args);
            // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
            register_setting($settings_group, $option, ['type' => 'string', 'sanitize_callback' => 'chama_sanitize_float']);
        }

    }
    add_action('admin_init', 'chama_protection_fields');

    /**
     * Options - checkbox generator
     */

    function chama_checkbox_option_generator(array $args)
    {

        $option         = $args['option'];
        $checkbox_value = $args['checkbox_value'];
        $label          = $args['label'];
        $desc           = $args['desc'];
        $db_value       = get_option($option);
    ?>
         <label for="<?php echo esc_attr($option) ?>">
             <input
                 name="<?php echo esc_attr($option) ?>"
                 id="<?php echo esc_attr($option) ?>"
                 value="<?php echo esc_attr($checkbox_value) ?>"
                 type="checkbox"
                 <?php checked($checkbox_value, $db_value); ?>
             />
 <?php echo esc_html($label); ?>
         </label>
         <?php if ($desc): ?>
         <p><em><?php echo esc_html($desc); ?></em></p>
         <?php endif; ?>
<?php
    }
    /**
     * Options - textbox generator
     */
    function chama_textbox_option_generator(array $args)
    {
        $option = $args['option'];
        $desc   = $args['desc'];
        $value  = get_option($option);
    ?>
     <input type="text" name="<?php echo esc_attr($option) ?>" value="<?php echo esc_attr($value) ?>" />
     <?php if ($desc): ?>
         <p><em><?php echo esc_html($desc); ?></em></p>
     <?php endif; ?>
<?php
    }
    /**
     * Options - textarea generator
     */
    function chama_textarea_option_generator(array $args)
    {
        $option = $args['option'];
        $desc   = $args['desc'];
        $max    = $args['max'];
        $value  = get_option($option);

    ?>
    <textarea  name="<?php echo esc_attr($option); ?>" id="<?php echo esc_attr($option); ?>" maxlength="<?php echo esc_attr($max); ?>" cols="90" rows="7"><?php echo esc_html("$value"); ?></textarea>
     <?php if ($desc): ?>
         <p><em><?php echo esc_html($desc); ?></em></p>
     <?php endif; ?>
<?php
    }
    /**
     * Options - rich text generator
     */
    function chama_rich_text_option_generator(array $args)
    {
        $option      = $args['option'];
        $desc        = $args['desc'];
        $rows        = $args['rows'];
        $value       = get_option($option);
        $editor_args = [
            'teeny'          => true,
            'textarea_rows'  => $rows,
            'default_editor' => 'QuickTags',
        ];
        wp_editor(wp_kses_post($value), esc_attr($option), $editor_args);
        $allowed_tags = [
            'code' => [],
            'br'   => [],
        ];
    ?>

     <?php if ($desc): ?>
        <div class="chama-email-template-tags">
        <?php echo wp_kses($desc, $allowed_tags); ?>
<?php endif; ?>
        </div>

     <?php
         }
         /**
          * Options - email input generator
          */
         function chama_email_option_generator(array $args)
         {
             $option = $args['option'];
             $desc   = $args['desc'];
             $value  = get_option($option);
         ?>
      <input type="email" name="<?php echo esc_attr($option) ?>" value="<?php echo esc_attr($value) ?>" />
      <?php if ($desc): ?>
          <p><em><?php echo esc_html($desc); ?></em></p>
      <?php endif; ?>
<?php
    }
    /**
     * Options - url input generator
     */
    function chama_url_option_generator(array $args)
    {
        $option = $args['option'];
        $desc   = $args['desc'];
        $value  = get_option($option);
    ?>
      <input type="url" name="<?php echo esc_attr($option) ?>" value="<?php echo esc_attr($value) ?>" />
      <?php if ($desc): ?>
          <p><em><?php echo esc_html($desc); ?></em></p>
      <?php endif; ?>
<?php
    }
    /**
     * Options - number input generator
     */
    function chama_number_option_generator(array $args)
    {
        $option = $args['option'];
        $desc   = $args['desc'];
        $value  = get_option($option);
    ?>
      <input type="number" name="<?php echo esc_attr($option) ?>" value="<?php echo esc_attr($value) ?>" />
      <?php if ($desc): ?>
          <p><em><?php echo esc_html($desc); ?></em></p>
      <?php endif; ?>
<?php
    }
    /**
     * Options - textbox generator
     */
    function chama_color_option_generator(array $args)
    {
        $option = $args['option'];
        $desc   = $args['desc'];
        $value  = get_option($option);
    ?>
     <input type="text" name="<?php echo esc_attr($option) ?>" value="<?php echo esc_attr($value) ?>" class="color-picker" />
     <?php if ($desc): ?>
         <p><em><?php echo esc_html($desc); ?></em></p>
     <?php endif; ?>
<?php
    }
    /**
     * Options - image upload generator
     */
    function chama_image_option_generator(array $args)
    {
        $option = $args['option'];
        $desc   = $args['desc'];
        $value  = get_option($option);
        $img_id = $args['img-id'];
    ?>
    <div class="chama-preview-image-wrapper">
    <img id="<?php echo esc_attr($img_id) ?>" style="max-height:120px"
        src="<?php echo esc_url((! empty($value) ? $value : CHAMA_ASSETS_URL . 'img/no-image.png')); ?>" />
</div>
<?php if (! empty($value)): ?>
    <input class="chama-delete-image" type="button" value="Remove image"  data-noimage="<?php echo esc_attr(CHAMA_ASSETS_URL . 'img/no-image.png') ?>" data-hidden="<?php echo esc_attr($option) ?>" data-image="<?php echo esc_attr($img_id) ?>"><br/><br/>
<?php endif?>

    <input class="chama-image-uploader" type="button" value="Choose an image"  data-hidden="<?php echo esc_attr($option) ?>" data-image="<?php echo esc_attr($img_id) ?>">
<input type="hidden" id="<?php echo esc_attr($option) ?>" name="<?php echo esc_attr($option) ?>" value="<?php echo esc_attr($value) ?>" />
     <?php if ($desc): ?>
         <p><em><?php echo esc_html($desc); ?></em></p>
     <?php endif; ?>
<?php
    }
    /**
     * Multi select checkbox
     */
    function chama_multi_select_checkbox_generator(array $args)
    {
        $html             = '';
        $option           = $args['option'];
        $desc             = $args['desc'];
        $value            = get_option($option) ? get_option($option) : [];
        $checkbox_options = $args['checkbox_options'];

        foreach ($checkbox_options as $k => $v) {

            $checked = false;
            if (in_array($k, $value)) {
                $checked = true;
            }
            $html .= '<label for="' . esc_attr($option . '_' . $k) . '"><input type="checkbox" ' . checked($checked, true, false) . ' name="' . esc_attr($option) . '[]" value="' . esc_attr($k) . '" id="' . esc_attr($option . '_' . $k) . '" /> ' . $v . '</label> ';
        }
        $html .= $desc ? ' <p><em>' . esc_html($desc) . '</em></p>' : '';
        echo wp_kses($html, chama_allowed_html());

    }
    /**
     * Select dropdown
     */
    function chama_select_dropdown_generator(array $args)
    {
        $html           = '';
        $option         = $args['option'];
        $desc           = $args['desc'];
        $value          = get_option($option) ? get_option($option) : [];
        $select_options = $args['select_options'];

        $html .= '<select name="' . esc_attr($option) . '" id="' . esc_attr($option) . '">';
        foreach ($select_options as $k => $v) {
            $selected = false;
            if ($k == $value) {
                $selected = true;
            }
            $html .= '<option ' . selected($selected, true, false) . ' value="' . esc_attr($k) . '">' . $v . '</option>';
        }
        $html .= '</select> ';
        $html .= $desc ? ' <p><em>' . esc_html($desc) . '</em></p>' : '';
        echo wp_kses($html, chama_allowed_html());
    }
    /**
     * Options - number input generator
     */
    function chama_hidden_option_generator(array $args)
    {
        $option = $args['option'];
        $value  = get_option($option);
    ?>
    <div class="notice notice-info inline">
    <p><?php echo esc_html($value) ?></p>
</div>
      <input type="hidden" name="<?php echo esc_attr($option) ?>" value="<?php echo esc_attr($value) ?>" />
      <?php
          }
          /**
           * Admin notices
           */
          add_action('admin_notices', 'chama_notice');

          function chama_notice()
          {
              // phpcs:ignore WordPress.Security.NonceVerification.Recommended
              $page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : '';

              // phpcs:ignore WordPress.Security.NonceVerification.Recommended
              $tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : '';

              // phpcs:ignore WordPress.Security.NonceVerification.Recommended
              $updated = isset($_GET['settings-updated']) ? sanitize_text_field(wp_unslash($_GET['settings-updated'])) : '';

              if (
                  $page === 'chama-settings' &&
                  in_array($tab, ['stripe', 'users', 'recaptcha', 'pages', 'content', 'display', 'branding', 'limits', 'email-settings', 'email-templates', 'newsletter', 'analytics', 'miscellaneous'], true) &&
                  $updated === 'true'
              ) {
                  echo wp_kses(chama_get_notice_success($tab), chama_allowed_html());
              }
          }
          function chama_sanitize_array($array)
          {
              return map_deep($array, 'sanitize_text_field');
          }
          function chama_sanitize_float($input)
          {
              return filter_var($input, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
          }
          function chama_sanitize_rich_text_editor($content)
          {
              //return $content;
              $allowed = [
                  'a'      => [
                      'class' => [],
                      'title' => [],
                      'href'  => [],
                      'rel'   => [],
                      'title' => [],
                  ],
                  'img'    => [
                      'src'    => [],
                      'alt'    => [],
                      'width'  => [],
                      'height' => [],
                      'class'  => [],
                  ],
                  'b'      => [],
                  'br'     => [],
                  'em'     => [],
                  'div'    => [
                      'class' => [],
                      'title' => [],
                      'style' => [],
                  ],
                  'h1'     => [],
                  'h2'     => [],
                  'h3'     => [],
                  'h4'     => [],
                  'h5'     => [],
                  'h6'     => [],
                  'i'      => [],
                  'li'     => [
                      'class' => [],
                  ],
                  'ol'     => [
                      'class' => [],
                  ],
                  'p'      => [
                      'class' => [],
                  ],
                  'span'   => [
                      'class' => [],
                      'title' => [],
                      'style' => [],
                  ],
                  'strike' => [],
                  'strong' => [],
                  'ul'     => [
                      'class' => [],
                  ],

              ];

              return wp_kses($content, $allowed);
          }
          /**
           * Chama get notice
           */
         function chama_get_notice_success($tab)
        {
            $notice  = '';
            $message = '';
            switch ($tab) {
                case 'stripe':
                    $message = 'Stripe settings saved.';
                    break;
                case 'recaptcha':
                    $message = 'Google reCAPTCHA settings saved.';
                    break;
                case 'users':
                    $message = 'Settings for the "Users" have been saved.';
                    break;
                case 'pages':
                    $message = 'Pages have been updated.';
                    break;
                case 'content':
                    $message = 'Content has been saved.';
                    break;
                case 'protection':
                    $message = 'Content protection settings have been saved.';
                    break;
                case 'display':
                    $message = 'Display settings have been saved.';
                    break;
                case 'branding':
                    $message = 'Branding settings have been saved.';
                    break;
                case 'limits':
                    $message = 'Payment limits have been saved.';
                    break;
                case 'email-settings':
                    $message = 'Email settings saved.';
                    break;
                case 'email-templates':
                    $message = 'Email templates have been saved.';
                    break;
                case 'newsletter':
                    $message = 'Newsletter settings saved.';
                    break;
                case 'miscellaneous':
                    $message = 'Miscellaneous settings saved.';
                    break;
                case 'analytics':
                    $message = 'Analytics have been saved.';
                    break;

            }

            $notice = sprintf(
                /* translators: %1$s message to be displayed when option is saved. */
                '<div class="notice notice-success is-dismissible"><p><strong>%1$s</strong></p></div>',
                $message
            );
            return $notice;
        }

          /**
           * Chama get default email template
           */
          function chama_get_default_email_template($option)
          {
              $message = '';
              switch ($option) {
                  case 'chama_donation_processing_template':
                      $message = sprintf(
                          esc_html__('Hello!', 'chama') . "\n\n" .
                          esc_html__('Thank you for your donation. Your payment is currently being processed and you will receive a final email receipt once it has completed. Your generosity is appreciated!',
                              'chama') . "\n\n" .
                          esc_html__('Here are the details of your donation:', 'chama') . "\n\n" .
                          '<strong>' . esc_html__('Donation Date:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Amount:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Receipt #:', 'chama') . '</strong>' . ' %s' . "\n\n" .
                          esc_html__('Sincerely ', 'chama') . "\n" .
                          get_bloginfo('name') . "\n"
                          , '{date}', '{amount}', '{receipt_id}');
                      break;
                  case 'chama_donation_receipt_template':
                      $message = sprintf(
                          esc_html__('Hello!', 'chama') . "\n\n" .
                          esc_html__('Thank you for your donation. Your generosity is appreciated! Here are the details of your donation:',
                              'chama') . "\n\n" .
                          '<strong>' . esc_html__('Donation Date:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Amount:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Receipt #:', 'chama') . '</strong>' . ' %s' . "\n\n" .
                          esc_html__('Sincerely ', 'chama') . "\n" .
                          get_bloginfo('name') . "\n"
                          , '{date}', '{amount}', '{receipt_id}');
                      break;
                  case 'chama_new_donation_template':
                      $message = sprintf(
                          esc_html__('Hi there,', 'chama') . "\n\n" .
                          esc_html__('This email is to inform you that a new donation has been made on your website:',
                              'chama') . "\n\n" .
                          '<strong>' . esc_html__('Donation Date:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Amount:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Receipt #:', 'chama') . '</strong>' . ' %s' . "\n\n" .
                          esc_html__('Thank you,', 'chama') . "\n" .
                          get_bloginfo('name') . "\n"
                          , '{date}', '{amount}', '{receipt_id}');
                      break;
                  case 'chama_subscription_processing_template':
                      $message = sprintf(
                          esc_html__('Hello!', 'chama') . "\n\n" .
                          esc_html__('Thank you for your donation. Your payment is currently being processed and you will receive a final email receipt once it has completed. Your generosity is appreciated!',
                              'chama') . "\n\n" .
                          esc_html__('Here are the details of your donation:', 'chama') . "\n\n" .
                          '<strong>' . esc_html__('Membership Tier:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Payment Date:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Renewal Date:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Amount:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Receipt #:', 'chama') . '</strong>' . ' %s' . "\n\n" .
                          esc_html__('Sincerely ', 'chama') . "\n" .
                          get_bloginfo('name') . "\n"
                          , '{description}', '{date}', '{renewal-date}', '{amount}', '{receipt_id}');
                      break;
                  case 'chama_subscription_receipt_template':
                      $message = sprintf(
                          esc_html__('Hello!', 'chama') . "\n\n" .
                          esc_html__('Thank you for your subscription. Your generosity is appreciated! Here are the details of your subscription:',
                              'chama') . "\n\n" .
                          '<strong>' . esc_html__('Membership Tier:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Payment Date:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Renewal Date:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Amount:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Receipt #:', 'chama') . '</strong>' . ' %s' . "\n\n" .
                          esc_html__('Sincerely ', 'chama') . "\n" .
                          get_bloginfo('name') . "\n"
                          , '{description}', '{date}', '{renewal-date}', '{amount}', '{receipt_id}');
                      break;
                  case 'chama_new_subscription_template':
                      $message = sprintf(
                          esc_html__('Hi there,', 'chama') . "\n\n" .
                          esc_html__('This email is to inform you that someone has signed up for a subscription on your website:',
                              'chama') . "\n\n" .
                          '<strong>' . esc_html__('Membership Tier:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Payment Date:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Renewal Date:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Amount:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Receipt #:', 'chama') . '</strong>' . ' %s' . "\n\n" .
                          esc_html__('Thank you,', 'chama') . "\n" .
                          get_bloginfo('name') . "\n"
                          , '{description}', '{date}', '{renewal-date}', '{amount}', '{receipt_id}');
                      break;
                  case 'chama_fund_processing_template':
                      $message = sprintf(
                          esc_html__('Hello!', 'chama') . "\n\n" .
                          esc_html__('Thank you for your donation. Your payment is currently being processed and you will receive a final email receipt once it has completed. Your generosity is appreciated!',
                              'chama') . "\n\n" .
                          esc_html__('Here are the details of your donation:', 'chama') . "\n\n" .
                          '<strong>' . esc_html__('Crowdfunding campaign:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Donation Date:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Amount:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Receipt #:', 'chama') . '</strong>' . ' %s' . "\n\n" .
                          esc_html__('Sincerely ', 'chama') . "\n" .
                          get_bloginfo('name') . "\n"
                          , '{description}', '{date}', '{amount}', '{receipt_id}');
                      break;
                  case 'chama_fund_receipt_template':
                      $message = sprintf(
                          esc_html__('Hello!', 'chama') . "\n\n" .
                          esc_html__('Thank you for your donation. Your generosity is appreciated! Here are the details of your donation:',
                              'chama') . "\n\n" .
                          '<strong>' . esc_html__('Crowdfunding campaign:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Donation Date:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Amount:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Receipt #:', 'chama') . '</strong>' . ' %s' . "\n\n" .
                          esc_html__('Sincerely ', 'chama') . "\n" .
                          get_bloginfo('name') . "\n"
                          , '{description}', '{date}', '{amount}', '{receipt_id}');
                      break;
                  case 'chama_new_fund_template':
                      $message = sprintf(
                          esc_html__('Hi there,', 'chama') . "\n\n" .
                          esc_html__('This email is to inform you that a new donation has been made to a crowdfunding campaign on your website:',
                              'chama') . "\n\n" .
                          '<strong>' . esc_html__('Crowdfunding campaign:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Donation Date:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Amount:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Receipt #:', 'chama') . '</strong>' . ' %s' . "\n\n" .
                          esc_html__('Thank you,', 'chama') . "\n" .
                          get_bloginfo('name') . "\n"
                          , '{description}', '{date}', '{amount}', '{receipt_id}');
                      break;
                  case 'chama_com_order_processing_template':
                      $message = sprintf(
                          esc_html__('Hello!', 'chama') . "\n\n" .
                          esc_html__('Thank you for your order. Your payment is currently being processed and you will receive a final email receipt once it has completed. Thank you for your business!',
                              'chama') . "\n\n" .
                          esc_html__('Here are the details of your order:', 'chama') . "\n\n" .
                          '<strong>' . esc_html__('Commission:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Commission Price:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Order Date:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Request:', 'chama') . '</strong>' . "\n" .
                          '%s' . "\n" .
                          '%s' . "\n" .
                          '<strong>' . esc_html__('Shipping Location:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Shipping Fee:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Total Amount:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Receipt #:', 'chama') . '</strong>' . ' %s' . "\n\n" .
                          esc_html__('Sincerely ', 'chama') . "\n" .
                          get_bloginfo('name') . "\n"
                          , '{description}', '{price}', '{date}', '{request}', '{add_ons}', '{shipping-location}', '{shipping-fee}', '{amount}', '{receipt_id}');
                      break;
                  case 'chama_com_order_receipt_template':
                      $message = sprintf(
                          esc_html__('Hello!', 'chama') . "\n\n" .
                          esc_html__('Thank you for your order! Here are the details of your order:',
                              'chama') . "\n\n" .
                          '<strong>' . esc_html__('Commission:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Commission Price:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Order Date:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Request:', 'chama') . '</strong>' . "\n" .
                          '%s' . "\n" .
                          '%s' . "\n" .
                          '<strong>' . esc_html__('Shipping Location:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Shipping Fee:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Total Amount:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Receipt #:', 'chama') . '</strong>' . ' %s' . "\n\n" .
                          esc_html__('Sincerely ', 'chama') . "\n" .
                          get_bloginfo('name') . "\n"
                          , '{description}', '{price}', '{date}', '{request}', '{add_ons}', '{shipping-location}', '{shipping-fee}', '{amount}', '{receipt_id}');
                      break;
                  case 'chama_new_commission_order_template':
                      $message = sprintf(
                          esc_html__('Hi there,', 'chama') . "\n\n" .
                          esc_html__('This email is to inform you that a new commission order has been made on your website:',
                              'chama') . "\n\n" .
                          '<strong>' . esc_html__('Commission:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Commission Price:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Order Date:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Request:', 'chama') . '</strong>' . "\n" .
                          '%s' . "\n" .
                          '%s' . "\n" .
                          '<strong>' . esc_html__('Shipping Location:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Shipping Fee:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Total Amount:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Receipt #:', 'chama') . '</strong>' . ' %s' . "\n\n" .
                          esc_html__('Thank you,', 'chama') . "\n" .
                          get_bloginfo('name') . "\n"
                          , '{description}', '{price}', '{date}', '{request}', '{add_ons}', '{shipping-location}', '{shipping-fee}', '{amount}', '{receipt_id}');
                      break;
                  case 'chama_payment_failure_template':
                      $message = sprintf(
                          esc_html__('Hello!', 'chama') . "\n\n" .
                          esc_html__('Thank you for your support. Unfortunately, we were unable to successfully complete payment for your subscription and your account is now past due. To continue enjoying your subscription benefits, please update your payment method.', 'chama') . "\n\n" .
                          esc_html__('To update the payment method on file, please log in to your account at the following link: ', 'chama') . '<a href="' . CHAMA_ACCOUNT_URL . '" target="_blank">' . esc_html__('Account Dashboard', 'chama') . '</a>. ' . esc_html__('If you need further assistance, please get in touch.', 'chama') . "\n\n" .
                          '<strong>' . esc_html__('Membership Tier:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Renewal Date:', 'chama') . '</strong>' . ' %s' . "\n" .
                          '<strong>' . esc_html__('Amount:', 'chama') . '</strong>' . ' %s' . "\n\n" .
                          esc_html__('Sincerely ', 'chama') . "\n" .
                          get_bloginfo('name') . "\n"
                          , '{description}', '{renewal-date}', '{amount}');
                      break;
                  case 'chama_subscription_cancellation_template':
                      $message = sprintf(
                          esc_html__('Hello!', 'chama') . "\n\n" .
                          esc_html__('We have succesfully cancelled your subscription to ', 'chama') .
                          get_bloginfo('name') . "\n\n" .
                          '%s' . "\n\n" .
                          esc_html__('Sincerely ', 'chama') . "\n" .
                          get_bloginfo('name') . "\n"
                          , '{description}');
                      break;
              }
              return $message;
          }
          /**
           * Chama icon
           */
          function chama_get_svg_icon_url()
          {
              return 'data:image/svg+xml;base64, ';
      }
